xref: /openbmc/linux/drivers/hwmon/via686a.c (revision 8d5d45fb14680326f833295f2316a4ec5e357220)
1*8d5d45fbSJean Delvare /*
2*8d5d45fbSJean Delvare     via686a.c - Part of lm_sensors, Linux kernel modules
3*8d5d45fbSJean Delvare 		for hardware monitoring
4*8d5d45fbSJean Delvare 
5*8d5d45fbSJean Delvare     Copyright (c) 1998 - 2002  Frodo Looijaard <frodol@dds.nl>,
6*8d5d45fbSJean Delvare 			Ky�sti M�lkki <kmalkki@cc.hut.fi>,
7*8d5d45fbSJean Delvare 			Mark Studebaker <mdsxyz123@yahoo.com>,
8*8d5d45fbSJean Delvare 			and Bob Dougherty <bobd@stanford.edu>
9*8d5d45fbSJean Delvare     (Some conversion-factor data were contributed by Jonathan Teh Soon Yew
10*8d5d45fbSJean Delvare     <j.teh@iname.com> and Alex van Kaam <darkside@chello.nl>.)
11*8d5d45fbSJean Delvare 
12*8d5d45fbSJean Delvare     This program is free software; you can redistribute it and/or modify
13*8d5d45fbSJean Delvare     it under the terms of the GNU General Public License as published by
14*8d5d45fbSJean Delvare     the Free Software Foundation; either version 2 of the License, or
15*8d5d45fbSJean Delvare     (at your option) any later version.
16*8d5d45fbSJean Delvare 
17*8d5d45fbSJean Delvare     This program is distributed in the hope that it will be useful,
18*8d5d45fbSJean Delvare     but WITHOUT ANY WARRANTY; without even the implied warranty of
19*8d5d45fbSJean Delvare     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20*8d5d45fbSJean Delvare     GNU General Public License for more details.
21*8d5d45fbSJean Delvare 
22*8d5d45fbSJean Delvare     You should have received a copy of the GNU General Public License
23*8d5d45fbSJean Delvare     along with this program; if not, write to the Free Software
24*8d5d45fbSJean Delvare     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*8d5d45fbSJean Delvare */
26*8d5d45fbSJean Delvare 
27*8d5d45fbSJean Delvare /*
28*8d5d45fbSJean Delvare     Supports the Via VT82C686A, VT82C686B south bridges.
29*8d5d45fbSJean Delvare     Reports all as a 686A.
30*8d5d45fbSJean Delvare     Warning - only supports a single device.
31*8d5d45fbSJean Delvare */
32*8d5d45fbSJean Delvare 
33*8d5d45fbSJean Delvare #include <linux/module.h>
34*8d5d45fbSJean Delvare #include <linux/slab.h>
35*8d5d45fbSJean Delvare #include <linux/pci.h>
36*8d5d45fbSJean Delvare #include <linux/jiffies.h>
37*8d5d45fbSJean Delvare #include <linux/i2c.h>
38*8d5d45fbSJean Delvare #include <linux/i2c-sensor.h>
39*8d5d45fbSJean Delvare #include <linux/init.h>
40*8d5d45fbSJean Delvare #include <asm/io.h>
41*8d5d45fbSJean Delvare 
42*8d5d45fbSJean Delvare 
43*8d5d45fbSJean Delvare /* If force_addr is set to anything different from 0, we forcibly enable
44*8d5d45fbSJean Delvare    the device at the given address. */
45*8d5d45fbSJean Delvare static unsigned short force_addr = 0;
46*8d5d45fbSJean Delvare module_param(force_addr, ushort, 0);
47*8d5d45fbSJean Delvare MODULE_PARM_DESC(force_addr,
48*8d5d45fbSJean Delvare 		 "Initialize the base address of the sensors");
49*8d5d45fbSJean Delvare 
50*8d5d45fbSJean Delvare /* Addresses to scan.
51*8d5d45fbSJean Delvare    Note that we can't determine the ISA address until we have initialized
52*8d5d45fbSJean Delvare    our module */
53*8d5d45fbSJean Delvare static unsigned short normal_i2c[] = { I2C_CLIENT_END };
54*8d5d45fbSJean Delvare static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
55*8d5d45fbSJean Delvare 
56*8d5d45fbSJean Delvare /* Insmod parameters */
57*8d5d45fbSJean Delvare SENSORS_INSMOD_1(via686a);
58*8d5d45fbSJean Delvare 
59*8d5d45fbSJean Delvare /*
60*8d5d45fbSJean Delvare    The Via 686a southbridge has a LM78-like chip integrated on the same IC.
61*8d5d45fbSJean Delvare    This driver is a customized copy of lm78.c
62*8d5d45fbSJean Delvare */
63*8d5d45fbSJean Delvare 
64*8d5d45fbSJean Delvare /* Many VIA686A constants specified below */
65*8d5d45fbSJean Delvare 
66*8d5d45fbSJean Delvare /* Length of ISA address segment */
67*8d5d45fbSJean Delvare #define VIA686A_EXTENT		0x80
68*8d5d45fbSJean Delvare #define VIA686A_BASE_REG	0x70
69*8d5d45fbSJean Delvare #define VIA686A_ENABLE_REG	0x74
70*8d5d45fbSJean Delvare 
71*8d5d45fbSJean Delvare /* The VIA686A registers */
72*8d5d45fbSJean Delvare /* ins numbered 0-4 */
73*8d5d45fbSJean Delvare #define VIA686A_REG_IN_MAX(nr)	(0x2b + ((nr) * 2))
74*8d5d45fbSJean Delvare #define VIA686A_REG_IN_MIN(nr)	(0x2c + ((nr) * 2))
75*8d5d45fbSJean Delvare #define VIA686A_REG_IN(nr)	(0x22 + (nr))
76*8d5d45fbSJean Delvare 
77*8d5d45fbSJean Delvare /* fans numbered 1-2 */
78*8d5d45fbSJean Delvare #define VIA686A_REG_FAN_MIN(nr)	(0x3a + (nr))
79*8d5d45fbSJean Delvare #define VIA686A_REG_FAN(nr)	(0x28 + (nr))
80*8d5d45fbSJean Delvare 
81*8d5d45fbSJean Delvare /* temps numbered 1-3 */
82*8d5d45fbSJean Delvare static const u8 VIA686A_REG_TEMP[]	= { 0x20, 0x21, 0x1f };
83*8d5d45fbSJean Delvare static const u8 VIA686A_REG_TEMP_OVER[]	= { 0x39, 0x3d, 0x1d };
84*8d5d45fbSJean Delvare static const u8 VIA686A_REG_TEMP_HYST[]	= { 0x3a, 0x3e, 0x1e };
85*8d5d45fbSJean Delvare /* bits 7-6 */
86*8d5d45fbSJean Delvare #define VIA686A_REG_TEMP_LOW1	0x4b
87*8d5d45fbSJean Delvare /* 2 = bits 5-4, 3 = bits 7-6 */
88*8d5d45fbSJean Delvare #define VIA686A_REG_TEMP_LOW23	0x49
89*8d5d45fbSJean Delvare 
90*8d5d45fbSJean Delvare #define VIA686A_REG_ALARM1	0x41
91*8d5d45fbSJean Delvare #define VIA686A_REG_ALARM2	0x42
92*8d5d45fbSJean Delvare #define VIA686A_REG_FANDIV	0x47
93*8d5d45fbSJean Delvare #define VIA686A_REG_CONFIG	0x40
94*8d5d45fbSJean Delvare /* The following register sets temp interrupt mode (bits 1-0 for temp1,
95*8d5d45fbSJean Delvare  3-2 for temp2, 5-4 for temp3).  Modes are:
96*8d5d45fbSJean Delvare     00 interrupt stays as long as value is out-of-range
97*8d5d45fbSJean Delvare     01 interrupt is cleared once register is read (default)
98*8d5d45fbSJean Delvare     10 comparator mode- like 00, but ignores hysteresis
99*8d5d45fbSJean Delvare     11 same as 00 */
100*8d5d45fbSJean Delvare #define VIA686A_REG_TEMP_MODE		0x4b
101*8d5d45fbSJean Delvare /* We'll just assume that you want to set all 3 simultaneously: */
102*8d5d45fbSJean Delvare #define VIA686A_TEMP_MODE_MASK		0x3F
103*8d5d45fbSJean Delvare #define VIA686A_TEMP_MODE_CONTINUOUS	0x00
104*8d5d45fbSJean Delvare 
105*8d5d45fbSJean Delvare /* Conversions. Limit checking is only done on the TO_REG
106*8d5d45fbSJean Delvare    variants.
107*8d5d45fbSJean Delvare 
108*8d5d45fbSJean Delvare ********* VOLTAGE CONVERSIONS (Bob Dougherty) ********
109*8d5d45fbSJean Delvare  From HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew):
110*8d5d45fbSJean Delvare  voltagefactor[0]=1.25/2628; (2628/1.25=2102.4)   // Vccp
111*8d5d45fbSJean Delvare  voltagefactor[1]=1.25/2628; (2628/1.25=2102.4)   // +2.5V
112*8d5d45fbSJean Delvare  voltagefactor[2]=1.67/2628; (2628/1.67=1573.7)   // +3.3V
113*8d5d45fbSJean Delvare  voltagefactor[3]=2.6/2628;  (2628/2.60=1010.8)   // +5V
114*8d5d45fbSJean Delvare  voltagefactor[4]=6.3/2628;  (2628/6.30=417.14)   // +12V
115*8d5d45fbSJean Delvare  in[i]=(data[i+2]*25.0+133)*voltagefactor[i];
116*8d5d45fbSJean Delvare  That is:
117*8d5d45fbSJean Delvare  volts = (25*regVal+133)*factor
118*8d5d45fbSJean Delvare  regVal = (volts/factor-133)/25
119*8d5d45fbSJean Delvare  (These conversions were contributed by Jonathan Teh Soon Yew
120*8d5d45fbSJean Delvare  <j.teh@iname.com>) */
121*8d5d45fbSJean Delvare static inline u8 IN_TO_REG(long val, int inNum)
122*8d5d45fbSJean Delvare {
123*8d5d45fbSJean Delvare 	/* To avoid floating point, we multiply constants by 10 (100 for +12V).
124*8d5d45fbSJean Delvare 	   Rounding is done (120500 is actually 133000 - 12500).
125*8d5d45fbSJean Delvare 	   Remember that val is expressed in 0.001V/bit, which is why we divide
126*8d5d45fbSJean Delvare 	   by an additional 10000 (100000 for +12V): 1000 for val and 10 (100)
127*8d5d45fbSJean Delvare 	   for the constants. */
128*8d5d45fbSJean Delvare 	if (inNum <= 1)
129*8d5d45fbSJean Delvare 		return (u8)
130*8d5d45fbSJean Delvare 		    SENSORS_LIMIT((val * 21024 - 1205000) / 250000, 0, 255);
131*8d5d45fbSJean Delvare 	else if (inNum == 2)
132*8d5d45fbSJean Delvare 		return (u8)
133*8d5d45fbSJean Delvare 		    SENSORS_LIMIT((val * 15737 - 1205000) / 250000, 0, 255);
134*8d5d45fbSJean Delvare 	else if (inNum == 3)
135*8d5d45fbSJean Delvare 		return (u8)
136*8d5d45fbSJean Delvare 		    SENSORS_LIMIT((val * 10108 - 1205000) / 250000, 0, 255);
137*8d5d45fbSJean Delvare 	else
138*8d5d45fbSJean Delvare 		return (u8)
139*8d5d45fbSJean Delvare 		    SENSORS_LIMIT((val * 41714 - 12050000) / 2500000, 0, 255);
140*8d5d45fbSJean Delvare }
141*8d5d45fbSJean Delvare 
142*8d5d45fbSJean Delvare static inline long IN_FROM_REG(u8 val, int inNum)
143*8d5d45fbSJean Delvare {
144*8d5d45fbSJean Delvare 	/* To avoid floating point, we multiply constants by 10 (100 for +12V).
145*8d5d45fbSJean Delvare 	   We also multiply them by 1000 because we want 0.001V/bit for the
146*8d5d45fbSJean Delvare 	   output value. Rounding is done. */
147*8d5d45fbSJean Delvare 	if (inNum <= 1)
148*8d5d45fbSJean Delvare 		return (long) ((250000 * val + 1330000 + 21024 / 2) / 21024);
149*8d5d45fbSJean Delvare 	else if (inNum == 2)
150*8d5d45fbSJean Delvare 		return (long) ((250000 * val + 1330000 + 15737 / 2) / 15737);
151*8d5d45fbSJean Delvare 	else if (inNum == 3)
152*8d5d45fbSJean Delvare 		return (long) ((250000 * val + 1330000 + 10108 / 2) / 10108);
153*8d5d45fbSJean Delvare 	else
154*8d5d45fbSJean Delvare 		return (long) ((2500000 * val + 13300000 + 41714 / 2) / 41714);
155*8d5d45fbSJean Delvare }
156*8d5d45fbSJean Delvare 
157*8d5d45fbSJean Delvare /********* FAN RPM CONVERSIONS ********/
158*8d5d45fbSJean Delvare /* Higher register values = slower fans (the fan's strobe gates a counter).
159*8d5d45fbSJean Delvare  But this chip saturates back at 0, not at 255 like all the other chips.
160*8d5d45fbSJean Delvare  So, 0 means 0 RPM */
161*8d5d45fbSJean Delvare static inline u8 FAN_TO_REG(long rpm, int div)
162*8d5d45fbSJean Delvare {
163*8d5d45fbSJean Delvare 	if (rpm == 0)
164*8d5d45fbSJean Delvare 		return 0;
165*8d5d45fbSJean Delvare 	rpm = SENSORS_LIMIT(rpm, 1, 1000000);
166*8d5d45fbSJean Delvare 	return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 255);
167*8d5d45fbSJean Delvare }
168*8d5d45fbSJean Delvare 
169*8d5d45fbSJean Delvare #define FAN_FROM_REG(val,div) ((val)==0?0:(val)==255?0:1350000/((val)*(div)))
170*8d5d45fbSJean Delvare 
171*8d5d45fbSJean Delvare /******** TEMP CONVERSIONS (Bob Dougherty) *********/
172*8d5d45fbSJean Delvare /* linear fits from HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew)
173*8d5d45fbSJean Delvare       if(temp<169)
174*8d5d45fbSJean Delvare 	      return double(temp)*0.427-32.08;
175*8d5d45fbSJean Delvare       else if(temp>=169 && temp<=202)
176*8d5d45fbSJean Delvare 	      return double(temp)*0.582-58.16;
177*8d5d45fbSJean Delvare       else
178*8d5d45fbSJean Delvare 	      return double(temp)*0.924-127.33;
179*8d5d45fbSJean Delvare 
180*8d5d45fbSJean Delvare  A fifth-order polynomial fits the unofficial data (provided by Alex van
181*8d5d45fbSJean Delvare  Kaam <darkside@chello.nl>) a bit better.  It also give more reasonable
182*8d5d45fbSJean Delvare  numbers on my machine (ie. they agree with what my BIOS tells me).
183*8d5d45fbSJean Delvare  Here's the fifth-order fit to the 8-bit data:
184*8d5d45fbSJean Delvare  temp = 1.625093e-10*val^5 - 1.001632e-07*val^4 + 2.457653e-05*val^3 -
185*8d5d45fbSJean Delvare 	2.967619e-03*val^2 + 2.175144e-01*val - 7.090067e+0.
186*8d5d45fbSJean Delvare 
187*8d5d45fbSJean Delvare  (2000-10-25- RFD: thanks to Uwe Andersen <uandersen@mayah.com> for
188*8d5d45fbSJean Delvare  finding my typos in this formula!)
189*8d5d45fbSJean Delvare 
190*8d5d45fbSJean Delvare  Alas, none of the elegant function-fit solutions will work because we
191*8d5d45fbSJean Delvare  aren't allowed to use floating point in the kernel and doing it with
192*8d5d45fbSJean Delvare  integers doesn't provide enough precision.  So we'll do boring old
193*8d5d45fbSJean Delvare  look-up table stuff.  The unofficial data (see below) have effectively
194*8d5d45fbSJean Delvare  7-bit resolution (they are rounded to the nearest degree).  I'm assuming
195*8d5d45fbSJean Delvare  that the transfer function of the device is monotonic and smooth, so a
196*8d5d45fbSJean Delvare  smooth function fit to the data will allow us to get better precision.
197*8d5d45fbSJean Delvare  I used the 5th-order poly fit described above and solved for
198*8d5d45fbSJean Delvare  VIA register values 0-255.  I *10 before rounding, so we get tenth-degree
199*8d5d45fbSJean Delvare  precision.  (I could have done all 1024 values for our 10-bit readings,
200*8d5d45fbSJean Delvare  but the function is very linear in the useful range (0-80 deg C), so
201*8d5d45fbSJean Delvare  we'll just use linear interpolation for 10-bit readings.)  So, tempLUT
202*8d5d45fbSJean Delvare  is the temp at via register values 0-255: */
203*8d5d45fbSJean Delvare static const long tempLUT[] =
204*8d5d45fbSJean Delvare { -709, -688, -667, -646, -627, -607, -589, -570, -553, -536, -519,
205*8d5d45fbSJean Delvare 	-503, -487, -471, -456, -442, -428, -414, -400, -387, -375,
206*8d5d45fbSJean Delvare 	-362, -350, -339, -327, -316, -305, -295, -285, -275, -265,
207*8d5d45fbSJean Delvare 	-255, -246, -237, -229, -220, -212, -204, -196, -188, -180,
208*8d5d45fbSJean Delvare 	-173, -166, -159, -152, -145, -139, -132, -126, -120, -114,
209*8d5d45fbSJean Delvare 	-108, -102, -96, -91, -85, -80, -74, -69, -64, -59, -54, -49,
210*8d5d45fbSJean Delvare 	-44, -39, -34, -29, -25, -20, -15, -11, -6, -2, 3, 7, 12, 16,
211*8d5d45fbSJean Delvare 	20, 25, 29, 33, 37, 42, 46, 50, 54, 59, 63, 67, 71, 75, 79, 84,
212*8d5d45fbSJean Delvare 	88, 92, 96, 100, 104, 109, 113, 117, 121, 125, 130, 134, 138,
213*8d5d45fbSJean Delvare 	142, 146, 151, 155, 159, 163, 168, 172, 176, 181, 185, 189,
214*8d5d45fbSJean Delvare 	193, 198, 202, 206, 211, 215, 219, 224, 228, 232, 237, 241,
215*8d5d45fbSJean Delvare 	245, 250, 254, 259, 263, 267, 272, 276, 281, 285, 290, 294,
216*8d5d45fbSJean Delvare 	299, 303, 307, 312, 316, 321, 325, 330, 334, 339, 344, 348,
217*8d5d45fbSJean Delvare 	353, 357, 362, 366, 371, 376, 380, 385, 390, 395, 399, 404,
218*8d5d45fbSJean Delvare 	409, 414, 419, 423, 428, 433, 438, 443, 449, 454, 459, 464,
219*8d5d45fbSJean Delvare 	469, 475, 480, 486, 491, 497, 502, 508, 514, 520, 526, 532,
220*8d5d45fbSJean Delvare 	538, 544, 551, 557, 564, 571, 578, 584, 592, 599, 606, 614,
221*8d5d45fbSJean Delvare 	621, 629, 637, 645, 654, 662, 671, 680, 689, 698, 708, 718,
222*8d5d45fbSJean Delvare 	728, 738, 749, 759, 770, 782, 793, 805, 818, 830, 843, 856,
223*8d5d45fbSJean Delvare 	870, 883, 898, 912, 927, 943, 958, 975, 991, 1008, 1026, 1044,
224*8d5d45fbSJean Delvare 	1062, 1081, 1101, 1121, 1141, 1162, 1184, 1206, 1229, 1252,
225*8d5d45fbSJean Delvare 	1276, 1301, 1326, 1352, 1378, 1406, 1434, 1462
226*8d5d45fbSJean Delvare };
227*8d5d45fbSJean Delvare 
228*8d5d45fbSJean Delvare /* the original LUT values from Alex van Kaam <darkside@chello.nl>
229*8d5d45fbSJean Delvare    (for via register values 12-240):
230*8d5d45fbSJean Delvare {-50,-49,-47,-45,-43,-41,-39,-38,-37,-35,-34,-33,-32,-31,
231*8d5d45fbSJean Delvare -30,-29,-28,-27,-26,-25,-24,-24,-23,-22,-21,-20,-20,-19,-18,-17,-17,-16,-15,
232*8d5d45fbSJean Delvare -15,-14,-14,-13,-12,-12,-11,-11,-10,-9,-9,-8,-8,-7,-7,-6,-6,-5,-5,-4,-4,-3,
233*8d5d45fbSJean Delvare -3,-2,-2,-1,-1,0,0,1,1,1,3,3,3,4,4,4,5,5,5,6,6,7,7,8,8,9,9,9,10,10,11,11,12,
234*8d5d45fbSJean Delvare 12,12,13,13,13,14,14,15,15,16,16,16,17,17,18,18,19,19,20,20,21,21,21,22,22,
235*8d5d45fbSJean Delvare 22,23,23,24,24,25,25,26,26,26,27,27,27,28,28,29,29,30,30,30,31,31,32,32,33,
236*8d5d45fbSJean Delvare 33,34,34,35,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,
237*8d5d45fbSJean Delvare 45,46,46,47,48,48,49,49,50,51,51,52,52,53,53,54,55,55,56,57,57,58,59,59,60,
238*8d5d45fbSJean Delvare 61,62,62,63,64,65,66,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,83,84,
239*8d5d45fbSJean Delvare 85,86,88,89,91,92,94,96,97,99,101,103,105,107,109,110};
240*8d5d45fbSJean Delvare 
241*8d5d45fbSJean Delvare 
242*8d5d45fbSJean Delvare  Here's the reverse LUT.  I got it by doing a 6-th order poly fit (needed
243*8d5d45fbSJean Delvare  an extra term for a good fit to these inverse data!) and then
244*8d5d45fbSJean Delvare  solving for each temp value from -50 to 110 (the useable range for
245*8d5d45fbSJean Delvare  this chip).  Here's the fit:
246*8d5d45fbSJean Delvare  viaRegVal = -1.160370e-10*val^6 +3.193693e-08*val^5 - 1.464447e-06*val^4
247*8d5d45fbSJean Delvare  - 2.525453e-04*val^3 + 1.424593e-02*val^2 + 2.148941e+00*val +7.275808e+01)
248*8d5d45fbSJean Delvare  Note that n=161: */
249*8d5d45fbSJean Delvare static const u8 viaLUT[] =
250*8d5d45fbSJean Delvare { 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 20, 21, 22, 23,
251*8d5d45fbSJean Delvare 	23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 39, 40,
252*8d5d45fbSJean Delvare 	41, 43, 45, 46, 48, 49, 51, 53, 55, 57, 59, 60, 62, 64, 66,
253*8d5d45fbSJean Delvare 	69, 71, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 95, 98, 100,
254*8d5d45fbSJean Delvare 	103, 105, 107, 110, 112, 115, 117, 119, 122, 124, 126, 129,
255*8d5d45fbSJean Delvare 	131, 134, 136, 138, 140, 143, 145, 147, 150, 152, 154, 156,
256*8d5d45fbSJean Delvare 	158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
257*8d5d45fbSJean Delvare 	182, 183, 185, 187, 188, 190, 192, 193, 195, 196, 198, 199,
258*8d5d45fbSJean Delvare 	200, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213,
259*8d5d45fbSJean Delvare 	214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224,
260*8d5d45fbSJean Delvare 	225, 226, 226, 227, 228, 228, 229, 230, 230, 231, 232, 232,
261*8d5d45fbSJean Delvare 	233, 233, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
262*8d5d45fbSJean Delvare 	239, 240
263*8d5d45fbSJean Delvare };
264*8d5d45fbSJean Delvare 
265*8d5d45fbSJean Delvare /* Converting temps to (8-bit) hyst and over registers
266*8d5d45fbSJean Delvare    No interpolation here.
267*8d5d45fbSJean Delvare    The +50 is because the temps start at -50 */
268*8d5d45fbSJean Delvare static inline u8 TEMP_TO_REG(long val)
269*8d5d45fbSJean Delvare {
270*8d5d45fbSJean Delvare 	return viaLUT[val <= -50000 ? 0 : val >= 110000 ? 160 :
271*8d5d45fbSJean Delvare 		      (val < 0 ? val - 500 : val + 500) / 1000 + 50];
272*8d5d45fbSJean Delvare }
273*8d5d45fbSJean Delvare 
274*8d5d45fbSJean Delvare /* for 8-bit temperature hyst and over registers */
275*8d5d45fbSJean Delvare #define TEMP_FROM_REG(val) (tempLUT[(val)] * 100)
276*8d5d45fbSJean Delvare 
277*8d5d45fbSJean Delvare /* for 10-bit temperature readings */
278*8d5d45fbSJean Delvare static inline long TEMP_FROM_REG10(u16 val)
279*8d5d45fbSJean Delvare {
280*8d5d45fbSJean Delvare 	u16 eightBits = val >> 2;
281*8d5d45fbSJean Delvare 	u16 twoBits = val & 3;
282*8d5d45fbSJean Delvare 
283*8d5d45fbSJean Delvare 	/* no interpolation for these */
284*8d5d45fbSJean Delvare 	if (twoBits == 0 || eightBits == 255)
285*8d5d45fbSJean Delvare 		return TEMP_FROM_REG(eightBits);
286*8d5d45fbSJean Delvare 
287*8d5d45fbSJean Delvare 	/* do some linear interpolation */
288*8d5d45fbSJean Delvare 	return (tempLUT[eightBits] * (4 - twoBits) +
289*8d5d45fbSJean Delvare 		tempLUT[eightBits + 1] * twoBits) * 25;
290*8d5d45fbSJean Delvare }
291*8d5d45fbSJean Delvare 
292*8d5d45fbSJean Delvare #define DIV_FROM_REG(val) (1 << (val))
293*8d5d45fbSJean Delvare #define DIV_TO_REG(val) ((val)==8?3:(val)==4?2:(val)==1?0:1)
294*8d5d45fbSJean Delvare 
295*8d5d45fbSJean Delvare /* For the VIA686A, we need to keep some data in memory.
296*8d5d45fbSJean Delvare    The structure is dynamically allocated, at the same time when a new
297*8d5d45fbSJean Delvare    via686a client is allocated. */
298*8d5d45fbSJean Delvare struct via686a_data {
299*8d5d45fbSJean Delvare 	struct i2c_client client;
300*8d5d45fbSJean Delvare 	struct semaphore update_lock;
301*8d5d45fbSJean Delvare 	char valid;		/* !=0 if following fields are valid */
302*8d5d45fbSJean Delvare 	unsigned long last_updated;	/* In jiffies */
303*8d5d45fbSJean Delvare 
304*8d5d45fbSJean Delvare 	u8 in[5];		/* Register value */
305*8d5d45fbSJean Delvare 	u8 in_max[5];		/* Register value */
306*8d5d45fbSJean Delvare 	u8 in_min[5];		/* Register value */
307*8d5d45fbSJean Delvare 	u8 fan[2];		/* Register value */
308*8d5d45fbSJean Delvare 	u8 fan_min[2];		/* Register value */
309*8d5d45fbSJean Delvare 	u16 temp[3];		/* Register value 10 bit */
310*8d5d45fbSJean Delvare 	u8 temp_over[3];	/* Register value */
311*8d5d45fbSJean Delvare 	u8 temp_hyst[3];	/* Register value */
312*8d5d45fbSJean Delvare 	u8 fan_div[2];		/* Register encoding, shifted right */
313*8d5d45fbSJean Delvare 	u16 alarms;		/* Register encoding, combined */
314*8d5d45fbSJean Delvare };
315*8d5d45fbSJean Delvare 
316*8d5d45fbSJean Delvare static struct pci_dev *s_bridge;	/* pointer to the (only) via686a */
317*8d5d45fbSJean Delvare 
318*8d5d45fbSJean Delvare static int via686a_attach_adapter(struct i2c_adapter *adapter);
319*8d5d45fbSJean Delvare static int via686a_detect(struct i2c_adapter *adapter, int address, int kind);
320*8d5d45fbSJean Delvare static int via686a_detach_client(struct i2c_client *client);
321*8d5d45fbSJean Delvare 
322*8d5d45fbSJean Delvare static inline int via686a_read_value(struct i2c_client *client, u8 reg)
323*8d5d45fbSJean Delvare {
324*8d5d45fbSJean Delvare 	return (inb_p(client->addr + reg));
325*8d5d45fbSJean Delvare }
326*8d5d45fbSJean Delvare 
327*8d5d45fbSJean Delvare static inline void via686a_write_value(struct i2c_client *client, u8 reg,
328*8d5d45fbSJean Delvare 				       u8 value)
329*8d5d45fbSJean Delvare {
330*8d5d45fbSJean Delvare 	outb_p(value, client->addr + reg);
331*8d5d45fbSJean Delvare }
332*8d5d45fbSJean Delvare 
333*8d5d45fbSJean Delvare static struct via686a_data *via686a_update_device(struct device *dev);
334*8d5d45fbSJean Delvare static void via686a_init_client(struct i2c_client *client);
335*8d5d45fbSJean Delvare 
336*8d5d45fbSJean Delvare /* following are the sysfs callback functions */
337*8d5d45fbSJean Delvare 
338*8d5d45fbSJean Delvare /* 7 voltage sensors */
339*8d5d45fbSJean Delvare static ssize_t show_in(struct device *dev, char *buf, int nr) {
340*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
341*8d5d45fbSJean Delvare 	return sprintf(buf, "%ld\n", IN_FROM_REG(data->in[nr], nr));
342*8d5d45fbSJean Delvare }
343*8d5d45fbSJean Delvare 
344*8d5d45fbSJean Delvare static ssize_t show_in_min(struct device *dev, char *buf, int nr) {
345*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
346*8d5d45fbSJean Delvare 	return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_min[nr], nr));
347*8d5d45fbSJean Delvare }
348*8d5d45fbSJean Delvare 
349*8d5d45fbSJean Delvare static ssize_t show_in_max(struct device *dev, char *buf, int nr) {
350*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
351*8d5d45fbSJean Delvare 	return sprintf(buf, "%ld\n", IN_FROM_REG(data->in_max[nr], nr));
352*8d5d45fbSJean Delvare }
353*8d5d45fbSJean Delvare 
354*8d5d45fbSJean Delvare static ssize_t set_in_min(struct device *dev, const char *buf,
355*8d5d45fbSJean Delvare 		size_t count, int nr) {
356*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
357*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
358*8d5d45fbSJean Delvare 	unsigned long val = simple_strtoul(buf, NULL, 10);
359*8d5d45fbSJean Delvare 
360*8d5d45fbSJean Delvare 	down(&data->update_lock);
361*8d5d45fbSJean Delvare 	data->in_min[nr] = IN_TO_REG(val, nr);
362*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_IN_MIN(nr),
363*8d5d45fbSJean Delvare 			data->in_min[nr]);
364*8d5d45fbSJean Delvare 	up(&data->update_lock);
365*8d5d45fbSJean Delvare 	return count;
366*8d5d45fbSJean Delvare }
367*8d5d45fbSJean Delvare static ssize_t set_in_max(struct device *dev, const char *buf,
368*8d5d45fbSJean Delvare 		size_t count, int nr) {
369*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
370*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
371*8d5d45fbSJean Delvare 	unsigned long val = simple_strtoul(buf, NULL, 10);
372*8d5d45fbSJean Delvare 
373*8d5d45fbSJean Delvare 	down(&data->update_lock);
374*8d5d45fbSJean Delvare 	data->in_max[nr] = IN_TO_REG(val, nr);
375*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_IN_MAX(nr),
376*8d5d45fbSJean Delvare 			data->in_max[nr]);
377*8d5d45fbSJean Delvare 	up(&data->update_lock);
378*8d5d45fbSJean Delvare 	return count;
379*8d5d45fbSJean Delvare }
380*8d5d45fbSJean Delvare #define show_in_offset(offset)					\
381*8d5d45fbSJean Delvare static ssize_t 							\
382*8d5d45fbSJean Delvare 	show_in##offset (struct device *dev, struct device_attribute *attr, char *buf)		\
383*8d5d45fbSJean Delvare {								\
384*8d5d45fbSJean Delvare 	return show_in(dev, buf, offset);			\
385*8d5d45fbSJean Delvare }								\
386*8d5d45fbSJean Delvare static ssize_t 							\
387*8d5d45fbSJean Delvare 	show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
388*8d5d45fbSJean Delvare {								\
389*8d5d45fbSJean Delvare 	return show_in_min(dev, buf, offset);		\
390*8d5d45fbSJean Delvare }								\
391*8d5d45fbSJean Delvare static ssize_t 							\
392*8d5d45fbSJean Delvare 	show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)	\
393*8d5d45fbSJean Delvare {								\
394*8d5d45fbSJean Delvare 	return show_in_max(dev, buf, offset);		\
395*8d5d45fbSJean Delvare }								\
396*8d5d45fbSJean Delvare static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, 	\
397*8d5d45fbSJean Delvare 		const char *buf, size_t count) 			\
398*8d5d45fbSJean Delvare {								\
399*8d5d45fbSJean Delvare 	return set_in_min(dev, buf, count, offset);		\
400*8d5d45fbSJean Delvare }								\
401*8d5d45fbSJean Delvare static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr,	\
402*8d5d45fbSJean Delvare 			const char *buf, size_t count)		\
403*8d5d45fbSJean Delvare {								\
404*8d5d45fbSJean Delvare 	return set_in_max(dev, buf, count, offset);		\
405*8d5d45fbSJean Delvare }								\
406*8d5d45fbSJean Delvare static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);\
407*8d5d45fbSJean Delvare static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, 	\
408*8d5d45fbSJean Delvare 		show_in##offset##_min, set_in##offset##_min);	\
409*8d5d45fbSJean Delvare static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, 	\
410*8d5d45fbSJean Delvare 		show_in##offset##_max, set_in##offset##_max);
411*8d5d45fbSJean Delvare 
412*8d5d45fbSJean Delvare show_in_offset(0);
413*8d5d45fbSJean Delvare show_in_offset(1);
414*8d5d45fbSJean Delvare show_in_offset(2);
415*8d5d45fbSJean Delvare show_in_offset(3);
416*8d5d45fbSJean Delvare show_in_offset(4);
417*8d5d45fbSJean Delvare 
418*8d5d45fbSJean Delvare /* 3 temperatures */
419*8d5d45fbSJean Delvare static ssize_t show_temp(struct device *dev, char *buf, int nr) {
420*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
421*8d5d45fbSJean Delvare 	return sprintf(buf, "%ld\n", TEMP_FROM_REG10(data->temp[nr]));
422*8d5d45fbSJean Delvare }
423*8d5d45fbSJean Delvare static ssize_t show_temp_over(struct device *dev, char *buf, int nr) {
424*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
425*8d5d45fbSJean Delvare 	return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_over[nr]));
426*8d5d45fbSJean Delvare }
427*8d5d45fbSJean Delvare static ssize_t show_temp_hyst(struct device *dev, char *buf, int nr) {
428*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
429*8d5d45fbSJean Delvare 	return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp_hyst[nr]));
430*8d5d45fbSJean Delvare }
431*8d5d45fbSJean Delvare static ssize_t set_temp_over(struct device *dev, const char *buf,
432*8d5d45fbSJean Delvare 		size_t count, int nr) {
433*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
434*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
435*8d5d45fbSJean Delvare 	int val = simple_strtol(buf, NULL, 10);
436*8d5d45fbSJean Delvare 
437*8d5d45fbSJean Delvare 	down(&data->update_lock);
438*8d5d45fbSJean Delvare 	data->temp_over[nr] = TEMP_TO_REG(val);
439*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_TEMP_OVER[nr],
440*8d5d45fbSJean Delvare 			    data->temp_over[nr]);
441*8d5d45fbSJean Delvare 	up(&data->update_lock);
442*8d5d45fbSJean Delvare 	return count;
443*8d5d45fbSJean Delvare }
444*8d5d45fbSJean Delvare static ssize_t set_temp_hyst(struct device *dev, const char *buf,
445*8d5d45fbSJean Delvare 		size_t count, int nr) {
446*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
447*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
448*8d5d45fbSJean Delvare 	int val = simple_strtol(buf, NULL, 10);
449*8d5d45fbSJean Delvare 
450*8d5d45fbSJean Delvare 	down(&data->update_lock);
451*8d5d45fbSJean Delvare 	data->temp_hyst[nr] = TEMP_TO_REG(val);
452*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_TEMP_HYST[nr],
453*8d5d45fbSJean Delvare 			    data->temp_hyst[nr]);
454*8d5d45fbSJean Delvare 	up(&data->update_lock);
455*8d5d45fbSJean Delvare 	return count;
456*8d5d45fbSJean Delvare }
457*8d5d45fbSJean Delvare #define show_temp_offset(offset)					\
458*8d5d45fbSJean Delvare static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
459*8d5d45fbSJean Delvare {									\
460*8d5d45fbSJean Delvare 	return show_temp(dev, buf, offset - 1);				\
461*8d5d45fbSJean Delvare }									\
462*8d5d45fbSJean Delvare static ssize_t								\
463*8d5d45fbSJean Delvare show_temp_##offset##_over (struct device *dev, struct device_attribute *attr, char *buf)		\
464*8d5d45fbSJean Delvare {									\
465*8d5d45fbSJean Delvare 	return show_temp_over(dev, buf, offset - 1);			\
466*8d5d45fbSJean Delvare }									\
467*8d5d45fbSJean Delvare static ssize_t								\
468*8d5d45fbSJean Delvare show_temp_##offset##_hyst (struct device *dev, struct device_attribute *attr, char *buf)		\
469*8d5d45fbSJean Delvare {									\
470*8d5d45fbSJean Delvare 	return show_temp_hyst(dev, buf, offset - 1);			\
471*8d5d45fbSJean Delvare }									\
472*8d5d45fbSJean Delvare static ssize_t set_temp_##offset##_over (struct device *dev, struct device_attribute *attr, 		\
473*8d5d45fbSJean Delvare 		const char *buf, size_t count) 				\
474*8d5d45fbSJean Delvare {									\
475*8d5d45fbSJean Delvare 	return set_temp_over(dev, buf, count, offset - 1);		\
476*8d5d45fbSJean Delvare }									\
477*8d5d45fbSJean Delvare static ssize_t set_temp_##offset##_hyst (struct device *dev, struct device_attribute *attr, 		\
478*8d5d45fbSJean Delvare 		const char *buf, size_t count) 				\
479*8d5d45fbSJean Delvare {									\
480*8d5d45fbSJean Delvare 	return set_temp_hyst(dev, buf, count, offset - 1);		\
481*8d5d45fbSJean Delvare }									\
482*8d5d45fbSJean Delvare static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL);\
483*8d5d45fbSJean Delvare static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, 		\
484*8d5d45fbSJean Delvare 		show_temp_##offset##_over, set_temp_##offset##_over);	\
485*8d5d45fbSJean Delvare static DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, 		\
486*8d5d45fbSJean Delvare 		show_temp_##offset##_hyst, set_temp_##offset##_hyst);
487*8d5d45fbSJean Delvare 
488*8d5d45fbSJean Delvare show_temp_offset(1);
489*8d5d45fbSJean Delvare show_temp_offset(2);
490*8d5d45fbSJean Delvare show_temp_offset(3);
491*8d5d45fbSJean Delvare 
492*8d5d45fbSJean Delvare /* 2 Fans */
493*8d5d45fbSJean Delvare static ssize_t show_fan(struct device *dev, char *buf, int nr) {
494*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
495*8d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
496*8d5d45fbSJean Delvare 				DIV_FROM_REG(data->fan_div[nr])) );
497*8d5d45fbSJean Delvare }
498*8d5d45fbSJean Delvare static ssize_t show_fan_min(struct device *dev, char *buf, int nr) {
499*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
500*8d5d45fbSJean Delvare 	return sprintf(buf, "%d\n",
501*8d5d45fbSJean Delvare 		FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])) );
502*8d5d45fbSJean Delvare }
503*8d5d45fbSJean Delvare static ssize_t show_fan_div(struct device *dev, char *buf, int nr) {
504*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
505*8d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
506*8d5d45fbSJean Delvare }
507*8d5d45fbSJean Delvare static ssize_t set_fan_min(struct device *dev, const char *buf,
508*8d5d45fbSJean Delvare 		size_t count, int nr) {
509*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
510*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
511*8d5d45fbSJean Delvare 	int val = simple_strtol(buf, NULL, 10);
512*8d5d45fbSJean Delvare 
513*8d5d45fbSJean Delvare 	down(&data->update_lock);
514*8d5d45fbSJean Delvare 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
515*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_FAN_MIN(nr+1), data->fan_min[nr]);
516*8d5d45fbSJean Delvare 	up(&data->update_lock);
517*8d5d45fbSJean Delvare 	return count;
518*8d5d45fbSJean Delvare }
519*8d5d45fbSJean Delvare static ssize_t set_fan_div(struct device *dev, const char *buf,
520*8d5d45fbSJean Delvare 		size_t count, int nr) {
521*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
522*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
523*8d5d45fbSJean Delvare 	int val = simple_strtol(buf, NULL, 10);
524*8d5d45fbSJean Delvare 	int old;
525*8d5d45fbSJean Delvare 
526*8d5d45fbSJean Delvare 	down(&data->update_lock);
527*8d5d45fbSJean Delvare 	old = via686a_read_value(client, VIA686A_REG_FANDIV);
528*8d5d45fbSJean Delvare 	data->fan_div[nr] = DIV_TO_REG(val);
529*8d5d45fbSJean Delvare 	old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
530*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_FANDIV, old);
531*8d5d45fbSJean Delvare 	up(&data->update_lock);
532*8d5d45fbSJean Delvare 	return count;
533*8d5d45fbSJean Delvare }
534*8d5d45fbSJean Delvare 
535*8d5d45fbSJean Delvare #define show_fan_offset(offset)						\
536*8d5d45fbSJean Delvare static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
537*8d5d45fbSJean Delvare {									\
538*8d5d45fbSJean Delvare 	return show_fan(dev, buf, offset - 1);				\
539*8d5d45fbSJean Delvare }									\
540*8d5d45fbSJean Delvare static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
541*8d5d45fbSJean Delvare {									\
542*8d5d45fbSJean Delvare 	return show_fan_min(dev, buf, offset - 1);			\
543*8d5d45fbSJean Delvare }									\
544*8d5d45fbSJean Delvare static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf)	\
545*8d5d45fbSJean Delvare {									\
546*8d5d45fbSJean Delvare 	return show_fan_div(dev, buf, offset - 1);			\
547*8d5d45fbSJean Delvare }									\
548*8d5d45fbSJean Delvare static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, 		\
549*8d5d45fbSJean Delvare 	const char *buf, size_t count) 					\
550*8d5d45fbSJean Delvare {									\
551*8d5d45fbSJean Delvare 	return set_fan_min(dev, buf, count, offset - 1);		\
552*8d5d45fbSJean Delvare }									\
553*8d5d45fbSJean Delvare static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, 		\
554*8d5d45fbSJean Delvare 		const char *buf, size_t count) 				\
555*8d5d45fbSJean Delvare {									\
556*8d5d45fbSJean Delvare 	return set_fan_div(dev, buf, count, offset - 1);		\
557*8d5d45fbSJean Delvare }									\
558*8d5d45fbSJean Delvare static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
559*8d5d45fbSJean Delvare static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, 		\
560*8d5d45fbSJean Delvare 		show_fan_##offset##_min, set_fan_##offset##_min);	\
561*8d5d45fbSJean Delvare static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, 		\
562*8d5d45fbSJean Delvare 		show_fan_##offset##_div, set_fan_##offset##_div);
563*8d5d45fbSJean Delvare 
564*8d5d45fbSJean Delvare show_fan_offset(1);
565*8d5d45fbSJean Delvare show_fan_offset(2);
566*8d5d45fbSJean Delvare 
567*8d5d45fbSJean Delvare /* Alarms */
568*8d5d45fbSJean Delvare static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) {
569*8d5d45fbSJean Delvare 	struct via686a_data *data = via686a_update_device(dev);
570*8d5d45fbSJean Delvare 	return sprintf(buf, "%u\n", data->alarms);
571*8d5d45fbSJean Delvare }
572*8d5d45fbSJean Delvare static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
573*8d5d45fbSJean Delvare 
574*8d5d45fbSJean Delvare /* The driver. I choose to use type i2c_driver, as at is identical to both
575*8d5d45fbSJean Delvare    smbus_driver and isa_driver, and clients could be of either kind */
576*8d5d45fbSJean Delvare static struct i2c_driver via686a_driver = {
577*8d5d45fbSJean Delvare 	.owner		= THIS_MODULE,
578*8d5d45fbSJean Delvare 	.name		= "via686a",
579*8d5d45fbSJean Delvare 	.id		= I2C_DRIVERID_VIA686A,
580*8d5d45fbSJean Delvare 	.flags		= I2C_DF_NOTIFY,
581*8d5d45fbSJean Delvare 	.attach_adapter	= via686a_attach_adapter,
582*8d5d45fbSJean Delvare 	.detach_client	= via686a_detach_client,
583*8d5d45fbSJean Delvare };
584*8d5d45fbSJean Delvare 
585*8d5d45fbSJean Delvare 
586*8d5d45fbSJean Delvare /* This is called when the module is loaded */
587*8d5d45fbSJean Delvare static int via686a_attach_adapter(struct i2c_adapter *adapter)
588*8d5d45fbSJean Delvare {
589*8d5d45fbSJean Delvare 	if (!(adapter->class & I2C_CLASS_HWMON))
590*8d5d45fbSJean Delvare 		return 0;
591*8d5d45fbSJean Delvare 	return i2c_detect(adapter, &addr_data, via686a_detect);
592*8d5d45fbSJean Delvare }
593*8d5d45fbSJean Delvare 
594*8d5d45fbSJean Delvare static int via686a_detect(struct i2c_adapter *adapter, int address, int kind)
595*8d5d45fbSJean Delvare {
596*8d5d45fbSJean Delvare 	struct i2c_client *new_client;
597*8d5d45fbSJean Delvare 	struct via686a_data *data;
598*8d5d45fbSJean Delvare 	int err = 0;
599*8d5d45fbSJean Delvare 	const char client_name[] = "via686a";
600*8d5d45fbSJean Delvare 	u16 val;
601*8d5d45fbSJean Delvare 
602*8d5d45fbSJean Delvare 	/* Make sure we are probing the ISA bus!!  */
603*8d5d45fbSJean Delvare 	if (!i2c_is_isa_adapter(adapter)) {
604*8d5d45fbSJean Delvare 		dev_err(&adapter->dev,
605*8d5d45fbSJean Delvare 		"via686a_detect called for an I2C bus adapter?!?\n");
606*8d5d45fbSJean Delvare 		return 0;
607*8d5d45fbSJean Delvare 	}
608*8d5d45fbSJean Delvare 
609*8d5d45fbSJean Delvare 	/* 8231 requires multiple of 256, we enforce that on 686 as well */
610*8d5d45fbSJean Delvare 	if (force_addr)
611*8d5d45fbSJean Delvare 		address = force_addr & 0xFF00;
612*8d5d45fbSJean Delvare 
613*8d5d45fbSJean Delvare 	if (force_addr) {
614*8d5d45fbSJean Delvare 		dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
615*8d5d45fbSJean Delvare 			 address);
616*8d5d45fbSJean Delvare 		if (PCIBIOS_SUCCESSFUL !=
617*8d5d45fbSJean Delvare 		    pci_write_config_word(s_bridge, VIA686A_BASE_REG, address))
618*8d5d45fbSJean Delvare 			return -ENODEV;
619*8d5d45fbSJean Delvare 	}
620*8d5d45fbSJean Delvare 	if (PCIBIOS_SUCCESSFUL !=
621*8d5d45fbSJean Delvare 	    pci_read_config_word(s_bridge, VIA686A_ENABLE_REG, &val))
622*8d5d45fbSJean Delvare 		return -ENODEV;
623*8d5d45fbSJean Delvare 	if (!(val & 0x0001)) {
624*8d5d45fbSJean Delvare 		dev_warn(&adapter->dev, "enabling sensors\n");
625*8d5d45fbSJean Delvare 		if (PCIBIOS_SUCCESSFUL !=
626*8d5d45fbSJean Delvare 		    pci_write_config_word(s_bridge, VIA686A_ENABLE_REG,
627*8d5d45fbSJean Delvare 					  val | 0x0001))
628*8d5d45fbSJean Delvare 			return -ENODEV;
629*8d5d45fbSJean Delvare 	}
630*8d5d45fbSJean Delvare 
631*8d5d45fbSJean Delvare 	/* Reserve the ISA region */
632*8d5d45fbSJean Delvare 	if (!request_region(address, VIA686A_EXTENT, via686a_driver.name)) {
633*8d5d45fbSJean Delvare 		dev_err(&adapter->dev, "region 0x%x already in use!\n",
634*8d5d45fbSJean Delvare 			address);
635*8d5d45fbSJean Delvare 		return -ENODEV;
636*8d5d45fbSJean Delvare 	}
637*8d5d45fbSJean Delvare 
638*8d5d45fbSJean Delvare 	if (!(data = kmalloc(sizeof(struct via686a_data), GFP_KERNEL))) {
639*8d5d45fbSJean Delvare 		err = -ENOMEM;
640*8d5d45fbSJean Delvare 		goto ERROR0;
641*8d5d45fbSJean Delvare 	}
642*8d5d45fbSJean Delvare 	memset(data, 0, sizeof(struct via686a_data));
643*8d5d45fbSJean Delvare 
644*8d5d45fbSJean Delvare 	new_client = &data->client;
645*8d5d45fbSJean Delvare 	i2c_set_clientdata(new_client, data);
646*8d5d45fbSJean Delvare 	new_client->addr = address;
647*8d5d45fbSJean Delvare 	new_client->adapter = adapter;
648*8d5d45fbSJean Delvare 	new_client->driver = &via686a_driver;
649*8d5d45fbSJean Delvare 	new_client->flags = 0;
650*8d5d45fbSJean Delvare 
651*8d5d45fbSJean Delvare 	/* Fill in the remaining client fields and put into the global list */
652*8d5d45fbSJean Delvare 	strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
653*8d5d45fbSJean Delvare 
654*8d5d45fbSJean Delvare 	data->valid = 0;
655*8d5d45fbSJean Delvare 	init_MUTEX(&data->update_lock);
656*8d5d45fbSJean Delvare 	/* Tell the I2C layer a new client has arrived */
657*8d5d45fbSJean Delvare 	if ((err = i2c_attach_client(new_client)))
658*8d5d45fbSJean Delvare 		goto ERROR3;
659*8d5d45fbSJean Delvare 
660*8d5d45fbSJean Delvare 	/* Initialize the VIA686A chip */
661*8d5d45fbSJean Delvare 	via686a_init_client(new_client);
662*8d5d45fbSJean Delvare 
663*8d5d45fbSJean Delvare 	/* Register sysfs hooks */
664*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in0_input);
665*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in1_input);
666*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in2_input);
667*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in3_input);
668*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in4_input);
669*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in0_min);
670*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in1_min);
671*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in2_min);
672*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in3_min);
673*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in4_min);
674*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in0_max);
675*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in1_max);
676*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in2_max);
677*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in3_max);
678*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_in4_max);
679*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp1_input);
680*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp2_input);
681*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp3_input);
682*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp1_max);
683*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp2_max);
684*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp3_max);
685*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
686*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp2_max_hyst);
687*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_temp3_max_hyst);
688*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_fan1_input);
689*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_fan2_input);
690*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_fan1_min);
691*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_fan2_min);
692*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_fan1_div);
693*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_fan2_div);
694*8d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_alarms);
695*8d5d45fbSJean Delvare 
696*8d5d45fbSJean Delvare 	return 0;
697*8d5d45fbSJean Delvare 
698*8d5d45fbSJean Delvare ERROR3:
699*8d5d45fbSJean Delvare 	kfree(data);
700*8d5d45fbSJean Delvare ERROR0:
701*8d5d45fbSJean Delvare 	release_region(address, VIA686A_EXTENT);
702*8d5d45fbSJean Delvare 	return err;
703*8d5d45fbSJean Delvare }
704*8d5d45fbSJean Delvare 
705*8d5d45fbSJean Delvare static int via686a_detach_client(struct i2c_client *client)
706*8d5d45fbSJean Delvare {
707*8d5d45fbSJean Delvare 	int err;
708*8d5d45fbSJean Delvare 
709*8d5d45fbSJean Delvare 	if ((err = i2c_detach_client(client))) {
710*8d5d45fbSJean Delvare 		dev_err(&client->dev,
711*8d5d45fbSJean Delvare 		"Client deregistration failed, client not detached.\n");
712*8d5d45fbSJean Delvare 		return err;
713*8d5d45fbSJean Delvare 	}
714*8d5d45fbSJean Delvare 
715*8d5d45fbSJean Delvare 	release_region(client->addr, VIA686A_EXTENT);
716*8d5d45fbSJean Delvare 	kfree(i2c_get_clientdata(client));
717*8d5d45fbSJean Delvare 
718*8d5d45fbSJean Delvare 	return 0;
719*8d5d45fbSJean Delvare }
720*8d5d45fbSJean Delvare 
721*8d5d45fbSJean Delvare /* Called when we have found a new VIA686A. Set limits, etc. */
722*8d5d45fbSJean Delvare static void via686a_init_client(struct i2c_client *client)
723*8d5d45fbSJean Delvare {
724*8d5d45fbSJean Delvare 	u8 reg;
725*8d5d45fbSJean Delvare 
726*8d5d45fbSJean Delvare 	/* Start monitoring */
727*8d5d45fbSJean Delvare 	reg = via686a_read_value(client, VIA686A_REG_CONFIG);
728*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_CONFIG, (reg|0x01)&0x7F);
729*8d5d45fbSJean Delvare 
730*8d5d45fbSJean Delvare 	/* Configure temp interrupt mode for continuous-interrupt operation */
731*8d5d45fbSJean Delvare 	via686a_write_value(client, VIA686A_REG_TEMP_MODE,
732*8d5d45fbSJean Delvare 			    via686a_read_value(client, VIA686A_REG_TEMP_MODE) &
733*8d5d45fbSJean Delvare 			    !(VIA686A_TEMP_MODE_MASK | VIA686A_TEMP_MODE_CONTINUOUS));
734*8d5d45fbSJean Delvare }
735*8d5d45fbSJean Delvare 
736*8d5d45fbSJean Delvare static struct via686a_data *via686a_update_device(struct device *dev)
737*8d5d45fbSJean Delvare {
738*8d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
739*8d5d45fbSJean Delvare 	struct via686a_data *data = i2c_get_clientdata(client);
740*8d5d45fbSJean Delvare 	int i;
741*8d5d45fbSJean Delvare 
742*8d5d45fbSJean Delvare 	down(&data->update_lock);
743*8d5d45fbSJean Delvare 
744*8d5d45fbSJean Delvare 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
745*8d5d45fbSJean Delvare 	    || !data->valid) {
746*8d5d45fbSJean Delvare 		for (i = 0; i <= 4; i++) {
747*8d5d45fbSJean Delvare 			data->in[i] =
748*8d5d45fbSJean Delvare 			    via686a_read_value(client, VIA686A_REG_IN(i));
749*8d5d45fbSJean Delvare 			data->in_min[i] = via686a_read_value(client,
750*8d5d45fbSJean Delvare 							     VIA686A_REG_IN_MIN
751*8d5d45fbSJean Delvare 							     (i));
752*8d5d45fbSJean Delvare 			data->in_max[i] =
753*8d5d45fbSJean Delvare 			    via686a_read_value(client, VIA686A_REG_IN_MAX(i));
754*8d5d45fbSJean Delvare 		}
755*8d5d45fbSJean Delvare 		for (i = 1; i <= 2; i++) {
756*8d5d45fbSJean Delvare 			data->fan[i - 1] =
757*8d5d45fbSJean Delvare 			    via686a_read_value(client, VIA686A_REG_FAN(i));
758*8d5d45fbSJean Delvare 			data->fan_min[i - 1] = via686a_read_value(client,
759*8d5d45fbSJean Delvare 						     VIA686A_REG_FAN_MIN(i));
760*8d5d45fbSJean Delvare 		}
761*8d5d45fbSJean Delvare 		for (i = 0; i <= 2; i++) {
762*8d5d45fbSJean Delvare 			data->temp[i] = via686a_read_value(client,
763*8d5d45fbSJean Delvare 						 VIA686A_REG_TEMP[i]) << 2;
764*8d5d45fbSJean Delvare 			data->temp_over[i] =
765*8d5d45fbSJean Delvare 			    via686a_read_value(client,
766*8d5d45fbSJean Delvare 					       VIA686A_REG_TEMP_OVER[i]);
767*8d5d45fbSJean Delvare 			data->temp_hyst[i] =
768*8d5d45fbSJean Delvare 			    via686a_read_value(client,
769*8d5d45fbSJean Delvare 					       VIA686A_REG_TEMP_HYST[i]);
770*8d5d45fbSJean Delvare 		}
771*8d5d45fbSJean Delvare 		/* add in lower 2 bits
772*8d5d45fbSJean Delvare 		   temp1 uses bits 7-6 of VIA686A_REG_TEMP_LOW1
773*8d5d45fbSJean Delvare 		   temp2 uses bits 5-4 of VIA686A_REG_TEMP_LOW23
774*8d5d45fbSJean Delvare 		   temp3 uses bits 7-6 of VIA686A_REG_TEMP_LOW23
775*8d5d45fbSJean Delvare 		 */
776*8d5d45fbSJean Delvare 		data->temp[0] |= (via686a_read_value(client,
777*8d5d45fbSJean Delvare 						     VIA686A_REG_TEMP_LOW1)
778*8d5d45fbSJean Delvare 				  & 0xc0) >> 6;
779*8d5d45fbSJean Delvare 		data->temp[1] |=
780*8d5d45fbSJean Delvare 		    (via686a_read_value(client, VIA686A_REG_TEMP_LOW23) &
781*8d5d45fbSJean Delvare 		     0x30) >> 4;
782*8d5d45fbSJean Delvare 		data->temp[2] |=
783*8d5d45fbSJean Delvare 		    (via686a_read_value(client, VIA686A_REG_TEMP_LOW23) &
784*8d5d45fbSJean Delvare 		     0xc0) >> 6;
785*8d5d45fbSJean Delvare 
786*8d5d45fbSJean Delvare 		i = via686a_read_value(client, VIA686A_REG_FANDIV);
787*8d5d45fbSJean Delvare 		data->fan_div[0] = (i >> 4) & 0x03;
788*8d5d45fbSJean Delvare 		data->fan_div[1] = i >> 6;
789*8d5d45fbSJean Delvare 		data->alarms =
790*8d5d45fbSJean Delvare 		    via686a_read_value(client,
791*8d5d45fbSJean Delvare 				       VIA686A_REG_ALARM1) |
792*8d5d45fbSJean Delvare 		    (via686a_read_value(client, VIA686A_REG_ALARM2) << 8);
793*8d5d45fbSJean Delvare 		data->last_updated = jiffies;
794*8d5d45fbSJean Delvare 		data->valid = 1;
795*8d5d45fbSJean Delvare 	}
796*8d5d45fbSJean Delvare 
797*8d5d45fbSJean Delvare 	up(&data->update_lock);
798*8d5d45fbSJean Delvare 
799*8d5d45fbSJean Delvare 	return data;
800*8d5d45fbSJean Delvare }
801*8d5d45fbSJean Delvare 
802*8d5d45fbSJean Delvare static struct pci_device_id via686a_pci_ids[] = {
803*8d5d45fbSJean Delvare 	{ PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4) },
804*8d5d45fbSJean Delvare 	{ 0, }
805*8d5d45fbSJean Delvare };
806*8d5d45fbSJean Delvare 
807*8d5d45fbSJean Delvare MODULE_DEVICE_TABLE(pci, via686a_pci_ids);
808*8d5d45fbSJean Delvare 
809*8d5d45fbSJean Delvare static int __devinit via686a_pci_probe(struct pci_dev *dev,
810*8d5d45fbSJean Delvare 				       const struct pci_device_id *id)
811*8d5d45fbSJean Delvare {
812*8d5d45fbSJean Delvare 	u16 val;
813*8d5d45fbSJean Delvare 	int addr = 0;
814*8d5d45fbSJean Delvare 
815*8d5d45fbSJean Delvare 	if (PCIBIOS_SUCCESSFUL !=
816*8d5d45fbSJean Delvare 	    pci_read_config_word(dev, VIA686A_BASE_REG, &val))
817*8d5d45fbSJean Delvare 		return -ENODEV;
818*8d5d45fbSJean Delvare 
819*8d5d45fbSJean Delvare 	addr = val & ~(VIA686A_EXTENT - 1);
820*8d5d45fbSJean Delvare 	if (addr == 0 && force_addr == 0) {
821*8d5d45fbSJean Delvare 		dev_err(&dev->dev, "base address not set - upgrade BIOS "
822*8d5d45fbSJean Delvare 			"or use force_addr=0xaddr\n");
823*8d5d45fbSJean Delvare 		return -ENODEV;
824*8d5d45fbSJean Delvare 	}
825*8d5d45fbSJean Delvare 	if (force_addr)
826*8d5d45fbSJean Delvare 		addr = force_addr;	/* so detect will get called */
827*8d5d45fbSJean Delvare 
828*8d5d45fbSJean Delvare 	if (!addr) {
829*8d5d45fbSJean Delvare 		dev_err(&dev->dev, "No Via 686A sensors found.\n");
830*8d5d45fbSJean Delvare 		return -ENODEV;
831*8d5d45fbSJean Delvare 	}
832*8d5d45fbSJean Delvare 	normal_isa[0] = addr;
833*8d5d45fbSJean Delvare 
834*8d5d45fbSJean Delvare 	s_bridge = pci_dev_get(dev);
835*8d5d45fbSJean Delvare 	if (i2c_add_driver(&via686a_driver)) {
836*8d5d45fbSJean Delvare 		pci_dev_put(s_bridge);
837*8d5d45fbSJean Delvare 		s_bridge = NULL;
838*8d5d45fbSJean Delvare 	}
839*8d5d45fbSJean Delvare 
840*8d5d45fbSJean Delvare 	/* Always return failure here.  This is to allow other drivers to bind
841*8d5d45fbSJean Delvare 	 * to this pci device.  We don't really want to have control over the
842*8d5d45fbSJean Delvare 	 * pci device, we only wanted to read as few register values from it.
843*8d5d45fbSJean Delvare 	 */
844*8d5d45fbSJean Delvare 	return -ENODEV;
845*8d5d45fbSJean Delvare }
846*8d5d45fbSJean Delvare 
847*8d5d45fbSJean Delvare static struct pci_driver via686a_pci_driver = {
848*8d5d45fbSJean Delvare 	.name		= "via686a",
849*8d5d45fbSJean Delvare 	.id_table	= via686a_pci_ids,
850*8d5d45fbSJean Delvare 	.probe		= via686a_pci_probe,
851*8d5d45fbSJean Delvare };
852*8d5d45fbSJean Delvare 
853*8d5d45fbSJean Delvare static int __init sm_via686a_init(void)
854*8d5d45fbSJean Delvare {
855*8d5d45fbSJean Delvare 	return pci_register_driver(&via686a_pci_driver);
856*8d5d45fbSJean Delvare }
857*8d5d45fbSJean Delvare 
858*8d5d45fbSJean Delvare static void __exit sm_via686a_exit(void)
859*8d5d45fbSJean Delvare {
860*8d5d45fbSJean Delvare 	pci_unregister_driver(&via686a_pci_driver);
861*8d5d45fbSJean Delvare 	if (s_bridge != NULL) {
862*8d5d45fbSJean Delvare 		i2c_del_driver(&via686a_driver);
863*8d5d45fbSJean Delvare 		pci_dev_put(s_bridge);
864*8d5d45fbSJean Delvare 		s_bridge = NULL;
865*8d5d45fbSJean Delvare 	}
866*8d5d45fbSJean Delvare }
867*8d5d45fbSJean Delvare 
868*8d5d45fbSJean Delvare MODULE_AUTHOR("Ky�sti M�lkki <kmalkki@cc.hut.fi>, "
869*8d5d45fbSJean Delvare 	      "Mark Studebaker <mdsxyz123@yahoo.com> "
870*8d5d45fbSJean Delvare 	      "and Bob Dougherty <bobd@stanford.edu>");
871*8d5d45fbSJean Delvare MODULE_DESCRIPTION("VIA 686A Sensor device");
872*8d5d45fbSJean Delvare MODULE_LICENSE("GPL");
873*8d5d45fbSJean Delvare 
874*8d5d45fbSJean Delvare module_init(sm_via686a_init);
875*8d5d45fbSJean Delvare module_exit(sm_via686a_exit);
876