xref: /openbmc/linux/drivers/hwmon/sis5595.c (revision 151f4e2b)
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 <jdelvare@suse.de>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/ioport.h>
58 #include <linux/pci.h>
59 #include <linux/platform_device.h>
60 #include <linux/hwmon.h>
61 #include <linux/hwmon-sysfs.h>
62 #include <linux/err.h>
63 #include <linux/init.h>
64 #include <linux/jiffies.h>
65 #include <linux/mutex.h>
66 #include <linux/sysfs.h>
67 #include <linux/acpi.h>
68 #include <linux/io.h>
69 
70 /*
71  * If force_addr is set to anything different from 0, we forcibly enable
72  * the device at the given address.
73  */
74 static u16 force_addr;
75 module_param(force_addr, ushort, 0);
76 MODULE_PARM_DESC(force_addr,
77 		 "Initialize the base address of the sensors");
78 
79 static struct platform_device *pdev;
80 
81 /* Many SIS5595 constants specified below */
82 
83 /* Length of ISA address segment */
84 #define SIS5595_EXTENT 8
85 /* PCI Config Registers */
86 #define SIS5595_BASE_REG 0x68
87 #define SIS5595_PIN_REG 0x7A
88 #define SIS5595_ENABLE_REG 0x7B
89 
90 /* Where are the ISA address/data registers relative to the base address */
91 #define SIS5595_ADDR_REG_OFFSET 5
92 #define SIS5595_DATA_REG_OFFSET 6
93 
94 /* The SIS5595 registers */
95 #define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
96 #define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
97 #define SIS5595_REG_IN(nr) (0x20 + (nr))
98 
99 #define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
100 #define SIS5595_REG_FAN(nr) (0x28 + (nr))
101 
102 /*
103  * On the first version of the chip, the temp registers are separate.
104  * On the second version,
105  * TEMP pin is shared with IN4, configured in PCI register 0x7A.
106  * The registers are the same as well.
107  * OVER and HYST are really MAX and MIN.
108  */
109 
110 #define REV2MIN	0xb0
111 #define SIS5595_REG_TEMP	(((data->revision) >= REV2MIN) ? \
112 					SIS5595_REG_IN(4) : 0x27)
113 #define SIS5595_REG_TEMP_OVER	(((data->revision) >= REV2MIN) ? \
114 					SIS5595_REG_IN_MAX(4) : 0x39)
115 #define SIS5595_REG_TEMP_HYST	(((data->revision) >= REV2MIN) ? \
116 					SIS5595_REG_IN_MIN(4) : 0x3a)
117 
118 #define SIS5595_REG_CONFIG 0x40
119 #define SIS5595_REG_ALARM1 0x41
120 #define SIS5595_REG_ALARM2 0x42
121 #define SIS5595_REG_FANDIV 0x47
122 
123 /*
124  * Conversions. Limit checking is only done on the TO_REG
125  * variants.
126  */
127 
128 /*
129  * IN: mV, (0V to 4.08V)
130  * REG: 16mV/bit
131  */
132 static inline u8 IN_TO_REG(unsigned long val)
133 {
134 	unsigned long nval = clamp_val(val, 0, 4080);
135 	return (nval + 8) / 16;
136 }
137 #define IN_FROM_REG(val) ((val) *  16)
138 
139 static inline u8 FAN_TO_REG(long rpm, int div)
140 {
141 	if (rpm <= 0)
142 		return 255;
143 	if (rpm > 1350000)
144 		return 1;
145 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
146 }
147 
148 static inline int FAN_FROM_REG(u8 val, int div)
149 {
150 	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
151 }
152 
153 /*
154  * TEMP: mC (-54.12C to +157.53C)
155  * REG: 0.83C/bit + 52.12, two's complement
156  */
157 static inline int TEMP_FROM_REG(s8 val)
158 {
159 	return val * 830 + 52120;
160 }
161 static inline s8 TEMP_TO_REG(long val)
162 {
163 	int nval = clamp_val(val, -54120, 157530) ;
164 	return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
165 }
166 
167 /*
168  * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
169  * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
170  */
171 static inline u8 DIV_TO_REG(int val)
172 {
173 	return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
174 }
175 #define DIV_FROM_REG(val) (1 << (val))
176 
177 /*
178  * For each registered chip, we need to keep some data in memory.
179  * The structure is dynamically allocated.
180  */
181 struct sis5595_data {
182 	unsigned short addr;
183 	const char *name;
184 	struct device *hwmon_dev;
185 	struct mutex lock;
186 
187 	struct mutex update_lock;
188 	char valid;		/* !=0 if following fields are valid */
189 	unsigned long last_updated;	/* In jiffies */
190 	char maxins;		/* == 3 if temp enabled, otherwise == 4 */
191 	u8 revision;		/* Reg. value */
192 
193 	u8 in[5];		/* Register value */
194 	u8 in_max[5];		/* Register value */
195 	u8 in_min[5];		/* Register value */
196 	u8 fan[2];		/* Register value */
197 	u8 fan_min[2];		/* Register value */
198 	s8 temp;		/* Register value */
199 	s8 temp_over;		/* Register value */
200 	s8 temp_hyst;		/* Register value */
201 	u8 fan_div[2];		/* Register encoding, shifted right */
202 	u16 alarms;		/* Register encoding, combined */
203 };
204 
205 static struct pci_dev *s_bridge;	/* pointer to the (only) sis5595 */
206 
207 static int sis5595_probe(struct platform_device *pdev);
208 static int sis5595_remove(struct platform_device *pdev);
209 
210 static int sis5595_read_value(struct sis5595_data *data, u8 reg);
211 static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
212 static struct sis5595_data *sis5595_update_device(struct device *dev);
213 static void sis5595_init_device(struct sis5595_data *data);
214 
215 static struct platform_driver sis5595_driver = {
216 	.driver = {
217 		.name	= "sis5595",
218 	},
219 	.probe		= sis5595_probe,
220 	.remove		= sis5595_remove,
221 };
222 
223 /* 4 Voltages */
224 static ssize_t in_show(struct device *dev, struct device_attribute *da,
225 		       char *buf)
226 {
227 	struct sis5595_data *data = sis5595_update_device(dev);
228 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
229 	int nr = attr->index;
230 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
231 }
232 
233 static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
234 			   char *buf)
235 {
236 	struct sis5595_data *data = sis5595_update_device(dev);
237 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
238 	int nr = attr->index;
239 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
240 }
241 
242 static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
243 			   char *buf)
244 {
245 	struct sis5595_data *data = sis5595_update_device(dev);
246 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
247 	int nr = attr->index;
248 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
249 }
250 
251 static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
252 			    const char *buf, size_t count)
253 {
254 	struct sis5595_data *data = dev_get_drvdata(dev);
255 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
256 	int nr = attr->index;
257 	unsigned long val;
258 	int err;
259 
260 	err = kstrtoul(buf, 10, &val);
261 	if (err)
262 		return err;
263 
264 	mutex_lock(&data->update_lock);
265 	data->in_min[nr] = IN_TO_REG(val);
266 	sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
267 	mutex_unlock(&data->update_lock);
268 	return count;
269 }
270 
271 static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
272 			    const char *buf, size_t count)
273 {
274 	struct sis5595_data *data = dev_get_drvdata(dev);
275 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
276 	int nr = attr->index;
277 	unsigned long val;
278 	int err;
279 
280 	err = kstrtoul(buf, 10, &val);
281 	if (err)
282 		return err;
283 
284 	mutex_lock(&data->update_lock);
285 	data->in_max[nr] = IN_TO_REG(val);
286 	sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
287 	mutex_unlock(&data->update_lock);
288 	return count;
289 }
290 
291 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
292 static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
293 static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
294 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
295 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
296 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
297 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
298 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
299 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
300 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
301 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
302 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
303 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
304 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
305 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
306 
307 /* Temperature */
308 static ssize_t temp1_input_show(struct device *dev,
309 				struct device_attribute *attr, char *buf)
310 {
311 	struct sis5595_data *data = sis5595_update_device(dev);
312 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
313 }
314 
315 static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
316 			      char *buf)
317 {
318 	struct sis5595_data *data = sis5595_update_device(dev);
319 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
320 }
321 
322 static ssize_t temp1_max_store(struct device *dev,
323 			       struct device_attribute *attr, const char *buf,
324 			       size_t count)
325 {
326 	struct sis5595_data *data = dev_get_drvdata(dev);
327 	long val;
328 	int err;
329 
330 	err = kstrtol(buf, 10, &val);
331 	if (err)
332 		return err;
333 
334 	mutex_lock(&data->update_lock);
335 	data->temp_over = TEMP_TO_REG(val);
336 	sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
337 	mutex_unlock(&data->update_lock);
338 	return count;
339 }
340 
341 static ssize_t temp1_max_hyst_show(struct device *dev,
342 				   struct device_attribute *attr, char *buf)
343 {
344 	struct sis5595_data *data = sis5595_update_device(dev);
345 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
346 }
347 
348 static ssize_t temp1_max_hyst_store(struct device *dev,
349 				    struct device_attribute *attr,
350 				    const char *buf, size_t count)
351 {
352 	struct sis5595_data *data = dev_get_drvdata(dev);
353 	long val;
354 	int err;
355 
356 	err = kstrtol(buf, 10, &val);
357 	if (err)
358 		return err;
359 
360 	mutex_lock(&data->update_lock);
361 	data->temp_hyst = TEMP_TO_REG(val);
362 	sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
363 	mutex_unlock(&data->update_lock);
364 	return count;
365 }
366 
367 static DEVICE_ATTR_RO(temp1_input);
368 static DEVICE_ATTR_RW(temp1_max);
369 static DEVICE_ATTR_RW(temp1_max_hyst);
370 
371 /* 2 Fans */
372 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
373 			char *buf)
374 {
375 	struct sis5595_data *data = sis5595_update_device(dev);
376 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
377 	int nr = attr->index;
378 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
379 		DIV_FROM_REG(data->fan_div[nr])));
380 }
381 
382 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
383 			    char *buf)
384 {
385 	struct sis5595_data *data = sis5595_update_device(dev);
386 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
387 	int nr = attr->index;
388 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
389 		DIV_FROM_REG(data->fan_div[nr])));
390 }
391 
392 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
393 			     const char *buf, size_t count)
394 {
395 	struct sis5595_data *data = dev_get_drvdata(dev);
396 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
397 	int nr = attr->index;
398 	unsigned long val;
399 	int err;
400 
401 	err = kstrtoul(buf, 10, &val);
402 	if (err)
403 		return err;
404 
405 	mutex_lock(&data->update_lock);
406 	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
407 	sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
408 	mutex_unlock(&data->update_lock);
409 	return count;
410 }
411 
412 static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
413 			    char *buf)
414 {
415 	struct sis5595_data *data = sis5595_update_device(dev);
416 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
417 	int nr = attr->index;
418 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
419 }
420 
421 /*
422  * Note: we save and restore the fan minimum here, because its value is
423  * determined in part by the fan divisor.  This follows the principle of
424  * least surprise; the user doesn't expect the fan minimum to change just
425  * because the divisor changed.
426  */
427 static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
428 			     const char *buf, size_t count)
429 {
430 	struct sis5595_data *data = dev_get_drvdata(dev);
431 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
432 	int nr = attr->index;
433 	unsigned long min;
434 	int reg;
435 	unsigned long val;
436 	int err;
437 
438 	err = kstrtoul(buf, 10, &val);
439 	if (err)
440 		return err;
441 
442 	mutex_lock(&data->update_lock);
443 	min = FAN_FROM_REG(data->fan_min[nr],
444 			DIV_FROM_REG(data->fan_div[nr]));
445 	reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
446 
447 	switch (val) {
448 	case 1:
449 		data->fan_div[nr] = 0;
450 		break;
451 	case 2:
452 		data->fan_div[nr] = 1;
453 		break;
454 	case 4:
455 		data->fan_div[nr] = 2;
456 		break;
457 	case 8:
458 		data->fan_div[nr] = 3;
459 		break;
460 	default:
461 		dev_err(dev,
462 			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
463 			val);
464 		mutex_unlock(&data->update_lock);
465 		return -EINVAL;
466 	}
467 
468 	switch (nr) {
469 	case 0:
470 		reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
471 		break;
472 	case 1:
473 		reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
474 		break;
475 	}
476 	sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
477 	data->fan_min[nr] =
478 		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
479 	sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
480 	mutex_unlock(&data->update_lock);
481 	return count;
482 }
483 
484 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
485 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
486 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
487 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
488 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
489 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
490 
491 /* Alarms */
492 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
493 			   char *buf)
494 {
495 	struct sis5595_data *data = sis5595_update_device(dev);
496 	return sprintf(buf, "%d\n", data->alarms);
497 }
498 static DEVICE_ATTR_RO(alarms);
499 
500 static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
501 			  char *buf)
502 {
503 	struct sis5595_data *data = sis5595_update_device(dev);
504 	int nr = to_sensor_dev_attr(da)->index;
505 	return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
506 }
507 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
508 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
509 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
510 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
511 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 15);
512 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
513 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
514 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 15);
515 
516 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
517 			 char *buf)
518 {
519 	struct sis5595_data *data = dev_get_drvdata(dev);
520 	return sprintf(buf, "%s\n", data->name);
521 }
522 static DEVICE_ATTR_RO(name);
523 
524 static struct attribute *sis5595_attributes[] = {
525 	&sensor_dev_attr_in0_input.dev_attr.attr,
526 	&sensor_dev_attr_in0_min.dev_attr.attr,
527 	&sensor_dev_attr_in0_max.dev_attr.attr,
528 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
529 	&sensor_dev_attr_in1_input.dev_attr.attr,
530 	&sensor_dev_attr_in1_min.dev_attr.attr,
531 	&sensor_dev_attr_in1_max.dev_attr.attr,
532 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
533 	&sensor_dev_attr_in2_input.dev_attr.attr,
534 	&sensor_dev_attr_in2_min.dev_attr.attr,
535 	&sensor_dev_attr_in2_max.dev_attr.attr,
536 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
537 	&sensor_dev_attr_in3_input.dev_attr.attr,
538 	&sensor_dev_attr_in3_min.dev_attr.attr,
539 	&sensor_dev_attr_in3_max.dev_attr.attr,
540 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
541 
542 	&sensor_dev_attr_fan1_input.dev_attr.attr,
543 	&sensor_dev_attr_fan1_min.dev_attr.attr,
544 	&sensor_dev_attr_fan1_div.dev_attr.attr,
545 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
546 	&sensor_dev_attr_fan2_input.dev_attr.attr,
547 	&sensor_dev_attr_fan2_min.dev_attr.attr,
548 	&sensor_dev_attr_fan2_div.dev_attr.attr,
549 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
550 
551 	&dev_attr_alarms.attr,
552 	&dev_attr_name.attr,
553 	NULL
554 };
555 
556 static const struct attribute_group sis5595_group = {
557 	.attrs = sis5595_attributes,
558 };
559 
560 static struct attribute *sis5595_attributes_in4[] = {
561 	&sensor_dev_attr_in4_input.dev_attr.attr,
562 	&sensor_dev_attr_in4_min.dev_attr.attr,
563 	&sensor_dev_attr_in4_max.dev_attr.attr,
564 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
565 	NULL
566 };
567 
568 static const struct attribute_group sis5595_group_in4 = {
569 	.attrs = sis5595_attributes_in4,
570 };
571 
572 static struct attribute *sis5595_attributes_temp1[] = {
573 	&dev_attr_temp1_input.attr,
574 	&dev_attr_temp1_max.attr,
575 	&dev_attr_temp1_max_hyst.attr,
576 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
577 	NULL
578 };
579 
580 static const struct attribute_group sis5595_group_temp1 = {
581 	.attrs = sis5595_attributes_temp1,
582 };
583 
584 /* This is called when the module is loaded */
585 static int sis5595_probe(struct platform_device *pdev)
586 {
587 	int err = 0;
588 	int i;
589 	struct sis5595_data *data;
590 	struct resource *res;
591 	char val;
592 
593 	/* Reserve the ISA region */
594 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
595 	if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
596 				 sis5595_driver.driver.name))
597 		return -EBUSY;
598 
599 	data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
600 			    GFP_KERNEL);
601 	if (!data)
602 		return -ENOMEM;
603 
604 	mutex_init(&data->lock);
605 	mutex_init(&data->update_lock);
606 	data->addr = res->start;
607 	data->name = "sis5595";
608 	platform_set_drvdata(pdev, data);
609 
610 	/*
611 	 * Check revision and pin registers to determine whether 4 or 5 voltages
612 	 */
613 	data->revision = s_bridge->revision;
614 	/* 4 voltages, 1 temp */
615 	data->maxins = 3;
616 	if (data->revision >= REV2MIN) {
617 		pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
618 		if (!(val & 0x80))
619 			/* 5 voltages, no temps */
620 			data->maxins = 4;
621 	}
622 
623 	/* Initialize the SIS5595 chip */
624 	sis5595_init_device(data);
625 
626 	/* A few vars need to be filled upon startup */
627 	for (i = 0; i < 2; i++) {
628 		data->fan_min[i] = sis5595_read_value(data,
629 					SIS5595_REG_FAN_MIN(i));
630 	}
631 
632 	/* Register sysfs hooks */
633 	err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
634 	if (err)
635 		return err;
636 	if (data->maxins == 4) {
637 		err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
638 		if (err)
639 			goto exit_remove_files;
640 	} else {
641 		err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
642 		if (err)
643 			goto exit_remove_files;
644 	}
645 
646 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
647 	if (IS_ERR(data->hwmon_dev)) {
648 		err = PTR_ERR(data->hwmon_dev);
649 		goto exit_remove_files;
650 	}
651 
652 	return 0;
653 
654 exit_remove_files:
655 	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
656 	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
657 	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
658 	return err;
659 }
660 
661 static int sis5595_remove(struct platform_device *pdev)
662 {
663 	struct sis5595_data *data = platform_get_drvdata(pdev);
664 
665 	hwmon_device_unregister(data->hwmon_dev);
666 	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
667 	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
668 	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
669 
670 	return 0;
671 }
672 
673 /* ISA access must be locked explicitly. */
674 static int sis5595_read_value(struct sis5595_data *data, u8 reg)
675 {
676 	int res;
677 
678 	mutex_lock(&data->lock);
679 	outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
680 	res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
681 	mutex_unlock(&data->lock);
682 	return res;
683 }
684 
685 static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
686 {
687 	mutex_lock(&data->lock);
688 	outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
689 	outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
690 	mutex_unlock(&data->lock);
691 }
692 
693 /* Called when we have found a new SIS5595. */
694 static void sis5595_init_device(struct sis5595_data *data)
695 {
696 	u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
697 	if (!(config & 0x01))
698 		sis5595_write_value(data, SIS5595_REG_CONFIG,
699 				(config & 0xf7) | 0x01);
700 }
701 
702 static struct sis5595_data *sis5595_update_device(struct device *dev)
703 {
704 	struct sis5595_data *data = dev_get_drvdata(dev);
705 	int i;
706 
707 	mutex_lock(&data->update_lock);
708 
709 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
710 	    || !data->valid) {
711 
712 		for (i = 0; i <= data->maxins; i++) {
713 			data->in[i] =
714 			    sis5595_read_value(data, SIS5595_REG_IN(i));
715 			data->in_min[i] =
716 			    sis5595_read_value(data,
717 					       SIS5595_REG_IN_MIN(i));
718 			data->in_max[i] =
719 			    sis5595_read_value(data,
720 					       SIS5595_REG_IN_MAX(i));
721 		}
722 		for (i = 0; i < 2; i++) {
723 			data->fan[i] =
724 			    sis5595_read_value(data, SIS5595_REG_FAN(i));
725 			data->fan_min[i] =
726 			    sis5595_read_value(data,
727 					       SIS5595_REG_FAN_MIN(i));
728 		}
729 		if (data->maxins == 3) {
730 			data->temp =
731 			    sis5595_read_value(data, SIS5595_REG_TEMP);
732 			data->temp_over =
733 			    sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
734 			data->temp_hyst =
735 			    sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
736 		}
737 		i = sis5595_read_value(data, SIS5595_REG_FANDIV);
738 		data->fan_div[0] = (i >> 4) & 0x03;
739 		data->fan_div[1] = i >> 6;
740 		data->alarms =
741 		    sis5595_read_value(data, SIS5595_REG_ALARM1) |
742 		    (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
743 		data->last_updated = jiffies;
744 		data->valid = 1;
745 	}
746 
747 	mutex_unlock(&data->update_lock);
748 
749 	return data;
750 }
751 
752 static const struct pci_device_id sis5595_pci_ids[] = {
753 	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
754 	{ 0, }
755 };
756 
757 MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
758 
759 static int blacklist[] = {
760 	PCI_DEVICE_ID_SI_540,
761 	PCI_DEVICE_ID_SI_550,
762 	PCI_DEVICE_ID_SI_630,
763 	PCI_DEVICE_ID_SI_645,
764 	PCI_DEVICE_ID_SI_730,
765 	PCI_DEVICE_ID_SI_735,
766 	PCI_DEVICE_ID_SI_5511, /*
767 				* 5513 chip has the 0008 device but
768 				* that ID shows up in other chips so we
769 				* use the 5511 ID for recognition
770 				*/
771 	PCI_DEVICE_ID_SI_5597,
772 	PCI_DEVICE_ID_SI_5598,
773 	0 };
774 
775 static int sis5595_device_add(unsigned short address)
776 {
777 	struct resource res = {
778 		.start	= address,
779 		.end	= address + SIS5595_EXTENT - 1,
780 		.name	= "sis5595",
781 		.flags	= IORESOURCE_IO,
782 	};
783 	int err;
784 
785 	err = acpi_check_resource_conflict(&res);
786 	if (err)
787 		goto exit;
788 
789 	pdev = platform_device_alloc("sis5595", address);
790 	if (!pdev) {
791 		err = -ENOMEM;
792 		pr_err("Device allocation failed\n");
793 		goto exit;
794 	}
795 
796 	err = platform_device_add_resources(pdev, &res, 1);
797 	if (err) {
798 		pr_err("Device resource addition failed (%d)\n", err);
799 		goto exit_device_put;
800 	}
801 
802 	err = platform_device_add(pdev);
803 	if (err) {
804 		pr_err("Device addition failed (%d)\n", err);
805 		goto exit_device_put;
806 	}
807 
808 	return 0;
809 
810 exit_device_put:
811 	platform_device_put(pdev);
812 exit:
813 	return err;
814 }
815 
816 static int sis5595_pci_probe(struct pci_dev *dev,
817 				       const struct pci_device_id *id)
818 {
819 	u16 address;
820 	u8 enable;
821 	int *i;
822 
823 	for (i = blacklist; *i != 0; i++) {
824 		struct pci_dev *d;
825 		d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
826 		if (d) {
827 			dev_err(&d->dev,
828 				"Looked for SIS5595 but found unsupported device %.4x\n",
829 				*i);
830 			pci_dev_put(d);
831 			return -ENODEV;
832 		}
833 	}
834 
835 	force_addr &= ~(SIS5595_EXTENT - 1);
836 	if (force_addr) {
837 		dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
838 		pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
839 	}
840 
841 	if (PCIBIOS_SUCCESSFUL !=
842 	    pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
843 		dev_err(&dev->dev, "Failed to read ISA address\n");
844 		return -ENODEV;
845 	}
846 
847 	address &= ~(SIS5595_EXTENT - 1);
848 	if (!address) {
849 		dev_err(&dev->dev,
850 			"Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
851 		return -ENODEV;
852 	}
853 	if (force_addr && address != force_addr) {
854 		/* doesn't work for some chips? */
855 		dev_err(&dev->dev, "Failed to force ISA address\n");
856 		return -ENODEV;
857 	}
858 
859 	if (PCIBIOS_SUCCESSFUL !=
860 	    pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
861 		dev_err(&dev->dev, "Failed to read enable register\n");
862 		return -ENODEV;
863 	}
864 	if (!(enable & 0x80)) {
865 		if ((PCIBIOS_SUCCESSFUL !=
866 		     pci_write_config_byte(dev, SIS5595_ENABLE_REG,
867 					   enable | 0x80))
868 		 || (PCIBIOS_SUCCESSFUL !=
869 		     pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
870 		 || (!(enable & 0x80))) {
871 			/* doesn't work for some chips! */
872 			dev_err(&dev->dev, "Failed to enable HWM device\n");
873 			return -ENODEV;
874 		}
875 	}
876 
877 	if (platform_driver_register(&sis5595_driver)) {
878 		dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
879 		goto exit;
880 	}
881 
882 	s_bridge = pci_dev_get(dev);
883 	/* Sets global pdev as a side effect */
884 	if (sis5595_device_add(address))
885 		goto exit_unregister;
886 
887 	/*
888 	 * Always return failure here.  This is to allow other drivers to bind
889 	 * to this pci device.  We don't really want to have control over the
890 	 * pci device, we only wanted to read as few register values from it.
891 	 */
892 	return -ENODEV;
893 
894 exit_unregister:
895 	pci_dev_put(dev);
896 	platform_driver_unregister(&sis5595_driver);
897 exit:
898 	return -ENODEV;
899 }
900 
901 static struct pci_driver sis5595_pci_driver = {
902 	.name            = "sis5595",
903 	.id_table        = sis5595_pci_ids,
904 	.probe           = sis5595_pci_probe,
905 };
906 
907 static int __init sm_sis5595_init(void)
908 {
909 	return pci_register_driver(&sis5595_pci_driver);
910 }
911 
912 static void __exit sm_sis5595_exit(void)
913 {
914 	pci_unregister_driver(&sis5595_pci_driver);
915 	if (s_bridge != NULL) {
916 		platform_device_unregister(pdev);
917 		platform_driver_unregister(&sis5595_driver);
918 		pci_dev_put(s_bridge);
919 		s_bridge = NULL;
920 	}
921 }
922 
923 MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
924 MODULE_DESCRIPTION("SiS 5595 Sensor device");
925 MODULE_LICENSE("GPL");
926 
927 module_init(sm_sis5595_init);
928 module_exit(sm_sis5595_exit);
929