]> err.no Git - linux-2.6/blob - drivers/hwmon/dme1737.c
hwmon: (dme1737) probe all addresses
[linux-2.6] / drivers / hwmon / dme1737.c
1 /*
2  * dme1737.c - Driver for the SMSC DME1737, Asus A8000, and SMSC SCH311x
3  *             Super-I/O chips integrated hardware monitoring features.
4  * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com>
5  *
6  * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
7  * the chip registers if a DME1737 (or A8000) is found and the ISA bus if a
8  * SCH311x chip is found. Both types of chips have very similar hardware
9  * monitoring capabilities but differ in the way they can be accessed.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/platform_device.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/hwmon-vid.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37 #include <asm/io.h>
38
39 /* ISA device, if found */
40 static struct platform_device *pdev;
41
42 /* Module load parameters */
43 static int force_start;
44 module_param(force_start, bool, 0);
45 MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
46
47 static unsigned short force_id;
48 module_param(force_id, ushort, 0);
49 MODULE_PARM_DESC(force_id, "Override the detected device ID");
50
51 static int probe_all_addr;
52 module_param(probe_all_addr, bool, 0);
53 MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
54                  "addresses");
55
56 /* Addresses to scan */
57 static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
58
59 /* Insmod parameters */
60 I2C_CLIENT_INSMOD_1(dme1737);
61
62 /* ---------------------------------------------------------------------
63  * Registers
64  *
65  * The sensors are defined as follows:
66  *
67  * Voltages                          Temperatures
68  * --------                          ------------
69  * in0   +5VTR (+5V stdby)           temp1   Remote diode 1
70  * in1   Vccp  (proc core)           temp2   Internal temp
71  * in2   VCC   (internal +3.3V)      temp3   Remote diode 2
72  * in3   +5V
73  * in4   +12V
74  * in5   VTR   (+3.3V stby)
75  * in6   Vbat
76  *
77  * --------------------------------------------------------------------- */
78
79 /* Voltages (in) numbered 0-6 (ix) */
80 #define DME1737_REG_IN(ix)              ((ix) < 5 ? 0x20 + (ix) \
81                                                   : 0x94 + (ix))
82 #define DME1737_REG_IN_MIN(ix)          ((ix) < 5 ? 0x44 + (ix) * 2 \
83                                                   : 0x91 + (ix) * 2)
84 #define DME1737_REG_IN_MAX(ix)          ((ix) < 5 ? 0x45 + (ix) * 2 \
85                                                   : 0x92 + (ix) * 2)
86
87 /* Temperatures (temp) numbered 0-2 (ix) */
88 #define DME1737_REG_TEMP(ix)            (0x25 + (ix))
89 #define DME1737_REG_TEMP_MIN(ix)        (0x4e + (ix) * 2)
90 #define DME1737_REG_TEMP_MAX(ix)        (0x4f + (ix) * 2)
91 #define DME1737_REG_TEMP_OFFSET(ix)     ((ix) == 0 ? 0x1f \
92                                                    : 0x1c + (ix))
93
94 /* Voltage and temperature LSBs
95  * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
96  *    IN_TEMP_LSB(0) = [in5, in6]
97  *    IN_TEMP_LSB(1) = [temp3, temp1]
98  *    IN_TEMP_LSB(2) = [in4, temp2]
99  *    IN_TEMP_LSB(3) = [in3, in0]
100  *    IN_TEMP_LSB(4) = [in2, in1] */
101 #define DME1737_REG_IN_TEMP_LSB(ix)     (0x84 + (ix))
102 static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
103 static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
104 static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
105 static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
106
107 /* Fans numbered 0-5 (ix) */
108 #define DME1737_REG_FAN(ix)             ((ix) < 4 ? 0x28 + (ix) * 2 \
109                                                   : 0xa1 + (ix) * 2)
110 #define DME1737_REG_FAN_MIN(ix)         ((ix) < 4 ? 0x54 + (ix) * 2 \
111                                                   : 0xa5 + (ix) * 2)
112 #define DME1737_REG_FAN_OPT(ix)         ((ix) < 4 ? 0x90 + (ix) \
113                                                   : 0xb2 + (ix))
114 #define DME1737_REG_FAN_MAX(ix)         (0xb4 + (ix)) /* only for fan[4-5] */
115
116 /* PWMs numbered 0-2, 4-5 (ix) */
117 #define DME1737_REG_PWM(ix)             ((ix) < 3 ? 0x30 + (ix) \
118                                                   : 0xa1 + (ix))
119 #define DME1737_REG_PWM_CONFIG(ix)      (0x5c + (ix)) /* only for pwm[0-2] */
120 #define DME1737_REG_PWM_MIN(ix)         (0x64 + (ix)) /* only for pwm[0-2] */
121 #define DME1737_REG_PWM_FREQ(ix)        ((ix) < 3 ? 0x5f + (ix) \
122                                                   : 0xa3 + (ix))
123 /* The layout of the ramp rate registers is different from the other pwm
124  * registers. The bits for the 3 PWMs are stored in 2 registers:
125  *    PWM_RR(0) = [OFF3, OFF2,  OFF1,  RES,   RR1E, RR1-2, RR1-1, RR1-0]
126  *    PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
127 #define DME1737_REG_PWM_RR(ix)          (0x62 + (ix)) /* only for pwm[0-2] */
128
129 /* Thermal zones 0-2 */
130 #define DME1737_REG_ZONE_LOW(ix)        (0x67 + (ix))
131 #define DME1737_REG_ZONE_ABS(ix)        (0x6a + (ix))
132 /* The layout of the hysteresis registers is different from the other zone
133  * registers. The bits for the 3 zones are stored in 2 registers:
134  *    ZONE_HYST(0) = [H1-3,  H1-2,  H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
135  *    ZONE_HYST(1) = [H3-3,  H3-2,  H3-1, H3-0, RES,  RES,  RES,  RES] */
136 #define DME1737_REG_ZONE_HYST(ix)       (0x6d + (ix))
137
138 /* Alarm registers and bit mapping
139  * The 3 8-bit alarm registers will be concatenated to a single 32-bit
140  * alarm value [0, ALARM3, ALARM2, ALARM1]. */
141 #define DME1737_REG_ALARM1              0x41
142 #define DME1737_REG_ALARM2              0x42
143 #define DME1737_REG_ALARM3              0x83
144 static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
145 static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
146 static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
147
148 /* Miscellaneous registers */
149 #define DME1737_REG_DEVICE              0x3d
150 #define DME1737_REG_COMPANY             0x3e
151 #define DME1737_REG_VERSTEP             0x3f
152 #define DME1737_REG_CONFIG              0x40
153 #define DME1737_REG_CONFIG2             0x7f
154 #define DME1737_REG_VID                 0x43
155 #define DME1737_REG_TACH_PWM            0x81
156
157 /* ---------------------------------------------------------------------
158  * Misc defines
159  * --------------------------------------------------------------------- */
160
161 /* Chip identification */
162 #define DME1737_COMPANY_SMSC    0x5c
163 #define DME1737_VERSTEP         0x88
164 #define DME1737_VERSTEP_MASK    0xf8
165 #define SCH311X_DEVICE          0x8c
166
167 /* Length of ISA address segment */
168 #define DME1737_EXTENT  2
169
170 /* ---------------------------------------------------------------------
171  * Data structures and manipulation thereof
172  * --------------------------------------------------------------------- */
173
174 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
175    the driver field to differentiate between I2C and ISA chips. */
176 struct dme1737_data {
177         struct i2c_client client;
178         struct device *hwmon_dev;
179
180         struct mutex update_lock;
181         int valid;                      /* !=0 if following fields are valid */
182         unsigned long last_update;      /* in jiffies */
183         unsigned long last_vbat;        /* in jiffies */
184
185         u8 vid;
186         u8 pwm_rr_en;
187         u8 has_pwm;
188         u8 has_fan;
189
190         /* Register values */
191         u16 in[7];
192         u8  in_min[7];
193         u8  in_max[7];
194         s16 temp[3];
195         s8  temp_min[3];
196         s8  temp_max[3];
197         s8  temp_offset[3];
198         u8  config;
199         u8  config2;
200         u8  vrm;
201         u16 fan[6];
202         u16 fan_min[6];
203         u8  fan_max[2];
204         u8  fan_opt[6];
205         u8  pwm[6];
206         u8  pwm_min[3];
207         u8  pwm_config[3];
208         u8  pwm_acz[3];
209         u8  pwm_freq[6];
210         u8  pwm_rr[2];
211         u8  zone_low[3];
212         u8  zone_abs[3];
213         u8  zone_hyst[2];
214         u32 alarms;
215 };
216
217 /* Nominal voltage values */
218 static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
219
220 /* Voltage input
221  * Voltage inputs have 16 bits resolution, limit values have 8 bits
222  * resolution. */
223 static inline int IN_FROM_REG(int reg, int ix, int res)
224 {
225         return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2));
226 }
227
228 static inline int IN_TO_REG(int val, int ix)
229 {
230         return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) /
231                              IN_NOMINAL[ix], 0, 255);
232 }
233
234 /* Temperature input
235  * The register values represent temperatures in 2's complement notation from
236  * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
237  * values have 8 bits resolution. */
238 static inline int TEMP_FROM_REG(int reg, int res)
239 {
240         return (reg * 1000) >> (res - 8);
241 }
242
243 static inline int TEMP_TO_REG(int val)
244 {
245         return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
246                              -128, 127);
247 }
248
249 /* Temperature range */
250 static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
251                                  10000, 13333, 16000, 20000, 26666, 32000,
252                                  40000, 53333, 80000};
253
254 static inline int TEMP_RANGE_FROM_REG(int reg)
255 {
256         return TEMP_RANGE[(reg >> 4) & 0x0f];
257 }
258
259 static int TEMP_RANGE_TO_REG(int val, int reg)
260 {
261         int i;
262
263         for (i = 15; i > 0; i--) {
264                 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
265                         break;
266                 }
267         }
268
269         return (reg & 0x0f) | (i << 4);
270 }
271
272 /* Temperature hysteresis
273  * Register layout:
274  *    reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
275  *    reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
276 static inline int TEMP_HYST_FROM_REG(int reg, int ix)
277 {
278         return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
279 }
280
281 static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
282 {
283         int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
284
285         return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
286 }
287
288 /* Fan input RPM */
289 static inline int FAN_FROM_REG(int reg, int tpc)
290 {
291         if (tpc) {
292                 return tpc * reg;
293         } else {
294                 return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
295         }
296 }
297
298 static inline int FAN_TO_REG(int val, int tpc)
299 {
300         if (tpc) {
301                 return SENSORS_LIMIT(val / tpc, 0, 0xffff);
302         } else {
303                 return (val <= 0) ? 0xffff :
304                         SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
305         }
306 }
307
308 /* Fan TPC (tach pulse count)
309  * Converts a register value to a TPC multiplier or returns 0 if the tachometer
310  * is configured in legacy (non-tpc) mode */
311 static inline int FAN_TPC_FROM_REG(int reg)
312 {
313         return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
314 }
315
316 /* Fan type
317  * The type of a fan is expressed in number of pulses-per-revolution that it
318  * emits */
319 static inline int FAN_TYPE_FROM_REG(int reg)
320 {
321         int edge = (reg >> 1) & 0x03;
322
323         return (edge > 0) ? 1 << (edge - 1) : 0;
324 }
325
326 static inline int FAN_TYPE_TO_REG(int val, int reg)
327 {
328         int edge = (val == 4) ? 3 : val;
329
330         return (reg & 0xf9) | (edge << 1);
331 }
332
333 /* Fan max RPM */
334 static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
335                               0x11, 0x0f, 0x0e};
336
337 static int FAN_MAX_FROM_REG(int reg)
338 {
339         int i;
340
341         for (i = 10; i > 0; i--) {
342                 if (reg == FAN_MAX[i]) {
343                         break;
344                 }
345         }
346
347         return 1000 + i * 500;
348 }
349
350 static int FAN_MAX_TO_REG(int val)
351 {
352         int i;
353
354         for (i = 10; i > 0; i--) {
355                 if (val > (1000 + (i - 1) * 500)) {
356                         break;
357                 }
358         }
359
360         return FAN_MAX[i];
361 }
362
363 /* PWM enable
364  * Register to enable mapping:
365  * 000:  2  fan on zone 1 auto
366  * 001:  2  fan on zone 2 auto
367  * 010:  2  fan on zone 3 auto
368  * 011:  0  fan full on
369  * 100: -1  fan disabled
370  * 101:  2  fan on hottest of zones 2,3 auto
371  * 110:  2  fan on hottest of zones 1,2,3 auto
372  * 111:  1  fan in manual mode */
373 static inline int PWM_EN_FROM_REG(int reg)
374 {
375         static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
376
377         return en[(reg >> 5) & 0x07];
378 }
379
380 static inline int PWM_EN_TO_REG(int val, int reg)
381 {
382         int en = (val == 1) ? 7 : 3;
383
384         return (reg & 0x1f) | ((en & 0x07) << 5);
385 }
386
387 /* PWM auto channels zone
388  * Register to auto channels zone mapping (ACZ is a bitfield with bit x
389  * corresponding to zone x+1):
390  * 000: 001  fan on zone 1 auto
391  * 001: 010  fan on zone 2 auto
392  * 010: 100  fan on zone 3 auto
393  * 011: 000  fan full on
394  * 100: 000  fan disabled
395  * 101: 110  fan on hottest of zones 2,3 auto
396  * 110: 111  fan on hottest of zones 1,2,3 auto
397  * 111: 000  fan in manual mode */
398 static inline int PWM_ACZ_FROM_REG(int reg)
399 {
400         static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
401
402         return acz[(reg >> 5) & 0x07];
403 }
404
405 static inline int PWM_ACZ_TO_REG(int val, int reg)
406 {
407         int acz = (val == 4) ? 2 : val - 1;
408
409         return (reg & 0x1f) | ((acz & 0x07) << 5);
410 }
411
412 /* PWM frequency */
413 static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
414                                15000, 20000, 30000, 25000, 0, 0, 0, 0};
415
416 static inline int PWM_FREQ_FROM_REG(int reg)
417 {
418         return PWM_FREQ[reg & 0x0f];
419 }
420
421 static int PWM_FREQ_TO_REG(int val, int reg)
422 {
423         int i;
424
425         /* the first two cases are special - stupid chip design! */
426         if (val > 27500) {
427                 i = 10;
428         } else if (val > 22500) {
429                 i = 11;
430         } else {
431                 for (i = 9; i > 0; i--) {
432                         if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
433                                 break;
434                         }
435                 }
436         }
437
438         return (reg & 0xf0) | i;
439 }
440
441 /* PWM ramp rate
442  * Register layout:
443  *    reg[0] = [OFF3,  OFF2,  OFF1,  RES,   RR1-E, RR1-2, RR1-1, RR1-0]
444  *    reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
445 static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
446
447 static inline int PWM_RR_FROM_REG(int reg, int ix)
448 {
449         int rr = (ix == 1) ? reg >> 4 : reg;
450
451         return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
452 }
453
454 static int PWM_RR_TO_REG(int val, int ix, int reg)
455 {
456         int i;
457
458         for (i = 0; i < 7; i++) {
459                 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
460                         break;
461                 }
462         }
463
464         return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
465 }
466
467 /* PWM ramp rate enable */
468 static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
469 {
470         return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
471 }
472
473 static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
474 {
475         int en = (ix == 1) ? 0x80 : 0x08;
476
477         return val ? reg | en : reg & ~en;
478 }
479
480 /* PWM min/off
481  * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
482  * the register layout). */
483 static inline int PWM_OFF_FROM_REG(int reg, int ix)
484 {
485         return (reg >> (ix + 5)) & 0x01;
486 }
487
488 static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
489 {
490         return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
491 }
492
493 /* ---------------------------------------------------------------------
494  * Device I/O access
495  *
496  * ISA access is performed through an index/data register pair and needs to
497  * be protected by a mutex during runtime (not required for initialization).
498  * We use data->update_lock for this and need to ensure that we acquire it
499  * before calling dme1737_read or dme1737_write.
500  * --------------------------------------------------------------------- */
501
502 static u8 dme1737_read(struct i2c_client *client, u8 reg)
503 {
504         s32 val;
505
506         if (client->driver) { /* I2C device */
507                 val = i2c_smbus_read_byte_data(client, reg);
508
509                 if (val < 0) {
510                         dev_warn(&client->dev, "Read from register "
511                                  "0x%02x failed! Please report to the driver "
512                                  "maintainer.\n", reg);
513                 }
514         } else { /* ISA device */
515                 outb(reg, client->addr);
516                 val = inb(client->addr + 1);
517         }
518
519         return val;
520 }
521
522 static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
523 {
524         s32 res = 0;
525
526         if (client->driver) { /* I2C device */
527                 res = i2c_smbus_write_byte_data(client, reg, val);
528
529                 if (res < 0) {
530                         dev_warn(&client->dev, "Write to register "
531                                  "0x%02x failed! Please report to the driver "
532                                  "maintainer.\n", reg);
533                 }
534         } else { /* ISA device */
535                 outb(reg, client->addr);
536                 outb(val, client->addr + 1);
537         }
538
539         return res;
540 }
541
542 static struct dme1737_data *dme1737_update_device(struct device *dev)
543 {
544         struct dme1737_data *data = dev_get_drvdata(dev);
545         struct i2c_client *client = &data->client;
546         int ix;
547         u8 lsb[5];
548
549         mutex_lock(&data->update_lock);
550
551         /* Enable a Vbat monitoring cycle every 10 mins */
552         if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
553                 dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
554                                                 DME1737_REG_CONFIG) | 0x10);
555                 data->last_vbat = jiffies;
556         }
557
558         /* Sample register contents every 1 sec */
559         if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
560                 data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
561
562                 /* In (voltage) registers */
563                 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
564                         /* Voltage inputs are stored as 16 bit values even
565                          * though they have only 12 bits resolution. This is
566                          * to make it consistent with the temp inputs. */
567                         data->in[ix] = dme1737_read(client,
568                                         DME1737_REG_IN(ix)) << 8;
569                         data->in_min[ix] = dme1737_read(client,
570                                         DME1737_REG_IN_MIN(ix));
571                         data->in_max[ix] = dme1737_read(client,
572                                         DME1737_REG_IN_MAX(ix));
573                 }
574
575                 /* Temp registers */
576                 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
577                         /* Temp inputs are stored as 16 bit values even
578                          * though they have only 12 bits resolution. This is
579                          * to take advantage of implicit conversions between
580                          * register values (2's complement) and temp values
581                          * (signed decimal). */
582                         data->temp[ix] = dme1737_read(client,
583                                         DME1737_REG_TEMP(ix)) << 8;
584                         data->temp_min[ix] = dme1737_read(client,
585                                         DME1737_REG_TEMP_MIN(ix));
586                         data->temp_max[ix] = dme1737_read(client,
587                                         DME1737_REG_TEMP_MAX(ix));
588                         data->temp_offset[ix] = dme1737_read(client,
589                                         DME1737_REG_TEMP_OFFSET(ix));
590                 }
591
592                 /* In and temp LSB registers
593                  * The LSBs are latched when the MSBs are read, so the order in
594                  * which the registers are read (MSB first, then LSB) is
595                  * important! */
596                 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
597                         lsb[ix] = dme1737_read(client,
598                                         DME1737_REG_IN_TEMP_LSB(ix));
599                 }
600                 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
601                         data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
602                                         DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
603                 }
604                 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
605                         data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
606                                         DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
607                 }
608
609                 /* Fan registers */
610                 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
611                         /* Skip reading registers if optional fans are not
612                          * present */
613                         if (!(data->has_fan & (1 << ix))) {
614                                 continue;
615                         }
616                         data->fan[ix] = dme1737_read(client,
617                                         DME1737_REG_FAN(ix));
618                         data->fan[ix] |= dme1737_read(client,
619                                         DME1737_REG_FAN(ix) + 1) << 8;
620                         data->fan_min[ix] = dme1737_read(client,
621                                         DME1737_REG_FAN_MIN(ix));
622                         data->fan_min[ix] |= dme1737_read(client,
623                                         DME1737_REG_FAN_MIN(ix) + 1) << 8;
624                         data->fan_opt[ix] = dme1737_read(client,
625                                         DME1737_REG_FAN_OPT(ix));
626                         /* fan_max exists only for fan[5-6] */
627                         if (ix > 3) {
628                                 data->fan_max[ix - 4] = dme1737_read(client,
629                                         DME1737_REG_FAN_MAX(ix));
630                         }
631                 }
632
633                 /* PWM registers */
634                 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
635                         /* Skip reading registers if optional PWMs are not
636                          * present */
637                         if (!(data->has_pwm & (1 << ix))) {
638                                 continue;
639                         }
640                         data->pwm[ix] = dme1737_read(client,
641                                         DME1737_REG_PWM(ix));
642                         data->pwm_freq[ix] = dme1737_read(client,
643                                         DME1737_REG_PWM_FREQ(ix));
644                         /* pwm_config and pwm_min exist only for pwm[1-3] */
645                         if (ix < 3) {
646                                 data->pwm_config[ix] = dme1737_read(client,
647                                                 DME1737_REG_PWM_CONFIG(ix));
648                                 data->pwm_min[ix] = dme1737_read(client,
649                                                 DME1737_REG_PWM_MIN(ix));
650                         }
651                 }
652                 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
653                         data->pwm_rr[ix] = dme1737_read(client,
654                                                 DME1737_REG_PWM_RR(ix));
655                 }
656
657                 /* Thermal zone registers */
658                 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
659                         data->zone_low[ix] = dme1737_read(client,
660                                         DME1737_REG_ZONE_LOW(ix));
661                         data->zone_abs[ix] = dme1737_read(client,
662                                         DME1737_REG_ZONE_ABS(ix));
663                 }
664                 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
665                         data->zone_hyst[ix] = dme1737_read(client,
666                                                 DME1737_REG_ZONE_HYST(ix));
667                 }
668
669                 /* Alarm registers */
670                 data->alarms = dme1737_read(client,
671                                                 DME1737_REG_ALARM1);
672                 /* Bit 7 tells us if the other alarm registers are non-zero and
673                  * therefore also need to be read */
674                 if (data->alarms & 0x80) {
675                         data->alarms |= dme1737_read(client,
676                                                 DME1737_REG_ALARM2) << 8;
677                         data->alarms |= dme1737_read(client,
678                                                 DME1737_REG_ALARM3) << 16;
679                 }
680
681                 /* The ISA chips require explicit clearing of alarm bits.
682                  * Don't worry, an alarm will come back if the condition
683                  * that causes it still exists */
684                 if (!client->driver) {
685                         if (data->alarms & 0xff0000) {
686                                 dme1737_write(client, DME1737_REG_ALARM3,
687                                               0xff);
688                         }
689                         if (data->alarms & 0xff00) {
690                                 dme1737_write(client, DME1737_REG_ALARM2,
691                                               0xff);
692                         }
693                         if (data->alarms & 0xff) {
694                                 dme1737_write(client, DME1737_REG_ALARM1,
695                                               0xff);
696                         }
697                 }
698
699                 data->last_update = jiffies;
700                 data->valid = 1;
701         }
702
703         mutex_unlock(&data->update_lock);
704
705         return data;
706 }
707
708 /* ---------------------------------------------------------------------
709  * Voltage sysfs attributes
710  * ix = [0-5]
711  * --------------------------------------------------------------------- */
712
713 #define SYS_IN_INPUT    0
714 #define SYS_IN_MIN      1
715 #define SYS_IN_MAX      2
716 #define SYS_IN_ALARM    3
717
718 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
719                        char *buf)
720 {
721         struct dme1737_data *data = dme1737_update_device(dev);
722         struct sensor_device_attribute_2
723                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
724         int ix = sensor_attr_2->index;
725         int fn = sensor_attr_2->nr;
726         int res;
727
728         switch (fn) {
729         case SYS_IN_INPUT:
730                 res = IN_FROM_REG(data->in[ix], ix, 16);
731                 break;
732         case SYS_IN_MIN:
733                 res = IN_FROM_REG(data->in_min[ix], ix, 8);
734                 break;
735         case SYS_IN_MAX:
736                 res = IN_FROM_REG(data->in_max[ix], ix, 8);
737                 break;
738         case SYS_IN_ALARM:
739                 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
740                 break;
741         default:
742                 res = 0;
743                 dev_dbg(dev, "Unknown function %d.\n", fn);
744         }
745
746         return sprintf(buf, "%d\n", res);
747 }
748
749 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
750                       const char *buf, size_t count)
751 {
752         struct dme1737_data *data = dev_get_drvdata(dev);
753         struct i2c_client *client = &data->client;
754         struct sensor_device_attribute_2
755                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
756         int ix = sensor_attr_2->index;
757         int fn = sensor_attr_2->nr;
758         long val = simple_strtol(buf, NULL, 10);
759
760         mutex_lock(&data->update_lock);
761         switch (fn) {
762         case SYS_IN_MIN:
763                 data->in_min[ix] = IN_TO_REG(val, ix);
764                 dme1737_write(client, DME1737_REG_IN_MIN(ix),
765                               data->in_min[ix]);
766                 break;
767         case SYS_IN_MAX:
768                 data->in_max[ix] = IN_TO_REG(val, ix);
769                 dme1737_write(client, DME1737_REG_IN_MAX(ix),
770                               data->in_max[ix]);
771                 break;
772         default:
773                 dev_dbg(dev, "Unknown function %d.\n", fn);
774         }
775         mutex_unlock(&data->update_lock);
776
777         return count;
778 }
779
780 /* ---------------------------------------------------------------------
781  * Temperature sysfs attributes
782  * ix = [0-2]
783  * --------------------------------------------------------------------- */
784
785 #define SYS_TEMP_INPUT                  0
786 #define SYS_TEMP_MIN                    1
787 #define SYS_TEMP_MAX                    2
788 #define SYS_TEMP_OFFSET                 3
789 #define SYS_TEMP_ALARM                  4
790 #define SYS_TEMP_FAULT                  5
791
792 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
793                          char *buf)
794 {
795         struct dme1737_data *data = dme1737_update_device(dev);
796         struct sensor_device_attribute_2
797                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
798         int ix = sensor_attr_2->index;
799         int fn = sensor_attr_2->nr;
800         int res;
801
802         switch (fn) {
803         case SYS_TEMP_INPUT:
804                 res = TEMP_FROM_REG(data->temp[ix], 16);
805                 break;
806         case SYS_TEMP_MIN:
807                 res = TEMP_FROM_REG(data->temp_min[ix], 8);
808                 break;
809         case SYS_TEMP_MAX:
810                 res = TEMP_FROM_REG(data->temp_max[ix], 8);
811                 break;
812         case SYS_TEMP_OFFSET:
813                 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
814                 break;
815         case SYS_TEMP_ALARM:
816                 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
817                 break;
818         case SYS_TEMP_FAULT:
819                 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
820                 break;
821         default:
822                 res = 0;
823                 dev_dbg(dev, "Unknown function %d.\n", fn);
824         }
825
826         return sprintf(buf, "%d\n", res);
827 }
828
829 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
830                         const char *buf, size_t count)
831 {
832         struct dme1737_data *data = dev_get_drvdata(dev);
833         struct i2c_client *client = &data->client;
834         struct sensor_device_attribute_2
835                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
836         int ix = sensor_attr_2->index;
837         int fn = sensor_attr_2->nr;
838         long val = simple_strtol(buf, NULL, 10);
839
840         mutex_lock(&data->update_lock);
841         switch (fn) {
842         case SYS_TEMP_MIN:
843                 data->temp_min[ix] = TEMP_TO_REG(val);
844                 dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
845                               data->temp_min[ix]);
846                 break;
847         case SYS_TEMP_MAX:
848                 data->temp_max[ix] = TEMP_TO_REG(val);
849                 dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
850                               data->temp_max[ix]);
851                 break;
852         case SYS_TEMP_OFFSET:
853                 data->temp_offset[ix] = TEMP_TO_REG(val);
854                 dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
855                               data->temp_offset[ix]);
856                 break;
857         default:
858                 dev_dbg(dev, "Unknown function %d.\n", fn);
859         }
860         mutex_unlock(&data->update_lock);
861
862         return count;
863 }
864
865 /* ---------------------------------------------------------------------
866  * Zone sysfs attributes
867  * ix = [0-2]
868  * --------------------------------------------------------------------- */
869
870 #define SYS_ZONE_AUTO_CHANNELS_TEMP     0
871 #define SYS_ZONE_AUTO_POINT1_TEMP_HYST  1
872 #define SYS_ZONE_AUTO_POINT1_TEMP       2
873 #define SYS_ZONE_AUTO_POINT2_TEMP       3
874 #define SYS_ZONE_AUTO_POINT3_TEMP       4
875
876 static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
877                          char *buf)
878 {
879         struct dme1737_data *data = dme1737_update_device(dev);
880         struct sensor_device_attribute_2
881                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
882         int ix = sensor_attr_2->index;
883         int fn = sensor_attr_2->nr;
884         int res;
885
886         switch (fn) {
887         case SYS_ZONE_AUTO_CHANNELS_TEMP:
888                 /* check config2 for non-standard temp-to-zone mapping */
889                 if ((ix == 1) && (data->config2 & 0x02)) {
890                         res = 4;
891                 } else {
892                         res = 1 << ix;
893                 }
894                 break;
895         case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
896                 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
897                       TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
898                 break;
899         case SYS_ZONE_AUTO_POINT1_TEMP:
900                 res = TEMP_FROM_REG(data->zone_low[ix], 8);
901                 break;
902         case SYS_ZONE_AUTO_POINT2_TEMP:
903                 /* pwm_freq holds the temp range bits in the upper nibble */
904                 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
905                       TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
906                 break;
907         case SYS_ZONE_AUTO_POINT3_TEMP:
908                 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
909                 break;
910         default:
911                 res = 0;
912                 dev_dbg(dev, "Unknown function %d.\n", fn);
913         }
914
915         return sprintf(buf, "%d\n", res);
916 }
917
918 static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
919                         const char *buf, size_t count)
920 {
921         struct dme1737_data *data = dev_get_drvdata(dev);
922         struct i2c_client *client = &data->client;
923         struct sensor_device_attribute_2
924                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
925         int ix = sensor_attr_2->index;
926         int fn = sensor_attr_2->nr;
927         long val = simple_strtol(buf, NULL, 10);
928
929         mutex_lock(&data->update_lock);
930         switch (fn) {
931         case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
932                 /* Refresh the cache */
933                 data->zone_low[ix] = dme1737_read(client,
934                                                   DME1737_REG_ZONE_LOW(ix));
935                 /* Modify the temp hyst value */
936                 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
937                                         TEMP_FROM_REG(data->zone_low[ix], 8) -
938                                         val, ix, dme1737_read(client,
939                                         DME1737_REG_ZONE_HYST(ix == 2)));
940                 dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
941                               data->zone_hyst[ix == 2]);
942                 break;
943         case SYS_ZONE_AUTO_POINT1_TEMP:
944                 data->zone_low[ix] = TEMP_TO_REG(val);
945                 dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
946                               data->zone_low[ix]);
947                 break;
948         case SYS_ZONE_AUTO_POINT2_TEMP:
949                 /* Refresh the cache */
950                 data->zone_low[ix] = dme1737_read(client,
951                                                   DME1737_REG_ZONE_LOW(ix));
952                 /* Modify the temp range value (which is stored in the upper
953                  * nibble of the pwm_freq register) */
954                 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
955                                         TEMP_FROM_REG(data->zone_low[ix], 8),
956                                         dme1737_read(client,
957                                         DME1737_REG_PWM_FREQ(ix)));
958                 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
959                               data->pwm_freq[ix]);
960                 break;
961         case SYS_ZONE_AUTO_POINT3_TEMP:
962                 data->zone_abs[ix] = TEMP_TO_REG(val);
963                 dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
964                               data->zone_abs[ix]);
965                 break;
966         default:
967                 dev_dbg(dev, "Unknown function %d.\n", fn);
968         }
969         mutex_unlock(&data->update_lock);
970
971         return count;
972 }
973
974 /* ---------------------------------------------------------------------
975  * Fan sysfs attributes
976  * ix = [0-5]
977  * --------------------------------------------------------------------- */
978
979 #define SYS_FAN_INPUT   0
980 #define SYS_FAN_MIN     1
981 #define SYS_FAN_MAX     2
982 #define SYS_FAN_ALARM   3
983 #define SYS_FAN_TYPE    4
984
985 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
986                         char *buf)
987 {
988         struct dme1737_data *data = dme1737_update_device(dev);
989         struct sensor_device_attribute_2
990                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
991         int ix = sensor_attr_2->index;
992         int fn = sensor_attr_2->nr;
993         int res;
994
995         switch (fn) {
996         case SYS_FAN_INPUT:
997                 res = FAN_FROM_REG(data->fan[ix],
998                                    ix < 4 ? 0 :
999                                    FAN_TPC_FROM_REG(data->fan_opt[ix]));
1000                 break;
1001         case SYS_FAN_MIN:
1002                 res = FAN_FROM_REG(data->fan_min[ix],
1003                                    ix < 4 ? 0 :
1004                                    FAN_TPC_FROM_REG(data->fan_opt[ix]));
1005                 break;
1006         case SYS_FAN_MAX:
1007                 /* only valid for fan[5-6] */
1008                 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1009                 break;
1010         case SYS_FAN_ALARM:
1011                 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1012                 break;
1013         case SYS_FAN_TYPE:
1014                 /* only valid for fan[1-4] */
1015                 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1016                 break;
1017         default:
1018                 res = 0;
1019                 dev_dbg(dev, "Unknown function %d.\n", fn);
1020         }
1021
1022         return sprintf(buf, "%d\n", res);
1023 }
1024
1025 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1026                        const char *buf, size_t count)
1027 {
1028         struct dme1737_data *data = dev_get_drvdata(dev);
1029         struct i2c_client *client = &data->client;
1030         struct sensor_device_attribute_2
1031                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1032         int ix = sensor_attr_2->index;
1033         int fn = sensor_attr_2->nr;
1034         long val = simple_strtol(buf, NULL, 10);
1035
1036         mutex_lock(&data->update_lock);
1037         switch (fn) {
1038         case SYS_FAN_MIN:
1039                 if (ix < 4) {
1040                         data->fan_min[ix] = FAN_TO_REG(val, 0);
1041                 } else {
1042                         /* Refresh the cache */
1043                         data->fan_opt[ix] = dme1737_read(client,
1044                                                 DME1737_REG_FAN_OPT(ix));
1045                         /* Modify the fan min value */
1046                         data->fan_min[ix] = FAN_TO_REG(val,
1047                                         FAN_TPC_FROM_REG(data->fan_opt[ix]));
1048                 }
1049                 dme1737_write(client, DME1737_REG_FAN_MIN(ix),
1050                               data->fan_min[ix] & 0xff);
1051                 dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
1052                               data->fan_min[ix] >> 8);
1053                 break;
1054         case SYS_FAN_MAX:
1055                 /* Only valid for fan[5-6] */
1056                 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1057                 dme1737_write(client, DME1737_REG_FAN_MAX(ix),
1058                               data->fan_max[ix - 4]);
1059                 break;
1060         case SYS_FAN_TYPE:
1061                 /* Only valid for fan[1-4] */
1062                 if (!(val == 1 || val == 2 || val == 4)) {
1063                         count = -EINVAL;
1064                         dev_warn(dev, "Fan type value %ld not "
1065                                  "supported. Choose one of 1, 2, or 4.\n",
1066                                  val);
1067                         goto exit;
1068                 }
1069                 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
1070                                         DME1737_REG_FAN_OPT(ix)));
1071                 dme1737_write(client, DME1737_REG_FAN_OPT(ix),
1072                               data->fan_opt[ix]);
1073                 break;
1074         default:
1075                 dev_dbg(dev, "Unknown function %d.\n", fn);
1076         }
1077 exit:
1078         mutex_unlock(&data->update_lock);
1079
1080         return count;
1081 }
1082
1083 /* ---------------------------------------------------------------------
1084  * PWM sysfs attributes
1085  * ix = [0-4]
1086  * --------------------------------------------------------------------- */
1087
1088 #define SYS_PWM                         0
1089 #define SYS_PWM_FREQ                    1
1090 #define SYS_PWM_ENABLE                  2
1091 #define SYS_PWM_RAMP_RATE               3
1092 #define SYS_PWM_AUTO_CHANNELS_ZONE      4
1093 #define SYS_PWM_AUTO_PWM_MIN            5
1094 #define SYS_PWM_AUTO_POINT1_PWM         6
1095 #define SYS_PWM_AUTO_POINT2_PWM         7
1096
1097 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1098                         char *buf)
1099 {
1100         struct dme1737_data *data = dme1737_update_device(dev);
1101         struct sensor_device_attribute_2
1102                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1103         int ix = sensor_attr_2->index;
1104         int fn = sensor_attr_2->nr;
1105         int res;
1106
1107         switch (fn) {
1108         case SYS_PWM:
1109                 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1110                         res = 255;
1111                 } else {
1112                         res = data->pwm[ix];
1113                 }
1114                 break;
1115         case SYS_PWM_FREQ:
1116                 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1117                 break;
1118         case SYS_PWM_ENABLE:
1119                 if (ix > 3) {
1120                         res = 1; /* pwm[5-6] hard-wired to manual mode */
1121                 } else {
1122                         res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1123                 }
1124                 break;
1125         case SYS_PWM_RAMP_RATE:
1126                 /* Only valid for pwm[1-3] */
1127                 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1128                 break;
1129         case SYS_PWM_AUTO_CHANNELS_ZONE:
1130                 /* Only valid for pwm[1-3] */
1131                 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1132                         res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1133                 } else {
1134                         res = data->pwm_acz[ix];
1135                 }
1136                 break;
1137         case SYS_PWM_AUTO_PWM_MIN:
1138                 /* Only valid for pwm[1-3] */
1139                 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1140                         res = data->pwm_min[ix];
1141                 } else {
1142                         res = 0;
1143                 }
1144                 break;
1145         case SYS_PWM_AUTO_POINT1_PWM:
1146                 /* Only valid for pwm[1-3] */
1147                 res = data->pwm_min[ix];
1148                 break;
1149         case SYS_PWM_AUTO_POINT2_PWM:
1150                 /* Only valid for pwm[1-3] */
1151                 res = 255; /* hard-wired */
1152                 break;
1153         default:
1154                 res = 0;
1155                 dev_dbg(dev, "Unknown function %d.\n", fn);
1156         }
1157
1158         return sprintf(buf, "%d\n", res);
1159 }
1160
1161 static struct attribute *dme1737_attr_pwm[];
1162 static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
1163
1164 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1165                        const char *buf, size_t count)
1166 {
1167         struct dme1737_data *data = dev_get_drvdata(dev);
1168         struct i2c_client *client = &data->client;
1169         struct sensor_device_attribute_2
1170                 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1171         int ix = sensor_attr_2->index;
1172         int fn = sensor_attr_2->nr;
1173         long val = simple_strtol(buf, NULL, 10);
1174
1175         mutex_lock(&data->update_lock);
1176         switch (fn) {
1177         case SYS_PWM:
1178                 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1179                 dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
1180                 break;
1181         case SYS_PWM_FREQ:
1182                 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
1183                                                 DME1737_REG_PWM_FREQ(ix)));
1184                 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
1185                               data->pwm_freq[ix]);
1186                 break;
1187         case SYS_PWM_ENABLE:
1188                 /* Only valid for pwm[1-3] */
1189                 if (val < 0 || val > 2) {
1190                         count = -EINVAL;
1191                         dev_warn(dev, "PWM enable %ld not "
1192                                  "supported. Choose one of 0, 1, or 2.\n",
1193                                  val);
1194                         goto exit;
1195                 }
1196                 /* Refresh the cache */
1197                 data->pwm_config[ix] = dme1737_read(client,
1198                                                 DME1737_REG_PWM_CONFIG(ix));
1199                 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1200                         /* Bail out if no change */
1201                         goto exit;
1202                 }
1203                 /* Do some housekeeping if we are currently in auto mode */
1204                 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1205                         /* Save the current zone channel assignment */
1206                         data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1207                                                         data->pwm_config[ix]);
1208                         /* Save the current ramp rate state and disable it */
1209                         data->pwm_rr[ix > 0] = dme1737_read(client,
1210                                                 DME1737_REG_PWM_RR(ix > 0));
1211                         data->pwm_rr_en &= ~(1 << ix);
1212                         if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1213                                 data->pwm_rr_en |= (1 << ix);
1214                                 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1215                                                         data->pwm_rr[ix > 0]);
1216                                 dme1737_write(client,
1217                                               DME1737_REG_PWM_RR(ix > 0),
1218                                               data->pwm_rr[ix > 0]);
1219                         }
1220                 }
1221                 /* Set the new PWM mode */
1222                 switch (val) {
1223                 case 0:
1224                         /* Change permissions of pwm[ix] to read-only */
1225                         dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1226                                            S_IRUGO);
1227                         /* Turn fan fully on */
1228                         data->pwm_config[ix] = PWM_EN_TO_REG(0,
1229                                                         data->pwm_config[ix]);
1230                         dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1231                                       data->pwm_config[ix]);
1232                         break;
1233                 case 1:
1234                         /* Turn on manual mode */
1235                         data->pwm_config[ix] = PWM_EN_TO_REG(1,
1236                                                         data->pwm_config[ix]);
1237                         dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1238                                       data->pwm_config[ix]);
1239                         /* Change permissions of pwm[ix] to read-writeable */
1240                         dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1241                                            S_IRUGO | S_IWUSR);
1242                         break;
1243                 case 2:
1244                         /* Change permissions of pwm[ix] to read-only */
1245                         dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1246                                            S_IRUGO);
1247                         /* Turn on auto mode using the saved zone channel
1248                          * assignment */
1249                         data->pwm_config[ix] = PWM_ACZ_TO_REG(
1250                                                         data->pwm_acz[ix],
1251                                                         data->pwm_config[ix]);
1252                         dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1253                                       data->pwm_config[ix]);
1254                         /* Enable PWM ramp rate if previously enabled */
1255                         if (data->pwm_rr_en & (1 << ix)) {
1256                                 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1257                                                 dme1737_read(client,
1258                                                 DME1737_REG_PWM_RR(ix > 0)));
1259                                 dme1737_write(client,
1260                                               DME1737_REG_PWM_RR(ix > 0),
1261                                               data->pwm_rr[ix > 0]);
1262                         }
1263                         break;
1264                 }
1265                 break;
1266         case SYS_PWM_RAMP_RATE:
1267                 /* Only valid for pwm[1-3] */
1268                 /* Refresh the cache */
1269                 data->pwm_config[ix] = dme1737_read(client,
1270                                                 DME1737_REG_PWM_CONFIG(ix));
1271                 data->pwm_rr[ix > 0] = dme1737_read(client,
1272                                                 DME1737_REG_PWM_RR(ix > 0));
1273                 /* Set the ramp rate value */
1274                 if (val > 0) {
1275                         data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1276                                                         data->pwm_rr[ix > 0]);
1277                 }
1278                 /* Enable/disable the feature only if the associated PWM
1279                  * output is in automatic mode. */
1280                 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1281                         data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1282                                                         data->pwm_rr[ix > 0]);
1283                 }
1284                 dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
1285                               data->pwm_rr[ix > 0]);
1286                 break;
1287         case SYS_PWM_AUTO_CHANNELS_ZONE:
1288                 /* Only valid for pwm[1-3] */
1289                 if (!(val == 1 || val == 2 || val == 4 ||
1290                       val == 6 || val == 7)) {
1291                         count = -EINVAL;
1292                         dev_warn(dev, "PWM auto channels zone %ld "
1293                                  "not supported. Choose one of 1, 2, 4, 6, "
1294                                  "or 7.\n", val);
1295                         goto exit;
1296                 }
1297                 /* Refresh the cache */
1298                 data->pwm_config[ix] = dme1737_read(client,
1299                                                 DME1737_REG_PWM_CONFIG(ix));
1300                 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1301                         /* PWM is already in auto mode so update the temp
1302                          * channel assignment */
1303                         data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1304                                                 data->pwm_config[ix]);
1305                         dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1306                                       data->pwm_config[ix]);
1307                 } else {
1308                         /* PWM is not in auto mode so we save the temp
1309                          * channel assignment for later use */
1310                         data->pwm_acz[ix] = val;
1311                 }
1312                 break;
1313         case SYS_PWM_AUTO_PWM_MIN:
1314                 /* Only valid for pwm[1-3] */
1315                 /* Refresh the cache */
1316                 data->pwm_min[ix] = dme1737_read(client,
1317                                                 DME1737_REG_PWM_MIN(ix));
1318                 /* There are only 2 values supported for the auto_pwm_min
1319                  * value: 0 or auto_point1_pwm. So if the temperature drops
1320                  * below the auto_point1_temp_hyst value, the fan either turns
1321                  * off or runs at auto_point1_pwm duty-cycle. */
1322                 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1323                         data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1324                                                 dme1737_read(client,
1325                                                 DME1737_REG_PWM_RR(0)));
1326                 } else {
1327                         data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1328                                                 dme1737_read(client,
1329                                                 DME1737_REG_PWM_RR(0)));
1330                 }
1331                 dme1737_write(client, DME1737_REG_PWM_RR(0),
1332                               data->pwm_rr[0]);
1333                 break;
1334         case SYS_PWM_AUTO_POINT1_PWM:
1335                 /* Only valid for pwm[1-3] */
1336                 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1337                 dme1737_write(client, DME1737_REG_PWM_MIN(ix),
1338                               data->pwm_min[ix]);
1339                 break;
1340         default:
1341                 dev_dbg(dev, "Unknown function %d.\n", fn);
1342         }
1343 exit:
1344         mutex_unlock(&data->update_lock);
1345
1346         return count;
1347 }
1348
1349 /* ---------------------------------------------------------------------
1350  * Miscellaneous sysfs attributes
1351  * --------------------------------------------------------------------- */
1352
1353 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1354                         char *buf)
1355 {
1356         struct i2c_client *client = to_i2c_client(dev);
1357         struct dme1737_data *data = i2c_get_clientdata(client);
1358
1359         return sprintf(buf, "%d\n", data->vrm);
1360 }
1361
1362 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1363                        const char *buf, size_t count)
1364 {
1365         struct dme1737_data *data = dev_get_drvdata(dev);
1366         long val = simple_strtol(buf, NULL, 10);
1367
1368         data->vrm = val;
1369         return count;
1370 }
1371
1372 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1373                         char *buf)
1374 {
1375         struct dme1737_data *data = dme1737_update_device(dev);
1376
1377         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1378 }
1379
1380 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1381                          char *buf)
1382 {
1383         struct dme1737_data *data = dev_get_drvdata(dev);
1384
1385         return sprintf(buf, "%s\n", data->client.name);
1386 }
1387
1388 /* ---------------------------------------------------------------------
1389  * Sysfs device attribute defines and structs
1390  * --------------------------------------------------------------------- */
1391
1392 /* Voltages 0-6 */
1393
1394 #define SENSOR_DEVICE_ATTR_IN(ix) \
1395 static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
1396         show_in, NULL, SYS_IN_INPUT, ix); \
1397 static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
1398         show_in, set_in, SYS_IN_MIN, ix); \
1399 static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
1400         show_in, set_in, SYS_IN_MAX, ix); \
1401 static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
1402         show_in, NULL, SYS_IN_ALARM, ix)
1403
1404 SENSOR_DEVICE_ATTR_IN(0);
1405 SENSOR_DEVICE_ATTR_IN(1);
1406 SENSOR_DEVICE_ATTR_IN(2);
1407 SENSOR_DEVICE_ATTR_IN(3);
1408 SENSOR_DEVICE_ATTR_IN(4);
1409 SENSOR_DEVICE_ATTR_IN(5);
1410 SENSOR_DEVICE_ATTR_IN(6);
1411
1412 /* Temperatures 1-3 */
1413
1414 #define SENSOR_DEVICE_ATTR_TEMP(ix) \
1415 static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
1416         show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
1417 static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
1418         show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
1419 static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
1420         show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
1421 static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
1422         show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
1423 static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
1424         show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
1425 static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
1426         show_temp, NULL, SYS_TEMP_FAULT, ix-1)
1427
1428 SENSOR_DEVICE_ATTR_TEMP(1);
1429 SENSOR_DEVICE_ATTR_TEMP(2);
1430 SENSOR_DEVICE_ATTR_TEMP(3);
1431
1432 /* Zones 1-3 */
1433
1434 #define SENSOR_DEVICE_ATTR_ZONE(ix) \
1435 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
1436         show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
1437 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
1438         show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
1439 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
1440         show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
1441 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
1442         show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
1443 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
1444         show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
1445
1446 SENSOR_DEVICE_ATTR_ZONE(1);
1447 SENSOR_DEVICE_ATTR_ZONE(2);
1448 SENSOR_DEVICE_ATTR_ZONE(3);
1449
1450 /* Fans 1-4 */
1451
1452 #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1453 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1454         show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1455 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1456         show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1457 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1458         show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1459 static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
1460         show_fan, set_fan, SYS_FAN_TYPE, ix-1)
1461
1462 SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1463 SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1464 SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1465 SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1466
1467 /* Fans 5-6 */
1468
1469 #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1470 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1471         show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1472 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1473         show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1474 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1475         show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1476 static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
1477         show_fan, set_fan, SYS_FAN_MAX, ix-1)
1478
1479 SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1480 SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1481
1482 /* PWMs 1-3 */
1483
1484 #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1485 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1486         show_pwm, set_pwm, SYS_PWM, ix-1); \
1487 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1488         show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1489 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1490         show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
1491 static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
1492         show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
1493 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
1494         show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
1495 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
1496         show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
1497 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
1498         show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
1499 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
1500         show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
1501
1502 SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1503 SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1504 SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1505
1506 /* PWMs 5-6 */
1507
1508 #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
1509 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1510         show_pwm, set_pwm, SYS_PWM, ix-1); \
1511 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1512         show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1513 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1514         show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
1515
1516 SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1517 SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1518
1519 /* Misc */
1520
1521 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1522 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1523 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);   /* for ISA devices */
1524
1525 /* This struct holds all the attributes that are always present and need to be
1526  * created unconditionally. The attributes that need modification of their
1527  * permissions are created read-only and write permissions are added or removed
1528  * on the fly when required */
1529 static struct attribute *dme1737_attr[] ={
1530         /* Voltages */
1531         &sensor_dev_attr_in0_input.dev_attr.attr,
1532         &sensor_dev_attr_in0_min.dev_attr.attr,
1533         &sensor_dev_attr_in0_max.dev_attr.attr,
1534         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1535         &sensor_dev_attr_in1_input.dev_attr.attr,
1536         &sensor_dev_attr_in1_min.dev_attr.attr,
1537         &sensor_dev_attr_in1_max.dev_attr.attr,
1538         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1539         &sensor_dev_attr_in2_input.dev_attr.attr,
1540         &sensor_dev_attr_in2_min.dev_attr.attr,
1541         &sensor_dev_attr_in2_max.dev_attr.attr,
1542         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1543         &sensor_dev_attr_in3_input.dev_attr.attr,
1544         &sensor_dev_attr_in3_min.dev_attr.attr,
1545         &sensor_dev_attr_in3_max.dev_attr.attr,
1546         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1547         &sensor_dev_attr_in4_input.dev_attr.attr,
1548         &sensor_dev_attr_in4_min.dev_attr.attr,
1549         &sensor_dev_attr_in4_max.dev_attr.attr,
1550         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1551         &sensor_dev_attr_in5_input.dev_attr.attr,
1552         &sensor_dev_attr_in5_min.dev_attr.attr,
1553         &sensor_dev_attr_in5_max.dev_attr.attr,
1554         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1555         &sensor_dev_attr_in6_input.dev_attr.attr,
1556         &sensor_dev_attr_in6_min.dev_attr.attr,
1557         &sensor_dev_attr_in6_max.dev_attr.attr,
1558         &sensor_dev_attr_in6_alarm.dev_attr.attr,
1559         /* Temperatures */
1560         &sensor_dev_attr_temp1_input.dev_attr.attr,
1561         &sensor_dev_attr_temp1_min.dev_attr.attr,
1562         &sensor_dev_attr_temp1_max.dev_attr.attr,
1563         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1564         &sensor_dev_attr_temp1_fault.dev_attr.attr,
1565         &sensor_dev_attr_temp1_offset.dev_attr.attr,
1566         &sensor_dev_attr_temp2_input.dev_attr.attr,
1567         &sensor_dev_attr_temp2_min.dev_attr.attr,
1568         &sensor_dev_attr_temp2_max.dev_attr.attr,
1569         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1570         &sensor_dev_attr_temp2_fault.dev_attr.attr,
1571         &sensor_dev_attr_temp2_offset.dev_attr.attr,
1572         &sensor_dev_attr_temp3_input.dev_attr.attr,
1573         &sensor_dev_attr_temp3_min.dev_attr.attr,
1574         &sensor_dev_attr_temp3_max.dev_attr.attr,
1575         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1576         &sensor_dev_attr_temp3_fault.dev_attr.attr,
1577         &sensor_dev_attr_temp3_offset.dev_attr.attr,
1578         /* Zones */
1579         &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1580         &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1581         &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1582         &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1583         &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
1584         &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1585         &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1586         &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1587         &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1588         &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
1589         &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1590         &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1591         &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1592         &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1593         &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
1594         /* Misc */
1595         &dev_attr_vrm.attr,
1596         &dev_attr_cpu0_vid.attr,
1597         NULL
1598 };
1599
1600 static const struct attribute_group dme1737_group = {
1601         .attrs = dme1737_attr,
1602 };
1603
1604 /* The following structs hold the PWM attributes, some of which are optional.
1605  * Their creation depends on the chip configuration which is determined during
1606  * module load. */
1607 static struct attribute *dme1737_attr_pwm1[] = {
1608         &sensor_dev_attr_pwm1.dev_attr.attr,
1609         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1610         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1611         &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1612         &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1613         &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1614         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1615         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1616         NULL
1617 };
1618 static struct attribute *dme1737_attr_pwm2[] = {
1619         &sensor_dev_attr_pwm2.dev_attr.attr,
1620         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1621         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1622         &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1623         &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1624         &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1625         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1626         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1627         NULL
1628 };
1629 static struct attribute *dme1737_attr_pwm3[] = {
1630         &sensor_dev_attr_pwm3.dev_attr.attr,
1631         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1632         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1633         &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1634         &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1635         &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1636         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1637         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1638         NULL
1639 };
1640 static struct attribute *dme1737_attr_pwm5[] = {
1641         &sensor_dev_attr_pwm5.dev_attr.attr,
1642         &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1643         &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1644         NULL
1645 };
1646 static struct attribute *dme1737_attr_pwm6[] = {
1647         &sensor_dev_attr_pwm6.dev_attr.attr,
1648         &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1649         &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1650         NULL
1651 };
1652
1653 static const struct attribute_group dme1737_pwm_group[] = {
1654         { .attrs = dme1737_attr_pwm1 },
1655         { .attrs = dme1737_attr_pwm2 },
1656         { .attrs = dme1737_attr_pwm3 },
1657         { .attrs = NULL },
1658         { .attrs = dme1737_attr_pwm5 },
1659         { .attrs = dme1737_attr_pwm6 },
1660 };
1661
1662 /* The following structs hold the fan attributes, some of which are optional.
1663  * Their creation depends on the chip configuration which is determined during
1664  * module load. */
1665 static struct attribute *dme1737_attr_fan1[] = {
1666         &sensor_dev_attr_fan1_input.dev_attr.attr,
1667         &sensor_dev_attr_fan1_min.dev_attr.attr,
1668         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1669         &sensor_dev_attr_fan1_type.dev_attr.attr,
1670         NULL
1671 };
1672 static struct attribute *dme1737_attr_fan2[] = {
1673         &sensor_dev_attr_fan2_input.dev_attr.attr,
1674         &sensor_dev_attr_fan2_min.dev_attr.attr,
1675         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1676         &sensor_dev_attr_fan2_type.dev_attr.attr,
1677         NULL
1678 };
1679 static struct attribute *dme1737_attr_fan3[] = {
1680         &sensor_dev_attr_fan3_input.dev_attr.attr,
1681         &sensor_dev_attr_fan3_min.dev_attr.attr,
1682         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1683         &sensor_dev_attr_fan3_type.dev_attr.attr,
1684         NULL
1685 };
1686 static struct attribute *dme1737_attr_fan4[] = {
1687         &sensor_dev_attr_fan4_input.dev_attr.attr,
1688         &sensor_dev_attr_fan4_min.dev_attr.attr,
1689         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1690         &sensor_dev_attr_fan4_type.dev_attr.attr,
1691         NULL
1692 };
1693 static struct attribute *dme1737_attr_fan5[] = {
1694         &sensor_dev_attr_fan5_input.dev_attr.attr,
1695         &sensor_dev_attr_fan5_min.dev_attr.attr,
1696         &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1697         &sensor_dev_attr_fan5_max.dev_attr.attr,
1698         NULL
1699 };
1700 static struct attribute *dme1737_attr_fan6[] = {
1701         &sensor_dev_attr_fan6_input.dev_attr.attr,
1702         &sensor_dev_attr_fan6_min.dev_attr.attr,
1703         &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1704         &sensor_dev_attr_fan6_max.dev_attr.attr,
1705         NULL
1706 };
1707
1708 static const struct attribute_group dme1737_fan_group[] = {
1709         { .attrs = dme1737_attr_fan1 },
1710         { .attrs = dme1737_attr_fan2 },
1711         { .attrs = dme1737_attr_fan3 },
1712         { .attrs = dme1737_attr_fan4 },
1713         { .attrs = dme1737_attr_fan5 },
1714         { .attrs = dme1737_attr_fan6 },
1715 };
1716
1717 /* The permissions of all of the following attributes are changed to read-
1718  * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1719 static struct attribute *dme1737_attr_lock[] = {
1720         /* Temperatures */
1721         &sensor_dev_attr_temp1_offset.dev_attr.attr,
1722         &sensor_dev_attr_temp2_offset.dev_attr.attr,
1723         &sensor_dev_attr_temp3_offset.dev_attr.attr,
1724         /* Zones */
1725         &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1726         &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1727         &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1728         &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1729         &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1730         &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1731         &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1732         &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1733         &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1734         &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1735         &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1736         &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1737         NULL
1738 };
1739
1740 static const struct attribute_group dme1737_lock_group = {
1741         .attrs = dme1737_attr_lock,
1742 };
1743
1744 /* The permissions of the following PWM attributes are changed to read-
1745  * writeable if the chip is *not* locked and the respective PWM is available.
1746  * Otherwise they stay read-only. */
1747 static struct attribute *dme1737_attr_pwm1_lock[] = {
1748         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1749         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1750         &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1751         &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1752         &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1753         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1754         NULL
1755 };
1756 static struct attribute *dme1737_attr_pwm2_lock[] = {
1757         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1758         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1759         &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1760         &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1761         &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1762         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1763         NULL
1764 };
1765 static struct attribute *dme1737_attr_pwm3_lock[] = {
1766         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1767         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1768         &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1769         &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1770         &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1771         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1772         NULL
1773 };
1774 static struct attribute *dme1737_attr_pwm5_lock[] = {
1775         &sensor_dev_attr_pwm5.dev_attr.attr,
1776         &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1777         NULL
1778 };
1779 static struct attribute *dme1737_attr_pwm6_lock[] = {
1780         &sensor_dev_attr_pwm6.dev_attr.attr,
1781         &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1782         NULL
1783 };
1784
1785 static const struct attribute_group dme1737_pwm_lock_group[] = {
1786         { .attrs = dme1737_attr_pwm1_lock },
1787         { .attrs = dme1737_attr_pwm2_lock },
1788         { .attrs = dme1737_attr_pwm3_lock },
1789         { .attrs = NULL },
1790         { .attrs = dme1737_attr_pwm5_lock },
1791         { .attrs = dme1737_attr_pwm6_lock },
1792 };
1793
1794 /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1795  * chip is not locked. Otherwise they are read-only. */
1796 static struct attribute *dme1737_attr_pwm[] = {
1797         &sensor_dev_attr_pwm1.dev_attr.attr,
1798         &sensor_dev_attr_pwm2.dev_attr.attr,
1799         &sensor_dev_attr_pwm3.dev_attr.attr,
1800 };
1801
1802 /* ---------------------------------------------------------------------
1803  * Super-IO functions
1804  * --------------------------------------------------------------------- */
1805
1806 static inline void dme1737_sio_enter(int sio_cip)
1807 {
1808         outb(0x55, sio_cip);
1809 }
1810
1811 static inline void dme1737_sio_exit(int sio_cip)
1812 {
1813         outb(0xaa, sio_cip);
1814 }
1815
1816 static inline int dme1737_sio_inb(int sio_cip, int reg)
1817 {
1818         outb(reg, sio_cip);
1819         return inb(sio_cip + 1);
1820 }
1821
1822 static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1823 {
1824         outb(reg, sio_cip);
1825         outb(val, sio_cip + 1);
1826 }
1827
1828 /* ---------------------------------------------------------------------
1829  * Device initialization
1830  * --------------------------------------------------------------------- */
1831
1832 static int dme1737_i2c_get_features(int, struct dme1737_data*);
1833
1834 static void dme1737_chmod_file(struct device *dev,
1835                                struct attribute *attr, mode_t mode)
1836 {
1837         if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1838                 dev_warn(dev, "Failed to change permissions of %s.\n",
1839                          attr->name);
1840         }
1841 }
1842
1843 static void dme1737_chmod_group(struct device *dev,
1844                                 const struct attribute_group *group,
1845                                 mode_t mode)
1846 {
1847         struct attribute **attr;
1848
1849         for (attr = group->attrs; *attr; attr++) {
1850                 dme1737_chmod_file(dev, *attr, mode);
1851         }
1852 }
1853
1854 static void dme1737_remove_files(struct device *dev)
1855 {
1856         struct dme1737_data *data = dev_get_drvdata(dev);
1857         int ix;
1858
1859         for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1860                 if (data->has_fan & (1 << ix)) {
1861                         sysfs_remove_group(&dev->kobj,
1862                                            &dme1737_fan_group[ix]);
1863                 }
1864         }
1865
1866         for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1867                 if (data->has_pwm & (1 << ix)) {
1868                         sysfs_remove_group(&dev->kobj,
1869                                            &dme1737_pwm_group[ix]);
1870                 }
1871         }
1872
1873         sysfs_remove_group(&dev->kobj, &dme1737_group);
1874
1875         if (!data->client.driver) {
1876                 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
1877         }
1878 }
1879
1880 static int dme1737_create_files(struct device *dev)
1881 {
1882         struct dme1737_data *data = dev_get_drvdata(dev);
1883         int err, ix;
1884
1885         /* Create a name attribute for ISA devices */
1886         if (!data->client.driver &&
1887             (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
1888                 goto exit;
1889         }
1890
1891         /* Create standard sysfs attributes */
1892         if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
1893                 goto exit_remove;
1894         }
1895
1896         /* Create fan sysfs attributes */
1897         for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1898                 if (data->has_fan & (1 << ix)) {
1899                         if ((err = sysfs_create_group(&dev->kobj,
1900                                                 &dme1737_fan_group[ix]))) {
1901                                 goto exit_remove;
1902                         }
1903                 }
1904         }
1905
1906         /* Create PWM sysfs attributes */
1907         for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1908                 if (data->has_pwm & (1 << ix)) {
1909                         if ((err = sysfs_create_group(&dev->kobj,
1910                                                 &dme1737_pwm_group[ix]))) {
1911                                 goto exit_remove;
1912                         }
1913                 }
1914         }
1915
1916         /* Inform if the device is locked. Otherwise change the permissions of
1917          * selected attributes from read-only to read-writeable. */
1918         if (data->config & 0x02) {
1919                 dev_info(dev, "Device is locked. Some attributes "
1920                          "will be read-only.\n");
1921         } else {
1922                 /* Change permissions of standard attributes */
1923                 dme1737_chmod_group(dev, &dme1737_lock_group,
1924                                     S_IRUGO | S_IWUSR);
1925
1926                 /* Change permissions of PWM attributes */
1927                 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
1928                         if (data->has_pwm & (1 << ix)) {
1929                                 dme1737_chmod_group(dev,
1930                                                 &dme1737_pwm_lock_group[ix],
1931                                                 S_IRUGO | S_IWUSR);
1932                         }
1933                 }
1934
1935                 /* Change permissions of pwm[1-3] if in manual mode */
1936                 for (ix = 0; ix < 3; ix++) {
1937                         if ((data->has_pwm & (1 << ix)) &&
1938                             (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
1939                                 dme1737_chmod_file(dev,
1940                                                 dme1737_attr_pwm[ix],
1941                                                 S_IRUGO | S_IWUSR);
1942                         }
1943                 }
1944         }
1945
1946         return 0;
1947
1948 exit_remove:
1949         dme1737_remove_files(dev);
1950 exit:
1951         return err;
1952 }
1953
1954 static int dme1737_init_device(struct device *dev)
1955 {
1956         struct dme1737_data *data = dev_get_drvdata(dev);
1957         struct i2c_client *client = &data->client;
1958         int ix;
1959         u8 reg;
1960
1961         data->config = dme1737_read(client, DME1737_REG_CONFIG);
1962         /* Inform if part is not monitoring/started */
1963         if (!(data->config & 0x01)) {
1964                 if (!force_start) {
1965                         dev_err(dev, "Device is not monitoring. "
1966                                 "Use the force_start load parameter to "
1967                                 "override.\n");
1968                         return -EFAULT;
1969                 }
1970
1971                 /* Force monitoring */
1972                 data->config |= 0x01;
1973                 dme1737_write(client, DME1737_REG_CONFIG, data->config);
1974         }
1975         /* Inform if part is not ready */
1976         if (!(data->config & 0x04)) {
1977                 dev_err(dev, "Device is not ready.\n");
1978                 return -EFAULT;
1979         }
1980
1981         /* Determine which optional fan and pwm features are enabled/present */
1982         if (client->driver) {   /* I2C chip */
1983                 data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
1984                 /* Check if optional fan3 input is enabled */
1985                 if (data->config2 & 0x04) {
1986                         data->has_fan |= (1 << 2);
1987                 }
1988
1989                 /* Fan4 and pwm3 are only available if the client's I2C address
1990                  * is the default 0x2e. Otherwise the I/Os associated with
1991                  * these functions are used for addr enable/select. */
1992                 if (data->client.addr == 0x2e) {
1993                         data->has_fan |= (1 << 3);
1994                         data->has_pwm |= (1 << 2);
1995                 }
1996
1997                 /* Determine which of the optional fan[5-6] and pwm[5-6]
1998                  * features are enabled. For this, we need to query the runtime
1999                  * registers through the Super-IO LPC interface. Try both
2000                  * config ports 0x2e and 0x4e. */
2001                 if (dme1737_i2c_get_features(0x2e, data) &&
2002                     dme1737_i2c_get_features(0x4e, data)) {
2003                         dev_warn(dev, "Failed to query Super-IO for optional "
2004                                  "features.\n");
2005                 }
2006         } else {   /* ISA chip */
2007                 /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6]
2008                  * don't exist in the ISA chip. */
2009                 data->has_fan |= (1 << 2);
2010                 data->has_pwm |= (1 << 2);
2011         }
2012
2013         /* Fan1, fan2, pwm1, and pwm2 are always present */
2014         data->has_fan |= 0x03;
2015         data->has_pwm |= 0x03;
2016
2017         dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
2018                  "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
2019                  (data->has_pwm & (1 << 2)) ? "yes" : "no",
2020                  (data->has_pwm & (1 << 4)) ? "yes" : "no",
2021                  (data->has_pwm & (1 << 5)) ? "yes" : "no",
2022                  (data->has_fan & (1 << 2)) ? "yes" : "no",
2023                  (data->has_fan & (1 << 3)) ? "yes" : "no",
2024                  (data->has_fan & (1 << 4)) ? "yes" : "no",
2025                  (data->has_fan & (1 << 5)) ? "yes" : "no");
2026
2027         reg = dme1737_read(client, DME1737_REG_TACH_PWM);
2028         /* Inform if fan-to-pwm mapping differs from the default */
2029         if (client->driver && reg != 0xa4) {   /* I2C chip */
2030                 dev_warn(dev, "Non-standard fan to pwm mapping: "
2031                          "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
2032                          "fan4->pwm%d. Please report to the driver "
2033                          "maintainer.\n",
2034                          (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2035                          ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
2036         } else if (!client->driver && reg != 0x24) {   /* ISA chip */
2037                 dev_warn(dev, "Non-standard fan to pwm mapping: "
2038                          "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2039                          "Please report to the driver maintainer.\n",
2040                          (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2041                          ((reg >> 4) & 0x03) + 1);
2042         }
2043
2044         /* Switch pwm[1-3] to manual mode if they are currently disabled and
2045          * set the duty-cycles to 0% (which is identical to the PWMs being
2046          * disabled). */
2047         if (!(data->config & 0x02)) {
2048                 for (ix = 0; ix < 3; ix++) {
2049                         data->pwm_config[ix] = dme1737_read(client,
2050                                                 DME1737_REG_PWM_CONFIG(ix));
2051                         if ((data->has_pwm & (1 << ix)) &&
2052                             (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
2053                                 dev_info(dev, "Switching pwm%d to "
2054                                          "manual mode.\n", ix + 1);
2055                                 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2056                                                         data->pwm_config[ix]);
2057                                 dme1737_write(client, DME1737_REG_PWM(ix), 0);
2058                                 dme1737_write(client,
2059                                               DME1737_REG_PWM_CONFIG(ix),
2060                                               data->pwm_config[ix]);
2061                         }
2062                 }
2063         }
2064
2065         /* Initialize the default PWM auto channels zone (acz) assignments */
2066         data->pwm_acz[0] = 1;   /* pwm1 -> zone1 */
2067         data->pwm_acz[1] = 2;   /* pwm2 -> zone2 */
2068         data->pwm_acz[2] = 4;   /* pwm3 -> zone3 */
2069
2070         /* Set VRM */
2071         data->vrm = vid_which_vrm();
2072
2073         return 0;
2074 }
2075
2076 /* ---------------------------------------------------------------------
2077  * I2C device detection and registration
2078  * --------------------------------------------------------------------- */
2079
2080 static struct i2c_driver dme1737_i2c_driver;
2081
2082 static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2083 {
2084         int err = 0, reg;
2085         u16 addr;
2086
2087         dme1737_sio_enter(sio_cip);
2088
2089         /* Check device ID
2090          * The DME1737 can return either 0x78 or 0x77 as its device ID. */
2091         reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2092         if (!(reg == 0x77 || reg == 0x78)) {
2093                 err = -ENODEV;
2094                 goto exit;
2095         }
2096
2097         /* Select logical device A (runtime registers) */
2098         dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2099
2100         /* Get the base address of the runtime registers */
2101         if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2102                       dme1737_sio_inb(sio_cip, 0x61))) {
2103                 err = -ENODEV;
2104                 goto exit;
2105         }
2106
2107         /* Read the runtime registers to determine which optional features
2108          * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2109          * to '10' if the respective feature is enabled. */
2110         if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
2111                 data->has_fan |= (1 << 5);
2112         }
2113         if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
2114                 data->has_pwm |= (1 << 5);
2115         }
2116         if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
2117                 data->has_fan |= (1 << 4);
2118         }
2119         if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
2120                 data->has_pwm |= (1 << 4);
2121         }
2122
2123 exit:
2124         dme1737_sio_exit(sio_cip);
2125
2126         return err;
2127 }
2128
2129 static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2130                               int kind)
2131 {
2132         u8 company, verstep = 0;
2133         struct i2c_client *client;
2134         struct dme1737_data *data;
2135         struct device *dev;
2136         int err = 0;
2137         const char *name;
2138
2139         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
2140                 goto exit;
2141         }
2142
2143         if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2144                 err = -ENOMEM;
2145                 goto exit;
2146         }
2147
2148         client = &data->client;
2149         i2c_set_clientdata(client, data);
2150         client->addr = address;
2151         client->adapter = adapter;
2152         client->driver = &dme1737_i2c_driver;
2153         dev = &client->dev;
2154
2155         /* A negative kind means that the driver was loaded with no force
2156          * parameter (default), so we must identify the chip. */
2157         if (kind < 0) {
2158                 company = dme1737_read(client, DME1737_REG_COMPANY);
2159                 verstep = dme1737_read(client, DME1737_REG_VERSTEP);
2160
2161                 if (!((company == DME1737_COMPANY_SMSC) &&
2162                       ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
2163                         err = -ENODEV;
2164                         goto exit_kfree;
2165                 }
2166         }
2167
2168         kind = dme1737;
2169         name = "dme1737";
2170
2171         /* Fill in the remaining client fields and put it into the global
2172          * list */
2173         strlcpy(client->name, name, I2C_NAME_SIZE);
2174         mutex_init(&data->update_lock);
2175
2176         /* Tell the I2C layer a new client has arrived */
2177         if ((err = i2c_attach_client(client))) {
2178                 goto exit_kfree;
2179         }
2180
2181         dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2182                  client->addr, verstep);
2183
2184         /* Initialize the DME1737 chip */
2185         if ((err = dme1737_init_device(dev))) {
2186                 dev_err(dev, "Failed to initialize device.\n");
2187                 goto exit_detach;
2188         }
2189
2190         /* Create sysfs files */
2191         if ((err = dme1737_create_files(dev))) {
2192                 dev_err(dev, "Failed to create sysfs files.\n");
2193                 goto exit_detach;
2194         }
2195
2196         /* Register device */
2197         data->hwmon_dev = hwmon_device_register(dev);
2198         if (IS_ERR(data->hwmon_dev)) {
2199                 dev_err(dev, "Failed to register device.\n");
2200                 err = PTR_ERR(data->hwmon_dev);
2201                 goto exit_remove;
2202         }
2203
2204         return 0;
2205
2206 exit_remove:
2207         dme1737_remove_files(dev);
2208 exit_detach:
2209         i2c_detach_client(client);
2210 exit_kfree:
2211         kfree(data);
2212 exit:
2213         return err;
2214 }
2215
2216 static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
2217 {
2218         if (!(adapter->class & I2C_CLASS_HWMON)) {
2219                 return 0;
2220         }
2221
2222         return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
2223 }
2224
2225 static int dme1737_i2c_detach_client(struct i2c_client *client)
2226 {
2227         struct dme1737_data *data = i2c_get_clientdata(client);
2228         int err;
2229
2230         hwmon_device_unregister(data->hwmon_dev);
2231         dme1737_remove_files(&client->dev);
2232
2233         if ((err = i2c_detach_client(client))) {
2234                 return err;
2235         }
2236
2237         kfree(data);
2238         return 0;
2239 }
2240
2241 static struct i2c_driver dme1737_i2c_driver = {
2242         .driver = {
2243                 .name = "dme1737",
2244         },
2245         .attach_adapter = dme1737_i2c_attach_adapter,
2246         .detach_client = dme1737_i2c_detach_client,
2247 };
2248
2249 /* ---------------------------------------------------------------------
2250  * ISA device detection and registration
2251  * --------------------------------------------------------------------- */
2252
2253 static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2254 {
2255         int err = 0, reg;
2256         unsigned short base_addr;
2257
2258         dme1737_sio_enter(sio_cip);
2259
2260         /* Check device ID
2261          * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and
2262          * SCH3116 (0x7f). */
2263         reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2264         if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
2265                 err = -ENODEV;
2266                 goto exit;
2267         }
2268
2269         /* Select logical device A (runtime registers) */
2270         dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2271
2272         /* Get the base address of the runtime registers */
2273         if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2274                            dme1737_sio_inb(sio_cip, 0x61))) {
2275                 printk(KERN_ERR "dme1737: Base address not set.\n");
2276                 err = -ENODEV;
2277                 goto exit;
2278         }
2279
2280         /* Access to the hwmon registers is through an index/data register
2281          * pair located at offset 0x70/0x71. */
2282         *addr = base_addr + 0x70;
2283
2284 exit:
2285         dme1737_sio_exit(sio_cip);
2286         return err;
2287 }
2288
2289 static int __init dme1737_isa_device_add(unsigned short addr)
2290 {
2291         struct resource res = {
2292                 .start  = addr,
2293                 .end    = addr + DME1737_EXTENT - 1,
2294                 .name   = "dme1737",
2295                 .flags  = IORESOURCE_IO,
2296         };
2297         int err;
2298
2299         if (!(pdev = platform_device_alloc("dme1737", addr))) {
2300                 printk(KERN_ERR "dme1737: Failed to allocate device.\n");
2301                 err = -ENOMEM;
2302                 goto exit;
2303         }
2304
2305         if ((err = platform_device_add_resources(pdev, &res, 1))) {
2306                 printk(KERN_ERR "dme1737: Failed to add device resource "
2307                        "(err = %d).\n", err);
2308                 goto exit_device_put;
2309         }
2310
2311         if ((err = platform_device_add(pdev))) {
2312                 printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n",
2313                        err);
2314                 goto exit_device_put;
2315         }
2316
2317         return 0;
2318
2319 exit_device_put:
2320         platform_device_put(pdev);
2321         pdev = NULL;
2322 exit:
2323         return err;
2324 }
2325
2326 static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2327 {
2328         u8 company, device;
2329         struct resource *res;
2330         struct i2c_client *client;
2331         struct dme1737_data *data;
2332         struct device *dev = &pdev->dev;
2333         int err;
2334
2335         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2336         if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
2337                 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2338                         (unsigned short)res->start,
2339                         (unsigned short)res->start + DME1737_EXTENT - 1);
2340                 err = -EBUSY;
2341                 goto exit;
2342         }
2343
2344         if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2345                 err = -ENOMEM;
2346                 goto exit_release_region;
2347         }
2348
2349         client = &data->client;
2350         i2c_set_clientdata(client, data);
2351         client->addr = res->start;
2352         platform_set_drvdata(pdev, data);
2353
2354         company = dme1737_read(client, DME1737_REG_COMPANY);
2355         device = dme1737_read(client, DME1737_REG_DEVICE);
2356
2357         if (!((company == DME1737_COMPANY_SMSC) &&
2358               (device == SCH311X_DEVICE))) {
2359                 err = -ENODEV;
2360                 goto exit_kfree;
2361         }
2362
2363         /* Fill in the remaining client fields and initialize the mutex */
2364         strlcpy(client->name, "sch311x", I2C_NAME_SIZE);
2365         mutex_init(&data->update_lock);
2366
2367         dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr);
2368
2369         /* Initialize the chip */
2370         if ((err = dme1737_init_device(dev))) {
2371                 dev_err(dev, "Failed to initialize device.\n");
2372                 goto exit_kfree;
2373         }
2374
2375         /* Create sysfs files */
2376         if ((err = dme1737_create_files(dev))) {
2377                 dev_err(dev, "Failed to create sysfs files.\n");
2378                 goto exit_kfree;
2379         }
2380
2381         /* Register device */
2382         data->hwmon_dev = hwmon_device_register(dev);
2383         if (IS_ERR(data->hwmon_dev)) {
2384                 dev_err(dev, "Failed to register device.\n");
2385                 err = PTR_ERR(data->hwmon_dev);
2386                 goto exit_remove_files;
2387         }
2388
2389         return 0;
2390
2391 exit_remove_files:
2392         dme1737_remove_files(dev);
2393 exit_kfree:
2394         platform_set_drvdata(pdev, NULL);
2395         kfree(data);
2396 exit_release_region:
2397         release_region(res->start, DME1737_EXTENT);
2398 exit:
2399         return err;
2400 }
2401
2402 static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2403 {
2404         struct dme1737_data *data = platform_get_drvdata(pdev);
2405
2406         hwmon_device_unregister(data->hwmon_dev);
2407         dme1737_remove_files(&pdev->dev);
2408         release_region(data->client.addr, DME1737_EXTENT);
2409         platform_set_drvdata(pdev, NULL);
2410         kfree(data);
2411
2412         return 0;
2413 }
2414
2415 static struct platform_driver dme1737_isa_driver = {
2416         .driver = {
2417                 .owner = THIS_MODULE,
2418                 .name = "dme1737",
2419         },
2420         .probe = dme1737_isa_probe,
2421         .remove = __devexit_p(dme1737_isa_remove),
2422 };
2423
2424 /* ---------------------------------------------------------------------
2425  * Module initialization and cleanup
2426  * --------------------------------------------------------------------- */
2427
2428 static int __init dme1737_init(void)
2429 {
2430         int err;
2431         unsigned short addr;
2432
2433         if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
2434                 goto exit;
2435         }
2436
2437         if (dme1737_isa_detect(0x2e, &addr) &&
2438             dme1737_isa_detect(0x4e, &addr) &&
2439             (!probe_all_addr ||
2440              (dme1737_isa_detect(0x162e, &addr) &&
2441               dme1737_isa_detect(0x164e, &addr)))) {
2442                 /* Return 0 if we didn't find an ISA device */
2443                 return 0;
2444         }
2445
2446         if ((err = platform_driver_register(&dme1737_isa_driver))) {
2447                 goto exit_del_i2c_driver;
2448         }
2449
2450         /* Sets global pdev as a side effect */
2451         if ((err = dme1737_isa_device_add(addr))) {
2452                 goto exit_del_isa_driver;
2453         }
2454
2455         return 0;
2456
2457 exit_del_isa_driver:
2458         platform_driver_unregister(&dme1737_isa_driver);
2459 exit_del_i2c_driver:
2460         i2c_del_driver(&dme1737_i2c_driver);
2461 exit:
2462         return err;
2463 }
2464
2465 static void __exit dme1737_exit(void)
2466 {
2467         if (pdev) {
2468                 platform_device_unregister(pdev);
2469                 platform_driver_unregister(&dme1737_isa_driver);
2470         }
2471
2472         i2c_del_driver(&dme1737_i2c_driver);
2473 }
2474
2475 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2476 MODULE_DESCRIPTION("DME1737 sensors");
2477 MODULE_LICENSE("GPL");
2478
2479 module_init(dme1737_init);
2480 module_exit(dme1737_exit);