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