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 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/platform_device.h>
42 #include <linux/ioport.h>
43 #include <linux/hwmon.h>
44 #include <linux/hwmon-vid.h>
45 #include <linux/hwmon-sysfs.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 const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
57 0x2e, 0x2f, I2C_CLIENT_END };
58 static unsigned short isa_address = 0x290;
60 /* Insmod parameters */
61 I2C_CLIENT_INSMOD_4(w83781d, w83782d, w83783s, as99127f);
62 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
63 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
66 module_param(reset, bool, 0);
67 MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
70 module_param(init, bool, 0);
71 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
73 /* Constants specified below */
75 /* Length of ISA address segment */
76 #define W83781D_EXTENT 8
78 /* Where are the ISA address/data registers relative to the base address */
79 #define W83781D_ADDR_REG_OFFSET 5
80 #define W83781D_DATA_REG_OFFSET 6
82 /* The device registers */
83 /* in nr from 0 to 8 */
84 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
85 (0x554 + (((nr) - 7) * 2)))
86 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
87 (0x555 + (((nr) - 7) * 2)))
88 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
91 /* fan nr from 0 to 2 */
92 #define W83781D_REG_FAN_MIN(nr) (0x3b + (nr))
93 #define W83781D_REG_FAN(nr) (0x28 + (nr))
95 #define W83781D_REG_BANK 0x4E
96 #define W83781D_REG_TEMP2_CONFIG 0x152
97 #define W83781D_REG_TEMP3_CONFIG 0x252
98 /* temp nr from 1 to 3 */
99 #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
100 ((nr == 2) ? (0x0150) : \
102 #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
103 ((nr == 2) ? (0x153) : \
105 #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
106 ((nr == 2) ? (0x155) : \
109 #define W83781D_REG_CONFIG 0x40
111 /* Interrupt status (W83781D, AS99127F) */
112 #define W83781D_REG_ALARM1 0x41
113 #define W83781D_REG_ALARM2 0x42
115 /* Real-time status (W83782D, W83783S) */
116 #define W83782D_REG_ALARM1 0x459
117 #define W83782D_REG_ALARM2 0x45A
118 #define W83782D_REG_ALARM3 0x45B
120 #define W83781D_REG_BEEP_CONFIG 0x4D
121 #define W83781D_REG_BEEP_INTS1 0x56
122 #define W83781D_REG_BEEP_INTS2 0x57
123 #define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
125 #define W83781D_REG_VID_FANDIV 0x47
127 #define W83781D_REG_CHIPID 0x49
128 #define W83781D_REG_WCHIPID 0x58
129 #define W83781D_REG_CHIPMAN 0x4F
130 #define W83781D_REG_PIN 0x4B
133 #define W83781D_REG_VBAT 0x5D
135 /* PWM 782D (1-4) and 783S (1-2) only */
136 static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
137 #define W83781D_REG_PWMCLK12 0x5C
138 #define W83781D_REG_PWMCLK34 0x45C
140 #define W83781D_REG_I2C_ADDR 0x48
141 #define W83781D_REG_I2C_SUBADDR 0x4A
143 /* The following are undocumented in the data sheets however we
144 received the information in an email from Winbond tech support */
145 /* Sensor selection - not on 781d */
146 #define W83781D_REG_SCFG1 0x5D
147 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
149 #define W83781D_REG_SCFG2 0x59
150 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
152 #define W83781D_DEFAULT_BETA 3435
155 #define IN_TO_REG(val) SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
156 #define IN_FROM_REG(val) ((val) * 16)
159 FAN_TO_REG(long rpm, int div)
163 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
164 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
168 FAN_FROM_REG(u8 val, int div)
174 return 1350000 / (val * div);
177 #define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -127, 128)
178 #define TEMP_FROM_REG(val) ((val) * 1000)
180 #define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
181 (val) ^ 0x7fff : (val))
182 #define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
183 (~(val)) & 0x7fff : (val) & 0xffffff)
185 #define DIV_FROM_REG(val) (1 << (val))
188 DIV_TO_REG(long val, enum chips type)
191 val = SENSORS_LIMIT(val, 1,
193 || type == as99127f) ? 8 : 128)) >> 1;
194 for (i = 0; i < 7; i++) {
202 /* There are some complications in a module like this. First off, W83781D chips
203 may be both present on the SMBus and the ISA bus, and we have to handle
204 those cases separately at some places. Second, there might be several
205 W83781D chips available (well, actually, that is probably never done; but
206 it is a clean illustration of how to handle a case like that). Finally,
207 a specific chip may be attached to *both* ISA and SMBus, and we would
208 not like to detect it double. Fortunately, in the case of the W83781D at
209 least, a register tells us what SMBus address we are on, so that helps
210 a bit - except if there could be more than one SMBus. Groan. No solution
213 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
214 the driver field to differentiate between I2C and ISA chips. */
215 struct w83781d_data {
216 struct i2c_client client;
217 struct device *hwmon_dev;
221 struct mutex update_lock;
222 char valid; /* !=0 if following fields are valid */
223 unsigned long last_updated; /* In jiffies */
225 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
226 /* array of 2 pointers to subclients */
228 u8 in[9]; /* Register value - 8 & 9 for 782D only */
229 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
230 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
231 u8 fan[3]; /* Register value */
232 u8 fan_min[3]; /* Register value */
233 s8 temp; /* Register value */
234 s8 temp_max; /* Register value */
235 s8 temp_max_hyst; /* Register value */
236 u16 temp_add[2]; /* Register value */
237 u16 temp_max_add[2]; /* Register value */
238 u16 temp_max_hyst_add[2]; /* Register value */
239 u8 fan_div[3]; /* Register encoding, shifted right */
240 u8 vid; /* Register encoding, combined */
241 u32 alarms; /* Register encoding, combined */
242 u32 beep_mask; /* Register encoding, combined */
243 u8 beep_enable; /* Boolean */
244 u8 pwm[4]; /* Register value */
245 u8 pwm2_enable; /* Boolean */
246 u16 sens[3]; /* 782D/783S only.
247 1 = pentium diode; 2 = 3904 diode;
252 static int w83781d_attach_adapter(struct i2c_adapter *adapter);
253 static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
254 static int w83781d_detach_client(struct i2c_client *client);
256 static int __devinit w83781d_isa_probe(struct platform_device *pdev);
257 static int __devexit w83781d_isa_remove(struct platform_device *pdev);
259 static int w83781d_read_value(struct w83781d_data *data, u16 reg);
260 static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
261 static struct w83781d_data *w83781d_update_device(struct device *dev);
262 static void w83781d_init_device(struct device *dev);
264 static struct i2c_driver w83781d_driver = {
268 .attach_adapter = w83781d_attach_adapter,
269 .detach_client = w83781d_detach_client,
272 static struct platform_driver w83781d_isa_driver = {
274 .owner = THIS_MODULE,
277 .probe = w83781d_isa_probe,
278 .remove = w83781d_isa_remove,
282 /* following are the sysfs callback functions */
283 #define show_in_reg(reg) \
284 static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
287 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
288 struct w83781d_data *data = w83781d_update_device(dev); \
289 return sprintf(buf, "%ld\n", \
290 (long)IN_FROM_REG(data->reg[attr->index])); \
296 #define store_in_reg(REG, reg) \
297 static ssize_t store_in_##reg (struct device *dev, struct device_attribute \
298 *da, const char *buf, size_t count) \
300 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
301 struct w83781d_data *data = dev_get_drvdata(dev); \
302 int nr = attr->index; \
305 val = simple_strtoul(buf, NULL, 10); \
307 mutex_lock(&data->update_lock); \
308 data->in_##reg[nr] = IN_TO_REG(val); \
309 w83781d_write_value(data, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
311 mutex_unlock(&data->update_lock); \
314 store_in_reg(MIN, min);
315 store_in_reg(MAX, max);
317 #define sysfs_in_offsets(offset) \
318 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
319 show_in, NULL, offset); \
320 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
321 show_in_min, store_in_min, offset); \
322 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
323 show_in_max, store_in_max, offset)
335 #define show_fan_reg(reg) \
336 static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
339 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
340 struct w83781d_data *data = w83781d_update_device(dev); \
341 return sprintf(buf,"%ld\n", \
342 FAN_FROM_REG(data->reg[attr->index], \
343 DIV_FROM_REG(data->fan_div[attr->index]))); \
346 show_fan_reg(fan_min);
349 store_fan_min(struct device *dev, struct device_attribute *da,
350 const char *buf, size_t count)
352 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
353 struct w83781d_data *data = dev_get_drvdata(dev);
354 int nr = attr->index;
357 val = simple_strtoul(buf, NULL, 10);
359 mutex_lock(&data->update_lock);
361 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
362 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
365 mutex_unlock(&data->update_lock);
369 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
370 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
371 show_fan_min, store_fan_min, 0);
372 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
373 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
374 show_fan_min, store_fan_min, 1);
375 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
376 static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
377 show_fan_min, store_fan_min, 2);
379 #define show_temp_reg(reg) \
380 static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
383 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
384 struct w83781d_data *data = w83781d_update_device(dev); \
385 int nr = attr->index; \
386 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
387 return sprintf(buf,"%d\n", \
388 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
389 } else { /* TEMP1 */ \
390 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
394 show_temp_reg(temp_max);
395 show_temp_reg(temp_max_hyst);
397 #define store_temp_reg(REG, reg) \
398 static ssize_t store_temp_##reg (struct device *dev, \
399 struct device_attribute *da, const char *buf, size_t count) \
401 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
402 struct w83781d_data *data = dev_get_drvdata(dev); \
403 int nr = attr->index; \
406 val = simple_strtol(buf, NULL, 10); \
408 mutex_lock(&data->update_lock); \
410 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
411 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
412 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
413 data->temp_##reg##_add[nr-2]); \
414 } else { /* TEMP1 */ \
415 data->temp_##reg = TEMP_TO_REG(val); \
416 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
420 mutex_unlock(&data->update_lock); \
423 store_temp_reg(OVER, max);
424 store_temp_reg(HYST, max_hyst);
426 #define sysfs_temp_offsets(offset) \
427 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
428 show_temp, NULL, offset); \
429 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
430 show_temp_max, store_temp_max, offset); \
431 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
432 show_temp_max_hyst, store_temp_max_hyst, offset);
434 sysfs_temp_offsets(1);
435 sysfs_temp_offsets(2);
436 sysfs_temp_offsets(3);
439 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
441 struct w83781d_data *data = w83781d_update_device(dev);
442 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
445 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
448 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
450 struct w83781d_data *data = dev_get_drvdata(dev);
451 return sprintf(buf, "%ld\n", (long) data->vrm);
455 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
457 struct w83781d_data *data = dev_get_drvdata(dev);
460 val = simple_strtoul(buf, NULL, 10);
466 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
469 show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
471 struct w83781d_data *data = w83781d_update_device(dev);
472 return sprintf(buf, "%u\n", data->alarms);
475 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
477 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
480 struct w83781d_data *data = w83781d_update_device(dev);
481 int bitnr = to_sensor_dev_attr(attr)->index;
482 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
485 /* The W83781D has a single alarm bit for temp2 and temp3 */
486 static ssize_t show_temp3_alarm(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 struct w83781d_data *data = w83781d_update_device(dev);
490 int bitnr = (data->type == w83781d) ? 5 : 13;
491 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
494 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
495 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
496 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
497 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
498 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
499 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
500 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
501 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
502 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
503 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
504 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
505 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
506 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
507 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
508 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0);
510 static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
512 struct w83781d_data *data = w83781d_update_device(dev);
513 return sprintf(buf, "%ld\n",
514 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
516 static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
518 struct w83781d_data *data = w83781d_update_device(dev);
519 return sprintf(buf, "%ld\n", (long)data->beep_enable);
523 store_beep_mask(struct device *dev, struct device_attribute *attr,
524 const char *buf, size_t count)
526 struct w83781d_data *data = dev_get_drvdata(dev);
529 val = simple_strtoul(buf, NULL, 10);
531 mutex_lock(&data->update_lock);
532 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
533 w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
534 data->beep_mask & 0xff);
535 w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
536 ((data->beep_mask >> 8) & 0x7f)
537 | data->beep_enable << 7);
538 if (data->type != w83781d && data->type != as99127f) {
539 w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
540 ((data->beep_mask) >> 16) & 0xff);
542 mutex_unlock(&data->update_lock);
548 store_beep_enable(struct device *dev, struct device_attribute *attr,
549 const char *buf, size_t count)
551 struct w83781d_data *data = dev_get_drvdata(dev);
554 val = simple_strtoul(buf, NULL, 10);
555 if (val != 0 && val != 1)
558 mutex_lock(&data->update_lock);
559 data->beep_enable = val;
560 val = w83781d_read_value(data, W83781D_REG_BEEP_INTS2) & 0x7f;
561 val |= data->beep_enable << 7;
562 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, val);
563 mutex_unlock(&data->update_lock);
568 static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
569 show_beep_mask, store_beep_mask);
570 static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
571 show_beep_enable, store_beep_enable);
573 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
576 struct w83781d_data *data = w83781d_update_device(dev);
577 int bitnr = to_sensor_dev_attr(attr)->index;
578 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
582 store_beep(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
585 struct w83781d_data *data = dev_get_drvdata(dev);
586 int bitnr = to_sensor_dev_attr(attr)->index;
590 bit = simple_strtoul(buf, NULL, 10);
594 mutex_lock(&data->update_lock);
596 data->beep_mask |= (1 << bitnr);
598 data->beep_mask &= ~(1 << bitnr);
601 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
605 reg &= ~(1 << bitnr);
606 w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg);
607 } else if (bitnr < 16) {
608 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
610 reg |= (1 << (bitnr - 8));
612 reg &= ~(1 << (bitnr - 8));
613 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg);
615 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3);
617 reg |= (1 << (bitnr - 16));
619 reg &= ~(1 << (bitnr - 16));
620 w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg);
622 mutex_unlock(&data->update_lock);
627 /* The W83781D has a single beep bit for temp2 and temp3 */
628 static ssize_t show_temp3_beep(struct device *dev,
629 struct device_attribute *attr, char *buf)
631 struct w83781d_data *data = w83781d_update_device(dev);
632 int bitnr = (data->type == w83781d) ? 5 : 13;
633 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
636 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
637 show_beep, store_beep, 0);
638 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
639 show_beep, store_beep, 1);
640 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
641 show_beep, store_beep, 2);
642 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
643 show_beep, store_beep, 3);
644 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
645 show_beep, store_beep, 8);
646 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
647 show_beep, store_beep, 9);
648 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
649 show_beep, store_beep, 10);
650 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
651 show_beep, store_beep, 16);
652 static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
653 show_beep, store_beep, 17);
654 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
655 show_beep, store_beep, 6);
656 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
657 show_beep, store_beep, 7);
658 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
659 show_beep, store_beep, 11);
660 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
661 show_beep, store_beep, 4);
662 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
663 show_beep, store_beep, 5);
664 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO,
665 show_temp3_beep, store_beep, 13);
668 show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
670 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
671 struct w83781d_data *data = w83781d_update_device(dev);
672 return sprintf(buf, "%ld\n",
673 (long) DIV_FROM_REG(data->fan_div[attr->index]));
676 /* Note: we save and restore the fan minimum here, because its value is
677 determined in part by the fan divisor. This follows the principle of
678 least surprise; the user doesn't expect the fan minimum to change just
679 because the divisor changed. */
681 store_fan_div(struct device *dev, struct device_attribute *da,
682 const char *buf, size_t count)
684 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
685 struct w83781d_data *data = dev_get_drvdata(dev);
687 int nr = attr->index;
689 unsigned long val = simple_strtoul(buf, NULL, 10);
691 mutex_lock(&data->update_lock);
694 min = FAN_FROM_REG(data->fan_min[nr],
695 DIV_FROM_REG(data->fan_div[nr]));
697 data->fan_div[nr] = DIV_TO_REG(val, data->type);
699 reg = (w83781d_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
700 & (nr==0 ? 0xcf : 0x3f))
701 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
702 w83781d_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
704 /* w83781d and as99127f don't have extended divisor bits */
705 if (data->type != w83781d && data->type != as99127f) {
706 reg = (w83781d_read_value(data, W83781D_REG_VBAT)
708 | ((data->fan_div[nr] & 0x04) << (3 + nr));
709 w83781d_write_value(data, W83781D_REG_VBAT, reg);
712 /* Restore fan_min */
713 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
714 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
716 mutex_unlock(&data->update_lock);
720 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
721 show_fan_div, store_fan_div, 0);
722 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
723 show_fan_div, store_fan_div, 1);
724 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
725 show_fan_div, store_fan_div, 2);
728 show_pwm(struct device *dev, struct device_attribute *da, char *buf)
730 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
731 struct w83781d_data *data = w83781d_update_device(dev);
732 return sprintf(buf, "%d\n", (int)data->pwm[attr->index]);
736 show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf)
738 struct w83781d_data *data = w83781d_update_device(dev);
739 return sprintf(buf, "%d\n", (int)data->pwm2_enable);
743 store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
746 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
747 struct w83781d_data *data = dev_get_drvdata(dev);
748 int nr = attr->index;
751 val = simple_strtoul(buf, NULL, 10);
753 mutex_lock(&data->update_lock);
754 data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
755 w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
756 mutex_unlock(&data->update_lock);
761 store_pwm2_enable(struct device *dev, struct device_attribute *da,
762 const char *buf, size_t count)
764 struct w83781d_data *data = dev_get_drvdata(dev);
767 val = simple_strtoul(buf, NULL, 10);
769 mutex_lock(&data->update_lock);
774 reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
775 w83781d_write_value(data, W83781D_REG_PWMCLK12,
776 (reg & 0xf7) | (val << 3));
778 reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
779 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
780 (reg & 0xef) | (!val << 4));
782 data->pwm2_enable = val;
786 mutex_unlock(&data->update_lock);
790 mutex_unlock(&data->update_lock);
794 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
795 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
796 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
797 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
798 /* only PWM2 can be enabled/disabled */
799 static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
800 show_pwm2_enable, store_pwm2_enable);
803 show_sensor(struct device *dev, struct device_attribute *da, char *buf)
805 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
806 struct w83781d_data *data = w83781d_update_device(dev);
807 return sprintf(buf, "%d\n", (int)data->sens[attr->index]);
811 store_sensor(struct device *dev, struct device_attribute *da,
812 const char *buf, size_t count)
814 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
815 struct w83781d_data *data = dev_get_drvdata(dev);
816 int nr = attr->index;
819 val = simple_strtoul(buf, NULL, 10);
821 mutex_lock(&data->update_lock);
824 case 1: /* PII/Celeron diode */
825 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
826 w83781d_write_value(data, W83781D_REG_SCFG1,
827 tmp | BIT_SCFG1[nr]);
828 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
829 w83781d_write_value(data, W83781D_REG_SCFG2,
830 tmp | BIT_SCFG2[nr]);
831 data->sens[nr] = val;
834 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
835 w83781d_write_value(data, W83781D_REG_SCFG1,
836 tmp | BIT_SCFG1[nr]);
837 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
838 w83781d_write_value(data, W83781D_REG_SCFG2,
839 tmp & ~BIT_SCFG2[nr]);
840 data->sens[nr] = val;
842 case W83781D_DEFAULT_BETA:
843 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
844 "instead\n", W83781D_DEFAULT_BETA);
846 case 4: /* thermistor */
847 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
848 w83781d_write_value(data, W83781D_REG_SCFG1,
849 tmp & ~BIT_SCFG1[nr]);
850 data->sens[nr] = val;
853 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n",
858 mutex_unlock(&data->update_lock);
862 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
863 show_sensor, store_sensor, 0);
864 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
865 show_sensor, store_sensor, 1);
866 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
867 show_sensor, store_sensor, 2);
869 /* I2C devices get this name attribute automatically, but for ISA devices
870 we must create it by ourselves. */
872 show_name(struct device *dev, struct device_attribute *devattr, char *buf)
874 struct w83781d_data *data = dev_get_drvdata(dev);
875 return sprintf(buf, "%s\n", data->client.name);
877 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
879 /* This function is called when:
880 * w83781d_driver is inserted (when this module is loaded), for each
882 * when a new adapter is inserted (and w83781d_driver is still present) */
884 w83781d_attach_adapter(struct i2c_adapter *adapter)
886 if (!(adapter->class & I2C_CLASS_HWMON))
888 return i2c_probe(adapter, &addr_data, w83781d_detect);
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(data, 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(data, 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 == as99127f)
960 client_name = "as99127f subclient";
962 for (i = 0; i <= 1; i++) {
963 /* store all data in w83781d */
964 i2c_set_clientdata(data->lm75[i], NULL);
965 data->lm75[i]->adapter = adapter;
966 data->lm75[i]->driver = &w83781d_driver;
967 data->lm75[i]->flags = 0;
968 strlcpy(data->lm75[i]->name, client_name,
970 if ((err = i2c_attach_client(data->lm75[i]))) {
971 dev_err(&new_client->dev, "Subclient %d "
972 "registration at address 0x%x "
973 "failed.\n", i, data->lm75[i]->addr);
984 /* Undo inits in case of errors */
986 i2c_detach_client(data->lm75[0]);
988 kfree(data->lm75[1]);
990 kfree(data->lm75[0]);
995 #define IN_UNIT_ATTRS(X) \
996 &sensor_dev_attr_in##X##_input.dev_attr.attr, \
997 &sensor_dev_attr_in##X##_min.dev_attr.attr, \
998 &sensor_dev_attr_in##X##_max.dev_attr.attr, \
999 &sensor_dev_attr_in##X##_alarm.dev_attr.attr, \
1000 &sensor_dev_attr_in##X##_beep.dev_attr.attr
1002 #define FAN_UNIT_ATTRS(X) \
1003 &sensor_dev_attr_fan##X##_input.dev_attr.attr, \
1004 &sensor_dev_attr_fan##X##_min.dev_attr.attr, \
1005 &sensor_dev_attr_fan##X##_div.dev_attr.attr, \
1006 &sensor_dev_attr_fan##X##_alarm.dev_attr.attr, \
1007 &sensor_dev_attr_fan##X##_beep.dev_attr.attr
1009 #define TEMP_UNIT_ATTRS(X) \
1010 &sensor_dev_attr_temp##X##_input.dev_attr.attr, \
1011 &sensor_dev_attr_temp##X##_max.dev_attr.attr, \
1012 &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr, \
1013 &sensor_dev_attr_temp##X##_alarm.dev_attr.attr, \
1014 &sensor_dev_attr_temp##X##_beep.dev_attr.attr
1016 static struct attribute* w83781d_attributes[] = {
1028 &dev_attr_cpu0_vid.attr,
1030 &dev_attr_alarms.attr,
1031 &dev_attr_beep_mask.attr,
1032 &dev_attr_beep_enable.attr,
1035 static const struct attribute_group w83781d_group = {
1036 .attrs = w83781d_attributes,
1039 static struct attribute *w83781d_attributes_opt[] = {
1044 &sensor_dev_attr_pwm1.dev_attr.attr,
1045 &sensor_dev_attr_pwm2.dev_attr.attr,
1046 &sensor_dev_attr_pwm3.dev_attr.attr,
1047 &sensor_dev_attr_pwm4.dev_attr.attr,
1048 &dev_attr_pwm2_enable.attr,
1049 &sensor_dev_attr_temp1_type.dev_attr.attr,
1050 &sensor_dev_attr_temp2_type.dev_attr.attr,
1051 &sensor_dev_attr_temp3_type.dev_attr.attr,
1054 static const struct attribute_group w83781d_group_opt = {
1055 .attrs = w83781d_attributes_opt,
1058 /* No clean up is done on error, it's up to the caller */
1060 w83781d_create_files(struct device *dev, int kind, int is_isa)
1064 if ((err = sysfs_create_group(&dev->kobj, &w83781d_group)))
1067 if (kind != w83783s) {
1068 if ((err = device_create_file(dev,
1069 &sensor_dev_attr_in1_input.dev_attr))
1070 || (err = device_create_file(dev,
1071 &sensor_dev_attr_in1_min.dev_attr))
1072 || (err = device_create_file(dev,
1073 &sensor_dev_attr_in1_max.dev_attr))
1074 || (err = device_create_file(dev,
1075 &sensor_dev_attr_in1_alarm.dev_attr))
1076 || (err = device_create_file(dev,
1077 &sensor_dev_attr_in1_beep.dev_attr)))
1080 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1081 if ((err = device_create_file(dev,
1082 &sensor_dev_attr_in7_input.dev_attr))
1083 || (err = device_create_file(dev,
1084 &sensor_dev_attr_in7_min.dev_attr))
1085 || (err = device_create_file(dev,
1086 &sensor_dev_attr_in7_max.dev_attr))
1087 || (err = device_create_file(dev,
1088 &sensor_dev_attr_in7_alarm.dev_attr))
1089 || (err = device_create_file(dev,
1090 &sensor_dev_attr_in7_beep.dev_attr))
1091 || (err = device_create_file(dev,
1092 &sensor_dev_attr_in8_input.dev_attr))
1093 || (err = device_create_file(dev,
1094 &sensor_dev_attr_in8_min.dev_attr))
1095 || (err = device_create_file(dev,
1096 &sensor_dev_attr_in8_max.dev_attr))
1097 || (err = device_create_file(dev,
1098 &sensor_dev_attr_in8_alarm.dev_attr))
1099 || (err = device_create_file(dev,
1100 &sensor_dev_attr_in8_beep.dev_attr)))
1103 if (kind != w83783s) {
1104 if ((err = device_create_file(dev,
1105 &sensor_dev_attr_temp3_input.dev_attr))
1106 || (err = device_create_file(dev,
1107 &sensor_dev_attr_temp3_max.dev_attr))
1108 || (err = device_create_file(dev,
1109 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1110 || (err = device_create_file(dev,
1111 &sensor_dev_attr_temp3_alarm.dev_attr))
1112 || (err = device_create_file(dev,
1113 &sensor_dev_attr_temp3_beep.dev_attr)))
1116 if (kind != w83781d) {
1117 err = sysfs_chmod_file(&dev->kobj,
1118 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1125 if (kind != w83781d && kind != as99127f) {
1126 if ((err = device_create_file(dev,
1127 &sensor_dev_attr_pwm1.dev_attr))
1128 || (err = device_create_file(dev,
1129 &sensor_dev_attr_pwm2.dev_attr))
1130 || (err = device_create_file(dev, &dev_attr_pwm2_enable)))
1133 if (kind == w83782d && !is_isa) {
1134 if ((err = device_create_file(dev,
1135 &sensor_dev_attr_pwm3.dev_attr))
1136 || (err = device_create_file(dev,
1137 &sensor_dev_attr_pwm4.dev_attr)))
1141 if (kind != as99127f && kind != w83781d) {
1142 if ((err = device_create_file(dev,
1143 &sensor_dev_attr_temp1_type.dev_attr))
1144 || (err = device_create_file(dev,
1145 &sensor_dev_attr_temp2_type.dev_attr)))
1147 if (kind != w83783s) {
1148 if ((err = device_create_file(dev,
1149 &sensor_dev_attr_temp3_type.dev_attr)))
1155 err = device_create_file(&pdev->dev, &dev_attr_name);
1164 w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
1167 struct i2c_client *client;
1169 struct w83781d_data *data;
1171 const char *client_name = "";
1172 enum vendor { winbond, asus } vendid;
1174 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1179 /* OK. For now, we presume we have a valid client. We now create the
1180 client structure, even though we cannot fill it completely yet.
1181 But it allows us to access w83781d_{read,write}_value. */
1183 if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1188 client = &data->client;
1189 i2c_set_clientdata(client, data);
1190 client->addr = address;
1191 mutex_init(&data->lock);
1192 client->adapter = adapter;
1193 client->driver = &w83781d_driver;
1196 /* Now, we do the remaining detection. */
1198 /* The w8378?d may be stuck in some other bank than bank 0. This may
1199 make reading other information impossible. Specify a force=... or
1200 force_*=... parameter, and the Winbond will be reset to the right
1203 if (w83781d_read_value(data, W83781D_REG_CONFIG) & 0x80) {
1204 dev_dbg(&adapter->dev, "Detection of w83781d chip "
1205 "failed at step 3\n");
1209 val1 = w83781d_read_value(data, W83781D_REG_BANK);
1210 val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN);
1211 /* Check for Winbond or Asus ID if in bank 0 */
1212 if ((!(val1 & 0x07)) &&
1213 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1214 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1215 dev_dbg(&adapter->dev, "Detection of w83781d chip "
1216 "failed at step 4\n");
1220 /* If Winbond SMBus, check address at 0x48.
1221 Asus doesn't support, except for as99127f rev.2 */
1222 if ((!(val1 & 0x80) && (val2 == 0xa3)) ||
1223 ((val1 & 0x80) && (val2 == 0x5c))) {
1224 if (w83781d_read_value
1225 (data, W83781D_REG_I2C_ADDR) != address) {
1226 dev_dbg(&adapter->dev, "Detection of w83781d "
1227 "chip failed at step 5\n");
1234 /* We have either had a force parameter, or we have already detected the
1235 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1236 w83781d_write_value(data, W83781D_REG_BANK,
1237 (w83781d_read_value(data, W83781D_REG_BANK)
1240 /* Determine the chip type. */
1243 val2 = w83781d_read_value(data, W83781D_REG_CHIPMAN);
1246 else if (val2 == 0x12)
1249 dev_dbg(&adapter->dev, "w83781d chip vendor is "
1250 "neither Winbond nor Asus\n");
1255 val1 = w83781d_read_value(data, W83781D_REG_WCHIPID);
1256 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1258 else if (val1 == 0x30 && vendid == winbond)
1260 else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
1262 else if (val1 == 0x31)
1266 dev_warn(&adapter->dev, "Ignoring 'force' "
1267 "parameter for unknown chip at "
1268 "address 0x%02x\n", address);
1274 if (kind == w83781d) {
1275 client_name = "w83781d";
1276 } else if (kind == w83782d) {
1277 client_name = "w83782d";
1278 } else if (kind == w83783s) {
1279 client_name = "w83783s";
1280 } else if (kind == as99127f) {
1281 client_name = "as99127f";
1284 /* Fill in the remaining client fields and put into the global list */
1285 strlcpy(client->name, client_name, I2C_NAME_SIZE);
1288 /* Tell the I2C layer a new client has arrived */
1289 if ((err = i2c_attach_client(client)))
1292 /* attach secondary i2c lm75-like clients */
1293 if ((err = w83781d_detect_subclients(adapter, address,
1297 /* Initialize the chip */
1298 w83781d_init_device(dev);
1300 /* Register sysfs hooks */
1301 err = w83781d_create_files(dev, kind, 0);
1305 data->hwmon_dev = hwmon_device_register(dev);
1306 if (IS_ERR(data->hwmon_dev)) {
1307 err = PTR_ERR(data->hwmon_dev);
1314 sysfs_remove_group(&dev->kobj, &w83781d_group);
1315 sysfs_remove_group(&dev->kobj, &w83781d_group_opt);
1317 if (data->lm75[1]) {
1318 i2c_detach_client(data->lm75[1]);
1319 kfree(data->lm75[1]);
1321 if (data->lm75[0]) {
1322 i2c_detach_client(data->lm75[0]);
1323 kfree(data->lm75[0]);
1326 i2c_detach_client(client);
1334 w83781d_detach_client(struct i2c_client *client)
1336 struct w83781d_data *data = i2c_get_clientdata(client);
1341 hwmon_device_unregister(data->hwmon_dev);
1342 sysfs_remove_group(&client->dev.kobj, &w83781d_group);
1343 sysfs_remove_group(&client->dev.kobj, &w83781d_group_opt);
1346 if ((err = i2c_detach_client(client)))
1360 static int __devinit
1361 w83781d_isa_probe(struct platform_device *pdev)
1364 struct w83781d_data *data;
1365 struct resource *res;
1368 /* Reserve the ISA region */
1369 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1370 if (!request_region(res->start, W83781D_EXTENT, "w83781d")) {
1375 if (!(data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1377 goto exit_release_region;
1379 mutex_init(&data->lock);
1380 data->client.addr = res->start;
1381 i2c_set_clientdata(&data->client, data);
1382 platform_set_drvdata(pdev, data);
1384 reg = w83781d_read_value(data, W83781D_REG_WCHIPID);
1387 data->type = w83782d;
1391 data->type = w83781d;
1394 strlcpy(data->client.name, name, I2C_NAME_SIZE);
1396 /* Initialize the W83781D chip */
1397 w83781d_init_device(&pdev->dev);
1399 /* Register sysfs hooks */
1400 err = w83781d_create_files(&pdev->dev, data->type, 1);
1402 goto exit_remove_files;
1404 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1405 if (IS_ERR(data->hwmon_dev)) {
1406 err = PTR_ERR(data->hwmon_dev);
1407 goto exit_remove_files;
1413 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
1414 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
1415 device_remove_file(&pdev->dev, &dev_attr_name);
1417 exit_release_region:
1418 release_region(res->start, W83781D_EXTENT);
1423 static int __devexit
1424 w83781d_isa_remove(struct platform_device *pdev)
1426 struct w83781d_data *data = platform_get_drvdata(pdev);
1428 hwmon_device_unregister(data->hwmon_dev);
1429 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
1430 sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
1431 device_remove_file(&pdev->dev, &dev_attr_name);
1432 release_region(data->client.addr, W83781D_EXTENT);
1438 /* The SMBus locks itself, usually, but nothing may access the Winbond between
1439 bank switches. ISA access must always be locked explicitly!
1440 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1441 would slow down the W83781D access and should not be necessary.
1442 There are some ugly typecasts here, but the good news is - they should
1443 nowhere else be necessary! */
1445 w83781d_read_value(struct w83781d_data *data, u16 reg)
1447 struct i2c_client *client = &data->client;
1448 int res, word_sized, bank;
1449 struct i2c_client *cl;
1451 mutex_lock(&data->lock);
1452 if (!client->driver) { /* ISA device */
1453 word_sized = (((reg & 0xff00) == 0x100)
1454 || ((reg & 0xff00) == 0x200))
1455 && (((reg & 0x00ff) == 0x50)
1456 || ((reg & 0x00ff) == 0x53)
1457 || ((reg & 0x00ff) == 0x55));
1459 outb_p(W83781D_REG_BANK,
1460 client->addr + W83781D_ADDR_REG_OFFSET);
1462 client->addr + W83781D_DATA_REG_OFFSET);
1464 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1465 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1467 outb_p((reg & 0xff) + 1,
1468 client->addr + W83781D_ADDR_REG_OFFSET);
1470 (res << 8) + inb_p(client->addr +
1471 W83781D_DATA_REG_OFFSET);
1474 outb_p(W83781D_REG_BANK,
1475 client->addr + W83781D_ADDR_REG_OFFSET);
1476 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1479 bank = (reg >> 8) & 0x0f;
1482 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1484 if (bank == 0 || bank > 2) {
1485 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1487 /* switch to subclient */
1488 cl = data->lm75[bank - 1];
1489 /* convert from ISA to LM75 I2C addresses */
1490 switch (reg & 0xff) {
1491 case 0x50: /* TEMP */
1492 res = swab16(i2c_smbus_read_word_data(cl, 0));
1494 case 0x52: /* CONFIG */
1495 res = i2c_smbus_read_byte_data(cl, 1);
1497 case 0x53: /* HYST */
1498 res = swab16(i2c_smbus_read_word_data(cl, 2));
1500 case 0x55: /* OVER */
1502 res = swab16(i2c_smbus_read_word_data(cl, 3));
1507 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1509 mutex_unlock(&data->lock);
1514 w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
1516 struct i2c_client *client = &data->client;
1517 int word_sized, bank;
1518 struct i2c_client *cl;
1520 mutex_lock(&data->lock);
1521 if (!client->driver) { /* ISA device */
1522 word_sized = (((reg & 0xff00) == 0x100)
1523 || ((reg & 0xff00) == 0x200))
1524 && (((reg & 0x00ff) == 0x53)
1525 || ((reg & 0x00ff) == 0x55));
1527 outb_p(W83781D_REG_BANK,
1528 client->addr + W83781D_ADDR_REG_OFFSET);
1530 client->addr + W83781D_DATA_REG_OFFSET);
1532 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1535 client->addr + W83781D_DATA_REG_OFFSET);
1536 outb_p((reg & 0xff) + 1,
1537 client->addr + W83781D_ADDR_REG_OFFSET);
1539 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1541 outb_p(W83781D_REG_BANK,
1542 client->addr + W83781D_ADDR_REG_OFFSET);
1543 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1546 bank = (reg >> 8) & 0x0f;
1549 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1551 if (bank == 0 || bank > 2) {
1552 i2c_smbus_write_byte_data(client, reg & 0xff,
1555 /* switch to subclient */
1556 cl = data->lm75[bank - 1];
1557 /* convert from ISA to LM75 I2C addresses */
1558 switch (reg & 0xff) {
1559 case 0x52: /* CONFIG */
1560 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1562 case 0x53: /* HYST */
1563 i2c_smbus_write_word_data(cl, 2, swab16(value));
1565 case 0x55: /* OVER */
1566 i2c_smbus_write_word_data(cl, 3, swab16(value));
1571 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1573 mutex_unlock(&data->lock);
1578 w83781d_init_device(struct device *dev)
1580 struct w83781d_data *data = dev_get_drvdata(dev);
1582 int type = data->type;
1585 if (reset && type != as99127f) { /* this resets registers we don't have
1586 documentation for on the as99127f */
1587 /* Resetting the chip has been the default for a long time,
1588 but it causes the BIOS initializations (fan clock dividers,
1589 thermal sensor types...) to be lost, so it is now optional.
1590 It might even go away if nobody reports it as being useful,
1591 as I see very little reason why this would be needed at
1593 dev_info(dev, "If reset=1 solved a problem you were "
1594 "having, please report!\n");
1596 /* save these registers */
1597 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1598 p = w83781d_read_value(data, W83781D_REG_PWMCLK12);
1599 /* Reset all except Watchdog values and last conversion values
1600 This sets fan-divs to 2, among others */
1601 w83781d_write_value(data, W83781D_REG_CONFIG, 0x80);
1602 /* Restore the registers and disable power-on abnormal beep.
1603 This saves FAN 1/2/3 input/output values set by BIOS. */
1604 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
1605 w83781d_write_value(data, W83781D_REG_PWMCLK12, p);
1606 /* Disable master beep-enable (reset turns it on).
1607 Individual beep_mask should be reset to off but for some reason
1608 disabling this bit helps some people not get beeped */
1609 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0);
1612 /* Disable power-on abnormal beep, as advised by the datasheet.
1613 Already done if reset=1. */
1614 if (init && !reset && type != as99127f) {
1615 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1616 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
1619 data->vrm = vid_which_vrm();
1621 if ((type != w83781d) && (type != as99127f)) {
1622 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
1623 for (i = 1; i <= 3; i++) {
1624 if (!(tmp & BIT_SCFG1[i - 1])) {
1625 data->sens[i - 1] = 4;
1627 if (w83781d_read_value
1629 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1630 data->sens[i - 1] = 1;
1632 data->sens[i - 1] = 2;
1634 if (type == w83783s && i == 2)
1639 if (init && type != as99127f) {
1641 tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG);
1643 dev_warn(dev, "Enabling temp2, readings "
1644 "might not make sense\n");
1645 w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG,
1650 if (type != w83783s) {
1651 tmp = w83781d_read_value(data,
1652 W83781D_REG_TEMP3_CONFIG);
1654 dev_warn(dev, "Enabling temp3, "
1655 "readings might not make sense\n");
1656 w83781d_write_value(data,
1657 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1662 /* Start monitoring */
1663 w83781d_write_value(data, W83781D_REG_CONFIG,
1664 (w83781d_read_value(data,
1665 W83781D_REG_CONFIG) & 0xf7)
1668 /* A few vars need to be filled upon startup */
1669 for (i = 0; i < 3; i++) {
1670 data->fan_min[i] = w83781d_read_value(data,
1671 W83781D_REG_FAN_MIN(i));
1674 mutex_init(&data->update_lock);
1677 static struct w83781d_data *w83781d_update_device(struct device *dev)
1679 struct w83781d_data *data = dev_get_drvdata(dev);
1680 struct i2c_client *client = &data->client;
1683 mutex_lock(&data->update_lock);
1685 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1687 dev_dbg(dev, "Starting device update\n");
1689 for (i = 0; i <= 8; i++) {
1690 if (data->type == w83783s && i == 1)
1691 continue; /* 783S has no in1 */
1693 w83781d_read_value(data, W83781D_REG_IN(i));
1695 w83781d_read_value(data, W83781D_REG_IN_MIN(i));
1697 w83781d_read_value(data, W83781D_REG_IN_MAX(i));
1698 if ((data->type != w83782d) && (i == 6))
1701 for (i = 0; i < 3; i++) {
1703 w83781d_read_value(data, W83781D_REG_FAN(i));
1705 w83781d_read_value(data, W83781D_REG_FAN_MIN(i));
1707 if (data->type != w83781d && data->type != as99127f) {
1708 for (i = 0; i < 4; i++) {
1710 w83781d_read_value(data,
1711 W83781D_REG_PWM[i]);
1712 if ((data->type != w83782d || !client->driver)
1716 /* Only PWM2 can be disabled */
1717 data->pwm2_enable = (w83781d_read_value(data,
1718 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1721 data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1));
1723 w83781d_read_value(data, W83781D_REG_TEMP_OVER(1));
1724 data->temp_max_hyst =
1725 w83781d_read_value(data, W83781D_REG_TEMP_HYST(1));
1727 w83781d_read_value(data, W83781D_REG_TEMP(2));
1728 data->temp_max_add[0] =
1729 w83781d_read_value(data, W83781D_REG_TEMP_OVER(2));
1730 data->temp_max_hyst_add[0] =
1731 w83781d_read_value(data, W83781D_REG_TEMP_HYST(2));
1732 if (data->type != w83783s) {
1734 w83781d_read_value(data, W83781D_REG_TEMP(3));
1735 data->temp_max_add[1] =
1736 w83781d_read_value(data,
1737 W83781D_REG_TEMP_OVER(3));
1738 data->temp_max_hyst_add[1] =
1739 w83781d_read_value(data,
1740 W83781D_REG_TEMP_HYST(3));
1742 i = w83781d_read_value(data, W83781D_REG_VID_FANDIV);
1743 data->vid = i & 0x0f;
1744 data->vid |= (w83781d_read_value(data,
1745 W83781D_REG_CHIPID) & 0x01) << 4;
1746 data->fan_div[0] = (i >> 4) & 0x03;
1747 data->fan_div[1] = (i >> 6) & 0x03;
1748 data->fan_div[2] = (w83781d_read_value(data,
1749 W83781D_REG_PIN) >> 6) & 0x03;
1750 if ((data->type != w83781d) && (data->type != as99127f)) {
1751 i = w83781d_read_value(data, W83781D_REG_VBAT);
1752 data->fan_div[0] |= (i >> 3) & 0x04;
1753 data->fan_div[1] |= (i >> 4) & 0x04;
1754 data->fan_div[2] |= (i >> 5) & 0x04;
1756 if (data->type == w83782d) {
1757 data->alarms = w83781d_read_value(data,
1759 | (w83781d_read_value(data,
1760 W83782D_REG_ALARM2) << 8)
1761 | (w83781d_read_value(data,
1762 W83782D_REG_ALARM3) << 16);
1763 } else if (data->type == w83783s) {
1764 data->alarms = w83781d_read_value(data,
1766 | (w83781d_read_value(data,
1767 W83782D_REG_ALARM2) << 8);
1769 /* No real-time status registers, fall back to
1770 interrupt status registers */
1771 data->alarms = w83781d_read_value(data,
1773 | (w83781d_read_value(data,
1774 W83781D_REG_ALARM2) << 8);
1776 i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
1777 data->beep_enable = i >> 7;
1778 data->beep_mask = ((i & 0x7f) << 8) +
1779 w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
1780 if ((data->type != w83781d) && (data->type != as99127f)) {
1782 w83781d_read_value(data,
1783 W83781D_REG_BEEP_INTS3) << 16;
1785 data->last_updated = jiffies;
1789 mutex_unlock(&data->update_lock);
1794 /* return 1 if a supported chip is found, 0 otherwise */
1796 w83781d_isa_found(unsigned short address)
1798 int val, save, found = 0;
1800 if (!request_region(address, W83781D_EXTENT, "w83781d"))
1803 #define REALLY_SLOW_IO
1804 /* We need the timeouts for at least some W83781D-like
1805 chips. But only if we read 'undefined' registers. */
1806 val = inb_p(address + 1);
1807 if (inb_p(address + 2) != val
1808 || inb_p(address + 3) != val
1809 || inb_p(address + 7) != val) {
1810 pr_debug("w83781d: Detection failed at step 1\n");
1813 #undef REALLY_SLOW_IO
1815 /* We should be able to change the 7 LSB of the address port. The
1816 MSB (busy flag) should be clear initially, set after the write. */
1817 save = inb_p(address + W83781D_ADDR_REG_OFFSET);
1819 pr_debug("w83781d: Detection failed at step 2\n");
1823 outb_p(val, address + W83781D_ADDR_REG_OFFSET);
1824 if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
1825 outb_p(save, address + W83781D_ADDR_REG_OFFSET);
1826 pr_debug("w83781d: Detection failed at step 3\n");
1830 /* We found a device, now see if it could be a W83781D */
1831 outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
1832 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1834 pr_debug("w83781d: Detection failed at step 4\n");
1837 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1838 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1839 outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
1840 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1841 if ((!(save & 0x80) && (val != 0xa3))
1842 || ((save & 0x80) && (val != 0x5c))) {
1843 pr_debug("w83781d: Detection failed at step 5\n");
1846 outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
1847 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1848 if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */
1849 pr_debug("w83781d: Detection failed at step 6\n");
1853 /* The busy flag should be clear again */
1854 if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
1855 pr_debug("w83781d: Detection failed at step 7\n");
1859 /* Determine the chip type */
1860 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1861 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1862 outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
1863 outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
1864 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1865 if ((val & 0xfe) == 0x10 /* W83781D */
1866 || val == 0x30) /* W83782D */
1870 pr_info("w83781d: Found a %s chip at %#x\n",
1871 val == 0x30 ? "W83782D" : "W83781D", (int)address);
1874 release_region(address, W83781D_EXTENT);
1879 w83781d_isa_device_add(unsigned short address)
1881 struct resource res = {
1883 .end = address + W83781D_EXTENT - 1,
1885 .flags = IORESOURCE_IO,
1889 pdev = platform_device_alloc("w83781d", address);
1892 printk(KERN_ERR "w83781d: Device allocation failed\n");
1896 err = platform_device_add_resources(pdev, &res, 1);
1898 printk(KERN_ERR "w83781d: Device resource addition failed "
1900 goto exit_device_put;
1903 err = platform_device_add(pdev);
1905 printk(KERN_ERR "w83781d: Device addition failed (%d)\n",
1907 goto exit_device_put;
1913 platform_device_put(pdev);
1920 sensors_w83781d_init(void)
1924 res = i2c_add_driver(&w83781d_driver);
1928 if (w83781d_isa_found(isa_address)) {
1929 res = platform_driver_register(&w83781d_isa_driver);
1931 goto exit_unreg_i2c_driver;
1933 /* Sets global pdev as a side effect */
1934 res = w83781d_isa_device_add(isa_address);
1936 goto exit_unreg_isa_driver;
1941 exit_unreg_isa_driver:
1942 platform_driver_unregister(&w83781d_isa_driver);
1943 exit_unreg_i2c_driver:
1944 i2c_del_driver(&w83781d_driver);
1950 sensors_w83781d_exit(void)
1953 platform_device_unregister(pdev);
1954 platform_driver_unregister(&w83781d_isa_driver);
1956 i2c_del_driver(&w83781d_driver);
1959 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1960 "Philip Edelbrock <phil@netroedge.com>, "
1961 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1962 MODULE_DESCRIPTION("W83781D driver");
1963 MODULE_LICENSE("GPL");
1965 module_init(sensors_w83781d_init);
1966 module_exit(sensors_w83781d_exit);