e2f3c5a82153636f502bc007de5a0023a32b7501
[sfrench/cifs-2.6.git] / drivers / hwmon / f71882fg.c
1 /***************************************************************************
2  *   Copyright (C) 2006 by Hans Edgington <hans@edgington.nl>              *
3  *   Copyright (C) 2007 by Hans de Goede  <j.w.r.degoede@hhs.nl>           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/platform_device.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <asm/io.h>
31
32 #define DRVNAME "f71882fg"
33
34 #define SIO_F71882FG_LD_HWM     0x04    /* Hardware monitor logical device*/
35 #define SIO_UNLOCK_KEY          0x87    /* Key to enable Super-I/O */
36 #define SIO_LOCK_KEY            0xAA    /* Key to diasble Super-I/O */
37
38 #define SIO_REG_LDSEL           0x07    /* Logical device select */
39 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
40 #define SIO_REG_DEVREV          0x22    /* Device revision */
41 #define SIO_REG_MANID           0x23    /* Fintek ID (2 bytes) */
42 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
43 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
44
45 #define SIO_FINTEK_ID           0x1934  /* Manufacturers ID */
46 #define SIO_F71882_ID           0x0541  /* Chipset ID */
47
48 #define REGION_LENGTH           8
49 #define ADDR_REG_OFFSET         5
50 #define DATA_REG_OFFSET         6
51
52 #define F71882FG_REG_PECI               0x0A
53
54 #define F71882FG_REG_IN_STATUS          0x12
55 #define F71882FG_REG_IN_BEEP            0x13
56 #define F71882FG_REG_IN(nr)             (0x20  + (nr))
57 #define F71882FG_REG_IN1_HIGH           0x32
58
59 #define F71882FG_REG_FAN(nr)            (0xA0 + (16 * (nr)))
60 #define F71882FG_REG_FAN_STATUS         0x92
61 #define F71882FG_REG_FAN_BEEP           0x93
62
63 #define F71882FG_REG_TEMP(nr)           (0x72 + 2 * (nr))
64 #define F71882FG_REG_TEMP_OVT(nr)       (0x82 + 2 * (nr))
65 #define F71882FG_REG_TEMP_HIGH(nr)      (0x83 + 2 * (nr))
66 #define F71882FG_REG_TEMP_STATUS        0x62
67 #define F71882FG_REG_TEMP_BEEP          0x63
68 #define F71882FG_REG_TEMP_HYST1         0x6C
69 #define F71882FG_REG_TEMP_HYST23        0x6D
70 #define F71882FG_REG_TEMP_TYPE          0x6B
71 #define F71882FG_REG_TEMP_DIODE_OPEN    0x6F
72
73 #define F71882FG_REG_START              0x01
74
75 #define FAN_MIN_DETECT                  366 /* Lowest detectable fanspeed */
76
77 static unsigned short force_id;
78 module_param(force_id, ushort, 0);
79 MODULE_PARM_DESC(force_id, "Override the detected device ID");
80
81 static struct platform_device *f71882fg_pdev = NULL;
82
83 /* Super-I/O Function prototypes */
84 static inline int superio_inb(int base, int reg);
85 static inline int superio_inw(int base, int reg);
86 static inline void superio_enter(int base);
87 static inline void superio_select(int base, int ld);
88 static inline void superio_exit(int base);
89
90 struct f71882fg_data {
91         unsigned short addr;
92         struct device *hwmon_dev;
93
94         struct mutex update_lock;
95         char valid;                     /* !=0 if following fields are valid */
96         unsigned long last_updated;     /* In jiffies */
97         unsigned long last_limits;      /* In jiffies */
98
99         /* Register Values */
100         u8      in[9];
101         u8      in1_max;
102         u8      in_status;
103         u8      in_beep;
104         u16     fan[4];
105         u8      fan_status;
106         u8      fan_beep;
107         u8      temp[3];
108         u8      temp_ovt[3];
109         u8      temp_high[3];
110         u8      temp_hyst[3];
111         u8      temp_type[3];
112         u8      temp_status;
113         u8      temp_beep;
114         u8      temp_diode_open;
115 };
116
117 /* Sysfs in*/
118 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
119         char *buf);
120 static ssize_t show_in_max(struct device *dev, struct device_attribute
121         *devattr, char *buf);
122 static ssize_t store_in_max(struct device *dev, struct device_attribute
123         *devattr, const char *buf, size_t count);
124 static ssize_t show_in_beep(struct device *dev, struct device_attribute
125         *devattr, char *buf);
126 static ssize_t store_in_beep(struct device *dev, struct device_attribute
127         *devattr, const char *buf, size_t count);
128 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
129         *devattr, char *buf);
130 /* Sysfs Fan */
131 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
132         char *buf);
133 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
134         *devattr, char *buf);
135 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
136         *devattr, const char *buf, size_t count);
137 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
138         *devattr, char *buf);
139 /* Sysfs Temp */
140 static ssize_t show_temp(struct device *dev, struct device_attribute
141         *devattr, char *buf);
142 static ssize_t show_temp_max(struct device *dev, struct device_attribute
143         *devattr, char *buf);
144 static ssize_t store_temp_max(struct device *dev, struct device_attribute
145         *devattr, const char *buf, size_t count);
146 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
147         *devattr, char *buf);
148 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
149         *devattr, const char *buf, size_t count);
150 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
151         *devattr, char *buf);
152 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
153         *devattr, const char *buf, size_t count);
154 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
155         *devattr, char *buf);
156 static ssize_t show_temp_type(struct device *dev, struct device_attribute
157         *devattr, char *buf);
158 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
159         *devattr, char *buf);
160 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
161         *devattr, const char *buf, size_t count);
162 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
163         *devattr, char *buf);
164 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
165         *devattr, char *buf);
166 /* Sysfs misc */
167 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
168         char *buf);
169
170 static int __devinit f71882fg_probe(struct platform_device * pdev);
171 static int __devexit f71882fg_remove(struct platform_device *pdev);
172 static int __init f71882fg_init(void);
173 static int __init f71882fg_find(int sioaddr, unsigned short *address);
174 static int __init f71882fg_device_add(unsigned short address);
175 static void __exit f71882fg_exit(void);
176
177 static struct platform_driver f71882fg_driver = {
178         .driver = {
179                 .owner  = THIS_MODULE,
180                 .name   = DRVNAME,
181         },
182         .probe          = f71882fg_probe,
183         .remove         = __devexit_p(f71882fg_remove),
184 };
185
186 static struct device_attribute f71882fg_dev_attr[] =
187 {
188         __ATTR( name, S_IRUGO, show_name, NULL ),
189 };
190
191 static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
192         SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
193         SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
194         SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
195                 0, 1),
196         SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
197                 0, 1),
198         SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
199         SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
200         SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
201         SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
202         SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
203         SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
204         SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
205         SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
206         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
207         SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
208                 store_temp_max, 0, 0),
209         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
210                 store_temp_max_hyst, 0, 0),
211         SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
212                 store_temp_crit, 0, 0),
213         SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
214                 0, 0),
215         SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 0),
216         SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
217                 store_temp_beep, 0, 0),
218         SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
219         SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
220         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
221         SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
222                 store_temp_max, 0, 1),
223         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
224                 store_temp_max_hyst, 0, 1),
225         SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
226                 store_temp_crit, 0, 1),
227         SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
228                 0, 1),
229         SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 1),
230         SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
231                 store_temp_beep, 0, 1),
232         SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
233         SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
234         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
235         SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
236                 store_temp_max, 0, 2),
237         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
238                 store_temp_max_hyst, 0, 2),
239         SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
240                 store_temp_crit, 0, 2),
241         SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
242                 0, 2),
243         SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 2),
244         SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
245                 store_temp_beep, 0, 2),
246         SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
247         SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
248 };
249
250 static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
251         SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
252         SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
253                 store_fan_beep, 0, 0),
254         SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
255         SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
256         SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
257                 store_fan_beep, 0, 1),
258         SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
259         SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
260         SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
261                 store_fan_beep, 0, 2),
262         SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
263         SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
264         SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
265                 store_fan_beep, 0, 3),
266         SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
267 };
268
269
270 /* Super I/O functions */
271 static inline int superio_inb(int base, int reg)
272 {
273         outb(reg, base);
274         return inb(base + 1);
275 }
276
277 static int superio_inw(int base, int reg)
278 {
279         int val;
280         outb(reg++, base);
281         val = inb(base + 1) << 8;
282         outb(reg, base);
283         val |= inb(base + 1);
284         return val;
285 }
286
287 static inline void superio_enter(int base)
288 {
289         /* according to the datasheet the key must be send twice! */
290         outb( SIO_UNLOCK_KEY, base);
291         outb( SIO_UNLOCK_KEY, base);
292 }
293
294 static inline void superio_select( int base, int ld)
295 {
296         outb(SIO_REG_LDSEL, base);
297         outb(ld, base + 1);
298 }
299
300 static inline void superio_exit(int base)
301 {
302         outb(SIO_LOCK_KEY, base);
303 }
304
305 static inline u16 fan_from_reg(u16 reg)
306 {
307         return reg ? (1500000 / reg) : 0;
308 }
309
310 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
311 {
312         u8 val;
313
314         outb(reg, data->addr + ADDR_REG_OFFSET);
315         val = inb(data->addr + DATA_REG_OFFSET);
316
317         return val;
318 }
319
320 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
321 {
322         u16 val;
323
324         outb(reg++, data->addr + ADDR_REG_OFFSET);
325         val = inb(data->addr + DATA_REG_OFFSET) << 8;
326         outb(reg, data->addr + ADDR_REG_OFFSET);
327         val |= inb(data->addr + DATA_REG_OFFSET);
328
329         return val;
330 }
331
332 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
333 {
334         outb(reg, data->addr + ADDR_REG_OFFSET);
335         outb(val, data->addr + DATA_REG_OFFSET);
336 }
337
338 static struct f71882fg_data *f71882fg_update_device(struct device * dev)
339 {
340         struct f71882fg_data *data = dev_get_drvdata(dev);
341         int nr, reg, reg2;
342
343         mutex_lock(&data->update_lock);
344
345         /* Update once every 60 seconds */
346         if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
347                         !data->valid) {
348                 data->in1_max = f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
349                 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
350
351                 /* Get High & boundary temps*/
352                 for (nr = 0; nr < 3; nr++) {
353                         data->temp_ovt[nr] = f71882fg_read8(data,
354                                                 F71882FG_REG_TEMP_OVT(nr));
355                         data->temp_high[nr] = f71882fg_read8(data,
356                                                 F71882FG_REG_TEMP_HIGH(nr));
357                 }
358
359                 /* Have to hardcode hyst*/
360                 data->temp_hyst[0] = f71882fg_read8(data,
361                                                 F71882FG_REG_TEMP_HYST1) >> 4;
362                 /* Hyst temps 2 & 3 stored in same register */
363                 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
364                 data->temp_hyst[1] = reg & 0x0F;
365                 data->temp_hyst[2] = reg >> 4;
366
367                 /* Have to hardcode type, because temp1 is special */
368                 reg  = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
369                 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
370                 if ((reg2 & 0x03) == 0x01)
371                         data->temp_type[0] = 6 /* PECI */;
372                 else if ((reg2 & 0x03) == 0x02)
373                         data->temp_type[0] = 5 /* AMDSI */;
374                 else
375                         data->temp_type[0] = (reg & 0x02) ? 2 : 4;
376
377                 data->temp_type[1] = (reg & 0x04) ? 2 : 4;
378                 data->temp_type[2] = (reg & 0x08) ? 2 : 4;
379
380                 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
381
382                 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
383
384                 data->last_limits = jiffies;
385         }
386
387         /* Update every second */
388         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
389                 data->temp_status = f71882fg_read8(data,
390                                                 F71882FG_REG_TEMP_STATUS);
391                 data->temp_diode_open = f71882fg_read8(data,
392                                                 F71882FG_REG_TEMP_DIODE_OPEN);
393                 for (nr = 0; nr < 3; nr++)
394                         data->temp[nr] = f71882fg_read8(data,
395                                                 F71882FG_REG_TEMP(nr));
396
397                 data->fan_status = f71882fg_read8(data,
398                                                 F71882FG_REG_FAN_STATUS);
399                 for (nr = 0; nr < 4; nr++)
400                         data->fan[nr] = f71882fg_read16(data,
401                                                 F71882FG_REG_FAN(nr));
402
403                 data->in_status = f71882fg_read8(data,
404                                                 F71882FG_REG_IN_STATUS);
405                 for (nr = 0; nr < 9; nr++)
406                         data->in[nr] = f71882fg_read8(data,
407                                                 F71882FG_REG_IN(nr));
408
409                 data->last_updated = jiffies;
410                 data->valid = 1;
411         }
412
413         mutex_unlock(&data->update_lock);
414
415         return data;
416 }
417
418 /* Sysfs Interface */
419 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
420         char *buf)
421 {
422         struct f71882fg_data *data = f71882fg_update_device(dev);
423         int nr = to_sensor_dev_attr_2(devattr)->index;
424         int speed = fan_from_reg(data->fan[nr]);
425
426         if (speed == FAN_MIN_DETECT)
427                 speed = 0;
428
429         return sprintf(buf, "%d\n", speed);
430 }
431
432 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
433         *devattr, char *buf)
434 {
435         struct f71882fg_data *data = f71882fg_update_device(dev);
436         int nr = to_sensor_dev_attr_2(devattr)->index;
437
438         if (data->fan_beep & (1 << nr))
439                 return sprintf(buf, "1\n");
440         else
441                 return sprintf(buf, "0\n");
442 }
443
444 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
445         *devattr, const char *buf, size_t count)
446 {
447         struct f71882fg_data *data = dev_get_drvdata(dev);
448         int nr = to_sensor_dev_attr_2(devattr)->index;
449         int val = simple_strtoul(buf, NULL, 10);
450
451         mutex_lock(&data->update_lock);
452         if (val)
453                 data->fan_beep |= 1 << nr;
454         else
455                 data->fan_beep &= ~(1 << nr);
456
457         f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
458         mutex_unlock(&data->update_lock);
459
460         return count;
461 }
462
463 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
464         *devattr, char *buf)
465 {
466         struct f71882fg_data *data = f71882fg_update_device(dev);
467         int nr = to_sensor_dev_attr_2(devattr)->index;
468
469         if (data->fan_status & (1 << nr))
470                 return sprintf(buf, "1\n");
471         else
472                 return sprintf(buf, "0\n");
473 }
474
475 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
476         char *buf)
477 {
478         struct f71882fg_data *data = f71882fg_update_device(dev);
479         int nr = to_sensor_dev_attr_2(devattr)->index;
480
481         return sprintf(buf, "%d\n", data->in[nr] * 8);
482 }
483
484 static ssize_t show_in_max(struct device *dev, struct device_attribute
485         *devattr, char *buf)
486 {
487         struct f71882fg_data *data = f71882fg_update_device(dev);
488
489         return sprintf(buf, "%d\n", data->in1_max * 8);
490 }
491
492 static ssize_t store_in_max(struct device *dev, struct device_attribute
493         *devattr, const char *buf, size_t count)
494 {
495         struct f71882fg_data *data = dev_get_drvdata(dev);
496         int val = simple_strtoul(buf, NULL, 10) / 8;
497
498         if (val > 255)
499                 val = 255;
500
501         mutex_lock(&data->update_lock);
502         f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
503         data->in1_max = val;
504         mutex_unlock(&data->update_lock);
505
506         return count;
507 }
508
509 static ssize_t show_in_beep(struct device *dev, struct device_attribute
510         *devattr, char *buf)
511 {
512         struct f71882fg_data *data = f71882fg_update_device(dev);
513         int nr = to_sensor_dev_attr_2(devattr)->index;
514
515         if (data->in_beep & (1 << nr))
516                 return sprintf(buf, "1\n");
517         else
518                 return sprintf(buf, "0\n");
519 }
520
521 static ssize_t store_in_beep(struct device *dev, struct device_attribute
522         *devattr, const char *buf, size_t count)
523 {
524         struct f71882fg_data *data = dev_get_drvdata(dev);
525         int nr = to_sensor_dev_attr_2(devattr)->index;
526         int val = simple_strtoul(buf, NULL, 10);
527
528         mutex_lock(&data->update_lock);
529         if (val)
530                 data->in_beep |= 1 << nr;
531         else
532                 data->in_beep &= ~(1 << nr);
533
534         f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
535         mutex_unlock(&data->update_lock);
536
537         return count;
538 }
539
540 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
541         *devattr, char *buf)
542 {
543         struct f71882fg_data *data = f71882fg_update_device(dev);
544         int nr = to_sensor_dev_attr_2(devattr)->index;
545
546         if (data->in_status & (1 << nr))
547                 return sprintf(buf, "1\n");
548         else
549                 return sprintf(buf, "0\n");
550 }
551
552 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
553         char *buf)
554 {
555         struct f71882fg_data *data = f71882fg_update_device(dev);
556         int nr = to_sensor_dev_attr_2(devattr)->index;
557
558         return sprintf(buf, "%d\n", data->temp[nr] * 1000);
559 }
560
561 static ssize_t show_temp_max(struct device *dev, struct device_attribute
562         *devattr, char *buf)
563 {
564         struct f71882fg_data *data = f71882fg_update_device(dev);
565         int nr = to_sensor_dev_attr_2(devattr)->index;
566
567         return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
568 }
569
570 static ssize_t store_temp_max(struct device *dev, struct device_attribute
571         *devattr, const char *buf, size_t count)
572 {
573         struct f71882fg_data *data = dev_get_drvdata(dev);
574         int nr = to_sensor_dev_attr_2(devattr)->index;
575         int val = simple_strtoul(buf, NULL, 10) / 1000;
576
577         if (val > 255)
578                 val = 255;
579
580         mutex_lock(&data->update_lock);
581         f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
582         data->temp_high[nr] = val;
583         mutex_unlock(&data->update_lock);
584
585         return count;
586 }
587
588 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
589         *devattr, char *buf)
590 {
591         struct f71882fg_data *data = f71882fg_update_device(dev);
592         int nr = to_sensor_dev_attr_2(devattr)->index;
593
594         return sprintf(buf, "%d\n",
595                 (data->temp_high[nr] - data->temp_hyst[nr]) * 1000);
596 }
597
598 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
599         *devattr, const char *buf, size_t count)
600 {
601         struct f71882fg_data *data = dev_get_drvdata(dev);
602         int nr = to_sensor_dev_attr_2(devattr)->index;
603         int val = simple_strtoul(buf, NULL, 10) / 1000;
604         ssize_t ret = count;
605
606         mutex_lock(&data->update_lock);
607
608         /* convert abs to relative and check */
609         val = data->temp_high[nr] - val;
610         if (val < 0 || val > 15) {
611                 ret = -EINVAL;
612                 goto store_temp_max_hyst_exit;
613         }
614
615         data->temp_hyst[nr] = val;
616
617         /* convert value to register contents */
618         switch (nr) {
619                 case 0:
620                         val = val << 4;
621                         break;
622                 case 1:
623                         val = val | (data->temp_hyst[2] << 4);
624                         break;
625                 case 2:
626                         val = data->temp_hyst[1] | (val << 4);
627                         break;
628         }
629
630         f71882fg_write8(data, nr ? F71882FG_REG_TEMP_HYST23 :
631                 F71882FG_REG_TEMP_HYST1, val);
632
633 store_temp_max_hyst_exit:
634         mutex_unlock(&data->update_lock);
635         return ret;
636 }
637
638 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
639         *devattr, char *buf)
640 {
641         struct f71882fg_data *data = f71882fg_update_device(dev);
642         int nr = to_sensor_dev_attr_2(devattr)->index;
643
644         return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
645 }
646
647 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
648         *devattr, const char *buf, size_t count)
649 {
650         struct f71882fg_data *data = dev_get_drvdata(dev);
651         int nr = to_sensor_dev_attr_2(devattr)->index;
652         int val = simple_strtoul(buf, NULL, 10) / 1000;
653
654         if (val > 255)
655                 val = 255;
656
657         mutex_lock(&data->update_lock);
658         f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
659         data->temp_ovt[nr] = val;
660         mutex_unlock(&data->update_lock);
661
662         return count;
663 }
664
665 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
666         *devattr, char *buf)
667 {
668         struct f71882fg_data *data = f71882fg_update_device(dev);
669         int nr = to_sensor_dev_attr_2(devattr)->index;
670
671         return sprintf(buf, "%d\n",
672                 (data->temp_ovt[nr] - data->temp_hyst[nr]) * 1000);
673 }
674
675 static ssize_t show_temp_type(struct device *dev, struct device_attribute
676         *devattr, char *buf)
677 {
678         struct f71882fg_data *data = f71882fg_update_device(dev);
679         int nr = to_sensor_dev_attr_2(devattr)->index;
680
681         return sprintf(buf, "%d\n", data->temp_type[nr]);
682 }
683
684 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
685         *devattr, char *buf)
686 {
687         struct f71882fg_data *data = f71882fg_update_device(dev);
688         int nr = to_sensor_dev_attr_2(devattr)->index;
689
690         if (data->temp_beep & (1 << (nr + 1)))
691                 return sprintf(buf, "1\n");
692         else
693                 return sprintf(buf, "0\n");
694 }
695
696 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
697         *devattr, const char *buf, size_t count)
698 {
699         struct f71882fg_data *data = dev_get_drvdata(dev);
700         int nr = to_sensor_dev_attr_2(devattr)->index;
701         int val = simple_strtoul(buf, NULL, 10);
702
703         mutex_lock(&data->update_lock);
704         if (val)
705                 data->temp_beep |= 1 << (nr + 1);
706         else
707                 data->temp_beep &= ~(1 << (nr + 1));
708
709         f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
710         mutex_unlock(&data->update_lock);
711
712         return count;
713 }
714
715 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
716         *devattr, char *buf)
717 {
718         struct f71882fg_data *data = f71882fg_update_device(dev);
719         int nr = to_sensor_dev_attr_2(devattr)->index;
720
721         if (data->temp_status & (1 << (nr + 1)))
722                 return sprintf(buf, "1\n");
723         else
724                 return sprintf(buf, "0\n");
725 }
726
727 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
728         *devattr, char *buf)
729 {
730         struct f71882fg_data *data = f71882fg_update_device(dev);
731         int nr = to_sensor_dev_attr_2(devattr)->index;
732
733         if (data->temp_diode_open & (1 << (nr + 1)))
734                 return sprintf(buf, "1\n");
735         else
736                 return sprintf(buf, "0\n");
737 }
738
739 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
740         char *buf)
741 {
742         return sprintf(buf, DRVNAME "\n");
743 }
744
745
746 static int __devinit f71882fg_probe(struct platform_device * pdev)
747 {
748         struct f71882fg_data *data;
749         int err, i;
750         u8 start_reg;
751
752         if (!(data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL)))
753                 return -ENOMEM;
754
755         data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
756         mutex_init(&data->update_lock);
757         platform_set_drvdata(pdev, data);
758
759         /* Register sysfs interface files */
760         for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++) {
761                 err = device_create_file(&pdev->dev, &f71882fg_dev_attr[i]);
762                 if (err)
763                         goto exit_unregister_sysfs;
764         }
765
766         start_reg = f71882fg_read8(data, F71882FG_REG_START);
767         if (start_reg & 0x01) {
768                 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++) {
769                         err = device_create_file(&pdev->dev,
770                                         &f71882fg_in_temp_attr[i].dev_attr);
771                         if (err)
772                                 goto exit_unregister_sysfs;
773                 }
774         }
775
776         if (start_reg & 0x02) {
777                 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++) {
778                         err = device_create_file(&pdev->dev,
779                                         &f71882fg_fan_attr[i].dev_attr);
780                         if (err)
781                                 goto exit_unregister_sysfs;
782                 }
783         }
784
785         data->hwmon_dev = hwmon_device_register(&pdev->dev);
786         if (IS_ERR(data->hwmon_dev)) {
787                 err = PTR_ERR(data->hwmon_dev);
788                 goto exit_unregister_sysfs;
789         }
790
791         return 0;
792
793 exit_unregister_sysfs:
794         for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
795                 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
796
797         for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
798                 device_remove_file(&pdev->dev,
799                                         &f71882fg_in_temp_attr[i].dev_attr);
800
801         for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
802                 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
803
804         kfree(data);
805
806         return err;
807 }
808
809 static int __devexit f71882fg_remove(struct platform_device *pdev)
810 {
811         int i;
812         struct f71882fg_data *data = platform_get_drvdata(pdev);
813
814         platform_set_drvdata(pdev, NULL);
815         hwmon_device_unregister(data->hwmon_dev);
816
817         for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
818                 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
819
820         for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
821                 device_remove_file(&pdev->dev,
822                                         &f71882fg_in_temp_attr[i].dev_attr);
823
824         for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
825                 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
826
827         kfree(data);
828
829         return 0;
830 }
831
832 static int __init f71882fg_find(int sioaddr, unsigned short *address)
833 {
834         int err = -ENODEV;
835         u16 devid;
836         u8 start_reg;
837         struct f71882fg_data data;
838
839         superio_enter(sioaddr);
840
841         devid = superio_inw(sioaddr, SIO_REG_MANID);
842         if (devid != SIO_FINTEK_ID) {
843                 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
844                 goto exit;
845         }
846
847         devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
848         if (devid != SIO_F71882_ID) {
849                 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
850                 goto exit;
851         }
852
853         superio_select(sioaddr, SIO_F71882FG_LD_HWM);
854         if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
855                 printk(KERN_WARNING DRVNAME ": Device not activated\n");
856                 goto exit;
857         }
858
859         *address = superio_inw(sioaddr, SIO_REG_ADDR);
860         if (*address == 0)
861         {
862                 printk(KERN_WARNING DRVNAME ": Base address not set\n");
863                 goto exit;
864         }
865         *address &= ~(REGION_LENGTH - 1);       /* Ignore 3 LSB */
866
867         data.addr = *address;
868         start_reg = f71882fg_read8(&data, F71882FG_REG_START);
869         if (!(start_reg & 0x03)) {
870                 printk(KERN_WARNING DRVNAME
871                         ": Hardware monitoring not activated\n");
872                 goto exit;
873         }
874
875         err = 0;
876         printk(KERN_INFO DRVNAME ": Found F71882FG chip at %#x, revision %d\n",
877                 (unsigned int)*address,
878                 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
879 exit:
880         superio_exit(sioaddr);
881         return err;
882 }
883
884 static int __init f71882fg_device_add(unsigned short address)
885 {
886         struct resource res = {
887                 .start  = address,
888                 .end    = address + REGION_LENGTH - 1,
889                 .flags  = IORESOURCE_IO,
890         };
891         int err;
892
893         f71882fg_pdev = platform_device_alloc(DRVNAME, address);
894         if (!f71882fg_pdev)
895                 return -ENOMEM;
896
897         res.name = f71882fg_pdev->name;
898         err = platform_device_add_resources(f71882fg_pdev, &res, 1);
899         if (err) {
900                 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
901                 goto exit_device_put;
902         }
903
904         err = platform_device_add(f71882fg_pdev);
905         if (err) {
906                 printk(KERN_ERR DRVNAME ": Device addition failed\n");
907                 goto exit_device_put;
908         }
909
910         return 0;
911
912 exit_device_put:
913         platform_device_put(f71882fg_pdev);
914
915         return err;
916 }
917
918 static int __init f71882fg_init(void)
919 {
920         int err = -ENODEV;
921         unsigned short address;
922
923         if (f71882fg_find(0x2e, &address) && f71882fg_find(0x4e, &address))
924                 goto exit;
925
926         if ((err = platform_driver_register(&f71882fg_driver)))
927                 goto exit;
928
929         if ((err = f71882fg_device_add(address)))
930                 goto exit_driver;
931
932         return 0;
933
934 exit_driver:
935         platform_driver_unregister(&f71882fg_driver);
936 exit:
937         return err;
938 }
939
940 static void __exit f71882fg_exit(void)
941 {
942         platform_device_unregister(f71882fg_pdev);
943         platform_driver_unregister(&f71882fg_driver);
944 }
945
946 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
947 MODULE_AUTHOR("Hans Edgington (hans@edgington.nl)");
948 MODULE_LICENSE("GPL");
949
950 module_init(f71882fg_init);
951 module_exit(f71882fg_exit);