Blame view

drivers/hwmon/sis5595.c 24.6 KB
74ba9207e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
3
4
5
6
7
8
9
   * sis5595.c - Part of lm_sensors, Linux kernel modules
   *	       for hardware monitoring
   *
   * Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
   *			     Kyösti Mälkki <kmalkki@cc.hut.fi>, and
   *			     Mark D. Studebaker <mdsxyz123@yahoo.com>
   * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
7c81c60f3   Jean Delvare   Update Jean Delva...
10
   * the help of Jean Delvare <jdelvare@suse.de>
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
11
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
  
  /*
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   * SiS southbridge has a LM78-like chip integrated on the same IC.
   * This driver is a customized copy of lm78.c
   *
   * Supports following revisions:
   *	Version		PCI ID		PCI Revision
   *	1		1039/0008	AF or less
   *	2		1039/0008	B0 or greater
   *
   *  Note: these chips contain a 0008 device which is incompatible with the
   *	 5595. We recognize these by the presence of the listed
   *	 "blacklist" PCI ID and refuse to load.
   *
   * NOT SUPPORTED	PCI ID		BLACKLIST PCI ID
   *	 540		0008		0540
   *	 550		0008		0550
   *	5513		0008		5511
   *	5581		0008		5597
   *	5582		0008		5597
   *	5597		0008		5597
   *	5598		0008		5597/5598
   *	 630		0008		0630
   *	 645		0008		0645
   *	 730		0008		0730
   *	 735		0008		0735
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39

4b2515dbb   Joe Perches   hwmon: (sis5595) ...
40
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/ioport.h>
  #include <linux/pci.h>
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
45
  #include <linux/platform_device.h>
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
46
  #include <linux/hwmon.h>
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
47
  #include <linux/hwmon-sysfs.h>
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
48
  #include <linux/err.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  #include <linux/init.h>
ff3240946   Dominik Hackl   [PATCH] I2C: incl...
50
  #include <linux/jiffies.h>
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
51
  #include <linux/mutex.h>
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
52
  #include <linux/sysfs.h>
b9acb64a3   Jean Delvare   hwmon: Check for ...
53
  #include <linux/acpi.h>
6055fae8a   H Hartley Sweeten   hwmon: Include <l...
54
  #include <linux/io.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55

8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
56
57
58
59
  /*
   * If force_addr is set to anything different from 0, we forcibly enable
   * the device at the given address.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
  static u16 force_addr;
  module_param(force_addr, ushort, 0);
  MODULE_PARM_DESC(force_addr,
  		 "Initialize the base address of the sensors");
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
64
  static struct platform_device *pdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
  
  /* Many SIS5595 constants specified below */
  
  /* Length of ISA address segment */
  #define SIS5595_EXTENT 8
  /* PCI Config Registers */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #define SIS5595_BASE_REG 0x68
  #define SIS5595_PIN_REG 0x7A
  #define SIS5595_ENABLE_REG 0x7B
  
  /* Where are the ISA address/data registers relative to the base address */
  #define SIS5595_ADDR_REG_OFFSET 5
  #define SIS5595_DATA_REG_OFFSET 6
  
  /* The SIS5595 registers */
  #define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  #define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  #define SIS5595_REG_IN(nr) (0x20 + (nr))
  
  #define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
  #define SIS5595_REG_FAN(nr) (0x28 + (nr))
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
86
87
88
89
90
91
92
  /*
   * On the first version of the chip, the temp registers are separate.
   * On the second version,
   * TEMP pin is shared with IN4, configured in PCI register 0x7A.
   * The registers are the same as well.
   * OVER and HYST are really MAX and MIN.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
  
  #define REV2MIN	0xb0
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
95
96
97
98
99
100
  #define SIS5595_REG_TEMP	(((data->revision) >= REV2MIN) ? \
  					SIS5595_REG_IN(4) : 0x27)
  #define SIS5595_REG_TEMP_OVER	(((data->revision) >= REV2MIN) ? \
  					SIS5595_REG_IN_MAX(4) : 0x39)
  #define SIS5595_REG_TEMP_HYST	(((data->revision) >= REV2MIN) ? \
  					SIS5595_REG_IN_MIN(4) : 0x3a)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
  
  #define SIS5595_REG_CONFIG 0x40
  #define SIS5595_REG_ALARM1 0x41
  #define SIS5595_REG_ALARM2 0x42
  #define SIS5595_REG_FANDIV 0x47
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
106
107
108
109
  /*
   * Conversions. Limit checking is only done on the TO_REG
   * variants.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110

8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
111
112
113
114
  /*
   * IN: mV, (0V to 4.08V)
   * REG: 16mV/bit
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
  static inline u8 IN_TO_REG(unsigned long val)
  {
2a844c148   Guenter Roeck   hwmon: Replace SE...
117
  	unsigned long nval = clamp_val(val, 0, 4080);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
121
122
123
124
125
  	return (nval + 8) / 16;
  }
  #define IN_FROM_REG(val) ((val) *  16)
  
  static inline u8 FAN_TO_REG(long rpm, int div)
  {
  	if (rpm <= 0)
  		return 255;
3806b45ba   Dan Carpenter   hwmon: Prevent so...
126
127
  	if (rpm > 1350000)
  		return 1;
2a844c148   Guenter Roeck   hwmon: Replace SE...
128
  	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
  }
  
  static inline int FAN_FROM_REG(u8 val, int div)
  {
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
133
  	return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  }
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
135
136
137
138
  /*
   * TEMP: mC (-54.12C to +157.53C)
   * REG: 0.83C/bit + 52.12, two's complement
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
  static inline int TEMP_FROM_REG(s8 val)
  {
  	return val * 830 + 52120;
  }
cc336546d   Axel Lin   hwmon: (sis5595) ...
143
  static inline s8 TEMP_TO_REG(long val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
  {
2a844c148   Guenter Roeck   hwmon: Replace SE...
145
  	int nval = clamp_val(val, -54120, 157530) ;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
146
  	return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
  }
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
148
149
150
151
  /*
   * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
   * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
  static inline u8 DIV_TO_REG(int val)
  {
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
154
  	return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  }
  #define DIV_FROM_REG(val) (1 << (val))
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
157
158
159
160
  /*
   * For each registered chip, we need to keep some data in memory.
   * The structure is dynamically allocated.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  struct sis5595_data {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
162
163
  	unsigned short addr;
  	const char *name;
1beeffe43   Tony Jones   hwmon: Convert fr...
164
  	struct device *hwmon_dev;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
165
  	struct mutex lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
167
  	struct mutex update_lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  	char valid;		/* !=0 if following fields are valid */
  	unsigned long last_updated;	/* In jiffies */
  	char maxins;		/* == 3 if temp enabled, otherwise == 4 */
  	u8 revision;		/* Reg. value */
  
  	u8 in[5];		/* Register value */
  	u8 in_max[5];		/* Register value */
  	u8 in_min[5];		/* Register value */
  	u8 fan[2];		/* Register value */
  	u8 fan_min[2];		/* Register value */
  	s8 temp;		/* Register value */
  	s8 temp_over;		/* Register value */
  	s8 temp_hyst;		/* Register value */
  	u8 fan_div[2];		/* Register encoding, shifted right */
  	u16 alarms;		/* Register encoding, combined */
  };
  
  static struct pci_dev *s_bridge;	/* pointer to the (only) sis5595 */
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
186
  static int sis5595_probe(struct platform_device *pdev);
281dfd0b6   Bill Pemberton   hwmon: remove use...
187
  static int sis5595_remove(struct platform_device *pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188

17e7dc437   Jean Delvare   hwmon/sis5595: Co...
189
190
  static int sis5595_read_value(struct sis5595_data *data, u8 reg);
  static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
  static struct sis5595_data *sis5595_update_device(struct device *dev);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
192
  static void sis5595_init_device(struct sis5595_data *data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193

17e7dc437   Jean Delvare   hwmon/sis5595: Co...
194
  static struct platform_driver sis5595_driver = {
cdaf79349   Laurent Riffard   [PATCH] i2c: Drop...
195
  	.driver = {
cdaf79349   Laurent Riffard   [PATCH] i2c: Drop...
196
197
  		.name	= "sis5595",
  	},
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
198
  	.probe		= sis5595_probe,
9e5e9b7a9   Bill Pemberton   hwmon: remove use...
199
  	.remove		= sis5595_remove,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
  };
  
  /* 4 Voltages */
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
203
  static ssize_t in_show(struct device *dev, struct device_attribute *da,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
204
  		       char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
207
208
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
211
  	return sprintf(buf, "%d
  ", IN_FROM_REG(data->in[nr]));
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
212
  static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
213
  			   char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
216
217
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
  	return sprintf(buf, "%d
  ", IN_FROM_REG(data->in_min[nr]));
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
221
  static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
222
  			   char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
225
226
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
  	return sprintf(buf, "%d
  ", IN_FROM_REG(data->in_max[nr]));
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
230
231
  static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
  			    const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
233
  	struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
234
235
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
236
237
238
239
240
241
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
243
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
  	data->in_min[nr] = IN_TO_REG(val);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
245
  	sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
246
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
  	return count;
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
249
250
  static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
  			    const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
252
  	struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
253
254
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
255
256
257
258
259
260
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
262
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  	data->in_max[nr] = IN_TO_REG(val);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
264
  	sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
265
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
  	return count;
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
  static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
  static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
  static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
  static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
  static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
  static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
  static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
  static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
  static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
  static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
  static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
  static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
  static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
  static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
  
  /* Temperature */
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
285
286
  static ssize_t temp1_input_show(struct device *dev,
  				struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG(data->temp));
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
292
  static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
293
  			      char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
295
296
297
298
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG(data->temp_over));
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
299
300
301
  static ssize_t temp1_max_store(struct device *dev,
  			       struct device_attribute *attr, const char *buf,
  			       size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
303
  	struct sis5595_data *data = dev_get_drvdata(dev);
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
304
305
306
307
308
309
  	long val;
  	int err;
  
  	err = kstrtol(buf, 10, &val);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
311
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  	data->temp_over = TEMP_TO_REG(val);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
313
  	sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
314
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
316
  	return count;
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
317
318
  static ssize_t temp1_max_hyst_show(struct device *dev,
  				   struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG(data->temp_hyst));
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
324
325
326
  static ssize_t temp1_max_hyst_store(struct device *dev,
  				    struct device_attribute *attr,
  				    const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
328
  	struct sis5595_data *data = dev_get_drvdata(dev);
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
329
330
331
332
333
334
  	long val;
  	int err;
  
  	err = kstrtol(buf, 10, &val);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
336
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
  	data->temp_hyst = TEMP_TO_REG(val);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
338
  	sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
339
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
  	return count;
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
342
343
344
  static DEVICE_ATTR_RO(temp1_input);
  static DEVICE_ATTR_RW(temp1_max);
  static DEVICE_ATTR_RW(temp1_max_hyst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
  
  /* 2 Fans */
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
347
  static ssize_t fan_show(struct device *dev, struct device_attribute *da,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
348
  			char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349
350
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
351
352
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
  	return sprintf(buf, "%d
  ", FAN_FROM_REG(data->fan[nr],
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
355
  		DIV_FROM_REG(data->fan_div[nr])));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
357
  static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
358
  			    char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
361
362
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
363
364
365
  	return sprintf(buf, "%d
  ", FAN_FROM_REG(data->fan_min[nr],
  		DIV_FROM_REG(data->fan_div[nr])));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
367
368
  static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
  			     const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
370
  	struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
371
372
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
373
374
375
376
377
378
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
380
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  	data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
382
  	sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
383
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
385
  	return count;
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
386
  static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
387
  			    char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
390
391
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
392
393
  	return sprintf(buf, "%d
  ", DIV_FROM_REG(data->fan_div[nr]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  }
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
395
396
397
398
399
400
  /*
   * Note: we save and restore the fan minimum here, because its value is
   * determined in part by the fan divisor.  This follows the principle of
   * least surprise; the user doesn't expect the fan minimum to change just
   * because the divisor changed.
   */
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
401
402
  static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
  			     const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
404
  	struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
405
406
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
  	unsigned long min;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
  	int reg;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
409
410
411
412
413
414
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
416
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
  	min = FAN_FROM_REG(data->fan_min[nr],
  			DIV_FROM_REG(data->fan_div[nr]));
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
419
  	reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
  
  	switch (val) {
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
422
423
424
425
426
427
428
429
430
431
432
433
  	case 1:
  		data->fan_div[nr] = 0;
  		break;
  	case 2:
  		data->fan_div[nr] = 1;
  		break;
  	case 4:
  		data->fan_div[nr] = 2;
  		break;
  	case 8:
  		data->fan_div[nr] = 3;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
  	default:
b55f37572   Guenter Roeck   hwmon: Fix checkp...
435
436
437
438
  		dev_err(dev,
  			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!
  ",
  			val);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
439
  		mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
441
  		return -EINVAL;
  	}
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
442

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
446
447
448
449
450
  	switch (nr) {
  	case 0:
  		reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
  		break;
  	case 1:
  		reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
  		break;
  	}
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
451
  	sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
  	data->fan_min[nr] =
  		FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
454
  	sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
455
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
457
  	return count;
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
458
459
460
461
462
463
  static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
  static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
  static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
  static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
  static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
  static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
  /* Alarms */
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
466
  static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
467
  			   char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
470
471
472
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
  	return sprintf(buf, "%d
  ", data->alarms);
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
473
  static DEVICE_ATTR_RO(alarms);
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
474

60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
475
  static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
476
477
478
479
480
481
482
  			  char *buf)
  {
  	struct sis5595_data *data = sis5595_update_device(dev);
  	int nr = to_sensor_dev_attr(da)->index;
  	return sprintf(buf, "%u
  ", (data->alarms >> nr) & 1);
  }
60a9c3f15   Guenter Roeck   hwmon: (sis5595) ...
483
484
485
486
487
488
489
490
  static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
  static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
  static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
  static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
  static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 15);
  static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
  static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
  static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 15);
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
491

feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
492
  static ssize_t name_show(struct device *dev, struct device_attribute *attr,
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
493
494
495
496
497
498
  			 char *buf)
  {
  	struct sis5595_data *data = dev_get_drvdata(dev);
  	return sprintf(buf, "%s
  ", data->name);
  }
feaf8bd49   Julia Lawall   hwmon: (sis5595) ...
499
  static DEVICE_ATTR_RO(name);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
500

a5ebe668a   Jean Delvare   hwmon: Fix unchec...
501
  static struct attribute *sis5595_attributes[] = {
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
502
503
504
  	&sensor_dev_attr_in0_input.dev_attr.attr,
  	&sensor_dev_attr_in0_min.dev_attr.attr,
  	&sensor_dev_attr_in0_max.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
505
  	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
506
507
508
  	&sensor_dev_attr_in1_input.dev_attr.attr,
  	&sensor_dev_attr_in1_min.dev_attr.attr,
  	&sensor_dev_attr_in1_max.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
509
  	&sensor_dev_attr_in1_alarm.dev_attr.attr,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
510
511
512
  	&sensor_dev_attr_in2_input.dev_attr.attr,
  	&sensor_dev_attr_in2_min.dev_attr.attr,
  	&sensor_dev_attr_in2_max.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
513
  	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
514
515
516
  	&sensor_dev_attr_in3_input.dev_attr.attr,
  	&sensor_dev_attr_in3_min.dev_attr.attr,
  	&sensor_dev_attr_in3_max.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
517
  	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
518
519
520
521
  
  	&sensor_dev_attr_fan1_input.dev_attr.attr,
  	&sensor_dev_attr_fan1_min.dev_attr.attr,
  	&sensor_dev_attr_fan1_div.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
522
  	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
523
524
525
  	&sensor_dev_attr_fan2_input.dev_attr.attr,
  	&sensor_dev_attr_fan2_min.dev_attr.attr,
  	&sensor_dev_attr_fan2_div.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
526
  	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
527
528
  
  	&dev_attr_alarms.attr,
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
529
  	&dev_attr_name.attr,
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
530
531
532
533
534
535
  	NULL
  };
  
  static const struct attribute_group sis5595_group = {
  	.attrs = sis5595_attributes,
  };
76e63860d   Ivo Manca   hwmon: (sis5595) ...
536
  static struct attribute *sis5595_attributes_in4[] = {
1f5f48dde   Jean Delvare   hwmon/sis5595: Us...
537
538
539
  	&sensor_dev_attr_in4_input.dev_attr.attr,
  	&sensor_dev_attr_in4_min.dev_attr.attr,
  	&sensor_dev_attr_in4_max.dev_attr.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
540
  	&sensor_dev_attr_in4_alarm.dev_attr.attr,
76e63860d   Ivo Manca   hwmon: (sis5595) ...
541
542
543
544
545
546
  	NULL
  };
  
  static const struct attribute_group sis5595_group_in4 = {
  	.attrs = sis5595_attributes_in4,
  };
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
547

76e63860d   Ivo Manca   hwmon: (sis5595) ...
548
  static struct attribute *sis5595_attributes_temp1[] = {
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
549
550
551
  	&dev_attr_temp1_input.attr,
  	&dev_attr_temp1_max.attr,
  	&dev_attr_temp1_max_hyst.attr,
5c726b3ba   Ivo Manca   hwmon: (sis5595) ...
552
  	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
553
554
  	NULL
  };
76e63860d   Ivo Manca   hwmon: (sis5595) ...
555
556
  static const struct attribute_group sis5595_group_temp1 = {
  	.attrs = sis5595_attributes_temp1,
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
557
  };
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
558

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
  /* This is called when the module is loaded */
6c931ae1c   Bill Pemberton   hwmon: remove use...
560
  static int sis5595_probe(struct platform_device *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
562
563
  {
  	int err = 0;
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  	struct sis5595_data *data;
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
565
  	struct resource *res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
  	char val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568
  	/* Reserve the ISA region */
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
569
  	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
5d224ade3   Guenter Roeck   hwmon: (sis5595) ...
570
571
572
  	if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
  				 sis5595_driver.driver.name))
  		return -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573

5d224ade3   Guenter Roeck   hwmon: (sis5595) ...
574
575
576
577
  	data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
  			    GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
579
  	mutex_init(&data->lock);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
580
581
582
583
  	mutex_init(&data->update_lock);
  	data->addr = res->start;
  	data->name = "sis5595";
  	platform_set_drvdata(pdev, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584

8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
585
586
587
  	/*
  	 * Check revision and pin registers to determine whether 4 or 5 voltages
  	 */
7b6d1f044   Auke Kok   hwmon: (sis5595) ...
588
  	data->revision = s_bridge->revision;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
590
591
592
593
594
595
596
  	/* 4 voltages, 1 temp */
  	data->maxins = 3;
  	if (data->revision >= REV2MIN) {
  		pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
  		if (!(val & 0x80))
  			/* 5 voltages, no temps */
  			data->maxins = 4;
  	}
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
597

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
598
  	/* Initialize the SIS5595 chip */
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
599
  	sis5595_init_device(data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
602
  
  	/* A few vars need to be filled upon startup */
  	for (i = 0; i < 2; i++) {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
603
  		data->fan_min[i] = sis5595_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
607
  					SIS5595_REG_FAN_MIN(i));
  	}
  
  	/* Register sysfs hooks */
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
608
609
  	err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
  	if (err)
5d224ade3   Guenter Roeck   hwmon: (sis5595) ...
610
  		return err;
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
611
  	if (data->maxins == 4) {
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
612
613
  		err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
  		if (err)
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
614
615
  			goto exit_remove_files;
  	} else {
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
616
617
  		err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
  		if (err)
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
618
619
  			goto exit_remove_files;
  	}
1beeffe43   Tony Jones   hwmon: Convert fr...
620
621
622
  	data->hwmon_dev = hwmon_device_register(&pdev->dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
a5ebe668a   Jean Delvare   hwmon: Fix unchec...
623
  		goto exit_remove_files;
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
624
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625
  	return 0;
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
626

a5ebe668a   Jean Delvare   hwmon: Fix unchec...
627
  exit_remove_files:
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
628
  	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
76e63860d   Ivo Manca   hwmon: (sis5595) ...
629
630
  	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
632
  	return err;
  }
281dfd0b6   Bill Pemberton   hwmon: remove use...
633
  static int sis5595_remove(struct platform_device *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
635
  	struct sis5595_data *data = platform_get_drvdata(pdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
636

1beeffe43   Tony Jones   hwmon: Convert fr...
637
  	hwmon_device_unregister(data->hwmon_dev);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
638
  	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
76e63860d   Ivo Manca   hwmon: (sis5595) ...
639
640
  	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  	sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
  /* ISA access must be locked explicitly. */
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
645
  static int sis5595_read_value(struct sis5595_data *data, u8 reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646
647
  {
  	int res;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
648
  	mutex_lock(&data->lock);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
649
650
  	outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  	res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
651
  	mutex_unlock(&data->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
  	return res;
  }
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
654
  static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
  {
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
656
  	mutex_lock(&data->lock);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
657
658
  	outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  	outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
659
  	mutex_unlock(&data->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
661
662
  }
  
  /* Called when we have found a new SIS5595. */
6c931ae1c   Bill Pemberton   hwmon: remove use...
663
  static void sis5595_init_device(struct sis5595_data *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
665
  	u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
  	if (!(config & 0x01))
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
667
  		sis5595_write_value(data, SIS5595_REG_CONFIG,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
669
670
671
672
  				(config & 0xf7) | 0x01);
  }
  
  static struct sis5595_data *sis5595_update_device(struct device *dev)
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
673
  	struct sis5595_data *data = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
  	int i;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
675
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
677
678
679
680
681
  
  	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  	    || !data->valid) {
  
  		for (i = 0; i <= data->maxins; i++) {
  			data->in[i] =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
682
  			    sis5595_read_value(data, SIS5595_REG_IN(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
  			data->in_min[i] =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
684
  			    sis5595_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
685
686
  					       SIS5595_REG_IN_MIN(i));
  			data->in_max[i] =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
687
  			    sis5595_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
  					       SIS5595_REG_IN_MAX(i));
  		}
  		for (i = 0; i < 2; i++) {
  			data->fan[i] =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
692
  			    sis5595_read_value(data, SIS5595_REG_FAN(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
  			data->fan_min[i] =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
694
  			    sis5595_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
696
697
698
  					       SIS5595_REG_FAN_MIN(i));
  		}
  		if (data->maxins == 3) {
  			data->temp =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
699
  			    sis5595_read_value(data, SIS5595_REG_TEMP);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
700
  			data->temp_over =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
701
  			    sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
  			data->temp_hyst =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
703
  			    sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
  		}
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
705
  		i = sis5595_read_value(data, SIS5595_REG_FANDIV);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
708
  		data->fan_div[0] = (i >> 4) & 0x03;
  		data->fan_div[1] = i >> 6;
  		data->alarms =
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
709
710
  		    sis5595_read_value(data, SIS5595_REG_ALARM1) |
  		    (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
  		data->last_updated = jiffies;
  		data->valid = 1;
  	}
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
714
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
716
717
  
  	return data;
  }
cd9bb0564   Jingoo Han   hwmon: remove DEF...
718
  static const struct pci_device_id sis5595_pci_ids[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
720
721
722
723
  	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  	{ 0, }
  };
  
  MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
a5977246c   Bill Pemberton   hwmon: remove use...
724
  static int blacklist[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
725
726
727
728
729
730
  	PCI_DEVICE_ID_SI_540,
  	PCI_DEVICE_ID_SI_550,
  	PCI_DEVICE_ID_SI_630,
  	PCI_DEVICE_ID_SI_645,
  	PCI_DEVICE_ID_SI_730,
  	PCI_DEVICE_ID_SI_735,
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
731
732
733
734
735
  	PCI_DEVICE_ID_SI_5511, /*
  				* 5513 chip has the 0008 device but
  				* that ID shows up in other chips so we
  				* use the 5511 ID for recognition
  				*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
737
738
  	PCI_DEVICE_ID_SI_5597,
  	PCI_DEVICE_ID_SI_5598,
  	0 };
6c931ae1c   Bill Pemberton   hwmon: remove use...
739
  static int sis5595_device_add(unsigned short address)
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
740
741
742
743
744
745
746
747
  {
  	struct resource res = {
  		.start	= address,
  		.end	= address + SIS5595_EXTENT - 1,
  		.name	= "sis5595",
  		.flags	= IORESOURCE_IO,
  	};
  	int err;
b9acb64a3   Jean Delvare   hwmon: Check for ...
748
749
750
  	err = acpi_check_resource_conflict(&res);
  	if (err)
  		goto exit;
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
751
752
753
  	pdev = platform_device_alloc("sis5595", address);
  	if (!pdev) {
  		err = -ENOMEM;
4b2515dbb   Joe Perches   hwmon: (sis5595) ...
754
755
  		pr_err("Device allocation failed
  ");
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
756
757
758
759
760
  		goto exit;
  	}
  
  	err = platform_device_add_resources(pdev, &res, 1);
  	if (err) {
4b2515dbb   Joe Perches   hwmon: (sis5595) ...
761
762
  		pr_err("Device resource addition failed (%d)
  ", err);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
763
764
765
766
767
  		goto exit_device_put;
  	}
  
  	err = platform_device_add(pdev);
  	if (err) {
4b2515dbb   Joe Perches   hwmon: (sis5595) ...
768
769
  		pr_err("Device addition failed (%d)
  ", err);
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
770
771
772
773
774
775
776
777
778
779
  		goto exit_device_put;
  	}
  
  	return 0;
  
  exit_device_put:
  	platform_device_put(pdev);
  exit:
  	return err;
  }
6c931ae1c   Bill Pemberton   hwmon: remove use...
780
  static int sis5595_pci_probe(struct pci_dev *dev,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
  				       const struct pci_device_id *id)
  {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
783
784
  	u16 address;
  	u8 enable;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
  	int *i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786
787
  
  	for (i = blacklist; *i != 0; i++) {
5460a9d0f   Mark M. Hoffman   hwmon: (sis5595) ...
788
  		struct pci_dev *d;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
789
790
791
792
793
794
  		d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
  		if (d) {
  			dev_err(&d->dev,
  				"Looked for SIS5595 but found unsupported device %.4x
  ",
  				*i);
5460a9d0f   Mark M. Hoffman   hwmon: (sis5595) ...
795
  			pci_dev_put(d);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
797
798
  			return -ENODEV;
  		}
  	}
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
799

17e7dc437   Jean Delvare   hwmon/sis5595: Co...
800
801
802
803
804
805
  	force_addr &= ~(SIS5595_EXTENT - 1);
  	if (force_addr) {
  		dev_warn(&dev->dev, "Forcing ISA address 0x%x
  ", force_addr);
  		pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
  	if (PCIBIOS_SUCCESSFUL !=
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
807
808
809
  	    pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
  		dev_err(&dev->dev, "Failed to read ISA address
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
  		return -ENODEV;
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
811
  	}
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
812

17e7dc437   Jean Delvare   hwmon/sis5595: Co...
813
814
  	address &= ~(SIS5595_EXTENT - 1);
  	if (!address) {
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
815
816
817
  		dev_err(&dev->dev,
  			"Base address not set - upgrade BIOS or use force_addr=0xaddr
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
  	}
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
820
821
822
823
824
825
  	if (force_addr && address != force_addr) {
  		/* doesn't work for some chips? */
  		dev_err(&dev->dev, "Failed to force ISA address
  ");
  		return -ENODEV;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826

17e7dc437   Jean Delvare   hwmon/sis5595: Co...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
  	if (PCIBIOS_SUCCESSFUL !=
  	    pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
  		dev_err(&dev->dev, "Failed to read enable register
  ");
  		return -ENODEV;
  	}
  	if (!(enable & 0x80)) {
  		if ((PCIBIOS_SUCCESSFUL !=
  		     pci_write_config_byte(dev, SIS5595_ENABLE_REG,
  					   enable | 0x80))
  		 || (PCIBIOS_SUCCESSFUL !=
  		     pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
  		 || (!(enable & 0x80))) {
  			/* doesn't work for some chips! */
  			dev_err(&dev->dev, "Failed to enable HWM device
  ");
  			return -ENODEV;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
  	}
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
846
847
848
849
850
851
852
853
854
855
  	if (platform_driver_register(&sis5595_driver)) {
  		dev_dbg(&dev->dev, "Failed to register sis5595 driver
  ");
  		goto exit;
  	}
  
  	s_bridge = pci_dev_get(dev);
  	/* Sets global pdev as a side effect */
  	if (sis5595_device_add(address))
  		goto exit_unregister;
8fda79ec8   Guenter Roeck   hwmon: (sis5595) ...
856
857
  	/*
  	 * Always return failure here.  This is to allow other drivers to bind
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
859
860
861
  	 * to this pci device.  We don't really want to have control over the
  	 * pci device, we only wanted to read as few register values from it.
  	 */
  	return -ENODEV;
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
862
863
864
865
866
867
  
  exit_unregister:
  	pci_dev_put(dev);
  	platform_driver_unregister(&sis5595_driver);
  exit:
  	return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
  }
  
  static struct pci_driver sis5595_pci_driver = {
  	.name            = "sis5595",
  	.id_table        = sis5595_pci_ids,
  	.probe           = sis5595_pci_probe,
  };
  
  static int __init sm_sis5595_init(void)
  {
  	return pci_register_driver(&sis5595_pci_driver);
  }
  
  static void __exit sm_sis5595_exit(void)
  {
  	pci_unregister_driver(&sis5595_pci_driver);
  	if (s_bridge != NULL) {
17e7dc437   Jean Delvare   hwmon/sis5595: Co...
885
886
  		platform_device_unregister(pdev);
  		platform_driver_unregister(&sis5595_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
888
889
890
891
892
893
894
895
896
897
  		pci_dev_put(s_bridge);
  		s_bridge = NULL;
  	}
  }
  
  MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  MODULE_DESCRIPTION("SiS 5595 Sensor device");
  MODULE_LICENSE("GPL");
  
  module_init(sm_sis5595_init);
  module_exit(sm_sis5595_exit);