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) /* 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  *   Creation Date: <2003/03/14 20:54:13 samuel>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *   Time-stamp: <2004/03/20 14:20:59 samuel>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *   
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	<therm_windtunnel.c>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	The G4 "windtunnel" has a single fan controlled by an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	ADM1030 fan controller and a DS1775 thermostat.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	The fan controller is equipped with a temperature sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	which measures the case temperature. The DS1775 sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *	measures the CPU temperature. This driver tunes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *	behavior of the fan. It is based upon empirical observations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *	of the 'AppleFan' driver under Mac OS X.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *	WARNING: This driver has only been testen on Apple's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *	1.25 MHz Dual G4 (March 03). It is tuned for a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *	temperature around 57 C.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *   Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *   Loosely based upon 'thermostat.c' written by Benjamin Herrenschmidt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *   
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *   This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *   modify it under the terms of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *   as published by the Free Software Foundation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *   
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <asm/prom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <asm/machdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <asm/macio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define LOG_TEMP		0			/* continuously log temperature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	volatile int		running;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct task_struct	*poll_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct mutex	 	lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct platform_device	*of_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct i2c_client	*thermostat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct i2c_client	*fan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int			overheat_temp;		/* 100% fan at this temp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int			overheat_hyst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int			temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int			casetemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int			fan_level;		/* active fan_table setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int			downind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	int			upind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int			r0, r1, r20, r23, r25;	/* saved register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) } x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define T(x,y)			(((x)<<8) | (y)*0x100/10 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int			fan_down_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int			temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int			fan_up_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) } fan_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	{ 11, T(0,0),  11 },	/* min fan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	{ 11, T(55,0), 11 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	{  6, T(55,3), 11 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	{  7, T(56,0), 11 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	{  8, T(57,0), 8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	{  7, T(58,3), 7 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	{  6, T(58,8), 6 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	{  5, T(59,2), 5 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	{  4, T(59,6), 4 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{  3, T(59,9), 3 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{  2, T(60,1), 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{  1, 0xfffff, 1 }	/* on fire */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) print_temp( const char *s, int temp )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
^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 DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*	controller thread						*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) write_reg( struct i2c_client *cl, int reg, int data, int len )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	u8 tmp[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if( len < 1 || len > 2 || data < 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	tmp[0] = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	tmp[1] = (len == 1) ? data : (data >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	tmp[2] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if( i2c_master_send(cl, tmp, len) != len )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return 0;
^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) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) read_reg( struct i2c_client *cl, int reg, int len )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	u8 buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if( len != 1 && len != 2 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	buf[0] = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if( i2c_master_send(cl, buf, 1) != 1 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if( i2c_master_recv(cl, buf, len) != len )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) tune_fan( int fan_setting )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int val = (fan_setting << 3) | 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/* write_reg( x.fan, 0x24, val, 1 ); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	write_reg( x.fan, 0x25, val, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	write_reg( x.fan, 0x20, 0, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	print_temp("CPU-temp: ", x.temp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if( x.casetemp )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		print_temp(", Case: ", x.casetemp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	printk(",  Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	x.fan_level = fan_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) poll_temp( void )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int temp, i, level, casetemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	temp = read_reg( x.thermostat, 0, 2 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	/* this actually occurs when the computer is loaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if( temp < 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	casetemp = read_reg(x.fan, 0x0b, 1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if( LOG_TEMP && x.temp != temp ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		print_temp("CPU-temp: ", temp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		print_temp(", Case: ", casetemp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		printk(",  Fan: %d\n", 11-x.fan_level );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	x.temp = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	x.casetemp = casetemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	level = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if( i < x.downind )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		level = fan_table[i].fan_down_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	x.downind = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if( x.upind < i )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		level = fan_table[i].fan_up_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	x.upind = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if( level >= 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		tune_fan( level );
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) setup_hardware( void )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	/* save registers (if we unload the module) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	x.r0 = read_reg( x.fan, 0x00, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	x.r1 = read_reg( x.fan, 0x01, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	x.r20 = read_reg( x.fan, 0x20, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	x.r23 = read_reg( x.fan, 0x23, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	x.r25 = read_reg( x.fan, 0x25, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/* improve measurement resolution (convergence time 1.5s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		val |= 0x60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		if( write_reg( x.thermostat, 1, val, 1 ) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			printk("Failed writing config register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	/* disable interrupts and TAC input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	write_reg( x.fan, 0x01, 0x01, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	/* enable filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	write_reg( x.fan, 0x23, 0x91, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	/* remote temp. controls fan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	write_reg( x.fan, 0x00, 0x95, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	/* The thermostat (which besides measureing temperature controls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * has a THERM output which puts the fan on 100%) is usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * set to kick in at 80 C (chip default). We reduce this a bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * to be on the safe side (OSX doesn't)...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if( x.overheat_temp == (80 << 8) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		x.overheat_temp = 75 << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		x.overheat_hyst = 70 << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		write_reg( x.thermostat, 3, x.overheat_temp, 2 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		print_temp("Reducing overheating limit to ", x.overheat_temp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		print_temp(" (Hyst: ", x.overheat_hyst );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		printk(")\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* set an initial fan setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	x.downind = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	x.upind = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/* tune_fan( fan_up_table[x.upind].fan_setting ); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	err = device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	err |= device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			"Failed to create temperature attribute file(s).\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) restore_regs( void )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	write_reg( x.fan, 0x01, x.r1, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	write_reg( x.fan, 0x20, x.r20, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	write_reg( x.fan, 0x23, x.r23, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	write_reg( x.fan, 0x25, x.r25, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	write_reg( x.fan, 0x00, x.r0, 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int control_loop(void *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	mutex_lock(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	setup_hardware();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	mutex_unlock(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		msleep_interruptible(8000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		mutex_lock(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		poll_temp();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		mutex_unlock(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	mutex_lock(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	restore_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	mutex_unlock(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /*	i2c probing and setup						*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static void do_attach(struct i2c_adapter *adapter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct i2c_board_info info = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	/* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	static const unsigned short scan_ds1775[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		I2C_CLIENT_END
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	static const unsigned short scan_adm1030[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		0x2c, 0x2d, 0x2e, 0x2f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		I2C_CLIENT_END
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (x.running || strncmp(adapter->name, "uni-n", 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	np = of_find_compatible_node(adapter->dev.of_node, NULL, "MAC,ds1775");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		strlcpy(info.type, "MAC,ds1775", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		i2c_new_scanned_device(adapter, &info, scan_ds1775, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	np = of_find_compatible_node(adapter->dev.of_node, NULL, "MAC,adm1030");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		strlcpy(info.type, "MAC,adm1030", I2C_NAME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		i2c_new_scanned_device(adapter, &info, scan_adm1030, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) do_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (x.running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		x.running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		kthread_stop(x.poll_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		x.poll_task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (client == x.thermostat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		x.thermostat = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	else if (client == x.fan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		x.fan = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		printk(KERN_ERR "g4fan: bad client\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) attach_fan( struct i2c_client *cl )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if( x.fan )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	/* check that this is an ADM1030 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	printk("ADM1030 fan controller [@%02x]\n", cl->addr );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	x.fan = cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) attach_thermostat( struct i2c_client *cl ) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	int hyst_temp, os_temp, temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if( x.thermostat )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if( (temp=read_reg(cl, 0, 2)) < 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* temperature sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if( temp < 0x1600 || temp > 0x3c00 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	hyst_temp = read_reg(cl, 2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	os_temp = read_reg(cl, 3, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if( hyst_temp < 0 || os_temp < 0 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	printk("DS1775 digital thermometer [@%02x]\n", cl->addr );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	print_temp("Temp: ", temp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	print_temp("  Hyst: ", hyst_temp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	print_temp("  OS: ", os_temp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	printk("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	x.temp = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	x.overheat_temp = os_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	x.overheat_hyst = hyst_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	x.thermostat = cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) enum chip { ds1775, adm1030 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static const struct i2c_device_id therm_windtunnel_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	{ "MAC,ds1775", ds1775 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	{ "MAC,adm1030", adm1030 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) MODULE_DEVICE_TABLE(i2c, therm_windtunnel_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) do_probe(struct i2c_client *cl, const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	struct i2c_adapter *adapter = cl->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				     | I2C_FUNC_SMBUS_WRITE_BYTE) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	switch (id->driver_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	case adm1030:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		ret = attach_fan(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	case ds1775:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		ret = attach_thermostat(cl);
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (!x.running && x.thermostat && x.fan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		x.running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		x.poll_task = kthread_run(control_loop, NULL, "g4fand");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static struct i2c_driver g4fan_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		.name	= "therm_windtunnel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	.probe		= do_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	.remove		= do_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	.id_table	= therm_windtunnel_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /*	initialization / cleanup					*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static int therm_of_probe(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct i2c_adapter *adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	int ret, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	adap = i2c_get_adapter(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (!adap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	ret = i2c_add_driver(&g4fan_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		i2c_put_adapter(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	/* We assume Macs have consecutive I2C bus numbers starting at 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	while (adap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		do_attach(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		if (x.running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		i2c_put_adapter(adap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		adap = i2c_get_adapter(++i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) therm_of_remove( struct platform_device *dev )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	i2c_del_driver( &g4fan_driver );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) static const struct of_device_id therm_of_match[] = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	.name		= "fan",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	.compatible	= "adm1030"
^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) MODULE_DEVICE_TABLE(of, therm_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) static struct platform_driver therm_of_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		.name = "temperature",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		.of_match_table = therm_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	.probe		= therm_of_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	.remove		= therm_of_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct apple_thermal_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	u8		id;			/* implementation ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	u8		fan_count;		/* number of fans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	u8		thermostat_count;	/* number of thermostats */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	u8		unused;
^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) static int __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) g4fan_init( void )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	const struct apple_thermal_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	mutex_init(&x.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	info = of_get_property(np, "thermal-info", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if( !info || !of_machine_is_compatible("PowerMac3,6") )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	if( info->id != 3 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if( !(np=of_find_node_by_name(NULL, "fan")) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	x.of_dev = of_platform_device_create(np, "temperature", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	of_node_put( np );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if( !x.of_dev ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		printk(KERN_ERR "Can't register fan controller!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	platform_driver_register( &therm_of_driver );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static void __exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) g4fan_exit( void )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	platform_driver_unregister( &therm_of_driver );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if( x.of_dev )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		of_device_unregister( x.of_dev );
^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) module_init(g4fan_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) module_exit(g4fan_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) MODULE_LICENSE("GPL");