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