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>
7 Copyright (c) 2007 Jean Delvare <khali@linux-fr.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Supports following chips:
27 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
28 as99127f 7 3 0 3 0x31 0x12c3 yes no
29 as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
30 w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
31 w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC)
32 w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
33 w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/slab.h>
40 #include <linux/jiffies.h>
41 #include <linux/i2c.h>
42 #include <linux/platform_device.h>
43 #include <linux/ioport.h>
44 #include <linux/hwmon.h>
45 #include <linux/hwmon-vid.h>
46 #include <linux/sysfs.h>
47 #include <linux/err.h>
48 #include <linux/mutex.h>
52 /* ISA device, if found */
53 static struct platform_device *pdev;
55 /* Addresses to scan */
56 static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
57 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
58 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
59 static unsigned short isa_address = 0x290;
61 /* Insmod parameters */
62 I2C_CLIENT_INSMOD_5(w83781d, w83782d, w83783s, w83627hf, as99127f);
63 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
64 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
67 module_param(reset, bool, 0);
68 MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
71 module_param(init, bool, 0);
72 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
74 /* Constants specified below */
76 /* Length of ISA address segment */
77 #define W83781D_EXTENT 8
79 /* Where are the ISA address/data registers relative to the base address */
80 #define W83781D_ADDR_REG_OFFSET 5
81 #define W83781D_DATA_REG_OFFSET 6
83 /* The W83781D registers */
84 /* The W83782D registers for nr=7,8 are in bank 5 */
85 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
86 (0x554 + (((nr) - 7) * 2)))
87 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
88 (0x555 + (((nr) - 7) * 2)))
89 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
92 #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
93 #define W83781D_REG_FAN(nr) (0x27 + (nr))
95 #define W83781D_REG_BANK 0x4E
96 #define W83781D_REG_TEMP2_CONFIG 0x152
97 #define W83781D_REG_TEMP3_CONFIG 0x252
98 #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
99 ((nr == 2) ? (0x0150) : \
101 #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
102 ((nr == 2) ? (0x153) : \
104 #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
105 ((nr == 2) ? (0x155) : \
108 #define W83781D_REG_CONFIG 0x40
110 /* Interrupt status (W83781D, AS99127F) */
111 #define W83781D_REG_ALARM1 0x41
112 #define W83781D_REG_ALARM2 0x42
114 /* Real-time status (W83782D, W83783S, W83627HF) */
115 #define W83782D_REG_ALARM1 0x459
116 #define W83782D_REG_ALARM2 0x45A
117 #define W83782D_REG_ALARM3 0x45B
119 #define W83781D_REG_BEEP_CONFIG 0x4D
120 #define W83781D_REG_BEEP_INTS1 0x56
121 #define W83781D_REG_BEEP_INTS2 0x57
122 #define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
124 #define W83781D_REG_VID_FANDIV 0x47
126 #define W83781D_REG_CHIPID 0x49
127 #define W83781D_REG_WCHIPID 0x58
128 #define W83781D_REG_CHIPMAN 0x4F
129 #define W83781D_REG_PIN 0x4B
132 #define W83781D_REG_VBAT 0x5D
134 /* PWM 782D (1-4) and 783S (1-2) only */
135 #define W83781D_REG_PWM1 0x5B /* 782d and 783s/627hf datasheets disagree */
136 /* on which is which; */
137 #define W83781D_REG_PWM2 0x5A /* We follow the 782d convention here, */
138 /* However 782d is probably wrong. */
139 #define W83781D_REG_PWM3 0x5E
140 #define W83781D_REG_PWM4 0x5F
141 #define W83781D_REG_PWMCLK12 0x5C
142 #define W83781D_REG_PWMCLK34 0x45C
143 static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
144 W83781D_REG_PWM3, W83781D_REG_PWM4
147 #define W83781D_REG_PWM(nr) (regpwm[(nr) - 1])
149 #define W83781D_REG_I2C_ADDR 0x48
150 #define W83781D_REG_I2C_SUBADDR 0x4A
152 /* The following are undocumented in the data sheets however we
153 received the information in an email from Winbond tech support */
154 /* Sensor selection - not on 781d */
155 #define W83781D_REG_SCFG1 0x5D
156 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
158 #define W83781D_REG_SCFG2 0x59
159 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
161 #define W83781D_DEFAULT_BETA 3435
163 /* RT Table registers */
164 #define W83781D_REG_RT_IDX 0x50
165 #define W83781D_REG_RT_VAL 0x51
167 /* Conversions. Rounding and limit checking is only done on the TO_REG
168 variants. Note that you should be a bit careful with which arguments
169 these macros are called: arguments may be evaluated more than once.
170 Fixing this is just not worth it. */
171 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
172 #define IN_FROM_REG(val) (((val) * 16) / 10)
175 FAN_TO_REG(long rpm, int div)
179 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
180 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
183 #define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
184 ((val) == 255 ? 0 : \
185 1350000 / ((val) * (div))))
187 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
188 : (val)) / 1000, 0, 0xff))
189 #define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
191 #define PWM_FROM_REG(val) (val)
192 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
193 #define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
194 (val) ^ 0x7fff : (val))
195 #define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
196 (~(val)) & 0x7fff : (val) & 0xffffff)
198 #define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
199 #define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
201 #define DIV_FROM_REG(val) (1 << (val))
204 DIV_TO_REG(long val, enum chips type)
207 val = SENSORS_LIMIT(val, 1,
209 || type == as99127f) ? 8 : 128)) >> 1;
210 for (i = 0; i < 7; i++) {
218 /* There are some complications in a module like this. First off, W83781D chips
219 may be both present on the SMBus and the ISA bus, and we have to handle
220 those cases separately at some places. Second, there might be several
221 W83781D chips available (well, actually, that is probably never done; but
222 it is a clean illustration of how to handle a case like that). Finally,
223 a specific chip may be attached to *both* ISA and SMBus, and we would
224 not like to detect it double. Fortunately, in the case of the W83781D at
225 least, a register tells us what SMBus address we are on, so that helps
226 a bit - except if there could be more than one SMBus. Groan. No solution
229 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
230 the driver field to differentiate between I2C and ISA chips. */
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_detect(struct i2c_adapter *adapter, int address, int kind);
272 static int w83781d_detach_client(struct i2c_client *client);
274 static int __devinit w83781d_isa_probe(struct platform_device *pdev);
275 static int __devexit w83781d_isa_remove(struct platform_device *pdev);
277 static int w83781d_read_value(struct i2c_client *client, u16 reg);
278 static int w83781d_write_value(struct i2c_client *client, u16 reg, u16 value);
279 static struct w83781d_data *w83781d_update_device(struct device *dev);
280 static void w83781d_init_device(struct device *dev);
282 static struct i2c_driver w83781d_driver = {
286 .id = I2C_DRIVERID_W83781D,
287 .attach_adapter = w83781d_attach_adapter,
288 .detach_client = w83781d_detach_client,
291 static struct platform_driver w83781d_isa_driver = {
293 .owner = THIS_MODULE,
296 .probe = w83781d_isa_probe,
297 .remove = w83781d_isa_remove,
301 /* following are the sysfs callback functions */
302 #define show_in_reg(reg) \
303 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
305 struct w83781d_data *data = w83781d_update_device(dev); \
306 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
312 #define store_in_reg(REG, reg) \
313 static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
315 struct w83781d_data *data = dev_get_drvdata(dev); \
316 struct i2c_client *client = &data->client; \
319 val = simple_strtoul(buf, NULL, 10) / 10; \
321 mutex_lock(&data->update_lock); \
322 data->in_##reg[nr] = IN_TO_REG(val); \
323 w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
325 mutex_unlock(&data->update_lock); \
328 store_in_reg(MIN, min);
329 store_in_reg(MAX, max);
331 #define sysfs_in_offset(offset) \
333 show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
335 return show_in(dev, buf, offset); \
337 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
339 #define sysfs_in_reg_offset(reg, offset) \
340 static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
342 return show_in_##reg (dev, buf, offset); \
344 static ssize_t store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
346 return store_in_##reg (dev, buf, count, offset); \
348 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset);
350 #define sysfs_in_offsets(offset) \
351 sysfs_in_offset(offset); \
352 sysfs_in_reg_offset(min, offset); \
353 sysfs_in_reg_offset(max, offset);
365 #define show_fan_reg(reg) \
366 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
368 struct w83781d_data *data = w83781d_update_device(dev); \
369 return sprintf(buf,"%ld\n", \
370 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
373 show_fan_reg(fan_min);
376 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
378 struct w83781d_data *data = dev_get_drvdata(dev);
379 struct i2c_client *client = &data->client;
382 val = simple_strtoul(buf, NULL, 10);
384 mutex_lock(&data->update_lock);
385 data->fan_min[nr - 1] =
386 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
387 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
388 data->fan_min[nr - 1]);
390 mutex_unlock(&data->update_lock);
394 #define sysfs_fan_offset(offset) \
395 static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
397 return show_fan(dev, buf, offset); \
399 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
401 #define sysfs_fan_min_offset(offset) \
402 static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
404 return show_fan_min(dev, buf, offset); \
406 static ssize_t store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
408 return store_fan_min(dev, buf, count, offset); \
410 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset);
413 sysfs_fan_min_offset(1);
415 sysfs_fan_min_offset(2);
417 sysfs_fan_min_offset(3);
419 #define show_temp_reg(reg) \
420 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
422 struct w83781d_data *data = w83781d_update_device(dev); \
423 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
424 return sprintf(buf,"%d\n", \
425 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
426 } else { /* TEMP1 */ \
427 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
431 show_temp_reg(temp_max);
432 show_temp_reg(temp_max_hyst);
434 #define store_temp_reg(REG, reg) \
435 static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
437 struct w83781d_data *data = dev_get_drvdata(dev); \
438 struct i2c_client *client = &data->client; \
441 val = simple_strtol(buf, NULL, 10); \
443 mutex_lock(&data->update_lock); \
445 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
446 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
447 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
448 data->temp_##reg##_add[nr-2]); \
449 } else { /* TEMP1 */ \
450 data->temp_##reg = TEMP_TO_REG(val); \
451 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
455 mutex_unlock(&data->update_lock); \
458 store_temp_reg(OVER, max);
459 store_temp_reg(HYST, max_hyst);
461 #define sysfs_temp_offset(offset) \
463 show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
465 return show_temp(dev, buf, offset); \
467 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
469 #define sysfs_temp_reg_offset(reg, offset) \
470 static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
472 return show_temp_##reg (dev, buf, offset); \
474 static ssize_t store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
476 return store_temp_##reg (dev, buf, count, offset); \
478 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
480 #define sysfs_temp_offsets(offset) \
481 sysfs_temp_offset(offset); \
482 sysfs_temp_reg_offset(max, offset); \
483 sysfs_temp_reg_offset(max_hyst, offset);
485 sysfs_temp_offsets(1);
486 sysfs_temp_offsets(2);
487 sysfs_temp_offsets(3);
490 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
492 struct w83781d_data *data = w83781d_update_device(dev);
493 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
496 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
499 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
501 struct w83781d_data *data = w83781d_update_device(dev);
502 return sprintf(buf, "%ld\n", (long) data->vrm);
506 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
508 struct w83781d_data *data = dev_get_drvdata(dev);
511 val = simple_strtoul(buf, NULL, 10);
517 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
520 show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
522 struct w83781d_data *data = w83781d_update_device(dev);
523 return sprintf(buf, "%u\n", data->alarms);
526 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
528 static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
530 struct w83781d_data *data = w83781d_update_device(dev);
531 return sprintf(buf, "%ld\n",
532 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
534 static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
536 struct w83781d_data *data = w83781d_update_device(dev);
537 return sprintf(buf, "%ld\n",
538 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
541 #define BEEP_ENABLE 0 /* Store beep_enable */
542 #define BEEP_MASK 1 /* Store beep_mask */
545 store_beep_reg(struct device *dev, const char *buf, size_t count,
548 struct w83781d_data *data = dev_get_drvdata(dev);
549 struct i2c_client *client = &data->client;
552 val = simple_strtoul(buf, NULL, 10);
554 mutex_lock(&data->update_lock);
556 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
557 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
558 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
559 data->beep_mask & 0xff);
561 if ((data->type != w83781d) && (data->type != as99127f)) {
562 w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
563 ((data->beep_mask) >> 16) & 0xff);
566 val2 = (data->beep_mask >> 8) & 0x7f;
567 } else { /* We are storing beep_enable */
568 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
569 data->beep_enable = BEEP_ENABLE_TO_REG(val);
572 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
573 val2 | data->beep_enable << 7);
575 mutex_unlock(&data->update_lock);
579 #define sysfs_beep(REG, reg) \
580 static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
582 return show_beep_##reg(dev, attr, buf); \
584 static ssize_t store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
586 return store_beep_reg(dev, buf, count, BEEP_##REG); \
588 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg);
590 sysfs_beep(ENABLE, enable);
591 sysfs_beep(MASK, mask);
594 show_fan_div_reg(struct device *dev, char *buf, int nr)
596 struct w83781d_data *data = w83781d_update_device(dev);
597 return sprintf(buf, "%ld\n",
598 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
601 /* Note: we save and restore the fan minimum here, because its value is
602 determined in part by the fan divisor. This follows the principle of
603 least surprise; the user doesn't expect the fan minimum to change just
604 because the divisor changed. */
606 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
608 struct w83781d_data *data = dev_get_drvdata(dev);
609 struct i2c_client *client = &data->client;
612 unsigned long val = simple_strtoul(buf, NULL, 10);
614 mutex_lock(&data->update_lock);
617 min = FAN_FROM_REG(data->fan_min[nr],
618 DIV_FROM_REG(data->fan_div[nr]));
620 data->fan_div[nr] = DIV_TO_REG(val, data->type);
622 reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
623 & (nr==0 ? 0xcf : 0x3f))
624 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
625 w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
627 /* w83781d and as99127f don't have extended divisor bits */
628 if (data->type != w83781d && data->type != as99127f) {
629 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
631 | ((data->fan_div[nr] & 0x04) << (3 + nr));
632 w83781d_write_value(client, W83781D_REG_VBAT, reg);
635 /* Restore fan_min */
636 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
637 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
639 mutex_unlock(&data->update_lock);
643 #define sysfs_fan_div(offset) \
644 static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
646 return show_fan_div_reg(dev, buf, offset); \
648 static ssize_t store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
650 return store_fan_div_reg(dev, buf, count, offset - 1); \
652 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset);
659 show_pwm_reg(struct device *dev, char *buf, int nr)
661 struct w83781d_data *data = w83781d_update_device(dev);
662 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
666 show_pwmenable_reg(struct device *dev, char *buf, int nr)
668 struct w83781d_data *data = w83781d_update_device(dev);
669 return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
673 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
675 struct w83781d_data *data = dev_get_drvdata(dev);
676 struct i2c_client *client = &data->client;
679 val = simple_strtoul(buf, NULL, 10);
681 mutex_lock(&data->update_lock);
682 data->pwm[nr - 1] = PWM_TO_REG(val);
683 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
684 mutex_unlock(&data->update_lock);
689 store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
691 struct w83781d_data *data = dev_get_drvdata(dev);
692 struct i2c_client *client = &data->client;
695 val = simple_strtoul(buf, NULL, 10);
697 mutex_lock(&data->update_lock);
702 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
703 w83781d_write_value(client, W83781D_REG_PWMCLK12,
704 (reg & 0xf7) | (val << 3));
706 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
707 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
708 (reg & 0xef) | (!val << 4));
710 data->pwmenable[nr - 1] = val;
714 mutex_unlock(&data->update_lock);
718 mutex_unlock(&data->update_lock);
722 #define sysfs_pwm(offset) \
723 static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
725 return show_pwm_reg(dev, buf, offset); \
727 static ssize_t store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, \
728 const char *buf, size_t count) \
730 return store_pwm_reg(dev, buf, count, offset); \
732 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
733 show_regs_pwm_##offset, store_regs_pwm_##offset);
735 #define sysfs_pwmenable(offset) \
736 static ssize_t show_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
738 return show_pwmenable_reg(dev, buf, offset); \
740 static ssize_t store_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, \
741 const char *buf, size_t count) \
743 return store_pwmenable_reg(dev, buf, count, offset); \
745 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
746 show_regs_pwmenable_##offset, store_regs_pwmenable_##offset);
750 sysfs_pwmenable(2); /* only PWM2 can be enabled/disabled */
755 show_sensor_reg(struct device *dev, char *buf, int nr)
757 struct w83781d_data *data = w83781d_update_device(dev);
758 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
762 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
764 struct w83781d_data *data = dev_get_drvdata(dev);
765 struct i2c_client *client = &data->client;
768 val = simple_strtoul(buf, NULL, 10);
770 mutex_lock(&data->update_lock);
773 case 1: /* PII/Celeron diode */
774 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
775 w83781d_write_value(client, W83781D_REG_SCFG1,
776 tmp | BIT_SCFG1[nr - 1]);
777 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
778 w83781d_write_value(client, W83781D_REG_SCFG2,
779 tmp | BIT_SCFG2[nr - 1]);
780 data->sens[nr - 1] = val;
783 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
784 w83781d_write_value(client, W83781D_REG_SCFG1,
785 tmp | BIT_SCFG1[nr - 1]);
786 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
787 w83781d_write_value(client, W83781D_REG_SCFG2,
788 tmp & ~BIT_SCFG2[nr - 1]);
789 data->sens[nr - 1] = val;
791 case W83781D_DEFAULT_BETA: /* thermistor */
792 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
793 w83781d_write_value(client, W83781D_REG_SCFG1,
794 tmp & ~BIT_SCFG1[nr - 1]);
795 data->sens[nr - 1] = val;
798 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
799 (long) val, W83781D_DEFAULT_BETA);
803 mutex_unlock(&data->update_lock);
807 #define sysfs_sensor(offset) \
808 static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
810 return show_sensor_reg(dev, buf, offset); \
812 static ssize_t store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
814 return store_sensor_reg(dev, buf, count, offset); \
816 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset);
822 /* I2C devices get this name attribute automatically, but for ISA devices
823 we must create it by ourselves. */
825 show_name(struct device *dev, struct device_attribute *devattr, char *buf)
827 struct w83781d_data *data = dev_get_drvdata(dev);
828 return sprintf(buf, "%s\n", data->client.name);
830 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
832 /* This function is called when:
833 * w83781d_driver is inserted (when this module is loaded), for each
835 * when a new adapter is inserted (and w83781d_driver is still present) */
837 w83781d_attach_adapter(struct i2c_adapter *adapter)
839 if (!(adapter->class & I2C_CLASS_HWMON))
841 return i2c_probe(adapter, &addr_data, w83781d_detect);
844 /* Assumes that adapter is of I2C, not ISA variety.
845 * OTHERWISE DON'T CALL THIS
848 w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
849 struct i2c_client *new_client)
853 const char *client_name = "";
854 struct w83781d_data *data = i2c_get_clientdata(new_client);
856 data->lm75[0] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
857 if (!(data->lm75[0])) {
862 id = i2c_adapter_id(adapter);
864 if (force_subclients[0] == id && force_subclients[1] == address) {
865 for (i = 2; i <= 3; i++) {
866 if (force_subclients[i] < 0x48 ||
867 force_subclients[i] > 0x4f) {
868 dev_err(&new_client->dev, "Invalid subclient "
869 "address %d; must be 0x48-0x4f\n",
870 force_subclients[i]);
875 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
876 (force_subclients[2] & 0x07) |
877 ((force_subclients[3] & 0x07) << 4));
878 data->lm75[0]->addr = force_subclients[2];
880 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
881 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
884 if (kind != w83783s) {
885 data->lm75[1] = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
886 if (!(data->lm75[1])) {
891 if (force_subclients[0] == id &&
892 force_subclients[1] == address) {
893 data->lm75[1]->addr = force_subclients[3];
895 data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
897 if (data->lm75[0]->addr == data->lm75[1]->addr) {
898 dev_err(&new_client->dev,
899 "Duplicate addresses 0x%x for subclients.\n",
900 data->lm75[0]->addr);
907 client_name = "w83781d subclient";
908 else if (kind == w83782d)
909 client_name = "w83782d subclient";
910 else if (kind == w83783s)
911 client_name = "w83783s subclient";
912 else if (kind == w83627hf)
913 client_name = "w83627hf subclient";
914 else if (kind == as99127f)
915 client_name = "as99127f subclient";
917 for (i = 0; i <= 1; i++) {
918 /* store all data in w83781d */
919 i2c_set_clientdata(data->lm75[i], NULL);
920 data->lm75[i]->adapter = adapter;
921 data->lm75[i]->driver = &w83781d_driver;
922 data->lm75[i]->flags = 0;
923 strlcpy(data->lm75[i]->name, client_name,
925 if ((err = i2c_attach_client(data->lm75[i]))) {
926 dev_err(&new_client->dev, "Subclient %d "
927 "registration at address 0x%x "
928 "failed.\n", i, data->lm75[i]->addr);
939 /* Undo inits in case of errors */
941 i2c_detach_client(data->lm75[0]);
943 kfree(data->lm75[1]);
945 kfree(data->lm75[0]);
950 #define IN_UNIT_ATTRS(X) \
951 &dev_attr_in##X##_input.attr, \
952 &dev_attr_in##X##_min.attr, \
953 &dev_attr_in##X##_max.attr
955 #define FAN_UNIT_ATTRS(X) \
956 &dev_attr_fan##X##_input.attr, \
957 &dev_attr_fan##X##_min.attr, \
958 &dev_attr_fan##X##_div.attr
960 #define TEMP_UNIT_ATTRS(X) \
961 &dev_attr_temp##X##_input.attr, \
962 &dev_attr_temp##X##_max.attr, \
963 &dev_attr_temp##X##_max_hyst.attr
965 static struct attribute* w83781d_attributes[] = {
977 &dev_attr_cpu0_vid.attr,
979 &dev_attr_alarms.attr,
980 &dev_attr_beep_mask.attr,
981 &dev_attr_beep_enable.attr,
984 static const struct attribute_group w83781d_group = {
985 .attrs = w83781d_attributes,
988 static struct attribute *w83781d_attributes_opt[] = {
995 &dev_attr_pwm2_enable.attr,
998 &dev_attr_temp1_type.attr,
999 &dev_attr_temp2_type.attr,
1000 &dev_attr_temp3_type.attr,
1003 static const struct attribute_group w83781d_group_opt = {
1004 .attrs = w83781d_attributes_opt,
1007 /* No clean up is done on error, it's up to the caller */
1009 w83781d_create_files(struct device *dev, int kind, int is_isa)
1013 if ((err = sysfs_create_group(&dev->kobj, &w83781d_group)))
1016 if (kind != w83783s) {
1017 if ((err = device_create_file(dev, &dev_attr_in1_input))
1018 || (err = device_create_file(dev, &dev_attr_in1_min))
1019 || (err = device_create_file(dev, &dev_attr_in1_max)))
1022 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1023 if ((err = device_create_file(dev, &dev_attr_in7_input))
1024 || (err = device_create_file(dev, &dev_attr_in7_min))
1025 || (err = device_create_file(dev, &dev_attr_in7_max))
1026 || (err = device_create_file(dev, &dev_attr_in8_input))
1027 || (err = device_create_file(dev, &dev_attr_in8_min))
1028 || (err = device_create_file(dev, &dev_attr_in8_max)))
1031 if (kind != w83783s) {
1032 if ((err = device_create_file(dev, &dev_attr_temp3_input))
1033 || (err = device_create_file(dev, &dev_attr_temp3_max))
1034 || (err = device_create_file(dev,
1035 &dev_attr_temp3_max_hyst)))
1039 if (kind != w83781d && kind != as99127f) {
1040 if ((err = device_create_file(dev, &dev_attr_pwm1))
1041 || (err = device_create_file(dev, &dev_attr_pwm2))
1042 || (err = device_create_file(dev, &dev_attr_pwm2_enable)))
1045 if (kind == w83782d && !is_isa) {
1046 if ((err = device_create_file(dev, &dev_attr_pwm3))
1047 || (err = device_create_file(dev, &dev_attr_pwm4)))
1051 if (kind != as99127f && kind != w83781d) {
1052 if ((err = device_create_file(dev, &dev_attr_temp1_type))
1053 || (err = device_create_file(dev,
1054 &dev_attr_temp2_type)))
1056 if (kind != w83783s) {
1057 if ((err = device_create_file(dev,
1058 &dev_attr_temp3_type)))
1064 err = device_create_file(&pdev->dev, &dev_attr_name);
1073 w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
1076 struct i2c_client *client;
1078 struct w83781d_data *data;
1080 const char *client_name = "";
1081 enum vendor { winbond, asus } vendid;
1083 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1088 /* OK. For now, we presume we have a valid client. We now create the
1089 client structure, even though we cannot fill it completely yet.
1090 But it allows us to access w83781d_{read,write}_value. */
1092 if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1097 client = &data->client;
1098 i2c_set_clientdata(client, data);
1099 client->addr = address;
1100 mutex_init(&data->lock);
1101 client->adapter = adapter;
1102 client->driver = &w83781d_driver;
1105 /* Now, we do the remaining detection. */
1107 /* The w8378?d may be stuck in some other bank than bank 0. This may
1108 make reading other information impossible. Specify a force=... or
1109 force_*=... parameter, and the Winbond will be reset to the right
1112 if (w83781d_read_value(client, W83781D_REG_CONFIG) & 0x80) {
1113 dev_dbg(&adapter->dev, "Detection of w83781d chip "
1114 "failed at step 3\n");
1118 val1 = w83781d_read_value(client, W83781D_REG_BANK);
1119 val2 = w83781d_read_value(client, W83781D_REG_CHIPMAN);
1120 /* Check for Winbond or Asus ID if in bank 0 */
1121 if ((!(val1 & 0x07)) &&
1122 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1123 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1124 dev_dbg(&adapter->dev, "Detection of w83781d chip "
1125 "failed at step 4\n");
1129 /* If Winbond SMBus, check address at 0x48.
1130 Asus doesn't support, except for as99127f rev.2 */
1131 if ((!(val1 & 0x80) && (val2 == 0xa3)) ||
1132 ((val1 & 0x80) && (val2 == 0x5c))) {
1133 if (w83781d_read_value
1134 (client, W83781D_REG_I2C_ADDR) != address) {
1135 dev_dbg(&adapter->dev, "Detection of w83781d "
1136 "chip failed at step 5\n");
1143 /* We have either had a force parameter, or we have already detected the
1144 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1145 w83781d_write_value(client, W83781D_REG_BANK,
1146 (w83781d_read_value(client, W83781D_REG_BANK)
1149 /* Determine the chip type. */
1152 val2 = w83781d_read_value(client, W83781D_REG_CHIPMAN);
1155 else if (val2 == 0x12)
1158 dev_dbg(&adapter->dev, "w83781d chip vendor is "
1159 "neither Winbond nor Asus\n");
1164 val1 = w83781d_read_value(client, W83781D_REG_WCHIPID);
1165 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1167 else if (val1 == 0x30 && vendid == winbond)
1169 else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
1171 else if (val1 == 0x21 && vendid == winbond)
1173 else if (val1 == 0x31 && address >= 0x28)
1177 dev_warn(&adapter->dev, "Ignoring 'force' "
1178 "parameter for unknown chip at "
1179 "address 0x%02x\n", address);
1185 if (kind == w83781d) {
1186 client_name = "w83781d";
1187 } else if (kind == w83782d) {
1188 client_name = "w83782d";
1189 } else if (kind == w83783s) {
1190 client_name = "w83783s";
1191 } else if (kind == w83627hf) {
1192 client_name = "w83627hf";
1193 } else if (kind == as99127f) {
1194 client_name = "as99127f";
1197 /* Fill in the remaining client fields and put into the global list */
1198 strlcpy(client->name, client_name, I2C_NAME_SIZE);
1201 /* Tell the I2C layer a new client has arrived */
1202 if ((err = i2c_attach_client(client)))
1205 /* attach secondary i2c lm75-like clients */
1206 if ((err = w83781d_detect_subclients(adapter, address,
1210 /* Initialize the chip */
1211 w83781d_init_device(dev);
1213 /* Register sysfs hooks */
1214 err = w83781d_create_files(dev, kind, 0);
1218 data->class_dev = hwmon_device_register(dev);
1219 if (IS_ERR(data->class_dev)) {
1220 err = PTR_ERR(data->class_dev);
1227 sysfs_remove_group(&dev->kobj, &w83781d_group);
1228 sysfs_remove_group(&dev->kobj, &w83781d_group_opt);
1230 if (data->lm75[1]) {
1231 i2c_detach_client(data->lm75[1]);
1232 kfree(data->lm75[1]);
1234 if (data->lm75[0]) {
1235 i2c_detach_client(data->lm75[0]);
1236 kfree(data->lm75[0]);
1239 i2c_detach_client(client);
1247 w83781d_detach_client(struct i2c_client *client)
1249 struct w83781d_data *data = i2c_get_clientdata(client);
1254 hwmon_device_unregister(data->class_dev);
1255 sysfs_remove_group(&client->dev.kobj, &w83781d_group);
1256 sysfs_remove_group(&client->dev.kobj, &w83781d_group_opt);
1259 if ((err = i2c_detach_client(client)))
1273 static int __devinit
1274 w83781d_isa_probe(struct platform_device *pdev)
1277 struct w83781d_data *data;
1278 struct resource *res;
1281 /* Reserve the ISA region */
1282 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1283 if (!request_region(res->start, W83781D_EXTENT, "w83781d")) {
1288 if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1290 goto exit_release_region;
1292 mutex_init(&data->lock);
1293 data->client.addr = res->start;
1294 i2c_set_clientdata(&data->client, data);
1295 platform_set_drvdata(pdev, data);
1297 reg = w83781d_read_value(&data->client, W83781D_REG_WCHIPID);
1300 data->type = w83627hf;
1304 data->type = w83782d;
1308 data->type = w83781d;
1311 strlcpy(data->client.name, name, I2C_NAME_SIZE);
1313 /* Initialize the W83781D chip */
1314 w83781d_init_device(&pdev->dev);
1316 /* Register sysfs hooks */
1317 err = w83781d_create_files(&pdev->dev, data->type, 1);
1319 goto exit_remove_files;
1321 data->class_dev = hwmon_device_register(&pdev->dev);
1322 if (IS_ERR(data->class_dev)) {
1323 err = PTR_ERR(data->class_dev);
1324 goto exit_remove_files;
1330 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
1331 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
1332 device_remove_file(&pdev->dev, &dev_attr_name);
1334 exit_release_region:
1335 release_region(res->start, W83781D_EXTENT);
1340 static int __devexit
1341 w83781d_isa_remove(struct platform_device *pdev)
1343 struct w83781d_data *data = platform_get_drvdata(pdev);
1345 hwmon_device_unregister(data->class_dev);
1346 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
1347 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
1348 device_remove_file(&pdev->dev, &dev_attr_name);
1349 release_region(data->client.addr, W83781D_EXTENT);
1355 /* The SMBus locks itself, usually, but nothing may access the Winbond between
1356 bank switches. ISA access must always be locked explicitly!
1357 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1358 would slow down the W83781D access and should not be necessary.
1359 There are some ugly typecasts here, but the good news is - they should
1360 nowhere else be necessary! */
1362 w83781d_read_value(struct i2c_client *client, u16 reg)
1364 struct w83781d_data *data = i2c_get_clientdata(client);
1365 int res, word_sized, bank;
1366 struct i2c_client *cl;
1368 mutex_lock(&data->lock);
1369 if (!client->driver) { /* ISA device */
1370 word_sized = (((reg & 0xff00) == 0x100)
1371 || ((reg & 0xff00) == 0x200))
1372 && (((reg & 0x00ff) == 0x50)
1373 || ((reg & 0x00ff) == 0x53)
1374 || ((reg & 0x00ff) == 0x55));
1376 outb_p(W83781D_REG_BANK,
1377 client->addr + W83781D_ADDR_REG_OFFSET);
1379 client->addr + W83781D_DATA_REG_OFFSET);
1381 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1382 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1384 outb_p((reg & 0xff) + 1,
1385 client->addr + W83781D_ADDR_REG_OFFSET);
1387 (res << 8) + inb_p(client->addr +
1388 W83781D_DATA_REG_OFFSET);
1391 outb_p(W83781D_REG_BANK,
1392 client->addr + W83781D_ADDR_REG_OFFSET);
1393 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1396 bank = (reg >> 8) & 0x0f;
1399 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1401 if (bank == 0 || bank > 2) {
1402 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1404 /* switch to subclient */
1405 cl = data->lm75[bank - 1];
1406 /* convert from ISA to LM75 I2C addresses */
1407 switch (reg & 0xff) {
1408 case 0x50: /* TEMP */
1409 res = swab16(i2c_smbus_read_word_data(cl, 0));
1411 case 0x52: /* CONFIG */
1412 res = i2c_smbus_read_byte_data(cl, 1);
1414 case 0x53: /* HYST */
1415 res = swab16(i2c_smbus_read_word_data(cl, 2));
1417 case 0x55: /* OVER */
1419 res = swab16(i2c_smbus_read_word_data(cl, 3));
1424 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1426 mutex_unlock(&data->lock);
1431 w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1433 struct w83781d_data *data = i2c_get_clientdata(client);
1434 int word_sized, bank;
1435 struct i2c_client *cl;
1437 mutex_lock(&data->lock);
1438 if (!client->driver) { /* ISA device */
1439 word_sized = (((reg & 0xff00) == 0x100)
1440 || ((reg & 0xff00) == 0x200))
1441 && (((reg & 0x00ff) == 0x53)
1442 || ((reg & 0x00ff) == 0x55));
1444 outb_p(W83781D_REG_BANK,
1445 client->addr + W83781D_ADDR_REG_OFFSET);
1447 client->addr + W83781D_DATA_REG_OFFSET);
1449 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1452 client->addr + W83781D_DATA_REG_OFFSET);
1453 outb_p((reg & 0xff) + 1,
1454 client->addr + W83781D_ADDR_REG_OFFSET);
1456 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1458 outb_p(W83781D_REG_BANK,
1459 client->addr + W83781D_ADDR_REG_OFFSET);
1460 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1463 bank = (reg >> 8) & 0x0f;
1466 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1468 if (bank == 0 || bank > 2) {
1469 i2c_smbus_write_byte_data(client, reg & 0xff,
1472 /* switch to subclient */
1473 cl = data->lm75[bank - 1];
1474 /* convert from ISA to LM75 I2C addresses */
1475 switch (reg & 0xff) {
1476 case 0x52: /* CONFIG */
1477 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1479 case 0x53: /* HYST */
1480 i2c_smbus_write_word_data(cl, 2, swab16(value));
1482 case 0x55: /* OVER */
1483 i2c_smbus_write_word_data(cl, 3, swab16(value));
1488 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1490 mutex_unlock(&data->lock);
1495 w83781d_init_device(struct device *dev)
1497 struct w83781d_data *data = dev_get_drvdata(dev);
1498 struct i2c_client *client = &data->client;
1500 int type = data->type;
1503 if (reset && type != as99127f) { /* this resets registers we don't have
1504 documentation for on the as99127f */
1505 /* Resetting the chip has been the default for a long time,
1506 but it causes the BIOS initializations (fan clock dividers,
1507 thermal sensor types...) to be lost, so it is now optional.
1508 It might even go away if nobody reports it as being useful,
1509 as I see very little reason why this would be needed at
1511 dev_info(dev, "If reset=1 solved a problem you were "
1512 "having, please report!\n");
1514 /* save these registers */
1515 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1516 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1517 /* Reset all except Watchdog values and last conversion values
1518 This sets fan-divs to 2, among others */
1519 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1520 /* Restore the registers and disable power-on abnormal beep.
1521 This saves FAN 1/2/3 input/output values set by BIOS. */
1522 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1523 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1524 /* Disable master beep-enable (reset turns it on).
1525 Individual beep_mask should be reset to off but for some reason
1526 disabling this bit helps some people not get beeped */
1527 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1530 /* Disable power-on abnormal beep, as advised by the datasheet.
1531 Already done if reset=1. */
1532 if (init && !reset && type != as99127f) {
1533 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1534 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1537 data->vrm = vid_which_vrm();
1539 if ((type != w83781d) && (type != as99127f)) {
1540 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1541 for (i = 1; i <= 3; i++) {
1542 if (!(tmp & BIT_SCFG1[i - 1])) {
1543 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1545 if (w83781d_read_value
1547 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1548 data->sens[i - 1] = 1;
1550 data->sens[i - 1] = 2;
1552 if (type == w83783s && i == 2)
1557 if (init && type != as99127f) {
1559 tmp = w83781d_read_value(client, W83781D_REG_TEMP2_CONFIG);
1561 dev_warn(dev, "Enabling temp2, readings "
1562 "might not make sense\n");
1563 w83781d_write_value(client, W83781D_REG_TEMP2_CONFIG,
1568 if (type != w83783s) {
1569 tmp = w83781d_read_value(client,
1570 W83781D_REG_TEMP3_CONFIG);
1572 dev_warn(dev, "Enabling temp3, "
1573 "readings might not make sense\n");
1574 w83781d_write_value(client,
1575 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1580 /* Start monitoring */
1581 w83781d_write_value(client, W83781D_REG_CONFIG,
1582 (w83781d_read_value(client,
1583 W83781D_REG_CONFIG) & 0xf7)
1586 /* A few vars need to be filled upon startup */
1587 for (i = 1; i <= 3; i++) {
1588 data->fan_min[i - 1] = w83781d_read_value(client,
1589 W83781D_REG_FAN_MIN(i));
1591 if (type != w83781d && type != as99127f)
1592 for (i = 0; i < 4; i++)
1593 data->pwmenable[i] = 1;
1595 mutex_init(&data->update_lock);
1598 static struct w83781d_data *w83781d_update_device(struct device *dev)
1600 struct w83781d_data *data = dev_get_drvdata(dev);
1601 struct i2c_client *client = &data->client;
1604 mutex_lock(&data->update_lock);
1606 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1608 dev_dbg(dev, "Starting device update\n");
1610 for (i = 0; i <= 8; i++) {
1611 if (data->type == w83783s && i == 1)
1612 continue; /* 783S has no in1 */
1614 w83781d_read_value(client, W83781D_REG_IN(i));
1616 w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1618 w83781d_read_value(client, W83781D_REG_IN_MAX(i));
1619 if ((data->type != w83782d)
1620 && (data->type != w83627hf) && (i == 6))
1623 for (i = 1; i <= 3; i++) {
1625 w83781d_read_value(client, W83781D_REG_FAN(i));
1626 data->fan_min[i - 1] =
1627 w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1629 if (data->type != w83781d && data->type != as99127f) {
1630 for (i = 1; i <= 4; i++) {
1632 w83781d_read_value(client,
1633 W83781D_REG_PWM(i));
1634 if ((data->type != w83782d || !client->driver)
1638 /* Only PWM2 can be disabled */
1639 data->pwmenable[1] = (w83781d_read_value(client,
1640 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1643 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1645 w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1646 data->temp_max_hyst =
1647 w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1649 w83781d_read_value(client, W83781D_REG_TEMP(2));
1650 data->temp_max_add[0] =
1651 w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1652 data->temp_max_hyst_add[0] =
1653 w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
1654 if (data->type != w83783s) {
1656 w83781d_read_value(client, W83781D_REG_TEMP(3));
1657 data->temp_max_add[1] =
1658 w83781d_read_value(client,
1659 W83781D_REG_TEMP_OVER(3));
1660 data->temp_max_hyst_add[1] =
1661 w83781d_read_value(client,
1662 W83781D_REG_TEMP_HYST(3));
1664 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
1665 data->vid = i & 0x0f;
1666 data->vid |= (w83781d_read_value(client,
1667 W83781D_REG_CHIPID) & 0x01) << 4;
1668 data->fan_div[0] = (i >> 4) & 0x03;
1669 data->fan_div[1] = (i >> 6) & 0x03;
1670 data->fan_div[2] = (w83781d_read_value(client,
1671 W83781D_REG_PIN) >> 6) & 0x03;
1672 if ((data->type != w83781d) && (data->type != as99127f)) {
1673 i = w83781d_read_value(client, W83781D_REG_VBAT);
1674 data->fan_div[0] |= (i >> 3) & 0x04;
1675 data->fan_div[1] |= (i >> 4) & 0x04;
1676 data->fan_div[2] |= (i >> 5) & 0x04;
1678 if ((data->type == w83782d) || (data->type == w83627hf)) {
1679 data->alarms = w83781d_read_value(client,
1681 | (w83781d_read_value(client,
1682 W83782D_REG_ALARM2) << 8)
1683 | (w83781d_read_value(client,
1684 W83782D_REG_ALARM3) << 16);
1685 } else if (data->type == w83783s) {
1686 data->alarms = w83781d_read_value(client,
1688 | (w83781d_read_value(client,
1689 W83782D_REG_ALARM2) << 8);
1691 /* No real-time status registers, fall back to
1692 interrupt status registers */
1693 data->alarms = w83781d_read_value(client,
1695 | (w83781d_read_value(client,
1696 W83781D_REG_ALARM2) << 8);
1698 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1699 data->beep_enable = i >> 7;
1700 data->beep_mask = ((i & 0x7f) << 8) +
1701 w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1702 if ((data->type != w83781d) && (data->type != as99127f)) {
1704 w83781d_read_value(client,
1705 W83781D_REG_BEEP_INTS3) << 16;
1707 data->last_updated = jiffies;
1711 mutex_unlock(&data->update_lock);
1716 /* return 1 if a supported chip is found, 0 otherwise */
1718 w83781d_isa_found(unsigned short address)
1720 int val, save, found = 0;
1722 if (!request_region(address, W83781D_EXTENT, "w83781d"))
1725 #define REALLY_SLOW_IO
1726 /* We need the timeouts for at least some W83781D-like
1727 chips. But only if we read 'undefined' registers. */
1728 val = inb_p(address + 1);
1729 if (inb_p(address + 2) != val
1730 || inb_p(address + 3) != val
1731 || inb_p(address + 7) != val) {
1732 pr_debug("w83781d: Detection failed at step 1\n");
1735 #undef REALLY_SLOW_IO
1737 /* We should be able to change the 7 LSB of the address port. The
1738 MSB (busy flag) should be clear initially, set after the write. */
1739 save = inb_p(address + W83781D_ADDR_REG_OFFSET);
1741 pr_debug("w83781d: Detection failed at step 2\n");
1745 outb_p(val, address + W83781D_ADDR_REG_OFFSET);
1746 if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
1747 outb_p(save, address + W83781D_ADDR_REG_OFFSET);
1748 pr_debug("w83781d: Detection failed at step 3\n");
1752 /* We found a device, now see if it could be a W83781D */
1753 outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
1754 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1756 pr_debug("w83781d: Detection failed at step 4\n");
1759 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1760 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1761 outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
1762 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1763 if ((!(save & 0x80) && (val != 0xa3))
1764 || ((save & 0x80) && (val != 0x5c))) {
1765 pr_debug("w83781d: Detection failed at step 5\n");
1768 outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
1769 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1770 if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */
1771 pr_debug("w83781d: Detection failed at step 6\n");
1775 /* The busy flag should be clear again */
1776 if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
1777 pr_debug("w83781d: Detection failed at step 7\n");
1781 /* Determine the chip type */
1782 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1783 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1784 outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
1785 outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
1786 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1787 if ((val & 0xfe) == 0x10 /* W83781D */
1788 || val == 0x30 /* W83782D */
1789 || val == 0x21) /* W83627HF */
1793 pr_info("w83781d: Found a %s chip at %#x\n",
1794 val == 0x21 ? "W83627HF" :
1795 val == 0x30 ? "W83782D" : "W83781D", (int)address);
1798 release_region(address, W83781D_EXTENT);
1803 w83781d_isa_device_add(unsigned short address)
1805 struct resource res = {
1807 .end = address + W83781D_EXTENT,
1809 .flags = IORESOURCE_IO,
1813 pdev = platform_device_alloc("w83781d", address);
1816 printk(KERN_ERR "w83781d: Device allocation failed\n");
1820 err = platform_device_add_resources(pdev, &res, 1);
1822 printk(KERN_ERR "w83781d: Device resource addition failed "
1824 goto exit_device_put;
1827 err = platform_device_add(pdev);
1829 printk(KERN_ERR "w83781d: Device addition failed (%d)\n",
1831 goto exit_device_put;
1837 platform_device_put(pdev);
1844 sensors_w83781d_init(void)
1848 res = i2c_add_driver(&w83781d_driver);
1852 if (w83781d_isa_found(isa_address)) {
1853 res = platform_driver_register(&w83781d_isa_driver);
1855 goto exit_unreg_i2c_driver;
1857 /* Sets global pdev as a side effect */
1858 res = w83781d_isa_device_add(isa_address);
1860 goto exit_unreg_isa_driver;
1865 exit_unreg_isa_driver:
1866 platform_driver_unregister(&w83781d_isa_driver);
1867 exit_unreg_i2c_driver:
1868 i2c_del_driver(&w83781d_driver);
1874 sensors_w83781d_exit(void)
1877 platform_device_unregister(pdev);
1878 platform_driver_unregister(&w83781d_isa_driver);
1880 i2c_del_driver(&w83781d_driver);
1883 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1884 "Philip Edelbrock <phil@netroedge.com>, "
1885 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1886 MODULE_DESCRIPTION("W83781D driver");
1887 MODULE_LICENSE("GPL");
1889 module_init(sensors_w83781d_init);
1890 module_exit(sensors_w83781d_exit);