Linux preempt-rt

Check our new training course

Real-Time Linux with PREEMPT_RT

Check our new training course
with Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
/*
    lm85.c - Part of lm_sensors, Linux kernel modules for hardware
             monitoring
    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 
    Copyright (c) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
    Copyright (c) 2003        Margit Schubert-While <margitsw@t-online.de>

    Chip details at	      <http://www.national.com/ds/LM/LM85.pdf>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/i2c-sensor.h>
#include <linux/i2c-vid.h>
/*
#include <asm/io.h>
*/

#undef	LM85EXTENDEDFUNC	/* Extended functionality */

/* Addresses to scan */
static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };

/* Insmod parameters */
SENSORS_INSMOD_4(lm85b, lm85c, adm1027, adt7463);

/* Enable debug if true */
static int	lm85debug = 0;

/* The LM85 registers */

#define	LM85_REG_IN(nr)			(0x20 + (nr))
#define	LM85_REG_IN_MIN(nr)		(0x44 + (nr) * 2)
#define	LM85_REG_IN_MAX(nr)		(0x45 + (nr) * 2)

#define	LM85_REG_TEMP(nr)		(0x25 + (nr))
#define	LM85_REG_TEMP_MIN(nr)		(0x4e + (nr) * 2)
#define	LM85_REG_TEMP_MAX(nr)		(0x4f + (nr) * 2)

/* Fan speeds are LSB, MSB (2 bytes) */
#define	LM85_REG_FAN(nr)		(0x28 + (nr) *2)
#define	LM85_REG_FAN_MIN(nr)		(0x54 + (nr) *2)

#define	LM85_REG_PWM(nr)		(0x30 + (nr))

#define	ADT7463_REG_OPPOINT(nr)		(0x33 + (nr))

#define	ADT7463_REG_TMIN_CTL1		0x36
#define	ADT7463_REG_TMIN_CTL2		0x37

#define	LM85_REG_DEVICE			0x3d
#define	LM85_REG_COMPANY		0x3e
#define	LM85_REG_VERSTEP		0x3f
/* These are the recognized values for the above regs */
#define	LM85_DEVICE_ADX			0x27
#define	LM85_COMPANY_NATIONAL		0x01
#define	LM85_COMPANY_ANALOG_DEV		0x41
#define	LM85_VERSTEP_GENERIC		0x60
#define	LM85_VERSTEP_LM85C		0x60
#define	LM85_VERSTEP_LM85B		0x62
#define	LM85_VERSTEP_ADM1027		0x60
#define	LM85_VERSTEP_ADT7463		0x62

#define	LM85_REG_CONFIG			0x40

#define	LM85_REG_ALARM1			0x41
#define	LM85_REG_ALARM2			0x42

#define	LM85_REG_VID			0x43

/* Automated FAN control */
#define	LM85_REG_AFAN_CONFIG(nr)	(0x5c + (nr))
#define	LM85_REG_AFAN_RANGE(nr)		(0x5f + (nr))
#define	LM85_REG_AFAN_SPIKE1		0x62
#define	LM85_REG_AFAN_SPIKE2		0x63
#define	LM85_REG_AFAN_MINPWM(nr)	(0x64 + (nr))
#define	LM85_REG_AFAN_LIMIT(nr)		(0x67 + (nr))
#define	LM85_REG_AFAN_CRITICAL(nr)	(0x6a + (nr))
#define	LM85_REG_AFAN_HYST1		0x6d
#define	LM85_REG_AFAN_HYST2		0x6e

#define	LM85_REG_TACH_MODE		0x74
#define	LM85_REG_SPINUP_CTL		0x75

#define	ADM1027_REG_TEMP_OFFSET(nr)	(0x70 + (nr))
#define	ADM1027_REG_CONFIG2		0x73
#define	ADM1027_REG_INTMASK1		0x74
#define	ADM1027_REG_INTMASK2		0x75
#define	ADM1027_REG_EXTEND_ADC1		0x76
#define	ADM1027_REG_EXTEND_ADC2		0x77
#define	ADM1027_REG_CONFIG3		0x78
#define	ADM1027_REG_FAN_PPR		0x7b

#define	ADT7463_REG_THERM		0x79
#define	ADT7463_REG_THERM_LIMIT		0x7A

#define	LM85_ALARM_IN0			0x0001
#define	LM85_ALARM_IN1			0x0002
#define	LM85_ALARM_IN2			0x0004
#define	LM85_ALARM_IN3			0x0008
#define	LM85_ALARM_TEMP1		0x0010
#define	LM85_ALARM_TEMP2		0x0020
#define	LM85_ALARM_TEMP3		0x0040
#define	LM85_ALARM_ALARM2		0x0080
#define	LM85_ALARM_IN4			0x0100
#define	LM85_ALARM_RESERVED		0x0200
#define	LM85_ALARM_FAN1			0x0400
#define	LM85_ALARM_FAN2			0x0800
#define	LM85_ALARM_FAN3			0x1000
#define	LM85_ALARM_FAN4			0x2000
#define	LM85_ALARM_TEMP1_FAULT		0x4000
#define	LM85_ALARM_TEMP3_FAULT		0x8000


/* Conversions. Rounding and limit checking is only done on the TO_REG 
   variants. Note that you should be a bit careful with which arguments
   these macros are called: arguments may be evaluated more than once.
 */

/* IN are scaled 1.000 == 0xc0, mag = 3 */
#define IN_TO_REG(val)		(SENSORS_LIMIT((((val)*0xc0+500)/1000),0,255))
#define INEXT_FROM_REG(val,ext) (((val)*1000 + (ext)*250 + 96)/0xc0)
#define IN_FROM_REG(val)	(INEXT_FROM_REG(val,0))

/* IN are scaled acording to built-in resistors */
static int lm85_scaling[] = {  /* .001 Volts */
		2500, 2250, 3300, 5000, 12000
	};
#define SCALE(val,from,to)		(((val)*(to) + ((from)/2))/(from))
#define INS_TO_REG(n,val)		(SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255))
#define INSEXT_FROM_REG(n,val,ext)	(SCALE((val)*4 + (ext),192*4,lm85_scaling[n]))
#define INS_FROM_REG(n,val)		(INSEXT_FROM_REG(n,val,0))

/* FAN speed is measured using 90kHz clock */
#define FAN_TO_REG(val)		(SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
#define FAN_FROM_REG(val)	((val)==0?-1:(val)==0xffff?0:5400000/(val))

/* Temperature is reported in .001 degC increments */
#define TEMP_TO_REG(val)		(SENSORS_LIMIT(((val)+500)/1000,-127,127))
#define TEMPEXT_FROM_REG(val,ext)	((val)*1000 + (ext)*250)
#define TEMP_FROM_REG(val)		(TEMPEXT_FROM_REG(val,0))
#define EXTTEMP_TO_REG(val)		(SENSORS_LIMIT((val)/250,-127,127))

#define PWM_TO_REG(val)			(SENSORS_LIMIT(val,0,255))
#define PWM_FROM_REG(val)		(val)

#define EXT_FROM_REG(val,sensor)	(((val)>>(sensor * 2))&0x03)

#ifdef	LM85EXTENDEDFUNC	/* Extended functionality */

/* ZONEs have the following parameters:
 *    Limit (low) temp,           1. degC
 *    Hysteresis (below limit),   1. degC (0-15)
 *    Range of speed control,     .1 degC (2-80)
 *    Critical (high) temp,       1. degC
 *
 * FAN PWMs have the following parameters:
 *    Reference Zone,                 1, 2, 3, etc.
 *    Spinup time,                    .05 sec
 *    PWM value at limit/low temp,    1 count
 *    PWM Frequency,                  1. Hz
 *    PWM is Min or OFF below limit,  flag
 *    Invert PWM output,              flag
 *
 * Some chips filter the temp, others the fan.
 *    Filter constant (or disabled)   .1 seconds
 */

/* These are the zone temperature range encodings */
static int lm85_range_map[] = {   /* .1 degC */
		 20,  25,  33,  40,  50,  66,
		 80, 100, 133, 160, 200, 266,
		320, 400, 533, 800
	};
static int RANGE_TO_REG( int range )
{
	int i;

	if( range >= lm85_range_map[15] ) { return 15 ; }
	for( i = 0 ; i < 15 ; ++i )
		if( range <= lm85_range_map[i] )
			break ;
	return( i & 0x0f );
}
#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])

/* These are the Acoustic Enhancement, or Temperature smoothing encodings
 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
 *       MSB (bit 3, value 8).  If the enable bit is 0, the encoded value
 *       is ignored, or set to 0.
 */
static int lm85_smooth_map[] = {  /* .1 sec */
		350, 176, 118,  70,  44,   30,   16,    8
/*    35.4 *    1/1, 1/2, 1/3, 1/5, 1/8, 1/12, 1/24, 1/48  */
	};
static int SMOOTH_TO_REG( int smooth )
{
	int i;

	if( smooth <= 0 ) { return 0 ; }  /* Disabled */
	for( i = 0 ; i < 7 ; ++i )
		if( smooth >= lm85_smooth_map[i] )
			break ;
	return( (i & 0x07) | 0x08 );
}
#define SMOOTH_FROM_REG(val) ((val)&0x08?lm85_smooth_map[(val)&0x07]:0)

/* These are the fan spinup delay time encodings */
static int lm85_spinup_map[] = {  /* .1 sec */
		0, 1, 2, 4, 7, 10, 20, 40
	};
static int SPINUP_TO_REG( int spinup )
{
	int i;

	if( spinup >= lm85_spinup_map[7] ) { return 7 ; }
	for( i = 0 ; i < 7 ; ++i )
		if( spinup <= lm85_spinup_map[i] )
			break ;
	return( i & 0x07 );
}
#define SPINUP_FROM_REG(val) (lm85_spinup_map[(val)&0x07])

/* These are the PWM frequency encodings */
static int lm85_freq_map[] = { /* .1 Hz */
		100, 150, 230, 300, 380, 470, 620, 980
	};
static int FREQ_TO_REG( int freq )
{
	int i;

	if( freq >= lm85_freq_map[7] ) { return 7 ; }
	for( i = 0 ; i < 7 ; ++i )
		if( freq <= lm85_freq_map[i] )
			break ;
	return( i & 0x07 );
}
#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])

/* Since we can't use strings, I'm abusing these numbers
 *   to stand in for the following meanings:
 *      1 -- PWM responds to Zone 1
 *      2 -- PWM responds to Zone 2
 *      3 -- PWM responds to Zone 3
 *     23 -- PWM responds to the higher temp of Zone 2 or 3
 *    123 -- PWM responds to highest of Zone 1, 2, or 3
 *      0 -- PWM is always at 0% (ie, off)
 *     -1 -- PWM is always at 100%
 *     -2 -- PWM responds to manual control
 */

#endif		/* Extended functionality */

static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])

#ifdef	LM85EXTENDEDFUNC	/* Extended functionality */

static int ZONE_TO_REG( int zone )
{
	int i;

	for( i = 0 ; i <= 7 ; ++i )
		if( zone == lm85_zone_map[i] )
			break ;
	if( i > 7 )   /* Not found. */
		i = 3;  /* Always 100% */
	return( (i & 0x07)<<5 );
}

#endif		/* Extended functionality */

#define HYST_TO_REG(val) (SENSORS_LIMIT((-(val)+5)/10,0,15))
#define HYST_FROM_REG(val) (-(val)*10)

#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
#define OFFSET_FROM_REG(val) ((val)*25)

#define PPR_MASK(fan) (0x03<<(fan *2))
#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)

/* i2c-vid.h defines vid_from_reg() */
#define VID_FROM_REG(val,vrm) (vid_from_reg((val),(vrm)))

#define ALARMS_FROM_REG(val) (val)

/* Unlike some other drivers we DO NOT set initial limits.  Use
 * the config file to set limits.  Some users have reported
 * motherboards shutting down when we set limits in a previous
 * version of the driver.
 */

/* Typically used with Pentium 4 systems v9.1 VRM spec */
#define LM85_INIT_VRM  91

/* Chip sampling rates
 *
 * Some sensors are not updated more frequently than once per second
 *    so it doesn't make sense to read them more often than that.
 *    We cache the results and return the saved data if the driver
 *    is called again before a second has elapsed.
 *
 * Also, there is significant configuration data for this chip
 *    given the automatic PWM fan control that is possible.  There
 *    are about 47 bytes of config data to only 22 bytes of actual
 *    readings.  So, we keep the config data up to date in the cache
 *    when it is written and only sample it once every 1 *minute*
 */
#define LM85_DATA_INTERVAL  (HZ + HZ / 2)
#define LM85_CONFIG_INTERVAL  (1 * 60 * HZ)

/* For each registered LM85, we need to keep some data in memory. That
   data is pointed to by lm85_list[NR]->data. The structure itself is
   dynamically allocated, at the same time when a new lm85 client is
   allocated. */

/* LM85 can automatically adjust fan speeds based on temperature
 * This structure encapsulates an entire Zone config.  There are
 * three zones (one for each temperature input) on the lm85
 */
struct lm85_zone {
	s8 limit;	/* Low temp limit */
	u8 hyst;	/* Low limit hysteresis. (0-15) */
	u8 range;	/* Temp range, encoded */
	s8 critical;	/* "All fans ON" temp limit */
};

struct lm85_autofan {
	u8 config;	/* Register value */
	u8 freq;	/* PWM frequency, encoded */
	u8 min_pwm;	/* Minimum PWM value, encoded */
	u8 min_off;	/* Min PWM or OFF below "limit", flag */
};

struct lm85_data {
	struct semaphore lock;
	enum chips type;

	struct semaphore update_lock;
	int valid;		/* !=0 if following fields are valid */
	unsigned long last_reading;	/* In jiffies */
	unsigned long last_config;	/* In jiffies */

	u8 in[5];		/* Register value */
	u8 in_max[5];		/* Register value */
	u8 in_min[5];		/* Register value */
	s8 temp[3];		/* Register value */
	s8 temp_min[3];		/* Register value */
	s8 temp_max[3];		/* Register value */
	s8 temp_offset[3];	/* Register value */
	u16 fan[4];		/* Register value */
	u16 fan_min[4];		/* Register value */
	u8 pwm[3];		/* Register value */
	u8 spinup_ctl;		/* Register encoding, combined */
	u8 tach_mode;		/* Register encoding, combined */
	u16 extend_adc;		/* Register value */
	u8 fan_ppr;		/* Register value */
	u8 smooth[3];		/* Register encoding */
	u8 vid;			/* Register value */
	u8 vrm;			/* VRM version */
	u8 syncpwm3;		/* Saved PWM3 for TACH 2,3,4 config */
	u8 oppoint[3];		/* Register value */
	u16 tmin_ctl;		/* Register value */
	u32 therm_total;	/* Cummulative therm count */
	u8 therm_limit;		/* Register value */
	u16 alarms;		/* Register encoding, combined */
	struct lm85_autofan autofan[3];
	struct lm85_zone zone[3];
};

static int lm85_attach_adapter(struct i2c_adapter *adapter);
static int lm85_detect(struct i2c_adapter *adapter, int address,
			int kind);
static int lm85_detach_client(struct i2c_client *client);

static int lm85_read_value(struct i2c_client *client, u8 register);
static int lm85_write_value(struct i2c_client *client, u8 register, int value);
static void lm85_update_client(struct i2c_client *client);
static void lm85_init_client(struct i2c_client *client);


static struct i2c_driver lm85_driver = {
	.owner          = THIS_MODULE,
	.name           = "lm85",
	.id             = I2C_DRIVERID_LM85,
	.flags          = I2C_DF_NOTIFY,
	.attach_adapter = lm85_attach_adapter,
	.detach_client  = lm85_detach_client,
};

/* Unique ID assigned to each LM85 detected */
static int lm85_id = 0;


/* 4 Fans */
static ssize_t show_fan(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
}
static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
}
static ssize_t set_fan_min(struct device *dev, const char *buf, 
		size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	val;

	down(&data->update_lock);
	val = simple_strtol(buf, NULL, 10);
	data->fan_min[nr] = FAN_TO_REG(val);
	lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
	up(&data->update_lock);
	return count;
}

#define show_fan_offset(offset)						\
static ssize_t show_fan_##offset (struct device *dev, char *buf)	\
{									\
	return show_fan(dev, buf, 0x##offset - 1);			\
}									\
static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)	\
{									\
	return show_fan_min(dev, buf, 0x##offset - 1);			\
}									\
static ssize_t set_fan_##offset##_min (struct device *dev, 		\
	const char *buf, size_t count) 					\
{									\
	return set_fan_min(dev, buf, count, 0x##offset - 1);		\
}									\
static DEVICE_ATTR(fan_input##offset, S_IRUGO, show_fan_##offset, NULL) \
static DEVICE_ATTR(fan_min##offset, S_IRUGO | S_IWUSR, 			\
		show_fan_##offset##_min, set_fan_##offset##_min)

show_fan_offset(1);
show_fan_offset(2);
show_fan_offset(3);
show_fan_offset(4);

/* vid, vrm, alarms */

static ssize_t show_vid_reg(struct device *dev, char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
}

static DEVICE_ATTR(vid, S_IRUGO, show_vid_reg, NULL)

static ssize_t show_vrm_reg(struct device *dev, char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf, "%ld\n", (long) data->vrm);
}

static ssize_t store_vrm_reg(struct device *dev, const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	u32 val;

	val = simple_strtoul(buf, NULL, 10);
	data->vrm = val;
	return count;
}

static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg)

static ssize_t show_alarms_reg(struct device *dev, char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
}

static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL)

/* pwm */

static ssize_t show_pwm(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
}
static ssize_t set_pwm(struct device *dev, const char *buf, 
		size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	val;

	down(&data->update_lock);
	val = simple_strtol(buf, NULL, 10);
	data->pwm[nr] = PWM_TO_REG(val);
	lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
	up(&data->update_lock);
	return count;
}
static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	pwm_zone;

	lm85_update_client(client);
	pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
	return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
}

#define show_pwm_reg(offset)						\
static ssize_t show_pwm_##offset (struct device *dev, char *buf)	\
{									\
	return show_pwm(dev, buf, 0x##offset - 1);			\
}									\
static ssize_t set_pwm_##offset (struct device *dev,			\
				 const char *buf, size_t count)		\
{									\
	return set_pwm(dev, buf, count, 0x##offset - 1);		\
}									\
static ssize_t show_pwm_enable##offset (struct device *dev, char *buf)	\
{									\
	return show_pwm_enable(dev, buf, 0x##offset - 1);			\
}									\
static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, 			\
		show_pwm_##offset, set_pwm_##offset)			\
static DEVICE_ATTR(pwm_enable##offset, S_IRUGO, show_pwm_enable##offset, NULL)

show_pwm_reg(1);
show_pwm_reg(2);
show_pwm_reg(3);

/* Voltages */

static ssize_t show_in(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]) );
}
static ssize_t show_in_min(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
}
static ssize_t set_in_min(struct device *dev, const char *buf, 
		size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	val;

	down(&data->update_lock);
	val = simple_strtol(buf, NULL, 10);
	data->in_min[nr] = INS_TO_REG(nr, val);
	lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
	up(&data->update_lock);
	return count;
}
static ssize_t show_in_max(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
}
static ssize_t set_in_max(struct device *dev, const char *buf, 
		size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	val;

	down(&data->update_lock);
	val = simple_strtol(buf, NULL, 10);
	data->in_max[nr] = INS_TO_REG(nr, val);
	lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
	up(&data->update_lock);
	return count;
}
#define show_in_reg(offset)						\
static ssize_t show_in_##offset (struct device *dev, char *buf)		\
{									\
	return show_in(dev, buf, 0x##offset);				\
}									\
static ssize_t show_in_##offset##_min (struct device *dev, char *buf)	\
{									\
	return show_in_min(dev, buf, 0x##offset);			\
}									\
static ssize_t show_in_##offset##_max (struct device *dev, char *buf)	\
{									\
	return show_in_max(dev, buf, 0x##offset);			\
}									\
static ssize_t set_in_##offset##_min (struct device *dev, 		\
	const char *buf, size_t count) 					\
{									\
	return set_in_min(dev, buf, count, 0x##offset);			\
}									\
static ssize_t set_in_##offset##_max (struct device *dev, 		\
	const char *buf, size_t count) 					\
{									\
	return set_in_max(dev, buf, count, 0x##offset);			\
}									\
static DEVICE_ATTR(in_input##offset, S_IRUGO, show_in_##offset, NULL)	\
static DEVICE_ATTR(in_min##offset, S_IRUGO | S_IWUSR, 			\
		show_in_##offset##_min, set_in_##offset##_min)		\
static DEVICE_ATTR(in_max##offset, S_IRUGO | S_IWUSR, 			\
		show_in_##offset##_max, set_in_##offset##_max)

show_in_reg(0);
show_in_reg(1);
show_in_reg(2);
show_in_reg(3);
show_in_reg(4);

/* Temps */

static ssize_t show_temp(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]) );
}
static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
}
static ssize_t set_temp_min(struct device *dev, const char *buf, 
		size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	val;

	down(&data->update_lock);
	val = simple_strtol(buf, NULL, 10);
	data->temp_min[nr] = TEMP_TO_REG(val);
	lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
	up(&data->update_lock);
	return count;
}
static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);

	lm85_update_client(client);
	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
}
static ssize_t set_temp_max(struct device *dev, const char *buf, 
		size_t count, int nr)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct lm85_data *data = i2c_get_clientdata(client);
	int	val;

	down(&data->update_lock);
	val = simple_strtol(buf, NULL, 10);
	data->temp_max[nr] = TEMP_TO_REG(val);
	lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
	up(&data->update_lock);
	return count;
}
#define show_temp_reg(offset)						\
static ssize_t show_temp_##offset (struct device *dev, char *buf)	\
{									\
	return show_temp(dev, buf, 0x##offset - 1);			\
}									\
static ssize_t show_temp_##offset##_min (struct device *dev, char *buf)	\
{									\
	return show_temp_min(dev, buf, 0x##offset - 1);			\
}									\
static ssize_t show_temp_##offset##_max (struct device *dev, char *buf)	\
{									\
	return show_temp_max(dev, buf, 0x##offset - 1);			\
}									\
static ssize_t set_temp_##offset##_min (struct device *dev, 		\
	const char *buf, size_t count) 					\
{									\
	return set_temp_min(dev, buf, count, 0x##offset - 1);		\
}									\
static ssize_t set_temp_##offset##_max (struct device *dev, 		\
	const char *buf, size_t count) 					\
{									\
	return set_temp_max(dev, buf, count, 0x##offset - 1);		\
}									\
static DEVICE_ATTR(temp_input##offset, S_IRUGO, show_temp_##offset, NULL)	\
static DEVICE_ATTR(temp_min##offset, S_IRUGO | S_IWUSR, 		\
		show_temp_##offset##_min, set_temp_##offset##_min)	\
static DEVICE_ATTR(temp_max##offset, S_IRUGO | S_IWUSR, 		\
		show_temp_##offset##_max, set_temp_##offset##_max)

show_temp_reg(1);
show_temp_reg(2);
show_temp_reg(3);


int lm85_attach_adapter(struct i2c_adapter *adapter)
{
	return i2c_detect(adapter, &addr_data, lm85_detect);
}

int lm85_detect(struct i2c_adapter *adapter, int address,
		int kind)
{
	int company, verstep ;
	struct i2c_client *new_client = NULL;
	struct lm85_data *data;
	int err = 0;
	const char *type_name = "";

	if (i2c_is_isa_adapter(adapter)) {
		/* This chip has no ISA interface */
		goto ERROR0 ;
	};

	if (!i2c_check_functionality(adapter,
					I2C_FUNC_SMBUS_BYTE_DATA)) {
		/* We need to be able to do byte I/O */
		goto ERROR0 ;
	};

	/* OK. For now, we presume we have a valid client. We now create the
	   client structure, even though we cannot fill it completely yet.
	   But it allows us to access lm85_{read,write}_value. */

	if (!(new_client = kmalloc((sizeof(struct i2c_client)) +
				    sizeof(struct lm85_data),
				    GFP_KERNEL))) {
		err = -ENOMEM;
		goto ERROR0;
	}

	memset(new_client, 0, sizeof(struct i2c_client) +
			      sizeof(struct lm85_data));
	data = (struct lm85_data *) (new_client + 1);
	i2c_set_clientdata(new_client, data);
	new_client->addr = address;
	new_client->adapter = adapter;
	new_client->driver = &lm85_driver;
	new_client->flags = 0;

	/* Now, we do the remaining detection. */

	company = lm85_read_value(new_client, LM85_REG_COMPANY);
	verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);

	if (lm85debug) {
		printk("lm85: Detecting device at %d,0x%02x with"
		" COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
		i2c_adapter_id(new_client->adapter), new_client->addr,
		company, verstep);
	}

	/* If auto-detecting, Determine the chip type. */
	if (kind <= 0) {
		if (lm85debug) {
			printk("lm85: Autodetecting device at %d,0x%02x ...\n",
			i2c_adapter_id(adapter), address );
		}
		if( company == LM85_COMPANY_NATIONAL
		    && verstep == LM85_VERSTEP_LM85C ) {
			kind = lm85c ;
		} else if( company == LM85_COMPANY_NATIONAL
		    && verstep == LM85_VERSTEP_LM85B ) {
			kind = lm85b ;
		} else if( company == LM85_COMPANY_NATIONAL
		    && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
			printk("lm85: Unrecgonized version/stepping 0x%02x"
			    " Defaulting to LM85.\n", verstep );
			kind = any_chip ;
		} else if( company == LM85_COMPANY_ANALOG_DEV
		    && verstep == LM85_VERSTEP_ADM1027 ) {
			kind = adm1027 ;
		} else if( company == LM85_COMPANY_ANALOG_DEV
		    && verstep == LM85_VERSTEP_ADT7463 ) {
			kind = adt7463 ;
		} else if( company == LM85_COMPANY_ANALOG_DEV
		    && (verstep & 0xf0) == LM85_VERSTEP_GENERIC ) {
			printk("lm85: Unrecgonized version/stepping 0x%02x"
			    " Defaulting to ADM1027.\n", verstep );
			kind = adm1027 ;
		} else if( kind == 0 && (verstep & 0xf0) == 0x60) {
			printk("lm85: Generic LM85 Version 6 detected\n");
			/* Leave kind as "any_chip" */
		} else {
			if (lm85debug) {
				printk("lm85: Autodetection failed\n");
			}
			/* Not an LM85 ... */
			if( kind == 0 ) {  /* User used force=x,y */
			    printk("lm85: Generic LM85 Version 6 not"
				" found at %d,0x%02x. Try force_lm85c.\n",
				i2c_adapter_id(adapter), address );
			}
			err = 0 ;
			goto ERROR1;
		}
	}

	/* Fill in the chip specific driver values */
	if ( kind == any_chip ) {
		type_name = "lm85";
		strlcpy(new_client->name, "Generic LM85", DEVICE_NAME_SIZE);
	} else if ( kind == lm85b ) {
		type_name = "lm85b";
		strlcpy(new_client->name, "National LM85-B", DEVICE_NAME_SIZE);
	} else if ( kind == lm85c ) {
		type_name = "lm85c";
		strlcpy(new_client->name, "National LM85-C", DEVICE_NAME_SIZE);
	} else if ( kind == adm1027 ) {
		type_name = "adm1027";
		strlcpy(new_client->name, "Analog Devices ADM1027", DEVICE_NAME_SIZE);
	} else if ( kind == adt7463 ) {
		type_name = "adt7463";
		strlcpy(new_client->name, "Analog Devices ADT7463", DEVICE_NAME_SIZE);
	} else {
		dev_dbg(&adapter->dev, "Internal error, invalid kind (%d)!", kind);
		err = -EFAULT ;
		goto ERROR1;
	}

	/* Fill in the remaining client fields */
	new_client->id = lm85_id++;
	data->type = kind;
	data->valid = 0;
	init_MUTEX(&data->update_lock);

	if (lm85debug) {
		printk("lm85: Assigning ID %d to %s at %d,0x%02x\n",
		new_client->id, new_client->name,
		i2c_adapter_id(new_client->adapter),
		new_client->addr);
	}

	/* Tell the I2C layer a new client has arrived */
	if ((err = i2c_attach_client(new_client)))
		goto ERROR1;

	/* Set the VRM version */
	data->vrm = LM85_INIT_VRM ;

	device_create_file(&new_client->dev, &dev_attr_fan_input1);
	device_create_file(&new_client->dev, &dev_attr_fan_input2);
	device_create_file(&new_client->dev, &dev_attr_fan_input3);
	device_create_file(&new_client->dev, &dev_attr_fan_input4);
	device_create_file(&new_client->dev, &dev_attr_fan_min1);
	device_create_file(&new_client->dev, &dev_attr_fan_min2);
	device_create_file(&new_client->dev, &dev_attr_fan_min3);
	device_create_file(&new_client->dev, &dev_attr_fan_min4);
	device_create_file(&new_client->dev, &dev_attr_pwm1);
	device_create_file(&new_client->dev, &dev_attr_pwm2);
	device_create_file(&new_client->dev, &dev_attr_pwm3);
	device_create_file(&new_client->dev, &dev_attr_pwm_enable1);
	device_create_file(&new_client->dev, &dev_attr_pwm_enable2);
	device_create_file(&new_client->dev, &dev_attr_pwm_enable3);
	device_create_file(&new_client->dev, &dev_attr_in_input0);
	device_create_file(&new_client->dev, &dev_attr_in_input1);
	device_create_file(&new_client->dev, &dev_attr_in_input2);
	device_create_file(&new_client->dev, &dev_attr_in_input3);
	device_create_file(&new_client->dev, &dev_attr_in_input4);
	device_create_file(&new_client->dev, &dev_attr_in_min0);
	device_create_file(&new_client->dev, &dev_attr_in_min1);
	device_create_file(&new_client->dev, &dev_attr_in_min2);
	device_create_file(&new_client->dev, &dev_attr_in_min3);
	device_create_file(&new_client->dev, &dev_attr_in_min4);
	device_create_file(&new_client->dev, &dev_attr_in_max0);
	device_create_file(&new_client->dev, &dev_attr_in_max1);
	device_create_file(&new_client->dev, &dev_attr_in_max2);
	device_create_file(&new_client->dev, &dev_attr_in_max3);
	device_create_file(&new_client->dev, &dev_attr_in_max4);
	device_create_file(&new_client->dev, &dev_attr_temp_input1);
	device_create_file(&new_client->dev, &dev_attr_temp_input2);
	device_create_file(&new_client->dev, &dev_attr_temp_input3);
	device_create_file(&new_client->dev, &dev_attr_temp_min1);
	device_create_file(&new_client->dev, &dev_attr_temp_min2);
	device_create_file(&new_client->dev, &dev_attr_temp_min3);
	device_create_file(&new_client->dev, &dev_attr_temp_max1);
	device_create_file(&new_client->dev, &dev_attr_temp_max2);
	device_create_file(&new_client->dev, &dev_attr_temp_max3);
	device_create_file(&new_client->dev, &dev_attr_vrm);
	device_create_file(&new_client->dev, &dev_attr_vid);
	device_create_file(&new_client->dev, &dev_attr_alarms);

	/* Initialize the LM85 chip */
	lm85_init_client(new_client);
	return 0;

	/* Error out and cleanup code */
    ERROR1:
	kfree(new_client);
    ERROR0:
	return err;
}

int lm85_detach_client(struct i2c_client *client)
{
	i2c_detach_client(client);
	kfree(client);
	return 0;
}


int lm85_read_value(struct i2c_client *client, u8 reg)
{
	int res;

	/* What size location is it? */
	switch( reg ) {
	case LM85_REG_FAN(0) :  /* Read WORD data */
	case LM85_REG_FAN(1) :
	case LM85_REG_FAN(2) :
	case LM85_REG_FAN(3) :
	case LM85_REG_FAN_MIN(0) :
	case LM85_REG_FAN_MIN(1) :
	case LM85_REG_FAN_MIN(2) :
	case LM85_REG_FAN_MIN(3) :
	case LM85_REG_ALARM1 :	/* Read both bytes at once */
	case ADM1027_REG_EXTEND_ADC1 :  /* Read two bytes at once */
		res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
		res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
		break ;
	case ADT7463_REG_TMIN_CTL1 :  /* Read WORD MSB, LSB */
		res = i2c_smbus_read_byte_data(client, reg) << 8 ;
		res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
		break ;
	default:	/* Read BYTE data */
		res = i2c_smbus_read_byte_data(client, reg);
		break ;
	}

	return res ;
}

int lm85_write_value(struct i2c_client *client, u8 reg, int value)
{
	int res ;

	switch( reg ) {
	case LM85_REG_FAN(0) :  /* Write WORD data */
	case LM85_REG_FAN(1) :
	case LM85_REG_FAN(2) :
	case LM85_REG_FAN(3) :
	case LM85_REG_FAN_MIN(0) :
	case LM85_REG_FAN_MIN(1) :
	case LM85_REG_FAN_MIN(2) :
	case LM85_REG_FAN_MIN(3) :
	/* NOTE: ALARM is read only, so not included here */
		res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
		res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
		break ;
	case ADT7463_REG_TMIN_CTL1 :  /* Write WORD MSB, LSB */
		res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
		res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
		break ;
	default:	/* Write BYTE data */
		res = i2c_smbus_write_byte_data(client, reg, value);
		break ;
	}

	return res ;
}

void lm85_init_client(struct i2c_client *client)
{
	int value;
	struct lm85_data *data = i2c_get_clientdata(client);

	if (lm85debug) {
		printk("lm85(%d): Initializing device\n", client->id);
	}

	/* Warn if part was not "READY" */
	value = lm85_read_value(client, LM85_REG_CONFIG);
	if (lm85debug) {
		printk("lm85(%d): LM85_REG_CONFIG is: 0x%02x\n", client->id, value );
	}
	if( value & 0x02 ) {
		printk("lm85(%d): Client (%d,0x%02x) config is locked.\n",
			    client->id,
			    i2c_adapter_id(client->adapter), client->addr );
	};
	if( ! (value & 0x04) ) {
		printk("lm85(%d): Client (%d,0x%02x) is not ready.\n",
			    client->id,
			    i2c_adapter_id(client->adapter), client->addr );
	};
	if( value & 0x10
	    && ( data->type == adm1027
		|| data->type == adt7463 ) ) {
		printk("lm85(%d): Client (%d,0x%02x) VxI mode is set.  "
			"Please report this to the lm85 maintainer.\n",
			    client->id,
			    i2c_adapter_id(client->adapter), client->addr );
	};

	/* WE INTENTIONALLY make no changes to the limits,
	 *   offsets, pwms, fans and zones.  If they were
	 *   configured, we don't want to mess with them.
	 *   If they weren't, the default is 100% PWM, no
	 *   control and will suffice until 'sensors -s'
	 *   can be run by the user.
	 */

	/* Start monitoring */
	value = lm85_read_value(client, LM85_REG_CONFIG);
	/* Try to clear LOCK, Set START, save everything else */
	value = (value & ~ 0x02) | 0x01 ;
	if (lm85debug) {
		printk("lm85(%d): Setting CONFIG to: 0x%02x\n", client->id, value );
	}
	lm85_write_value(client, LM85_REG_CONFIG, value);

}

void lm85_update_client(struct i2c_client *client)
{
	struct lm85_data *data = i2c_get_clientdata(client);
	int i;

	down(&data->update_lock);

	if ( !data->valid ||
	     (jiffies - data->last_reading > LM85_DATA_INTERVAL ) ) {
		/* Things that change quickly */

		if (lm85debug) {
			printk("lm85(%d): Reading sensor values\n", client->id);
		}
		/* Have to read extended bits first to "freeze" the
		 * more significant bits that are read later.
		 */
		if ( (data->type == adm1027) || (data->type == adt7463) ) {
			data->extend_adc =
			    lm85_read_value(client, ADM1027_REG_EXTEND_ADC1);
		}

		for (i = 0; i <= 4; ++i) {
			data->in[i] =
			    lm85_read_value(client, LM85_REG_IN(i));
		}

		for (i = 0; i <= 3; ++i) {
			data->fan[i] =
			    lm85_read_value(client, LM85_REG_FAN(i));
		}

		for (i = 0; i <= 2; ++i) {
			data->temp[i] =
			    lm85_read_value(client, LM85_REG_TEMP(i));
		}

		for (i = 0; i <= 2; ++i) {
			data->pwm[i] =
			    lm85_read_value(client, LM85_REG_PWM(i));
		}

		if ( data->type == adt7463 ) {
			if( data->therm_total < ULONG_MAX - 256 ) {
			    data->therm_total +=
				lm85_read_value(client, ADT7463_REG_THERM );
			}
		}

		data->alarms = lm85_read_value(client, LM85_REG_ALARM1);

		data->last_reading = jiffies ;
	};  /* last_reading */

	if ( !data->valid ||
	     (jiffies - data->last_config > LM85_CONFIG_INTERVAL) ) {
		/* Things that don't change often */

		if (lm85debug) {
			printk("lm85(%d): Reading config values\n", client->id);
		}
		for (i = 0; i <= 4; ++i) {
			data->in_min[i] =
			    lm85_read_value(client, LM85_REG_IN_MIN(i));
			data->in_max[i] =
			    lm85_read_value(client, LM85_REG_IN_MAX(i));
		}

		for (i = 0; i <= 3; ++i) {
			data->fan_min[i] =
			    lm85_read_value(client, LM85_REG_FAN_MIN(i));
		}

		for (i = 0; i <= 2; ++i) {
			data->temp_min[i] =
			    lm85_read_value(client, LM85_REG_TEMP_MIN(i));
			data->temp_max[i] =
			    lm85_read_value(client, LM85_REG_TEMP_MAX(i));
		}

		data->vid = lm85_read_value(client, LM85_REG_VID);

		for (i = 0; i <= 2; ++i) {
			int val ;
			data->autofan[i].config =
			    lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
			val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
			data->autofan[i].freq = val & 0x07 ;
			data->zone[i].range = (val >> 4) & 0x0f ;
			data->autofan[i].min_pwm =
			    lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
			data->zone[i].limit =
			    lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
			data->zone[i].critical =
			    lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
		}

		i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
		data->smooth[0] = i & 0x0f ;
		data->syncpwm3 = i & 0x10 ;  /* Save PWM3 config */
		data->autofan[0].min_off = (i & 0x20) != 0 ;
		data->autofan[1].min_off = (i & 0x40) != 0 ;
		data->autofan[2].min_off = (i & 0x80) != 0 ;
		i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
		data->smooth[1] = (i>>4) & 0x0f ;
		data->smooth[2] = i & 0x0f ;

		i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
		data->zone[0].hyst = (i>>4) & 0x0f ;
		data->zone[1].hyst = i & 0x0f ;

		i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
		data->zone[2].hyst = (i>>4) & 0x0f ;

		if ( (data->type == lm85b) || (data->type == lm85c) ) {
			data->tach_mode = lm85_read_value(client,
				LM85_REG_TACH_MODE );
			data->spinup_ctl = lm85_read_value(client,
				LM85_REG_SPINUP_CTL );
		} else if ( (data->type == adt7463) || (data->type == adm1027) ) {
			if ( data->type == adt7463 ) {
				for (i = 0; i <= 2; ++i) {
				    data->oppoint[i] = lm85_read_value(client,
					ADT7463_REG_OPPOINT(i) );
				}
				data->tmin_ctl = lm85_read_value(client,
					ADT7463_REG_TMIN_CTL1 );
				data->therm_limit = lm85_read_value(client,
					ADT7463_REG_THERM_LIMIT );
			}
			for (i = 0; i <= 2; ++i) {
			    data->temp_offset[i] = lm85_read_value(client,
				ADM1027_REG_TEMP_OFFSET(i) );
			}
			data->tach_mode = lm85_read_value(client,
				ADM1027_REG_CONFIG3 );
			data->fan_ppr = lm85_read_value(client,
				ADM1027_REG_FAN_PPR );
		}
	
		data->last_config = jiffies;
	};  /* last_config */

	data->valid = 1;

	up(&data->update_lock);
}


static int __init sm_lm85_init(void)
{
	return i2c_add_driver(&lm85_driver);
}

static void  __exit sm_lm85_exit(void)
{
	i2c_del_driver(&lm85_driver);
}

/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
 *     post 2.7.0 CVS changes
 */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>");
MODULE_DESCRIPTION("LM85-B, LM85-C driver");
MODULE_PARM(lm85debug, "i");
MODULE_PARM_DESC(lm85debug, "Enable debugging statements");

module_init(sm_lm85_init);
module_exit(sm_lm85_exit);