blob: 8be5189d9bd9edda783f4f44f038702ad881a461 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/io.h>
64
65
66/* If force_addr is set to anything different from 0, we forcibly enable
67 the device at the given address. */
68static u16 force_addr;
69module_param(force_addr, ushort, 0);
70MODULE_PARM_DESC(force_addr,
71 "Initialize the base address of the sensors");
72
Jean Delvare2d8672c2005-07-19 23:56:35 +020073/* Device address
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 Note that we can't determine the ISA address until we have initialized
75 our module */
Jean Delvare2d8672c2005-07-19 23:56:35 +020076static unsigned short address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/* Many SIS5595 constants specified below */
79
80/* Length of ISA address segment */
81#define SIS5595_EXTENT 8
82/* PCI Config Registers */
83#define SIS5595_REVISION_REG 0x08
84#define SIS5595_BASE_REG 0x68
85#define SIS5595_PIN_REG 0x7A
86#define SIS5595_ENABLE_REG 0x7B
87
88/* Where are the ISA address/data registers relative to the base address */
89#define SIS5595_ADDR_REG_OFFSET 5
90#define SIS5595_DATA_REG_OFFSET 6
91
92/* The SIS5595 registers */
93#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
94#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
95#define SIS5595_REG_IN(nr) (0x20 + (nr))
96
97#define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
98#define SIS5595_REG_FAN(nr) (0x28 + (nr))
99
100/* On the first version of the chip, the temp registers are separate.
101 On the second version,
102 TEMP pin is shared with IN4, configured in PCI register 0x7A.
103 The registers are the same as well.
104 OVER and HYST are really MAX and MIN. */
105
106#define REV2MIN 0xb0
107#define SIS5595_REG_TEMP (( data->revision) >= REV2MIN) ? \
108 SIS5595_REG_IN(4) : 0x27
109#define SIS5595_REG_TEMP_OVER (( data->revision) >= REV2MIN) ? \
110 SIS5595_REG_IN_MAX(4) : 0x39
111#define SIS5595_REG_TEMP_HYST (( data->revision) >= REV2MIN) ? \
112 SIS5595_REG_IN_MIN(4) : 0x3a
113
114#define SIS5595_REG_CONFIG 0x40
115#define SIS5595_REG_ALARM1 0x41
116#define SIS5595_REG_ALARM2 0x42
117#define SIS5595_REG_FANDIV 0x47
118
119/* Conversions. Limit checking is only done on the TO_REG
120 variants. */
121
122/* IN: mV, (0V to 4.08V)
123 REG: 16mV/bit */
124static inline u8 IN_TO_REG(unsigned long val)
125{
126 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
127 return (nval + 8) / 16;
128}
129#define IN_FROM_REG(val) ((val) * 16)
130
131static inline u8 FAN_TO_REG(long rpm, int div)
132{
133 if (rpm <= 0)
134 return 255;
135 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
136}
137
138static inline int FAN_FROM_REG(u8 val, int div)
139{
140 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
141}
142
143/* TEMP: mC (-54.12C to +157.53C)
144 REG: 0.83C/bit + 52.12, two's complement */
145static inline int TEMP_FROM_REG(s8 val)
146{
147 return val * 830 + 52120;
148}
149static inline s8 TEMP_TO_REG(int val)
150{
151 int nval = SENSORS_LIMIT(val, -54120, 157530) ;
152 return nval<0 ? (nval-5212-415)/830 : (nval-5212+415)/830;
153}
154
155/* FAN DIV: 1, 2, 4, or 8 (defaults to 2)
156 REG: 0, 1, 2, or 3 (respectively) (defaults to 1) */
157static inline u8 DIV_TO_REG(int val)
158{
159 return val==8 ? 3 : val==4 ? 2 : val==1 ? 0 : 1;
160}
161#define DIV_FROM_REG(val) (1 << (val))
162
163/* For the SIS5595, we need to keep some data in memory. That
164 data is pointed to by sis5595_list[NR]->data. The structure itself is
165 dynamically allocated, at the time when the new sis5595 client is
166 allocated. */
167struct sis5595_data {
168 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400169 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct semaphore lock;
171
172 struct semaphore update_lock;
173 char valid; /* !=0 if following fields are valid */
174 unsigned long last_updated; /* In jiffies */
175 char maxins; /* == 3 if temp enabled, otherwise == 4 */
176 u8 revision; /* Reg. value */
177
178 u8 in[5]; /* Register value */
179 u8 in_max[5]; /* Register value */
180 u8 in_min[5]; /* Register value */
181 u8 fan[2]; /* Register value */
182 u8 fan_min[2]; /* Register value */
183 s8 temp; /* Register value */
184 s8 temp_over; /* Register value */
185 s8 temp_hyst; /* Register value */
186 u8 fan_div[2]; /* Register encoding, shifted right */
187 u16 alarms; /* Register encoding, combined */
188};
189
190static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
191
Jean Delvare2d8672c2005-07-19 23:56:35 +0200192static int sis5595_detect(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int sis5595_detach_client(struct i2c_client *client);
194
195static int sis5595_read_value(struct i2c_client *client, u8 register);
196static int sis5595_write_value(struct i2c_client *client, u8 register, u8 value);
197static struct sis5595_data *sis5595_update_device(struct device *dev);
198static void sis5595_init_client(struct i2c_client *client);
199
200static struct i2c_driver sis5595_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100201 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100202 .name = "sis5595",
203 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200204 .attach_adapter = sis5595_detect,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .detach_client = sis5595_detach_client,
206};
207
208/* 4 Voltages */
209static ssize_t show_in(struct device *dev, char *buf, int nr)
210{
211 struct sis5595_data *data = sis5595_update_device(dev);
212 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
213}
214
215static ssize_t show_in_min(struct device *dev, char *buf, int nr)
216{
217 struct sis5595_data *data = sis5595_update_device(dev);
218 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
219}
220
221static ssize_t show_in_max(struct device *dev, char *buf, int nr)
222{
223 struct sis5595_data *data = sis5595_update_device(dev);
224 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
225}
226
227static ssize_t set_in_min(struct device *dev, const char *buf,
228 size_t count, int nr)
229{
230 struct i2c_client *client = to_i2c_client(dev);
231 struct sis5595_data *data = i2c_get_clientdata(client);
232 unsigned long val = simple_strtoul(buf, NULL, 10);
233
234 down(&data->update_lock);
235 data->in_min[nr] = IN_TO_REG(val);
236 sis5595_write_value(client, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
237 up(&data->update_lock);
238 return count;
239}
240
241static ssize_t set_in_max(struct device *dev, const char *buf,
242 size_t count, int nr)
243{
244 struct i2c_client *client = to_i2c_client(dev);
245 struct sis5595_data *data = i2c_get_clientdata(client);
246 unsigned long val = simple_strtoul(buf, NULL, 10);
247
248 down(&data->update_lock);
249 data->in_max[nr] = IN_TO_REG(val);
250 sis5595_write_value(client, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
251 up(&data->update_lock);
252 return count;
253}
254
255#define show_in_offset(offset) \
256static ssize_t \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400257 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{ \
259 return show_in(dev, buf, offset); \
260} \
261static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
262 show_in##offset, NULL); \
263static ssize_t \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400264 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{ \
266 return show_in_min(dev, buf, offset); \
267} \
268static ssize_t \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400269 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{ \
271 return show_in_max(dev, buf, offset); \
272} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400273static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 const char *buf, size_t count) \
275{ \
276 return set_in_min(dev, buf, count, offset); \
277} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400278static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 const char *buf, size_t count) \
280{ \
281 return set_in_max(dev, buf, count, offset); \
282} \
283static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
284 show_in##offset##_min, set_in##offset##_min); \
285static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
286 show_in##offset##_max, set_in##offset##_max);
287
288show_in_offset(0);
289show_in_offset(1);
290show_in_offset(2);
291show_in_offset(3);
292show_in_offset(4);
293
294/* Temperature */
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400295static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct sis5595_data *data = sis5595_update_device(dev);
298 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
299}
300
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400301static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct sis5595_data *data = sis5595_update_device(dev);
304 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
305}
306
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400307static 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 -0700308{
309 struct i2c_client *client = to_i2c_client(dev);
310 struct sis5595_data *data = i2c_get_clientdata(client);
311 long val = simple_strtol(buf, NULL, 10);
312
313 down(&data->update_lock);
314 data->temp_over = TEMP_TO_REG(val);
315 sis5595_write_value(client, SIS5595_REG_TEMP_OVER, data->temp_over);
316 up(&data->update_lock);
317 return count;
318}
319
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400320static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct sis5595_data *data = sis5595_update_device(dev);
323 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
324}
325
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400326static 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 -0700327{
328 struct i2c_client *client = to_i2c_client(dev);
329 struct sis5595_data *data = i2c_get_clientdata(client);
330 long val = simple_strtol(buf, NULL, 10);
331
332 down(&data->update_lock);
333 data->temp_hyst = TEMP_TO_REG(val);
334 sis5595_write_value(client, SIS5595_REG_TEMP_HYST, data->temp_hyst);
335 up(&data->update_lock);
336 return count;
337}
338
339static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
340static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
341 show_temp_over, set_temp_over);
342static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
343 show_temp_hyst, set_temp_hyst);
344
345/* 2 Fans */
346static ssize_t show_fan(struct device *dev, char *buf, int nr)
347{
348 struct sis5595_data *data = sis5595_update_device(dev);
349 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
350 DIV_FROM_REG(data->fan_div[nr])) );
351}
352
353static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
354{
355 struct sis5595_data *data = sis5595_update_device(dev);
356 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
357 DIV_FROM_REG(data->fan_div[nr])) );
358}
359
360static ssize_t set_fan_min(struct device *dev, const char *buf,
361 size_t count, int nr)
362{
363 struct i2c_client *client = to_i2c_client(dev);
364 struct sis5595_data *data = i2c_get_clientdata(client);
365 unsigned long val = simple_strtoul(buf, NULL, 10);
366
367 down(&data->update_lock);
368 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
369 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
370 up(&data->update_lock);
371 return count;
372}
373
374static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
375{
376 struct sis5595_data *data = sis5595_update_device(dev);
377 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
378}
379
380/* Note: we save and restore the fan minimum here, because its value is
381 determined in part by the fan divisor. This follows the principle of
382 least suprise; the user doesn't expect the fan minimum to change just
383 because the divisor changed. */
384static ssize_t set_fan_div(struct device *dev, const char *buf,
385 size_t count, int nr)
386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct sis5595_data *data = i2c_get_clientdata(client);
389 unsigned long min;
390 unsigned long val = simple_strtoul(buf, NULL, 10);
391 int reg;
392
393 down(&data->update_lock);
394 min = FAN_FROM_REG(data->fan_min[nr],
395 DIV_FROM_REG(data->fan_div[nr]));
396 reg = sis5595_read_value(client, SIS5595_REG_FANDIV);
397
398 switch (val) {
399 case 1: data->fan_div[nr] = 0; break;
400 case 2: data->fan_div[nr] = 1; break;
401 case 4: data->fan_div[nr] = 2; break;
402 case 8: data->fan_div[nr] = 3; break;
403 default:
404 dev_err(&client->dev, "fan_div value %ld not "
405 "supported. Choose one of 1, 2, 4 or 8!\n", val);
406 up(&data->update_lock);
407 return -EINVAL;
408 }
409
410 switch (nr) {
411 case 0:
412 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
413 break;
414 case 1:
415 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
416 break;
417 }
418 sis5595_write_value(client, SIS5595_REG_FANDIV, reg);
419 data->fan_min[nr] =
420 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
421 sis5595_write_value(client, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
422 up(&data->update_lock);
423 return count;
424}
425
426#define show_fan_offset(offset) \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400427static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{ \
429 return show_fan(dev, buf, offset - 1); \
430} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400431static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{ \
433 return show_fan_min(dev, buf, offset - 1); \
434} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400435static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{ \
437 return show_fan_div(dev, buf, offset - 1); \
438} \
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400439static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 const char *buf, size_t count) \
441{ \
442 return set_fan_min(dev, buf, count, offset - 1); \
443} \
444static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
445static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
446 show_fan_##offset##_min, set_fan_##offset##_min);
447
448show_fan_offset(1);
449show_fan_offset(2);
450
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400451static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 size_t count)
453{
454 return set_fan_div(dev, buf, count, 0) ;
455}
456
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400457static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 size_t count)
459{
460 return set_fan_div(dev, buf, count, 1) ;
461}
462static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
463 show_fan_1_div, set_fan_1_div);
464static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
465 show_fan_2_div, set_fan_2_div);
466
467/* Alarms */
Yani Ioannoua5099cf2005-05-17 06:42:25 -0400468static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct sis5595_data *data = sis5595_update_device(dev);
471 return sprintf(buf, "%d\n", data->alarms);
472}
473static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
474
475/* This is called when the module is loaded */
Jean Delvare2d8672c2005-07-19 23:56:35 +0200476static int sis5595_detect(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 int err = 0;
479 int i;
480 struct i2c_client *new_client;
481 struct sis5595_data *data;
482 char val;
483 u16 a;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (force_addr)
486 address = force_addr & ~(SIS5595_EXTENT - 1);
487 /* Reserve the ISA region */
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100488 if (!request_region(address, SIS5595_EXTENT,
489 sis5595_driver.driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 err = -EBUSY;
491 goto exit;
492 }
493 if (force_addr) {
494 dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n", address);
495 if (PCIBIOS_SUCCESSFUL !=
496 pci_write_config_word(s_bridge, SIS5595_BASE_REG, address))
497 goto exit_release;
498 if (PCIBIOS_SUCCESSFUL !=
499 pci_read_config_word(s_bridge, SIS5595_BASE_REG, &a))
500 goto exit_release;
501 if ((a & ~(SIS5595_EXTENT - 1)) != address)
502 /* doesn't work for some chips? */
503 goto exit_release;
504 }
505
506 if (PCIBIOS_SUCCESSFUL !=
507 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val)) {
508 goto exit_release;
509 }
510 if ((val & 0x80) == 0) {
511 if (PCIBIOS_SUCCESSFUL !=
512 pci_write_config_byte(s_bridge, SIS5595_ENABLE_REG,
513 val | 0x80))
514 goto exit_release;
515 if (PCIBIOS_SUCCESSFUL !=
516 pci_read_config_byte(s_bridge, SIS5595_ENABLE_REG, &val))
517 goto exit_release;
518 if ((val & 0x80) == 0)
519 /* doesn't work for some chips! */
520 goto exit_release;
521 }
522
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200523 if (!(data = kzalloc(sizeof(struct sis5595_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 err = -ENOMEM;
525 goto exit_release;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 new_client = &data->client;
529 new_client->addr = address;
530 init_MUTEX(&data->lock);
531 i2c_set_clientdata(new_client, data);
532 new_client->adapter = adapter;
533 new_client->driver = &sis5595_driver;
534 new_client->flags = 0;
535
536 /* Check revision and pin registers to determine whether 4 or 5 voltages */
537 pci_read_config_byte(s_bridge, SIS5595_REVISION_REG, &(data->revision));
538 /* 4 voltages, 1 temp */
539 data->maxins = 3;
540 if (data->revision >= REV2MIN) {
541 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
542 if (!(val & 0x80))
543 /* 5 voltages, no temps */
544 data->maxins = 4;
545 }
546
547 /* Fill in the remaining client fields and put it into the global list */
548 strlcpy(new_client->name, "sis5595", I2C_NAME_SIZE);
549
550 data->valid = 0;
551 init_MUTEX(&data->update_lock);
552
553 /* Tell the I2C layer a new client has arrived */
554 if ((err = i2c_attach_client(new_client)))
555 goto exit_free;
556
557 /* Initialize the SIS5595 chip */
558 sis5595_init_client(new_client);
559
560 /* A few vars need to be filled upon startup */
561 for (i = 0; i < 2; i++) {
562 data->fan_min[i] = sis5595_read_value(new_client,
563 SIS5595_REG_FAN_MIN(i));
564 }
565
566 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400567 data->class_dev = hwmon_device_register(&new_client->dev);
568 if (IS_ERR(data->class_dev)) {
569 err = PTR_ERR(data->class_dev);
570 goto exit_detach;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 device_create_file(&new_client->dev, &dev_attr_in0_input);
574 device_create_file(&new_client->dev, &dev_attr_in0_min);
575 device_create_file(&new_client->dev, &dev_attr_in0_max);
576 device_create_file(&new_client->dev, &dev_attr_in1_input);
577 device_create_file(&new_client->dev, &dev_attr_in1_min);
578 device_create_file(&new_client->dev, &dev_attr_in1_max);
579 device_create_file(&new_client->dev, &dev_attr_in2_input);
580 device_create_file(&new_client->dev, &dev_attr_in2_min);
581 device_create_file(&new_client->dev, &dev_attr_in2_max);
582 device_create_file(&new_client->dev, &dev_attr_in3_input);
583 device_create_file(&new_client->dev, &dev_attr_in3_min);
584 device_create_file(&new_client->dev, &dev_attr_in3_max);
585 if (data->maxins == 4) {
586 device_create_file(&new_client->dev, &dev_attr_in4_input);
587 device_create_file(&new_client->dev, &dev_attr_in4_min);
588 device_create_file(&new_client->dev, &dev_attr_in4_max);
589 }
590 device_create_file(&new_client->dev, &dev_attr_fan1_input);
591 device_create_file(&new_client->dev, &dev_attr_fan1_min);
592 device_create_file(&new_client->dev, &dev_attr_fan1_div);
593 device_create_file(&new_client->dev, &dev_attr_fan2_input);
594 device_create_file(&new_client->dev, &dev_attr_fan2_min);
595 device_create_file(&new_client->dev, &dev_attr_fan2_div);
596 device_create_file(&new_client->dev, &dev_attr_alarms);
597 if (data->maxins == 3) {
598 device_create_file(&new_client->dev, &dev_attr_temp1_input);
599 device_create_file(&new_client->dev, &dev_attr_temp1_max);
600 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
601 }
602 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400603
604exit_detach:
605 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606exit_free:
607 kfree(data);
608exit_release:
609 release_region(address, SIS5595_EXTENT);
610exit:
611 return err;
612}
613
614static int sis5595_detach_client(struct i2c_client *client)
615{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400616 struct sis5595_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int err;
618
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400619 hwmon_device_unregister(data->class_dev);
620
Jean Delvare7bef5592005-07-27 22:14:49 +0200621 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jean Delvare2d8672c2005-07-19 23:56:35 +0200624 release_region(client->addr, SIS5595_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400626 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 return 0;
629}
630
631
632/* ISA access must be locked explicitly. */
633static int sis5595_read_value(struct i2c_client *client, u8 reg)
634{
635 int res;
636
637 struct sis5595_data *data = i2c_get_clientdata(client);
638 down(&data->lock);
639 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
640 res = inb_p(client->addr + SIS5595_DATA_REG_OFFSET);
641 up(&data->lock);
642 return res;
643}
644
645static int sis5595_write_value(struct i2c_client *client, u8 reg, u8 value)
646{
647 struct sis5595_data *data = i2c_get_clientdata(client);
648 down(&data->lock);
649 outb_p(reg, client->addr + SIS5595_ADDR_REG_OFFSET);
650 outb_p(value, client->addr + SIS5595_DATA_REG_OFFSET);
651 up(&data->lock);
652 return 0;
653}
654
655/* Called when we have found a new SIS5595. */
656static void sis5595_init_client(struct i2c_client *client)
657{
658 u8 config = sis5595_read_value(client, SIS5595_REG_CONFIG);
659 if (!(config & 0x01))
660 sis5595_write_value(client, SIS5595_REG_CONFIG,
661 (config & 0xf7) | 0x01);
662}
663
664static struct sis5595_data *sis5595_update_device(struct device *dev)
665{
666 struct i2c_client *client = to_i2c_client(dev);
667 struct sis5595_data *data = i2c_get_clientdata(client);
668 int i;
669
670 down(&data->update_lock);
671
672 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
673 || !data->valid) {
674
675 for (i = 0; i <= data->maxins; i++) {
676 data->in[i] =
677 sis5595_read_value(client, SIS5595_REG_IN(i));
678 data->in_min[i] =
679 sis5595_read_value(client,
680 SIS5595_REG_IN_MIN(i));
681 data->in_max[i] =
682 sis5595_read_value(client,
683 SIS5595_REG_IN_MAX(i));
684 }
685 for (i = 0; i < 2; i++) {
686 data->fan[i] =
687 sis5595_read_value(client, SIS5595_REG_FAN(i));
688 data->fan_min[i] =
689 sis5595_read_value(client,
690 SIS5595_REG_FAN_MIN(i));
691 }
692 if (data->maxins == 3) {
693 data->temp =
694 sis5595_read_value(client, SIS5595_REG_TEMP);
695 data->temp_over =
696 sis5595_read_value(client, SIS5595_REG_TEMP_OVER);
697 data->temp_hyst =
698 sis5595_read_value(client, SIS5595_REG_TEMP_HYST);
699 }
700 i = sis5595_read_value(client, SIS5595_REG_FANDIV);
701 data->fan_div[0] = (i >> 4) & 0x03;
702 data->fan_div[1] = i >> 6;
703 data->alarms =
704 sis5595_read_value(client, SIS5595_REG_ALARM1) |
705 (sis5595_read_value(client, SIS5595_REG_ALARM2) << 8);
706 data->last_updated = jiffies;
707 data->valid = 1;
708 }
709
710 up(&data->update_lock);
711
712 return data;
713}
714
715static struct pci_device_id sis5595_pci_ids[] = {
716 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
717 { 0, }
718};
719
720MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
721
722static int blacklist[] __devinitdata = {
723 PCI_DEVICE_ID_SI_540,
724 PCI_DEVICE_ID_SI_550,
725 PCI_DEVICE_ID_SI_630,
726 PCI_DEVICE_ID_SI_645,
727 PCI_DEVICE_ID_SI_730,
728 PCI_DEVICE_ID_SI_735,
729 PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but
730 that ID shows up in other chips so we
731 use the 5511 ID for recognition */
732 PCI_DEVICE_ID_SI_5597,
733 PCI_DEVICE_ID_SI_5598,
734 0 };
735
736static int __devinit sis5595_pci_probe(struct pci_dev *dev,
737 const struct pci_device_id *id)
738{
739 u16 val;
740 int *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 for (i = blacklist; *i != 0; i++) {
743 struct pci_dev *dev;
744 dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
745 if (dev) {
746 dev_err(&dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
747 pci_dev_put(dev);
748 return -ENODEV;
749 }
750 }
751
752 if (PCIBIOS_SUCCESSFUL !=
753 pci_read_config_word(dev, SIS5595_BASE_REG, &val))
754 return -ENODEV;
755
Jean Delvare2d8672c2005-07-19 23:56:35 +0200756 address = val & ~(SIS5595_EXTENT - 1);
757 if (address == 0 && force_addr == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 dev_err(&dev->dev, "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
759 return -ENODEV;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 s_bridge = pci_dev_get(dev);
Jean Delvarefde09502005-07-19 23:51:07 +0200763 if (i2c_isa_add_driver(&sis5595_driver)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 pci_dev_put(s_bridge);
765 s_bridge = NULL;
766 }
767
768 /* Always return failure here. This is to allow other drivers to bind
769 * to this pci device. We don't really want to have control over the
770 * pci device, we only wanted to read as few register values from it.
771 */
772 return -ENODEV;
773}
774
775static struct pci_driver sis5595_pci_driver = {
776 .name = "sis5595",
777 .id_table = sis5595_pci_ids,
778 .probe = sis5595_pci_probe,
779};
780
781static int __init sm_sis5595_init(void)
782{
783 return pci_register_driver(&sis5595_pci_driver);
784}
785
786static void __exit sm_sis5595_exit(void)
787{
788 pci_unregister_driver(&sis5595_pci_driver);
789 if (s_bridge != NULL) {
Jean Delvarefde09502005-07-19 23:51:07 +0200790 i2c_isa_del_driver(&sis5595_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 pci_dev_put(s_bridge);
792 s_bridge = NULL;
793 }
794}
795
796MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
797MODULE_DESCRIPTION("SiS 5595 Sensor device");
798MODULE_LICENSE("GPL");
799
800module_init(sm_sis5595_init);
801module_exit(sm_sis5595_exit);