2 w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 Supports following chips:
26 Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
27 as99127f 7 3 0 3 0x31 0x12c3 yes no
28 as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
29 w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
30 w83627hf 9 3 2 3 0x21 0x5ca3 yes yes(LPC)
31 w83627thf 9 3 2 3 0x90 0x5ca3 no 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
34 w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/i2c.h>
43 #include <linux/i2c-sensor.h>
44 #include <linux/i2c-vid.h>
48 /* RT Table support #defined so we can take it out if it gets bothersome */
51 /* Addresses to scan */
52 static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
53 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
54 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
55 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
57 /* Insmod parameters */
58 SENSORS_INSMOD_6(w83781d, w83782d, w83783s, w83627hf, as99127f, w83697hf);
59 I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
60 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
63 module_param(init, bool, 0);
64 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
66 /* Constants specified below */
68 /* Length of ISA address segment */
69 #define W83781D_EXTENT 8
71 /* Where are the ISA address/data registers relative to the base address */
72 #define W83781D_ADDR_REG_OFFSET 5
73 #define W83781D_DATA_REG_OFFSET 6
75 /* The W83781D registers */
76 /* The W83782D registers for nr=7,8 are in bank 5 */
77 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
78 (0x554 + (((nr) - 7) * 2)))
79 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
80 (0x555 + (((nr) - 7) * 2)))
81 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
84 #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
85 #define W83781D_REG_FAN(nr) (0x27 + (nr))
87 #define W83781D_REG_BANK 0x4E
88 #define W83781D_REG_TEMP2_CONFIG 0x152
89 #define W83781D_REG_TEMP3_CONFIG 0x252
90 #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
91 ((nr == 2) ? (0x0150) : \
93 #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
94 ((nr == 2) ? (0x153) : \
96 #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
97 ((nr == 2) ? (0x155) : \
100 #define W83781D_REG_CONFIG 0x40
101 #define W83781D_REG_ALARM1 0x41
102 #define W83781D_REG_ALARM2 0x42
103 #define W83781D_REG_ALARM3 0x450 /* not on W83781D */
105 #define W83781D_REG_IRQ 0x4C
106 #define W83781D_REG_BEEP_CONFIG 0x4D
107 #define W83781D_REG_BEEP_INTS1 0x56
108 #define W83781D_REG_BEEP_INTS2 0x57
109 #define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
111 #define W83781D_REG_VID_FANDIV 0x47
113 #define W83781D_REG_CHIPID 0x49
114 #define W83781D_REG_WCHIPID 0x58
115 #define W83781D_REG_CHIPMAN 0x4F
116 #define W83781D_REG_PIN 0x4B
119 #define W83781D_REG_VBAT 0x5D
121 /* PWM 782D (1-4) and 783S (1-2) only */
122 #define W83781D_REG_PWM1 0x5B /* 782d and 783s/627hf datasheets disagree */
123 /* on which is which; */
124 #define W83781D_REG_PWM2 0x5A /* We follow the 782d convention here, */
125 /* However 782d is probably wrong. */
126 #define W83781D_REG_PWM3 0x5E
127 #define W83781D_REG_PWM4 0x5F
128 #define W83781D_REG_PWMCLK12 0x5C
129 #define W83781D_REG_PWMCLK34 0x45C
130 static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
131 W83781D_REG_PWM3, W83781D_REG_PWM4
134 #define W83781D_REG_PWM(nr) (regpwm[(nr) - 1])
136 #define W83781D_REG_I2C_ADDR 0x48
137 #define W83781D_REG_I2C_SUBADDR 0x4A
139 /* The following are undocumented in the data sheets however we
140 received the information in an email from Winbond tech support */
141 /* Sensor selection - not on 781d */
142 #define W83781D_REG_SCFG1 0x5D
143 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
145 #define W83781D_REG_SCFG2 0x59
146 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
148 #define W83781D_DEFAULT_BETA 3435
150 /* RT Table registers */
151 #define W83781D_REG_RT_IDX 0x50
152 #define W83781D_REG_RT_VAL 0x51
154 /* Conversions. Rounding and limit checking is only done on the TO_REG
155 variants. Note that you should be a bit careful with which arguments
156 these macros are called: arguments may be evaluated more than once.
157 Fixing this is just not worth it. */
158 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
159 #define IN_FROM_REG(val) (((val) * 16) / 10)
162 FAN_TO_REG(long rpm, int div)
166 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
167 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
170 #define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
171 ((val) == 255 ? 0 : \
172 1350000 / ((val) * (div))))
174 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
175 : (val)) / 1000, 0, 0xff))
176 #define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
178 #define AS99127_TEMP_ADD_TO_REG(val) (SENSORS_LIMIT((((val) < 0 ? (val)+0x10000*250 \
179 : (val)) / 250) << 7, 0, 0xffff))
180 #define AS99127_TEMP_ADD_FROM_REG(val) ((((val) & 0x8000 ? (val)-0x10000 : (val)) \
183 #define ALARMS_FROM_REG(val) (val)
184 #define PWM_FROM_REG(val) (val)
185 #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
186 #define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
187 (val) ^ 0x7fff : (val))
188 #define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
189 (~(val)) & 0x7fff : (val) & 0xffffff)
191 #define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
192 #define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
194 #define DIV_FROM_REG(val) (1 << (val))
197 DIV_TO_REG(long val, enum chips type)
200 val = SENSORS_LIMIT(val, 1,
202 || type == as99127f) ? 8 : 128)) >> 1;
203 for (i = 0; i < 6; i++) {
211 /* There are some complications in a module like this. First off, W83781D chips
212 may be both present on the SMBus and the ISA bus, and we have to handle
213 those cases separately at some places. Second, there might be several
214 W83781D chips available (well, actually, that is probably never done; but
215 it is a clean illustration of how to handle a case like that). Finally,
216 a specific chip may be attached to *both* ISA and SMBus, and we would
217 not like to detect it double. Fortunately, in the case of the W83781D at
218 least, a register tells us what SMBus address we are on, so that helps
219 a bit - except if there could be more than one SMBus. Groan. No solution
222 /* This module may seem overly long and complicated. In fact, it is not so
223 bad. Quite a lot of bookkeeping is done. A real driver can often cut
226 /* For each registered W83781D, we need to keep some data in memory. That
227 data is pointed to by w83781d_list[NR]->data. The structure itself is
228 dynamically allocated, at the same time when a new w83781d client is
230 struct w83781d_data {
231 struct i2c_client client;
232 struct semaphore lock;
235 struct semaphore update_lock;
236 char valid; /* !=0 if following fields are valid */
237 unsigned long last_updated; /* In jiffies */
239 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
240 /* array of 2 pointers to subclients */
242 u8 in[9]; /* Register value - 8 & 9 for 782D only */
243 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
244 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
245 u8 fan[3]; /* Register value */
246 u8 fan_min[3]; /* Register value */
248 u8 temp_max; /* Register value */
249 u8 temp_max_hyst; /* Register value */
250 u16 temp_add[2]; /* Register value */
251 u16 temp_max_add[2]; /* Register value */
252 u16 temp_max_hyst_add[2]; /* Register value */
253 u8 fan_div[3]; /* Register encoding, shifted right */
254 u8 vid; /* Register encoding, combined */
255 u32 alarms; /* Register encoding, combined */
256 u32 beep_mask; /* Register encoding, combined */
257 u8 beep_enable; /* Boolean */
258 u8 pwm[4]; /* Register value */
259 u8 pwmenable[4]; /* Boolean */
260 u16 sens[3]; /* 782D/783S only.
261 1 = pentium diode; 2 = 3904 diode;
262 3000-5000 = thermistor beta.
264 Other Betas unimplemented */
266 u8 rt[3][32]; /* Register value */
271 static int w83781d_attach_adapter(struct i2c_adapter *adapter);
272 static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
273 static int w83781d_detach_client(struct i2c_client *client);
275 static int w83781d_read_value(struct i2c_client *client, u16 register);
276 static int w83781d_write_value(struct i2c_client *client, u16 register,
278 static struct w83781d_data *w83781d_update_device(struct device *dev);
279 static void w83781d_init_client(struct i2c_client *client);
281 static struct i2c_driver w83781d_driver = {
282 .owner = THIS_MODULE,
284 .id = I2C_DRIVERID_W83781D,
285 .flags = I2C_DF_NOTIFY,
286 .attach_adapter = w83781d_attach_adapter,
287 .detach_client = w83781d_detach_client,
290 /* following are the sysfs callback functions */
291 #define show_in_reg(reg) \
292 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
294 struct w83781d_data *data = w83781d_update_device(dev); \
295 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
301 #define store_in_reg(REG, reg) \
302 static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
304 struct i2c_client *client = to_i2c_client(dev); \
305 struct w83781d_data *data = i2c_get_clientdata(client); \
308 val = simple_strtoul(buf, NULL, 10) / 10; \
309 data->in_##reg[nr] = IN_TO_REG(val); \
310 w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
314 store_in_reg(MIN, min);
315 store_in_reg(MAX, max);
317 #define sysfs_in_offset(offset) \
319 show_regs_in_##offset (struct device *dev, char *buf) \
321 return show_in(dev, buf, offset); \
323 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
325 #define sysfs_in_reg_offset(reg, offset) \
326 static ssize_t show_regs_in_##reg##offset (struct device *dev, char *buf) \
328 return show_in_##reg (dev, buf, offset); \
330 static ssize_t store_regs_in_##reg##offset (struct device *dev, const char *buf, size_t count) \
332 return store_in_##reg (dev, buf, count, offset); \
334 static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset);
336 #define sysfs_in_offsets(offset) \
337 sysfs_in_offset(offset); \
338 sysfs_in_reg_offset(min, offset); \
339 sysfs_in_reg_offset(max, offset);
351 #define device_create_file_in(client, offset) \
353 device_create_file(&client->dev, &dev_attr_in##offset##_input); \
354 device_create_file(&client->dev, &dev_attr_in##offset##_min); \
355 device_create_file(&client->dev, &dev_attr_in##offset##_max); \
358 #define show_fan_reg(reg) \
359 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
361 struct w83781d_data *data = w83781d_update_device(dev); \
362 return sprintf(buf,"%ld\n", \
363 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
366 show_fan_reg(fan_min);
369 store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
371 struct i2c_client *client = to_i2c_client(dev);
372 struct w83781d_data *data = i2c_get_clientdata(client);
375 val = simple_strtoul(buf, NULL, 10);
376 data->fan_min[nr - 1] =
377 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
378 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
379 data->fan_min[nr - 1]);
384 #define sysfs_fan_offset(offset) \
385 static ssize_t show_regs_fan_##offset (struct device *dev, char *buf) \
387 return show_fan(dev, buf, offset); \
389 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
391 #define sysfs_fan_min_offset(offset) \
392 static ssize_t show_regs_fan_min##offset (struct device *dev, char *buf) \
394 return show_fan_min(dev, buf, offset); \
396 static ssize_t store_regs_fan_min##offset (struct device *dev, const char *buf, size_t count) \
398 return store_fan_min(dev, buf, count, offset); \
400 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset);
403 sysfs_fan_min_offset(1);
405 sysfs_fan_min_offset(2);
407 sysfs_fan_min_offset(3);
409 #define device_create_file_fan(client, offset) \
411 device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
412 device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
415 #define show_temp_reg(reg) \
416 static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
418 struct w83781d_data *data = w83781d_update_device(dev); \
419 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
420 if (data->type == as99127f) { \
421 return sprintf(buf,"%ld\n", \
422 (long)AS99127_TEMP_ADD_FROM_REG(data->reg##_add[nr-2])); \
424 return sprintf(buf,"%d\n", \
425 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
427 } else { /* TEMP1 */ \
428 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
432 show_temp_reg(temp_max);
433 show_temp_reg(temp_max_hyst);
435 #define store_temp_reg(REG, reg) \
436 static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
438 struct i2c_client *client = to_i2c_client(dev); \
439 struct w83781d_data *data = i2c_get_clientdata(client); \
442 val = simple_strtol(buf, NULL, 10); \
444 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
445 if (data->type == as99127f) \
446 data->temp_##reg##_add[nr-2] = AS99127_TEMP_ADD_TO_REG(val); \
448 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
450 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
451 data->temp_##reg##_add[nr-2]); \
452 } else { /* TEMP1 */ \
453 data->temp_##reg = TEMP_TO_REG(val); \
454 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
460 store_temp_reg(OVER, max);
461 store_temp_reg(HYST, max_hyst);
463 #define sysfs_temp_offset(offset) \
465 show_regs_temp_##offset (struct device *dev, char *buf) \
467 return show_temp(dev, buf, offset); \
469 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
471 #define sysfs_temp_reg_offset(reg, offset) \
472 static ssize_t show_regs_temp_##reg##offset (struct device *dev, char *buf) \
474 return show_temp_##reg (dev, buf, offset); \
476 static ssize_t store_regs_temp_##reg##offset (struct device *dev, const char *buf, size_t count) \
478 return store_temp_##reg (dev, buf, count, offset); \
480 static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
482 #define sysfs_temp_offsets(offset) \
483 sysfs_temp_offset(offset); \
484 sysfs_temp_reg_offset(max, offset); \
485 sysfs_temp_reg_offset(max_hyst, offset);
487 sysfs_temp_offsets(1);
488 sysfs_temp_offsets(2);
489 sysfs_temp_offsets(3);
491 #define device_create_file_temp(client, offset) \
493 device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
494 device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
495 device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
499 show_vid_reg(struct device *dev, char *buf)
501 struct w83781d_data *data = w83781d_update_device(dev);
502 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
506 DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
507 #define device_create_file_vid(client) \
508 device_create_file(&client->dev, &dev_attr_cpu0_vid);
510 show_vrm_reg(struct device *dev, char *buf)
512 struct w83781d_data *data = w83781d_update_device(dev);
513 return sprintf(buf, "%ld\n", (long) data->vrm);
517 store_vrm_reg(struct device *dev, const char *buf, size_t count)
519 struct i2c_client *client = to_i2c_client(dev);
520 struct w83781d_data *data = i2c_get_clientdata(client);
523 val = simple_strtoul(buf, NULL, 10);
530 DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
531 #define device_create_file_vrm(client) \
532 device_create_file(&client->dev, &dev_attr_vrm);
534 show_alarms_reg(struct device *dev, char *buf)
536 struct w83781d_data *data = w83781d_update_device(dev);
537 return sprintf(buf, "%ld\n", (long) ALARMS_FROM_REG(data->alarms));
541 DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
542 #define device_create_file_alarms(client) \
543 device_create_file(&client->dev, &dev_attr_alarms);
544 static ssize_t show_beep_mask (struct device *dev, char *buf)
546 struct w83781d_data *data = w83781d_update_device(dev);
547 return sprintf(buf, "%ld\n",
548 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
550 static ssize_t show_beep_enable (struct device *dev, char *buf)
552 struct w83781d_data *data = w83781d_update_device(dev);
553 return sprintf(buf, "%ld\n",
554 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
557 #define BEEP_ENABLE 0 /* Store beep_enable */
558 #define BEEP_MASK 1 /* Store beep_mask */
561 store_beep_reg(struct device *dev, const char *buf, size_t count,
564 struct i2c_client *client = to_i2c_client(dev);
565 struct w83781d_data *data = i2c_get_clientdata(client);
568 val = simple_strtoul(buf, NULL, 10);
570 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
571 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
572 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
573 data->beep_mask & 0xff);
575 if ((data->type != w83781d) && (data->type != as99127f)) {
576 w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
577 ((data->beep_mask) >> 16) & 0xff);
580 val2 = (data->beep_mask >> 8) & 0x7f;
581 } else { /* We are storing beep_enable */
582 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
583 data->beep_enable = BEEP_ENABLE_TO_REG(val);
586 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
587 val2 | data->beep_enable << 7);
592 #define sysfs_beep(REG, reg) \
593 static ssize_t show_regs_beep_##reg (struct device *dev, char *buf) \
595 return show_beep_##reg(dev, buf); \
597 static ssize_t store_regs_beep_##reg (struct device *dev, const char *buf, size_t count) \
599 return store_beep_reg(dev, buf, count, BEEP_##REG); \
601 static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg);
603 sysfs_beep(ENABLE, enable);
604 sysfs_beep(MASK, mask);
606 #define device_create_file_beep(client) \
608 device_create_file(&client->dev, &dev_attr_beep_enable); \
609 device_create_file(&client->dev, &dev_attr_beep_mask); \
613 show_fan_div_reg(struct device *dev, char *buf, int nr)
615 struct w83781d_data *data = w83781d_update_device(dev);
616 return sprintf(buf, "%ld\n",
617 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
620 /* Note: we save and restore the fan minimum here, because its value is
621 determined in part by the fan divisor. This follows the principle of
622 least suprise; the user doesn't expect the fan minimum to change just
623 because the divisor changed. */
625 store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
627 struct i2c_client *client = to_i2c_client(dev);
628 struct w83781d_data *data = i2c_get_clientdata(client);
633 min = FAN_FROM_REG(data->fan_min[nr],
634 DIV_FROM_REG(data->fan_div[nr]));
636 data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10),
639 reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
640 & (nr==0 ? 0xcf : 0x3f))
641 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
642 w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
644 /* w83781d and as99127f don't have extended divisor bits */
645 if (data->type != w83781d && data->type != as99127f) {
646 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
648 | ((data->fan_div[nr] & 0x04) << (3 + nr));
649 w83781d_write_value(client, W83781D_REG_VBAT, reg);
652 /* Restore fan_min */
653 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
654 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
659 #define sysfs_fan_div(offset) \
660 static ssize_t show_regs_fan_div_##offset (struct device *dev, char *buf) \
662 return show_fan_div_reg(dev, buf, offset); \
664 static ssize_t store_regs_fan_div_##offset (struct device *dev, const char *buf, size_t count) \
666 return store_fan_div_reg(dev, buf, count, offset - 1); \
668 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset);
674 #define device_create_file_fan_div(client, offset) \
676 device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
680 show_pwm_reg(struct device *dev, char *buf, int nr)
682 struct w83781d_data *data = w83781d_update_device(dev);
683 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
687 show_pwmenable_reg(struct device *dev, char *buf, int nr)
689 struct w83781d_data *data = w83781d_update_device(dev);
690 return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
694 store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
696 struct i2c_client *client = to_i2c_client(dev);
697 struct w83781d_data *data = i2c_get_clientdata(client);
700 val = simple_strtoul(buf, NULL, 10);
702 data->pwm[nr - 1] = PWM_TO_REG(val);
703 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
709 store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
711 struct i2c_client *client = to_i2c_client(dev);
712 struct w83781d_data *data = i2c_get_clientdata(client);
715 val = simple_strtoul(buf, NULL, 10);
720 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
721 w83781d_write_value(client, W83781D_REG_PWMCLK12,
722 (reg & 0xf7) | (val << 3));
724 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
725 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
726 (reg & 0xef) | (!val << 4));
728 data->pwmenable[nr - 1] = val;
738 #define sysfs_pwm(offset) \
739 static ssize_t show_regs_pwm_##offset (struct device *dev, char *buf) \
741 return show_pwm_reg(dev, buf, offset); \
743 static ssize_t store_regs_pwm_##offset (struct device *dev, \
744 const char *buf, size_t count) \
746 return store_pwm_reg(dev, buf, count, offset); \
748 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
749 show_regs_pwm_##offset, store_regs_pwm_##offset);
751 #define sysfs_pwmenable(offset) \
752 static ssize_t show_regs_pwmenable_##offset (struct device *dev, char *buf) \
754 return show_pwmenable_reg(dev, buf, offset); \
756 static ssize_t store_regs_pwmenable_##offset (struct device *dev, \
757 const char *buf, size_t count) \
759 return store_pwmenable_reg(dev, buf, count, offset); \
761 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
762 show_regs_pwmenable_##offset, store_regs_pwmenable_##offset);
766 sysfs_pwmenable(2); /* only PWM2 can be enabled/disabled */
770 #define device_create_file_pwm(client, offset) \
772 device_create_file(&client->dev, &dev_attr_pwm##offset); \
775 #define device_create_file_pwmenable(client, offset) \
777 device_create_file(&client->dev, &dev_attr_pwm##offset##_enable); \
781 show_sensor_reg(struct device *dev, char *buf, int nr)
783 struct w83781d_data *data = w83781d_update_device(dev);
784 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
788 store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
790 struct i2c_client *client = to_i2c_client(dev);
791 struct w83781d_data *data = i2c_get_clientdata(client);
794 val = simple_strtoul(buf, NULL, 10);
797 case 1: /* PII/Celeron diode */
798 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
799 w83781d_write_value(client, W83781D_REG_SCFG1,
800 tmp | BIT_SCFG1[nr - 1]);
801 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
802 w83781d_write_value(client, W83781D_REG_SCFG2,
803 tmp | BIT_SCFG2[nr - 1]);
804 data->sens[nr - 1] = val;
807 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
808 w83781d_write_value(client, W83781D_REG_SCFG1,
809 tmp | BIT_SCFG1[nr - 1]);
810 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
811 w83781d_write_value(client, W83781D_REG_SCFG2,
812 tmp & ~BIT_SCFG2[nr - 1]);
813 data->sens[nr - 1] = val;
815 case W83781D_DEFAULT_BETA: /* thermistor */
816 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
817 w83781d_write_value(client, W83781D_REG_SCFG1,
818 tmp & ~BIT_SCFG1[nr - 1]);
819 data->sens[nr - 1] = val;
822 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
823 (long) val, W83781D_DEFAULT_BETA);
830 #define sysfs_sensor(offset) \
831 static ssize_t show_regs_sensor_##offset (struct device *dev, char *buf) \
833 return show_sensor_reg(dev, buf, offset); \
835 static ssize_t store_regs_sensor_##offset (struct device *dev, const char *buf, size_t count) \
837 return store_sensor_reg(dev, buf, count, offset); \
839 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset);
845 #define device_create_file_sensor(client, offset) \
847 device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
852 show_rt_reg(struct device *dev, char *buf, int nr)
854 struct w83781d_data *data = w83781d_update_device(dev);
857 for (i = 0; i < 32; i++) {
859 j += sprintf(buf, " %ld", (long) data->rt[nr - 1][i]);
861 j += sprintf(buf, "%ld", (long) data->rt[nr - 1][i]);
863 j += sprintf(buf, "\n");
869 store_rt_reg(struct device *dev, const char *buf, size_t count, int nr)
871 struct i2c_client *client = to_i2c_client(dev);
872 struct w83781d_data *data = i2c_get_clientdata(client);
875 for (i = 0; i < count; i++) {
876 val = simple_strtoul(buf + count, NULL, 10);
878 /* fixme: no bounds checking 0-255 */
879 data->rt[nr - 1][i] = val & 0xff;
880 w83781d_write_value(client, W83781D_REG_RT_IDX, i);
881 w83781d_write_value(client, W83781D_REG_RT_VAL,
882 data->rt[nr - 1][i]);
888 #define sysfs_rt(offset) \
889 static ssize_t show_regs_rt_##offset (struct device *dev, char *buf) \
891 return show_rt_reg(dev, buf, offset); \
893 static ssize_t store_regs_rt_##offset (struct device *dev, const char *buf, size_t count) \
895 return store_rt_reg(dev, buf, count, offset); \
897 static DEVICE_ATTR(rt##offset, S_IRUGO | S_IWUSR, show_regs_rt_##offset, store_regs_rt_##offset);
903 #define device_create_file_rt(client, offset) \
905 device_create_file(&client->dev, &dev_attr_rt##offset); \
908 #endif /* ifdef W83781D_RT */
910 /* This function is called when:
911 * w83781d_driver is inserted (when this module is loaded), for each
913 * when a new adapter is inserted (and w83781d_driver is still present) */
915 w83781d_attach_adapter(struct i2c_adapter *adapter)
917 if (!(adapter->class & I2C_CLASS_HWMON))
919 return i2c_detect(adapter, &addr_data, w83781d_detect);
922 /* Assumes that adapter is of I2C, not ISA variety.
923 * OTHERWISE DON'T CALL THIS
926 w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
927 struct i2c_client *new_client)
931 const char *client_name = "";
932 struct w83781d_data *data = i2c_get_clientdata(new_client);
934 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
935 if (!(data->lm75[0])) {
939 memset(data->lm75[0], 0x00, sizeof (struct i2c_client));
941 id = i2c_adapter_id(adapter);
943 if (force_subclients[0] == id && force_subclients[1] == address) {
944 for (i = 2; i <= 3; i++) {
945 if (force_subclients[i] < 0x48 ||
946 force_subclients[i] > 0x4f) {
947 dev_err(&new_client->dev, "Invalid subclient "
948 "address %d; must be 0x48-0x4f\n",
949 force_subclients[i]);
954 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
955 (force_subclients[2] & 0x07) |
956 ((force_subclients[3] & 0x07) << 4));
957 data->lm75[0]->addr = force_subclients[2];
959 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
960 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
963 if (kind != w83783s) {
965 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
966 if (!(data->lm75[1])) {
970 memset(data->lm75[1], 0x0, sizeof(struct i2c_client));
972 if (force_subclients[0] == id &&
973 force_subclients[1] == address) {
974 data->lm75[1]->addr = force_subclients[3];
976 data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
978 if (data->lm75[0]->addr == data->lm75[1]->addr) {
979 dev_err(&new_client->dev,
980 "Duplicate addresses 0x%x for subclients.\n",
981 data->lm75[0]->addr);
988 client_name = "w83781d subclient";
989 else if (kind == w83782d)
990 client_name = "w83782d subclient";
991 else if (kind == w83783s)
992 client_name = "w83783s subclient";
993 else if (kind == w83627hf)
994 client_name = "w83627hf subclient";
995 else if (kind == as99127f)
996 client_name = "as99127f subclient";
998 for (i = 0; i <= 1; i++) {
999 /* store all data in w83781d */
1000 i2c_set_clientdata(data->lm75[i], NULL);
1001 data->lm75[i]->adapter = adapter;
1002 data->lm75[i]->driver = &w83781d_driver;
1003 data->lm75[i]->flags = 0;
1004 strlcpy(data->lm75[i]->name, client_name,
1006 if ((err = i2c_attach_client(data->lm75[i]))) {
1007 dev_err(&new_client->dev, "Subclient %d "
1008 "registration at address 0x%x "
1009 "failed.\n", i, data->lm75[i]->addr);
1014 if (kind == w83783s)
1020 /* Undo inits in case of errors */
1022 i2c_detach_client(data->lm75[0]);
1024 if (NULL != data->lm75[1])
1025 kfree(data->lm75[1]);
1027 if (NULL != data->lm75[0])
1028 kfree(data->lm75[0]);
1034 w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
1036 int i = 0, val1 = 0, val2;
1037 struct i2c_client *new_client;
1038 struct w83781d_data *data;
1040 const char *client_name = "";
1041 int is_isa = i2c_is_isa_adapter(adapter);
1042 enum vendor { winbond, asus } vendid;
1045 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1050 /* Prevent users from forcing a kind for a bus it isn't supposed
1051 to possibly be on */
1052 if (is_isa && (kind == as99127f || kind == w83783s)) {
1053 dev_err(&adapter->dev,
1054 "Cannot force I2C-only chip for ISA address 0x%02x.\n",
1059 if (!is_isa && kind == w83697hf) {
1060 dev_err(&adapter->dev,
1061 "Cannot force ISA-only chip for I2C address 0x%02x.\n",
1068 if (!request_region(address, W83781D_EXTENT,
1069 w83781d_driver.name)) {
1070 dev_dbg(&adapter->dev, "Request of region "
1071 "0x%x-0x%x for w83781d failed\n", address,
1072 address + W83781D_EXTENT - 1);
1077 /* Probe whether there is anything available on this address. Already
1078 done for SMBus clients */
1082 #define REALLY_SLOW_IO
1083 /* We need the timeouts for at least some LM78-like
1084 chips. But only if we read 'undefined' registers. */
1085 i = inb_p(address + 1);
1086 if (inb_p(address + 2) != i
1087 || inb_p(address + 3) != i
1088 || inb_p(address + 7) != i) {
1089 dev_dbg(&adapter->dev, "Detection of w83781d "
1090 "chip failed at step 1\n");
1094 #undef REALLY_SLOW_IO
1096 /* Let's just hope nothing breaks here */
1097 i = inb_p(address + 5) & 0x7f;
1098 outb_p(~i & 0x7f, address + 5);
1099 val2 = inb_p(address + 5) & 0x7f;
1100 if (val2 != (~i & 0x7f)) {
1101 outb_p(i, address + 5);
1102 dev_dbg(&adapter->dev, "Detection of w83781d "
1103 "chip failed at step 2 (0x%x != "
1104 "0x%x at 0x%x)\n", val2, ~i & 0x7f,
1112 /* OK. For now, we presume we have a valid client. We now create the
1113 client structure, even though we cannot fill it completely yet.
1114 But it allows us to access w83781d_{read,write}_value. */
1116 if (!(data = kmalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1120 memset(data, 0, sizeof(struct w83781d_data));
1122 new_client = &data->client;
1123 i2c_set_clientdata(new_client, data);
1124 new_client->addr = address;
1125 init_MUTEX(&data->lock);
1126 new_client->adapter = adapter;
1127 new_client->driver = &w83781d_driver;
1128 new_client->flags = 0;
1130 /* Now, we do the remaining detection. */
1132 /* The w8378?d may be stuck in some other bank than bank 0. This may
1133 make reading other information impossible. Specify a force=... or
1134 force_*=... parameter, and the Winbond will be reset to the right
1137 if (w83781d_read_value(new_client, W83781D_REG_CONFIG) & 0x80) {
1138 dev_dbg(&new_client->dev, "Detection failed at step "
1143 val1 = w83781d_read_value(new_client, W83781D_REG_BANK);
1144 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1145 /* Check for Winbond or Asus ID if in bank 0 */
1146 if ((!(val1 & 0x07)) &&
1147 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1148 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1149 dev_dbg(&new_client->dev, "Detection failed at step "
1154 /* If Winbond SMBus, check address at 0x48.
1155 Asus doesn't support, except for as99127f rev.2 */
1156 if ((!is_isa) && (((!(val1 & 0x80)) && (val2 == 0xa3)) ||
1157 ((val1 & 0x80) && (val2 == 0x5c)))) {
1158 if (w83781d_read_value
1159 (new_client, W83781D_REG_I2C_ADDR) != address) {
1160 dev_dbg(&new_client->dev, "Detection failed "
1168 /* We have either had a force parameter, or we have already detected the
1169 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1170 w83781d_write_value(new_client, W83781D_REG_BANK,
1171 (w83781d_read_value(new_client,
1172 W83781D_REG_BANK) & 0x78) |
1175 /* Determine the chip type. */
1178 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1181 else if (val2 == 0x12)
1184 dev_dbg(&new_client->dev, "Chip was made by neither "
1185 "Winbond nor Asus?\n");
1190 val1 = w83781d_read_value(new_client, W83781D_REG_WCHIPID);
1191 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1193 else if (val1 == 0x30 && vendid == winbond)
1195 else if (val1 == 0x40 && vendid == winbond && !is_isa
1198 else if ((val1 == 0x21 || val1 == 0x90) && vendid == winbond)
1200 else if (val1 == 0x31 && !is_isa && address >= 0x28)
1202 else if (val1 == 0x60 && vendid == winbond && is_isa)
1206 dev_warn(&new_client->dev, "Ignoring 'force' "
1207 "parameter for unknown chip at "
1208 "adapter %d, address 0x%02x\n",
1209 i2c_adapter_id(adapter), address);
1215 if (kind == w83781d) {
1216 client_name = "w83781d";
1217 } else if (kind == w83782d) {
1218 client_name = "w83782d";
1219 } else if (kind == w83783s) {
1220 client_name = "w83783s";
1221 } else if (kind == w83627hf) {
1223 client_name = "w83627thf";
1225 client_name = "w83627hf";
1226 } else if (kind == as99127f) {
1227 client_name = "as99127f";
1228 } else if (kind == w83697hf) {
1229 client_name = "w83697hf";
1232 /* Fill in the remaining client fields and put into the global list */
1233 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1237 init_MUTEX(&data->update_lock);
1239 /* Tell the I2C layer a new client has arrived */
1240 if ((err = i2c_attach_client(new_client)))
1243 /* attach secondary i2c lm75-like clients */
1245 if ((err = w83781d_detect_subclients(adapter, address,
1249 data->lm75[0] = NULL;
1250 data->lm75[1] = NULL;
1253 /* Initialize the chip */
1254 w83781d_init_client(new_client);
1256 /* A few vars need to be filled upon startup */
1257 for (i = 1; i <= 3; i++) {
1258 data->fan_min[i - 1] = w83781d_read_value(new_client,
1259 W83781D_REG_FAN_MIN(i));
1261 if (kind != w83781d && kind != as99127f)
1262 for (i = 0; i < 4; i++)
1263 data->pwmenable[i] = 1;
1265 /* Register sysfs hooks */
1266 device_create_file_in(new_client, 0);
1267 if (kind != w83783s && kind != w83697hf)
1268 device_create_file_in(new_client, 1);
1269 device_create_file_in(new_client, 2);
1270 device_create_file_in(new_client, 3);
1271 device_create_file_in(new_client, 4);
1272 device_create_file_in(new_client, 5);
1273 device_create_file_in(new_client, 6);
1274 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1275 device_create_file_in(new_client, 7);
1276 device_create_file_in(new_client, 8);
1279 device_create_file_fan(new_client, 1);
1280 device_create_file_fan(new_client, 2);
1281 if (kind != w83697hf)
1282 device_create_file_fan(new_client, 3);
1284 device_create_file_temp(new_client, 1);
1285 device_create_file_temp(new_client, 2);
1286 if (kind != w83783s && kind != w83697hf)
1287 device_create_file_temp(new_client, 3);
1289 if (kind != w83697hf)
1290 device_create_file_vid(new_client);
1292 if (kind != w83697hf)
1293 device_create_file_vrm(new_client);
1295 device_create_file_fan_div(new_client, 1);
1296 device_create_file_fan_div(new_client, 2);
1297 if (kind != w83697hf)
1298 device_create_file_fan_div(new_client, 3);
1300 device_create_file_alarms(new_client);
1302 device_create_file_beep(new_client);
1304 if (kind != w83781d && kind != as99127f) {
1305 device_create_file_pwm(new_client, 1);
1306 device_create_file_pwm(new_client, 2);
1307 device_create_file_pwmenable(new_client, 2);
1309 if (kind == w83782d && !is_isa) {
1310 device_create_file_pwm(new_client, 3);
1311 device_create_file_pwm(new_client, 4);
1314 if (kind != as99127f && kind != w83781d) {
1315 device_create_file_sensor(new_client, 1);
1316 device_create_file_sensor(new_client, 2);
1317 if (kind != w83783s && kind != w83697hf)
1318 device_create_file_sensor(new_client, 3);
1321 if (kind == w83781d) {
1322 device_create_file_rt(new_client, 1);
1323 device_create_file_rt(new_client, 2);
1324 device_create_file_rt(new_client, 3);
1331 i2c_detach_client(new_client);
1336 release_region(address, W83781D_EXTENT);
1342 w83781d_detach_client(struct i2c_client *client)
1346 if (i2c_is_isa_client(client))
1347 release_region(client->addr, W83781D_EXTENT);
1349 if ((err = i2c_detach_client(client))) {
1350 dev_err(&client->dev,
1351 "Client deregistration failed, client not detached.\n");
1355 if (i2c_get_clientdata(client)==NULL) {
1360 kfree(i2c_get_clientdata(client));
1366 /* The SMBus locks itself, usually, but nothing may access the Winbond between
1367 bank switches. ISA access must always be locked explicitly!
1368 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1369 would slow down the W83781D access and should not be necessary.
1370 There are some ugly typecasts here, but the good news is - they should
1371 nowhere else be necessary! */
1373 w83781d_read_value(struct i2c_client *client, u16 reg)
1375 struct w83781d_data *data = i2c_get_clientdata(client);
1376 int res, word_sized, bank;
1377 struct i2c_client *cl;
1380 if (i2c_is_isa_client(client)) {
1381 word_sized = (((reg & 0xff00) == 0x100)
1382 || ((reg & 0xff00) == 0x200))
1383 && (((reg & 0x00ff) == 0x50)
1384 || ((reg & 0x00ff) == 0x53)
1385 || ((reg & 0x00ff) == 0x55));
1387 outb_p(W83781D_REG_BANK,
1388 client->addr + W83781D_ADDR_REG_OFFSET);
1390 client->addr + W83781D_DATA_REG_OFFSET);
1392 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1393 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1395 outb_p((reg & 0xff) + 1,
1396 client->addr + W83781D_ADDR_REG_OFFSET);
1398 (res << 8) + inb_p(client->addr +
1399 W83781D_DATA_REG_OFFSET);
1402 outb_p(W83781D_REG_BANK,
1403 client->addr + W83781D_ADDR_REG_OFFSET);
1404 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1407 bank = (reg >> 8) & 0x0f;
1410 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1412 if (bank == 0 || bank > 2) {
1413 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1415 /* switch to subclient */
1416 cl = data->lm75[bank - 1];
1417 /* convert from ISA to LM75 I2C addresses */
1418 switch (reg & 0xff) {
1419 case 0x50: /* TEMP */
1420 res = swab16(i2c_smbus_read_word_data(cl, 0));
1422 case 0x52: /* CONFIG */
1423 res = i2c_smbus_read_byte_data(cl, 1);
1425 case 0x53: /* HYST */
1426 res = swab16(i2c_smbus_read_word_data(cl, 2));
1428 case 0x55: /* OVER */
1430 res = swab16(i2c_smbus_read_word_data(cl, 3));
1435 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1442 w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1444 struct w83781d_data *data = i2c_get_clientdata(client);
1445 int word_sized, bank;
1446 struct i2c_client *cl;
1449 if (i2c_is_isa_client(client)) {
1450 word_sized = (((reg & 0xff00) == 0x100)
1451 || ((reg & 0xff00) == 0x200))
1452 && (((reg & 0x00ff) == 0x53)
1453 || ((reg & 0x00ff) == 0x55));
1455 outb_p(W83781D_REG_BANK,
1456 client->addr + W83781D_ADDR_REG_OFFSET);
1458 client->addr + W83781D_DATA_REG_OFFSET);
1460 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1463 client->addr + W83781D_DATA_REG_OFFSET);
1464 outb_p((reg & 0xff) + 1,
1465 client->addr + W83781D_ADDR_REG_OFFSET);
1467 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1469 outb_p(W83781D_REG_BANK,
1470 client->addr + W83781D_ADDR_REG_OFFSET);
1471 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1474 bank = (reg >> 8) & 0x0f;
1477 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1479 if (bank == 0 || bank > 2) {
1480 i2c_smbus_write_byte_data(client, reg & 0xff,
1483 /* switch to subclient */
1484 cl = data->lm75[bank - 1];
1485 /* convert from ISA to LM75 I2C addresses */
1486 switch (reg & 0xff) {
1487 case 0x52: /* CONFIG */
1488 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1490 case 0x53: /* HYST */
1491 i2c_smbus_write_word_data(cl, 2, swab16(value));
1493 case 0x55: /* OVER */
1494 i2c_smbus_write_word_data(cl, 3, swab16(value));
1499 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1505 /* Called when we have found a new W83781D. It should set limits, etc. */
1507 w83781d_init_client(struct i2c_client *client)
1509 struct w83781d_data *data = i2c_get_clientdata(client);
1511 int type = data->type;
1514 if (init && type != as99127f) { /* this resets registers we don't have
1515 documentation for on the as99127f */
1516 /* save these registers */
1517 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1518 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1519 /* Reset all except Watchdog values and last conversion values
1520 This sets fan-divs to 2, among others */
1521 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1522 /* Restore the registers and disable power-on abnormal beep.
1523 This saves FAN 1/2/3 input/output values set by BIOS. */
1524 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1525 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1526 /* Disable master beep-enable (reset turns it on).
1527 Individual beep_mask should be reset to off but for some reason
1528 disabling this bit helps some people not get beeped */
1529 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1532 data->vrm = i2c_which_vrm();
1534 if ((type != w83781d) && (type != as99127f)) {
1535 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1536 for (i = 1; i <= 3; i++) {
1537 if (!(tmp & BIT_SCFG1[i - 1])) {
1538 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1540 if (w83781d_read_value
1542 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1543 data->sens[i - 1] = 1;
1545 data->sens[i - 1] = 2;
1547 if ((type == w83783s || type == w83697hf) && (i == 2))
1553 Fill up the RT Tables.
1554 We assume that they are 32 bytes long, in order for temp 1-3.
1555 Data sheet documentation is sparse.
1556 We also assume that it is only for the 781D although I suspect
1557 that the others support it as well....
1560 if (init && type == w83781d) {
1563 Auto-indexing doesn't seem to work...
1564 w83781d_write_value(client,W83781D_REG_RT_IDX,0);
1566 for (i = 0; i < 3; i++) {
1568 for (j = 0; j < 32; j++) {
1569 w83781d_write_value(client,
1570 W83781D_REG_RT_IDX, k++);
1572 w83781d_read_value(client,
1573 W83781D_REG_RT_VAL);
1577 #endif /* W83781D_RT */
1580 if (type != w83783s && type != w83697hf) {
1581 w83781d_write_value(client, W83781D_REG_TEMP3_CONFIG,
1584 if (type != w83781d) {
1585 /* enable comparator mode for temp2 and temp3 so
1586 alarm indication will work correctly */
1587 i = w83781d_read_value(client, W83781D_REG_IRQ);
1589 w83781d_write_value(client, W83781D_REG_IRQ,
1594 /* Start monitoring */
1595 w83781d_write_value(client, W83781D_REG_CONFIG,
1596 (w83781d_read_value(client,
1597 W83781D_REG_CONFIG) & 0xf7)
1601 static struct w83781d_data *w83781d_update_device(struct device *dev)
1603 struct i2c_client *client = to_i2c_client(dev);
1604 struct w83781d_data *data = i2c_get_clientdata(client);
1607 down(&data->update_lock);
1610 (jiffies - data->last_updated, (unsigned long) (HZ + HZ / 2))
1611 || time_before(jiffies, data->last_updated) || !data->valid) {
1612 dev_dbg(dev, "Starting device update\n");
1614 for (i = 0; i <= 8; i++) {
1615 if ((data->type == w83783s || data->type == w83697hf)
1617 continue; /* 783S has no in1 */
1619 w83781d_read_value(client, W83781D_REG_IN(i));
1621 w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1623 w83781d_read_value(client, W83781D_REG_IN_MAX(i));
1624 if ((data->type != w83782d) && (data->type != w83697hf)
1625 && (data->type != w83627hf) && (i == 6))
1628 for (i = 1; i <= 3; i++) {
1630 w83781d_read_value(client, W83781D_REG_FAN(i));
1631 data->fan_min[i - 1] =
1632 w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1634 if (data->type != w83781d && data->type != as99127f) {
1635 for (i = 1; i <= 4; i++) {
1637 w83781d_read_value(client,
1638 W83781D_REG_PWM(i));
1639 if ((data->type != w83782d
1640 || i2c_is_isa_client(client))
1644 /* Only PWM2 can be disabled */
1645 data->pwmenable[1] = (w83781d_read_value(client,
1646 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1649 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1651 w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1652 data->temp_max_hyst =
1653 w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1655 w83781d_read_value(client, W83781D_REG_TEMP(2));
1656 data->temp_max_add[0] =
1657 w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1658 data->temp_max_hyst_add[0] =
1659 w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
1660 if (data->type != w83783s && data->type != w83697hf) {
1662 w83781d_read_value(client, W83781D_REG_TEMP(3));
1663 data->temp_max_add[1] =
1664 w83781d_read_value(client,
1665 W83781D_REG_TEMP_OVER(3));
1666 data->temp_max_hyst_add[1] =
1667 w83781d_read_value(client,
1668 W83781D_REG_TEMP_HYST(3));
1670 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
1671 if (data->type != w83697hf) {
1672 data->vid = i & 0x0f;
1674 (w83781d_read_value(client, W83781D_REG_CHIPID) &
1678 data->fan_div[0] = (i >> 4) & 0x03;
1679 data->fan_div[1] = (i >> 6) & 0x03;
1680 if (data->type != w83697hf) {
1681 data->fan_div[2] = (w83781d_read_value(client,
1685 if ((data->type != w83781d) && (data->type != as99127f)) {
1686 i = w83781d_read_value(client, W83781D_REG_VBAT);
1687 data->fan_div[0] |= (i >> 3) & 0x04;
1688 data->fan_div[1] |= (i >> 4) & 0x04;
1689 if (data->type != w83697hf)
1690 data->fan_div[2] |= (i >> 5) & 0x04;
1693 w83781d_read_value(client,
1694 W83781D_REG_ALARM1) +
1695 (w83781d_read_value(client, W83781D_REG_ALARM2) << 8);
1696 if ((data->type == w83782d) || (data->type == w83627hf)) {
1698 w83781d_read_value(client,
1699 W83781D_REG_ALARM3) << 16;
1701 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1702 data->beep_enable = i >> 7;
1703 data->beep_mask = ((i & 0x7f) << 8) +
1704 w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1705 if ((data->type != w83781d) && (data->type != as99127f)) {
1707 w83781d_read_value(client,
1708 W83781D_REG_BEEP_INTS3) << 16;
1710 data->last_updated = jiffies;
1714 up(&data->update_lock);
1720 sensors_w83781d_init(void)
1722 return i2c_add_driver(&w83781d_driver);
1726 sensors_w83781d_exit(void)
1728 i2c_del_driver(&w83781d_driver);
1731 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1732 "Philip Edelbrock <phil@netroedge.com>, "
1733 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1734 MODULE_DESCRIPTION("W83781D driver");
1735 MODULE_LICENSE("GPL");
1737 module_init(sensors_w83781d_init);
1738 module_exit(sensors_w83781d_exit);