0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include <linux/types.h>
0031 #include <linux/module.h>
0032 #include <linux/errno.h>
0033 #include <linux/kernel.h>
0034 #include <linux/delay.h>
0035 #include <linux/sched.h>
0036 #include <linux/i2c.h>
0037 #include <linux/init.h>
0038 #include <linux/kthread.h>
0039 #include <linux/of_platform.h>
0040
0041 #include <asm/machdep.h>
0042 #include <asm/io.h>
0043 #include <asm/sections.h>
0044 #include <asm/macio.h>
0045
0046 #define LOG_TEMP 0
0047
0048 static struct {
0049 volatile int running;
0050 struct task_struct *poll_task;
0051
0052 struct mutex lock;
0053 struct platform_device *of_dev;
0054
0055 struct i2c_client *thermostat;
0056 struct i2c_client *fan;
0057
0058 int overheat_temp;
0059 int overheat_hyst;
0060 int temp;
0061 int casetemp;
0062 int fan_level;
0063
0064 int downind;
0065 int upind;
0066
0067 int r0, r1, r20, r23, r25;
0068 } x;
0069
0070 #define T(x,y) (((x)<<8) | (y)*0x100/10 )
0071
0072 static struct {
0073 int fan_down_setting;
0074 int temp;
0075 int fan_up_setting;
0076 } fan_table[] = {
0077 { 11, T(0,0), 11 },
0078 { 11, T(55,0), 11 },
0079 { 6, T(55,3), 11 },
0080 { 7, T(56,0), 11 },
0081 { 8, T(57,0), 8 },
0082 { 7, T(58,3), 7 },
0083 { 6, T(58,8), 6 },
0084 { 5, T(59,2), 5 },
0085 { 4, T(59,6), 4 },
0086 { 3, T(59,9), 3 },
0087 { 2, T(60,1), 2 },
0088 { 1, 0xfffff, 1 }
0089 };
0090
0091 static void
0092 print_temp( const char *s, int temp )
0093 {
0094 printk("%s%d.%d C", s ? s : "", temp>>8, (temp & 255)*10/256 );
0095 }
0096
0097 static ssize_t
0098 show_cpu_temperature( struct device *dev, struct device_attribute *attr, char *buf )
0099 {
0100 return sprintf(buf, "%d.%d\n", x.temp>>8, (x.temp & 255)*10/256 );
0101 }
0102
0103 static ssize_t
0104 show_case_temperature( struct device *dev, struct device_attribute *attr, char *buf )
0105 {
0106 return sprintf(buf, "%d.%d\n", x.casetemp>>8, (x.casetemp & 255)*10/256 );
0107 }
0108
0109 static DEVICE_ATTR(cpu_temperature, S_IRUGO, show_cpu_temperature, NULL );
0110 static DEVICE_ATTR(case_temperature, S_IRUGO, show_case_temperature, NULL );
0111
0112
0113
0114
0115
0116
0117
0118 static int
0119 write_reg( struct i2c_client *cl, int reg, int data, int len )
0120 {
0121 u8 tmp[3];
0122
0123 if( len < 1 || len > 2 || data < 0 )
0124 return -EINVAL;
0125
0126 tmp[0] = reg;
0127 tmp[1] = (len == 1) ? data : (data >> 8);
0128 tmp[2] = data;
0129 len++;
0130
0131 if( i2c_master_send(cl, tmp, len) != len )
0132 return -ENODEV;
0133 return 0;
0134 }
0135
0136 static int
0137 read_reg( struct i2c_client *cl, int reg, int len )
0138 {
0139 u8 buf[2];
0140
0141 if( len != 1 && len != 2 )
0142 return -EINVAL;
0143 buf[0] = reg;
0144 if( i2c_master_send(cl, buf, 1) != 1 )
0145 return -ENODEV;
0146 if( i2c_master_recv(cl, buf, len) != len )
0147 return -ENODEV;
0148 return (len == 2)? ((unsigned int)buf[0] << 8) | buf[1] : buf[0];
0149 }
0150
0151 static void
0152 tune_fan( int fan_setting )
0153 {
0154 int val = (fan_setting << 3) | 7;
0155
0156
0157 write_reg( x.fan, 0x25, val, 1 );
0158 write_reg( x.fan, 0x20, 0, 1 );
0159 print_temp("CPU-temp: ", x.temp );
0160 if( x.casetemp )
0161 print_temp(", Case: ", x.casetemp );
0162 printk(", Fan: %d (tuned %+d)\n", 11-fan_setting, x.fan_level-fan_setting );
0163
0164 x.fan_level = fan_setting;
0165 }
0166
0167 static void
0168 poll_temp( void )
0169 {
0170 int temp, i, level, casetemp;
0171
0172 temp = read_reg( x.thermostat, 0, 2 );
0173
0174
0175 if( temp < 0 )
0176 return;
0177
0178 casetemp = read_reg(x.fan, 0x0b, 1) << 8;
0179 casetemp |= (read_reg(x.fan, 0x06, 1) & 0x7) << 5;
0180
0181 if( LOG_TEMP && x.temp != temp ) {
0182 print_temp("CPU-temp: ", temp );
0183 print_temp(", Case: ", casetemp );
0184 printk(", Fan: %d\n", 11-x.fan_level );
0185 }
0186 x.temp = temp;
0187 x.casetemp = casetemp;
0188
0189 level = -1;
0190 for( i=0; (temp & 0xffff) > fan_table[i].temp ; i++ )
0191 ;
0192 if( i < x.downind )
0193 level = fan_table[i].fan_down_setting;
0194 x.downind = i;
0195
0196 for( i=0; (temp & 0xffff) >= fan_table[i+1].temp ; i++ )
0197 ;
0198 if( x.upind < i )
0199 level = fan_table[i].fan_up_setting;
0200 x.upind = i;
0201
0202 if( level >= 0 )
0203 tune_fan( level );
0204 }
0205
0206
0207 static void
0208 setup_hardware( void )
0209 {
0210 int val;
0211 int err;
0212
0213
0214 x.r0 = read_reg( x.fan, 0x00, 1 );
0215 x.r1 = read_reg( x.fan, 0x01, 1 );
0216 x.r20 = read_reg( x.fan, 0x20, 1 );
0217 x.r23 = read_reg( x.fan, 0x23, 1 );
0218 x.r25 = read_reg( x.fan, 0x25, 1 );
0219
0220
0221 if( (val=read_reg(x.thermostat, 1, 1)) >= 0 ) {
0222 val |= 0x60;
0223 if( write_reg( x.thermostat, 1, val, 1 ) )
0224 printk("Failed writing config register\n");
0225 }
0226
0227 write_reg( x.fan, 0x01, 0x01, 1 );
0228
0229 write_reg( x.fan, 0x23, 0x91, 1 );
0230
0231 write_reg( x.fan, 0x00, 0x95, 1 );
0232
0233
0234
0235
0236
0237
0238 if( x.overheat_temp == (80 << 8) ) {
0239 x.overheat_temp = 75 << 8;
0240 x.overheat_hyst = 70 << 8;
0241 write_reg( x.thermostat, 2, x.overheat_hyst, 2 );
0242 write_reg( x.thermostat, 3, x.overheat_temp, 2 );
0243
0244 print_temp("Reducing overheating limit to ", x.overheat_temp );
0245 print_temp(" (Hyst: ", x.overheat_hyst );
0246 printk(")\n");
0247 }
0248
0249
0250 x.downind = 0xffff;
0251 x.upind = -1;
0252
0253
0254 err = device_create_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
0255 err |= device_create_file( &x.of_dev->dev, &dev_attr_case_temperature );
0256 if (err)
0257 printk(KERN_WARNING
0258 "Failed to create temperature attribute file(s).\n");
0259 }
0260
0261 static void
0262 restore_regs( void )
0263 {
0264 device_remove_file( &x.of_dev->dev, &dev_attr_cpu_temperature );
0265 device_remove_file( &x.of_dev->dev, &dev_attr_case_temperature );
0266
0267 write_reg( x.fan, 0x01, x.r1, 1 );
0268 write_reg( x.fan, 0x20, x.r20, 1 );
0269 write_reg( x.fan, 0x23, x.r23, 1 );
0270 write_reg( x.fan, 0x25, x.r25, 1 );
0271 write_reg( x.fan, 0x00, x.r0, 1 );
0272 }
0273
0274 static int control_loop(void *dummy)
0275 {
0276 mutex_lock(&x.lock);
0277 setup_hardware();
0278 mutex_unlock(&x.lock);
0279
0280 for (;;) {
0281 msleep_interruptible(8000);
0282 if (kthread_should_stop())
0283 break;
0284
0285 mutex_lock(&x.lock);
0286 poll_temp();
0287 mutex_unlock(&x.lock);
0288 }
0289
0290 mutex_lock(&x.lock);
0291 restore_regs();
0292 mutex_unlock(&x.lock);
0293
0294 return 0;
0295 }
0296
0297
0298
0299
0300
0301
0302 static void do_attach(struct i2c_adapter *adapter)
0303 {
0304 struct i2c_board_info info = { };
0305 struct device_node *np;
0306
0307
0308 static const unsigned short scan_ds1775[] = {
0309 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0310 I2C_CLIENT_END
0311 };
0312 static const unsigned short scan_adm1030[] = {
0313 0x2c, 0x2d, 0x2e, 0x2f,
0314 I2C_CLIENT_END
0315 };
0316
0317 if (x.running || strncmp(adapter->name, "uni-n", 5))
0318 return;
0319
0320 np = of_find_compatible_node(adapter->dev.of_node, NULL, "MAC,ds1775");
0321 if (np) {
0322 of_node_put(np);
0323 } else {
0324 strlcpy(info.type, "MAC,ds1775", I2C_NAME_SIZE);
0325 i2c_new_scanned_device(adapter, &info, scan_ds1775, NULL);
0326 }
0327
0328 np = of_find_compatible_node(adapter->dev.of_node, NULL, "MAC,adm1030");
0329 if (np) {
0330 of_node_put(np);
0331 } else {
0332 strlcpy(info.type, "MAC,adm1030", I2C_NAME_SIZE);
0333 i2c_new_scanned_device(adapter, &info, scan_adm1030, NULL);
0334 }
0335 }
0336
0337 static int
0338 do_remove(struct i2c_client *client)
0339 {
0340 if (x.running) {
0341 x.running = 0;
0342 kthread_stop(x.poll_task);
0343 x.poll_task = NULL;
0344 }
0345 if (client == x.thermostat)
0346 x.thermostat = NULL;
0347 else if (client == x.fan)
0348 x.fan = NULL;
0349 else
0350 printk(KERN_ERR "g4fan: bad client\n");
0351
0352 return 0;
0353 }
0354
0355 static int
0356 attach_fan( struct i2c_client *cl )
0357 {
0358 if( x.fan )
0359 goto out;
0360
0361
0362 if( read_reg(cl, 0x3d, 1) != 0x30 || read_reg(cl, 0x3e, 1) != 0x41 )
0363 goto out;
0364 printk("ADM1030 fan controller [@%02x]\n", cl->addr );
0365
0366 x.fan = cl;
0367 out:
0368 return 0;
0369 }
0370
0371 static int
0372 attach_thermostat( struct i2c_client *cl )
0373 {
0374 int hyst_temp, os_temp, temp;
0375
0376 if( x.thermostat )
0377 goto out;
0378
0379 if( (temp=read_reg(cl, 0, 2)) < 0 )
0380 goto out;
0381
0382
0383 if( temp < 0x1600 || temp > 0x3c00 )
0384 goto out;
0385 hyst_temp = read_reg(cl, 2, 2);
0386 os_temp = read_reg(cl, 3, 2);
0387 if( hyst_temp < 0 || os_temp < 0 )
0388 goto out;
0389
0390 printk("DS1775 digital thermometer [@%02x]\n", cl->addr );
0391 print_temp("Temp: ", temp );
0392 print_temp(" Hyst: ", hyst_temp );
0393 print_temp(" OS: ", os_temp );
0394 printk("\n");
0395
0396 x.temp = temp;
0397 x.overheat_temp = os_temp;
0398 x.overheat_hyst = hyst_temp;
0399 x.thermostat = cl;
0400 out:
0401 return 0;
0402 }
0403
0404 enum chip { ds1775, adm1030 };
0405
0406 static const struct i2c_device_id therm_windtunnel_id[] = {
0407 { "MAC,ds1775", ds1775 },
0408 { "MAC,adm1030", adm1030 },
0409 { }
0410 };
0411 MODULE_DEVICE_TABLE(i2c, therm_windtunnel_id);
0412
0413 static int
0414 do_probe(struct i2c_client *cl, const struct i2c_device_id *id)
0415 {
0416 struct i2c_adapter *adapter = cl->adapter;
0417 int ret = 0;
0418
0419 if( !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA
0420 | I2C_FUNC_SMBUS_WRITE_BYTE) )
0421 return 0;
0422
0423 switch (id->driver_data) {
0424 case adm1030:
0425 ret = attach_fan(cl);
0426 break;
0427 case ds1775:
0428 ret = attach_thermostat(cl);
0429 break;
0430 }
0431
0432 if (!x.running && x.thermostat && x.fan) {
0433 x.running = 1;
0434 x.poll_task = kthread_run(control_loop, NULL, "g4fand");
0435 }
0436
0437 return ret;
0438 }
0439
0440 static struct i2c_driver g4fan_driver = {
0441 .driver = {
0442 .name = "therm_windtunnel",
0443 },
0444 .probe = do_probe,
0445 .remove = do_remove,
0446 .id_table = therm_windtunnel_id,
0447 };
0448
0449
0450
0451
0452
0453
0454 static int therm_of_probe(struct platform_device *dev)
0455 {
0456 struct i2c_adapter *adap;
0457 int ret, i = 0;
0458
0459 adap = i2c_get_adapter(0);
0460 if (!adap)
0461 return -EPROBE_DEFER;
0462
0463 ret = i2c_add_driver(&g4fan_driver);
0464 if (ret) {
0465 i2c_put_adapter(adap);
0466 return ret;
0467 }
0468
0469
0470 while (adap) {
0471 do_attach(adap);
0472 if (x.running)
0473 return 0;
0474 i2c_put_adapter(adap);
0475 adap = i2c_get_adapter(++i);
0476 }
0477
0478 return -ENODEV;
0479 }
0480
0481 static int
0482 therm_of_remove( struct platform_device *dev )
0483 {
0484 i2c_del_driver( &g4fan_driver );
0485 return 0;
0486 }
0487
0488 static const struct of_device_id therm_of_match[] = {{
0489 .name = "fan",
0490 .compatible = "adm1030"
0491 }, {}
0492 };
0493 MODULE_DEVICE_TABLE(of, therm_of_match);
0494
0495 static struct platform_driver therm_of_driver = {
0496 .driver = {
0497 .name = "temperature",
0498 .of_match_table = therm_of_match,
0499 },
0500 .probe = therm_of_probe,
0501 .remove = therm_of_remove,
0502 };
0503
0504 struct apple_thermal_info {
0505 u8 id;
0506 u8 fan_count;
0507 u8 thermostat_count;
0508 u8 unused;
0509 };
0510
0511 static int __init
0512 g4fan_init( void )
0513 {
0514 const struct apple_thermal_info *info;
0515 struct device_node *np;
0516
0517 mutex_init(&x.lock);
0518
0519 if( !(np=of_find_node_by_name(NULL, "power-mgt")) )
0520 return -ENODEV;
0521 info = of_get_property(np, "thermal-info", NULL);
0522 of_node_put(np);
0523
0524 if( !info || !of_machine_is_compatible("PowerMac3,6") )
0525 return -ENODEV;
0526
0527 if( info->id != 3 ) {
0528 printk(KERN_ERR "therm_windtunnel: unsupported thermal design %d\n", info->id );
0529 return -ENODEV;
0530 }
0531 if( !(np=of_find_node_by_name(NULL, "fan")) )
0532 return -ENODEV;
0533 x.of_dev = of_platform_device_create(np, "temperature", NULL);
0534 of_node_put( np );
0535
0536 if( !x.of_dev ) {
0537 printk(KERN_ERR "Can't register fan controller!\n");
0538 return -ENODEV;
0539 }
0540
0541 platform_driver_register( &therm_of_driver );
0542 return 0;
0543 }
0544
0545 static void __exit
0546 g4fan_exit( void )
0547 {
0548 platform_driver_unregister( &therm_of_driver );
0549
0550 if( x.of_dev )
0551 of_device_unregister( x.of_dev );
0552 }
0553
0554 module_init(g4fan_init);
0555 module_exit(g4fan_exit);
0556
0557 MODULE_AUTHOR("Samuel Rydh <samuel@ibrium.se>");
0558 MODULE_DESCRIPTION("Apple G4 (windtunnel) fan controller");
0559 MODULE_LICENSE("GPL");