blob: 063f71c5f07eabf14f33dc671e2b1da6df73eedf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Jean Delvarefde09502005-07-19 23:51:07 +020058#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040059#include <linux/hwmon.h>
60#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/init.h>
Dominik Hacklff324092005-05-16 18:12:18 +020062#include <linux/jiffies.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010063#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#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. */
69static u16 force_addr;
70module_param(force_addr, ushort, 0);
71MODULE_PARM_DESC(force_addr,
72 "Initialize the base address of the sensors");
73
Jean Delvare2d8672c2005-07-19 23:56:35 +020074/* Device address
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 Note that we can't determine the ISA address until we have initialized
76 our module */
Jean Delvare2d8672c2005-07-19 23:56:35 +020077static unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Many SIS5595 constants specified below */
80
81/* Length of ISA address segment */
82#define SIS5595_EXTENT 8
83/* PCI Config Registers */
84#define SIS5595_REVISION_REG 0x08
85#define SIS5595_BASE_REG 0x68
86#define SIS5595_PIN_REG 0x7A
87#define SIS5595_ENABLE_REG 0x7B
88
89/* Where are the ISA address/data registers relative to the base address */
90#define SIS5595_ADDR_REG_OFFSET 5
91#define SIS5595_DATA_REG_OFFSET 6
92
93/* The SIS5595 registers */
94#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
95#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
96#define SIS5595_REG_IN(nr) (0x20 + (nr))
97
98#define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
99#define SIS5595_REG_FAN(nr) (0x28 + (nr))
100
101/* On the first version of the chip, the temp registers are separate.
102 On the second version,
103 TEMP pin is shared with IN4, configured in PCI register 0x7A.
104 The registers are the same as well.
105 OVER and HYST are really MAX and MIN. */
106
107#define REV2MIN 0xb0
108#define SIS5595_REG_TEMP (( data->revision) >= REV2MIN) ? \
109 SIS5595_REG_IN(4) : 0x27
110#define SIS5595_REG_TEMP_OVER (( data->revision) >= REV2MIN) ? \
111 SIS5595_REG_IN_MAX(4) : 0x39
112#define SIS5595_REG_TEMP_HYST (( data->revision) >= REV2MIN) ? \
113 SIS5595_REG_IN_MIN(4) : 0x3a
114
115#define SIS5595_REG_CONFIG 0x40
116#define SIS5595_REG_ALARM1 0x41
117#define SIS5595_REG_ALARM2 0x42
118#define SIS5595_REG_FANDIV 0x47
119
120/* Conversions. Limit checking is only done on the TO_REG
121 variants. */
122
123/* IN: mV, (0V to 4.08V)
124 REG: 16mV/bit */
125static inline u8 IN_TO_REG(unsigned long val)
126{
127 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
128 return (nval + 8) / 16;
129}
130#define IN_FROM_REG(val) ((val) * 16)
131
132static inline u8 FAN_TO_REG(long rpm, int div)
133{
134 if (rpm <= 0)
135 return 255;
136 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
137}
138
139static inline int FAN_FROM_REG(u8 val, int div)
140{
141 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
142}
143
144/* TEMP: mC (-54.12C to +157.53C)
145 REG: 0.83C/bit + 52.12, two's complement */
146static inline int TEMP_FROM_REG(s8 val)
147{
148 return val * 830 + 52120;
149}
150static inline s8 TEMP_TO_REG(int val)
151{
152 int nval = SENSORS_LIMIT(val, -54120, 157530) ;
153 return nval<0 ? (nval-5212-415)/830 : (nval-5212+415)/830;
154}
155
156/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
157 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
158static inline u8 DIV_TO_REG(int val)
159{
160 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
161}
162#define DIV_FROM_REG(val) (1 << (val))
163
164/* For the SIS5595, we need to keep some data in memory. That
165 data is pointed to by sis5595_list[NR]->data. The structure itself is
166 dynamically allocated, at the time when the new sis5595 client is
167 allocated. */
168struct sis5595_data {
169 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400170 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100171 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100173 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 char valid; /* !=0 if following fields are valid */
175 unsigned long last_updated; /* In jiffies */
176 char maxins; /* == 3 if temp enabled, otherwise == 4 */
177 u8 revision; /* Reg. value */
178
179 u8 in[5]; /* Register value */
180 u8 in_max[5]; /* Register value */
181 u8 in_min[5]; /* Register value */
182 u8 fan[2]; /* Register value */
183 u8 fan_min[2]; /* Register value */
184 s8 temp; /* Register value */
185 s8 temp_over; /* Register value */
186 s8 temp_hyst; /* Register value */
187 u8 fan_div[2]; /* Register encoding, shifted right */
188 u16 alarms; /* Register encoding, combined */
189};
190
191static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
192
Jean Delvare2d8672c2005-07-19 23:56:35 +0200193static int sis5595_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int sis5595_detach_client(struct i2c_client *client);
195
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100196static int sis5595_read_value(struct i2c_client *client, u8 reg);
197static int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static struct sis5595_data *sis5595_update_device(struct device *dev);
199static void sis5595_init_client(struct i2c_client *client);
200
201static struct i2c_driver sis5595_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100202 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100203 .name = "sis5595",
204 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200205 .attach_adapter = sis5595_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .detach_client = sis5595_detach_client,
207};
208
209/* 4 Voltages */
210static ssize_t show_in(struct device *dev, char *buf, int nr)
211{
212 struct sis5595_data *data = sis5595_update_device(dev);
213 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
214}
215
216static ssize_t show_in_min(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_min[nr]));
220}
221
222static ssize_t show_in_max(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_max[nr]));
226}
227
228static ssize_t set_in_min(struct device *dev, const char *buf,
229 size_t count, int nr)
230{
231 struct i2c_client *client = to_i2c_client(dev);
232 struct sis5595_data *data = i2c_get_clientdata(client);
233 unsigned long val = simple_strtoul(buf, NULL, 10);
234
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100235 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 data->in_min[nr] = IN_TO_REG(val);
237 sis5595_write_value(client, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100238 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return count;
240}
241
242static ssize_t set_in_max(struct device *dev, const char *buf,
243 size_t count, int nr)
244{
245 struct i2c_client *client = to_i2c_client(dev);
246 struct sis5595_data *data = i2c_get_clientdata(client);
247 unsigned long val = simple_strtoul(buf, NULL, 10);
248
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100249 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 data->in_max[nr] = IN_TO_REG(val);
251 sis5595_write_value(client, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100252 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return count;
254}
255
256#define show_in_offset(offset) \
257static ssize_t \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400258 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{ \
260 return show_in(dev, buf, offset); \
261} \
262static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
263 show_in##offset, NULL); \
264static ssize_t \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400265 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{ \
267 return show_in_min(dev, buf, offset); \
268} \
269static ssize_t \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400270 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{ \
272 return show_in_max(dev, buf, offset); \
273} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400274static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 const char *buf, size_t count) \
276{ \
277 return set_in_min(dev, buf, count, offset); \
278} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400279static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 const char *buf, size_t count) \
281{ \
282 return set_in_max(dev, buf, count, offset); \
283} \
284static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
285 show_in##offset##_min, set_in##offset##_min); \
286static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
287 show_in##offset##_max, set_in##offset##_max);
288
289show_in_offset(0);
290show_in_offset(1);
291show_in_offset(2);
292show_in_offset(3);
293show_in_offset(4);
294
295/* Temperature */
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400296static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct sis5595_data *data = sis5595_update_device(dev);
299 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
300}
301
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400302static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct sis5595_data *data = sis5595_update_device(dev);
305 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
306}
307
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400308static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct i2c_client *client = to_i2c_client(dev);
311 struct sis5595_data *data = i2c_get_clientdata(client);
312 long val = simple_strtol(buf, NULL, 10);
313
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100314 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 data->temp_over = TEMP_TO_REG(val);
316 sis5595_write_value(client, SIS5595_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100317 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return count;
319}
320
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400321static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct sis5595_data *data = sis5595_update_device(dev);
324 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
325}
326
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400327static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct i2c_client *client = to_i2c_client(dev);
330 struct sis5595_data *data = i2c_get_clientdata(client);
331 long val = simple_strtol(buf, NULL, 10);
332
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100333 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 data->temp_hyst = TEMP_TO_REG(val);
335 sis5595_write_value(client, SIS5595_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100336 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return count;
338}
339
340static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
341static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
342 show_temp_over, set_temp_over);
343static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
344 show_temp_hyst, set_temp_hyst);
345
346/* 2 Fans */
347static ssize_t show_fan(struct device *dev, char *buf, int nr)
348{
349 struct sis5595_data *data = sis5595_update_device(dev);
350 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
351 DIV_FROM_REG(data->fan_div[nr])) );
352}
353
354static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
355{
356 struct sis5595_data *data = sis5595_update_device(dev);
357 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
358 DIV_FROM_REG(data->fan_div[nr])) );
359}
360
361static ssize_t set_fan_min(struct device *dev, const char *buf,
362 size_t count, int nr)
363{
364 struct i2c_client *client = to_i2c_client(dev);
365 struct sis5595_data *data = i2c_get_clientdata(client);
366 unsigned long val = simple_strtoul(buf, NULL, 10);
367
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
370 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100371 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return count;
373}
374
375static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
376{
377 struct sis5595_data *data = sis5595_update_device(dev);
378 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
379}
380
381/* Note: we save and restore the fan minimum here, because its value is
382 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200383 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 because the divisor changed. */
385static ssize_t set_fan_div(struct device *dev, const char *buf,
386 size_t count, int nr)
387{
388 struct i2c_client *client = to_i2c_client(dev);
389 struct sis5595_data *data = i2c_get_clientdata(client);
390 unsigned long min;
391 unsigned long val = simple_strtoul(buf, NULL, 10);
392 int reg;
393
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100394 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 min = FAN_FROM_REG(data->fan_min[nr],
396 DIV_FROM_REG(data->fan_div[nr]));
397 reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
398
399 switch (val) {
400 case 1: data->fan_div[nr] = 0; break;
401 case 2: data->fan_div[nr] = 1; break;
402 case 4: data->fan_div[nr] = 2; break;
403 case 8: data->fan_div[nr] = 3; break;
404 default:
405 dev_err(&client->dev, "fan_div value %ld not "
406 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100407 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -EINVAL;
409 }
410
411 switch (nr) {
412 case 0:
413 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
414 break;
415 case 1:
416 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
417 break;
418 }
419 sis5595_write_value(client, SIS5595_REG_FANDIV, reg);
420 data->fan_min[nr] =
421 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
422 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100423 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return count;
425}
426
427#define show_fan_offset(offset) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400428static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{ \
430 return show_fan(dev, buf, offset - 1); \
431} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400432static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{ \
434 return show_fan_min(dev, buf, offset - 1); \
435} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400436static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{ \
438 return show_fan_div(dev, buf, offset - 1); \
439} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400440static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 const char *buf, size_t count) \
442{ \
443 return set_fan_min(dev, buf, count, offset - 1); \
444} \
445static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
446static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
447 show_fan_##offset##_min, set_fan_##offset##_min);
448
449show_fan_offset(1);
450show_fan_offset(2);
451
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400452static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 size_t count)
454{
455 return set_fan_div(dev, buf, count, 0) ;
456}
457
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400458static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 size_t count)
460{
461 return set_fan_div(dev, buf, count, 1) ;
462}
463static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
464 show_fan_1_div, set_fan_1_div);
465static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
466 show_fan_2_div, set_fan_2_div);
467
468/* Alarms */
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400469static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct sis5595_data *data = sis5595_update_device(dev);
472 return sprintf(buf, "%d\n", data->alarms);
473}
474static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
475
476/* This is called when the module is loaded */
Jean Delvare2d8672c2005-07-19 23:56:35 +0200477static int sis5595_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int err = 0;
480 int i;
481 struct i2c_client *new_client;
482 struct sis5595_data *data;
483 char val;
484 u16 a;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (force_addr)
487 address = force_addr & ~(SIS5595_EXTENT - 1);
488 /* Reserve the ISA region */
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100489 if (!request_region(address, SIS5595_EXTENT,
490 sis5595_driver.driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 err = -EBUSY;
492 goto exit;
493 }
494 if (force_addr) {
495 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n", address);
496 if (PCIBIOS_SUCCESSFUL !=
497 pci_write_config_word(s_bridge, SIS5595_BASE_REG, address))
498 goto exit_release;
499 if (PCIBIOS_SUCCESSFUL !=
500 pci_read_config_word(s_bridge, SIS5595_BASE_REG, &a))
501 goto exit_release;
502 if ((a & ~(SIS5595_EXTENT - 1)) != address)
503 /* doesn't work for some chips? */
504 goto exit_release;
505 }
506
507 if (PCIBIOS_SUCCESSFUL !=
508 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val)) {
509 goto exit_release;
510 }
511 if ((val & 0x80) == 0) {
512 if (PCIBIOS_SUCCESSFUL !=
513 pci_write_config_byte(s_bridge, SIS5595_ENABLE_REG,
514 val | 0x80))
515 goto exit_release;
516 if (PCIBIOS_SUCCESSFUL !=
517 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val))
518 goto exit_release;
519 if ((val & 0x80) == 0)
520 /* doesn't work for some chips! */
521 goto exit_release;
522 }
523
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200524 if (!(data = kzalloc(sizeof(struct sis5595_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 err = -ENOMEM;
526 goto exit_release;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 new_client = &data->client;
530 new_client->addr = address;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100531 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 i2c_set_clientdata(new_client, data);
533 new_client->adapter = adapter;
534 new_client->driver = &sis5595_driver;
535 new_client->flags = 0;
536
537 /* Check revision and pin registers to determine whether 4 or 5 voltages */
538 pci_read_config_byte(s_bridge, SIS5595_REVISION_REG, &(data->revision));
539 /* 4 voltages, 1 temp */
540 data->maxins = 3;
541 if (data->revision >= REV2MIN) {
542 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
543 if (!(val & 0x80))
544 /* 5 voltages, no temps */
545 data->maxins = 4;
546 }
547
548 /* Fill in the remaining client fields and put it into the global list */
549 strlcpy(new_client->name, "sis5595", I2C_NAME_SIZE);
550
551 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100552 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Tell the I2C layer a new client has arrived */
555 if ((err = i2c_attach_client(new_client)))
556 goto exit_free;
557
558 /* Initialize the SIS5595 chip */
559 sis5595_init_client(new_client);
560
561 /* A few vars need to be filled upon startup */
562 for (i = 0; i < 2; i++) {
563 data->fan_min[i] = sis5595_read_value(new_client,
564 SIS5595_REG_FAN_MIN(i));
565 }
566
567 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400568 data->class_dev = hwmon_device_register(&new_client->dev);
569 if (IS_ERR(data->class_dev)) {
570 err = PTR_ERR(data->class_dev);
571 goto exit_detach;
572 }
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 device_create_file(&new_client->dev, &dev_attr_in0_input);
575 device_create_file(&new_client->dev, &dev_attr_in0_min);
576 device_create_file(&new_client->dev, &dev_attr_in0_max);
577 device_create_file(&new_client->dev, &dev_attr_in1_input);
578 device_create_file(&new_client->dev, &dev_attr_in1_min);
579 device_create_file(&new_client->dev, &dev_attr_in1_max);
580 device_create_file(&new_client->dev, &dev_attr_in2_input);
581 device_create_file(&new_client->dev, &dev_attr_in2_min);
582 device_create_file(&new_client->dev, &dev_attr_in2_max);
583 device_create_file(&new_client->dev, &dev_attr_in3_input);
584 device_create_file(&new_client->dev, &dev_attr_in3_min);
585 device_create_file(&new_client->dev, &dev_attr_in3_max);
586 if (data->maxins == 4) {
587 device_create_file(&new_client->dev, &dev_attr_in4_input);
588 device_create_file(&new_client->dev, &dev_attr_in4_min);
589 device_create_file(&new_client->dev, &dev_attr_in4_max);
590 }
591 device_create_file(&new_client->dev, &dev_attr_fan1_input);
592 device_create_file(&new_client->dev, &dev_attr_fan1_min);
593 device_create_file(&new_client->dev, &dev_attr_fan1_div);
594 device_create_file(&new_client->dev, &dev_attr_fan2_input);
595 device_create_file(&new_client->dev, &dev_attr_fan2_min);
596 device_create_file(&new_client->dev, &dev_attr_fan2_div);
597 device_create_file(&new_client->dev, &dev_attr_alarms);
598 if (data->maxins == 3) {
599 device_create_file(&new_client->dev, &dev_attr_temp1_input);
600 device_create_file(&new_client->dev, &dev_attr_temp1_max);
601 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
602 }
603 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400604
605exit_detach:
606 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607exit_free:
608 kfree(data);
609exit_release:
610 release_region(address, SIS5595_EXTENT);
611exit:
612 return err;
613}
614
615static int sis5595_detach_client(struct i2c_client *client)
616{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400617 struct sis5595_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int err;
619
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400620 hwmon_device_unregister(data->class_dev);
621
Jean Delvare7bef5592005-07-27 22:14:49 +0200622 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jean Delvare2d8672c2005-07-19 23:56:35 +0200625 release_region(client->addr, SIS5595_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400627 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return 0;
630}
631
632
633/* ISA access must be locked explicitly. */
634static int sis5595_read_value(struct i2c_client *client, u8 reg)
635{
636 int res;
637
638 struct sis5595_data *data = i2c_get_clientdata(client);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100639 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
641 res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100642 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return res;
644}
645
646static int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
647{
648 struct sis5595_data *data = i2c_get_clientdata(client);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100649 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
651 outb_p(value, client->addr + SIS5595_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100652 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
655
656/* Called when we have found a new SIS5595. */
657static void sis5595_init_client(struct i2c_client *client)
658{
659 u8 config = sis5595_read_value(client, SIS5595_REG_CONFIG);
660 if (!(config & 0x01))
661 sis5595_write_value(client, SIS5595_REG_CONFIG,
662 (config & 0xf7) | 0x01);
663}
664
665static struct sis5595_data *sis5595_update_device(struct device *dev)
666{
667 struct i2c_client *client = to_i2c_client(dev);
668 struct sis5595_data *data = i2c_get_clientdata(client);
669 int i;
670
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
674 || !data->valid) {
675
676 for (i = 0; i <= data->maxins; i++) {
677 data->in[i] =
678 sis5595_read_value(client, SIS5595_REG_IN(i));
679 data->in_min[i] =
680 sis5595_read_value(client,
681 SIS5595_REG_IN_MIN(i));
682 data->in_max[i] =
683 sis5595_read_value(client,
684 SIS5595_REG_IN_MAX(i));
685 }
686 for (i = 0; i < 2; i++) {
687 data->fan[i] =
688 sis5595_read_value(client, SIS5595_REG_FAN(i));
689 data->fan_min[i] =
690 sis5595_read_value(client,
691 SIS5595_REG_FAN_MIN(i));
692 }
693 if (data->maxins == 3) {
694 data->temp =
695 sis5595_read_value(client, SIS5595_REG_TEMP);
696 data->temp_over =
697 sis5595_read_value(client, SIS5595_REG_TEMP_OVER);
698 data->temp_hyst =
699 sis5595_read_value(client, SIS5595_REG_TEMP_HYST);
700 }
701 i = sis5595_read_value(client, SIS5595_REG_FANDIV);
702 data->fan_div[0] = (i >> 4) & 0x03;
703 data->fan_div[1] = i >> 6;
704 data->alarms =
705 sis5595_read_value(client, SIS5595_REG_ALARM1) |
706 (sis5595_read_value(client, SIS5595_REG_ALARM2) << 8);
707 data->last_updated = jiffies;
708 data->valid = 1;
709 }
710
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100711 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return data;
714}
715
716static struct pci_device_id sis5595_pci_ids[] = {
717 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
718 { 0, }
719};
720
721MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
722
723static int blacklist[] __devinitdata = {
724 PCI_DEVICE_ID_SI_540,
725 PCI_DEVICE_ID_SI_550,
726 PCI_DEVICE_ID_SI_630,
727 PCI_DEVICE_ID_SI_645,
728 PCI_DEVICE_ID_SI_730,
729 PCI_DEVICE_ID_SI_735,
730 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but
731 that ID shows up in other chips so we
732 use the 5511 ID for recognition */
733 PCI_DEVICE_ID_SI_5597,
734 PCI_DEVICE_ID_SI_5598,
735 0 };
736
737static int __devinit sis5595_pci_probe(struct pci_dev *dev,
738 const struct pci_device_id *id)
739{
740 u16 val;
741 int *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 for (i = blacklist; *i != 0; i++) {
744 struct pci_dev *dev;
745 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
746 if (dev) {
747 dev_err(&dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
748 pci_dev_put(dev);
749 return -ENODEV;
750 }
751 }
752
753 if (PCIBIOS_SUCCESSFUL !=
754 pci_read_config_word(dev, SIS5595_BASE_REG, &val))
755 return -ENODEV;
756
Jean Delvare2d8672c2005-07-19 23:56:35 +0200757 address = val & ~(SIS5595_EXTENT - 1);
758 if (address == 0 && force_addr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dev_err(&dev->dev, "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
760 return -ENODEV;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 s_bridge = pci_dev_get(dev);
Jean Delvarefde09502005-07-19 23:51:07 +0200764 if (i2c_isa_add_driver(&sis5595_driver)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 pci_dev_put(s_bridge);
766 s_bridge = NULL;
767 }
768
769 /* Always return failure here. This is to allow other drivers to bind
770 * to this pci device. We don't really want to have control over the
771 * pci device, we only wanted to read as few register values from it.
772 */
773 return -ENODEV;
774}
775
776static struct pci_driver sis5595_pci_driver = {
777 .name = "sis5595",
778 .id_table = sis5595_pci_ids,
779 .probe = sis5595_pci_probe,
780};
781
782static int __init sm_sis5595_init(void)
783{
784 return pci_register_driver(&sis5595_pci_driver);
785}
786
787static void __exit sm_sis5595_exit(void)
788{
789 pci_unregister_driver(&sis5595_pci_driver);
790 if (s_bridge != NULL) {
Jean Delvarefde09502005-07-19 23:51:07 +0200791 i2c_isa_del_driver(&sis5595_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 pci_dev_put(s_bridge);
793 s_bridge = NULL;
794 }
795}
796
797MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
798MODULE_DESCRIPTION("SiS 5595 Sensor device");
799MODULE_LICENSE("GPL");
800
801module_init(sm_sis5595_init);
802module_exit(sm_sis5595_exit);