2 w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 Supports following chips:
26 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
27 as99127f 7 3 0 3 0x31 0x12c3 yes no
28 as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
29 w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
30 w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC)
31 w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
32 w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/jiffies.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-isa.h>
42 #include <linux/hwmon.h>
43 #include <linux/hwmon-vid.h>
44 #include <linux/err.h>
45 #include <linux/mutex.h>
49 /* Addresses to scan */
50 static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
51 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
52 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
53 static unsigned short isa_address = 0x290;
55 /* Insmod parameters */
56 I2C_CLIENT_INSMOD_5(w83781d, w83782d, w83783s, w83627hf, as99127f);
57 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
58 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
61 module_param(reset, bool, 0);
62 MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
65 module_param(init, bool, 0);
66 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
68 /* Constants specified below */
70 /* Length of ISA address segment */
71 #define W83781D_EXTENT 8
73 /* Where are the ISA address/data registers relative to the base address */
74 #define W83781D_ADDR_REG_OFFSET 5
75 #define W83781D_DATA_REG_OFFSET 6
77 /* The W83781D registers */
78 /* The W83782D registers for nr=7,8 are in bank 5 */
79 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
80 (0x554 + (((nr) - 7) * 2)))
81 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
82 (0x555 + (((nr) - 7) * 2)))
83 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
86 #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
87 #define W83781D_REG_FAN(nr) (0x27 + (nr))
89 #define W83781D_REG_BANK 0x4E
90 #define W83781D_REG_TEMP2_CONFIG 0x152
91 #define W83781D_REG_TEMP3_CONFIG 0x252
92 #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
93 ((nr == 2) ? (0x0150) : \
95 #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
96 ((nr == 2) ? (0x153) : \
98 #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
99 ((nr == 2) ? (0x155) : \
102 #define W83781D_REG_CONFIG 0x40
104 /* Interrupt status (W83781D, AS99127F) */
105 #define W83781D_REG_ALARM1 0x41
106 #define W83781D_REG_ALARM2 0x42
108 /* Real-time status (W83782D, W83783S, W83627HF) */
109 #define W83782D_REG_ALARM1 0x459
110 #define W83782D_REG_ALARM2 0x45A
111 #define W83782D_REG_ALARM3 0x45B
113 #define W83781D_REG_BEEP_CONFIG 0x4D
114 #define W83781D_REG_BEEP_INTS1 0x56
115 #define W83781D_REG_BEEP_INTS2 0x57
116 #define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
118 #define W83781D_REG_VID_FANDIV 0x47
120 #define W83781D_REG_CHIPID 0x49
121 #define W83781D_REG_WCHIPID 0x58
122 #define W83781D_REG_CHIPMAN 0x4F
123 #define W83781D_REG_PIN 0x4B
126 #define W83781D_REG_VBAT 0x5D
128 /* PWM 782D (1-4) and 783S (1-2) only */
129 #define W83781D_REG_PWM1 0x5B /* 782d and 783s/627hf datasheets disagree */
130 /* on which is which; */
131 #define W83781D_REG_PWM2 0x5A /* We follow the 782d convention here, */
132 /* However 782d is probably wrong. */
133 #define W83781D_REG_PWM3 0x5E
134 #define W83781D_REG_PWM4 0x5F
135 #define W83781D_REG_PWMCLK12 0x5C
136 #define W83781D_REG_PWMCLK34 0x45C
137 static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
138 W83781D_REG_PWM3, W83781D_REG_PWM4
141 #define W83781D_REG_PWM(nr) (regpwm[(nr) - 1])
143 #define W83781D_REG_I2C_ADDR 0x48
144 #define W83781D_REG_I2C_SUBADDR 0x4A
146 /* The following are undocumented in the data sheets however we
147 received the information in an email from Winbond tech support */
148 /* Sensor selection - not on 781d */
149 #define W83781D_REG_SCFG1 0x5D
150 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
152 #define W83781D_REG_SCFG2 0x59
153 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
155 #define W83781D_DEFAULT_BETA 3435
157 /* RT Table registers */
158 #define W83781D_REG_RT_IDX 0x50
159 #define W83781D_REG_RT_VAL 0x51
161 /* Conversions. Rounding and limit checking is only done on the TO_REG
162 variants. Note that you should be a bit careful with which arguments
163 these macros are called: arguments may be evaluated more than once.
164 Fixing this is just not worth it. */
165 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
166 #define IN_FROM_REG(val) (((val) * 16) / 10)
169 FAN_TO_REG(long rpm, int div)
173 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
174 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
177 #define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
178 ((val) == 255 ? 0 : \
179 1350000 / ((val) * (div))))
181 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
182 : (val)) / 1000, 0, 0xff))
183 #define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
185 #define PWM_FROM_REG(val) (val)
186 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
187 #define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
188 (val) ^ 0x7fff : (val))
189 #define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
190 (~(val)) & 0x7fff : (val) & 0xffffff)
192 #define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
193 #define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
195 #define DIV_FROM_REG(val) (1 << (val))
198 DIV_TO_REG(long val, enum chips type)
201 val = SENSORS_LIMIT(val, 1,
203 || type == as99127f) ? 8 : 128)) >> 1;
204 for (i = 0; i < 7; i++) {
212 /* There are some complications in a module like this. First off, W83781D chips
213 may be both present on the SMBus and the ISA bus, and we have to handle
214 those cases separately at some places. Second, there might be several
215 W83781D chips available (well, actually, that is probably never done; but
216 it is a clean illustration of how to handle a case like that). Finally,
217 a specific chip may be attached to *both* ISA and SMBus, and we would
218 not like to detect it double. Fortunately, in the case of the W83781D at
219 least, a register tells us what SMBus address we are on, so that helps
220 a bit - except if there could be more than one SMBus. Groan. No solution
223 /* This module may seem overly long and complicated. In fact, it is not so
224 bad. Quite a lot of bookkeeping is done. A real driver can often cut
227 /* For each registered W83781D, we need to keep some data in memory. That
228 data is pointed to by w83781d_list[NR]->data. The structure itself is
229 dynamically allocated, at the same time when a new w83781d client is
231 struct w83781d_data {
232 struct i2c_client client;
233 struct class_device *class_dev;
237 struct mutex update_lock;
238 char valid; /* !=0 if following fields are valid */
239 unsigned long last_updated; /* In jiffies */
241 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
242 /* array of 2 pointers to subclients */
244 u8 in[9]; /* Register value - 8 & 9 for 782D only */
245 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
246 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
247 u8 fan[3]; /* Register value */
248 u8 fan_min[3]; /* Register value */
250 u8 temp_max; /* Register value */
251 u8 temp_max_hyst; /* Register value */
252 u16 temp_add[2]; /* Register value */
253 u16 temp_max_add[2]; /* Register value */
254 u16 temp_max_hyst_add[2]; /* Register value */
255 u8 fan_div[3]; /* Register encoding, shifted right */
256 u8 vid; /* Register encoding, combined */
257 u32 alarms; /* Register encoding, combined */
258 u32 beep_mask; /* Register encoding, combined */
259 u8 beep_enable; /* Boolean */
260 u8 pwm[4]; /* Register value */
261 u8 pwmenable[4]; /* Boolean */
262 u16 sens[3]; /* 782D/783S only.
263 1 = pentium diode; 2 = 3904 diode;
264 3000-5000 = thermistor beta.
266 Other Betas unimplemented */
270 static int w83781d_attach_adapter(struct i2c_adapter *adapter);
271 static int w83781d_isa_attach_adapter(struct i2c_adapter *adapter);
272 static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
273 static int w83781d_detach_client(struct i2c_client *client);
275 static int w83781d_read_value(struct i2c_client *client, u16 reg);
276 static int w83781d_write_value(struct i2c_client *client, u16 reg, u16 value);
277 static struct w83781d_data *w83781d_update_device(struct device *dev);
278 static void w83781d_init_client(struct i2c_client *client);
280 static struct i2c_driver w83781d_driver = {
284 .id = I2C_DRIVERID_W83781D,
285 .attach_adapter = w83781d_attach_adapter,
286 .detach_client = w83781d_detach_client,
289 static struct i2c_driver w83781d_isa_driver = {
291 .owner = THIS_MODULE,
292 .name = "w83781d-isa",
294 .attach_adapter = w83781d_isa_attach_adapter,
295 .detach_client = w83781d_detach_client,
299 /* following are the sysfs callback functions */
300 #define show_in_reg(reg) \
301 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
303 struct w83781d_data *data = w83781d_update_device(dev); \
304 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
310 #define store_in_reg(REG, reg) \
311 static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
313 struct i2c_client *client = to_i2c_client(dev); \
314 struct w83781d_data *data = i2c_get_clientdata(client); \
317 val = simple_strtoul(buf, NULL, 10) / 10; \
319 mutex_lock(&data->update_lock); \
320 data->in_##reg[nr] = IN_TO_REG(val); \
321 w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
323 mutex_unlock(&data->update_lock); \
326 store_in_reg(MIN, min);
327 store_in_reg(MAX, max);
329 #define sysfs_in_offset(offset) \
331 show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
333 return show_in(dev, buf, offset); \
335 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
337 #define sysfs_in_reg_offset(reg, offset) \
338 static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
340 return show_in_##reg (dev, buf, offset); \
342 static ssize_t store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
344 return store_in_##reg (dev, buf, count, offset); \
346 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset);
348 #define sysfs_in_offsets(offset) \
349 sysfs_in_offset(offset); \
350 sysfs_in_reg_offset(min, offset); \
351 sysfs_in_reg_offset(max, offset);
363 #define device_create_file_in(client, offset) \
365 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
366 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
367 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
370 #define show_fan_reg(reg) \
371 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
373 struct w83781d_data *data = w83781d_update_device(dev); \
374 return sprintf(buf,"%ld\n", \
375 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
378 show_fan_reg(fan_min);
381 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
383 struct i2c_client *client = to_i2c_client(dev);
384 struct w83781d_data *data = i2c_get_clientdata(client);
387 val = simple_strtoul(buf, NULL, 10);
389 mutex_lock(&data->update_lock);
390 data->fan_min[nr - 1] =
391 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
392 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
393 data->fan_min[nr - 1]);
395 mutex_unlock(&data->update_lock);
399 #define sysfs_fan_offset(offset) \
400 static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
402 return show_fan(dev, buf, offset); \
404 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
406 #define sysfs_fan_min_offset(offset) \
407 static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
409 return show_fan_min(dev, buf, offset); \
411 static ssize_t store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
413 return store_fan_min(dev, buf, count, offset); \
415 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset);
418 sysfs_fan_min_offset(1);
420 sysfs_fan_min_offset(2);
422 sysfs_fan_min_offset(3);
424 #define device_create_file_fan(client, offset) \
426 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
427 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
430 #define show_temp_reg(reg) \
431 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
433 struct w83781d_data *data = w83781d_update_device(dev); \
434 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
435 return sprintf(buf,"%d\n", \
436 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
437 } else { /* TEMP1 */ \
438 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
442 show_temp_reg(temp_max);
443 show_temp_reg(temp_max_hyst);
445 #define store_temp_reg(REG, reg) \
446 static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
448 struct i2c_client *client = to_i2c_client(dev); \
449 struct w83781d_data *data = i2c_get_clientdata(client); \
452 val = simple_strtol(buf, NULL, 10); \
454 mutex_lock(&data->update_lock); \
456 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
457 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
458 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
459 data->temp_##reg##_add[nr-2]); \
460 } else { /* TEMP1 */ \
461 data->temp_##reg = TEMP_TO_REG(val); \
462 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
466 mutex_unlock(&data->update_lock); \
469 store_temp_reg(OVER, max);
470 store_temp_reg(HYST, max_hyst);
472 #define sysfs_temp_offset(offset) \
474 show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
476 return show_temp(dev, buf, offset); \
478 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
480 #define sysfs_temp_reg_offset(reg, offset) \
481 static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
483 return show_temp_##reg (dev, buf, offset); \
485 static ssize_t store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
487 return store_temp_##reg (dev, buf, count, offset); \
489 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
491 #define sysfs_temp_offsets(offset) \
492 sysfs_temp_offset(offset); \
493 sysfs_temp_reg_offset(max, offset); \
494 sysfs_temp_reg_offset(max_hyst, offset);
496 sysfs_temp_offsets(1);
497 sysfs_temp_offsets(2);
498 sysfs_temp_offsets(3);
500 #define device_create_file_temp(client, offset) \
502 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
503 device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
504 device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
508 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
510 struct w83781d_data *data = w83781d_update_device(dev);
511 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
515 DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
516 #define device_create_file_vid(client) \
517 device_create_file(&client->dev, &dev_attr_cpu0_vid);
519 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
521 struct w83781d_data *data = w83781d_update_device(dev);
522 return sprintf(buf, "%ld\n", (long) data->vrm);
526 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
528 struct i2c_client *client = to_i2c_client(dev);
529 struct w83781d_data *data = i2c_get_clientdata(client);
532 val = simple_strtoul(buf, NULL, 10);
539 DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
540 #define device_create_file_vrm(client) \
541 device_create_file(&client->dev, &dev_attr_vrm);
543 show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
545 struct w83781d_data *data = w83781d_update_device(dev);
546 return sprintf(buf, "%u\n", data->alarms);
550 DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
551 #define device_create_file_alarms(client) \
552 device_create_file(&client->dev, &dev_attr_alarms);
553 static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
555 struct w83781d_data *data = w83781d_update_device(dev);
556 return sprintf(buf, "%ld\n",
557 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
559 static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
561 struct w83781d_data *data = w83781d_update_device(dev);
562 return sprintf(buf, "%ld\n",
563 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
566 #define BEEP_ENABLE 0 /* Store beep_enable */
567 #define BEEP_MASK 1 /* Store beep_mask */
570 store_beep_reg(struct device *dev, const char *buf, size_t count,
573 struct i2c_client *client = to_i2c_client(dev);
574 struct w83781d_data *data = i2c_get_clientdata(client);
577 val = simple_strtoul(buf, NULL, 10);
579 mutex_lock(&data->update_lock);
581 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
582 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
583 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
584 data->beep_mask & 0xff);
586 if ((data->type != w83781d) && (data->type != as99127f)) {
587 w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
588 ((data->beep_mask) >> 16) & 0xff);
591 val2 = (data->beep_mask >> 8) & 0x7f;
592 } else { /* We are storing beep_enable */
593 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
594 data->beep_enable = BEEP_ENABLE_TO_REG(val);
597 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
598 val2 | data->beep_enable << 7);
600 mutex_unlock(&data->update_lock);
604 #define sysfs_beep(REG, reg) \
605 static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
607 return show_beep_##reg(dev, attr, buf); \
609 static ssize_t store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
611 return store_beep_reg(dev, buf, count, BEEP_##REG); \
613 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg);
615 sysfs_beep(ENABLE, enable);
616 sysfs_beep(MASK, mask);
618 #define device_create_file_beep(client) \
620 device_create_file(&client->dev, &dev_attr_beep_enable); \
621 device_create_file(&client->dev, &dev_attr_beep_mask); \
625 show_fan_div_reg(struct device *dev, char *buf, int nr)
627 struct w83781d_data *data = w83781d_update_device(dev);
628 return sprintf(buf, "%ld\n",
629 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
632 /* Note: we save and restore the fan minimum here, because its value is
633 determined in part by the fan divisor. This follows the principle of
634 least surprise; the user doesn't expect the fan minimum to change just
635 because the divisor changed. */
637 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
639 struct i2c_client *client = to_i2c_client(dev);
640 struct w83781d_data *data = i2c_get_clientdata(client);
643 unsigned long val = simple_strtoul(buf, NULL, 10);
645 mutex_lock(&data->update_lock);
648 min = FAN_FROM_REG(data->fan_min[nr],
649 DIV_FROM_REG(data->fan_div[nr]));
651 data->fan_div[nr] = DIV_TO_REG(val, data->type);
653 reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
654 & (nr==0 ? 0xcf : 0x3f))
655 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
656 w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
658 /* w83781d and as99127f don't have extended divisor bits */
659 if (data->type != w83781d && data->type != as99127f) {
660 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
662 | ((data->fan_div[nr] & 0x04) << (3 + nr));
663 w83781d_write_value(client, W83781D_REG_VBAT, reg);
666 /* Restore fan_min */
667 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
668 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
670 mutex_unlock(&data->update_lock);
674 #define sysfs_fan_div(offset) \
675 static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
677 return show_fan_div_reg(dev, buf, offset); \
679 static ssize_t store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
681 return store_fan_div_reg(dev, buf, count, offset - 1); \
683 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset);
689 #define device_create_file_fan_div(client, offset) \
691 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
695 show_pwm_reg(struct device *dev, char *buf, int nr)
697 struct w83781d_data *data = w83781d_update_device(dev);
698 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
702 show_pwmenable_reg(struct device *dev, char *buf, int nr)
704 struct w83781d_data *data = w83781d_update_device(dev);
705 return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
709 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
711 struct i2c_client *client = to_i2c_client(dev);
712 struct w83781d_data *data = i2c_get_clientdata(client);
715 val = simple_strtoul(buf, NULL, 10);
717 mutex_lock(&data->update_lock);
718 data->pwm[nr - 1] = PWM_TO_REG(val);
719 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
720 mutex_unlock(&data->update_lock);
725 store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
727 struct i2c_client *client = to_i2c_client(dev);
728 struct w83781d_data *data = i2c_get_clientdata(client);
731 val = simple_strtoul(buf, NULL, 10);
733 mutex_lock(&data->update_lock);
738 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
739 w83781d_write_value(client, W83781D_REG_PWMCLK12,
740 (reg & 0xf7) | (val << 3));
742 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
743 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
744 (reg & 0xef) | (!val << 4));
746 data->pwmenable[nr - 1] = val;
750 mutex_unlock(&data->update_lock);
754 mutex_unlock(&data->update_lock);
758 #define sysfs_pwm(offset) \
759 static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
761 return show_pwm_reg(dev, buf, offset); \
763 static ssize_t store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, \
764 const char *buf, size_t count) \
766 return store_pwm_reg(dev, buf, count, offset); \
768 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
769 show_regs_pwm_##offset, store_regs_pwm_##offset);
771 #define sysfs_pwmenable(offset) \
772 static ssize_t show_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
774 return show_pwmenable_reg(dev, buf, offset); \
776 static ssize_t store_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, \
777 const char *buf, size_t count) \
779 return store_pwmenable_reg(dev, buf, count, offset); \
781 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
782 show_regs_pwmenable_##offset, store_regs_pwmenable_##offset);
786 sysfs_pwmenable(2); /* only PWM2 can be enabled/disabled */
790 #define device_create_file_pwm(client, offset) \
792 device_create_file(&client->dev, &dev_attr_pwm##offset); \
795 #define device_create_file_pwmenable(client, offset) \
797 device_create_file(&client->dev, &dev_attr_pwm##offset##_enable); \
801 show_sensor_reg(struct device *dev, char *buf, int nr)
803 struct w83781d_data *data = w83781d_update_device(dev);
804 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
808 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
810 struct i2c_client *client = to_i2c_client(dev);
811 struct w83781d_data *data = i2c_get_clientdata(client);
814 val = simple_strtoul(buf, NULL, 10);
816 mutex_lock(&data->update_lock);
819 case 1: /* PII/Celeron diode */
820 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
821 w83781d_write_value(client, W83781D_REG_SCFG1,
822 tmp | BIT_SCFG1[nr - 1]);
823 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
824 w83781d_write_value(client, W83781D_REG_SCFG2,
825 tmp | BIT_SCFG2[nr - 1]);
826 data->sens[nr - 1] = val;
829 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
830 w83781d_write_value(client, W83781D_REG_SCFG1,
831 tmp | BIT_SCFG1[nr - 1]);
832 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
833 w83781d_write_value(client, W83781D_REG_SCFG2,
834 tmp & ~BIT_SCFG2[nr - 1]);
835 data->sens[nr - 1] = val;
837 case W83781D_DEFAULT_BETA: /* thermistor */
838 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
839 w83781d_write_value(client, W83781D_REG_SCFG1,
840 tmp & ~BIT_SCFG1[nr - 1]);
841 data->sens[nr - 1] = val;
844 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
845 (long) val, W83781D_DEFAULT_BETA);
849 mutex_unlock(&data->update_lock);
853 #define sysfs_sensor(offset) \
854 static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
856 return show_sensor_reg(dev, buf, offset); \
858 static ssize_t store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
860 return store_sensor_reg(dev, buf, count, offset); \
862 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset);
868 #define device_create_file_sensor(client, offset) \
870 device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
873 /* This function is called when:
874 * w83781d_driver is inserted (when this module is loaded), for each
876 * when a new adapter is inserted (and w83781d_driver is still present) */
878 w83781d_attach_adapter(struct i2c_adapter *adapter)
880 if (!(adapter->class & I2C_CLASS_HWMON))
882 return i2c_probe(adapter, &addr_data, w83781d_detect);
886 w83781d_isa_attach_adapter(struct i2c_adapter *adapter)
888 return w83781d_detect(adapter, isa_address, -1);
891 /* Assumes that adapter is of I2C, not ISA variety.
892 * OTHERWISE DON'T CALL THIS
895 w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
896 struct i2c_client *new_client)
900 const char *client_name = "";
901 struct w83781d_data *data = i2c_get_clientdata(new_client);
903 data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
904 if (!(data->lm75[0])) {
909 id = i2c_adapter_id(adapter);
911 if (force_subclients[0] == id && force_subclients[1] == address) {
912 for (i = 2; i <= 3; i++) {
913 if (force_subclients[i] < 0x48 ||
914 force_subclients[i] > 0x4f) {
915 dev_err(&new_client->dev, "Invalid subclient "
916 "address %d; must be 0x48-0x4f\n",
917 force_subclients[i]);
922 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
923 (force_subclients[2] & 0x07) |
924 ((force_subclients[3] & 0x07) << 4));
925 data->lm75[0]->addr = force_subclients[2];
927 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
928 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
931 if (kind != w83783s) {
932 data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
933 if (!(data->lm75[1])) {
938 if (force_subclients[0] == id &&
939 force_subclients[1] == address) {
940 data->lm75[1]->addr = force_subclients[3];
942 data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
944 if (data->lm75[0]->addr == data->lm75[1]->addr) {
945 dev_err(&new_client->dev,
946 "Duplicate addresses 0x%x for subclients.\n",
947 data->lm75[0]->addr);
954 client_name = "w83781d subclient";
955 else if (kind == w83782d)
956 client_name = "w83782d subclient";
957 else if (kind == w83783s)
958 client_name = "w83783s subclient";
959 else if (kind == w83627hf)
960 client_name = "w83627hf subclient";
961 else if (kind == as99127f)
962 client_name = "as99127f subclient";
964 for (i = 0; i <= 1; i++) {
965 /* store all data in w83781d */
966 i2c_set_clientdata(data->lm75[i], NULL);
967 data->lm75[i]->adapter = adapter;
968 data->lm75[i]->driver = &w83781d_driver;
969 data->lm75[i]->flags = 0;
970 strlcpy(data->lm75[i]->name, client_name,
972 if ((err = i2c_attach_client(data->lm75[i]))) {
973 dev_err(&new_client->dev, "Subclient %d "
974 "registration at address 0x%x "
975 "failed.\n", i, data->lm75[i]->addr);
986 /* Undo inits in case of errors */
988 i2c_detach_client(data->lm75[0]);
990 kfree(data->lm75[1]);
992 kfree(data->lm75[0]);
998 w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
1000 int i = 0, val1 = 0, val2;
1001 struct i2c_client *new_client;
1002 struct w83781d_data *data;
1004 const char *client_name = "";
1005 int is_isa = i2c_is_isa_adapter(adapter);
1006 enum vendor { winbond, asus } vendid;
1009 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1014 /* Prevent users from forcing a kind for a bus it isn't supposed
1015 to possibly be on */
1016 if (is_isa && (kind == as99127f || kind == w83783s)) {
1017 dev_err(&adapter->dev,
1018 "Cannot force I2C-only chip for ISA address 0x%02x.\n",
1025 if (!request_region(address, W83781D_EXTENT,
1026 w83781d_isa_driver.driver.name)) {
1027 dev_dbg(&adapter->dev, "Request of region "
1028 "0x%x-0x%x for w83781d failed\n", address,
1029 address + W83781D_EXTENT - 1);
1034 /* Probe whether there is anything available on this address. Already
1035 done for SMBus clients */
1039 #define REALLY_SLOW_IO
1040 /* We need the timeouts for at least some LM78-like
1041 chips. But only if we read 'undefined' registers. */
1042 i = inb_p(address + 1);
1043 if (inb_p(address + 2) != i
1044 || inb_p(address + 3) != i
1045 || inb_p(address + 7) != i) {
1046 dev_dbg(&adapter->dev, "Detection of w83781d "
1047 "chip failed at step 1\n");
1051 #undef REALLY_SLOW_IO
1053 /* Let's just hope nothing breaks here */
1054 i = inb_p(address + 5) & 0x7f;
1055 outb_p(~i & 0x7f, address + 5);
1056 val2 = inb_p(address + 5) & 0x7f;
1057 if (val2 != (~i & 0x7f)) {
1058 outb_p(i, address + 5);
1059 dev_dbg(&adapter->dev, "Detection of w83781d "
1060 "chip failed at step 2 (0x%x != "
1061 "0x%x at 0x%x)\n", val2, ~i & 0x7f,
1069 /* OK. For now, we presume we have a valid client. We now create the
1070 client structure, even though we cannot fill it completely yet.
1071 But it allows us to access w83781d_{read,write}_value. */
1073 if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1078 new_client = &data->client;
1079 i2c_set_clientdata(new_client, data);
1080 new_client->addr = address;
1081 mutex_init(&data->lock);
1082 new_client->adapter = adapter;
1083 new_client->driver = is_isa ? &w83781d_isa_driver : &w83781d_driver;
1084 new_client->flags = 0;
1086 /* Now, we do the remaining detection. */
1088 /* The w8378?d may be stuck in some other bank than bank 0. This may
1089 make reading other information impossible. Specify a force=... or
1090 force_*=... parameter, and the Winbond will be reset to the right
1093 if (w83781d_read_value(new_client, W83781D_REG_CONFIG) & 0x80) {
1094 dev_dbg(&new_client->dev, "Detection failed at step "
1099 val1 = w83781d_read_value(new_client, W83781D_REG_BANK);
1100 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1101 /* Check for Winbond or Asus ID if in bank 0 */
1102 if ((!(val1 & 0x07)) &&
1103 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1104 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1105 dev_dbg(&new_client->dev, "Detection failed at step "
1110 /* If Winbond SMBus, check address at 0x48.
1111 Asus doesn't support, except for as99127f rev.2 */
1112 if ((!is_isa) && (((!(val1 & 0x80)) && (val2 == 0xa3)) ||
1113 ((val1 & 0x80) && (val2 == 0x5c)))) {
1114 if (w83781d_read_value
1115 (new_client, W83781D_REG_I2C_ADDR) != address) {
1116 dev_dbg(&new_client->dev, "Detection failed "
1124 /* We have either had a force parameter, or we have already detected the
1125 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1126 w83781d_write_value(new_client, W83781D_REG_BANK,
1127 (w83781d_read_value(new_client,
1128 W83781D_REG_BANK) & 0x78) |
1131 /* Determine the chip type. */
1134 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1137 else if (val2 == 0x12)
1140 dev_dbg(&new_client->dev, "Chip was made by neither "
1141 "Winbond nor Asus?\n");
1146 val1 = w83781d_read_value(new_client, W83781D_REG_WCHIPID);
1147 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1149 else if (val1 == 0x30 && vendid == winbond)
1151 else if (val1 == 0x40 && vendid == winbond && !is_isa
1154 else if (val1 == 0x21 && vendid == winbond)
1156 else if (val1 == 0x31 && !is_isa && address >= 0x28)
1160 dev_warn(&new_client->dev, "Ignoring 'force' "
1161 "parameter for unknown chip at "
1162 "adapter %d, address 0x%02x\n",
1163 i2c_adapter_id(adapter), address);
1169 if (kind == w83781d) {
1170 client_name = "w83781d";
1171 } else if (kind == w83782d) {
1172 client_name = "w83782d";
1173 } else if (kind == w83783s) {
1174 client_name = "w83783s";
1175 } else if (kind == w83627hf) {
1176 client_name = "w83627hf";
1177 } else if (kind == as99127f) {
1178 client_name = "as99127f";
1181 /* Fill in the remaining client fields and put into the global list */
1182 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1186 mutex_init(&data->update_lock);
1188 /* Tell the I2C layer a new client has arrived */
1189 if ((err = i2c_attach_client(new_client)))
1192 /* attach secondary i2c lm75-like clients */
1194 if ((err = w83781d_detect_subclients(adapter, address,
1198 data->lm75[0] = NULL;
1199 data->lm75[1] = NULL;
1202 /* Initialize the chip */
1203 w83781d_init_client(new_client);
1205 /* A few vars need to be filled upon startup */
1206 for (i = 1; i <= 3; i++) {
1207 data->fan_min[i - 1] = w83781d_read_value(new_client,
1208 W83781D_REG_FAN_MIN(i));
1210 if (kind != w83781d && kind != as99127f)
1211 for (i = 0; i < 4; i++)
1212 data->pwmenable[i] = 1;
1214 /* Register sysfs hooks */
1215 data->class_dev = hwmon_device_register(&new_client->dev);
1216 if (IS_ERR(data->class_dev)) {
1217 err = PTR_ERR(data->class_dev);
1221 device_create_file_in(new_client, 0);
1222 if (kind != w83783s)
1223 device_create_file_in(new_client, 1);
1224 device_create_file_in(new_client, 2);
1225 device_create_file_in(new_client, 3);
1226 device_create_file_in(new_client, 4);
1227 device_create_file_in(new_client, 5);
1228 device_create_file_in(new_client, 6);
1229 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1230 device_create_file_in(new_client, 7);
1231 device_create_file_in(new_client, 8);
1234 device_create_file_fan(new_client, 1);
1235 device_create_file_fan(new_client, 2);
1236 device_create_file_fan(new_client, 3);
1238 device_create_file_temp(new_client, 1);
1239 device_create_file_temp(new_client, 2);
1240 if (kind != w83783s)
1241 device_create_file_temp(new_client, 3);
1243 device_create_file_vid(new_client);
1244 device_create_file_vrm(new_client);
1246 device_create_file_fan_div(new_client, 1);
1247 device_create_file_fan_div(new_client, 2);
1248 device_create_file_fan_div(new_client, 3);
1250 device_create_file_alarms(new_client);
1252 device_create_file_beep(new_client);
1254 if (kind != w83781d && kind != as99127f) {
1255 device_create_file_pwm(new_client, 1);
1256 device_create_file_pwm(new_client, 2);
1257 device_create_file_pwmenable(new_client, 2);
1259 if (kind == w83782d && !is_isa) {
1260 device_create_file_pwm(new_client, 3);
1261 device_create_file_pwm(new_client, 4);
1264 if (kind != as99127f && kind != w83781d) {
1265 device_create_file_sensor(new_client, 1);
1266 device_create_file_sensor(new_client, 2);
1267 if (kind != w83783s)
1268 device_create_file_sensor(new_client, 3);
1274 if (data->lm75[1]) {
1275 i2c_detach_client(data->lm75[1]);
1276 kfree(data->lm75[1]);
1278 if (data->lm75[0]) {
1279 i2c_detach_client(data->lm75[0]);
1280 kfree(data->lm75[0]);
1283 i2c_detach_client(new_client);
1288 release_region(address, W83781D_EXTENT);
1294 w83781d_detach_client(struct i2c_client *client)
1296 struct w83781d_data *data = i2c_get_clientdata(client);
1301 hwmon_device_unregister(data->class_dev);
1303 if (i2c_is_isa_client(client))
1304 release_region(client->addr, W83781D_EXTENT);
1306 if ((err = i2c_detach_client(client)))
1320 /* The SMBus locks itself, usually, but nothing may access the Winbond between
1321 bank switches. ISA access must always be locked explicitly!
1322 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1323 would slow down the W83781D access and should not be necessary.
1324 There are some ugly typecasts here, but the good news is - they should
1325 nowhere else be necessary! */
1327 w83781d_read_value(struct i2c_client *client, u16 reg)
1329 struct w83781d_data *data = i2c_get_clientdata(client);
1330 int res, word_sized, bank;
1331 struct i2c_client *cl;
1333 mutex_lock(&data->lock);
1334 if (i2c_is_isa_client(client)) {
1335 word_sized = (((reg & 0xff00) == 0x100)
1336 || ((reg & 0xff00) == 0x200))
1337 && (((reg & 0x00ff) == 0x50)
1338 || ((reg & 0x00ff) == 0x53)
1339 || ((reg & 0x00ff) == 0x55));
1341 outb_p(W83781D_REG_BANK,
1342 client->addr + W83781D_ADDR_REG_OFFSET);
1344 client->addr + W83781D_DATA_REG_OFFSET);
1346 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1347 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1349 outb_p((reg & 0xff) + 1,
1350 client->addr + W83781D_ADDR_REG_OFFSET);
1352 (res << 8) + inb_p(client->addr +
1353 W83781D_DATA_REG_OFFSET);
1356 outb_p(W83781D_REG_BANK,
1357 client->addr + W83781D_ADDR_REG_OFFSET);
1358 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1361 bank = (reg >> 8) & 0x0f;
1364 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1366 if (bank == 0 || bank > 2) {
1367 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1369 /* switch to subclient */
1370 cl = data->lm75[bank - 1];
1371 /* convert from ISA to LM75 I2C addresses */
1372 switch (reg & 0xff) {
1373 case 0x50: /* TEMP */
1374 res = swab16(i2c_smbus_read_word_data(cl, 0));
1376 case 0x52: /* CONFIG */
1377 res = i2c_smbus_read_byte_data(cl, 1);
1379 case 0x53: /* HYST */
1380 res = swab16(i2c_smbus_read_word_data(cl, 2));
1382 case 0x55: /* OVER */
1384 res = swab16(i2c_smbus_read_word_data(cl, 3));
1389 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1391 mutex_unlock(&data->lock);
1396 w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1398 struct w83781d_data *data = i2c_get_clientdata(client);
1399 int word_sized, bank;
1400 struct i2c_client *cl;
1402 mutex_lock(&data->lock);
1403 if (i2c_is_isa_client(client)) {
1404 word_sized = (((reg & 0xff00) == 0x100)
1405 || ((reg & 0xff00) == 0x200))
1406 && (((reg & 0x00ff) == 0x53)
1407 || ((reg & 0x00ff) == 0x55));
1409 outb_p(W83781D_REG_BANK,
1410 client->addr + W83781D_ADDR_REG_OFFSET);
1412 client->addr + W83781D_DATA_REG_OFFSET);
1414 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1417 client->addr + W83781D_DATA_REG_OFFSET);
1418 outb_p((reg & 0xff) + 1,
1419 client->addr + W83781D_ADDR_REG_OFFSET);
1421 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1423 outb_p(W83781D_REG_BANK,
1424 client->addr + W83781D_ADDR_REG_OFFSET);
1425 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1428 bank = (reg >> 8) & 0x0f;
1431 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1433 if (bank == 0 || bank > 2) {
1434 i2c_smbus_write_byte_data(client, reg & 0xff,
1437 /* switch to subclient */
1438 cl = data->lm75[bank - 1];
1439 /* convert from ISA to LM75 I2C addresses */
1440 switch (reg & 0xff) {
1441 case 0x52: /* CONFIG */
1442 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1444 case 0x53: /* HYST */
1445 i2c_smbus_write_word_data(cl, 2, swab16(value));
1447 case 0x55: /* OVER */
1448 i2c_smbus_write_word_data(cl, 3, swab16(value));
1453 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1455 mutex_unlock(&data->lock);
1460 w83781d_init_client(struct i2c_client *client)
1462 struct w83781d_data *data = i2c_get_clientdata(client);
1464 int type = data->type;
1467 if (reset && type != as99127f) { /* this resets registers we don't have
1468 documentation for on the as99127f */
1469 /* Resetting the chip has been the default for a long time,
1470 but it causes the BIOS initializations (fan clock dividers,
1471 thermal sensor types...) to be lost, so it is now optional.
1472 It might even go away if nobody reports it as being useful,
1473 as I see very little reason why this would be needed at
1475 dev_info(&client->dev, "If reset=1 solved a problem you were "
1476 "having, please report!\n");
1478 /* save these registers */
1479 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1480 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1481 /* Reset all except Watchdog values and last conversion values
1482 This sets fan-divs to 2, among others */
1483 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1484 /* Restore the registers and disable power-on abnormal beep.
1485 This saves FAN 1/2/3 input/output values set by BIOS. */
1486 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1487 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1488 /* Disable master beep-enable (reset turns it on).
1489 Individual beep_mask should be reset to off but for some reason
1490 disabling this bit helps some people not get beeped */
1491 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1494 /* Disable power-on abnormal beep, as advised by the datasheet.
1495 Already done if reset=1. */
1496 if (init && !reset && type != as99127f) {
1497 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1498 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1501 data->vrm = vid_which_vrm();
1503 if ((type != w83781d) && (type != as99127f)) {
1504 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1505 for (i = 1; i <= 3; i++) {
1506 if (!(tmp & BIT_SCFG1[i - 1])) {
1507 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1509 if (w83781d_read_value
1511 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1512 data->sens[i - 1] = 1;
1514 data->sens[i - 1] = 2;
1516 if (type == w83783s && i == 2)
1521 if (init && type != as99127f) {
1523 tmp = w83781d_read_value(client, W83781D_REG_TEMP2_CONFIG);
1525 dev_warn(&client->dev, "Enabling temp2, readings "
1526 "might not make sense\n");
1527 w83781d_write_value(client, W83781D_REG_TEMP2_CONFIG,
1532 if (type != w83783s) {
1533 tmp = w83781d_read_value(client,
1534 W83781D_REG_TEMP3_CONFIG);
1536 dev_warn(&client->dev, "Enabling temp3, "
1537 "readings might not make sense\n");
1538 w83781d_write_value(client,
1539 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1544 /* Start monitoring */
1545 w83781d_write_value(client, W83781D_REG_CONFIG,
1546 (w83781d_read_value(client,
1547 W83781D_REG_CONFIG) & 0xf7)
1551 static struct w83781d_data *w83781d_update_device(struct device *dev)
1553 struct i2c_client *client = to_i2c_client(dev);
1554 struct w83781d_data *data = i2c_get_clientdata(client);
1557 mutex_lock(&data->update_lock);
1559 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1561 dev_dbg(dev, "Starting device update\n");
1563 for (i = 0; i <= 8; i++) {
1564 if (data->type == w83783s && i == 1)
1565 continue; /* 783S has no in1 */
1567 w83781d_read_value(client, W83781D_REG_IN(i));
1569 w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1571 w83781d_read_value(client, W83781D_REG_IN_MAX(i));
1572 if ((data->type != w83782d)
1573 && (data->type != w83627hf) && (i == 6))
1576 for (i = 1; i <= 3; i++) {
1578 w83781d_read_value(client, W83781D_REG_FAN(i));
1579 data->fan_min[i - 1] =
1580 w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1582 if (data->type != w83781d && data->type != as99127f) {
1583 for (i = 1; i <= 4; i++) {
1585 w83781d_read_value(client,
1586 W83781D_REG_PWM(i));
1587 if ((data->type != w83782d
1588 || i2c_is_isa_client(client))
1592 /* Only PWM2 can be disabled */
1593 data->pwmenable[1] = (w83781d_read_value(client,
1594 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1597 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1599 w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1600 data->temp_max_hyst =
1601 w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1603 w83781d_read_value(client, W83781D_REG_TEMP(2));
1604 data->temp_max_add[0] =
1605 w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1606 data->temp_max_hyst_add[0] =
1607 w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
1608 if (data->type != w83783s) {
1610 w83781d_read_value(client, W83781D_REG_TEMP(3));
1611 data->temp_max_add[1] =
1612 w83781d_read_value(client,
1613 W83781D_REG_TEMP_OVER(3));
1614 data->temp_max_hyst_add[1] =
1615 w83781d_read_value(client,
1616 W83781D_REG_TEMP_HYST(3));
1618 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
1619 data->vid = i & 0x0f;
1620 data->vid |= (w83781d_read_value(client,
1621 W83781D_REG_CHIPID) & 0x01) << 4;
1622 data->fan_div[0] = (i >> 4) & 0x03;
1623 data->fan_div[1] = (i >> 6) & 0x03;
1624 data->fan_div[2] = (w83781d_read_value(client,
1625 W83781D_REG_PIN) >> 6) & 0x03;
1626 if ((data->type != w83781d) && (data->type != as99127f)) {
1627 i = w83781d_read_value(client, W83781D_REG_VBAT);
1628 data->fan_div[0] |= (i >> 3) & 0x04;
1629 data->fan_div[1] |= (i >> 4) & 0x04;
1630 data->fan_div[2] |= (i >> 5) & 0x04;
1632 if ((data->type == w83782d) || (data->type == w83627hf)) {
1633 data->alarms = w83781d_read_value(client,
1635 | (w83781d_read_value(client,
1636 W83782D_REG_ALARM2) << 8)
1637 | (w83781d_read_value(client,
1638 W83782D_REG_ALARM3) << 16);
1639 } else if (data->type == w83783s) {
1640 data->alarms = w83781d_read_value(client,
1642 | (w83781d_read_value(client,
1643 W83782D_REG_ALARM2) << 8);
1645 /* No real-time status registers, fall back to
1646 interrupt status registers */
1647 data->alarms = w83781d_read_value(client,
1649 | (w83781d_read_value(client,
1650 W83781D_REG_ALARM2) << 8);
1652 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1653 data->beep_enable = i >> 7;
1654 data->beep_mask = ((i & 0x7f) << 8) +
1655 w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1656 if ((data->type != w83781d) && (data->type != as99127f)) {
1658 w83781d_read_value(client,
1659 W83781D_REG_BEEP_INTS3) << 16;
1661 data->last_updated = jiffies;
1665 mutex_unlock(&data->update_lock);
1671 sensors_w83781d_init(void)
1675 res = i2c_add_driver(&w83781d_driver);
1679 res = i2c_isa_add_driver(&w83781d_isa_driver);
1681 i2c_del_driver(&w83781d_driver);
1689 sensors_w83781d_exit(void)
1691 i2c_isa_del_driver(&w83781d_isa_driver);
1692 i2c_del_driver(&w83781d_driver);
1695 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1696 "Philip Edelbrock <phil@netroedge.com>, "
1697 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1698 MODULE_DESCRIPTION("W83781D driver");
1699 MODULE_LICENSE("GPL");
1701 module_init(sensors_w83781d_init);
1702 module_exit(sensors_w83781d_exit);