Merge branch 'next' into for-linus
[pandora-kernel.git] / drivers / hwmon / vt1211.c
1 /*
2  * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
3  *            monitoring features
4  * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
5  *
6  * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
7  * and its port to kernel 2.6 by Lars Ekman.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/platform_device.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/hwmon-vid.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/ioport.h>
35 #include <asm/io.h>
36
37 static int uch_config = -1;
38 module_param(uch_config, int, 0);
39 MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
40
41 static int int_mode = -1;
42 module_param(int_mode, int, 0);
43 MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
44
45 static unsigned short force_id;
46 module_param(force_id, ushort, 0);
47 MODULE_PARM_DESC(force_id, "Override the detected device ID");
48
49 static struct platform_device *pdev;
50
51 #define DRVNAME "vt1211"
52
53 /* ---------------------------------------------------------------------
54  * Registers
55  *
56  * The sensors are defined as follows.
57  *
58  * Sensor          Voltage Mode   Temp Mode   Notes (from the datasheet)
59  * --------        ------------   ---------   --------------------------
60  * Reading 1                      temp1       Intel thermal diode
61  * Reading 3                      temp2       Internal thermal diode
62  * UCH1/Reading2   in0            temp3       NTC type thermistor
63  * UCH2            in1            temp4       +2.5V
64  * UCH3            in2            temp5       VccP
65  * UCH4            in3            temp6       +5V
66  * UCH5            in4            temp7       +12V
67  * 3.3V            in5                        Internal VDD (+3.3V)
68  *
69  * --------------------------------------------------------------------- */
70
71 /* Voltages (in) numbered 0-5 (ix) */
72 #define VT1211_REG_IN(ix)               (0x21 + (ix))
73 #define VT1211_REG_IN_MIN(ix)           ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
74 #define VT1211_REG_IN_MAX(ix)           ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
75
76 /* Temperatures (temp) numbered 0-6 (ix) */
77 static u8 regtemp[]     = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
78 static u8 regtempmax[]  = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
79 static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
80
81 /* Fans numbered 0-1 (ix) */
82 #define VT1211_REG_FAN(ix)              (0x29 + (ix))
83 #define VT1211_REG_FAN_MIN(ix)          (0x3b + (ix))
84 #define VT1211_REG_FAN_DIV               0x47
85
86 /* PWMs numbered 0-1 (ix) */
87 /* Auto points numbered 0-3 (ap) */
88 #define VT1211_REG_PWM(ix)              (0x60 + (ix))
89 #define VT1211_REG_PWM_CLK               0x50
90 #define VT1211_REG_PWM_CTL               0x51
91 #define VT1211_REG_PWM_AUTO_TEMP(ap)    (0x55 - (ap))
92 #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
93
94 /* Miscellaneous registers */
95 #define VT1211_REG_CONFIG               0x40
96 #define VT1211_REG_ALARM1               0x41
97 #define VT1211_REG_ALARM2               0x42
98 #define VT1211_REG_VID                  0x45
99 #define VT1211_REG_UCH_CONFIG           0x4a
100 #define VT1211_REG_TEMP1_CONFIG         0x4b
101 #define VT1211_REG_TEMP2_CONFIG         0x4c
102
103 /* In, temp & fan alarm bits */
104 static const u8 bitalarmin[]    = {11, 0, 1, 3, 8, 2, 9};
105 static const u8 bitalarmtemp[]  = {4, 15, 11, 0, 1, 3, 8};
106 static const u8 bitalarmfan[]   = {6, 7};
107
108 /* ---------------------------------------------------------------------
109  * Data structures and manipulation thereof
110  * --------------------------------------------------------------------- */
111
112 struct vt1211_data {
113         unsigned short addr;
114         const char *name;
115         struct device *hwmon_dev;
116
117         struct mutex update_lock;
118         char valid;                     /* !=0 if following fields are valid */
119         unsigned long last_updated;     /* In jiffies */
120
121         /* Register values */
122         u8  in[6];
123         u8  in_max[6];
124         u8  in_min[6];
125         u8  temp[7];
126         u8  temp_max[7];
127         u8  temp_hyst[7];
128         u8  fan[2];
129         u8  fan_min[2];
130         u8  fan_div[2];
131         u8  fan_ctl;
132         u8  pwm[2];
133         u8  pwm_ctl[2];
134         u8  pwm_clk;
135         u8  pwm_auto_temp[4];
136         u8  pwm_auto_pwm[2][4];
137         u8  vid;                /* Read once at init time */
138         u8  vrm;
139         u8  uch_config;         /* Read once at init time */
140         u16 alarms;
141 };
142
143 /* ix = [0-5] */
144 #define ISVOLT(ix, uch_config)  ((ix) > 4 ? 1 : \
145                                  !(((uch_config) >> ((ix) + 2)) & 1))
146
147 /* ix = [0-6] */
148 #define ISTEMP(ix, uch_config)  ((ix) < 2 ? 1 : \
149                                  ((uch_config) >> (ix)) & 1)
150
151 /* in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
152    driver according to the VT1211 BIOS porting guide */
153 #define IN_FROM_REG(ix, reg)    ((reg) < 3 ? 0 : (ix) == 5 ? \
154                                  (((reg) - 3) * 15882 + 479) / 958 : \
155                                  (((reg) - 3) * 10000 + 479) / 958)
156 #define IN_TO_REG(ix, val)      (SENSORS_LIMIT((ix) == 5 ? \
157                                  ((val) * 958 + 7941) / 15882 + 3 : \
158                                  ((val) * 958 + 5000) / 10000 + 3, 0, 255))
159
160 /* temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
161    temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
162    according to some measurements that I took on an EPIA M10000.
163    temp3-7 are thermistor based so the driver returns the voltage measured at
164    the pin (range 0V - 2.2V). */
165 #define TEMP_FROM_REG(ix, reg)  ((ix) == 0 ? (reg) * 1000 : \
166                                  (ix) == 1 ? (reg) < 51 ? 0 : \
167                                  ((reg) - 51) * 1000 : \
168                                  ((253 - (reg)) * 2200 + 105) / 210)
169 #define TEMP_TO_REG(ix, val)    SENSORS_LIMIT( \
170                                  ((ix) == 0 ? ((val) + 500) / 1000 : \
171                                   (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
172                                   253 - ((val) * 210 + 1100) / 2200), 0, 255)
173
174 #define DIV_FROM_REG(reg)       (1 << (reg))
175
176 #define RPM_FROM_REG(reg, div)  (((reg) == 0) || ((reg) == 255) ? 0 : \
177                                  1310720 / (reg) / DIV_FROM_REG(div))
178 #define RPM_TO_REG(val, div)    ((val) == 0 ? 255 : \
179                                  SENSORS_LIMIT((1310720 / (val) / \
180                                  DIV_FROM_REG(div)), 1, 254))
181
182 /* ---------------------------------------------------------------------
183  * Super-I/O constants and functions
184  * --------------------------------------------------------------------- */
185
186 /* Configuration index port registers
187  * The vt1211 can live at 2 different addresses so we need to probe both */
188 #define SIO_REG_CIP1            0x2e
189 #define SIO_REG_CIP2            0x4e
190
191 /* Configuration registers */
192 #define SIO_VT1211_LDN          0x07    /* logical device number */
193 #define SIO_VT1211_DEVID        0x20    /* device ID */
194 #define SIO_VT1211_DEVREV       0x21    /* device revision */
195 #define SIO_VT1211_ACTIVE       0x30    /* HW monitor active */
196 #define SIO_VT1211_BADDR        0x60    /* base I/O address */
197 #define SIO_VT1211_ID           0x3c    /* VT1211 device ID */
198
199 /* VT1211 logical device numbers */
200 #define SIO_VT1211_LDN_HWMON    0x0b    /* HW monitor */
201
202 static inline void superio_outb(int sio_cip, int reg, int val)
203 {
204         outb(reg, sio_cip);
205         outb(val, sio_cip + 1);
206 }
207
208 static inline int superio_inb(int sio_cip, int reg)
209 {
210         outb(reg, sio_cip);
211         return inb(sio_cip + 1);
212 }
213
214 static inline void superio_select(int sio_cip, int ldn)
215 {
216         outb(SIO_VT1211_LDN, sio_cip);
217         outb(ldn, sio_cip + 1);
218 }
219
220 static inline void superio_enter(int sio_cip)
221 {
222         outb(0x87, sio_cip);
223         outb(0x87, sio_cip);
224 }
225
226 static inline void superio_exit(int sio_cip)
227 {
228         outb(0xaa, sio_cip);
229 }
230
231 /* ---------------------------------------------------------------------
232  * Device I/O access
233  * --------------------------------------------------------------------- */
234
235 static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
236 {
237         return inb(data->addr + reg);
238 }
239
240 static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
241 {
242         outb(val, data->addr + reg);
243 }
244
245 static struct vt1211_data *vt1211_update_device(struct device *dev)
246 {
247         struct vt1211_data *data = dev_get_drvdata(dev);
248         int ix, val;
249
250         mutex_lock(&data->update_lock);
251
252         /* registers cache is refreshed after 1 second */
253         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
254                 /* read VID */
255                 data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
256
257                 /* voltage (in) registers */
258                 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
259                         if (ISVOLT(ix, data->uch_config)) {
260                                 data->in[ix] = vt1211_read8(data,
261                                                 VT1211_REG_IN(ix));
262                                 data->in_min[ix] = vt1211_read8(data,
263                                                 VT1211_REG_IN_MIN(ix));
264                                 data->in_max[ix] = vt1211_read8(data,
265                                                 VT1211_REG_IN_MAX(ix));
266                         }
267                 }
268
269                 /* temp registers */
270                 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
271                         if (ISTEMP(ix, data->uch_config)) {
272                                 data->temp[ix] = vt1211_read8(data,
273                                                 regtemp[ix]);
274                                 data->temp_max[ix] = vt1211_read8(data,
275                                                 regtempmax[ix]);
276                                 data->temp_hyst[ix] = vt1211_read8(data,
277                                                 regtemphyst[ix]);
278                         }
279                 }
280
281                 /* fan & pwm registers */
282                 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
283                         data->fan[ix] = vt1211_read8(data,
284                                                 VT1211_REG_FAN(ix));
285                         data->fan_min[ix] = vt1211_read8(data,
286                                                 VT1211_REG_FAN_MIN(ix));
287                         data->pwm[ix] = vt1211_read8(data,
288                                                 VT1211_REG_PWM(ix));
289                 }
290                 val = vt1211_read8(data, VT1211_REG_FAN_DIV);
291                 data->fan_div[0] = (val >> 4) & 3;
292                 data->fan_div[1] = (val >> 6) & 3;
293                 data->fan_ctl = val & 0xf;
294
295                 val = vt1211_read8(data, VT1211_REG_PWM_CTL);
296                 data->pwm_ctl[0] = val & 0xf;
297                 data->pwm_ctl[1] = (val >> 4) & 0xf;
298
299                 data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
300
301                 /* pwm & temp auto point registers */
302                 data->pwm_auto_pwm[0][1] = vt1211_read8(data,
303                                                 VT1211_REG_PWM_AUTO_PWM(0, 1));
304                 data->pwm_auto_pwm[0][2] = vt1211_read8(data,
305                                                 VT1211_REG_PWM_AUTO_PWM(0, 2));
306                 data->pwm_auto_pwm[1][1] = vt1211_read8(data,
307                                                 VT1211_REG_PWM_AUTO_PWM(1, 1));
308                 data->pwm_auto_pwm[1][2] = vt1211_read8(data,
309                                                 VT1211_REG_PWM_AUTO_PWM(1, 2));
310                 for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
311                         data->pwm_auto_temp[ix] = vt1211_read8(data,
312                                                 VT1211_REG_PWM_AUTO_TEMP(ix));
313                 }
314
315                 /* alarm registers */
316                 data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
317                                 vt1211_read8(data, VT1211_REG_ALARM1);
318
319                 data->last_updated = jiffies;
320                 data->valid = 1;
321         }
322
323         mutex_unlock(&data->update_lock);
324
325         return data;
326 }
327
328 /* ---------------------------------------------------------------------
329  * Voltage sysfs interfaces
330  * ix = [0-5]
331  * --------------------------------------------------------------------- */
332
333 #define SHOW_IN_INPUT   0
334 #define SHOW_SET_IN_MIN 1
335 #define SHOW_SET_IN_MAX 2
336 #define SHOW_IN_ALARM   3
337
338 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
339                        char *buf)
340 {
341         struct vt1211_data *data = vt1211_update_device(dev);
342         struct sensor_device_attribute_2 *sensor_attr_2 =
343                                                 to_sensor_dev_attr_2(attr);
344         int ix = sensor_attr_2->index;
345         int fn = sensor_attr_2->nr;
346         int res;
347
348         switch (fn) {
349         case SHOW_IN_INPUT:
350                 res = IN_FROM_REG(ix, data->in[ix]);
351                 break;
352         case SHOW_SET_IN_MIN:
353                 res = IN_FROM_REG(ix, data->in_min[ix]);
354                 break;
355         case SHOW_SET_IN_MAX:
356                 res = IN_FROM_REG(ix, data->in_max[ix]);
357                 break;
358         case SHOW_IN_ALARM:
359                 res = (data->alarms >> bitalarmin[ix]) & 1;
360                 break;
361         default:
362                 res = 0;
363                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
364         }
365
366         return sprintf(buf, "%d\n", res);
367 }
368
369 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
370                       const char *buf, size_t count)
371 {
372         struct vt1211_data *data = dev_get_drvdata(dev);
373         struct sensor_device_attribute_2 *sensor_attr_2 =
374                                                 to_sensor_dev_attr_2(attr);
375         int ix = sensor_attr_2->index;
376         int fn = sensor_attr_2->nr;
377         long val = simple_strtol(buf, NULL, 10);
378
379         mutex_lock(&data->update_lock);
380         switch (fn) {
381         case SHOW_SET_IN_MIN:
382                 data->in_min[ix] = IN_TO_REG(ix, val);
383                 vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
384                 break;
385         case SHOW_SET_IN_MAX:
386                 data->in_max[ix] = IN_TO_REG(ix, val);
387                 vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
388                 break;
389         default:
390                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
391         }
392         mutex_unlock(&data->update_lock);
393
394         return count;
395 }
396
397 /* ---------------------------------------------------------------------
398  * Temperature sysfs interfaces
399  * ix = [0-6]
400  * --------------------------------------------------------------------- */
401
402 #define SHOW_TEMP_INPUT         0
403 #define SHOW_SET_TEMP_MAX       1
404 #define SHOW_SET_TEMP_MAX_HYST  2
405 #define SHOW_TEMP_ALARM         3
406
407 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
408                          char *buf)
409 {
410         struct vt1211_data *data = vt1211_update_device(dev);
411         struct sensor_device_attribute_2 *sensor_attr_2 =
412                                                 to_sensor_dev_attr_2(attr);
413         int ix = sensor_attr_2->index;
414         int fn = sensor_attr_2->nr;
415         int res;
416
417         switch (fn) {
418         case SHOW_TEMP_INPUT:
419                 res = TEMP_FROM_REG(ix, data->temp[ix]);
420                 break;
421         case SHOW_SET_TEMP_MAX:
422                 res = TEMP_FROM_REG(ix, data->temp_max[ix]);
423                 break;
424         case SHOW_SET_TEMP_MAX_HYST:
425                 res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
426                 break;
427         case SHOW_TEMP_ALARM:
428                 res = (data->alarms >> bitalarmtemp[ix]) & 1;
429                 break;
430         default:
431                 res = 0;
432                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
433         }
434
435         return sprintf(buf, "%d\n", res);
436 }
437
438 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
439                         const char *buf, size_t count)
440 {
441         struct vt1211_data *data = dev_get_drvdata(dev);
442         struct sensor_device_attribute_2 *sensor_attr_2 =
443                                                 to_sensor_dev_attr_2(attr);
444         int ix = sensor_attr_2->index;
445         int fn = sensor_attr_2->nr;
446         long val = simple_strtol(buf, NULL, 10);
447
448         mutex_lock(&data->update_lock);
449         switch (fn) {
450         case SHOW_SET_TEMP_MAX:
451                 data->temp_max[ix] = TEMP_TO_REG(ix, val);
452                 vt1211_write8(data, regtempmax[ix],
453                               data->temp_max[ix]);
454                 break;
455         case SHOW_SET_TEMP_MAX_HYST:
456                 data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
457                 vt1211_write8(data, regtemphyst[ix],
458                               data->temp_hyst[ix]);
459                 break;
460         default:
461                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
462         }
463         mutex_unlock(&data->update_lock);
464
465         return count;
466 }
467
468 /* ---------------------------------------------------------------------
469  * Fan sysfs interfaces
470  * ix = [0-1]
471  * --------------------------------------------------------------------- */
472
473 #define SHOW_FAN_INPUT          0
474 #define SHOW_SET_FAN_MIN        1
475 #define SHOW_SET_FAN_DIV        2
476 #define SHOW_FAN_ALARM          3
477
478 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
479                         char *buf)
480 {
481         struct vt1211_data *data = vt1211_update_device(dev);
482         struct sensor_device_attribute_2 *sensor_attr_2 =
483                                                 to_sensor_dev_attr_2(attr);
484         int ix = sensor_attr_2->index;
485         int fn = sensor_attr_2->nr;
486         int res;
487
488         switch (fn) {
489         case SHOW_FAN_INPUT:
490                 res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
491                 break;
492         case SHOW_SET_FAN_MIN:
493                 res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
494                 break;
495         case SHOW_SET_FAN_DIV:
496                 res = DIV_FROM_REG(data->fan_div[ix]);
497                 break;
498         case SHOW_FAN_ALARM:
499                 res = (data->alarms >> bitalarmfan[ix]) & 1;
500                 break;
501         default:
502                 res = 0;
503                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
504         }
505
506         return sprintf(buf, "%d\n", res);
507 }
508
509 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
510                        const char *buf, size_t count)
511 {
512         struct vt1211_data *data = dev_get_drvdata(dev);
513         struct sensor_device_attribute_2 *sensor_attr_2 =
514                                                 to_sensor_dev_attr_2(attr);
515         int ix = sensor_attr_2->index;
516         int fn = sensor_attr_2->nr;
517         long val = simple_strtol(buf, NULL, 10);
518         int reg;
519
520         mutex_lock(&data->update_lock);
521
522         /* sync the data cache */
523         reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
524         data->fan_div[0] = (reg >> 4) & 3;
525         data->fan_div[1] = (reg >> 6) & 3;
526         data->fan_ctl = reg & 0xf;
527
528         switch (fn) {
529         case SHOW_SET_FAN_MIN:
530                 data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
531                 vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
532                               data->fan_min[ix]);
533                 break;
534         case SHOW_SET_FAN_DIV:
535                 switch (val) {
536                         case 1: data->fan_div[ix] = 0; break;
537                         case 2: data->fan_div[ix] = 1; break;
538                         case 4: data->fan_div[ix] = 2; break;
539                         case 8: data->fan_div[ix] = 3; break;
540                         default:
541                                 count = -EINVAL;
542                                 dev_warn(dev, "fan div value %ld not "
543                                          "supported. Choose one of 1, 2, "
544                                          "4, or 8.\n", val);
545                                 goto EXIT;
546                 }
547                 vt1211_write8(data, VT1211_REG_FAN_DIV,
548                               ((data->fan_div[1] << 6) |
549                                (data->fan_div[0] << 4) |
550                                 data->fan_ctl));
551                 break;
552         default:
553                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
554         }
555
556 EXIT:
557         mutex_unlock(&data->update_lock);
558         return count;
559 }
560
561 /* ---------------------------------------------------------------------
562  * PWM sysfs interfaces
563  * ix = [0-1]
564  * --------------------------------------------------------------------- */
565
566 #define SHOW_PWM                        0
567 #define SHOW_SET_PWM_ENABLE             1
568 #define SHOW_SET_PWM_FREQ               2
569 #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
570
571 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
572                         char *buf)
573 {
574         struct vt1211_data *data = vt1211_update_device(dev);
575         struct sensor_device_attribute_2 *sensor_attr_2 =
576                                                 to_sensor_dev_attr_2(attr);
577         int ix = sensor_attr_2->index;
578         int fn = sensor_attr_2->nr;
579         int res;
580
581         switch (fn) {
582         case SHOW_PWM:
583                 res = data->pwm[ix];
584                 break;
585         case SHOW_SET_PWM_ENABLE:
586                 res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
587                 break;
588         case SHOW_SET_PWM_FREQ:
589                 res = 90000 >> (data->pwm_clk & 7);
590                 break;
591         case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
592                 res = (data->pwm_ctl[ix] & 7) + 1;
593                 break;
594         default:
595                 res = 0;
596                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
597         }
598
599         return sprintf(buf, "%d\n", res);
600 }
601
602 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
603                        const char *buf, size_t count)
604 {
605         struct vt1211_data *data = dev_get_drvdata(dev);
606         struct sensor_device_attribute_2 *sensor_attr_2 =
607                                                 to_sensor_dev_attr_2(attr);
608         int ix = sensor_attr_2->index;
609         int fn = sensor_attr_2->nr;
610         long val = simple_strtol(buf, NULL, 10);
611         int tmp, reg;
612
613         mutex_lock(&data->update_lock);
614
615         switch (fn) {
616         case SHOW_SET_PWM_ENABLE:
617                 /* sync the data cache */
618                 reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
619                 data->fan_div[0] = (reg >> 4) & 3;
620                 data->fan_div[1] = (reg >> 6) & 3;
621                 data->fan_ctl = reg & 0xf;
622                 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
623                 data->pwm_ctl[0] = reg & 0xf;
624                 data->pwm_ctl[1] = (reg >> 4) & 0xf;
625                 switch (val) {
626                 case 0:
627                         data->pwm_ctl[ix] &= 7;
628                         /* disable SmartGuardian if both PWM outputs are
629                          * disabled */
630                         if ((data->pwm_ctl[ix ^ 1] & 1) == 0) {
631                                 data->fan_ctl &= 0xe;
632                         }
633                         break;
634                 case 2:
635                         data->pwm_ctl[ix] |= 8;
636                         data->fan_ctl |= 1;
637                         break;
638                 default:
639                         count = -EINVAL;
640                         dev_warn(dev, "pwm mode %ld not supported. "
641                                  "Choose one of 0 or 2.\n", val);
642                         goto EXIT;
643                 }
644                 vt1211_write8(data, VT1211_REG_PWM_CTL,
645                               ((data->pwm_ctl[1] << 4) |
646                                 data->pwm_ctl[0]));
647                 vt1211_write8(data, VT1211_REG_FAN_DIV,
648                               ((data->fan_div[1] << 6) |
649                                (data->fan_div[0] << 4) |
650                                 data->fan_ctl));
651                 break;
652         case SHOW_SET_PWM_FREQ:
653                 val = 135000 / SENSORS_LIMIT(val, 135000 >> 7, 135000);
654                 /* calculate tmp = log2(val) */
655                 tmp = 0;
656                 for (val >>= 1; val > 0; val >>= 1) {
657                         tmp++;
658                 }
659                 /* sync the data cache */
660                 reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
661                 data->pwm_clk = (reg & 0xf8) | tmp;
662                 vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
663                 break;
664         case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
665                 if ((val < 1) || (val > 7)) {
666                         count = -EINVAL;
667                         dev_warn(dev, "temp channel %ld not supported. "
668                                  "Choose a value between 1 and 7.\n", val);
669                         goto EXIT;
670                 }
671                 if (!ISTEMP(val - 1, data->uch_config)) {
672                         count = -EINVAL;
673                         dev_warn(dev, "temp channel %ld is not available.\n",
674                                  val);
675                         goto EXIT;
676                 }
677                 /* sync the data cache */
678                 reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
679                 data->pwm_ctl[0] = reg & 0xf;
680                 data->pwm_ctl[1] = (reg >> 4) & 0xf;
681                 data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
682                 vt1211_write8(data, VT1211_REG_PWM_CTL,
683                               ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
684                 break;
685         default:
686                 dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
687         }
688
689 EXIT:
690         mutex_unlock(&data->update_lock);
691         return count;
692 }
693
694 /* ---------------------------------------------------------------------
695  * PWM auto point definitions
696  * ix = [0-1]
697  * ap = [0-3]
698  * --------------------------------------------------------------------- */
699
700 /*
701  * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
702  * Note that there is only a single set of temp auto points that controls both
703  * PWM controllers. We still create 2 sets of sysfs files to make it look
704  * more consistent even though they map to the same registers.
705  *
706  * ix ap : description
707  * -------------------
708  * 0  0  : pwm1/2 off temperature        (pwm_auto_temp[0])
709  * 0  1  : pwm1/2 low speed temperature  (pwm_auto_temp[1])
710  * 0  2  : pwm1/2 high speed temperature (pwm_auto_temp[2])
711  * 0  3  : pwm1/2 full speed temperature (pwm_auto_temp[3])
712  * 1  0  : pwm1/2 off temperature        (pwm_auto_temp[0])
713  * 1  1  : pwm1/2 low speed temperature  (pwm_auto_temp[1])
714  * 1  2  : pwm1/2 high speed temperature (pwm_auto_temp[2])
715  * 1  3  : pwm1/2 full speed temperature (pwm_auto_temp[3])
716  */
717
718 static ssize_t show_pwm_auto_point_temp(struct device *dev,
719                                         struct device_attribute *attr,
720                                         char *buf)
721 {
722         struct vt1211_data *data = vt1211_update_device(dev);
723         struct sensor_device_attribute_2 *sensor_attr_2 =
724                                                 to_sensor_dev_attr_2(attr);
725         int ix = sensor_attr_2->index;
726         int ap = sensor_attr_2->nr;
727
728         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
729                        data->pwm_auto_temp[ap]));
730 }
731
732 static ssize_t set_pwm_auto_point_temp(struct device *dev,
733                                        struct device_attribute *attr,
734                                        const char *buf, size_t count)
735 {
736         struct vt1211_data *data = dev_get_drvdata(dev);
737         struct sensor_device_attribute_2 *sensor_attr_2 =
738                                                 to_sensor_dev_attr_2(attr);
739         int ix = sensor_attr_2->index;
740         int ap = sensor_attr_2->nr;
741         long val = simple_strtol(buf, NULL, 10);
742         int reg;
743
744         mutex_lock(&data->update_lock);
745
746         /* sync the data cache */
747         reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
748         data->pwm_ctl[0] = reg & 0xf;
749         data->pwm_ctl[1] = (reg >> 4) & 0xf;
750
751         data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
752         vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
753                       data->pwm_auto_temp[ap]);
754         mutex_unlock(&data->update_lock);
755
756         return count;
757 }
758
759 /*
760  * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
761  * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
762  * be changed.
763  *
764  * ix ap : description
765  * -------------------
766  * 0  0  : pwm1 off                   (pwm_auto_pwm[0][0], hard-wired to 0)
767  * 0  1  : pwm1 low speed duty cycle  (pwm_auto_pwm[0][1])
768  * 0  2  : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
769  * 0  3  : pwm1 full speed            (pwm_auto_pwm[0][3], hard-wired to 255)
770  * 1  0  : pwm2 off                   (pwm_auto_pwm[1][0], hard-wired to 0)
771  * 1  1  : pwm2 low speed duty cycle  (pwm_auto_pwm[1][1])
772  * 1  2  : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
773  * 1  3  : pwm2 full speed            (pwm_auto_pwm[1][3], hard-wired to 255)
774 */
775
776 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
777                                        struct device_attribute *attr,
778                                        char *buf)
779 {
780         struct vt1211_data *data = vt1211_update_device(dev);
781         struct sensor_device_attribute_2 *sensor_attr_2 =
782                                                 to_sensor_dev_attr_2(attr);
783         int ix = sensor_attr_2->index;
784         int ap = sensor_attr_2->nr;
785
786         return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
787 }
788
789 static ssize_t set_pwm_auto_point_pwm(struct device *dev,
790                                       struct device_attribute *attr,
791                                       const char *buf, size_t count)
792 {
793         struct vt1211_data *data = dev_get_drvdata(dev);
794         struct sensor_device_attribute_2 *sensor_attr_2 =
795                                                 to_sensor_dev_attr_2(attr);
796         int ix = sensor_attr_2->index;
797         int ap = sensor_attr_2->nr;
798         long val = simple_strtol(buf, NULL, 10);
799
800         if ((val < 0) || (val > 255)) {
801                 dev_err(dev, "pwm value %ld is out of range. "
802                         "Choose a value between 0 and 255.\n" , val);
803                 return -EINVAL;
804         }
805
806         mutex_lock(&data->update_lock);
807         data->pwm_auto_pwm[ix][ap] = val;
808         vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
809                       data->pwm_auto_pwm[ix][ap]);
810         mutex_unlock(&data->update_lock);
811
812         return count;
813 }
814
815 /* ---------------------------------------------------------------------
816  * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
817  * --------------------------------------------------------------------- */
818
819 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
820                         char *buf)
821 {
822         struct vt1211_data *data = dev_get_drvdata(dev);
823
824         return sprintf(buf, "%d\n", data->vrm);
825 }
826
827 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
828                        const char *buf, size_t count)
829 {
830         struct vt1211_data *data = dev_get_drvdata(dev);
831         long val = simple_strtol(buf, NULL, 10);
832
833         data->vrm = val;
834
835         return count;
836 }
837
838 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
839                         char *buf)
840 {
841         struct vt1211_data *data = dev_get_drvdata(dev);
842
843         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
844 }
845
846 static ssize_t show_name(struct device *dev,
847                          struct device_attribute *attr, char *buf)
848 {
849         struct vt1211_data *data = dev_get_drvdata(dev);
850
851         return sprintf(buf, "%s\n", data->name);
852 }
853
854 static ssize_t show_alarms(struct device *dev,
855                            struct device_attribute *attr, char *buf)
856 {
857         struct vt1211_data *data = vt1211_update_device(dev);
858
859         return sprintf(buf, "%d\n", data->alarms);
860 }
861
862 /* ---------------------------------------------------------------------
863  * Device attribute structs
864  * --------------------------------------------------------------------- */
865
866 #define SENSOR_ATTR_IN_INPUT(ix) \
867         SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
868                 show_in, NULL, SHOW_IN_INPUT, ix)
869
870 static struct sensor_device_attribute_2 vt1211_sysfs_in_input[] = {
871         SENSOR_ATTR_IN_INPUT(0),
872         SENSOR_ATTR_IN_INPUT(1),
873         SENSOR_ATTR_IN_INPUT(2),
874         SENSOR_ATTR_IN_INPUT(3),
875         SENSOR_ATTR_IN_INPUT(4),
876         SENSOR_ATTR_IN_INPUT(5),
877 };
878
879 #define SENSOR_ATTR_IN_MIN(ix) \
880         SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
881                 show_in, set_in, SHOW_SET_IN_MIN, ix)
882
883 static struct sensor_device_attribute_2 vt1211_sysfs_in_min[] = {
884         SENSOR_ATTR_IN_MIN(0),
885         SENSOR_ATTR_IN_MIN(1),
886         SENSOR_ATTR_IN_MIN(2),
887         SENSOR_ATTR_IN_MIN(3),
888         SENSOR_ATTR_IN_MIN(4),
889         SENSOR_ATTR_IN_MIN(5),
890 };
891
892 #define SENSOR_ATTR_IN_MAX(ix) \
893         SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
894                 show_in, set_in, SHOW_SET_IN_MAX, ix)
895
896 static struct sensor_device_attribute_2 vt1211_sysfs_in_max[] = {
897         SENSOR_ATTR_IN_MAX(0),
898         SENSOR_ATTR_IN_MAX(1),
899         SENSOR_ATTR_IN_MAX(2),
900         SENSOR_ATTR_IN_MAX(3),
901         SENSOR_ATTR_IN_MAX(4),
902         SENSOR_ATTR_IN_MAX(5),
903 };
904
905 #define SENSOR_ATTR_IN_ALARM(ix) \
906         SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
907                 show_in, NULL, SHOW_IN_ALARM, ix)
908
909 static struct sensor_device_attribute_2 vt1211_sysfs_in_alarm[] = {
910         SENSOR_ATTR_IN_ALARM(0),
911         SENSOR_ATTR_IN_ALARM(1),
912         SENSOR_ATTR_IN_ALARM(2),
913         SENSOR_ATTR_IN_ALARM(3),
914         SENSOR_ATTR_IN_ALARM(4),
915         SENSOR_ATTR_IN_ALARM(5),
916 };
917
918 #define SENSOR_ATTR_TEMP_INPUT(ix) \
919         SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
920                 show_temp, NULL, SHOW_TEMP_INPUT, ix-1)
921
922 static struct sensor_device_attribute_2 vt1211_sysfs_temp_input[] = {
923         SENSOR_ATTR_TEMP_INPUT(1),
924         SENSOR_ATTR_TEMP_INPUT(2),
925         SENSOR_ATTR_TEMP_INPUT(3),
926         SENSOR_ATTR_TEMP_INPUT(4),
927         SENSOR_ATTR_TEMP_INPUT(5),
928         SENSOR_ATTR_TEMP_INPUT(6),
929         SENSOR_ATTR_TEMP_INPUT(7),
930 };
931
932 #define SENSOR_ATTR_TEMP_MAX(ix) \
933         SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
934                 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1)
935
936 static struct sensor_device_attribute_2 vt1211_sysfs_temp_max[] = {
937         SENSOR_ATTR_TEMP_MAX(1),
938         SENSOR_ATTR_TEMP_MAX(2),
939         SENSOR_ATTR_TEMP_MAX(3),
940         SENSOR_ATTR_TEMP_MAX(4),
941         SENSOR_ATTR_TEMP_MAX(5),
942         SENSOR_ATTR_TEMP_MAX(6),
943         SENSOR_ATTR_TEMP_MAX(7),
944 };
945
946 #define SENSOR_ATTR_TEMP_MAX_HYST(ix) \
947         SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
948                 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1)
949
950 static struct sensor_device_attribute_2 vt1211_sysfs_temp_max_hyst[] = {
951         SENSOR_ATTR_TEMP_MAX_HYST(1),
952         SENSOR_ATTR_TEMP_MAX_HYST(2),
953         SENSOR_ATTR_TEMP_MAX_HYST(3),
954         SENSOR_ATTR_TEMP_MAX_HYST(4),
955         SENSOR_ATTR_TEMP_MAX_HYST(5),
956         SENSOR_ATTR_TEMP_MAX_HYST(6),
957         SENSOR_ATTR_TEMP_MAX_HYST(7),
958 };
959
960 #define SENSOR_ATTR_TEMP_ALARM(ix) \
961         SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
962                 show_temp, NULL, SHOW_TEMP_ALARM, ix-1)
963
964 static struct sensor_device_attribute_2 vt1211_sysfs_temp_alarm[] = {
965         SENSOR_ATTR_TEMP_ALARM(1),
966         SENSOR_ATTR_TEMP_ALARM(2),
967         SENSOR_ATTR_TEMP_ALARM(3),
968         SENSOR_ATTR_TEMP_ALARM(4),
969         SENSOR_ATTR_TEMP_ALARM(5),
970         SENSOR_ATTR_TEMP_ALARM(6),
971         SENSOR_ATTR_TEMP_ALARM(7),
972 };
973
974 #define SENSOR_ATTR_FAN(ix) \
975         SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
976                 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
977         SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
978                 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
979         SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
980                 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
981         SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
982                 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
983
984 #define SENSOR_ATTR_PWM(ix) \
985         SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
986                 show_pwm, NULL, SHOW_PWM, ix-1), \
987         SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
988                 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
989         SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
990                 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
991
992 #define SENSOR_ATTR_PWM_FREQ(ix) \
993         SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
994                 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
995
996 #define SENSOR_ATTR_PWM_FREQ_RO(ix) \
997         SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
998                 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
999
1000 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1001         SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1002                 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1003                 ap-1, ix-1)
1004
1005 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1006         SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1007                 show_pwm_auto_point_temp, NULL, \
1008                 ap-1, ix-1)
1009
1010 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1011         SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1012                 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1013                 ap-1, ix-1)
1014
1015 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1016         SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1017                 show_pwm_auto_point_pwm, NULL, \
1018                 ap-1, ix-1)
1019
1020 static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
1021         SENSOR_ATTR_FAN(1),
1022         SENSOR_ATTR_FAN(2),
1023         SENSOR_ATTR_PWM(1),
1024         SENSOR_ATTR_PWM(2),
1025         SENSOR_ATTR_PWM_FREQ(1),
1026         SENSOR_ATTR_PWM_FREQ_RO(2),
1027         SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1028         SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1029         SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1030         SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1031         SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1032         SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1033         SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1034         SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1035         SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1036         SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1037         SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1038         SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1039         SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1040         SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1041         SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1042         SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1043 };
1044
1045 static struct device_attribute vt1211_sysfs_misc[] = {
1046         __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
1047         __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
1048         __ATTR(name, S_IRUGO, show_name, NULL),
1049         __ATTR(alarms, S_IRUGO, show_alarms, NULL),
1050 };
1051
1052 /* ---------------------------------------------------------------------
1053  * Device registration and initialization
1054  * --------------------------------------------------------------------- */
1055
1056 static void __devinit vt1211_init_device(struct vt1211_data *data)
1057 {
1058         /* set VRM */
1059         data->vrm = vid_which_vrm();
1060
1061         /* Read (and initialize) UCH config */
1062         data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
1063         if (uch_config > -1) {
1064                 data->uch_config = (data->uch_config & 0x83) |
1065                                    (uch_config << 2);
1066                 vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
1067         }
1068
1069         /* Initialize the interrupt mode (if request at module load time).
1070          * The VT1211 implements 3 different modes for clearing interrupts:
1071          * 0: Clear INT when status register is read. Regenerate INT as long
1072          *    as temp stays above hysteresis limit.
1073          * 1: Clear INT when status register is read. DON'T regenerate INT
1074          *    until temp falls below hysteresis limit and exceeds hot limit
1075          *    again.
1076          * 2: Clear INT when temp falls below max limit.
1077          *
1078          * The driver only allows to force mode 0 since that's the only one
1079          * that makes sense for 'sensors' */
1080         if (int_mode == 0) {
1081                 vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
1082                 vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
1083         }
1084
1085         /* Fill in some hard wired values into our data struct */
1086         data->pwm_auto_pwm[0][3] = 255;
1087         data->pwm_auto_pwm[1][3] = 255;
1088 }
1089
1090 static void vt1211_remove_sysfs(struct platform_device *pdev)
1091 {
1092         struct device *dev = &pdev->dev;
1093         int i;
1094
1095         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1096                 device_remove_file(dev,
1097                         &vt1211_sysfs_in_input[i].dev_attr);
1098                 device_remove_file(dev,
1099                         &vt1211_sysfs_in_min[i].dev_attr);
1100                 device_remove_file(dev,
1101                         &vt1211_sysfs_in_max[i].dev_attr);
1102                 device_remove_file(dev,
1103                         &vt1211_sysfs_in_alarm[i].dev_attr);
1104         }
1105         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1106                 device_remove_file(dev,
1107                         &vt1211_sysfs_temp_input[i].dev_attr);
1108                 device_remove_file(dev,
1109                         &vt1211_sysfs_temp_max[i].dev_attr);
1110                 device_remove_file(dev,
1111                         &vt1211_sysfs_temp_max_hyst[i].dev_attr);
1112                 device_remove_file(dev,
1113                         &vt1211_sysfs_temp_alarm[i].dev_attr);
1114         }
1115         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1116                 device_remove_file(dev,
1117                         &vt1211_sysfs_fan_pwm[i].dev_attr);
1118         }
1119         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1120                 device_remove_file(dev, &vt1211_sysfs_misc[i]);
1121         }
1122 }
1123
1124 static int __devinit vt1211_probe(struct platform_device *pdev)
1125 {
1126         struct device *dev = &pdev->dev;
1127         struct vt1211_data *data;
1128         struct resource *res;
1129         int i, err;
1130
1131         if (!(data = kzalloc(sizeof(struct vt1211_data), GFP_KERNEL))) {
1132                 err = -ENOMEM;
1133                 dev_err(dev, "Out of memory\n");
1134                 goto EXIT;
1135         }
1136
1137         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1138         if (!request_region(res->start, res->end - res->start + 1, DRVNAME)) {
1139                 err = -EBUSY;
1140                 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1141                         (unsigned long)res->start, (unsigned long)res->end);
1142                 goto EXIT_KFREE;
1143         }
1144         data->addr = res->start;
1145         data->name = DRVNAME;
1146         mutex_init(&data->update_lock);
1147
1148         platform_set_drvdata(pdev, data);
1149
1150         /* Initialize the VT1211 chip */
1151         vt1211_init_device(data);
1152
1153         /* Create sysfs interface files */
1154         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
1155                 if (ISVOLT(i, data->uch_config)) {
1156                         if ((err = device_create_file(dev,
1157                                 &vt1211_sysfs_in_input[i].dev_attr)) ||
1158                             (err = device_create_file(dev,
1159                                 &vt1211_sysfs_in_min[i].dev_attr)) ||
1160                             (err = device_create_file(dev,
1161                                 &vt1211_sysfs_in_max[i].dev_attr)) ||
1162                             (err = device_create_file(dev,
1163                                 &vt1211_sysfs_in_alarm[i].dev_attr))) {
1164                                 goto EXIT_DEV_REMOVE;
1165                         }
1166                 }
1167         }
1168         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
1169                 if (ISTEMP(i, data->uch_config)) {
1170                         if ((err = device_create_file(dev,
1171                                 &vt1211_sysfs_temp_input[i].dev_attr)) ||
1172                             (err = device_create_file(dev,
1173                                 &vt1211_sysfs_temp_max[i].dev_attr)) ||
1174                             (err = device_create_file(dev,
1175                                 &vt1211_sysfs_temp_max_hyst[i].dev_attr)) ||
1176                             (err = device_create_file(dev,
1177                                 &vt1211_sysfs_temp_alarm[i].dev_attr))) {
1178                                 goto EXIT_DEV_REMOVE;
1179                         }
1180                 }
1181         }
1182         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
1183                 err = device_create_file(dev,
1184                         &vt1211_sysfs_fan_pwm[i].dev_attr);
1185                 if (err) {
1186                         goto EXIT_DEV_REMOVE;
1187                 }
1188         }
1189         for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
1190                 err = device_create_file(dev,
1191                        &vt1211_sysfs_misc[i]);
1192                 if (err) {
1193                         goto EXIT_DEV_REMOVE;
1194                 }
1195         }
1196
1197         /* Register device */
1198         data->hwmon_dev = hwmon_device_register(dev);
1199         if (IS_ERR(data->hwmon_dev)) {
1200                 err = PTR_ERR(data->hwmon_dev);
1201                 dev_err(dev, "Class registration failed (%d)\n", err);
1202                 goto EXIT_DEV_REMOVE_SILENT;
1203         }
1204
1205         return 0;
1206
1207 EXIT_DEV_REMOVE:
1208         dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
1209 EXIT_DEV_REMOVE_SILENT:
1210         vt1211_remove_sysfs(pdev);
1211         release_region(res->start, res->end - res->start + 1);
1212 EXIT_KFREE:
1213         platform_set_drvdata(pdev, NULL);
1214         kfree(data);
1215 EXIT:
1216         return err;
1217 }
1218
1219 static int __devexit vt1211_remove(struct platform_device *pdev)
1220 {
1221         struct vt1211_data *data = platform_get_drvdata(pdev);
1222         struct resource *res;
1223
1224         hwmon_device_unregister(data->hwmon_dev);
1225         vt1211_remove_sysfs(pdev);
1226         platform_set_drvdata(pdev, NULL);
1227         kfree(data);
1228
1229         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1230         release_region(res->start, res->end - res->start + 1);
1231
1232         return 0;
1233 }
1234
1235 static struct platform_driver vt1211_driver = {
1236         .driver = {
1237                 .owner = THIS_MODULE,
1238                 .name  = DRVNAME,
1239         },
1240         .probe  = vt1211_probe,
1241         .remove = __devexit_p(vt1211_remove),
1242 };
1243
1244 static int __init vt1211_device_add(unsigned short address)
1245 {
1246         struct resource res = {
1247                 .start  = address,
1248                 .end    = address + 0x7f,
1249                 .flags  = IORESOURCE_IO,
1250         };
1251         int err;
1252
1253         pdev = platform_device_alloc(DRVNAME, address);
1254         if (!pdev) {
1255                 err = -ENOMEM;
1256                 printk(KERN_ERR DRVNAME ": Device allocation failed (%d)\n",
1257                        err);
1258                 goto EXIT;
1259         }
1260
1261         res.name = pdev->name;
1262         err = platform_device_add_resources(pdev, &res, 1);
1263         if (err) {
1264                 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1265                        "(%d)\n", err);
1266                 goto EXIT_DEV_PUT;
1267         }
1268
1269         err = platform_device_add(pdev);
1270         if (err) {
1271                 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1272                        err);
1273                 goto EXIT_DEV_PUT;
1274         }
1275
1276         return 0;
1277
1278 EXIT_DEV_PUT:
1279         platform_device_put(pdev);
1280 EXIT:
1281         return err;
1282 }
1283
1284 static int __init vt1211_find(int sio_cip, unsigned short *address)
1285 {
1286         int err = -ENODEV;
1287         int devid;
1288
1289         superio_enter(sio_cip);
1290
1291         devid = force_id ? force_id : superio_inb(sio_cip, SIO_VT1211_DEVID);
1292         if (devid != SIO_VT1211_ID) {
1293                 goto EXIT;
1294         }
1295
1296         superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
1297
1298         if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
1299                 printk(KERN_WARNING DRVNAME ": HW monitor is disabled, "
1300                        "skipping\n");
1301                 goto EXIT;
1302         }
1303
1304         *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
1305                     (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
1306         if (*address == 0) {
1307                 printk(KERN_WARNING DRVNAME ": Base address is not set, "
1308                        "skipping\n");
1309                 goto EXIT;
1310         }
1311
1312         err = 0;
1313         printk(KERN_INFO DRVNAME ": Found VT1211 chip at 0x%04x, "
1314                "revision %u\n", *address,
1315                superio_inb(sio_cip, SIO_VT1211_DEVREV));
1316
1317 EXIT:
1318         superio_exit(sio_cip);
1319         return err;
1320 }
1321
1322 static int __init vt1211_init(void)
1323 {
1324         int err;
1325         unsigned short address = 0;
1326
1327         if ((err = vt1211_find(SIO_REG_CIP1, &address)) &&
1328             (err = vt1211_find(SIO_REG_CIP2, &address))) {
1329                 goto EXIT;
1330         }
1331
1332         if ((uch_config < -1) || (uch_config > 31)) {
1333                 err = -EINVAL;
1334                 printk(KERN_WARNING DRVNAME ": Invalid UCH configuration %d. "
1335                        "Choose a value between 0 and 31.\n", uch_config);
1336           goto EXIT;
1337         }
1338
1339         if ((int_mode < -1) || (int_mode > 0)) {
1340                 err = -EINVAL;
1341                 printk(KERN_WARNING DRVNAME ": Invalid interrupt mode %d. "
1342                        "Only mode 0 is supported.\n", int_mode);
1343           goto EXIT;
1344         }
1345
1346         err = platform_driver_register(&vt1211_driver);
1347         if (err) {
1348                 goto EXIT;
1349         }
1350
1351         /* Sets global pdev as a side effect */
1352         err = vt1211_device_add(address);
1353         if (err) {
1354                 goto EXIT_DRV_UNREGISTER;
1355         }
1356
1357         return 0;
1358
1359 EXIT_DRV_UNREGISTER:
1360         platform_driver_unregister(&vt1211_driver);
1361 EXIT:
1362         return err;
1363 }
1364
1365 static void __exit vt1211_exit(void)
1366 {
1367         platform_device_unregister(pdev);
1368         platform_driver_unregister(&vt1211_driver);
1369 }
1370
1371 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1372 MODULE_DESCRIPTION("VT1211 sensors");
1373 MODULE_LICENSE("GPL");
1374
1375 module_init(vt1211_init);
1376 module_exit(vt1211_exit);