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