Path: blob/master/drivers/macintosh/therm_windtunnel.c
15111 views
/*1* Creation Date: <2003/03/14 20:54:13 samuel>2* Time-stamp: <2004/03/20 14:20:59 samuel>3*4* <therm_windtunnel.c>5*6* The G4 "windtunnel" has a single fan controlled by an7* ADM1030 fan controller and a DS1775 thermostat.8*9* The fan controller is equipped with a temperature sensor10* which measures the case temperature. The DS1775 sensor11* measures the CPU temperature. This driver tunes the12* behavior of the fan. It is based upon empirical observations13* of the 'AppleFan' driver under Mac OS X.14*15* WARNING: This driver has only been testen on Apple's16* 1.25 MHz Dual G4 (March 03). It is tuned for a CPU17* temperature around 57 C.18*19* Copyright (C) 2003, 2004 Samuel Rydh ([email protected])20*21* Loosely based upon 'thermostat.c' written by Benjamin Herrenschmidt22*23* This program is free software; you can redistribute it and/or24* modify it under the terms of the GNU General Public License25* as published by the Free Software Foundation26*27*/2829#include <linux/types.h>30#include <linux/module.h>31#include <linux/errno.h>32#include <linux/kernel.h>33#include <linux/delay.h>34#include <linux/sched.h>35#include <linux/i2c.h>36#include <linux/init.h>37#include <linux/kthread.h>38#include <linux/of_platform.h>3940#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/macio.h>4647#define LOG_TEMP 0 /* continuously log temperature */4849static struct {50volatile int running;51struct task_struct *poll_task;5253struct mutex lock;54struct platform_device *of_dev;5556struct i2c_client *thermostat;57struct i2c_client *fan;5859int overheat_temp; /* 100% fan at this temp */60int overheat_hyst;61int temp;62int casetemp;63int fan_level; /* active fan_table setting */6465int downind;66int upind;6768int r0, r1, r20, r23, r25; /* saved register */69} x;7071#define T(x,y) (((x)<<8) | (y)*0x100/10 )7273static struct {74int fan_down_setting;75int temp;76int 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};9192static void93print_temp( const char *s, int temp )94{95printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );96}9798static ssize_t99show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )100{101return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );102}103104static ssize_t105show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )106{107return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );108}109110static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );111static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );112113114115/************************************************************************/116/* controller thread */117/************************************************************************/118119static int120write_reg( struct i2c_client *cl, int reg, int data, int len )121{122u8 tmp[3];123124if( len < 1 || len > 2 || data < 0 )125return -EINVAL;126127tmp[0] = reg;128tmp[1] = (len == 1) ? data : (data >> 8);129tmp[2] = data;130len++;131132if( i2c_master_send(cl, tmp, len) != len )133return -ENODEV;134return 0;135}136137static int138read_reg( struct i2c_client *cl, int reg, int len )139{140u8 buf[2];141142if( len != 1 && len != 2 )143return -EINVAL;144buf[0] = reg;145if( i2c_master_send(cl, buf, 1) != 1 )146return -ENODEV;147if( i2c_master_recv(cl, buf, len) != len )148return -ENODEV;149return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];150}151152static void153tune_fan( int fan_setting )154{155int val = (fan_setting << 3) | 7;156157/* write_reg( x.fan, 0x24, val, 1 ); */158write_reg( x.fan, 0x25, val, 1 );159write_reg( x.fan, 0x20, 0, 1 );160print_temp("CPU-temp: ", x.temp );161if( x.casetemp )162print_temp(", Case: ", x.casetemp );163printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );164165x.fan_level = fan_setting;166}167168static void169poll_temp( void )170{171int temp, i, level, casetemp;172173temp = read_reg( x.thermostat, 0, 2 );174175/* this actually occurs when the computer is loaded */176if( temp < 0 )177return;178179casetemp = read_reg(x.fan, 0x0b, 1) << 8;180casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;181182if( LOG_TEMP && x.temp != temp ) {183print_temp("CPU-temp: ", temp );184print_temp(", Case: ", casetemp );185printk(", Fan: %d\n", 11-x.fan_level );186}187x.temp = temp;188x.casetemp = casetemp;189190level = -1;191for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )192;193if( i < x.downind )194level = fan_table[i].fan_down_setting;195x.downind = i;196197for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )198;199if( x.upind < i )200level = fan_table[i].fan_up_setting;201x.upind = i;202203if( level >= 0 )204tune_fan( level );205}206207208static void209setup_hardware( void )210{211int val;212int err;213214/* save registers (if we unload the module) */215x.r0 = read_reg( x.fan, 0x00, 1 );216x.r1 = read_reg( x.fan, 0x01, 1 );217x.r20 = read_reg( x.fan, 0x20, 1 );218x.r23 = read_reg( x.fan, 0x23, 1 );219x.r25 = read_reg( x.fan, 0x25, 1 );220221/* improve measurement resolution (convergence time 1.5s) */222if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {223val |= 0x60;224if( write_reg( x.thermostat, 1, val, 1 ) )225printk("Failed writing config register\n");226}227/* disable interrupts and TAC input */228write_reg( x.fan, 0x01, 0x01, 1 );229/* enable filter */230write_reg( x.fan, 0x23, 0x91, 1 );231/* remote temp. controls fan */232write_reg( x.fan, 0x00, 0x95, 1 );233234/* The thermostat (which besides measureing temperature controls235* has a THERM output which puts the fan on 100%) is usually236* set to kick in at 80 C (chip default). We reduce this a bit237* to be on the safe side (OSX doesn't)...238*/239if( x.overheat_temp == (80 << 8) ) {240x.overheat_temp = 75 << 8;241x.overheat_hyst = 70 << 8;242write_reg( x.thermostat, 2, x.overheat_hyst, 2 );243write_reg( x.thermostat, 3, x.overheat_temp, 2 );244245print_temp("Reducing overheating limit to ", x.overheat_temp );246print_temp(" (Hyst: ", x.overheat_hyst );247printk(")\n");248}249250/* set an initial fan setting */251x.downind = 0xffff;252x.upind = -1;253/* tune_fan( fan_up_table[x.upind].fan_setting ); */254255err = device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );256err |= device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );257if (err)258printk(KERN_WARNING259"Failed to create temperature attribute file(s).\n");260}261262static void263restore_regs( void )264{265device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );266device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );267268write_reg( x.fan, 0x01, x.r1, 1 );269write_reg( x.fan, 0x20, x.r20, 1 );270write_reg( x.fan, 0x23, x.r23, 1 );271write_reg( x.fan, 0x25, x.r25, 1 );272write_reg( x.fan, 0x00, x.r0, 1 );273}274275static int control_loop(void *dummy)276{277mutex_lock(&x.lock);278setup_hardware();279mutex_unlock(&x.lock);280281for (;;) {282msleep_interruptible(8000);283if (kthread_should_stop())284break;285286mutex_lock(&x.lock);287poll_temp();288mutex_unlock(&x.lock);289}290291mutex_lock(&x.lock);292restore_regs();293mutex_unlock(&x.lock);294295return 0;296}297298299/************************************************************************/300/* i2c probing and setup */301/************************************************************************/302303static int304do_attach( struct i2c_adapter *adapter )305{306/* scan 0x48-0x4f (DS1775) and 0x2c-2x2f (ADM1030) */307static const unsigned short scan_ds1775[] = {3080x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,309I2C_CLIENT_END310};311static const unsigned short scan_adm1030[] = {3120x2c, 0x2d, 0x2e, 0x2f,313I2C_CLIENT_END314};315316if( strncmp(adapter->name, "uni-n", 5) )317return 0;318319if( !x.running ) {320struct i2c_board_info info;321322memset(&info, 0, sizeof(struct i2c_board_info));323strlcpy(info.type, "therm_ds1775", I2C_NAME_SIZE);324i2c_new_probed_device(adapter, &info, scan_ds1775, NULL);325326strlcpy(info.type, "therm_adm1030", I2C_NAME_SIZE);327i2c_new_probed_device(adapter, &info, scan_adm1030, NULL);328329if( x.thermostat && x.fan ) {330x.running = 1;331x.poll_task = kthread_run(control_loop, NULL, "g4fand");332}333}334return 0;335}336337static int338do_remove(struct i2c_client *client)339{340if (x.running) {341x.running = 0;342kthread_stop(x.poll_task);343x.poll_task = NULL;344}345if (client == x.thermostat)346x.thermostat = NULL;347else if (client == x.fan)348x.fan = NULL;349else350printk(KERN_ERR "g4fan: bad client\n");351352return 0;353}354355static int356attach_fan( struct i2c_client *cl )357{358if( x.fan )359goto out;360361/* check that this is an ADM1030 */362if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )363goto out;364printk("ADM1030 fan controller [@%02x]\n", cl->addr );365366x.fan = cl;367out:368return 0;369}370371static int372attach_thermostat( struct i2c_client *cl )373{374int hyst_temp, os_temp, temp;375376if( x.thermostat )377goto out;378379if( (temp=read_reg(cl, 0, 2)) < 0 )380goto out;381382/* temperature sanity check */383if( temp < 0x1600 || temp > 0x3c00 )384goto out;385hyst_temp = read_reg(cl, 2, 2);386os_temp = read_reg(cl, 3, 2);387if( hyst_temp < 0 || os_temp < 0 )388goto out;389390printk("DS1775 digital thermometer [@%02x]\n", cl->addr );391print_temp("Temp: ", temp );392print_temp(" Hyst: ", hyst_temp );393print_temp(" OS: ", os_temp );394printk("\n");395396x.temp = temp;397x.overheat_temp = os_temp;398x.overheat_hyst = hyst_temp;399x.thermostat = cl;400out:401return 0;402}403404enum chip { ds1775, adm1030 };405406static const struct i2c_device_id therm_windtunnel_id[] = {407{ "therm_ds1775", ds1775 },408{ "therm_adm1030", adm1030 },409{ }410};411412static int413do_probe(struct i2c_client *cl, const struct i2c_device_id *id)414{415struct i2c_adapter *adapter = cl->adapter;416417if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA418| I2C_FUNC_SMBUS_WRITE_BYTE) )419return 0;420421switch (id->driver_data) {422case adm1030:423return attach_fan( cl );424case ds1775:425return attach_thermostat(cl);426}427return 0;428}429430static struct i2c_driver g4fan_driver = {431.driver = {432.name = "therm_windtunnel",433},434.attach_adapter = do_attach,435.probe = do_probe,436.remove = do_remove,437.id_table = therm_windtunnel_id,438};439440441/************************************************************************/442/* initialization / cleanup */443/************************************************************************/444445static int therm_of_probe(struct platform_device *dev)446{447return i2c_add_driver( &g4fan_driver );448}449450static int451therm_of_remove( struct platform_device *dev )452{453i2c_del_driver( &g4fan_driver );454return 0;455}456457static const struct of_device_id therm_of_match[] = {{458.name = "fan",459.compatible = "adm1030"460}, {}461};462463static struct platform_driver therm_of_driver = {464.driver = {465.name = "temperature",466.owner = THIS_MODULE,467.of_match_table = therm_of_match,468},469.probe = therm_of_probe,470.remove = therm_of_remove,471};472473struct apple_thermal_info {474u8 id; /* implementation ID */475u8 fan_count; /* number of fans */476u8 thermostat_count; /* number of thermostats */477u8 unused;478};479480static int __init481g4fan_init( void )482{483const struct apple_thermal_info *info;484struct device_node *np;485486mutex_init(&x.lock);487488if( !(np=of_find_node_by_name(NULL, "power-mgt")) )489return -ENODEV;490info = of_get_property(np, "thermal-info", NULL);491of_node_put(np);492493if( !info || !of_machine_is_compatible("PowerMac3,6") )494return -ENODEV;495496if( info->id != 3 ) {497printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );498return -ENODEV;499}500if( !(np=of_find_node_by_name(NULL, "fan")) )501return -ENODEV;502x.of_dev = of_platform_device_create(np, "temperature", NULL);503of_node_put( np );504505if( !x.of_dev ) {506printk(KERN_ERR "Can't register fan controller!\n");507return -ENODEV;508}509510platform_driver_register( &therm_of_driver );511return 0;512}513514static void __exit515g4fan_exit( void )516{517platform_driver_unregister( &therm_of_driver );518519if( x.of_dev )520of_device_unregister( x.of_dev );521}522523module_init(g4fan_init);524module_exit(g4fan_exit);525526MODULE_AUTHOR("Samuel Rydh <[email protected]>");527MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");528MODULE_LICENSE("GPL");529530531