linux-bk/drivers/macintosh/therm_windtunnel.c
<<
>>
Prefs
   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 *      temperatur 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/config.h>
  31#include <linux/types.h>
  32#include <linux/module.h>
  33#include <linux/errno.h>
  34#include <linux/kernel.h>
  35#include <linux/delay.h>
  36#include <linux/sched.h>
  37#include <linux/i2c.h>
  38#include <linux/slab.h>
  39#include <linux/init.h>
  40#include <asm/prom.h>
  41#include <asm/machdep.h>
  42#include <asm/io.h>
  43#include <asm/system.h>
  44#include <asm/sections.h>
  45#include <asm/of_device.h>
  46
  47#define LOG_TEMP                0                       /* continously log temperature */
  48
  49#define I2C_DRIVERID_G4FAN      0x9001                  /* fixme */
  50#define THERMOSTAT_CLIENT_ID    1
  51#define FAN_CLIENT_ID           2
  52
  53static int                      do_probe( struct i2c_adapter *adapter, int addr, int kind);
  54
  55/* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */
  56static unsigned short           normal_i2c[] = { 0x49, 0x2c, I2C_CLIENT_END };
  57static unsigned short           normal_i2c_range[] = { 0x48, 0x4f, 0x2c, 0x2f, I2C_CLIENT_END };
  58
  59I2C_CLIENT_INSMOD;
  60
  61static struct {
  62        volatile int            running;
  63        struct completion       completion;
  64        pid_t                   poll_task;
  65        
  66        struct semaphore        lock;
  67        struct of_device        *of_dev;
  68        
  69        struct i2c_client       *thermostat;
  70        struct i2c_client       *fan;
  71
  72        int                     overheat_temp;          /* 100% fan at this temp */
  73        int                     overheat_hyst;
  74        int                     temp;
  75        int                     casetemp;
  76        int                     fan_level;              /* active fan_table setting */
  77
  78        int                     downind;
  79        int                     upind;
  80
  81        int                     r0, r1, r20, r23, r25;  /* saved register */
  82} x;
  83
  84#define T(x,y)                  (((x)<<8) | (y)*0x100/10 )
  85
  86static struct {
  87        int                     fan_down_setting;
  88        int                     temp;
  89        int                     fan_up_setting;
  90} fan_table[] = {
  91        { 11, T(0,0),  11 },    /* min fan */
  92        { 11, T(55,0), 11 },
  93        {  6, T(55,3), 11 },
  94        {  7, T(56,0), 11 },
  95        {  8, T(57,0), 8 },
  96        {  7, T(58,3), 7 },
  97        {  6, T(58,8), 6 },
  98        {  5, T(59,2), 5 },
  99        {  4, T(59,6), 4 },
 100        {  3, T(59,9), 3 },
 101        {  2, T(60,1), 2 },
 102        {  1, 0xfffff, 1 }      /* on fire */
 103};
 104
 105static void
 106print_temp( const char *s, int temp )
 107{
 108        printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
 109}
 110
 111static ssize_t
 112show_cpu_temperature( struct device *dev, char *buf )
 113{
 114        return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
 115}
 116
 117static ssize_t
 118show_case_temperature( struct device *dev, char *buf )
 119{
 120        return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
 121}
 122
 123static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
 124static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
 125
 126
 127
 128/************************************************************************/
 129/*      controller thread                                               */
 130/************************************************************************/
 131
 132static int
 133write_reg( struct i2c_client *cl, int reg, int data, int len )
 134{
 135        u8 tmp[3];
 136
 137        if( len < 1 || len > 2 || data < 0 )
 138                return -EINVAL;
 139
 140        tmp[0] = reg;
 141        tmp[1] = (len == 1) ? data : (data >> 8);
 142        tmp[2] = data;
 143        len++;
 144        
 145        if( i2c_master_send(cl, tmp, len) != len )
 146                return -ENODEV;
 147        return 0;
 148}
 149
 150static int
 151read_reg( struct i2c_client *cl, int reg, int len )
 152{
 153        u8 buf[2];
 154
 155        if( len != 1 && len != 2 )
 156                return -EINVAL;
 157        buf[0] = reg;
 158        if( i2c_master_send(cl, buf, 1) != 1 )
 159                return -ENODEV;
 160        if( i2c_master_recv(cl, buf, len) != len )
 161                return -ENODEV;
 162        return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
 163}
 164
 165static void
 166tune_fan( int fan_setting )
 167{
 168        int val = (fan_setting << 3) | 7;
 169
 170        /* write_reg( x.fan, 0x24, val, 1 ); */
 171        write_reg( x.fan, 0x25, val, 1 );
 172        write_reg( x.fan, 0x20, 0, 1 );
 173        print_temp("CPU-temp: ", x.temp );
 174        if( x.casetemp )
 175                print_temp(", Case: ", x.casetemp );
 176        printk(",  Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
 177
 178        x.fan_level = fan_setting;
 179}
 180
 181static void
 182poll_temp( void )
 183{
 184        int temp, i, level, casetemp;
 185
 186        temp = read_reg( x.thermostat, 0, 2 );
 187
 188        /* this actually occurs when the computer is loaded */
 189        if( temp < 0 )
 190                return;
 191
 192        casetemp = read_reg(x.fan, 0x0b, 1) << 8;
 193        casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
 194
 195        if( LOG_TEMP && x.temp != temp ) {
 196                print_temp("CPU-temp: ", temp );
 197                print_temp(", Case: ", casetemp );
 198                printk(",  Fan: %d\n", 11-x.fan_level );
 199        }
 200        x.temp = temp;
 201        x.casetemp = casetemp;
 202
 203        level = -1;
 204        for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
 205                ;
 206        if( i < x.downind )
 207                level = fan_table[i].fan_down_setting;
 208        x.downind = i;
 209
 210        for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
 211                ;
 212        if( x.upind < i )
 213                level = fan_table[i].fan_up_setting;
 214        x.upind = i;
 215
 216        if( level >= 0 )
 217                tune_fan( level );
 218}
 219
 220
 221static void
 222setup_hardware( void )
 223{
 224        int val;
 225
 226        /* save registers (if we unload the module) */
 227        x.r0 = read_reg( x.fan, 0x00, 1 );
 228        x.r1 = read_reg( x.fan, 0x01, 1 );
 229        x.r20 = read_reg( x.fan, 0x20, 1 );
 230        x.r23 = read_reg( x.fan, 0x23, 1 );
 231        x.r25 = read_reg( x.fan, 0x25, 1 );
 232
 233        /* improve measurement resolution (convergence time 1.5s) */
 234        if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
 235                val |= 0x60;
 236                if( write_reg( x.thermostat, 1, val, 1 ) )
 237                        printk("Failed writing config register\n");
 238        }
 239        /* disable interrupts and TAC input */
 240        write_reg( x.fan, 0x01, 0x01, 1 );
 241        /* enable filter */
 242        write_reg( x.fan, 0x23, 0x91, 1 );
 243        /* remote temp. controls fan */
 244        write_reg( x.fan, 0x00, 0x95, 1 );
 245
 246        /* The thermostat (which besides measureing temperature controls
 247         * has a THERM output which puts the fan on 100%) is usually
 248         * set to kick in at 80 C (chip default). We reduce this a bit
 249         * to be on the safe side (OSX doesn't)...
 250         */
 251        if( x.overheat_temp == (80 << 8) ) {
 252                x.overheat_temp = 65 << 8;
 253                x.overheat_hyst = 60 << 8;
 254                write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
 255                write_reg( x.thermostat, 3, x.overheat_temp, 2 );
 256
 257                print_temp("Reducing overheating limit to ", x.overheat_temp );
 258                print_temp(" (Hyst: ", x.overheat_hyst );
 259                printk(")\n");
 260        }
 261
 262        /* set an initial fan setting */
 263        x.downind = 0xffff;
 264        x.upind = -1;
 265        /* tune_fan( fan_up_table[x.upind].fan_setting ); */
 266
 267        device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
 268        device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
 269}
 270
 271static void
 272restore_regs( void )
 273{
 274        device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
 275        device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
 276
 277        write_reg( x.fan, 0x01, x.r1, 1 );
 278        write_reg( x.fan, 0x20, x.r20, 1 );
 279        write_reg( x.fan, 0x23, x.r23, 1 );
 280        write_reg( x.fan, 0x25, x.r25, 1 );
 281        write_reg( x.fan, 0x00, x.r0, 1 );
 282}
 283
 284static int
 285control_loop( void *dummy )
 286{
 287        daemonize("g4fand");
 288
 289        down( &x.lock );
 290        setup_hardware();
 291
 292        while( x.running ) {
 293                up( &x.lock );
 294
 295                set_current_state(TASK_INTERRUPTIBLE);
 296                schedule_timeout( 8*HZ );
 297                
 298                down( &x.lock );
 299                poll_temp();
 300        }
 301
 302        restore_regs();
 303        up( &x.lock );
 304
 305        complete_and_exit( &x.completion, 0 );
 306}
 307
 308
 309/************************************************************************/
 310/*      i2c probing and setup                                           */
 311/************************************************************************/
 312
 313static int
 314do_attach( struct i2c_adapter *adapter )
 315{
 316        int ret = 0;
 317
 318        if( strncmp(adapter->name, "uni-n", 5) )
 319                return 0;
 320
 321        if( !x.running ) {
 322                ret = i2c_probe( adapter, &addr_data, &do_probe );
 323                if( x.thermostat && x.fan ) {
 324                        x.running = 1;
 325                        init_completion( &x.completion );
 326                        x.poll_task = kernel_thread( control_loop, NULL, SIGCHLD | CLONE_KERNEL );
 327                }
 328        }
 329        return ret;
 330}
 331
 332static int
 333do_detach( struct i2c_client *client )
 334{
 335        int err;
 336
 337        if( (err=i2c_detach_client(client)) )
 338                printk(KERN_ERR "failed to detach thermostat client\n");
 339        else {
 340                if( x.running ) {
 341                        x.running = 0;
 342                        wait_for_completion( &x.completion );
 343                }
 344                if( client == x.thermostat )
 345                        x.thermostat = NULL;
 346                else if( client == x.fan )
 347                        x.fan = NULL;
 348                else {
 349                        printk(KERN_ERR "g4fan: bad client\n");
 350                }
 351                kfree( client );
 352        }
 353        return err;
 354}
 355
 356static struct i2c_driver g4fan_driver = {  
 357        .name           = "Apple G4 Thermostat/Fan",
 358        .id             = I2C_DRIVERID_G4FAN,
 359        .flags          = I2C_DF_NOTIFY,
 360        .attach_adapter = &do_attach,
 361        .detach_client  = &do_detach,
 362        .command        = NULL,
 363};
 364
 365static int
 366attach_fan( struct i2c_client *cl )
 367{
 368        if( x.fan )
 369                goto out;
 370
 371        /* check that this is an ADM1030 */
 372        if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
 373                goto out;
 374        printk("ADM1030 fan controller [@%02x]\n", cl->addr );
 375
 376        cl->id = FAN_CLIENT_ID;
 377        strlcpy( cl->name, "ADM1030 fan controller", sizeof(cl->name) );
 378
 379        if( !i2c_attach_client(cl) )
 380                x.fan = cl;
 381 out:
 382        if( cl != x.fan )
 383                kfree( cl );
 384        return 0;
 385}
 386
 387static int
 388attach_thermostat( struct i2c_client *cl ) 
 389{
 390        int hyst_temp, os_temp, temp;
 391
 392        if( x.thermostat )
 393                goto out;
 394
 395        if( (temp=read_reg(cl, 0, 2)) < 0 )
 396                goto out;
 397        
 398        /* temperature sanity check */
 399        if( temp < 0x1600 || temp > 0x3c00 )
 400                goto out;
 401        hyst_temp = read_reg(cl, 2, 2);
 402        os_temp = read_reg(cl, 3, 2);
 403        if( hyst_temp < 0 || os_temp < 0 )
 404                goto out;
 405
 406        printk("DS1775 digital thermometer [@%02x]\n", cl->addr );
 407        print_temp("Temp: ", temp );
 408        print_temp("  Hyst: ", hyst_temp );
 409        print_temp("  OS: ", os_temp );
 410        printk("\n");
 411
 412        x.temp = temp;
 413        x.overheat_temp = os_temp;
 414        x.overheat_hyst = hyst_temp;
 415        
 416        cl->id = THERMOSTAT_CLIENT_ID;
 417        strlcpy( cl->name, "DS1775 thermostat", sizeof(cl->name) );
 418
 419        if( !i2c_attach_client(cl) )
 420                x.thermostat = cl;
 421out:
 422        if( cl != x.thermostat )
 423                kfree( cl );
 424        return 0;
 425}
 426
 427static int
 428do_probe( struct i2c_adapter *adapter, int addr, int kind )
 429{
 430        struct i2c_client *cl;
 431
 432        if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
 433                                     | I2C_FUNC_SMBUS_WRITE_BYTE) )
 434                return 0;
 435
 436        if( !(cl=kmalloc(sizeof(*cl), GFP_KERNEL)) )
 437                return -ENOMEM;
 438        memset( cl, 0, sizeof(struct i2c_client) );
 439
 440        cl->addr = addr;
 441        cl->adapter = adapter;
 442        cl->driver = &g4fan_driver;
 443        cl->flags = 0;
 444
 445        if( addr < 0x48 )
 446                return attach_fan( cl );
 447        return attach_thermostat( cl );
 448}
 449
 450
 451/************************************************************************/
 452/*      initialization / cleanup                                        */
 453/************************************************************************/
 454
 455static int
 456therm_of_probe( struct of_device *dev, const struct of_match *match )
 457{
 458        return i2c_add_driver( &g4fan_driver );
 459}
 460
 461static int
 462therm_of_remove( struct of_device *dev )
 463{
 464        return i2c_del_driver( &g4fan_driver );
 465}
 466
 467static struct of_match therm_of_match[] = {{
 468        .name           = "fan",
 469        .type           = OF_ANY_MATCH,
 470        .compatible     = "adm1030"
 471    }, {}
 472};
 473
 474static struct of_platform_driver therm_of_driver = {
 475        .name           = "temperature",
 476        .match_table    = therm_of_match,
 477        .probe          = therm_of_probe,
 478        .remove         = therm_of_remove,
 479};
 480
 481struct apple_thermal_info {
 482        u8              id;                     /* implementation ID */
 483        u8              fan_count;              /* number of fans */
 484        u8              thermostat_count;       /* number of thermostats */
 485        u8              unused;
 486};
 487
 488static int __init
 489g4fan_init( void )
 490{
 491        struct apple_thermal_info *info;
 492        struct device_node *np;
 493
 494        init_MUTEX( &x.lock );
 495
 496        if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
 497                return -ENODEV;
 498        info = (struct apple_thermal_info*)get_property(np, "thermal-info", NULL);
 499        of_node_put(np);
 500
 501        if( !info || !machine_is_compatible("PowerMac3,6") )
 502                return -ENODEV;
 503
 504        if( info->id != 3 ) {
 505                printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
 506                return -ENODEV;
 507        }
 508        if( !(np=of_find_node_by_name(NULL, "fan")) )
 509                return -ENODEV;
 510        x.of_dev = of_platform_device_create( np, "temperature" );
 511        of_node_put( np );
 512
 513        if( !x.of_dev ) {
 514                printk(KERN_ERR "Can't register fan controller!\n");
 515                return -ENODEV;
 516        }
 517
 518        of_register_driver( &therm_of_driver );
 519        return 0;
 520}
 521
 522static void __exit
 523g4fan_exit( void )
 524{
 525        of_unregister_driver( &therm_of_driver );
 526
 527        if( x.of_dev )
 528                of_device_unregister( x.of_dev );
 529}
 530
 531module_init(g4fan_init);
 532module_exit(g4fan_exit);
 533
 534MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
 535MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
 536MODULE_LICENSE("GPL");
 537
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.