xref: /linux/drivers/hwmon/pc87360.c (revision 20d0021394c1b070bf04b22c5bc8fdb437edd4c5)
1 /*
2  *  pc87360.c - Part of lm_sensors, Linux kernel modules
3  *              for hardware monitoring
4  *  Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
5  *
6  *  Copied from smsc47m1.c:
7  *  Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  Supports the following chips:
24  *
25  *  Chip        #vin    #fan    #pwm    #temp   devid
26  *  PC87360     -       2       2       -       0xE1
27  *  PC87363     -       2       2       -       0xE8
28  *  PC87364     -       3       3       -       0xE4
29  *  PC87365     11      3       3       2       0xE5
30  *  PC87366     11      3       3       3-4     0xE9
31  *
32  *  This driver assumes that no more than one chip is present, and one of
33  *  the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
34  */
35 
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-sensor.h>
42 #include <linux/i2c-vid.h>
43 #include <asm/io.h>
44 
45 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
46 static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
47 static struct i2c_force_data forces[] = {{ NULL }};
48 static u8 devid;
49 static unsigned int extra_isa[3];
50 static u8 confreg[4];
51 
52 enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
53 static struct i2c_address_data addr_data = {
54 	.normal_i2c		= normal_i2c,
55 	.normal_isa		= normal_isa,
56 	.forces			= forces,
57 };
58 
59 static int init = 1;
60 module_param(init, int, 0);
61 MODULE_PARM_DESC(init,
62  "Chip initialization level:\n"
63  " 0: None\n"
64  "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
65  " 2: Forcibly enable all voltage and temperature channels, except in9\n"
66  " 3: Forcibly enable all voltage and temperature channels, including in9");
67 
68 /*
69  * Super-I/O registers and operations
70  */
71 
72 #define DEV	0x07	/* Register: Logical device select */
73 #define DEVID	0x20	/* Register: Device ID */
74 #define ACT	0x30	/* Register: Device activation */
75 #define BASE	0x60	/* Register: Base address */
76 
77 #define FSCM	0x09	/* Logical device: fans */
78 #define VLM	0x0d	/* Logical device: voltages */
79 #define TMS	0x0e	/* Logical device: temperatures */
80 static const u8 logdev[3] = { FSCM, VLM, TMS };
81 
82 #define LD_FAN		0
83 #define LD_IN		1
84 #define LD_TEMP		2
85 
86 static inline void superio_outb(int sioaddr, int reg, int val)
87 {
88 	outb(reg, sioaddr);
89 	outb(val, sioaddr+1);
90 }
91 
92 static inline int superio_inb(int sioaddr, int reg)
93 {
94 	outb(reg, sioaddr);
95 	return inb(sioaddr+1);
96 }
97 
98 static inline void superio_exit(int sioaddr)
99 {
100 	outb(0x02, sioaddr);
101 	outb(0x02, sioaddr+1);
102 }
103 
104 /*
105  * Logical devices
106  */
107 
108 #define PC87360_EXTENT		0x10
109 #define PC87365_REG_BANK	0x09
110 #define NO_BANK			0xff
111 
112 /*
113  * Fan registers and conversions
114  */
115 
116 /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
117 #define PC87360_REG_PRESCALE(nr)	(0x00 + 2 * (nr))
118 #define PC87360_REG_PWM(nr)		(0x01 + 2 * (nr))
119 #define PC87360_REG_FAN_MIN(nr)		(0x06 + 3 * (nr))
120 #define PC87360_REG_FAN(nr)		(0x07 + 3 * (nr))
121 #define PC87360_REG_FAN_STATUS(nr)	(0x08 + 3 * (nr))
122 
123 #define FAN_FROM_REG(val,div)		((val) == 0 ? 0: \
124 					 480000 / ((val)*(div)))
125 #define FAN_TO_REG(val,div)		((val) <= 100 ? 0 : \
126 					 480000 / ((val)*(div)))
127 #define FAN_DIV_FROM_REG(val)		(1 << ((val >> 5) & 0x03))
128 #define FAN_STATUS_FROM_REG(val)	((val) & 0x07)
129 
130 #define FAN_CONFIG_MONITOR(val,nr)	(((val) >> (2 + nr * 3)) & 1)
131 #define FAN_CONFIG_CONTROL(val,nr)	(((val) >> (3 + nr * 3)) & 1)
132 #define FAN_CONFIG_INVERT(val,nr)	(((val) >> (4 + nr * 3)) & 1)
133 
134 #define PWM_FROM_REG(val,inv)		((inv) ? 255 - (val) : (val))
135 static inline u8 PWM_TO_REG(int val, int inv)
136 {
137 	if (inv)
138 		val = 255 - val;
139 	if (val < 0)
140 		return 0;
141 	if (val > 255)
142 		return 255;
143 	return val;
144 }
145 
146 /*
147  * Voltage registers and conversions
148  */
149 
150 #define PC87365_REG_IN_CONVRATE		0x07
151 #define PC87365_REG_IN_CONFIG		0x08
152 #define PC87365_REG_IN			0x0B
153 #define PC87365_REG_IN_MIN		0x0D
154 #define PC87365_REG_IN_MAX		0x0C
155 #define PC87365_REG_IN_STATUS		0x0A
156 #define PC87365_REG_IN_ALARMS1		0x00
157 #define PC87365_REG_IN_ALARMS2		0x01
158 #define PC87365_REG_VID			0x06
159 
160 #define IN_FROM_REG(val,ref)		(((val) * (ref) + 128) / 256)
161 #define IN_TO_REG(val,ref)		((val) < 0 ? 0 : \
162 					 (val)*256 >= (ref)*255 ? 255: \
163 					 ((val) * 256 + (ref)/2) / (ref))
164 
165 /*
166  * Temperature registers and conversions
167  */
168 
169 #define PC87365_REG_TEMP_CONFIG		0x08
170 #define PC87365_REG_TEMP		0x0B
171 #define PC87365_REG_TEMP_MIN		0x0D
172 #define PC87365_REG_TEMP_MAX		0x0C
173 #define PC87365_REG_TEMP_CRIT		0x0E
174 #define PC87365_REG_TEMP_STATUS		0x0A
175 #define PC87365_REG_TEMP_ALARMS		0x00
176 
177 #define TEMP_FROM_REG(val)		((val) * 1000)
178 #define TEMP_TO_REG(val)		((val) < -55000 ? -55 : \
179 					 (val) > 127000 ? 127 : \
180 					 (val) < 0 ? ((val) - 500) / 1000 : \
181 					 ((val) + 500) / 1000)
182 
183 /*
184  * Client data (each client gets its own)
185  */
186 
187 struct pc87360_data {
188 	struct i2c_client client;
189 	struct semaphore lock;
190 	struct semaphore update_lock;
191 	char valid;		/* !=0 if following fields are valid */
192 	unsigned long last_updated;	/* In jiffies */
193 
194 	int address[3];
195 
196 	u8 fannr, innr, tempnr;
197 
198 	u8 fan[3];		/* Register value */
199 	u8 fan_min[3];		/* Register value */
200 	u8 fan_status[3];	/* Register value */
201 	u8 pwm[3];		/* Register value */
202 	u16 fan_conf;		/* Configuration register values, combined */
203 
204 	u16 in_vref;		/* 1 mV/bit */
205 	u8 in[14];		/* Register value */
206 	u8 in_min[14];		/* Register value */
207 	u8 in_max[14];		/* Register value */
208 	u8 in_crit[3];		/* Register value */
209 	u8 in_status[14];	/* Register value */
210 	u16 in_alarms;		/* Register values, combined, masked */
211 	u8 vid_conf;		/* Configuration register value */
212 	u8 vrm;
213 	u8 vid;			/* Register value */
214 
215 	s8 temp[3];		/* Register value */
216 	s8 temp_min[3];		/* Register value */
217 	s8 temp_max[3];		/* Register value */
218 	s8 temp_crit[3];	/* Register value */
219 	u8 temp_status[3];	/* Register value */
220 	u8 temp_alarms;		/* Register value, masked */
221 };
222 
223 /*
224  * Functions declaration
225  */
226 
227 static int pc87360_attach_adapter(struct i2c_adapter *adapter);
228 static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
229 static int pc87360_detach_client(struct i2c_client *client);
230 
231 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
232 			      u8 reg);
233 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
234 				u8 reg, u8 value);
235 static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
236 static struct pc87360_data *pc87360_update_device(struct device *dev);
237 
238 /*
239  * Driver data (common to all clients)
240  */
241 
242 static struct i2c_driver pc87360_driver = {
243 	.owner		= THIS_MODULE,
244 	.name		= "pc87360",
245 	.flags		= I2C_DF_NOTIFY,
246 	.attach_adapter	= pc87360_attach_adapter,
247 	.detach_client	= pc87360_detach_client,
248 };
249 
250 /*
251  * Sysfs stuff
252  */
253 
254 static ssize_t set_fan_min(struct device *dev, const char *buf,
255 	size_t count, int nr)
256 {
257 	struct i2c_client *client = to_i2c_client(dev);
258 	struct pc87360_data *data = i2c_get_clientdata(client);
259 	long fan_min = simple_strtol(buf, NULL, 10);
260 
261 	down(&data->update_lock);
262 	fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
263 
264 	/* If it wouldn't fit, change clock divisor */
265 	while (fan_min > 255
266 	    && (data->fan_status[nr] & 0x60) != 0x60) {
267 		fan_min >>= 1;
268 		data->fan[nr] >>= 1;
269 		data->fan_status[nr] += 0x20;
270 	}
271 	data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
272 	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
273 			    data->fan_min[nr]);
274 
275 	/* Write new divider, preserve alarm bits */
276 	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
277 			    data->fan_status[nr] & 0xF9);
278 	up(&data->update_lock);
279 
280 	return count;
281 }
282 
283 #define show_and_set_fan(offset) \
284 static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
285 { \
286 	struct pc87360_data *data = pc87360_update_device(dev); \
287 	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
288 		       FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
289 } \
290 static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
291 { \
292 	struct pc87360_data *data = pc87360_update_device(dev); \
293 	return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
294 		       FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
295 } \
296 static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
297 { \
298 	struct pc87360_data *data = pc87360_update_device(dev); \
299 	return sprintf(buf, "%u\n", \
300 		       FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
301 } \
302 static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
303 { \
304 	struct pc87360_data *data = pc87360_update_device(dev); \
305 	return sprintf(buf, "%u\n", \
306 		       FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
307 } \
308 static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
309 	size_t count) \
310 { \
311 	return set_fan_min(dev, buf, count, offset-1); \
312 } \
313 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
314 	show_fan##offset##_input, NULL); \
315 static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
316 	show_fan##offset##_min, set_fan##offset##_min); \
317 static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
318 	show_fan##offset##_div, NULL); \
319 static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
320 	show_fan##offset##_status, NULL);
321 show_and_set_fan(1)
322 show_and_set_fan(2)
323 show_and_set_fan(3)
324 
325 #define show_and_set_pwm(offset) \
326 static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
327 { \
328 	struct pc87360_data *data = pc87360_update_device(dev); \
329 	return sprintf(buf, "%u\n", \
330 		       PWM_FROM_REG(data->pwm[offset-1], \
331 				    FAN_CONFIG_INVERT(data->fan_conf, \
332 						      offset-1))); \
333 } \
334 static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
335 	size_t count) \
336 { \
337 	struct i2c_client *client = to_i2c_client(dev); \
338 	struct pc87360_data *data = i2c_get_clientdata(client); \
339 	long val = simple_strtol(buf, NULL, 10); \
340  \
341 	down(&data->update_lock); \
342 	data->pwm[offset-1] = PWM_TO_REG(val, \
343 			      FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
344 	pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
345 			    data->pwm[offset-1]); \
346 	up(&data->update_lock); \
347 	return count; \
348 } \
349 static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
350 	show_pwm##offset, set_pwm##offset);
351 show_and_set_pwm(1)
352 show_and_set_pwm(2)
353 show_and_set_pwm(3)
354 
355 #define show_and_set_in(offset) \
356 static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
357 { \
358 	struct pc87360_data *data = pc87360_update_device(dev); \
359 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
360 		       data->in_vref)); \
361 } \
362 static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
363 { \
364 	struct pc87360_data *data = pc87360_update_device(dev); \
365 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
366 		       data->in_vref)); \
367 } \
368 static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
369 { \
370 	struct pc87360_data *data = pc87360_update_device(dev); \
371 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
372 		       data->in_vref)); \
373 } \
374 static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
375 { \
376 	struct pc87360_data *data = pc87360_update_device(dev); \
377 	return sprintf(buf, "%u\n", data->in_status[offset]); \
378 } \
379 static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
380 	size_t count) \
381 { \
382 	struct i2c_client *client = to_i2c_client(dev); \
383 	struct pc87360_data *data = i2c_get_clientdata(client); \
384 	long val = simple_strtol(buf, NULL, 10); \
385  \
386 	down(&data->update_lock); \
387 	data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
388 	pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
389 			    data->in_min[offset]); \
390 	up(&data->update_lock); \
391 	return count; \
392 } \
393 static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
394 	size_t count) \
395 { \
396 	struct i2c_client *client = to_i2c_client(dev); \
397 	struct pc87360_data *data = i2c_get_clientdata(client); \
398 	long val = simple_strtol(buf, NULL, 10); \
399  \
400 	down(&data->update_lock); \
401 	data->in_max[offset] = IN_TO_REG(val, \
402 			       data->in_vref); \
403 	pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
404 			    data->in_max[offset]); \
405 	up(&data->update_lock); \
406 	return count; \
407 } \
408 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
409 	show_in##offset##_input, NULL); \
410 static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
411 	show_in##offset##_min, set_in##offset##_min); \
412 static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
413 	show_in##offset##_max, set_in##offset##_max); \
414 static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
415 	show_in##offset##_status, NULL);
416 show_and_set_in(0)
417 show_and_set_in(1)
418 show_and_set_in(2)
419 show_and_set_in(3)
420 show_and_set_in(4)
421 show_and_set_in(5)
422 show_and_set_in(6)
423 show_and_set_in(7)
424 show_and_set_in(8)
425 show_and_set_in(9)
426 show_and_set_in(10)
427 
428 #define show_and_set_therm(offset) \
429 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
430 { \
431 	struct pc87360_data *data = pc87360_update_device(dev); \
432 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
433 		       data->in_vref)); \
434 } \
435 static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
436 { \
437 	struct pc87360_data *data = pc87360_update_device(dev); \
438 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
439 		       data->in_vref)); \
440 } \
441 static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
442 { \
443 	struct pc87360_data *data = pc87360_update_device(dev); \
444 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
445 		       data->in_vref)); \
446 } \
447 static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
448 { \
449 	struct pc87360_data *data = pc87360_update_device(dev); \
450 	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
451 		       data->in_vref)); \
452 } \
453 static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
454 { \
455 	struct pc87360_data *data = pc87360_update_device(dev); \
456 	return sprintf(buf, "%u\n", data->in_status[offset+7]); \
457 } \
458 static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
459 	size_t count) \
460 { \
461 	struct i2c_client *client = to_i2c_client(dev); \
462 	struct pc87360_data *data = i2c_get_clientdata(client); \
463 	long val = simple_strtol(buf, NULL, 10); \
464  \
465 	down(&data->update_lock); \
466 	data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
467 	pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
468 			    data->in_min[offset+7]); \
469 	up(&data->update_lock); \
470 	return count; \
471 } \
472 static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
473 	size_t count) \
474 { \
475 	struct i2c_client *client = to_i2c_client(dev); \
476 	struct pc87360_data *data = i2c_get_clientdata(client); \
477 	long val = simple_strtol(buf, NULL, 10); \
478  \
479 	down(&data->update_lock); \
480 	data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
481 	pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
482 			    data->in_max[offset+7]); \
483 	up(&data->update_lock); \
484 	return count; \
485 } \
486 static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
487 	size_t count) \
488 { \
489 	struct i2c_client *client = to_i2c_client(dev); \
490 	struct pc87360_data *data = i2c_get_clientdata(client); \
491 	long val = simple_strtol(buf, NULL, 10); \
492  \
493 	down(&data->update_lock); \
494 	data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
495 	pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
496 			    data->in_crit[offset-4]); \
497 	up(&data->update_lock); \
498 	return count; \
499 } \
500 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
501 	show_temp##offset##_input, NULL); \
502 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
503 	show_temp##offset##_min, set_temp##offset##_min); \
504 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
505 	show_temp##offset##_max, set_temp##offset##_max); \
506 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
507 	show_temp##offset##_crit, set_temp##offset##_crit); \
508 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
509 	show_temp##offset##_status, NULL);
510 show_and_set_therm(4)
511 show_and_set_therm(5)
512 show_and_set_therm(6)
513 
514 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
515 {
516 	struct pc87360_data *data = pc87360_update_device(dev);
517 	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
518 }
519 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
520 
521 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
522 {
523 	struct pc87360_data *data = pc87360_update_device(dev);
524 	return sprintf(buf, "%u\n", data->vrm);
525 }
526 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
527 {
528 	struct i2c_client *client = to_i2c_client(dev);
529 	struct pc87360_data *data = i2c_get_clientdata(client);
530 	data->vrm = simple_strtoul(buf, NULL, 10);
531 	return count;
532 }
533 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
534 
535 static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
536 {
537 	struct pc87360_data *data = pc87360_update_device(dev);
538 	return sprintf(buf, "%u\n", data->in_alarms);
539 }
540 static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
541 
542 #define show_and_set_temp(offset) \
543 static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
544 { \
545 	struct pc87360_data *data = pc87360_update_device(dev); \
546 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
547 } \
548 static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
549 { \
550 	struct pc87360_data *data = pc87360_update_device(dev); \
551 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
552 } \
553 static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
554 { \
555 	struct pc87360_data *data = pc87360_update_device(dev); \
556 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
557 }\
558 static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
559 { \
560 	struct pc87360_data *data = pc87360_update_device(dev); \
561 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
562 }\
563 static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
564 { \
565 	struct pc87360_data *data = pc87360_update_device(dev); \
566 	return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
567 }\
568 static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
569 	size_t count) \
570 { \
571 	struct i2c_client *client = to_i2c_client(dev); \
572 	struct pc87360_data *data = i2c_get_clientdata(client); \
573 	long val = simple_strtol(buf, NULL, 10); \
574  \
575 	down(&data->update_lock); \
576 	data->temp_min[offset-1] = TEMP_TO_REG(val); \
577 	pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
578 			    data->temp_min[offset-1]); \
579 	up(&data->update_lock); \
580 	return count; \
581 } \
582 static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
583 	size_t count) \
584 { \
585 	struct i2c_client *client = to_i2c_client(dev); \
586 	struct pc87360_data *data = i2c_get_clientdata(client); \
587 	long val = simple_strtol(buf, NULL, 10); \
588  \
589 	down(&data->update_lock); \
590 	data->temp_max[offset-1] = TEMP_TO_REG(val); \
591 	pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
592 			    data->temp_max[offset-1]); \
593 	up(&data->update_lock); \
594 	return count; \
595 } \
596 static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
597 	size_t count) \
598 { \
599 	struct i2c_client *client = to_i2c_client(dev); \
600 	struct pc87360_data *data = i2c_get_clientdata(client); \
601 	long val = simple_strtol(buf, NULL, 10); \
602  \
603 	down(&data->update_lock); \
604 	data->temp_crit[offset-1] = TEMP_TO_REG(val); \
605 	pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
606 			    data->temp_crit[offset-1]); \
607 	up(&data->update_lock); \
608 	return count; \
609 } \
610 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
611 	show_temp##offset##_input, NULL); \
612 static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
613 	show_temp##offset##_min, set_temp##offset##_min); \
614 static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
615 	show_temp##offset##_max, set_temp##offset##_max); \
616 static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
617 	show_temp##offset##_crit, set_temp##offset##_crit); \
618 static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
619 	show_temp##offset##_status, NULL);
620 show_and_set_temp(1)
621 show_and_set_temp(2)
622 show_and_set_temp(3)
623 
624 static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
625 {
626 	struct pc87360_data *data = pc87360_update_device(dev);
627 	return sprintf(buf, "%u\n", data->temp_alarms);
628 }
629 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
630 
631 /*
632  * Device detection, registration and update
633  */
634 
635 static int pc87360_attach_adapter(struct i2c_adapter *adapter)
636 {
637 	return i2c_detect(adapter, &addr_data, pc87360_detect);
638 }
639 
640 static int pc87360_find(int sioaddr, u8 *devid, int *address)
641 {
642 	u16 val;
643 	int i;
644 	int nrdev; /* logical device count */
645 
646 	/* No superio_enter */
647 
648 	/* Identify device */
649 	val = superio_inb(sioaddr, DEVID);
650 	switch (val) {
651 	case 0xE1: /* PC87360 */
652 	case 0xE8: /* PC87363 */
653 	case 0xE4: /* PC87364 */
654 		nrdev = 1;
655 		break;
656 	case 0xE5: /* PC87365 */
657 	case 0xE9: /* PC87366 */
658 		nrdev = 3;
659 		break;
660 	default:
661 		superio_exit(sioaddr);
662 		return -ENODEV;
663 	}
664 	/* Remember the device id */
665 	*devid = val;
666 
667 	for (i = 0; i < nrdev; i++) {
668 		/* select logical device */
669 		superio_outb(sioaddr, DEV, logdev[i]);
670 
671 		val = superio_inb(sioaddr, ACT);
672 		if (!(val & 0x01)) {
673 			printk(KERN_INFO "pc87360: Device 0x%02x not "
674 			       "activated\n", logdev[i]);
675 			continue;
676 		}
677 
678 		val = (superio_inb(sioaddr, BASE) << 8)
679 		    | superio_inb(sioaddr, BASE + 1);
680 		if (!val) {
681 			printk(KERN_INFO "pc87360: Base address not set for "
682 			       "device 0x%02x\n", logdev[i]);
683 			continue;
684 		}
685 
686 		address[i] = val;
687 
688 		if (i==0) { /* Fans */
689 			confreg[0] = superio_inb(sioaddr, 0xF0);
690 			confreg[1] = superio_inb(sioaddr, 0xF1);
691 
692 #ifdef DEBUG
693 			printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
694 			       "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
695 			       (confreg[0]>>3)&1, (confreg[0]>>4)&1);
696 			printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
697 			       "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
698 			       (confreg[0]>>6)&1, (confreg[0]>>7)&1);
699 			printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
700 			       "ctrl=%d inv=%d\n", confreg[1]&1,
701 			       (confreg[1]>>1)&1, (confreg[1]>>2)&1);
702 #endif
703 		} else if (i==1) { /* Voltages */
704 			/* Are we using thermistors? */
705 			if (*devid == 0xE9) { /* PC87366 */
706 				/* These registers are not logical-device
707 				   specific, just that we won't need them if
708 				   we don't use the VLM device */
709 				confreg[2] = superio_inb(sioaddr, 0x2B);
710 				confreg[3] = superio_inb(sioaddr, 0x25);
711 
712 				if (confreg[2] & 0x40) {
713 					printk(KERN_INFO "pc87360: Using "
714 					       "thermistors for temperature "
715 					       "monitoring\n");
716 				}
717 				if (confreg[3] & 0xE0) {
718 					printk(KERN_INFO "pc87360: VID "
719 					       "inputs routed (mode %u)\n",
720 					       confreg[3] >> 5);
721 				}
722 			}
723 		}
724 	}
725 
726 	superio_exit(sioaddr);
727 	return 0;
728 }
729 
730 /* We don't really care about the address.
731    Read from extra_isa instead. */
732 int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
733 {
734 	int i;
735 	struct i2c_client *new_client;
736 	struct pc87360_data *data;
737 	int err = 0;
738 	const char *name = "pc87360";
739 	int use_thermistors = 0;
740 
741 	if (!i2c_is_isa_adapter(adapter))
742 		return -ENODEV;
743 
744 	if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
745 		return -ENOMEM;
746 	memset(data, 0x00, sizeof(struct pc87360_data));
747 
748 	new_client = &data->client;
749 	i2c_set_clientdata(new_client, data);
750 	new_client->addr = address;
751 	init_MUTEX(&data->lock);
752 	new_client->adapter = adapter;
753 	new_client->driver = &pc87360_driver;
754 	new_client->flags = 0;
755 
756 	data->fannr = 2;
757 	data->innr = 0;
758 	data->tempnr = 0;
759 
760 	switch (devid) {
761 	case 0xe8:
762 		name = "pc87363";
763 		break;
764 	case 0xe4:
765 		name = "pc87364";
766 		data->fannr = 3;
767 		break;
768 	case 0xe5:
769 		name = "pc87365";
770 		data->fannr = extra_isa[0] ? 3 : 0;
771 		data->innr = extra_isa[1] ? 11 : 0;
772 		data->tempnr = extra_isa[2] ? 2 : 0;
773 		break;
774 	case 0xe9:
775 		name = "pc87366";
776 		data->fannr = extra_isa[0] ? 3 : 0;
777 		data->innr = extra_isa[1] ? 14 : 0;
778 		data->tempnr = extra_isa[2] ? 3 : 0;
779 		break;
780 	}
781 
782 	strcpy(new_client->name, name);
783 	data->valid = 0;
784 	init_MUTEX(&data->update_lock);
785 
786 	for (i = 0; i < 3; i++) {
787 		if (((data->address[i] = extra_isa[i]))
788 		 && !request_region(extra_isa[i], PC87360_EXTENT,
789 		 		    pc87360_driver.name)) {
790 			dev_err(&new_client->dev, "Region 0x%x-0x%x already "
791 				"in use!\n", extra_isa[i],
792 				extra_isa[i]+PC87360_EXTENT-1);
793 			for (i--; i >= 0; i--)
794 				release_region(extra_isa[i], PC87360_EXTENT);
795 			err = -EBUSY;
796 			goto ERROR1;
797 		}
798 	}
799 
800 	/* Retrieve the fans configuration from Super-I/O space */
801 	if (data->fannr)
802 		data->fan_conf = confreg[0] | (confreg[1] << 8);
803 
804 	if ((err = i2c_attach_client(new_client)))
805 		goto ERROR2;
806 
807 	/* Use the correct reference voltage
808 	   Unless both the VLM and the TMS logical devices agree to
809 	   use an external Vref, the internal one is used. */
810 	if (data->innr) {
811 		i = pc87360_read_value(data, LD_IN, NO_BANK,
812 				       PC87365_REG_IN_CONFIG);
813 		if (data->tempnr) {
814 			i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
815 						PC87365_REG_TEMP_CONFIG);
816 		}
817 		data->in_vref = (i&0x02) ? 3025 : 2966;
818 		dev_dbg(&new_client->dev, "Using %s reference voltage\n",
819 			(i&0x02) ? "external" : "internal");
820 
821 		data->vid_conf = confreg[3];
822 		data->vrm = 90;
823 	}
824 
825 	/* Fan clock dividers may be needed before any data is read */
826 	for (i = 0; i < data->fannr; i++) {
827 		if (FAN_CONFIG_MONITOR(data->fan_conf, i))
828 			data->fan_status[i] = pc87360_read_value(data,
829 					      LD_FAN, NO_BANK,
830 					      PC87360_REG_FAN_STATUS(i));
831 	}
832 
833 	if (init > 0) {
834 		if (devid == 0xe9 && data->address[1]) /* PC87366 */
835 			use_thermistors = confreg[2] & 0x40;
836 
837 		pc87360_init_client(new_client, use_thermistors);
838 	}
839 
840 	/* Register sysfs hooks */
841 	if (data->innr) {
842 		device_create_file(&new_client->dev, &dev_attr_in0_input);
843 		device_create_file(&new_client->dev, &dev_attr_in1_input);
844 		device_create_file(&new_client->dev, &dev_attr_in2_input);
845 		device_create_file(&new_client->dev, &dev_attr_in3_input);
846 		device_create_file(&new_client->dev, &dev_attr_in4_input);
847 		device_create_file(&new_client->dev, &dev_attr_in5_input);
848 		device_create_file(&new_client->dev, &dev_attr_in6_input);
849 		device_create_file(&new_client->dev, &dev_attr_in7_input);
850 		device_create_file(&new_client->dev, &dev_attr_in8_input);
851 		device_create_file(&new_client->dev, &dev_attr_in9_input);
852 		device_create_file(&new_client->dev, &dev_attr_in10_input);
853 		device_create_file(&new_client->dev, &dev_attr_in0_min);
854 		device_create_file(&new_client->dev, &dev_attr_in1_min);
855 		device_create_file(&new_client->dev, &dev_attr_in2_min);
856 		device_create_file(&new_client->dev, &dev_attr_in3_min);
857 		device_create_file(&new_client->dev, &dev_attr_in4_min);
858 		device_create_file(&new_client->dev, &dev_attr_in5_min);
859 		device_create_file(&new_client->dev, &dev_attr_in6_min);
860 		device_create_file(&new_client->dev, &dev_attr_in7_min);
861 		device_create_file(&new_client->dev, &dev_attr_in8_min);
862 		device_create_file(&new_client->dev, &dev_attr_in9_min);
863 		device_create_file(&new_client->dev, &dev_attr_in10_min);
864 		device_create_file(&new_client->dev, &dev_attr_in0_max);
865 		device_create_file(&new_client->dev, &dev_attr_in1_max);
866 		device_create_file(&new_client->dev, &dev_attr_in2_max);
867 		device_create_file(&new_client->dev, &dev_attr_in3_max);
868 		device_create_file(&new_client->dev, &dev_attr_in4_max);
869 		device_create_file(&new_client->dev, &dev_attr_in5_max);
870 		device_create_file(&new_client->dev, &dev_attr_in6_max);
871 		device_create_file(&new_client->dev, &dev_attr_in7_max);
872 		device_create_file(&new_client->dev, &dev_attr_in8_max);
873 		device_create_file(&new_client->dev, &dev_attr_in9_max);
874 		device_create_file(&new_client->dev, &dev_attr_in10_max);
875 		device_create_file(&new_client->dev, &dev_attr_in0_status);
876 		device_create_file(&new_client->dev, &dev_attr_in1_status);
877 		device_create_file(&new_client->dev, &dev_attr_in2_status);
878 		device_create_file(&new_client->dev, &dev_attr_in3_status);
879 		device_create_file(&new_client->dev, &dev_attr_in4_status);
880 		device_create_file(&new_client->dev, &dev_attr_in5_status);
881 		device_create_file(&new_client->dev, &dev_attr_in6_status);
882 		device_create_file(&new_client->dev, &dev_attr_in7_status);
883 		device_create_file(&new_client->dev, &dev_attr_in8_status);
884 		device_create_file(&new_client->dev, &dev_attr_in9_status);
885 		device_create_file(&new_client->dev, &dev_attr_in10_status);
886 
887 		device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
888 		device_create_file(&new_client->dev, &dev_attr_vrm);
889 		device_create_file(&new_client->dev, &dev_attr_alarms_in);
890 	}
891 
892 	if (data->tempnr) {
893 		device_create_file(&new_client->dev, &dev_attr_temp1_input);
894 		device_create_file(&new_client->dev, &dev_attr_temp2_input);
895 		device_create_file(&new_client->dev, &dev_attr_temp1_min);
896 		device_create_file(&new_client->dev, &dev_attr_temp2_min);
897 		device_create_file(&new_client->dev, &dev_attr_temp1_max);
898 		device_create_file(&new_client->dev, &dev_attr_temp2_max);
899 		device_create_file(&new_client->dev, &dev_attr_temp1_crit);
900 		device_create_file(&new_client->dev, &dev_attr_temp2_crit);
901 		device_create_file(&new_client->dev, &dev_attr_temp1_status);
902 		device_create_file(&new_client->dev, &dev_attr_temp2_status);
903 
904 		device_create_file(&new_client->dev, &dev_attr_alarms_temp);
905 	}
906 	if (data->tempnr == 3) {
907 		device_create_file(&new_client->dev, &dev_attr_temp3_input);
908 		device_create_file(&new_client->dev, &dev_attr_temp3_min);
909 		device_create_file(&new_client->dev, &dev_attr_temp3_max);
910 		device_create_file(&new_client->dev, &dev_attr_temp3_crit);
911 		device_create_file(&new_client->dev, &dev_attr_temp3_status);
912 	}
913 	if (data->innr == 14) {
914 		device_create_file(&new_client->dev, &dev_attr_temp4_input);
915 		device_create_file(&new_client->dev, &dev_attr_temp5_input);
916 		device_create_file(&new_client->dev, &dev_attr_temp6_input);
917 		device_create_file(&new_client->dev, &dev_attr_temp4_min);
918 		device_create_file(&new_client->dev, &dev_attr_temp5_min);
919 		device_create_file(&new_client->dev, &dev_attr_temp6_min);
920 		device_create_file(&new_client->dev, &dev_attr_temp4_max);
921 		device_create_file(&new_client->dev, &dev_attr_temp5_max);
922 		device_create_file(&new_client->dev, &dev_attr_temp6_max);
923 		device_create_file(&new_client->dev, &dev_attr_temp4_crit);
924 		device_create_file(&new_client->dev, &dev_attr_temp5_crit);
925 		device_create_file(&new_client->dev, &dev_attr_temp6_crit);
926 		device_create_file(&new_client->dev, &dev_attr_temp4_status);
927 		device_create_file(&new_client->dev, &dev_attr_temp5_status);
928 		device_create_file(&new_client->dev, &dev_attr_temp6_status);
929 	}
930 
931 	if (data->fannr) {
932 		if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
933 			device_create_file(&new_client->dev,
934 					   &dev_attr_fan1_input);
935 			device_create_file(&new_client->dev,
936 					   &dev_attr_fan1_min);
937 			device_create_file(&new_client->dev,
938 					   &dev_attr_fan1_div);
939 			device_create_file(&new_client->dev,
940 					   &dev_attr_fan1_status);
941 		}
942 
943 		if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
944 			device_create_file(&new_client->dev,
945 					   &dev_attr_fan2_input);
946 			device_create_file(&new_client->dev,
947 					   &dev_attr_fan2_min);
948 			device_create_file(&new_client->dev,
949 					   &dev_attr_fan2_div);
950 			device_create_file(&new_client->dev,
951 					   &dev_attr_fan2_status);
952 		}
953 
954 		if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
955 			device_create_file(&new_client->dev, &dev_attr_pwm1);
956 		if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
957 			device_create_file(&new_client->dev, &dev_attr_pwm2);
958 	}
959 	if (data->fannr == 3) {
960 		if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
961 			device_create_file(&new_client->dev,
962 					   &dev_attr_fan3_input);
963 			device_create_file(&new_client->dev,
964 					   &dev_attr_fan3_min);
965 			device_create_file(&new_client->dev,
966 					   &dev_attr_fan3_div);
967 			device_create_file(&new_client->dev,
968 					   &dev_attr_fan3_status);
969 		}
970 
971 		if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
972 			device_create_file(&new_client->dev, &dev_attr_pwm3);
973 	}
974 
975 	return 0;
976 
977 ERROR2:
978 	for (i = 0; i < 3; i++) {
979 		if (data->address[i]) {
980 			release_region(data->address[i], PC87360_EXTENT);
981 		}
982 	}
983 ERROR1:
984 	kfree(data);
985 	return err;
986 }
987 
988 static int pc87360_detach_client(struct i2c_client *client)
989 {
990 	struct pc87360_data *data = i2c_get_clientdata(client);
991 	int i;
992 
993 	if ((i = i2c_detach_client(client))) {
994 		dev_err(&client->dev, "Client deregistration failed, "
995 			"client not detached.\n");
996 		return i;
997 	}
998 
999 	for (i = 0; i < 3; i++) {
1000 		if (data->address[i]) {
1001 			release_region(data->address[i], PC87360_EXTENT);
1002 		}
1003 	}
1004 	kfree(data);
1005 
1006 	return 0;
1007 }
1008 
1009 /* ldi is the logical device index
1010    bank is for voltages and temperatures only */
1011 static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
1012 			      u8 reg)
1013 {
1014 	int res;
1015 
1016 	down(&(data->lock));
1017 	if (bank != NO_BANK)
1018 		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1019 	res = inb_p(data->address[ldi] + reg);
1020 	up(&(data->lock));
1021 
1022 	return res;
1023 }
1024 
1025 static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
1026 				u8 reg, u8 value)
1027 {
1028 	down(&(data->lock));
1029 	if (bank != NO_BANK)
1030 		outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
1031 	outb_p(value, data->address[ldi] + reg);
1032 	up(&(data->lock));
1033 }
1034 
1035 static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
1036 {
1037 	struct pc87360_data *data = i2c_get_clientdata(client);
1038 	int i, nr;
1039 	const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
1040 	const u8 init_temp[3] = { 2, 2, 1 };
1041 	u8 reg;
1042 
1043 	if (init >= 2 && data->innr) {
1044 		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1045 					 PC87365_REG_IN_CONVRATE);
1046 		dev_info(&client->dev, "VLM conversion set to"
1047 			 "1s period, 160us delay\n");
1048 		pc87360_write_value(data, LD_IN, NO_BANK,
1049 				    PC87365_REG_IN_CONVRATE,
1050 				    (reg & 0xC0) | 0x11);
1051 	}
1052 
1053 	nr = data->innr < 11 ? data->innr : 11;
1054 	for (i=0; i<nr; i++) {
1055 		if (init >= init_in[i]) {
1056 			/* Forcibly enable voltage channel */
1057 			reg = pc87360_read_value(data, LD_IN, i,
1058 						 PC87365_REG_IN_STATUS);
1059 			if (!(reg & 0x01)) {
1060 				dev_dbg(&client->dev, "Forcibly "
1061 					"enabling in%d\n", i);
1062 				pc87360_write_value(data, LD_IN, i,
1063 						    PC87365_REG_IN_STATUS,
1064 						    (reg & 0x68) | 0x87);
1065 			}
1066 		}
1067 	}
1068 
1069 	/* We can't blindly trust the Super-I/O space configuration bit,
1070 	   most BIOS won't set it properly */
1071 	for (i=11; i<data->innr; i++) {
1072 		reg = pc87360_read_value(data, LD_IN, i,
1073 					 PC87365_REG_TEMP_STATUS);
1074 		use_thermistors = use_thermistors || (reg & 0x01);
1075 	}
1076 
1077 	i = use_thermistors ? 2 : 0;
1078 	for (; i<data->tempnr; i++) {
1079 		if (init >= init_temp[i]) {
1080 			/* Forcibly enable temperature channel */
1081 			reg = pc87360_read_value(data, LD_TEMP, i,
1082 						 PC87365_REG_TEMP_STATUS);
1083 			if (!(reg & 0x01)) {
1084 				dev_dbg(&client->dev, "Forcibly "
1085 					"enabling temp%d\n", i+1);
1086 				pc87360_write_value(data, LD_TEMP, i,
1087 						    PC87365_REG_TEMP_STATUS,
1088 						    0xCF);
1089 			}
1090 		}
1091 	}
1092 
1093 	if (use_thermistors) {
1094 		for (i=11; i<data->innr; i++) {
1095 			if (init >= init_in[i]) {
1096 				/* The pin may already be used by thermal
1097 				   diodes */
1098 				reg = pc87360_read_value(data, LD_TEMP,
1099 				      (i-11)/2, PC87365_REG_TEMP_STATUS);
1100 				if (reg & 0x01) {
1101 					dev_dbg(&client->dev, "Skipping "
1102 						"temp%d, pin already in use "
1103 						"by temp%d\n", i-7, (i-11)/2);
1104 					continue;
1105 				}
1106 
1107 				/* Forcibly enable thermistor channel */
1108 				reg = pc87360_read_value(data, LD_IN, i,
1109 							 PC87365_REG_IN_STATUS);
1110 				if (!(reg & 0x01)) {
1111 					dev_dbg(&client->dev, "Forcibly "
1112 						"enabling temp%d\n", i-7);
1113 					pc87360_write_value(data, LD_IN, i,
1114 						PC87365_REG_TEMP_STATUS,
1115 						(reg & 0x60) | 0x8F);
1116 				}
1117 			}
1118 		}
1119 	}
1120 
1121 	if (data->innr) {
1122 		reg = pc87360_read_value(data, LD_IN, NO_BANK,
1123 					 PC87365_REG_IN_CONFIG);
1124 		if (reg & 0x01) {
1125 			dev_dbg(&client->dev, "Forcibly "
1126 				"enabling monitoring (VLM)\n");
1127 			pc87360_write_value(data, LD_IN, NO_BANK,
1128 					    PC87365_REG_IN_CONFIG,
1129 					    reg & 0xFE);
1130 		}
1131 	}
1132 
1133 	if (data->tempnr) {
1134 		reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
1135 					 PC87365_REG_TEMP_CONFIG);
1136 		if (reg & 0x01) {
1137 			dev_dbg(&client->dev, "Forcibly enabling "
1138 				"monitoring (TMS)\n");
1139 			pc87360_write_value(data, LD_TEMP, NO_BANK,
1140 					    PC87365_REG_TEMP_CONFIG,
1141 					    reg & 0xFE);
1142 		}
1143 
1144 		if (init >= 2) {
1145 			/* Chip config as documented by National Semi. */
1146 			pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
1147 			/* We voluntarily omit the bank here, in case the
1148 			   sequence itself matters. It shouldn't be a problem,
1149 			   since nobody else is supposed to access the
1150 			   device at that point. */
1151 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
1152 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
1153 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
1154 			pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
1155 		}
1156 	}
1157 }
1158 
1159 static void pc87360_autodiv(struct i2c_client *client, int nr)
1160 {
1161 	struct pc87360_data *data = i2c_get_clientdata(client);
1162 	u8 old_min = data->fan_min[nr];
1163 
1164 	/* Increase clock divider if needed and possible */
1165 	if ((data->fan_status[nr] & 0x04) /* overflow flag */
1166 	 || (data->fan[nr] >= 224)) { /* next to overflow */
1167 		if ((data->fan_status[nr] & 0x60) != 0x60) {
1168 			data->fan_status[nr] += 0x20;
1169 			data->fan_min[nr] >>= 1;
1170 			data->fan[nr] >>= 1;
1171 			dev_dbg(&client->dev, "Increasing "
1172 				"clock divider to %d for fan %d\n",
1173 				FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
1174 		}
1175 	} else {
1176 		/* Decrease clock divider if possible */
1177 		while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
1178 		 && data->fan[nr] < 85 /* bad accuracy */
1179 		 && (data->fan_status[nr] & 0x60) != 0x00) {
1180 			data->fan_status[nr] -= 0x20;
1181 			data->fan_min[nr] <<= 1;
1182 			data->fan[nr] <<= 1;
1183 			dev_dbg(&client->dev, "Decreasing "
1184 				"clock divider to %d for fan %d\n",
1185 				FAN_DIV_FROM_REG(data->fan_status[nr]),
1186 				nr+1);
1187 		}
1188 	}
1189 
1190 	/* Write new fan min if it changed */
1191 	if (old_min != data->fan_min[nr]) {
1192 		pc87360_write_value(data, LD_FAN, NO_BANK,
1193 				    PC87360_REG_FAN_MIN(nr),
1194 				    data->fan_min[nr]);
1195 	}
1196 }
1197 
1198 static struct pc87360_data *pc87360_update_device(struct device *dev)
1199 {
1200 	struct i2c_client *client = to_i2c_client(dev);
1201 	struct pc87360_data *data = i2c_get_clientdata(client);
1202 	u8 i;
1203 
1204 	down(&data->update_lock);
1205 
1206 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
1207 		dev_dbg(&client->dev, "Data update\n");
1208 
1209 		/* Fans */
1210 		for (i = 0; i < data->fannr; i++) {
1211 			if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
1212 				data->fan_status[i] =
1213 					pc87360_read_value(data, LD_FAN,
1214 					NO_BANK, PC87360_REG_FAN_STATUS(i));
1215 				data->fan[i] = pc87360_read_value(data, LD_FAN,
1216 					       NO_BANK, PC87360_REG_FAN(i));
1217 				data->fan_min[i] = pc87360_read_value(data,
1218 						   LD_FAN, NO_BANK,
1219 						   PC87360_REG_FAN_MIN(i));
1220 				/* Change clock divider if needed */
1221 				pc87360_autodiv(client, i);
1222 				/* Clear bits and write new divider */
1223 				pc87360_write_value(data, LD_FAN, NO_BANK,
1224 						    PC87360_REG_FAN_STATUS(i),
1225 						    data->fan_status[i]);
1226 			}
1227 			if (FAN_CONFIG_CONTROL(data->fan_conf, i))
1228 				data->pwm[i] = pc87360_read_value(data, LD_FAN,
1229 					       NO_BANK, PC87360_REG_PWM(i));
1230 		}
1231 
1232 		/* Voltages */
1233 		for (i = 0; i < data->innr; i++) {
1234 			data->in_status[i] = pc87360_read_value(data, LD_IN, i,
1235 					     PC87365_REG_IN_STATUS);
1236 			/* Clear bits */
1237 			pc87360_write_value(data, LD_IN, i,
1238 					    PC87365_REG_IN_STATUS,
1239 					    data->in_status[i]);
1240 			if ((data->in_status[i] & 0x81) == 0x81) {
1241 				data->in[i] = pc87360_read_value(data, LD_IN,
1242 					      i, PC87365_REG_IN);
1243 			}
1244 			if (data->in_status[i] & 0x01) {
1245 				data->in_min[i] = pc87360_read_value(data,
1246 						  LD_IN, i,
1247 						  PC87365_REG_IN_MIN);
1248 				data->in_max[i] = pc87360_read_value(data,
1249 						  LD_IN, i,
1250 						  PC87365_REG_IN_MAX);
1251 				if (i >= 11)
1252 					data->in_crit[i-11] =
1253 						pc87360_read_value(data, LD_IN,
1254 						i, PC87365_REG_TEMP_CRIT);
1255 			}
1256 		}
1257 		if (data->innr) {
1258 			data->in_alarms = pc87360_read_value(data, LD_IN,
1259 					  NO_BANK, PC87365_REG_IN_ALARMS1)
1260 					| ((pc87360_read_value(data, LD_IN,
1261 					    NO_BANK, PC87365_REG_IN_ALARMS2)
1262 					    & 0x07) << 8);
1263 			data->vid = (data->vid_conf & 0xE0) ?
1264 				    pc87360_read_value(data, LD_IN,
1265 				    NO_BANK, PC87365_REG_VID) : 0x1F;
1266 		}
1267 
1268 		/* Temperatures */
1269 		for (i = 0; i < data->tempnr; i++) {
1270 			data->temp_status[i] = pc87360_read_value(data,
1271 					       LD_TEMP, i,
1272 					       PC87365_REG_TEMP_STATUS);
1273 			/* Clear bits */
1274 			pc87360_write_value(data, LD_TEMP, i,
1275 					    PC87365_REG_TEMP_STATUS,
1276 					    data->temp_status[i]);
1277 			if ((data->temp_status[i] & 0x81) == 0x81) {
1278 				data->temp[i] = pc87360_read_value(data,
1279 						LD_TEMP, i,
1280 						PC87365_REG_TEMP);
1281 			}
1282 			if (data->temp_status[i] & 0x01) {
1283 				data->temp_min[i] = pc87360_read_value(data,
1284 						    LD_TEMP, i,
1285 						    PC87365_REG_TEMP_MIN);
1286 				data->temp_max[i] = pc87360_read_value(data,
1287 						    LD_TEMP, i,
1288 						    PC87365_REG_TEMP_MAX);
1289 				data->temp_crit[i] = pc87360_read_value(data,
1290 						     LD_TEMP, i,
1291 						     PC87365_REG_TEMP_CRIT);
1292 			}
1293 		}
1294 		if (data->tempnr) {
1295 			data->temp_alarms = pc87360_read_value(data, LD_TEMP,
1296 					    NO_BANK, PC87365_REG_TEMP_ALARMS)
1297 					    & 0x3F;
1298 		}
1299 
1300 		data->last_updated = jiffies;
1301 		data->valid = 1;
1302 	}
1303 
1304 	up(&data->update_lock);
1305 
1306 	return data;
1307 }
1308 
1309 static int __init pc87360_init(void)
1310 {
1311 	int i;
1312 
1313 	if (pc87360_find(0x2e, &devid, extra_isa)
1314 	 && pc87360_find(0x4e, &devid, extra_isa)) {
1315 		printk(KERN_WARNING "pc87360: PC8736x not detected, "
1316 		       "module not inserted.\n");
1317 		return -ENODEV;
1318 	}
1319 
1320 	/* Arbitrarily pick one of the addresses */
1321 	for (i = 0; i < 3; i++) {
1322 		if (extra_isa[i] != 0x0000) {
1323 			normal_isa[0] = extra_isa[i];
1324 			break;
1325 		}
1326 	}
1327 
1328 	if (normal_isa[0] == 0x0000) {
1329 		printk(KERN_WARNING "pc87360: No active logical device, "
1330 		       "module not inserted.\n");
1331 		return -ENODEV;
1332 	}
1333 
1334 	return i2c_add_driver(&pc87360_driver);
1335 }
1336 
1337 static void __exit pc87360_exit(void)
1338 {
1339 	i2c_del_driver(&pc87360_driver);
1340 }
1341 
1342 
1343 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1344 MODULE_DESCRIPTION("PC8736x hardware monitor");
1345 MODULE_LICENSE("GPL");
1346 
1347 module_init(pc87360_init);
1348 module_exit(pc87360_exit);
1349