hwmon: VRM is not written to registers
[safe/jmp/linux-2.6] / drivers / hwmon / w83793.c
1 /*
2     w83793.c - Linux kernel driver for hardware monitoring
3     Copyright (C) 2006 Winbond Electronics Corp.
4                   Yuan Mu
5                   Rudolf Marek <r.marek@assembler.cz>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation - version 2.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19     02110-1301 USA.
20 */
21
22 /*
23     Supports following chips:
24
25     Chip        #vin    #fanin  #pwm    #temp   wchipid vendid  i2c     ISA
26     w83793      10      12      8       6       0x7b    0x5ca3  yes     no
27 */
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
33 #include <linux/hwmon.h>
34 #include <linux/hwmon-vid.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/err.h>
37 #include <linux/mutex.h>
38
39 /* Addresses to scan */
40 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
41
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_1(w83793);
44 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
45                        "{bus, clientaddr, subclientaddr1, subclientaddr2}");
46
47 static int reset;
48 module_param(reset, bool, 0);
49 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
50
51 /*
52    Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
53    as ID, Bank Select registers
54 */
55 #define W83793_REG_BANKSEL              0x00
56 #define W83793_REG_VENDORID             0x0d
57 #define W83793_REG_CHIPID               0x0e
58 #define W83793_REG_DEVICEID             0x0f
59
60 #define W83793_REG_CONFIG               0x40
61 #define W83793_REG_MFC                  0x58
62 #define W83793_REG_FANIN_CTRL           0x5c
63 #define W83793_REG_FANIN_SEL            0x5d
64 #define W83793_REG_I2C_ADDR             0x0b
65 #define W83793_REG_I2C_SUBADDR          0x0c
66 #define W83793_REG_VID_INA              0x05
67 #define W83793_REG_VID_INB              0x06
68 #define W83793_REG_VID_LATCHA           0x07
69 #define W83793_REG_VID_LATCHB           0x08
70 #define W83793_REG_VID_CTRL             0x59
71
72 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
73
74 #define TEMP_READ       0
75 #define TEMP_CRIT       1
76 #define TEMP_CRIT_HYST  2
77 #define TEMP_WARN       3
78 #define TEMP_WARN_HYST  4
79 /* only crit and crit_hyst affect real-time alarm status
80    current crit crit_hyst warn warn_hyst */
81 static u16 W83793_REG_TEMP[][5] = {
82         {0x1c, 0x78, 0x79, 0x7a, 0x7b},
83         {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
84         {0x1e, 0x80, 0x81, 0x82, 0x83},
85         {0x1f, 0x84, 0x85, 0x86, 0x87},
86         {0x20, 0x88, 0x89, 0x8a, 0x8b},
87         {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
88 };
89
90 #define W83793_REG_TEMP_LOW_BITS        0x22
91
92 #define W83793_REG_BEEP(index)          (0x53 + (index))
93 #define W83793_REG_ALARM(index)         (0x4b + (index))
94
95 #define W83793_REG_CLR_CHASSIS          0x4a    /* SMI MASK4 */
96 #define W83793_REG_IRQ_CTRL             0x50
97 #define W83793_REG_OVT_CTRL             0x51
98 #define W83793_REG_OVT_BEEP             0x52
99
100 #define IN_READ                         0
101 #define IN_MAX                          1
102 #define IN_LOW                          2
103 static const u16 W83793_REG_IN[][3] = {
104         /* Current, High, Low */
105         {0x10, 0x60, 0x61},     /* Vcore A      */
106         {0x11, 0x62, 0x63},     /* Vcore B      */
107         {0x12, 0x64, 0x65},     /* Vtt          */
108         {0x14, 0x6a, 0x6b},     /* VSEN1        */
109         {0x15, 0x6c, 0x6d},     /* VSEN2        */
110         {0x16, 0x6e, 0x6f},     /* +3VSEN       */
111         {0x17, 0x70, 0x71},     /* +12VSEN      */
112         {0x18, 0x72, 0x73},     /* 5VDD         */
113         {0x19, 0x74, 0x75},     /* 5VSB         */
114         {0x1a, 0x76, 0x77},     /* VBAT         */
115 };
116
117 /* Low Bits of Vcore A/B Vtt Read/High/Low */
118 static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
119 static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
120 static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
121
122 #define W83793_REG_FAN(index)           (0x23 + 2 * (index))    /* High byte */
123 #define W83793_REG_FAN_MIN(index)       (0x90 + 2 * (index))    /* High byte */
124
125 #define W83793_REG_PWM_DEFAULT          0xb2
126 #define W83793_REG_PWM_ENABLE           0x207
127 #define W83793_REG_PWM_UPTIME           0xc3    /* Unit in 0.1 second */
128 #define W83793_REG_PWM_DOWNTIME         0xc4    /* Unit in 0.1 second */
129 #define W83793_REG_TEMP_CRITICAL        0xc5
130
131 #define PWM_DUTY                        0
132 #define PWM_START                       1
133 #define PWM_NONSTOP                     2
134 #define PWM_STOP_TIME                   3
135 #define W83793_REG_PWM(index, nr)       (((nr) == 0 ? 0xb3 : \
136                                          (nr) == 1 ? 0x220 : 0x218) + (index))
137
138 /* bit field, fan1 is bit0, fan2 is bit1 ... */
139 #define W83793_REG_TEMP_FAN_MAP(index)  (0x201 + (index))
140 #define W83793_REG_TEMP_TOL(index)      (0x208 + (index))
141 #define W83793_REG_TEMP_CRUISE(index)   (0x210 + (index))
142 #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
143 #define W83793_REG_SF2_TEMP(index, nr)  (0x230 + ((index) << 4) + (nr))
144 #define W83793_REG_SF2_PWM(index, nr)   (0x238 + ((index) << 4) + (nr))
145
146 static inline unsigned long FAN_FROM_REG(u16 val)
147 {
148         if ((val >= 0xfff) || (val == 0))
149                 return  0;
150         return (1350000UL / val);
151 }
152
153 static inline u16 FAN_TO_REG(long rpm)
154 {
155         if (rpm <= 0)
156                 return 0x0fff;
157         return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
158 }
159
160 static inline unsigned long TIME_FROM_REG(u8 reg)
161 {
162         return (reg * 100);
163 }
164
165 static inline u8 TIME_TO_REG(unsigned long val)
166 {
167         return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
168 }
169
170 static inline long TEMP_FROM_REG(s8 reg)
171 {
172         return (reg * 1000);
173 }
174
175 static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
176 {
177         return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
178 }
179
180 struct w83793_data {
181         struct i2c_client client;
182         struct i2c_client *lm75[2];
183         struct device *hwmon_dev;
184         struct mutex update_lock;
185         char valid;                     /* !=0 if following fields are valid */
186         unsigned long last_updated;     /* In jiffies */
187         unsigned long last_nonvolatile; /* In jiffies, last time we update the
188                                            nonvolatile registers */
189
190         u8 bank;
191         u8 vrm;
192         u8 vid[2];
193         u8 in[10][3];           /* Register value, read/high/low */
194         u8 in_low_bits[3];      /* Additional resolution for VCore A/B Vtt */
195
196         u16 has_fan;            /* Only fan1- fan5 has own pins */
197         u16 fan[12];            /* Register value combine */
198         u16 fan_min[12];        /* Register value combine */
199
200         s8 temp[6][5];          /* current, crit, crit_hyst,warn, warn_hyst */
201         u8 temp_low_bits;       /* Additional resolution TD1-TD4 */
202         u8 temp_mode[2];        /* byte 0: Temp D1-D4 mode each has 2 bits
203                                    byte 1: Temp R1,R2 mode, each has 1 bit */
204         u8 temp_critical;       /* If reached all fan will be at full speed */
205         u8 temp_fan_map[6];     /* Temp controls which pwm fan, bit field */
206
207         u8 has_pwm;
208         u8 has_temp;
209         u8 has_vid;
210         u8 pwm_enable;          /* Register value, each Temp has 1 bit */
211         u8 pwm_uptime;          /* Register value */
212         u8 pwm_downtime;        /* Register value */
213         u8 pwm_default;         /* All fan default pwm, next poweron valid */
214         u8 pwm[8][3];           /* Register value */
215         u8 pwm_stop_time[8];
216         u8 temp_cruise[6];
217
218         u8 alarms[5];           /* realtime status registers */
219         u8 beeps[5];
220         u8 beep_enable;
221         u8 tolerance[3];        /* Temp tolerance(Smart Fan I/II) */
222         u8 sf2_pwm[6][7];       /* Smart FanII: Fan duty cycle */
223         u8 sf2_temp[6][7];      /* Smart FanII: Temp level point */
224 };
225
226 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
227 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
228 static int w83793_attach_adapter(struct i2c_adapter *adapter);
229 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
230 static int w83793_detach_client(struct i2c_client *client);
231 static void w83793_init_client(struct i2c_client *client);
232 static void w83793_update_nonvolatile(struct device *dev);
233 static struct w83793_data *w83793_update_device(struct device *dev);
234
235 static struct i2c_driver w83793_driver = {
236         .driver = {
237                    .name = "w83793",
238         },
239         .attach_adapter = w83793_attach_adapter,
240         .detach_client = w83793_detach_client,
241 };
242
243 static ssize_t
244 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
245 {
246         struct w83793_data *data = dev_get_drvdata(dev);
247         return sprintf(buf, "%d\n", data->vrm);
248 }
249
250 static ssize_t
251 show_vid(struct device *dev, struct device_attribute *attr, char *buf)
252 {
253         struct w83793_data *data = w83793_update_device(dev);
254         struct sensor_device_attribute_2 *sensor_attr =
255             to_sensor_dev_attr_2(attr);
256         int index = sensor_attr->index;
257
258         return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
259 }
260
261 static ssize_t
262 store_vrm(struct device *dev, struct device_attribute *attr,
263           const char *buf, size_t count)
264 {
265         struct w83793_data *data = dev_get_drvdata(dev);
266         data->vrm = simple_strtoul(buf, NULL, 10);
267         return count;
268 }
269
270 #define ALARM_STATUS                    0
271 #define BEEP_ENABLE                     1
272 static ssize_t
273 show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
274 {
275         struct w83793_data *data = w83793_update_device(dev);
276         struct sensor_device_attribute_2 *sensor_attr =
277             to_sensor_dev_attr_2(attr);
278         int nr = sensor_attr->nr;
279         int index = sensor_attr->index >> 3;
280         int bit = sensor_attr->index & 0x07;
281         u8 val;
282
283         if (ALARM_STATUS == nr) {
284                 val = (data->alarms[index] >> (bit)) & 1;
285         } else {                /* BEEP_ENABLE */
286                 val = (data->beeps[index] >> (bit)) & 1;
287         }
288
289         return sprintf(buf, "%u\n", val);
290 }
291
292 static ssize_t
293 store_beep(struct device *dev, struct device_attribute *attr,
294            const char *buf, size_t count)
295 {
296         struct i2c_client *client = to_i2c_client(dev);
297         struct w83793_data *data = i2c_get_clientdata(client);
298         struct sensor_device_attribute_2 *sensor_attr =
299             to_sensor_dev_attr_2(attr);
300         int index = sensor_attr->index >> 3;
301         int shift = sensor_attr->index & 0x07;
302         u8 beep_bit = 1 << shift;
303         u8 val;
304
305         val = simple_strtoul(buf, NULL, 10);
306         if (val != 0 && val != 1)
307                 return -EINVAL;
308
309         mutex_lock(&data->update_lock);
310         data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
311         data->beeps[index] &= ~beep_bit;
312         data->beeps[index] |= val << shift;
313         w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
314         mutex_unlock(&data->update_lock);
315
316         return count;
317 }
318
319 static ssize_t
320 show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
321 {
322         struct w83793_data *data = w83793_update_device(dev);
323         return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
324 }
325
326 static ssize_t
327 store_beep_enable(struct device *dev, struct device_attribute *attr,
328                   const char *buf, size_t count)
329 {
330         struct i2c_client *client = to_i2c_client(dev);
331         struct w83793_data *data = i2c_get_clientdata(client);
332         u8 val = simple_strtoul(buf, NULL, 10);
333
334         if (val != 0 && val != 1)
335                 return -EINVAL;
336
337         mutex_lock(&data->update_lock);
338         data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
339                             & 0xfd;
340         data->beep_enable |= val << 1;
341         w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
342         mutex_unlock(&data->update_lock);
343
344         return count;
345 }
346
347 /* Write any value to clear chassis alarm */
348 static ssize_t
349 store_chassis_clear(struct device *dev,
350                     struct device_attribute *attr, const char *buf,
351                     size_t count)
352 {
353         struct i2c_client *client = to_i2c_client(dev);
354         struct w83793_data *data = i2c_get_clientdata(client);
355         u8 val;
356
357         mutex_lock(&data->update_lock);
358         val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
359         val |= 0x80;
360         w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
361         mutex_unlock(&data->update_lock);
362         return count;
363 }
364
365 #define FAN_INPUT                       0
366 #define FAN_MIN                         1
367 static ssize_t
368 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
369 {
370         struct sensor_device_attribute_2 *sensor_attr =
371             to_sensor_dev_attr_2(attr);
372         int nr = sensor_attr->nr;
373         int index = sensor_attr->index;
374         struct w83793_data *data = w83793_update_device(dev);
375         u16 val;
376
377         if (FAN_INPUT == nr) {
378                 val = data->fan[index] & 0x0fff;
379         } else {
380                 val = data->fan_min[index] & 0x0fff;
381         }
382
383         return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
384 }
385
386 static ssize_t
387 store_fan_min(struct device *dev, struct device_attribute *attr,
388               const char *buf, size_t count)
389 {
390         struct sensor_device_attribute_2 *sensor_attr =
391             to_sensor_dev_attr_2(attr);
392         int index = sensor_attr->index;
393         struct i2c_client *client = to_i2c_client(dev);
394         struct w83793_data *data = i2c_get_clientdata(client);
395         u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
396
397         mutex_lock(&data->update_lock);
398         data->fan_min[index] = val;
399         w83793_write_value(client, W83793_REG_FAN_MIN(index),
400                            (val >> 8) & 0xff);
401         w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
402         mutex_unlock(&data->update_lock);
403
404         return count;
405 }
406
407 static ssize_t
408 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
409 {
410         struct sensor_device_attribute_2 *sensor_attr =
411             to_sensor_dev_attr_2(attr);
412         struct w83793_data *data = w83793_update_device(dev);
413         u16 val;
414         int nr = sensor_attr->nr;
415         int index = sensor_attr->index;
416
417         if (PWM_STOP_TIME == nr)
418                 val = TIME_FROM_REG(data->pwm_stop_time[index]);
419         else
420                 val = (data->pwm[index][nr] & 0x3f) << 2;
421
422         return sprintf(buf, "%d\n", val);
423 }
424
425 static ssize_t
426 store_pwm(struct device *dev, struct device_attribute *attr,
427           const char *buf, size_t count)
428 {
429         struct i2c_client *client = to_i2c_client(dev);
430         struct w83793_data *data = i2c_get_clientdata(client);
431         struct sensor_device_attribute_2 *sensor_attr =
432             to_sensor_dev_attr_2(attr);
433         int nr = sensor_attr->nr;
434         int index = sensor_attr->index;
435         u8 val;
436
437         mutex_lock(&data->update_lock);
438         if (PWM_STOP_TIME == nr) {
439                 val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
440                 data->pwm_stop_time[index] = val;
441                 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
442                                    val);
443         } else {
444                 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
445                       >> 2;
446                 data->pwm[index][nr] =
447                     w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
448                 data->pwm[index][nr] |= val;
449                 w83793_write_value(client, W83793_REG_PWM(index, nr),
450                                                         data->pwm[index][nr]);
451         }
452
453         mutex_unlock(&data->update_lock);
454         return count;
455 }
456
457 static ssize_t
458 show_temp(struct device *dev, struct device_attribute *attr, char *buf)
459 {
460         struct sensor_device_attribute_2 *sensor_attr =
461             to_sensor_dev_attr_2(attr);
462         int nr = sensor_attr->nr;
463         int index = sensor_attr->index;
464         struct w83793_data *data = w83793_update_device(dev);
465         long temp = TEMP_FROM_REG(data->temp[index][nr]);
466
467         if (TEMP_READ == nr && index < 4) {     /* Only TD1-TD4 have low bits */
468                 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
469                 temp += temp > 0 ? low : -low;
470         }
471         return sprintf(buf, "%ld\n", temp);
472 }
473
474 static ssize_t
475 store_temp(struct device *dev, struct device_attribute *attr,
476            const char *buf, size_t count)
477 {
478         struct sensor_device_attribute_2 *sensor_attr =
479             to_sensor_dev_attr_2(attr);
480         int nr = sensor_attr->nr;
481         int index = sensor_attr->index;
482         struct i2c_client *client = to_i2c_client(dev);
483         struct w83793_data *data = i2c_get_clientdata(client);
484         long tmp = simple_strtol(buf, NULL, 10);
485
486         mutex_lock(&data->update_lock);
487         data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
488         w83793_write_value(client, W83793_REG_TEMP[index][nr],
489                            data->temp[index][nr]);
490         mutex_unlock(&data->update_lock);
491         return count;
492 }
493
494 /*
495         TD1-TD4
496         each has 4 mode:(2 bits)
497         0:      Stop monitor
498         1:      Use internal temp sensor(default)
499         2:      Reserved
500         3:      Use sensor in Intel CPU and get result by PECI
501
502         TR1-TR2
503         each has 2 mode:(1 bit)
504         0:      Disable temp sensor monitor
505         1:      To enable temp sensors monitor
506 */
507
508 /* 0 disable, 6 PECI */
509 static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
510
511 static ssize_t
512 show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
513 {
514         struct w83793_data *data = w83793_update_device(dev);
515         struct sensor_device_attribute_2 *sensor_attr =
516             to_sensor_dev_attr_2(attr);
517         int index = sensor_attr->index;
518         u8 mask = (index < 4) ? 0x03 : 0x01;
519         u8 shift = (index < 4) ? (2 * index) : (index - 4);
520         u8 tmp;
521         index = (index < 4) ? 0 : 1;
522
523         tmp = (data->temp_mode[index] >> shift) & mask;
524
525         /* for the internal sensor, found out if diode or thermistor */
526         if (tmp == 1) {
527                 tmp = index == 0 ? 3 : 4;
528         } else {
529                 tmp = TO_TEMP_MODE[tmp];
530         }
531
532         return sprintf(buf, "%d\n", tmp);
533 }
534
535 static ssize_t
536 store_temp_mode(struct device *dev, struct device_attribute *attr,
537                 const char *buf, size_t count)
538 {
539         struct i2c_client *client = to_i2c_client(dev);
540         struct w83793_data *data = i2c_get_clientdata(client);
541         struct sensor_device_attribute_2 *sensor_attr =
542             to_sensor_dev_attr_2(attr);
543         int index = sensor_attr->index;
544         u8 mask = (index < 4) ? 0x03 : 0x01;
545         u8 shift = (index < 4) ? (2 * index) : (index - 4);
546         u8 val = simple_strtoul(buf, NULL, 10);
547
548         /* transform the sysfs interface values into table above */
549         if ((val == 6) && (index < 4)) {
550                 val -= 3;
551         } else if ((val == 3 && index < 4)
552                 || (val == 4 && index >= 4)) {
553                 /* transform diode or thermistor into internal enable */
554                 val = !!val;
555         } else {
556                 return -EINVAL;
557         }
558
559         index = (index < 4) ? 0 : 1;
560         mutex_lock(&data->update_lock);
561         data->temp_mode[index] =
562             w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
563         data->temp_mode[index] &= ~(mask << shift);
564         data->temp_mode[index] |= val << shift;
565         w83793_write_value(client, W83793_REG_TEMP_MODE[index],
566                                                         data->temp_mode[index]);
567         mutex_unlock(&data->update_lock);
568
569         return count;
570 }
571
572 #define SETUP_PWM_DEFAULT               0
573 #define SETUP_PWM_UPTIME                1       /* Unit in 0.1s */
574 #define SETUP_PWM_DOWNTIME              2       /* Unit in 0.1s */
575 #define SETUP_TEMP_CRITICAL             3
576 static ssize_t
577 show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
578 {
579         struct sensor_device_attribute_2 *sensor_attr =
580             to_sensor_dev_attr_2(attr);
581         int nr = sensor_attr->nr;
582         struct w83793_data *data = w83793_update_device(dev);
583         u32 val = 0;
584
585         if (SETUP_PWM_DEFAULT == nr) {
586                 val = (data->pwm_default & 0x3f) << 2;
587         } else if (SETUP_PWM_UPTIME == nr) {
588                 val = TIME_FROM_REG(data->pwm_uptime);
589         } else if (SETUP_PWM_DOWNTIME == nr) {
590                 val = TIME_FROM_REG(data->pwm_downtime);
591         } else if (SETUP_TEMP_CRITICAL == nr) {
592                 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
593         }
594
595         return sprintf(buf, "%d\n", val);
596 }
597
598 static ssize_t
599 store_sf_setup(struct device *dev, struct device_attribute *attr,
600                const char *buf, size_t count)
601 {
602         struct sensor_device_attribute_2 *sensor_attr =
603             to_sensor_dev_attr_2(attr);
604         int nr = sensor_attr->nr;
605         struct i2c_client *client = to_i2c_client(dev);
606         struct w83793_data *data = i2c_get_clientdata(client);
607
608         mutex_lock(&data->update_lock);
609         if (SETUP_PWM_DEFAULT == nr) {
610                 data->pwm_default =
611                     w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
612                 data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
613                                                                   10),
614                                                    0, 0xff) >> 2;
615                 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
616                                                         data->pwm_default);
617         } else if (SETUP_PWM_UPTIME == nr) {
618                 data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
619                 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
620                 w83793_write_value(client, W83793_REG_PWM_UPTIME,
621                                                         data->pwm_uptime);
622         } else if (SETUP_PWM_DOWNTIME == nr) {
623                 data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
624                 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
625                 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
626                                                         data->pwm_downtime);
627         } else {                /* SETUP_TEMP_CRITICAL */
628                 data->temp_critical =
629                     w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
630                 data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
631                                                    0, 0x7f);
632                 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
633                                                         data->temp_critical);
634         }
635
636         mutex_unlock(&data->update_lock);
637         return count;
638 }
639
640 /*
641         Temp SmartFan control
642         TEMP_FAN_MAP
643         Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
644         It's possible two or more temp channels control the same fan, w83793
645         always prefers to pick the most critical request and applies it to
646         the related Fan.
647         It's possible one fan is not in any mapping of 6 temp channels, this
648         means the fan is manual mode
649
650         TEMP_PWM_ENABLE
651         Each temp channel has its own SmartFan mode, and temp channel
652         control fans that are set by TEMP_FAN_MAP
653         0:      SmartFanII mode
654         1:      Thermal Cruise Mode
655
656         TEMP_CRUISE
657         Target temperature in thermal cruise mode, w83793 will try to turn
658         fan speed to keep the temperature of target device around this
659         temperature.
660
661         TEMP_TOLERANCE
662         If Temp higher or lower than target with this tolerance, w83793
663         will take actions to speed up or slow down the fan to keep the
664         temperature within the tolerance range.
665 */
666
667 #define TEMP_FAN_MAP                    0
668 #define TEMP_PWM_ENABLE                 1
669 #define TEMP_CRUISE                     2
670 #define TEMP_TOLERANCE                  3
671 static ssize_t
672 show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
673 {
674         struct sensor_device_attribute_2 *sensor_attr =
675             to_sensor_dev_attr_2(attr);
676         int nr = sensor_attr->nr;
677         int index = sensor_attr->index;
678         struct w83793_data *data = w83793_update_device(dev);
679         u32 val;
680
681         if (TEMP_FAN_MAP == nr) {
682                 val = data->temp_fan_map[index];
683         } else if (TEMP_PWM_ENABLE == nr) {
684                 /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
685                 val = ((data->pwm_enable >> index) & 0x01) + 2;
686         } else if (TEMP_CRUISE == nr) {
687                 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
688         } else {                /* TEMP_TOLERANCE */
689                 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
690                 val = TEMP_FROM_REG(val & 0x0f);
691         }
692         return sprintf(buf, "%d\n", val);
693 }
694
695 static ssize_t
696 store_sf_ctrl(struct device *dev, struct device_attribute *attr,
697               const char *buf, size_t count)
698 {
699         struct sensor_device_attribute_2 *sensor_attr =
700             to_sensor_dev_attr_2(attr);
701         int nr = sensor_attr->nr;
702         int index = sensor_attr->index;
703         struct i2c_client *client = to_i2c_client(dev);
704         struct w83793_data *data = i2c_get_clientdata(client);
705         u32 val;
706
707         mutex_lock(&data->update_lock);
708         if (TEMP_FAN_MAP == nr) {
709                 val = simple_strtoul(buf, NULL, 10) & 0xff;
710                 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
711                 data->temp_fan_map[index] = val;
712         } else if (TEMP_PWM_ENABLE == nr) {
713                 val = simple_strtoul(buf, NULL, 10);
714                 if (2 == val || 3 == val) {
715                         data->pwm_enable =
716                             w83793_read_value(client, W83793_REG_PWM_ENABLE);
717                         if (val - 2)
718                                 data->pwm_enable |= 1 << index;
719                         else
720                                 data->pwm_enable &= ~(1 << index);
721                         w83793_write_value(client, W83793_REG_PWM_ENABLE,
722                                                         data->pwm_enable);
723                 } else {
724                         mutex_unlock(&data->update_lock);
725                         return -EINVAL;
726                 }
727         } else if (TEMP_CRUISE == nr) {
728                 data->temp_cruise[index] =
729                     w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
730                 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
731                 data->temp_cruise[index] &= 0x80;
732                 data->temp_cruise[index] |= val;
733
734                 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
735                                                 data->temp_cruise[index]);
736         } else {                /* TEMP_TOLERANCE */
737                 int i = index >> 1;
738                 u8 shift = (index & 0x01) ? 4 : 0;
739                 data->tolerance[i] =
740                     w83793_read_value(client, W83793_REG_TEMP_TOL(i));
741
742                 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
743                 data->tolerance[i] &= ~(0x0f << shift);
744                 data->tolerance[i] |= val << shift;
745                 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
746                                                         data->tolerance[i]);
747         }
748
749         mutex_unlock(&data->update_lock);
750         return count;
751 }
752
753 static ssize_t
754 show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
755 {
756         struct sensor_device_attribute_2 *sensor_attr =
757             to_sensor_dev_attr_2(attr);
758         int nr = sensor_attr->nr;
759         int index = sensor_attr->index;
760         struct w83793_data *data = w83793_update_device(dev);
761
762         return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
763 }
764
765 static ssize_t
766 store_sf2_pwm(struct device *dev, struct device_attribute *attr,
767               const char *buf, size_t count)
768 {
769         struct i2c_client *client = to_i2c_client(dev);
770         struct w83793_data *data = i2c_get_clientdata(client);
771         struct sensor_device_attribute_2 *sensor_attr =
772             to_sensor_dev_attr_2(attr);
773         int nr = sensor_attr->nr;
774         int index = sensor_attr->index;
775         u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
776
777         mutex_lock(&data->update_lock);
778         data->sf2_pwm[index][nr] =
779             w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
780         data->sf2_pwm[index][nr] |= val;
781         w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
782                                                 data->sf2_pwm[index][nr]);
783         mutex_unlock(&data->update_lock);
784         return count;
785 }
786
787 static ssize_t
788 show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
789 {
790         struct sensor_device_attribute_2 *sensor_attr =
791             to_sensor_dev_attr_2(attr);
792         int nr = sensor_attr->nr;
793         int index = sensor_attr->index;
794         struct w83793_data *data = w83793_update_device(dev);
795
796         return sprintf(buf, "%ld\n",
797                        TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
798 }
799
800 static ssize_t
801 store_sf2_temp(struct device *dev, struct device_attribute *attr,
802                const char *buf, size_t count)
803 {
804         struct i2c_client *client = to_i2c_client(dev);
805         struct w83793_data *data = i2c_get_clientdata(client);
806         struct sensor_device_attribute_2 *sensor_attr =
807             to_sensor_dev_attr_2(attr);
808         int nr = sensor_attr->nr;
809         int index = sensor_attr->index;
810         u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
811
812         mutex_lock(&data->update_lock);
813         data->sf2_temp[index][nr] =
814             w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
815         data->sf2_temp[index][nr] |= val;
816         w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
817                                              data->sf2_temp[index][nr]);
818         mutex_unlock(&data->update_lock);
819         return count;
820 }
821
822 /* only Vcore A/B and Vtt have additional 2 bits precision */
823 static ssize_t
824 show_in(struct device *dev, struct device_attribute *attr, char *buf)
825 {
826         struct sensor_device_attribute_2 *sensor_attr =
827             to_sensor_dev_attr_2(attr);
828         int nr = sensor_attr->nr;
829         int index = sensor_attr->index;
830         struct w83793_data *data = w83793_update_device(dev);
831         u16 val = data->in[index][nr];
832
833         if (index < 3) {
834                 val <<= 2;
835                 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
836         }
837         /* voltage inputs 5VDD and 5VSB needs 150mV offset */
838         val = val * scale_in[index] + scale_in_add[index];
839         return sprintf(buf, "%d\n", val);
840 }
841
842 static ssize_t
843 store_in(struct device *dev, struct device_attribute *attr,
844          const char *buf, size_t count)
845 {
846         struct sensor_device_attribute_2 *sensor_attr =
847             to_sensor_dev_attr_2(attr);
848         int nr = sensor_attr->nr;
849         int index = sensor_attr->index;
850         struct i2c_client *client = to_i2c_client(dev);
851         struct w83793_data *data = i2c_get_clientdata(client);
852         u32 val;
853
854         val =
855             (simple_strtoul(buf, NULL, 10) +
856              scale_in[index] / 2) / scale_in[index];
857         mutex_lock(&data->update_lock);
858         if (index > 2) {
859                 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
860                 if (1 == nr || 2 == nr) {
861                         val -= scale_in_add[index] / scale_in[index];
862                 }
863                 val = SENSORS_LIMIT(val, 0, 255);
864         } else {
865                 val = SENSORS_LIMIT(val, 0, 0x3FF);
866                 data->in_low_bits[nr] =
867                     w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
868                 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
869                 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
870                 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
871                                                      data->in_low_bits[nr]);
872                 val >>= 2;
873         }
874         data->in[index][nr] = val;
875         w83793_write_value(client, W83793_REG_IN[index][nr],
876                                                         data->in[index][nr]);
877         mutex_unlock(&data->update_lock);
878         return count;
879 }
880
881 #define NOT_USED                        -1
882
883 #define SENSOR_ATTR_IN(index)                                           \
884         SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL,        \
885                 IN_READ, index),                                        \
886         SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in,      \
887                 store_in, IN_MAX, index),                               \
888         SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in,      \
889                 store_in, IN_LOW, index),                               \
890         SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep,      \
891                 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)),     \
892         SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO,              \
893                 show_alarm_beep, store_beep, BEEP_ENABLE,               \
894                 index + ((index > 2) ? 1 : 0))
895
896 #define SENSOR_ATTR_FAN(index)                                          \
897         SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep,     \
898                 NULL, ALARM_STATUS, index + 17),                        \
899         SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO,             \
900                 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17),  \
901         SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan,            \
902                 NULL, FAN_INPUT, index - 1),                            \
903         SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO,              \
904                 show_fan, store_fan_min, FAN_MIN, index - 1)
905
906 #define SENSOR_ATTR_PWM(index)                                          \
907         SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm,          \
908                 store_pwm, PWM_DUTY, index - 1),                        \
909         SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO,          \
910                 show_pwm, store_pwm, PWM_NONSTOP, index - 1),           \
911         SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO,            \
912                 show_pwm, store_pwm, PWM_START, index - 1),             \
913         SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO,        \
914                 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
915
916 #define SENSOR_ATTR_TEMP(index)                                         \
917         SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR,            \
918                 show_temp_mode, store_temp_mode, NOT_USED, index - 1),  \
919         SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp,          \
920                 NULL, TEMP_READ, index - 1),                            \
921         SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp,  \
922                 store_temp, TEMP_CRIT, index - 1),                      \
923         SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR,        \
924                 show_temp, store_temp, TEMP_CRIT_HYST, index - 1),      \
925         SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
926                 store_temp, TEMP_WARN, index - 1),                      \
927         SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR,       \
928                 show_temp, store_temp, TEMP_WARN_HYST, index - 1),      \
929         SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO,                     \
930                 show_alarm_beep, NULL, ALARM_STATUS, index + 11),       \
931         SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO,            \
932                 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11),  \
933         SENSOR_ATTR_2(temp##index##_auto_channels_pwm,                  \
934                 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl,         \
935                 TEMP_FAN_MAP, index - 1),                               \
936         SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO,      \
937                 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE,           \
938                 index - 1),                                             \
939         SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR,         \
940                 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1),   \
941         SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
942                 store_sf_ctrl, TEMP_TOLERANCE, index - 1),              \
943         SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
944                 show_sf2_pwm, store_sf2_pwm, 0, index - 1),             \
945         SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
946                 show_sf2_pwm, store_sf2_pwm, 1, index - 1),             \
947         SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
948                 show_sf2_pwm, store_sf2_pwm, 2, index - 1),             \
949         SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
950                 show_sf2_pwm, store_sf2_pwm, 3, index - 1),             \
951         SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
952                 show_sf2_pwm, store_sf2_pwm, 4, index - 1),             \
953         SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
954                 show_sf2_pwm, store_sf2_pwm, 5, index - 1),             \
955         SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
956                 show_sf2_pwm, store_sf2_pwm, 6, index - 1),             \
957         SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
958                 show_sf2_temp, store_sf2_temp, 0, index - 1),           \
959         SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
960                 show_sf2_temp, store_sf2_temp, 1, index - 1),           \
961         SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
962                 show_sf2_temp, store_sf2_temp, 2, index - 1),           \
963         SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
964                 show_sf2_temp, store_sf2_temp, 3, index - 1),           \
965         SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
966                 show_sf2_temp, store_sf2_temp, 4, index - 1),           \
967         SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
968                 show_sf2_temp, store_sf2_temp, 5, index - 1),           \
969         SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
970                 show_sf2_temp, store_sf2_temp, 6, index - 1)
971
972 static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
973         SENSOR_ATTR_IN(0),
974         SENSOR_ATTR_IN(1),
975         SENSOR_ATTR_IN(2),
976         SENSOR_ATTR_IN(3),
977         SENSOR_ATTR_IN(4),
978         SENSOR_ATTR_IN(5),
979         SENSOR_ATTR_IN(6),
980         SENSOR_ATTR_IN(7),
981         SENSOR_ATTR_IN(8),
982         SENSOR_ATTR_IN(9),
983         SENSOR_ATTR_FAN(1),
984         SENSOR_ATTR_FAN(2),
985         SENSOR_ATTR_FAN(3),
986         SENSOR_ATTR_FAN(4),
987         SENSOR_ATTR_FAN(5),
988         SENSOR_ATTR_PWM(1),
989         SENSOR_ATTR_PWM(2),
990         SENSOR_ATTR_PWM(3),
991 };
992
993 static struct sensor_device_attribute_2 w83793_temp[] = {
994         SENSOR_ATTR_TEMP(1),
995         SENSOR_ATTR_TEMP(2),
996         SENSOR_ATTR_TEMP(3),
997         SENSOR_ATTR_TEMP(4),
998         SENSOR_ATTR_TEMP(5),
999         SENSOR_ATTR_TEMP(6),
1000 };
1001
1002 /* Fan6-Fan12 */
1003 static struct sensor_device_attribute_2 w83793_left_fan[] = {
1004         SENSOR_ATTR_FAN(6),
1005         SENSOR_ATTR_FAN(7),
1006         SENSOR_ATTR_FAN(8),
1007         SENSOR_ATTR_FAN(9),
1008         SENSOR_ATTR_FAN(10),
1009         SENSOR_ATTR_FAN(11),
1010         SENSOR_ATTR_FAN(12),
1011 };
1012
1013 /* Pwm4-Pwm8 */
1014 static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1015         SENSOR_ATTR_PWM(4),
1016         SENSOR_ATTR_PWM(5),
1017         SENSOR_ATTR_PWM(6),
1018         SENSOR_ATTR_PWM(7),
1019         SENSOR_ATTR_PWM(8),
1020 };
1021
1022 static struct sensor_device_attribute_2 w83793_vid[] = {
1023         SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1024         SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
1025 };
1026
1027 static struct sensor_device_attribute_2 sda_single_files[] = {
1028         SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
1029                       NOT_USED, NOT_USED),
1030         SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
1031                       store_chassis_clear, ALARM_STATUS, 30),
1032         SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1033                       store_beep_enable, NOT_USED, NOT_USED),
1034         SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1035                       store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1036         SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1037                       store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1038         SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1039                       store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1040         SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1041                       store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1042 };
1043
1044 static void w83793_init_client(struct i2c_client *client)
1045 {
1046         if (reset) {
1047                 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
1048         }
1049
1050         /* Start monitoring */
1051         w83793_write_value(client, W83793_REG_CONFIG,
1052                            w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
1053
1054 }
1055
1056 static int w83793_attach_adapter(struct i2c_adapter *adapter)
1057 {
1058         if (!(adapter->class & I2C_CLASS_HWMON))
1059                 return 0;
1060         return i2c_probe(adapter, &addr_data, w83793_detect);
1061 }
1062
1063 static int w83793_detach_client(struct i2c_client *client)
1064 {
1065         struct w83793_data *data = i2c_get_clientdata(client);
1066         struct device *dev = &client->dev;
1067         int err, i;
1068
1069         /* main client */
1070         if (data) {
1071                 hwmon_device_unregister(data->hwmon_dev);
1072
1073                 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1074                         device_remove_file(dev,
1075                                            &w83793_sensor_attr_2[i].dev_attr);
1076
1077                 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1078                         device_remove_file(dev, &sda_single_files[i].dev_attr);
1079
1080                 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1081                         device_remove_file(dev, &w83793_vid[i].dev_attr);
1082
1083                 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1084                         device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1085
1086                 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1087                         device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1088
1089                 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1090                         device_remove_file(dev, &w83793_temp[i].dev_attr);
1091         }
1092
1093         if ((err = i2c_detach_client(client)))
1094                 return err;
1095
1096         /* main client */
1097         if (data)
1098                 kfree(data);
1099         /* subclient */
1100         else
1101                 kfree(client);
1102
1103         return 0;
1104 }
1105
1106 static int
1107 w83793_create_subclient(struct i2c_adapter *adapter,
1108                         struct i2c_client *client, int addr,
1109                         struct i2c_client **sub_cli)
1110 {
1111         int err = 0;
1112         struct i2c_client *sub_client;
1113
1114         (*sub_cli) = sub_client =
1115             kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1116         if (!(sub_client)) {
1117                 return -ENOMEM;
1118         }
1119         sub_client->addr = 0x48 + addr;
1120         i2c_set_clientdata(sub_client, NULL);
1121         sub_client->adapter = adapter;
1122         sub_client->driver = &w83793_driver;
1123         strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
1124         if ((err = i2c_attach_client(sub_client))) {
1125                 dev_err(&client->dev, "subclient registration "
1126                         "at address 0x%x failed\n", sub_client->addr);
1127                 kfree(sub_client);
1128         }
1129         return err;
1130 }
1131
1132 static int
1133 w83793_detect_subclients(struct i2c_adapter *adapter, int address,
1134                          int kind, struct i2c_client *client)
1135 {
1136         int i, id, err;
1137         u8 tmp;
1138         struct w83793_data *data = i2c_get_clientdata(client);
1139
1140         id = i2c_adapter_id(adapter);
1141         if (force_subclients[0] == id && force_subclients[1] == address) {
1142                 for (i = 2; i <= 3; i++) {
1143                         if (force_subclients[i] < 0x48
1144                             || force_subclients[i] > 0x4f) {
1145                                 dev_err(&client->dev,
1146                                         "invalid subclient "
1147                                         "address %d; must be 0x48-0x4f\n",
1148                                         force_subclients[i]);
1149                                 err = -EINVAL;
1150                                 goto ERROR_SC_0;
1151                         }
1152                 }
1153                 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1154                                    (force_subclients[2] & 0x07) |
1155                                    ((force_subclients[3] & 0x07) << 4));
1156         }
1157
1158         tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
1159         if (!(tmp & 0x08)) {
1160                 err =
1161                     w83793_create_subclient(adapter, client, tmp & 0x7,
1162                                             &data->lm75[0]);
1163                 if (err < 0)
1164                         goto ERROR_SC_0;
1165         }
1166         if (!(tmp & 0x80)) {
1167                 if ((data->lm75[0] != NULL)
1168                     && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1169                         dev_err(&client->dev,
1170                                 "duplicate addresses 0x%x, "
1171                                 "use force_subclients\n", data->lm75[0]->addr);
1172                         err = -ENODEV;
1173                         goto ERROR_SC_1;
1174                 }
1175                 err = w83793_create_subclient(adapter, client,
1176                                               (tmp >> 4) & 0x7, &data->lm75[1]);
1177                 if (err < 0)
1178                         goto ERROR_SC_1;
1179         }
1180
1181         return 0;
1182
1183         /* Undo inits in case of errors */
1184
1185 ERROR_SC_1:
1186         if (data->lm75[0] != NULL) {
1187                 i2c_detach_client(data->lm75[0]);
1188                 kfree(data->lm75[0]);
1189         }
1190 ERROR_SC_0:
1191         return err;
1192 }
1193
1194 static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
1195 {
1196         int i;
1197         u8 tmp, val;
1198         struct i2c_client *client;
1199         struct device *dev;
1200         struct w83793_data *data;
1201         int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1202         int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1203         int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1204         int err = 0;
1205
1206         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1207                 goto exit;
1208         }
1209
1210         /* OK. For now, we presume we have a valid client. We now create the
1211            client structure, even though we cannot fill it completely yet.
1212            But it allows us to access w83793_{read,write}_value. */
1213
1214         if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
1215                 err = -ENOMEM;
1216                 goto exit;
1217         }
1218
1219         client = &data->client;
1220         dev = &client->dev;
1221         i2c_set_clientdata(client, data);
1222         client->addr = address;
1223         client->adapter = adapter;
1224         client->driver = &w83793_driver;
1225
1226         data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
1227
1228         /* Now, we do the remaining detection. */
1229         if (kind < 0) {
1230                 tmp = data->bank & 0x80 ? 0x5c : 0xa3;
1231                 /* Check Winbond vendor ID */
1232                 if (tmp != i2c_smbus_read_byte_data(client,
1233                                                         W83793_REG_VENDORID)) {
1234                         pr_debug("w83793: Detection failed at check "
1235                                  "vendor id\n");
1236                         err = -ENODEV;
1237                         goto free_mem;
1238                 }
1239
1240                 /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1241                    should match */
1242                 if ((data->bank & 0x07) == 0
1243                  && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1244                     (address << 1)) {
1245                         pr_debug("w83793: Detection failed at check "
1246                                  "i2c addr\n");
1247                         err = -ENODEV;
1248                         goto free_mem;
1249                 }
1250
1251         }
1252
1253         /* We have either had a force parameter, or we have already detected the
1254            Winbond. Determine the chip type now */
1255
1256         if (kind <= 0) {
1257                 if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
1258                         kind = w83793;
1259                 } else {
1260                         if (kind == 0)
1261                                 dev_warn(&adapter->dev, "w83793: Ignoring "
1262                                          "'force' parameter for unknown chip "
1263                                          "at address 0x%02x\n", address);
1264                         err = -ENODEV;
1265                         goto free_mem;
1266                 }
1267         }
1268
1269         /* Fill in the remaining client fields and put into the global list */
1270         strlcpy(client->name, "w83793", I2C_NAME_SIZE);
1271
1272         mutex_init(&data->update_lock);
1273
1274         /* Tell the I2C layer a new client has arrived */
1275         if ((err = i2c_attach_client(client)))
1276                 goto free_mem;
1277
1278         if ((err = w83793_detect_subclients(adapter, address, kind, client)))
1279                 goto detach_client;
1280
1281         /* Initialize the chip */
1282         w83793_init_client(client);
1283
1284         data->vrm = vid_which_vrm();
1285         /*
1286            Only fan 1-5 has their own input pins,
1287            Pwm 1-3 has their own pins
1288          */
1289         data->has_fan = 0x1f;
1290         data->has_pwm = 0x07;
1291         tmp = w83793_read_value(client, W83793_REG_MFC);
1292         val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1293
1294         /* check the function of pins 49-56 */
1295         if (!(tmp & 0x80)) {
1296                 data->has_pwm |= 0x18;  /* pwm 4,5 */
1297                 if (val & 0x01) {       /* fan 6 */
1298                         data->has_fan |= 0x20;
1299                         data->has_pwm |= 0x20;
1300                 }
1301                 if (val & 0x02) {       /* fan 7 */
1302                         data->has_fan |= 0x40;
1303                         data->has_pwm |= 0x40;
1304                 }
1305                 if (!(tmp & 0x40) && (val & 0x04)) {    /* fan 8 */
1306                         data->has_fan |= 0x80;
1307                         data->has_pwm |= 0x80;
1308                 }
1309         }
1310
1311         if (0x08 == (tmp & 0x0c)) {
1312                 if (val & 0x08) /* fan 9 */
1313                         data->has_fan |= 0x100;
1314                 if (val & 0x10) /* fan 10 */
1315                         data->has_fan |= 0x200;
1316         }
1317
1318         if (0x20 == (tmp & 0x30)) {
1319                 if (val & 0x20) /* fan 11 */
1320                         data->has_fan |= 0x400;
1321                 if (val & 0x40) /* fan 12 */
1322                         data->has_fan |= 0x800;
1323         }
1324
1325         if ((tmp & 0x01) && (val & 0x04)) {     /* fan 8, second location */
1326                 data->has_fan |= 0x80;
1327                 data->has_pwm |= 0x80;
1328         }
1329
1330         tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1331         if ((tmp & 0x01) && (val & 0x08)) {     /* fan 9, second location */
1332                 data->has_fan |= 0x100;
1333         }
1334         if ((tmp & 0x02) && (val & 0x10)) {     /* fan 10, second location */
1335                 data->has_fan |= 0x200;
1336         }
1337         if ((tmp & 0x04) && (val & 0x20)) {     /* fan 11, second location */
1338                 data->has_fan |= 0x400;
1339         }
1340         if ((tmp & 0x08) && (val & 0x40)) {     /* fan 12, second location */
1341                 data->has_fan |= 0x800;
1342         }
1343
1344         /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1345         tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1346         if (tmp & 0x01)
1347                 data->has_temp |= 0x01;
1348         if (tmp & 0x04)
1349                 data->has_temp |= 0x02;
1350         if (tmp & 0x10)
1351                 data->has_temp |= 0x04;
1352         if (tmp & 0x40)
1353                 data->has_temp |= 0x08;
1354
1355         tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1356         if (tmp & 0x01)
1357                 data->has_temp |= 0x10;
1358         if (tmp & 0x02)
1359                 data->has_temp |= 0x20;
1360
1361         /* Detect the VID usage and ignore unused input */
1362         tmp = w83793_read_value(client, W83793_REG_MFC);
1363         if (!(tmp & 0x29))
1364                 data->has_vid |= 0x1;   /* has VIDA */
1365         if (tmp & 0x80)
1366                 data->has_vid |= 0x2;   /* has VIDB */
1367
1368         /* Register sysfs hooks */
1369         for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1370                 err = device_create_file(dev,
1371                                          &w83793_sensor_attr_2[i].dev_attr);
1372                 if (err)
1373                         goto exit_remove;
1374         }
1375
1376         for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1377                 if (!(data->has_vid & (1 << i)))
1378                         continue;
1379                 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1380                 if (err)
1381                         goto exit_remove;
1382         }
1383
1384         for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1385                 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1386                 if (err)
1387                         goto exit_remove;
1388
1389         }
1390
1391         for (i = 0; i < 6; i++) {
1392                 int j;
1393                 if (!(data->has_temp & (1 << i)))
1394                         continue;
1395                 for (j = 0; j < files_temp; j++) {
1396                         err = device_create_file(dev,
1397                                                 &w83793_temp[(i) * files_temp
1398                                                                 + j].dev_attr);
1399                         if (err)
1400                                 goto exit_remove;
1401                 }
1402         }
1403
1404         for (i = 5; i < 12; i++) {
1405                 int j;
1406                 if (!(data->has_fan & (1 << i)))
1407                         continue;
1408                 for (j = 0; j < files_fan; j++) {
1409                         err = device_create_file(dev,
1410                                            &w83793_left_fan[(i - 5) * files_fan
1411                                                                 + j].dev_attr);
1412                         if (err)
1413                                 goto exit_remove;
1414                 }
1415         }
1416
1417         for (i = 3; i < 8; i++) {
1418                 int j;
1419                 if (!(data->has_pwm & (1 << i)))
1420                         continue;
1421                 for (j = 0; j < files_pwm; j++) {
1422                         err = device_create_file(dev,
1423                                            &w83793_left_pwm[(i - 3) * files_pwm
1424                                                                 + j].dev_attr);
1425                         if (err)
1426                                 goto exit_remove;
1427                 }
1428         }
1429
1430         data->hwmon_dev = hwmon_device_register(dev);
1431         if (IS_ERR(data->hwmon_dev)) {
1432                 err = PTR_ERR(data->hwmon_dev);
1433                 goto exit_remove;
1434         }
1435
1436         return 0;
1437
1438         /* Unregister sysfs hooks */
1439
1440 exit_remove:
1441         for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1442                 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1443
1444         for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1445                 device_remove_file(dev, &sda_single_files[i].dev_attr);
1446
1447         for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1448                 device_remove_file(dev, &w83793_vid[i].dev_attr);
1449
1450         for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1451                 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1452
1453         for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1454                 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1455
1456         for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1457                 device_remove_file(dev, &w83793_temp[i].dev_attr);
1458
1459         if (data->lm75[0] != NULL) {
1460                 i2c_detach_client(data->lm75[0]);
1461                 kfree(data->lm75[0]);
1462         }
1463         if (data->lm75[1] != NULL) {
1464                 i2c_detach_client(data->lm75[1]);
1465                 kfree(data->lm75[1]);
1466         }
1467 detach_client:
1468         i2c_detach_client(client);
1469 free_mem:
1470         kfree(data);
1471 exit:
1472         return err;
1473 }
1474
1475 static void w83793_update_nonvolatile(struct device *dev)
1476 {
1477         struct i2c_client *client = to_i2c_client(dev);
1478         struct w83793_data *data = i2c_get_clientdata(client);
1479         int i, j;
1480         /*
1481            They are somewhat "stable" registers, and to update them everytime
1482            takes so much time, it's just not worthy. Update them in a long
1483            interval to avoid exception.
1484          */
1485         if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1486               || !data->valid))
1487                 return;
1488         /* update voltage limits */
1489         for (i = 1; i < 3; i++) {
1490                 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1491                         data->in[j][i] =
1492                             w83793_read_value(client, W83793_REG_IN[j][i]);
1493                 }
1494                 data->in_low_bits[i] =
1495                     w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1496         }
1497
1498         for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1499                 /* Update the Fan measured value and limits */
1500                 if (!(data->has_fan & (1 << i))) {
1501                         continue;
1502                 }
1503                 data->fan_min[i] =
1504                     w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1505                 data->fan_min[i] |=
1506                     w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1507         }
1508
1509         for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1510                 if (!(data->has_temp & (1 << i)))
1511                         continue;
1512                 data->temp_fan_map[i] =
1513                     w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1514                 for (j = 1; j < 5; j++) {
1515                         data->temp[i][j] =
1516                             w83793_read_value(client, W83793_REG_TEMP[i][j]);
1517                 }
1518                 data->temp_cruise[i] =
1519                     w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1520                 for (j = 0; j < 7; j++) {
1521                         data->sf2_pwm[i][j] =
1522                             w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1523                         data->sf2_temp[i][j] =
1524                             w83793_read_value(client,
1525                                               W83793_REG_SF2_TEMP(i, j));
1526                 }
1527         }
1528
1529         for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1530                 data->temp_mode[i] =
1531                     w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1532
1533         for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1534                 data->tolerance[i] =
1535                     w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1536         }
1537
1538         for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1539                 if (!(data->has_pwm & (1 << i)))
1540                         continue;
1541                 data->pwm[i][PWM_NONSTOP] =
1542                     w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1543                 data->pwm[i][PWM_START] =
1544                     w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1545                 data->pwm_stop_time[i] =
1546                     w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1547         }
1548
1549         data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1550         data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1551         data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1552         data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1553         data->temp_critical =
1554             w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1555         data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1556
1557         for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1558                 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1559         }
1560
1561         data->last_nonvolatile = jiffies;
1562 }
1563
1564 static struct w83793_data *w83793_update_device(struct device *dev)
1565 {
1566         struct i2c_client *client = to_i2c_client(dev);
1567         struct w83793_data *data = i2c_get_clientdata(client);
1568         int i;
1569
1570         mutex_lock(&data->update_lock);
1571
1572         if (!(time_after(jiffies, data->last_updated + HZ * 2)
1573               || !data->valid))
1574                 goto END;
1575
1576         /* Update the voltages measured value and limits */
1577         for (i = 0; i < ARRAY_SIZE(data->in); i++)
1578                 data->in[i][IN_READ] =
1579                     w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1580
1581         data->in_low_bits[IN_READ] =
1582             w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1583
1584         for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1585                 if (!(data->has_fan & (1 << i))) {
1586                         continue;
1587                 }
1588                 data->fan[i] =
1589                     w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1590                 data->fan[i] |=
1591                     w83793_read_value(client, W83793_REG_FAN(i) + 1);
1592         }
1593
1594         for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
1595                 if (!(data->has_temp & (1 << i)))
1596                         continue;
1597                 data->temp[i][TEMP_READ] =
1598                     w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1599         }
1600
1601         data->temp_low_bits =
1602             w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1603
1604         for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1605                 if (data->has_pwm & (1 << i))
1606                         data->pwm[i][PWM_DUTY] =
1607                             w83793_read_value(client,
1608                                               W83793_REG_PWM(i, PWM_DUTY));
1609         }
1610
1611         for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1612                 data->alarms[i] =
1613                     w83793_read_value(client, W83793_REG_ALARM(i));
1614         if (data->has_vid & 0x01)
1615                 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1616         if (data->has_vid & 0x02)
1617                 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1618         w83793_update_nonvolatile(dev);
1619         data->last_updated = jiffies;
1620         data->valid = 1;
1621
1622 END:
1623         mutex_unlock(&data->update_lock);
1624         return data;
1625 }
1626
1627 /* Ignore the possibility that somebody change bank outside the driver
1628    Must be called with data->update_lock held, except during initialization */
1629 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1630 {
1631         struct w83793_data *data = i2c_get_clientdata(client);
1632         u8 res = 0xff;
1633         u8 new_bank = reg >> 8;
1634
1635         new_bank |= data->bank & 0xfc;
1636         if (data->bank != new_bank) {
1637                 if (i2c_smbus_write_byte_data
1638                     (client, W83793_REG_BANKSEL, new_bank) >= 0)
1639                         data->bank = new_bank;
1640                 else {
1641                         dev_err(&client->dev,
1642                                 "set bank to %d failed, fall back "
1643                                 "to bank %d, read reg 0x%x error\n",
1644                                 new_bank, data->bank, reg);
1645                         res = 0x0;      /* read 0x0 from the chip */
1646                         goto END;
1647                 }
1648         }
1649         res = i2c_smbus_read_byte_data(client, reg & 0xff);
1650 END:
1651         return res;
1652 }
1653
1654 /* Must be called with data->update_lock held, except during initialization */
1655 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1656 {
1657         struct w83793_data *data = i2c_get_clientdata(client);
1658         int res;
1659         u8 new_bank = reg >> 8;
1660
1661         new_bank |= data->bank & 0xfc;
1662         if (data->bank != new_bank) {
1663                 if ((res = i2c_smbus_write_byte_data
1664                     (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1665                         data->bank = new_bank;
1666                 else {
1667                         dev_err(&client->dev,
1668                                 "set bank to %d failed, fall back "
1669                                 "to bank %d, write reg 0x%x error\n",
1670                                 new_bank, data->bank, reg);
1671                         goto END;
1672                 }
1673         }
1674
1675         res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1676 END:
1677         return res;
1678 }
1679
1680 static int __init sensors_w83793_init(void)
1681 {
1682         return i2c_add_driver(&w83793_driver);
1683 }
1684
1685 static void __exit sensors_w83793_exit(void)
1686 {
1687         i2c_del_driver(&w83793_driver);
1688 }
1689
1690 MODULE_AUTHOR("Yuan Mu");
1691 MODULE_DESCRIPTION("w83793 driver");
1692 MODULE_LICENSE("GPL");
1693
1694 module_init(sensors_w83793_init);
1695 module_exit(sensors_w83793_exit);