xref: /openbmc/linux/drivers/hwmon/sis5595.c (revision fde0950903ce8cc38a91dd095280decceda2ff82)
1 /*
2     sis5595.c - Part of lm_sensors, Linux kernel modules
3 		for hardware monitoring
4 
5     Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
6 			Ky�sti M�lkki <kmalkki@cc.hut.fi>, and
7 			Mark D. Studebaker <mdsxyz123@yahoo.com>
8     Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
9     the help of Jean Delvare <khali@linux-fr.org>
10 
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15 
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20 
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25 
26 /*
27    SiS southbridge has a LM78-like chip integrated on the same IC.
28    This driver is a customized copy of lm78.c
29 
30    Supports following revisions:
31 	Version		PCI ID		PCI Revision
32 	1		1039/0008	AF or less
33 	2		1039/0008	B0 or greater
34 
35    Note: these chips contain a 0008 device which is incompatible with the
36 	 5595. We recognize these by the presence of the listed
37 	 "blacklist" PCI ID and refuse to load.
38 
39    NOT SUPPORTED	PCI ID		BLACKLIST PCI ID
40 	 540		0008		0540
41 	 550		0008		0550
42 	5513		0008		5511
43 	5581		0008		5597
44 	5582		0008		5597
45 	5597		0008		5597
46 	5598		0008		5597/5598
47 	 630		0008		0630
48 	 645		0008		0645
49 	 730		0008		0730
50 	 735		0008		0735
51 */
52 
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/ioport.h>
56 #include <linux/pci.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-isa.h>
59 #include <linux/i2c-sensor.h>
60 #include <linux/hwmon.h>
61 #include <linux/err.h>
62 #include <linux/init.h>
63 #include <linux/jiffies.h>
64 #include <asm/io.h>
65 
66 
67 /* If force_addr is set to anything different from 0, we forcibly enable
68    the device at the given address. */
69 static u16 force_addr;
70 module_param(force_addr, ushort, 0);
71 MODULE_PARM_DESC(force_addr,
72 		 "Initialize the base address of the sensors");
73 
74 /* Addresses to scan.
75    Note that we can't determine the ISA address until we have initialized
76    our module */
77 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
78 static unsigned int normal_isa[] = { 0x0000, I2C_CLIENT_ISA_END };
79 
80 /* Insmod parameters */
81 SENSORS_INSMOD_1(sis5595);
82 
83 /* Many SIS5595 constants specified below */
84 
85 /* Length of ISA address segment */
86 #define SIS5595_EXTENT 8
87 /* PCI Config Registers */
88 #define SIS5595_REVISION_REG 0x08
89 #define SIS5595_BASE_REG 0x68
90 #define SIS5595_PIN_REG 0x7A
91 #define SIS5595_ENABLE_REG 0x7B
92 
93 /* Where are the ISA address/data registers relative to the base address */
94 #define SIS5595_ADDR_REG_OFFSET 5
95 #define SIS5595_DATA_REG_OFFSET 6
96 
97 /* The SIS5595 registers */
98 #define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
99 #define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
100 #define SIS5595_REG_IN(nr) (0x20 + (nr))
101 
102 #define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
103 #define SIS5595_REG_FAN(nr) (0x28 + (nr))
104 
105 /* On the first version of the chip, the temp registers are separate.
106    On the second version,
107    TEMP pin is shared with IN4, configured in PCI register 0x7A.
108    The registers are the same as well.
109    OVER and HYST are really MAX and MIN. */
110 
111 #define REV2MIN	0xb0
112 #define SIS5595_REG_TEMP 	(( data->revision) >= REV2MIN) ? \
113 					SIS5595_REG_IN(4) : 0x27
114 #define SIS5595_REG_TEMP_OVER	(( data->revision) >= REV2MIN) ? \
115 					SIS5595_REG_IN_MAX(4) : 0x39
116 #define SIS5595_REG_TEMP_HYST	(( data->revision) >= REV2MIN) ? \
117 					SIS5595_REG_IN_MIN(4) : 0x3a
118 
119 #define SIS5595_REG_CONFIG 0x40
120 #define SIS5595_REG_ALARM1 0x41
121 #define SIS5595_REG_ALARM2 0x42
122 #define SIS5595_REG_FANDIV 0x47
123 
124 /* Conversions. Limit checking is only done on the TO_REG
125    variants. */
126 
127 /* IN: mV, (0V to 4.08V)
128    REG: 16mV/bit */
129 static inline u8 IN_TO_REG(unsigned long val)
130 {
131 	unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
132 	return (nval + 8) / 16;
133 }
134 #define IN_FROM_REG(val) ((val) *  16)
135 
136 static inline u8 FAN_TO_REG(long rpm, int div)
137 {
138 	if (rpm <= 0)
139 		return 255;
140 	return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
141 }
142 
143 static inline int FAN_FROM_REG(u8 val, int div)
144 {
145 	return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
146 }
147 
148 /* TEMP: mC (-54.12C to +157.53C)
149    REG: 0.83C/bit + 52.12, two's complement  */
150 static inline int TEMP_FROM_REG(s8 val)
151 {
152 	return val * 830 + 52120;
153 }
154 static inline s8 TEMP_TO_REG(int val)
155 {
156 	int nval = SENSORS_LIMIT(val, -54120, 157530) ;
157 	return nval<0 ? (nval-5212-415)/830 : (nval-5212+415)/830;
158 }
159 
160 /* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
161    REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
162 static inline u8 DIV_TO_REG(int val)
163 {
164 	return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
165 }
166 #define DIV_FROM_REG(val) (1 << (val))
167 
168 /* For the SIS5595, we need to keep some data in memory. That
169    data is pointed to by sis5595_list[NR]->data. The structure itself is
170    dynamically allocated, at the time when the new sis5595 client is
171    allocated. */
172 struct sis5595_data {
173 	struct i2c_client client;
174 	struct class_device *class_dev;
175 	struct semaphore lock;
176 
177 	struct semaphore update_lock;
178 	char valid;		/* !=0 if following fields are valid */
179 	unsigned long last_updated;	/* In jiffies */
180 	char maxins;		/* == 3 if temp enabled, otherwise == 4 */
181 	u8 revision;		/* Reg. value */
182 
183 	u8 in[5];		/* Register value */
184 	u8 in_max[5];		/* Register value */
185 	u8 in_min[5];		/* Register value */
186 	u8 fan[2];		/* Register value */
187 	u8 fan_min[2];		/* Register value */
188 	s8 temp;		/* Register value */
189 	s8 temp_over;		/* Register value */
190 	s8 temp_hyst;		/* Register value */
191 	u8 fan_div[2];		/* Register encoding, shifted right */
192 	u16 alarms;		/* Register encoding, combined */
193 };
194 
195 static struct pci_dev *s_bridge;	/* pointer to the (only) sis5595 */
196 
197 static int sis5595_attach_adapter(struct i2c_adapter *adapter);
198 static int sis5595_detect(struct i2c_adapter *adapter, int address, int kind);
199 static int sis5595_detach_client(struct i2c_client *client);
200 
201 static int sis5595_read_value(struct i2c_client *client, u8 register);
202 static int sis5595_write_value(struct i2c_client *client, u8 register, u8 value);
203 static struct sis5595_data *sis5595_update_device(struct device *dev);
204 static void sis5595_init_client(struct i2c_client *client);
205 
206 static struct i2c_driver sis5595_driver = {
207 	.owner		= THIS_MODULE,
208 	.name		= "sis5595",
209 	.id		= I2C_DRIVERID_SIS5595,
210 	.flags		= I2C_DF_NOTIFY,
211 	.attach_adapter	= sis5595_attach_adapter,
212 	.detach_client	= sis5595_detach_client,
213 };
214 
215 /* 4 Voltages */
216 static ssize_t show_in(struct device *dev, char *buf, int nr)
217 {
218 	struct sis5595_data *data = sis5595_update_device(dev);
219 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
220 }
221 
222 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
223 {
224 	struct sis5595_data *data = sis5595_update_device(dev);
225 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
226 }
227 
228 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
229 {
230 	struct sis5595_data *data = sis5595_update_device(dev);
231 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
232 }
233 
234 static ssize_t set_in_min(struct device *dev, const char *buf,
235 	       size_t count, int nr)
236 {
237 	struct i2c_client *client = to_i2c_client(dev);
238 	struct sis5595_data *data = i2c_get_clientdata(client);
239 	unsigned long val = simple_strtoul(buf, NULL, 10);
240 
241 	down(&data->update_lock);
242 	data->in_min[nr] = IN_TO_REG(val);
243 	sis5595_write_value(client, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
244 	up(&data->update_lock);
245 	return count;
246 }
247 
248 static ssize_t set_in_max(struct device *dev, const char *buf,
249 	       size_t count, int nr)
250 {
251 	struct i2c_client *client = to_i2c_client(dev);
252 	struct sis5595_data *data = i2c_get_clientdata(client);
253 	unsigned long val = simple_strtoul(buf, NULL, 10);
254 
255 	down(&data->update_lock);
256 	data->in_max[nr] = IN_TO_REG(val);
257 	sis5595_write_value(client, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
258 	up(&data->update_lock);
259 	return count;
260 }
261 
262 #define show_in_offset(offset)					\
263 static ssize_t							\
264 	show_in##offset (struct device *dev, struct device_attribute *attr, char *buf)		\
265 {								\
266 	return show_in(dev, buf, offset);			\
267 }								\
268 static DEVICE_ATTR(in##offset##_input, S_IRUGO, 		\
269 		show_in##offset, NULL);				\
270 static ssize_t							\
271 	show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
272 {								\
273 	return show_in_min(dev, buf, offset);			\
274 }								\
275 static ssize_t							\
276 	show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)	\
277 {								\
278 	return show_in_max(dev, buf, offset);			\
279 }								\
280 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr,	\
281 		const char *buf, size_t count)			\
282 {								\
283 	return set_in_min(dev, buf, count, offset);		\
284 }								\
285 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr,	\
286 		const char *buf, size_t count)			\
287 {								\
288 	return set_in_max(dev, buf, count, offset);		\
289 }								\
290 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,		\
291 		show_in##offset##_min, set_in##offset##_min);	\
292 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,		\
293 		show_in##offset##_max, set_in##offset##_max);
294 
295 show_in_offset(0);
296 show_in_offset(1);
297 show_in_offset(2);
298 show_in_offset(3);
299 show_in_offset(4);
300 
301 /* Temperature */
302 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
303 {
304 	struct sis5595_data *data = sis5595_update_device(dev);
305 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
306 }
307 
308 static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
309 {
310 	struct sis5595_data *data = sis5595_update_device(dev);
311 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
312 }
313 
314 static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
315 {
316 	struct i2c_client *client = to_i2c_client(dev);
317 	struct sis5595_data *data = i2c_get_clientdata(client);
318 	long val = simple_strtol(buf, NULL, 10);
319 
320 	down(&data->update_lock);
321 	data->temp_over = TEMP_TO_REG(val);
322 	sis5595_write_value(client, SIS5595_REG_TEMP_OVER, data->temp_over);
323 	up(&data->update_lock);
324 	return count;
325 }
326 
327 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
328 {
329 	struct sis5595_data *data = sis5595_update_device(dev);
330 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
331 }
332 
333 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
334 {
335 	struct i2c_client *client = to_i2c_client(dev);
336 	struct sis5595_data *data = i2c_get_clientdata(client);
337 	long val = simple_strtol(buf, NULL, 10);
338 
339 	down(&data->update_lock);
340 	data->temp_hyst = TEMP_TO_REG(val);
341 	sis5595_write_value(client, SIS5595_REG_TEMP_HYST, data->temp_hyst);
342 	up(&data->update_lock);
343 	return count;
344 }
345 
346 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
347 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
348 		show_temp_over, set_temp_over);
349 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
350 		show_temp_hyst, set_temp_hyst);
351 
352 /* 2 Fans */
353 static ssize_t show_fan(struct device *dev, char *buf, int nr)
354 {
355 	struct sis5595_data *data = sis5595_update_device(dev);
356 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
357 		DIV_FROM_REG(data->fan_div[nr])) );
358 }
359 
360 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
361 {
362 	struct sis5595_data *data = sis5595_update_device(dev);
363 	return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
364 		DIV_FROM_REG(data->fan_div[nr])) );
365 }
366 
367 static ssize_t set_fan_min(struct device *dev, const char *buf,
368 		size_t count, int nr)
369 {
370 	struct i2c_client *client = to_i2c_client(dev);
371 	struct sis5595_data *data = i2c_get_clientdata(client);
372 	unsigned long val = simple_strtoul(buf, NULL, 10);
373 
374 	down(&data->update_lock);
375 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
376 	sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
377 	up(&data->update_lock);
378 	return count;
379 }
380 
381 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
382 {
383 	struct sis5595_data *data = sis5595_update_device(dev);
384 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
385 }
386 
387 /* Note: we save and restore the fan minimum here, because its value is
388    determined in part by the fan divisor.  This follows the principle of
389    least suprise; the user doesn't expect the fan minimum to change just
390    because the divisor changed. */
391 static ssize_t set_fan_div(struct device *dev, const char *buf,
392 	size_t count, int nr)
393 {
394 	struct i2c_client *client = to_i2c_client(dev);
395 	struct sis5595_data *data = i2c_get_clientdata(client);
396 	unsigned long min;
397 	unsigned long val = simple_strtoul(buf, NULL, 10);
398 	int reg;
399 
400 	down(&data->update_lock);
401 	min = FAN_FROM_REG(data->fan_min[nr],
402 			DIV_FROM_REG(data->fan_div[nr]));
403 	reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
404 
405 	switch (val) {
406 	case 1: data->fan_div[nr] = 0; break;
407 	case 2: data->fan_div[nr] = 1; break;
408 	case 4: data->fan_div[nr] = 2; break;
409 	case 8: data->fan_div[nr] = 3; break;
410 	default:
411 		dev_err(&client->dev, "fan_div value %ld not "
412 			"supported. Choose one of 1, 2, 4 or 8!\n", val);
413 		up(&data->update_lock);
414 		return -EINVAL;
415 	}
416 
417 	switch (nr) {
418 	case 0:
419 		reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
420 		break;
421 	case 1:
422 		reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
423 		break;
424 	}
425 	sis5595_write_value(client, SIS5595_REG_FANDIV, reg);
426 	data->fan_min[nr] =
427 		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
428 	sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
429 	up(&data->update_lock);
430 	return count;
431 }
432 
433 #define show_fan_offset(offset)						\
434 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
435 {									\
436 	return show_fan(dev, buf, offset - 1);			\
437 }									\
438 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
439 {									\
440 	return show_fan_min(dev, buf, offset - 1);			\
441 }									\
442 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf)	\
443 {									\
444 	return show_fan_div(dev, buf, offset - 1);			\
445 }									\
446 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr,		\
447 		const char *buf, size_t count)				\
448 {									\
449 	return set_fan_min(dev, buf, count, offset - 1);		\
450 }									\
451 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
452 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\
453 		show_fan_##offset##_min, set_fan_##offset##_min);
454 
455 show_fan_offset(1);
456 show_fan_offset(2);
457 
458 static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
459 		size_t count)
460 {
461 	return set_fan_div(dev, buf, count, 0) ;
462 }
463 
464 static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
465 		size_t count)
466 {
467 	return set_fan_div(dev, buf, count, 1) ;
468 }
469 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
470 		show_fan_1_div, set_fan_1_div);
471 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
472 		show_fan_2_div, set_fan_2_div);
473 
474 /* Alarms */
475 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
476 {
477 	struct sis5595_data *data = sis5595_update_device(dev);
478 	return sprintf(buf, "%d\n", data->alarms);
479 }
480 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
481 
482 /* This is called when the module is loaded */
483 static int sis5595_attach_adapter(struct i2c_adapter *adapter)
484 {
485 	if (!(adapter->class & I2C_CLASS_HWMON))
486 		return 0;
487 	return i2c_detect(adapter, &addr_data, sis5595_detect);
488 }
489 
490 int sis5595_detect(struct i2c_adapter *adapter, int address, int kind)
491 {
492 	int err = 0;
493 	int i;
494 	struct i2c_client *new_client;
495 	struct sis5595_data *data;
496 	char val;
497 	u16 a;
498 
499 	/* Make sure we are probing the ISA bus!!  */
500 	if (!i2c_is_isa_adapter(adapter))
501 		goto exit;
502 
503 	if (force_addr)
504 		address = force_addr & ~(SIS5595_EXTENT - 1);
505 	/* Reserve the ISA region */
506 	if (!request_region(address, SIS5595_EXTENT, sis5595_driver.name)) {
507 		err = -EBUSY;
508 		goto exit;
509 	}
510 	if (force_addr) {
511 		dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n", address);
512 		if (PCIBIOS_SUCCESSFUL !=
513 		    pci_write_config_word(s_bridge, SIS5595_BASE_REG, address))
514 			goto exit_release;
515 		if (PCIBIOS_SUCCESSFUL !=
516 		    pci_read_config_word(s_bridge, SIS5595_BASE_REG, &a))
517 			goto exit_release;
518 		if ((a & ~(SIS5595_EXTENT - 1)) != address)
519 			/* doesn't work for some chips? */
520 			goto exit_release;
521 	}
522 
523 	if (PCIBIOS_SUCCESSFUL !=
524 	    pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val)) {
525 		goto exit_release;
526 	}
527 	if ((val & 0x80) == 0) {
528 		if (PCIBIOS_SUCCESSFUL !=
529 		    pci_write_config_byte(s_bridge, SIS5595_ENABLE_REG,
530 					  val | 0x80))
531 			goto exit_release;
532 		if (PCIBIOS_SUCCESSFUL !=
533 		    pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val))
534 			goto exit_release;
535 		if ((val & 0x80) == 0)
536 			/* doesn't work for some chips! */
537 			goto exit_release;
538 	}
539 
540 	if (!(data = kmalloc(sizeof(struct sis5595_data), GFP_KERNEL))) {
541 		err = -ENOMEM;
542 		goto exit_release;
543 	}
544 	memset(data, 0, sizeof(struct sis5595_data));
545 
546 	new_client = &data->client;
547 	new_client->addr = address;
548 	init_MUTEX(&data->lock);
549 	i2c_set_clientdata(new_client, data);
550 	new_client->adapter = adapter;
551 	new_client->driver = &sis5595_driver;
552 	new_client->flags = 0;
553 
554 	/* Check revision and pin registers to determine whether 4 or 5 voltages */
555 	pci_read_config_byte(s_bridge, SIS5595_REVISION_REG, &(data->revision));
556 	/* 4 voltages, 1 temp */
557 	data->maxins = 3;
558 	if (data->revision >= REV2MIN) {
559 		pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
560 		if (!(val & 0x80))
561 			/* 5 voltages, no temps */
562 			data->maxins = 4;
563 	}
564 
565 	/* Fill in the remaining client fields and put it into the global list */
566 	strlcpy(new_client->name, "sis5595", I2C_NAME_SIZE);
567 
568 	data->valid = 0;
569 	init_MUTEX(&data->update_lock);
570 
571 	/* Tell the I2C layer a new client has arrived */
572 	if ((err = i2c_attach_client(new_client)))
573 		goto exit_free;
574 
575 	/* Initialize the SIS5595 chip */
576 	sis5595_init_client(new_client);
577 
578 	/* A few vars need to be filled upon startup */
579 	for (i = 0; i < 2; i++) {
580 		data->fan_min[i] = sis5595_read_value(new_client,
581 					SIS5595_REG_FAN_MIN(i));
582 	}
583 
584 	/* Register sysfs hooks */
585 	data->class_dev = hwmon_device_register(&new_client->dev);
586 	if (IS_ERR(data->class_dev)) {
587 		err = PTR_ERR(data->class_dev);
588 		goto exit_detach;
589 	}
590 
591 	device_create_file(&new_client->dev, &dev_attr_in0_input);
592 	device_create_file(&new_client->dev, &dev_attr_in0_min);
593 	device_create_file(&new_client->dev, &dev_attr_in0_max);
594 	device_create_file(&new_client->dev, &dev_attr_in1_input);
595 	device_create_file(&new_client->dev, &dev_attr_in1_min);
596 	device_create_file(&new_client->dev, &dev_attr_in1_max);
597 	device_create_file(&new_client->dev, &dev_attr_in2_input);
598 	device_create_file(&new_client->dev, &dev_attr_in2_min);
599 	device_create_file(&new_client->dev, &dev_attr_in2_max);
600 	device_create_file(&new_client->dev, &dev_attr_in3_input);
601 	device_create_file(&new_client->dev, &dev_attr_in3_min);
602 	device_create_file(&new_client->dev, &dev_attr_in3_max);
603 	if (data->maxins == 4) {
604 		device_create_file(&new_client->dev, &dev_attr_in4_input);
605 		device_create_file(&new_client->dev, &dev_attr_in4_min);
606 		device_create_file(&new_client->dev, &dev_attr_in4_max);
607 	}
608 	device_create_file(&new_client->dev, &dev_attr_fan1_input);
609 	device_create_file(&new_client->dev, &dev_attr_fan1_min);
610 	device_create_file(&new_client->dev, &dev_attr_fan1_div);
611 	device_create_file(&new_client->dev, &dev_attr_fan2_input);
612 	device_create_file(&new_client->dev, &dev_attr_fan2_min);
613 	device_create_file(&new_client->dev, &dev_attr_fan2_div);
614 	device_create_file(&new_client->dev, &dev_attr_alarms);
615 	if (data->maxins == 3) {
616 		device_create_file(&new_client->dev, &dev_attr_temp1_input);
617 		device_create_file(&new_client->dev, &dev_attr_temp1_max);
618 		device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
619 	}
620 	return 0;
621 
622 exit_detach:
623 	i2c_detach_client(new_client);
624 exit_free:
625 	kfree(data);
626 exit_release:
627 	release_region(address, SIS5595_EXTENT);
628 exit:
629 	return err;
630 }
631 
632 static int sis5595_detach_client(struct i2c_client *client)
633 {
634 	struct sis5595_data *data = i2c_get_clientdata(client);
635 	int err;
636 
637 	hwmon_device_unregister(data->class_dev);
638 
639 	if ((err = i2c_detach_client(client))) {
640 		dev_err(&client->dev,
641 		    "Client deregistration failed, client not detached.\n");
642 		return err;
643 	}
644 
645 	if (i2c_is_isa_client(client))
646 		release_region(client->addr, SIS5595_EXTENT);
647 
648 	kfree(data);
649 
650 	return 0;
651 }
652 
653 
654 /* ISA access must be locked explicitly. */
655 static int sis5595_read_value(struct i2c_client *client, u8 reg)
656 {
657 	int res;
658 
659 	struct sis5595_data *data = i2c_get_clientdata(client);
660 	down(&data->lock);
661 	outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
662 	res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
663 	up(&data->lock);
664 	return res;
665 }
666 
667 static int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
668 {
669 	struct sis5595_data *data = i2c_get_clientdata(client);
670 	down(&data->lock);
671 	outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
672 	outb_p(value, client->addr + SIS5595_DATA_REG_OFFSET);
673 	up(&data->lock);
674 	return 0;
675 }
676 
677 /* Called when we have found a new SIS5595. */
678 static void sis5595_init_client(struct i2c_client *client)
679 {
680 	u8 config = sis5595_read_value(client, SIS5595_REG_CONFIG);
681 	if (!(config & 0x01))
682 		sis5595_write_value(client, SIS5595_REG_CONFIG,
683 				(config & 0xf7) | 0x01);
684 }
685 
686 static struct sis5595_data *sis5595_update_device(struct device *dev)
687 {
688 	struct i2c_client *client = to_i2c_client(dev);
689 	struct sis5595_data *data = i2c_get_clientdata(client);
690 	int i;
691 
692 	down(&data->update_lock);
693 
694 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
695 	    || !data->valid) {
696 
697 		for (i = 0; i <= data->maxins; i++) {
698 			data->in[i] =
699 			    sis5595_read_value(client, SIS5595_REG_IN(i));
700 			data->in_min[i] =
701 			    sis5595_read_value(client,
702 					       SIS5595_REG_IN_MIN(i));
703 			data->in_max[i] =
704 			    sis5595_read_value(client,
705 					       SIS5595_REG_IN_MAX(i));
706 		}
707 		for (i = 0; i < 2; i++) {
708 			data->fan[i] =
709 			    sis5595_read_value(client, SIS5595_REG_FAN(i));
710 			data->fan_min[i] =
711 			    sis5595_read_value(client,
712 					       SIS5595_REG_FAN_MIN(i));
713 		}
714 		if (data->maxins == 3) {
715 			data->temp =
716 			    sis5595_read_value(client, SIS5595_REG_TEMP);
717 			data->temp_over =
718 			    sis5595_read_value(client, SIS5595_REG_TEMP_OVER);
719 			data->temp_hyst =
720 			    sis5595_read_value(client, SIS5595_REG_TEMP_HYST);
721 		}
722 		i = sis5595_read_value(client, SIS5595_REG_FANDIV);
723 		data->fan_div[0] = (i >> 4) & 0x03;
724 		data->fan_div[1] = i >> 6;
725 		data->alarms =
726 		    sis5595_read_value(client, SIS5595_REG_ALARM1) |
727 		    (sis5595_read_value(client, SIS5595_REG_ALARM2) << 8);
728 		data->last_updated = jiffies;
729 		data->valid = 1;
730 	}
731 
732 	up(&data->update_lock);
733 
734 	return data;
735 }
736 
737 static struct pci_device_id sis5595_pci_ids[] = {
738 	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
739 	{ 0, }
740 };
741 
742 MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
743 
744 static int blacklist[] __devinitdata = {
745 	PCI_DEVICE_ID_SI_540,
746 	PCI_DEVICE_ID_SI_550,
747 	PCI_DEVICE_ID_SI_630,
748 	PCI_DEVICE_ID_SI_645,
749 	PCI_DEVICE_ID_SI_730,
750 	PCI_DEVICE_ID_SI_735,
751 	PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but
752 				  that ID shows up in other chips so we
753 				  use the 5511 ID for recognition */
754 	PCI_DEVICE_ID_SI_5597,
755 	PCI_DEVICE_ID_SI_5598,
756 	0 };
757 
758 static int __devinit sis5595_pci_probe(struct pci_dev *dev,
759 				       const struct pci_device_id *id)
760 {
761 	u16 val;
762 	int *i;
763 	int addr = 0;
764 
765 	for (i = blacklist; *i != 0; i++) {
766 		struct pci_dev *dev;
767 		dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
768 		if (dev) {
769 			dev_err(&dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
770 			pci_dev_put(dev);
771 			return -ENODEV;
772 		}
773 	}
774 
775 	if (PCIBIOS_SUCCESSFUL !=
776 	    pci_read_config_word(dev, SIS5595_BASE_REG, &val))
777 		return -ENODEV;
778 
779 	addr = val & ~(SIS5595_EXTENT - 1);
780 	if (addr == 0 && force_addr == 0) {
781 		dev_err(&dev->dev, "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
782 		return -ENODEV;
783 	}
784 	if (force_addr)
785 		addr = force_addr;	/* so detect will get called */
786 
787 	if (!addr) {
788 		dev_err(&dev->dev,"No SiS 5595 sensors found.\n");
789 		return -ENODEV;
790 	}
791 	normal_isa[0] = addr;
792 
793 	s_bridge = pci_dev_get(dev);
794 	if (i2c_isa_add_driver(&sis5595_driver)) {
795 		pci_dev_put(s_bridge);
796 		s_bridge = NULL;
797 	}
798 
799 	/* Always return failure here.  This is to allow other drivers to bind
800 	 * to this pci device.  We don't really want to have control over the
801 	 * pci device, we only wanted to read as few register values from it.
802 	 */
803 	return -ENODEV;
804 }
805 
806 static struct pci_driver sis5595_pci_driver = {
807 	.name            = "sis5595",
808 	.id_table        = sis5595_pci_ids,
809 	.probe           = sis5595_pci_probe,
810 };
811 
812 static int __init sm_sis5595_init(void)
813 {
814 	return pci_register_driver(&sis5595_pci_driver);
815 }
816 
817 static void __exit sm_sis5595_exit(void)
818 {
819 	pci_unregister_driver(&sis5595_pci_driver);
820 	if (s_bridge != NULL) {
821 		i2c_isa_del_driver(&sis5595_driver);
822 		pci_dev_put(s_bridge);
823 		s_bridge = NULL;
824 	}
825 }
826 
827 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
828 MODULE_DESCRIPTION("SiS 5595 Sensor device");
829 MODULE_LICENSE("GPL");
830 
831 module_init(sm_sis5595_init);
832 module_exit(sm_sis5595_exit);
833