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