Version:  2.0.40 2.2.26 2.4.37 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6

Linux/drivers/macintosh/therm_windtunnel.c

  1 /* 
  2  *   Creation Date: <2003/03/14 20:54:13 samuel>
  3  *   Time-stamp: <2004/03/20 14:20:59 samuel>
  4  *   
  5  *      <therm_windtunnel.c>
  6  *      
  7  *      The G4 "windtunnel" has a single fan controlled by an
  8  *      ADM1030 fan controller and a DS1775 thermostat.
  9  *
 10  *      The fan controller is equipped with a temperature sensor
 11  *      which measures the case temperature. The DS1775 sensor
 12  *      measures the CPU temperature. This driver tunes the
 13  *      behavior of the fan. It is based upon empirical observations
 14  *      of the 'AppleFan' driver under Mac OS X.
 15  *
 16  *      WARNING: This driver has only been testen on Apple's
 17  *      1.25 MHz Dual G4 (March 03). It is tuned for a CPU
 18  *      temperature around 57 C.
 19  *
 20  *   Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
 21  *
 22  *   Loosely based upon 'thermostat.c' written by Benjamin Herrenschmidt
 23  *   
 24  *   This program is free software; you can redistribute it and/or
 25  *   modify it under the terms of the GNU General Public License
 26  *   as published by the Free Software Foundation
 27  *   
 28  */
 29 
 30 #include <linux/types.h>
 31 #include <linux/module.h>
 32 #include <linux/errno.h>
 33 #include <linux/kernel.h>
 34 #include <linux/delay.h>
 35 #include <linux/sched.h>
 36 #include <linux/i2c.h>
 37 #include <linux/init.h>
 38 #include <linux/kthread.h>
 39 #include <linux/of_platform.h>
 40 
 41 #include <asm/prom.h>
 42 #include <asm/machdep.h>
 43 #include <asm/io.h>
 44 #include <asm/sections.h>
 45 #include <asm/macio.h>
 46 
 47 #define LOG_TEMP                0                       /* continuously log temperature */
 48 
 49 static struct {
 50         volatile int            running;
 51         struct task_struct      *poll_task;
 52         
 53         struct mutex            lock;
 54         struct platform_device  *of_dev;
 55         
 56         struct i2c_client       *thermostat;
 57         struct i2c_client       *fan;
 58 
 59         int                     overheat_temp;          /* 100% fan at this temp */
 60         int                     overheat_hyst;
 61         int                     temp;
 62         int                     casetemp;
 63         int                     fan_level;              /* active fan_table setting */
 64 
 65         int                     downind;
 66         int                     upind;
 67 
 68         int                     r0, r1, r20, r23, r25;  /* saved register */
 69 } x;
 70 
 71 #define T(x,y)                  (((x)<<8) | (y)*0x100/10 )
 72 
 73 static struct {
 74         int                     fan_down_setting;
 75         int                     temp;
 76         int                     fan_up_setting;
 77 } fan_table[] = {
 78         { 11, T(0,0),  11 },    /* min fan */
 79         { 11, T(55,0), 11 },
 80         {  6, T(55,3), 11 },
 81         {  7, T(56,0), 11 },
 82         {  8, T(57,0), 8 },
 83         {  7, T(58,3), 7 },
 84         {  6, T(58,8), 6 },
 85         {  5, T(59,2), 5 },
 86         {  4, T(59,6), 4 },
 87         {  3, T(59,9), 3 },
 88         {  2, T(60,1), 2 },
 89         {  1, 0xfffff, 1 }      /* on fire */
 90 };
 91 
 92 static void
 93 print_temp( const char *s, int temp )
 94 {
 95         printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
 96 }
 97 
 98 static ssize_t
 99 show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
100 {
101         return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
102 }
103 
104 static ssize_t
105 show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
106 {
107         return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
108 }
109 
110 static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
111 static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
112 
113 
114 
115 /************************************************************************/
116 /*      controller thread                                               */
117 /************************************************************************/
118 
119 static int
120 write_reg( struct i2c_client *cl, int reg, int data, int len )
121 {
122         u8 tmp[3];
123 
124         if( len < 1 || len > 2 || data < 0 )
125                 return -EINVAL;
126 
127         tmp[0] = reg;
128         tmp[1] = (len == 1) ? data : (data >> 8);
129         tmp[2] = data;
130         len++;
131         
132         if( i2c_master_send(cl, tmp, len) != len )
133                 return -ENODEV;
134         return 0;
135 }
136 
137 static int
138 read_reg( struct i2c_client *cl, int reg, int len )
139 {
140         u8 buf[2];
141 
142         if( len != 1 && len != 2 )
143                 return -EINVAL;
144         buf[0] = reg;
145         if( i2c_master_send(cl, buf, 1) != 1 )
146                 return -ENODEV;
147         if( i2c_master_recv(cl, buf, len) != len )
148                 return -ENODEV;
149         return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
150 }
151 
152 static void
153 tune_fan( int fan_setting )
154 {
155         int val = (fan_setting << 3) | 7;
156 
157         /* write_reg( x.fan, 0x24, val, 1 ); */
158         write_reg( x.fan, 0x25, val, 1 );
159