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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Device driver for the i2c thermostat found on the iBook G4, Albook G4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2003, 2004 Colin Leroy, Rasmus Rohde, Benjamin Herrenschmidt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Documentation from 115254175ADT7467_pra.pdf and 3686221171167ADT7460_b.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * https://www.onsemi.com/PowerSolutions/product.do?id=ADT7467
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * https://www.onsemi.com/PowerSolutions/product.do?id=ADT7460
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <asm/prom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <asm/machdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #undef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define CONFIG_REG   0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define MANUAL_MASK  0xe0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define AUTO_MASK    0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define INVERT_MASK  0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static u8 TEMP_REG[3]    = {0x26, 0x25, 0x27}; /* local, sensor1, sensor2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static u8 LIMIT_REG[3]   = {0x6b, 0x6a, 0x6c}; /* local, sensor1, sensor2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static u8 MANUAL_MODE[2] = {0x5c, 0x5d};       
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static u8 REM_CONTROL[2] = {0x00, 0x40};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static u8 FAN_SPEED[2]   = {0x28, 0x2a};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static u8 FAN_SPD_SET[2] = {0x30, 0x31};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static u8 default_limits_local[3] = {70, 50, 70};    /* local, sensor1, sensor2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static u8 default_limits_chip[3] = {80, 65, 80};    /* local, sensor1, sensor2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static const char *sensor_location[3] = { "?", "?", "?" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int limit_adjust;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int fan_speed = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static bool verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) MODULE_AUTHOR("Colin Leroy <colin@colino.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) MODULE_DESCRIPTION("Driver for ADT746x thermostat in iBook G4 and "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		   "Powerbook G4 Alu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) module_param(limit_adjust, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) MODULE_PARM_DESC(limit_adjust,"Adjust maximum temperatures (50 sensor1, 70 sensor2) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		 "by N degrees.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) module_param(fan_speed, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) MODULE_PARM_DESC(fan_speed,"Specify starting fan speed (0-255) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		 "(default 64)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) module_param(verbose, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) MODULE_PARM_DESC(verbose,"Verbose log operations "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		 "(default 0)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) struct thermostat {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct i2c_client	*clt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u8			temps[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	u8			cached_temp[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	u8			initial_limits[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	u8			limits[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int			last_speed[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int			last_var[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int			pwm_inv[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct task_struct	*thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct platform_device	*pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		ADT7460,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		ADT7467
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}			type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static void write_both_fan_speed(struct thermostat *th, int speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static void write_fan_speed(struct thermostat *th, int speed, int fan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) write_reg(struct thermostat* th, int reg, u8 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u8 tmp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	tmp[0] = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	tmp[1] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	rc = i2c_master_send(th->clt, (const char *)tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (rc != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) read_reg(struct thermostat* th, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	u8 reg_addr, data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	reg_addr = (u8)reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	rc = i2c_master_send(th->clt, &reg_addr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (rc != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	rc = i2c_master_recv(th->clt, (char *)&data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int read_fan_speed(struct thermostat *th, u8 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	u8 tmp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	u16 res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/* should start with low byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	tmp[1] = read_reg(th, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	tmp[0] = read_reg(th, addr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	res = tmp[1] + (tmp[0] << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	/* "a value of 0xffff means that the fan has stopped" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return (res == 0xffff ? 0 : (90000*60)/res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void write_both_fan_speed(struct thermostat *th, int speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	write_fan_speed(th, speed, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (th->type == ADT7460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		write_fan_speed(th, speed, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void write_fan_speed(struct thermostat *th, int speed, int fan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	u8 manual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (speed > 0xff) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		speed = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	else if (speed < -1) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		speed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (th->type == ADT7467 && fan == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (th->last_speed[fan] != speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		if (verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			if (speed == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				printk(KERN_DEBUG "adt746x: Setting speed to automatic "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 					"for %s fan.\n", sensor_location[fan+1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				printk(KERN_DEBUG "adt746x: Setting speed to %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					"for %s fan.\n", speed, sensor_location[fan+1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (speed >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		manual = read_reg(th, MANUAL_MODE[fan]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		manual &= ~INVERT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		write_reg(th, MANUAL_MODE[fan],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			manual | MANUAL_MASK | th->pwm_inv[fan]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		write_reg(th, FAN_SPD_SET[fan], speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		/* back to automatic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if(th->type == ADT7460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			manual = read_reg(th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				MANUAL_MODE[fan]) & (~MANUAL_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			manual &= ~INVERT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			manual |= th->pwm_inv[fan];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			write_reg(th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				MANUAL_MODE[fan], manual|REM_CONTROL[fan]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			manual = read_reg(th, MANUAL_MODE[fan]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			manual &= ~INVERT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			manual |= th->pwm_inv[fan];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			write_reg(th, MANUAL_MODE[fan], manual&(~AUTO_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	th->last_speed[fan] = speed;			
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void read_sensors(struct thermostat *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	for (i = 0; i < 3; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		th->temps[i]  = read_reg(th, TEMP_REG[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void display_stats(struct thermostat *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (th->temps[0] != th->cached_temp[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	||  th->temps[1] != th->cached_temp[1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	||  th->temps[2] != th->cached_temp[2]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		printk(KERN_INFO "adt746x: Temperature infos:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				 " thermostats: %d,%d,%d;"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				 " limits: %d,%d,%d;"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				 " fan speed: %d RPM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				 th->temps[0], th->temps[1], th->temps[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				 th->limits[0],  th->limits[1],  th->limits[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				 read_fan_speed(th, FAN_SPEED[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	th->cached_temp[0] = th->temps[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	th->cached_temp[1] = th->temps[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	th->cached_temp[2] = th->temps[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void update_fans_speed (struct thermostat *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	int lastvar = 0; /* last variation, for iBook */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* we don't care about local sensor, so we start at sensor 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	for (i = 1; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		bool started = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		int fan_number = (th->type == ADT7460 && i == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		int var = th->temps[i] - th->limits[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (var > -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			int step = (255 - fan_speed) / 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			int new_speed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			/* hysteresis : change fan speed only if variation is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			 * more than two degrees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			if (abs(var - th->last_var[fan_number]) < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			started = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			new_speed = fan_speed + ((var-1)*step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			if (new_speed < fan_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				new_speed = fan_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			if (new_speed > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				new_speed = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			if (verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 				printk(KERN_DEBUG "adt746x: Setting fans speed to %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 						 "(limit exceeded by %d on %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 						new_speed, var,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 						sensor_location[fan_number+1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			write_both_fan_speed(th, new_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			th->last_var[fan_number] = var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		} else if (var < -2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			/* don't stop fan if sensor2 is cold and sensor1 is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			 * so cold (lastvar >= -1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			if (i == 2 && lastvar < -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				if (th->last_speed[fan_number] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 					if (verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 						printk(KERN_DEBUG "adt746x: Stopping "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 							"fans.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				write_both_fan_speed(th, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		lastvar = var;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		if (started)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			return; /* we don't want to re-stop the fan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				* if sensor1 is heating and sensor2 is not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int monitor_task(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct thermostat* th = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	set_freezable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	while(!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		try_to_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		msleep_interruptible(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #ifndef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (fan_speed != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			read_sensors(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		read_sensors(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) #endif		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if (fan_speed != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			update_fans_speed(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		display_stats(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static void set_limit(struct thermostat *th, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	/* Set sensor1 limit higher to avoid powerdowns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	th->limits[i] = default_limits_chip[i] + limit_adjust;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	write_reg(th, LIMIT_REG[i], th->limits[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	/* set our limits to normal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	th->limits[i] = default_limits_local[i] + limit_adjust;
^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) #define BUILD_SHOW_FUNC_INT(name, data)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct thermostat *th = dev_get_drvdata(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return sprintf(buf, "%d\n", data);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) #define BUILD_SHOW_FUNC_INT_LITE(name, data)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return sprintf(buf, "%d\n", data);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) #define BUILD_SHOW_FUNC_STR(name, data)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	return sprintf(buf, "%s\n", data);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #define BUILD_SHOW_FUNC_FAN(name, data)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf)       \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct thermostat *th = dev_get_drvdata(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	return sprintf(buf, "%d (%d rpm)\n", 			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		th->last_speed[data],				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		read_fan_speed(th, FAN_SPEED[data])		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		);						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) #define BUILD_STORE_FUNC_DEG(name, data)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct thermostat *th = dev_get_drvdata(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	int val;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	int i;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	val = simple_strtol(buf, NULL, 10);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	printk(KERN_INFO "Adjusting limits by %d degrees\n", val);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	limit_adjust = val;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	for (i=0; i < 3; i++)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		set_limit(th, i);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return n;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) #define BUILD_STORE_FUNC_INT(name, data)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static ssize_t store_##name(struct device *dev, struct device_attribute *attr, const char *buf, size_t n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int val;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	val = simple_strtol(buf, NULL, 10);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (val < 0 || val > 255)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		return -EINVAL;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	printk(KERN_INFO "Setting specified fan speed to %d\n", val);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	data = val;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	return n;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) BUILD_SHOW_FUNC_INT(sensor1_temperature,	 (read_reg(th, TEMP_REG[1])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) BUILD_SHOW_FUNC_INT(sensor2_temperature,	 (read_reg(th, TEMP_REG[2])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) BUILD_SHOW_FUNC_INT(sensor1_limit,		 th->limits[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) BUILD_SHOW_FUNC_INT(sensor2_limit,		 th->limits[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) BUILD_SHOW_FUNC_STR(sensor1_location,		 sensor_location[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) BUILD_SHOW_FUNC_STR(sensor2_location,		 sensor_location[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) BUILD_SHOW_FUNC_INT_LITE(specified_fan_speed, fan_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) BUILD_STORE_FUNC_INT(specified_fan_speed,fan_speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) BUILD_SHOW_FUNC_FAN(sensor1_fan_speed,	 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) BUILD_SHOW_FUNC_FAN(sensor2_fan_speed,	 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) BUILD_SHOW_FUNC_INT_LITE(limit_adjust,	 limit_adjust)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) BUILD_STORE_FUNC_DEG(limit_adjust,	 th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static DEVICE_ATTR(sensor1_temperature,	S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		   show_sensor1_temperature,NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static DEVICE_ATTR(sensor2_temperature,	S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		   show_sensor2_temperature,NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static DEVICE_ATTR(sensor1_limit, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		   show_sensor1_limit,	NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static DEVICE_ATTR(sensor2_limit, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		   show_sensor2_limit,	NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static DEVICE_ATTR(sensor1_location, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		   show_sensor1_location, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static DEVICE_ATTR(sensor2_location, S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		   show_sensor2_location, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static DEVICE_ATTR(specified_fan_speed,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		   show_specified_fan_speed,store_specified_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static DEVICE_ATTR(sensor1_fan_speed,	S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		   show_sensor1_fan_speed,	NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static DEVICE_ATTR(sensor2_fan_speed,	S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		   show_sensor2_fan_speed,	NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static DEVICE_ATTR(limit_adjust,	S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		   show_limit_adjust,	store_limit_adjust);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static void thermostat_create_files(struct thermostat *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	struct device_node *np = th->clt->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	/* To maintain ABI compatibility with userspace, create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	 * the old style platform driver and attach the attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	 * to it here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	th->pdev = of_platform_device_create(np, "temperatures", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (!th->pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	dev = &th->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	dev_set_drvdata(dev, th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	err = device_create_file(dev, &dev_attr_sensor1_temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	err |= device_create_file(dev, &dev_attr_sensor2_temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	err |= device_create_file(dev, &dev_attr_sensor1_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	err |= device_create_file(dev, &dev_attr_sensor2_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	err |= device_create_file(dev, &dev_attr_sensor1_location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	err |= device_create_file(dev, &dev_attr_sensor2_location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	err |= device_create_file(dev, &dev_attr_limit_adjust);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	err |= device_create_file(dev, &dev_attr_specified_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	err |= device_create_file(dev, &dev_attr_sensor1_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if(th->type == ADT7460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		err |= device_create_file(dev, &dev_attr_sensor2_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			"Failed to create temperature attribute file(s).\n");
^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 thermostat_remove_files(struct thermostat *th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (!th->pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	dev = &th->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	device_remove_file(dev, &dev_attr_sensor1_temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	device_remove_file(dev, &dev_attr_sensor2_temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	device_remove_file(dev, &dev_attr_sensor1_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	device_remove_file(dev, &dev_attr_sensor2_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	device_remove_file(dev, &dev_attr_sensor1_location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	device_remove_file(dev, &dev_attr_sensor2_location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	device_remove_file(dev, &dev_attr_limit_adjust);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	device_remove_file(dev, &dev_attr_specified_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	device_remove_file(dev, &dev_attr_sensor1_fan_speed);	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (th->type == ADT7460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		device_remove_file(dev, &dev_attr_sensor2_fan_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	of_device_unregister(th->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static int probe_thermostat(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			    const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	struct device_node *np = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	struct thermostat* th;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	int i, rc, vers, offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	prop = of_get_property(np, "hwsensor-params-version", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	vers = be32_to_cpup(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	printk(KERN_INFO "adt746x: version %d (%ssupported)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	       vers, vers == 1 ? "" : "un");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (vers != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (of_get_property(np, "hwsensor-location", NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			sensor_location[i] = of_get_property(np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 					"hwsensor-location", NULL) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			if (sensor_location[i] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 				sensor_location[i] = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			printk(KERN_INFO "sensor %d: %s\n", i, sensor_location[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			offset += strlen(sensor_location[i]) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	th = kzalloc(sizeof(struct thermostat), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (!th)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	i2c_set_clientdata(client, th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	th->clt = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	th->type = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	rc = read_reg(th, CONFIG_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		dev_err(&client->dev, "Thermostat failed to read config!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		kfree(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	/* force manual control to start the fan quieter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (fan_speed == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		fan_speed = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (th->type == ADT7460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		printk(KERN_INFO "adt746x: ADT7460 initializing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		/* The 7460 needs to be started explicitly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		write_reg(th, CONFIG_REG, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		printk(KERN_INFO "adt746x: ADT7467 initializing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		th->initial_limits[i] = read_reg(th, LIMIT_REG[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		set_limit(th, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	printk(KERN_INFO "adt746x: Lowering max temperatures from %d, %d, %d"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			 " to %d, %d, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			 th->initial_limits[0], th->initial_limits[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			 th->initial_limits[2], th->limits[0], th->limits[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			 th->limits[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	/* record invert bit status because fw can corrupt it after suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	th->pwm_inv[0] = read_reg(th, MANUAL_MODE[0]) & INVERT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	th->pwm_inv[1] = read_reg(th, MANUAL_MODE[1]) & INVERT_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	/* be sure to really write fan speed the first time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	th->last_speed[0] = -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	th->last_speed[1] = -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	th->last_var[0] = -80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	th->last_var[1] = -80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (fan_speed != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		/* manual mode, stop fans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		write_both_fan_speed(th, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		/* automatic mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		write_both_fan_speed(th, -1);
^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) 	th->thread = kthread_run(monitor_task, th, "kfand");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (th->thread == ERR_PTR(-ENOMEM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		printk(KERN_INFO "adt746x: Kthread creation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		th->thread = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	thermostat_create_files(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) static int remove_thermostat(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	struct thermostat *th = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	thermostat_remove_files(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	if (th->thread != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		kthread_stop(th->thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	printk(KERN_INFO "adt746x: Putting max temperatures back from "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			 "%d, %d, %d to %d, %d, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		th->limits[0], th->limits[1], th->limits[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		th->initial_limits[0], th->initial_limits[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		th->initial_limits[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	for (i = 0; i < 3; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		write_reg(th, LIMIT_REG[i], th->initial_limits[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	write_both_fan_speed(th, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	kfree(th);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static const struct i2c_device_id therm_adt746x_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	{ "MAC,adt7460", ADT7460 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	{ "MAC,adt7467", ADT7467 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) MODULE_DEVICE_TABLE(i2c, therm_adt746x_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static struct i2c_driver thermostat_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		.name	= "therm_adt746x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	.probe = probe_thermostat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	.remove = remove_thermostat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	.id_table = therm_adt746x_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int __init thermostat_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) #ifndef CONFIG_I2C_POWERMAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	request_module("i2c-powermac");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return i2c_add_driver(&thermostat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static void __exit thermostat_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	i2c_del_driver(&thermostat_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) module_init(thermostat_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) module_exit(thermostat_exit);