Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* bbc_envctrl.c: UltraSPARC-III environment control driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/oplib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "bbc_i2c.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "max1617.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #undef ENVCTRL_TRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /* WARNING: Making changes to this driver is very dangerous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *          If you misprogram the sensor chips they can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *          cut the power on you instantly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) /* Two temperature sensors exist in the SunBLADE-1000 enclosure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Both are implemented using max1617 i2c devices.  Each max1617
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * monitors 2 temperatures, one for one of the cpu dies and the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * for the ambient temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * The max1617 is capable of being programmed with power-off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * temperature values, one low limit and one high limit.  These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * can be controlled independently for the cpu or ambient temperature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * If a limit is violated, the power is simply shut off.  The frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * with which the max1617 does temperature sampling can be controlled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * Three fans exist inside the machine, all three are controlled with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * an i2c digital to analog converter.  There is a fan directed at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * two processor slots, another for the rest of the enclosure, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * third is for the power supply.  The first two fans may be speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * controlled by changing the voltage fed to them.  The third fan may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * only be completely off or on.  The third fan is meant to only be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * disabled/enabled when entering/exiting the lowest power-saving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * mode of the machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * An environmental control kernel thread periodically monitors all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * temperature sensors.  Based upon the samples it will adjust the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * fan speeds to try and keep the system within a certain temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * range (the goal being to make the fans as quiet as possible without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * allowing the system to get too hot).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * If the temperature begins to rise/fall outside of the acceptable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * operating range, a periodic warning will be sent to the kernel log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * The fans will be put on full blast to attempt to deal with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * situation.  After exceeding the acceptable operating range by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * certain threshold, the kernel thread will shut down the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * Here, the thread is attempting to shut the machine down cleanly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * before the hardware based power-off event is triggered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /* These settings are in Celsius.  We use these defaults only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * if we cannot interrogate the cpu-fru SEEPROM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) struct temp_limits {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	s8 high_pwroff, high_shutdown, high_warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	s8 low_warn, low_shutdown, low_pwroff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static struct temp_limits cpu_temp_limits[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	{ 100, 85, 80, 5, -5, -10 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	{ 100, 85, 80, 5, -5, -10 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static struct temp_limits amb_temp_limits[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	{ 65, 55, 40, 5, -5, -10 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	{ 65, 55, 40, 5, -5, -10 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static LIST_HEAD(all_temps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static LIST_HEAD(all_fans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define CPU_FAN_REG	0xf0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define SYS_FAN_REG	0xf2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define PSUPPLY_FAN_REG	0xf4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define FAN_SPEED_MIN	0x0c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define FAN_SPEED_MAX	0x3f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define PSUPPLY_FAN_ON	0x1f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define PSUPPLY_FAN_OFF	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void set_fan_speeds(struct bbc_fan_control *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	/* Put temperatures into range so we don't mis-program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (fp->cpu_fan_speed < FAN_SPEED_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		fp->cpu_fan_speed = FAN_SPEED_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (fp->cpu_fan_speed > FAN_SPEED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		fp->cpu_fan_speed = FAN_SPEED_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (fp->system_fan_speed < FAN_SPEED_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		fp->system_fan_speed = FAN_SPEED_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (fp->system_fan_speed > FAN_SPEED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		fp->system_fan_speed = FAN_SPEED_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #ifdef ENVCTRL_TRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	       fp->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	       fp->cpu_fan_speed, fp->system_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	bbc_i2c_writeb(fp->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		       (fp->psupply_fan_on ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		       PSUPPLY_FAN_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void get_current_temps(struct bbc_cpu_temperature *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	tp->prev_amb_temp = tp->curr_amb_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	bbc_i2c_readb(tp->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		      (unsigned char *) &tp->curr_amb_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		      MAX1617_AMB_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	tp->prev_cpu_temp = tp->curr_cpu_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	bbc_i2c_readb(tp->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		      (unsigned char *) &tp->curr_cpu_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		      MAX1617_CPU_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #ifdef ENVCTRL_TRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	printk("temp%d: cpu(%d C) amb(%d C)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	       tp->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	       (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	static int shutting_down = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	char *type = "???";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	s8 val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (shutting_down != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	    tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		type = "ambient";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		val = tp->curr_amb_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		   tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		type = "CPU";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		val = tp->curr_cpu_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	printk(KERN_CRIT "temp%d: Outside of safe %s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	       "operating temperature, %d C.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	       tp->index, type, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	shutting_down = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	orderly_poweroff(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define WARN_INTERVAL	(30 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		if (tp->curr_amb_temp >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		    amb_temp_limits[tp->index].high_warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			printk(KERN_WARNING "temp%d: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			       "Above safe ambient operating temperature, %d C.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			       tp->index, (int) tp->curr_amb_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		} else if (tp->curr_amb_temp <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			   amb_temp_limits[tp->index].low_warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			printk(KERN_WARNING "temp%d: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			       "Below safe ambient operating temperature, %d C.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			       tp->index, (int) tp->curr_amb_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			*last_warn = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	} else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		   tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	/* Now check the shutdown limits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	    tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		do_envctrl_shutdown(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	} else if ((tick & (8 - 1)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		s8 amb_goal_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		amb_goal_lo = amb_goal_hi - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		/* We do not try to avoid 'too cold' events.  Basically we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		 * only try to deal with over-heating and fan noise reduction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (tp->avg_amb_temp < amb_goal_hi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			if (tp->avg_amb_temp >= amb_goal_lo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			tp->fan_todo[FAN_AMBIENT] = FAN_FASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		if (tp->curr_cpu_temp >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		    cpu_temp_limits[tp->index].high_warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			printk(KERN_WARNING "temp%d: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			       "Above safe CPU operating temperature, %d C.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			       tp->index, (int) tp->curr_cpu_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		} else if (tp->curr_cpu_temp <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			   cpu_temp_limits[tp->index].low_warn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			printk(KERN_WARNING "temp%d: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			       "Below safe CPU operating temperature, %d C.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			       tp->index, (int) tp->curr_cpu_temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			*last_warn = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	} else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		   tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	/* Now check the shutdown limits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	    tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		do_envctrl_shutdown(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		tp->fan_todo[FAN_CPU] = FAN_FULLBLAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	} else if ((tick & (8 - 1)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		s8 cpu_goal_lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		cpu_goal_lo = cpu_goal_hi - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		/* We do not try to avoid 'too cold' events.  Basically we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		 * only try to deal with over-heating and fan noise reduction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (tp->avg_cpu_temp < cpu_goal_hi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			if (tp->avg_cpu_temp >= cpu_goal_lo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				tp->fan_todo[FAN_CPU] = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				tp->fan_todo[FAN_CPU] = FAN_SLOWER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			tp->fan_todo[FAN_CPU] = FAN_FASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		tp->fan_todo[FAN_CPU] = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	analyze_ambient_temp(tp, last_warn, tp->sample_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	analyze_cpu_temp(tp, last_warn, tp->sample_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	tp->sample_tick++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static enum fan_action prioritize_fan_action(int which_fan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct bbc_cpu_temperature *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	enum fan_action decision = FAN_STATE_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	/* Basically, prioritize what the temperature sensors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 * recommend we do, and perform that action on all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 * fans.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	list_for_each_entry(tp, &all_temps, glob_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (tp->fan_todo[which_fan] == FAN_FULLBLAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			decision = FAN_FULLBLAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if (tp->fan_todo[which_fan] == FAN_SAME &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		    decision != FAN_FASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			decision = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		else if (tp->fan_todo[which_fan] == FAN_FASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			decision = FAN_FASTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		else if (decision != FAN_FASTER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			 decision != FAN_SAME &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			 tp->fan_todo[which_fan] == FAN_SLOWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			decision = FAN_SLOWER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (decision == FAN_STATE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		decision = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return decision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	enum fan_action decision = prioritize_fan_action(FAN_AMBIENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (decision == FAN_SAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (decision == FAN_FULLBLAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		if (fp->system_fan_speed >= FAN_SPEED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			fp->system_fan_speed = FAN_SPEED_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		if (decision == FAN_FASTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			if (fp->system_fan_speed >= FAN_SPEED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				fp->system_fan_speed += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			int orig_speed = fp->system_fan_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			if (orig_speed <= FAN_SPEED_MIN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			    orig_speed <= (fp->cpu_fan_speed - 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				fp->system_fan_speed -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	enum fan_action decision = prioritize_fan_action(FAN_CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (decision == FAN_SAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (decision == FAN_FULLBLAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			fp->cpu_fan_speed = FAN_SPEED_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		if (decision == FAN_FASTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				fp->cpu_fan_speed += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				if (fp->system_fan_speed <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				    (fp->cpu_fan_speed - 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 					fp->system_fan_speed =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 						fp->cpu_fan_speed - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			if (fp->cpu_fan_speed <= FAN_SPEED_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				fp->cpu_fan_speed -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static void maybe_new_fan_speeds(struct bbc_fan_control *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	new  = maybe_new_ambient_fan_speed(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	new |= maybe_new_cpu_fan_speed(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		set_fan_speeds(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static void fans_full_blast(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct bbc_fan_control *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	/* Since we will not be monitoring things anymore, put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	 * the fans on full blast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	list_for_each_entry(fp, &all_fans, glob_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		fp->cpu_fan_speed = FAN_SPEED_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		fp->system_fan_speed = FAN_SPEED_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		fp->psupply_fan_on = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		set_fan_speeds(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) #define POLL_INTERVAL	(5 * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static unsigned long last_warning_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static struct task_struct *kenvctrld_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int kenvctrld(void *__unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	last_warning_jiffies = jiffies - WARN_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		struct bbc_cpu_temperature *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		struct bbc_fan_control *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		msleep_interruptible(POLL_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		if (kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		list_for_each_entry(tp, &all_temps, glob_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			get_current_temps(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			analyze_temps(tp, &last_warning_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		list_for_each_entry(fp, &all_fans, glob_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			maybe_new_fan_speeds(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	fans_full_blast();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void attach_one_temp(struct bbc_i2c_bus *bp, struct platform_device *op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			    int temp_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct bbc_cpu_temperature *tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	tp = kzalloc(sizeof(*tp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (!tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	INIT_LIST_HEAD(&tp->bp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	INIT_LIST_HEAD(&tp->glob_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	tp->client = bbc_i2c_attach(bp, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (!tp->client) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		kfree(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	tp->index = temp_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	list_add(&tp->glob_list, &all_temps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	list_add(&tp->bp_list, &bp->temps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	/* Tell it to convert once every 5 seconds, clear all cfg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	 * bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	/* Program the hard temperature limits into the chip. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		       MAX1617_WR_AMB_HIGHLIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		       MAX1617_WR_AMB_LOWLIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		       MAX1617_WR_CPU_HIGHLIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		       MAX1617_WR_CPU_LOWLIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	get_current_temps(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	tp->fan_todo[FAN_CPU] = FAN_SAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static void attach_one_fan(struct bbc_i2c_bus *bp, struct platform_device *op,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			   int fan_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	struct bbc_fan_control *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	fp = kzalloc(sizeof(*fp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	INIT_LIST_HEAD(&fp->bp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	INIT_LIST_HEAD(&fp->glob_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	fp->client = bbc_i2c_attach(bp, op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (!fp->client) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		kfree(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	fp->index = fan_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	list_add(&fp->glob_list, &all_fans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	list_add(&fp->bp_list, &bp->fans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	/* The i2c device controlling the fans is write-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	 * So the only way to keep track of the current power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	 * level fed to the fans is via software.  Choose half
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	 * power for cpu/system and 'on' fo the powersupply fan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	 * and set it now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	fp->psupply_fan_on = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	fp->cpu_fan_speed += FAN_SPEED_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	fp->system_fan_speed += FAN_SPEED_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	set_fan_speeds(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void destroy_one_temp(struct bbc_cpu_temperature *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	bbc_i2c_detach(tp->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	kfree(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static void destroy_all_temps(struct bbc_i2c_bus *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	struct bbc_cpu_temperature *tp, *tpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	list_for_each_entry_safe(tp, tpos, &bp->temps, bp_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		list_del(&tp->bp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		list_del(&tp->glob_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		destroy_one_temp(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static void destroy_one_fan(struct bbc_fan_control *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	bbc_i2c_detach(fp->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	kfree(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static void destroy_all_fans(struct bbc_i2c_bus *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	struct bbc_fan_control *fp, *fpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	list_for_each_entry_safe(fp, fpos, &bp->fans, bp_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		list_del(&fp->bp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		list_del(&fp->glob_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		destroy_one_fan(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int bbc_envctrl_init(struct bbc_i2c_bus *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	struct platform_device *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	int temp_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	int fan_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	int devidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	while ((op = bbc_i2c_getdev(bp, devidx++)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		if (of_node_name_eq(op->dev.of_node, "temperature"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			attach_one_temp(bp, op, temp_index++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		if (of_node_name_eq(op->dev.of_node, "fan-control"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			attach_one_fan(bp, op, fan_index++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (temp_index != 0 && fan_index != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		if (IS_ERR(kenvctrld_task)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			int err = PTR_ERR(kenvctrld_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			kenvctrld_task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			destroy_all_temps(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			destroy_all_fans(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	if (kenvctrld_task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		kthread_stop(kenvctrld_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	destroy_all_temps(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	destroy_all_fans(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }