hwmon: (w83793) VID and VRM handling cleanups
[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 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 static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
1028
1029 static struct sensor_device_attribute_2 sda_single_files[] = {
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                 device_remove_file(dev, &dev_attr_vrm);
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         /*
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_vid |= 0x2;   /* has VIDB */
1297         } else {
1298                 data->has_pwm |= 0x18;  /* pwm 4,5 */
1299                 if (val & 0x01) {       /* fan 6 */
1300                         data->has_fan |= 0x20;
1301                         data->has_pwm |= 0x20;
1302                 }
1303                 if (val & 0x02) {       /* fan 7 */
1304                         data->has_fan |= 0x40;
1305                         data->has_pwm |= 0x40;
1306                 }
1307                 if (!(tmp & 0x40) && (val & 0x04)) {    /* fan 8 */
1308                         data->has_fan |= 0x80;
1309                         data->has_pwm |= 0x80;
1310                 }
1311         }
1312
1313         /* check the function of pins 37-40 */
1314         if (!(tmp & 0x29))
1315                 data->has_vid |= 0x1;   /* has VIDA */
1316         if (0x08 == (tmp & 0x0c)) {
1317                 if (val & 0x08) /* fan 9 */
1318                         data->has_fan |= 0x100;
1319                 if (val & 0x10) /* fan 10 */
1320                         data->has_fan |= 0x200;
1321         }
1322         if (0x20 == (tmp & 0x30)) {
1323                 if (val & 0x20) /* fan 11 */
1324                         data->has_fan |= 0x400;
1325                 if (val & 0x40) /* fan 12 */
1326                         data->has_fan |= 0x800;
1327         }
1328
1329         if ((tmp & 0x01) && (val & 0x04)) {     /* fan 8, second location */
1330                 data->has_fan |= 0x80;
1331                 data->has_pwm |= 0x80;
1332         }
1333
1334         tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1335         if ((tmp & 0x01) && (val & 0x08)) {     /* fan 9, second location */
1336                 data->has_fan |= 0x100;
1337         }
1338         if ((tmp & 0x02) && (val & 0x10)) {     /* fan 10, second location */
1339                 data->has_fan |= 0x200;
1340         }
1341         if ((tmp & 0x04) && (val & 0x20)) {     /* fan 11, second location */
1342                 data->has_fan |= 0x400;
1343         }
1344         if ((tmp & 0x08) && (val & 0x40)) {     /* fan 12, second location */
1345                 data->has_fan |= 0x800;
1346         }
1347
1348         /* check the temp1-6 mode, ignore former AMDSI selected inputs */
1349         tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
1350         if (tmp & 0x01)
1351                 data->has_temp |= 0x01;
1352         if (tmp & 0x04)
1353                 data->has_temp |= 0x02;
1354         if (tmp & 0x10)
1355                 data->has_temp |= 0x04;
1356         if (tmp & 0x40)
1357                 data->has_temp |= 0x08;
1358
1359         tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
1360         if (tmp & 0x01)
1361                 data->has_temp |= 0x10;
1362         if (tmp & 0x02)
1363                 data->has_temp |= 0x20;
1364
1365         /* Register sysfs hooks */
1366         for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1367                 err = device_create_file(dev,
1368                                          &w83793_sensor_attr_2[i].dev_attr);
1369                 if (err)
1370                         goto exit_remove;
1371         }
1372
1373         for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1374                 if (!(data->has_vid & (1 << i)))
1375                         continue;
1376                 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1377                 if (err)
1378                         goto exit_remove;
1379         }
1380         if (data->has_vid) {
1381                 data->vrm = vid_which_vrm();
1382                 err = device_create_file(dev, &dev_attr_vrm);
1383                 if (err)
1384                         goto exit_remove;
1385         }
1386
1387         for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1388                 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1389                 if (err)
1390                         goto exit_remove;
1391
1392         }
1393
1394         for (i = 0; i < 6; i++) {
1395                 int j;
1396                 if (!(data->has_temp & (1 << i)))
1397                         continue;
1398                 for (j = 0; j < files_temp; j++) {
1399                         err = device_create_file(dev,
1400                                                 &w83793_temp[(i) * files_temp
1401                                                                 + j].dev_attr);
1402                         if (err)
1403                                 goto exit_remove;
1404                 }
1405         }
1406
1407         for (i = 5; i < 12; i++) {
1408                 int j;
1409                 if (!(data->has_fan & (1 << i)))
1410                         continue;
1411                 for (j = 0; j < files_fan; j++) {
1412                         err = device_create_file(dev,
1413                                            &w83793_left_fan[(i - 5) * files_fan
1414                                                                 + j].dev_attr);
1415                         if (err)
1416                                 goto exit_remove;
1417                 }
1418         }
1419
1420         for (i = 3; i < 8; i++) {
1421                 int j;
1422                 if (!(data->has_pwm & (1 << i)))
1423                         continue;
1424                 for (j = 0; j < files_pwm; j++) {
1425                         err = device_create_file(dev,
1426                                            &w83793_left_pwm[(i - 3) * files_pwm
1427                                                                 + j].dev_attr);
1428                         if (err)
1429                                 goto exit_remove;
1430                 }
1431         }
1432
1433         data->hwmon_dev = hwmon_device_register(dev);
1434         if (IS_ERR(data->hwmon_dev)) {
1435                 err = PTR_ERR(data->hwmon_dev);
1436                 goto exit_remove;
1437         }
1438
1439         return 0;
1440
1441         /* Unregister sysfs hooks */
1442
1443 exit_remove:
1444         for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1445                 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1446
1447         for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1448                 device_remove_file(dev, &sda_single_files[i].dev_attr);
1449
1450         for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1451                 device_remove_file(dev, &w83793_vid[i].dev_attr);
1452
1453         for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1454                 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1455
1456         for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1457                 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1458
1459         for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1460                 device_remove_file(dev, &w83793_temp[i].dev_attr);
1461
1462         if (data->lm75[0] != NULL) {
1463                 i2c_detach_client(data->lm75[0]);
1464                 kfree(data->lm75[0]);
1465         }
1466         if (data->lm75[1] != NULL) {
1467                 i2c_detach_client(data->lm75[1]);
1468                 kfree(data->lm75[1]);
1469         }
1470 detach_client:
1471         i2c_detach_client(client);
1472 free_mem:
1473         kfree(data);
1474 exit:
1475         return err;
1476 }
1477
1478 static void w83793_update_nonvolatile(struct device *dev)
1479 {
1480         struct i2c_client *client = to_i2c_client(dev);
1481         struct w83793_data *data = i2c_get_clientdata(client);
1482         int i, j;
1483         /*
1484            They are somewhat "stable" registers, and to update them everytime
1485            takes so much time, it's just not worthy. Update them in a long
1486            interval to avoid exception.
1487          */
1488         if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1489               || !data->valid))
1490                 return;
1491         /* update voltage limits */
1492         for (i = 1; i < 3; i++) {
1493                 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1494                         data->in[j][i] =
1495                             w83793_read_value(client, W83793_REG_IN[j][i]);
1496                 }
1497                 data->in_low_bits[i] =
1498                     w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1499         }
1500
1501         for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1502                 /* Update the Fan measured value and limits */
1503                 if (!(data->has_fan & (1 << i))) {
1504                         continue;
1505                 }
1506                 data->fan_min[i] =
1507                     w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1508                 data->fan_min[i] |=
1509                     w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1510         }
1511
1512         for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
1513                 if (!(data->has_temp & (1 << i)))
1514                         continue;
1515                 data->temp_fan_map[i] =
1516                     w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1517                 for (j = 1; j < 5; j++) {
1518                         data->temp[i][j] =
1519                             w83793_read_value(client, W83793_REG_TEMP[i][j]);
1520                 }
1521                 data->temp_cruise[i] =
1522                     w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1523                 for (j = 0; j < 7; j++) {
1524                         data->sf2_pwm[i][j] =
1525                             w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1526                         data->sf2_temp[i][j] =
1527                             w83793_read_value(client,
1528                                               W83793_REG_SF2_TEMP(i, j));
1529                 }
1530         }
1531
1532         for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1533                 data->temp_mode[i] =
1534                     w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1535
1536         for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1537                 data->tolerance[i] =
1538                     w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1539         }
1540
1541         for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1542                 if (!(data->has_pwm & (1 << i)))
1543                         continue;
1544                 data->pwm[i][PWM_NONSTOP] =
1545                     w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
1546                 data->pwm[i][PWM_START] =
1547                     w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
1548                 data->pwm_stop_time[i] =
1549                     w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
1550         }
1551
1552         data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
1553         data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
1554         data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
1555         data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
1556         data->temp_critical =
1557             w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
1558         data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
1559
1560         for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
1561                 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
1562         }
1563
1564         data->last_nonvolatile = jiffies;
1565 }
1566
1567 static struct w83793_data *w83793_update_device(struct device *dev)
1568 {
1569         struct i2c_client *client = to_i2c_client(dev);
1570         struct w83793_data *data = i2c_get_clientdata(client);
1571         int i;
1572
1573         mutex_lock(&data->update_lock);
1574
1575         if (!(time_after(jiffies, data->last_updated + HZ * 2)
1576               || !data->valid))
1577                 goto END;
1578
1579         /* Update the voltages measured value and limits */
1580         for (i = 0; i < ARRAY_SIZE(data->in); i++)
1581                 data->in[i][IN_READ] =
1582                     w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
1583
1584         data->in_low_bits[IN_READ] =
1585             w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
1586
1587         for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
1588                 if (!(data->has_fan & (1 << i))) {
1589                         continue;
1590                 }
1591                 data->fan[i] =
1592                     w83793_read_value(client, W83793_REG_FAN(i)) << 8;
1593                 data->fan[i] |=
1594                     w83793_read_value(client, W83793_REG_FAN(i) + 1);
1595         }
1596
1597         for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
1598                 if (!(data->has_temp & (1 << i)))
1599                         continue;
1600                 data->temp[i][TEMP_READ] =
1601                     w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
1602         }
1603
1604         data->temp_low_bits =
1605             w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
1606
1607         for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
1608                 if (data->has_pwm & (1 << i))
1609                         data->pwm[i][PWM_DUTY] =
1610                             w83793_read_value(client,
1611                                               W83793_REG_PWM(i, PWM_DUTY));
1612         }
1613
1614         for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
1615                 data->alarms[i] =
1616                     w83793_read_value(client, W83793_REG_ALARM(i));
1617         if (data->has_vid & 0x01)
1618                 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
1619         if (data->has_vid & 0x02)
1620                 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
1621         w83793_update_nonvolatile(dev);
1622         data->last_updated = jiffies;
1623         data->valid = 1;
1624
1625 END:
1626         mutex_unlock(&data->update_lock);
1627         return data;
1628 }
1629
1630 /* Ignore the possibility that somebody change bank outside the driver
1631    Must be called with data->update_lock held, except during initialization */
1632 static u8 w83793_read_value(struct i2c_client *client, u16 reg)
1633 {
1634         struct w83793_data *data = i2c_get_clientdata(client);
1635         u8 res = 0xff;
1636         u8 new_bank = reg >> 8;
1637
1638         new_bank |= data->bank & 0xfc;
1639         if (data->bank != new_bank) {
1640                 if (i2c_smbus_write_byte_data
1641                     (client, W83793_REG_BANKSEL, new_bank) >= 0)
1642                         data->bank = new_bank;
1643                 else {
1644                         dev_err(&client->dev,
1645                                 "set bank to %d failed, fall back "
1646                                 "to bank %d, read reg 0x%x error\n",
1647                                 new_bank, data->bank, reg);
1648                         res = 0x0;      /* read 0x0 from the chip */
1649                         goto END;
1650                 }
1651         }
1652         res = i2c_smbus_read_byte_data(client, reg & 0xff);
1653 END:
1654         return res;
1655 }
1656
1657 /* Must be called with data->update_lock held, except during initialization */
1658 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
1659 {
1660         struct w83793_data *data = i2c_get_clientdata(client);
1661         int res;
1662         u8 new_bank = reg >> 8;
1663
1664         new_bank |= data->bank & 0xfc;
1665         if (data->bank != new_bank) {
1666                 if ((res = i2c_smbus_write_byte_data
1667                     (client, W83793_REG_BANKSEL, new_bank)) >= 0)
1668                         data->bank = new_bank;
1669                 else {
1670                         dev_err(&client->dev,
1671                                 "set bank to %d failed, fall back "
1672                                 "to bank %d, write reg 0x%x error\n",
1673                                 new_bank, data->bank, reg);
1674                         goto END;
1675                 }
1676         }
1677
1678         res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
1679 END:
1680         return res;
1681 }
1682
1683 static int __init sensors_w83793_init(void)
1684 {
1685         return i2c_add_driver(&w83793_driver);
1686 }
1687
1688 static void __exit sensors_w83793_exit(void)
1689 {
1690         i2c_del_driver(&w83793_driver);
1691 }
1692
1693 MODULE_AUTHOR("Yuan Mu");
1694 MODULE_DESCRIPTION("w83793 driver");
1695 MODULE_LICENSE("GPL");
1696
1697 module_init(sensors_w83793_init);
1698 module_exit(sensors_w83793_exit);