blob: c83ae769e3622231ed3eea289d3ca1ea065e5376 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 and Mark Studebaker <mdsxyz123@yahoo.com>
7
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.
12
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.
17
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.
21*/
22
23/*
24 Supports following chips:
25
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
32 w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34*/
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/jiffies.h>
40#include <linux/i2c.h>
41#include <linux/i2c-sensor.h>
42#include <linux/i2c-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
44#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/io.h>
46#include "lm75.h"
47
48/* Addresses to scan */
49static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
50 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
51 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
52static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
53
54/* Insmod parameters */
Jean Delvare7c7a5302005-06-16 19:24:14 +020055SENSORS_INSMOD_5(w83781d, w83782d, w83783s, w83627hf, as99127f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
57 "{bus, clientaddr, subclientaddr1, subclientaddr2}");
58
59static int init = 1;
60module_param(init, bool, 0);
61MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
62
63/* Constants specified below */
64
65/* Length of ISA address segment */
66#define W83781D_EXTENT 8
67
68/* Where are the ISA address/data registers relative to the base address */
69#define W83781D_ADDR_REG_OFFSET 5
70#define W83781D_DATA_REG_OFFSET 6
71
72/* The W83781D registers */
73/* The W83782D registers for nr=7,8 are in bank 5 */
74#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
75 (0x554 + (((nr) - 7) * 2)))
76#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
77 (0x555 + (((nr) - 7) * 2)))
78#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
79 (0x550 + (nr) - 7))
80
81#define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
82#define W83781D_REG_FAN(nr) (0x27 + (nr))
83
84#define W83781D_REG_BANK 0x4E
85#define W83781D_REG_TEMP2_CONFIG 0x152
86#define W83781D_REG_TEMP3_CONFIG 0x252
87#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
88 ((nr == 2) ? (0x0150) : \
89 (0x27)))
90#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
91 ((nr == 2) ? (0x153) : \
92 (0x3A)))
93#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
94 ((nr == 2) ? (0x155) : \
95 (0x39)))
96
97#define W83781D_REG_CONFIG 0x40
98#define W83781D_REG_ALARM1 0x41
99#define W83781D_REG_ALARM2 0x42
100#define W83781D_REG_ALARM3 0x450 /* not on W83781D */
101
102#define W83781D_REG_IRQ 0x4C
103#define W83781D_REG_BEEP_CONFIG 0x4D
104#define W83781D_REG_BEEP_INTS1 0x56
105#define W83781D_REG_BEEP_INTS2 0x57
106#define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
107
108#define W83781D_REG_VID_FANDIV 0x47
109
110#define W83781D_REG_CHIPID 0x49
111#define W83781D_REG_WCHIPID 0x58
112#define W83781D_REG_CHIPMAN 0x4F
113#define W83781D_REG_PIN 0x4B
114
115/* 782D/783S only */
116#define W83781D_REG_VBAT 0x5D
117
118/* PWM 782D (1-4) and 783S (1-2) only */
119#define W83781D_REG_PWM1 0x5B /* 782d and 783s/627hf datasheets disagree */
120 /* on which is which; */
121#define W83781D_REG_PWM2 0x5A /* We follow the 782d convention here, */
122 /* However 782d is probably wrong. */
123#define W83781D_REG_PWM3 0x5E
124#define W83781D_REG_PWM4 0x5F
125#define W83781D_REG_PWMCLK12 0x5C
126#define W83781D_REG_PWMCLK34 0x45C
127static const u8 regpwm[] = { W83781D_REG_PWM1, W83781D_REG_PWM2,
128 W83781D_REG_PWM3, W83781D_REG_PWM4
129};
130
131#define W83781D_REG_PWM(nr) (regpwm[(nr) - 1])
132
133#define W83781D_REG_I2C_ADDR 0x48
134#define W83781D_REG_I2C_SUBADDR 0x4A
135
136/* The following are undocumented in the data sheets however we
137 received the information in an email from Winbond tech support */
138/* Sensor selection - not on 781d */
139#define W83781D_REG_SCFG1 0x5D
140static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
141
142#define W83781D_REG_SCFG2 0x59
143static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
144
145#define W83781D_DEFAULT_BETA 3435
146
147/* RT Table registers */
148#define W83781D_REG_RT_IDX 0x50
149#define W83781D_REG_RT_VAL 0x51
150
151/* Conversions. Rounding and limit checking is only done on the TO_REG
152 variants. Note that you should be a bit careful with which arguments
153 these macros are called: arguments may be evaluated more than once.
154 Fixing this is just not worth it. */
155#define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
156#define IN_FROM_REG(val) (((val) * 16) / 10)
157
158static inline u8
159FAN_TO_REG(long rpm, int div)
160{
161 if (rpm == 0)
162 return 255;
163 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
164 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
165}
166
167#define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
168 ((val) == 255 ? 0 : \
169 1350000 / ((val) * (div))))
170
171#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
172 : (val)) / 1000, 0, 0xff))
173#define TEMP_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define PWM_FROM_REG(val) (val)
176#define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
177#define BEEP_MASK_FROM_REG(val,type) ((type) == as99127f ? \
178 (val) ^ 0x7fff : (val))
179#define BEEP_MASK_TO_REG(val,type) ((type) == as99127f ? \
180 (~(val)) & 0x7fff : (val) & 0xffffff)
181
182#define BEEP_ENABLE_TO_REG(val) ((val) ? 1 : 0)
183#define BEEP_ENABLE_FROM_REG(val) ((val) ? 1 : 0)
184
185#define DIV_FROM_REG(val) (1 << (val))
186
187static inline u8
188DIV_TO_REG(long val, enum chips type)
189{
190 int i;
191 val = SENSORS_LIMIT(val, 1,
192 ((type == w83781d
193 || type == as99127f) ? 8 : 128)) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000194 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (val == 0)
196 break;
197 val >>= 1;
198 }
199 return ((u8) i);
200}
201
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
211 for this yet. */
212
213/* This module may seem overly long and complicated. In fact, it is not so
214 bad. Quite a lot of bookkeeping is done. A real driver can often cut
215 some corners. */
216
217/* For each registered W83781D, we need to keep some data in memory. That
218 data is pointed to by w83781d_list[NR]->data. The structure itself is
219 dynamically allocated, at the same time when a new w83781d client is
220 allocated. */
221struct w83781d_data {
222 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400223 struct class_device *class_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct semaphore lock;
225 enum chips type;
226
227 struct semaphore update_lock;
228 char valid; /* !=0 if following fields are valid */
229 unsigned long last_updated; /* In jiffies */
230
231 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
232 /* array of 2 pointers to subclients */
233
234 u8 in[9]; /* Register value - 8 & 9 for 782D only */
235 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
236 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
237 u8 fan[3]; /* Register value */
238 u8 fan_min[3]; /* Register value */
239 u8 temp;
240 u8 temp_max; /* Register value */
241 u8 temp_max_hyst; /* Register value */
242 u16 temp_add[2]; /* Register value */
243 u16 temp_max_add[2]; /* Register value */
244 u16 temp_max_hyst_add[2]; /* Register value */
245 u8 fan_div[3]; /* Register encoding, shifted right */
246 u8 vid; /* Register encoding, combined */
247 u32 alarms; /* Register encoding, combined */
248 u32 beep_mask; /* Register encoding, combined */
249 u8 beep_enable; /* Boolean */
250 u8 pwm[4]; /* Register value */
251 u8 pwmenable[4]; /* Boolean */
252 u16 sens[3]; /* 782D/783S only.
253 1 = pentium diode; 2 = 3904 diode;
254 3000-5000 = thermistor beta.
255 Default = 3435.
256 Other Betas unimplemented */
257 u8 vrm;
258};
259
260static int w83781d_attach_adapter(struct i2c_adapter *adapter);
261static int w83781d_detect(struct i2c_adapter *adapter, int address, int kind);
262static int w83781d_detach_client(struct i2c_client *client);
263
264static int w83781d_read_value(struct i2c_client *client, u16 register);
265static int w83781d_write_value(struct i2c_client *client, u16 register,
266 u16 value);
267static struct w83781d_data *w83781d_update_device(struct device *dev);
268static void w83781d_init_client(struct i2c_client *client);
269
270static struct i2c_driver w83781d_driver = {
271 .owner = THIS_MODULE,
272 .name = "w83781d",
273 .id = I2C_DRIVERID_W83781D,
274 .flags = I2C_DF_NOTIFY,
275 .attach_adapter = w83781d_attach_adapter,
276 .detach_client = w83781d_detach_client,
277};
278
279/* following are the sysfs callback functions */
280#define show_in_reg(reg) \
281static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
282{ \
283 struct w83781d_data *data = w83781d_update_device(dev); \
284 return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr] * 10)); \
285}
286show_in_reg(in);
287show_in_reg(in_min);
288show_in_reg(in_max);
289
290#define store_in_reg(REG, reg) \
291static ssize_t store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
292{ \
293 struct i2c_client *client = to_i2c_client(dev); \
294 struct w83781d_data *data = i2c_get_clientdata(client); \
295 u32 val; \
296 \
297 val = simple_strtoul(buf, NULL, 10) / 10; \
298 \
299 down(&data->update_lock); \
300 data->in_##reg[nr] = IN_TO_REG(val); \
301 w83781d_write_value(client, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
302 \
303 up(&data->update_lock); \
304 return count; \
305}
306store_in_reg(MIN, min);
307store_in_reg(MAX, max);
308
309#define sysfs_in_offset(offset) \
310static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -0400311show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{ \
313 return show_in(dev, buf, offset); \
314} \
315static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
316
317#define sysfs_in_reg_offset(reg, offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400318static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{ \
320 return show_in_##reg (dev, buf, offset); \
321} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400322static ssize_t store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{ \
324 return store_in_##reg (dev, buf, count, offset); \
325} \
326static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_in_##reg##offset, store_regs_in_##reg##offset);
327
328#define sysfs_in_offsets(offset) \
329sysfs_in_offset(offset); \
330sysfs_in_reg_offset(min, offset); \
331sysfs_in_reg_offset(max, offset);
332
333sysfs_in_offsets(0);
334sysfs_in_offsets(1);
335sysfs_in_offsets(2);
336sysfs_in_offsets(3);
337sysfs_in_offsets(4);
338sysfs_in_offsets(5);
339sysfs_in_offsets(6);
340sysfs_in_offsets(7);
341sysfs_in_offsets(8);
342
343#define device_create_file_in(client, offset) \
344do { \
345device_create_file(&client->dev, &dev_attr_in##offset##_input); \
346device_create_file(&client->dev, &dev_attr_in##offset##_min); \
347device_create_file(&client->dev, &dev_attr_in##offset##_max); \
348} while (0)
349
350#define show_fan_reg(reg) \
351static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
352{ \
353 struct w83781d_data *data = w83781d_update_device(dev); \
354 return sprintf(buf,"%ld\n", \
355 FAN_FROM_REG(data->reg[nr-1], (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
356}
357show_fan_reg(fan);
358show_fan_reg(fan_min);
359
360static ssize_t
361store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
362{
363 struct i2c_client *client = to_i2c_client(dev);
364 struct w83781d_data *data = i2c_get_clientdata(client);
365 u32 val;
366
367 val = simple_strtoul(buf, NULL, 10);
368
369 down(&data->update_lock);
370 data->fan_min[nr - 1] =
371 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
372 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr),
373 data->fan_min[nr - 1]);
374
375 up(&data->update_lock);
376 return count;
377}
378
379#define sysfs_fan_offset(offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400380static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{ \
382 return show_fan(dev, buf, offset); \
383} \
384static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
385
386#define sysfs_fan_min_offset(offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400387static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{ \
389 return show_fan_min(dev, buf, offset); \
390} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400391static ssize_t store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{ \
393 return store_fan_min(dev, buf, count, offset); \
394} \
395static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, show_regs_fan_min##offset, store_regs_fan_min##offset);
396
397sysfs_fan_offset(1);
398sysfs_fan_min_offset(1);
399sysfs_fan_offset(2);
400sysfs_fan_min_offset(2);
401sysfs_fan_offset(3);
402sysfs_fan_min_offset(3);
403
404#define device_create_file_fan(client, offset) \
405do { \
406device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
407device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
408} while (0)
409
410#define show_temp_reg(reg) \
411static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
412{ \
413 struct w83781d_data *data = w83781d_update_device(dev); \
414 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
415 return sprintf(buf,"%d\n", \
416 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
417 } else { /* TEMP1 */ \
418 return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
419 } \
420}
421show_temp_reg(temp);
422show_temp_reg(temp_max);
423show_temp_reg(temp_max_hyst);
424
425#define store_temp_reg(REG, reg) \
426static ssize_t store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
427{ \
428 struct i2c_client *client = to_i2c_client(dev); \
429 struct w83781d_data *data = i2c_get_clientdata(client); \
430 s32 val; \
431 \
432 val = simple_strtol(buf, NULL, 10); \
433 \
434 down(&data->update_lock); \
435 \
436 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
437 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
438 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
439 data->temp_##reg##_add[nr-2]); \
440 } else { /* TEMP1 */ \
441 data->temp_##reg = TEMP_TO_REG(val); \
442 w83781d_write_value(client, W83781D_REG_TEMP_##REG(nr), \
443 data->temp_##reg); \
444 } \
445 \
446 up(&data->update_lock); \
447 return count; \
448}
449store_temp_reg(OVER, max);
450store_temp_reg(HYST, max_hyst);
451
452#define sysfs_temp_offset(offset) \
453static ssize_t \
Yani Ioannoue404e272005-05-17 06:42:58 -0400454show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{ \
456 return show_temp(dev, buf, offset); \
457} \
458static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
459
460#define sysfs_temp_reg_offset(reg, offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400461static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{ \
463 return show_temp_##reg (dev, buf, offset); \
464} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400465static ssize_t store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{ \
467 return store_temp_##reg (dev, buf, count, offset); \
468} \
469static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
470
471#define sysfs_temp_offsets(offset) \
472sysfs_temp_offset(offset); \
473sysfs_temp_reg_offset(max, offset); \
474sysfs_temp_reg_offset(max_hyst, offset);
475
476sysfs_temp_offsets(1);
477sysfs_temp_offsets(2);
478sysfs_temp_offsets(3);
479
480#define device_create_file_temp(client, offset) \
481do { \
482device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
483device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
484device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
485} while (0)
486
487static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400488show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct w83781d_data *data = w83781d_update_device(dev);
491 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
492}
493
494static
495DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
496#define device_create_file_vid(client) \
497device_create_file(&client->dev, &dev_attr_cpu0_vid);
498static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400499show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct w83781d_data *data = w83781d_update_device(dev);
502 return sprintf(buf, "%ld\n", (long) data->vrm);
503}
504
505static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400506store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct i2c_client *client = to_i2c_client(dev);
509 struct w83781d_data *data = i2c_get_clientdata(client);
510 u32 val;
511
512 val = simple_strtoul(buf, NULL, 10);
513 data->vrm = val;
514
515 return count;
516}
517
518static
519DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
520#define device_create_file_vrm(client) \
521device_create_file(&client->dev, &dev_attr_vrm);
522static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400523show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200526 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static
530DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
531#define device_create_file_alarms(client) \
532device_create_file(&client->dev, &dev_attr_alarms);
Yani Ioannoue404e272005-05-17 06:42:58 -0400533static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct w83781d_data *data = w83781d_update_device(dev);
536 return sprintf(buf, "%ld\n",
537 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
538}
Yani Ioannoue404e272005-05-17 06:42:58 -0400539static ssize_t show_beep_enable (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct w83781d_data *data = w83781d_update_device(dev);
542 return sprintf(buf, "%ld\n",
543 (long)BEEP_ENABLE_FROM_REG(data->beep_enable));
544}
545
546#define BEEP_ENABLE 0 /* Store beep_enable */
547#define BEEP_MASK 1 /* Store beep_mask */
548
549static ssize_t
550store_beep_reg(struct device *dev, const char *buf, size_t count,
551 int update_mask)
552{
553 struct i2c_client *client = to_i2c_client(dev);
554 struct w83781d_data *data = i2c_get_clientdata(client);
555 u32 val, val2;
556
557 val = simple_strtoul(buf, NULL, 10);
558
559 down(&data->update_lock);
560
561 if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
562 data->beep_mask = BEEP_MASK_TO_REG(val, data->type);
563 w83781d_write_value(client, W83781D_REG_BEEP_INTS1,
564 data->beep_mask & 0xff);
565
566 if ((data->type != w83781d) && (data->type != as99127f)) {
567 w83781d_write_value(client, W83781D_REG_BEEP_INTS3,
568 ((data->beep_mask) >> 16) & 0xff);
569 }
570
571 val2 = (data->beep_mask >> 8) & 0x7f;
572 } else { /* We are storing beep_enable */
573 val2 = w83781d_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
574 data->beep_enable = BEEP_ENABLE_TO_REG(val);
575 }
576
577 w83781d_write_value(client, W83781D_REG_BEEP_INTS2,
578 val2 | data->beep_enable << 7);
579
580 up(&data->update_lock);
581 return count;
582}
583
584#define sysfs_beep(REG, reg) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400585static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{ \
Yani Ioannoue404e272005-05-17 06:42:58 -0400587 return show_beep_##reg(dev, attr, buf); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400589static ssize_t store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{ \
591 return store_beep_reg(dev, buf, count, BEEP_##REG); \
592} \
593static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, show_regs_beep_##reg, store_regs_beep_##reg);
594
595sysfs_beep(ENABLE, enable);
596sysfs_beep(MASK, mask);
597
598#define device_create_file_beep(client) \
599do { \
600device_create_file(&client->dev, &dev_attr_beep_enable); \
601device_create_file(&client->dev, &dev_attr_beep_mask); \
602} while (0)
603
604static ssize_t
605show_fan_div_reg(struct device *dev, char *buf, int nr)
606{
607 struct w83781d_data *data = w83781d_update_device(dev);
608 return sprintf(buf, "%ld\n",
609 (long) DIV_FROM_REG(data->fan_div[nr - 1]));
610}
611
612/* Note: we save and restore the fan minimum here, because its value is
613 determined in part by the fan divisor. This follows the principle of
614 least suprise; the user doesn't expect the fan minimum to change just
615 because the divisor changed. */
616static ssize_t
617store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
618{
619 struct i2c_client *client = to_i2c_client(dev);
620 struct w83781d_data *data = i2c_get_clientdata(client);
621 unsigned long min;
622 u8 reg;
623 unsigned long val = simple_strtoul(buf, NULL, 10);
624
625 down(&data->update_lock);
626
627 /* Save fan_min */
628 min = FAN_FROM_REG(data->fan_min[nr],
629 DIV_FROM_REG(data->fan_div[nr]));
630
631 data->fan_div[nr] = DIV_TO_REG(val, data->type);
632
633 reg = (w83781d_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
634 & (nr==0 ? 0xcf : 0x3f))
635 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
636 w83781d_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
637
638 /* w83781d and as99127f don't have extended divisor bits */
639 if (data->type != w83781d && data->type != as99127f) {
640 reg = (w83781d_read_value(client, W83781D_REG_VBAT)
641 & ~(1 << (5 + nr)))
642 | ((data->fan_div[nr] & 0x04) << (3 + nr));
643 w83781d_write_value(client, W83781D_REG_VBAT, reg);
644 }
645
646 /* Restore fan_min */
647 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
648 w83781d_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
649
650 up(&data->update_lock);
651 return count;
652}
653
654#define sysfs_fan_div(offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400655static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{ \
657 return show_fan_div_reg(dev, buf, offset); \
658} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400659static ssize_t store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{ \
661 return store_fan_div_reg(dev, buf, count, offset - 1); \
662} \
663static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, show_regs_fan_div_##offset, store_regs_fan_div_##offset);
664
665sysfs_fan_div(1);
666sysfs_fan_div(2);
667sysfs_fan_div(3);
668
669#define device_create_file_fan_div(client, offset) \
670do { \
671device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
672} while (0)
673
674static ssize_t
675show_pwm_reg(struct device *dev, char *buf, int nr)
676{
677 struct w83781d_data *data = w83781d_update_device(dev);
678 return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr - 1]));
679}
680
681static ssize_t
682show_pwmenable_reg(struct device *dev, char *buf, int nr)
683{
684 struct w83781d_data *data = w83781d_update_device(dev);
685 return sprintf(buf, "%ld\n", (long) data->pwmenable[nr - 1]);
686}
687
688static ssize_t
689store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
690{
691 struct i2c_client *client = to_i2c_client(dev);
692 struct w83781d_data *data = i2c_get_clientdata(client);
693 u32 val;
694
695 val = simple_strtoul(buf, NULL, 10);
696
697 down(&data->update_lock);
698 data->pwm[nr - 1] = PWM_TO_REG(val);
699 w83781d_write_value(client, W83781D_REG_PWM(nr), data->pwm[nr - 1]);
700 up(&data->update_lock);
701 return count;
702}
703
704static ssize_t
705store_pwmenable_reg(struct device *dev, const char *buf, size_t count, int nr)
706{
707 struct i2c_client *client = to_i2c_client(dev);
708 struct w83781d_data *data = i2c_get_clientdata(client);
709 u32 val, reg;
710
711 val = simple_strtoul(buf, NULL, 10);
712
713 down(&data->update_lock);
714
715 switch (val) {
716 case 0:
717 case 1:
718 reg = w83781d_read_value(client, W83781D_REG_PWMCLK12);
719 w83781d_write_value(client, W83781D_REG_PWMCLK12,
720 (reg & 0xf7) | (val << 3));
721
722 reg = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
723 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG,
724 (reg & 0xef) | (!val << 4));
725
726 data->pwmenable[nr - 1] = val;
727 break;
728
729 default:
730 up(&data->update_lock);
731 return -EINVAL;
732 }
733
734 up(&data->update_lock);
735 return count;
736}
737
738#define sysfs_pwm(offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400739static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{ \
741 return show_pwm_reg(dev, buf, offset); \
742} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400743static ssize_t store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 const char *buf, size_t count) \
745{ \
746 return store_pwm_reg(dev, buf, count, offset); \
747} \
748static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
749 show_regs_pwm_##offset, store_regs_pwm_##offset);
750
751#define sysfs_pwmenable(offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400752static ssize_t show_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{ \
754 return show_pwmenable_reg(dev, buf, offset); \
755} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400756static ssize_t store_regs_pwmenable_##offset (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 const char *buf, size_t count) \
758{ \
759 return store_pwmenable_reg(dev, buf, count, offset); \
760} \
761static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
762 show_regs_pwmenable_##offset, store_regs_pwmenable_##offset);
763
764sysfs_pwm(1);
765sysfs_pwm(2);
766sysfs_pwmenable(2); /* only PWM2 can be enabled/disabled */
767sysfs_pwm(3);
768sysfs_pwm(4);
769
770#define device_create_file_pwm(client, offset) \
771do { \
772device_create_file(&client->dev, &dev_attr_pwm##offset); \
773} while (0)
774
775#define device_create_file_pwmenable(client, offset) \
776do { \
777device_create_file(&client->dev, &dev_attr_pwm##offset##_enable); \
778} while (0)
779
780static ssize_t
781show_sensor_reg(struct device *dev, char *buf, int nr)
782{
783 struct w83781d_data *data = w83781d_update_device(dev);
784 return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
785}
786
787static ssize_t
788store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
789{
790 struct i2c_client *client = to_i2c_client(dev);
791 struct w83781d_data *data = i2c_get_clientdata(client);
792 u32 val, tmp;
793
794 val = simple_strtoul(buf, NULL, 10);
795
796 down(&data->update_lock);
797
798 switch (val) {
799 case 1: /* PII/Celeron diode */
800 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
801 w83781d_write_value(client, W83781D_REG_SCFG1,
802 tmp | BIT_SCFG1[nr - 1]);
803 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
804 w83781d_write_value(client, W83781D_REG_SCFG2,
805 tmp | BIT_SCFG2[nr - 1]);
806 data->sens[nr - 1] = val;
807 break;
808 case 2: /* 3904 */
809 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
810 w83781d_write_value(client, W83781D_REG_SCFG1,
811 tmp | BIT_SCFG1[nr - 1]);
812 tmp = w83781d_read_value(client, W83781D_REG_SCFG2);
813 w83781d_write_value(client, W83781D_REG_SCFG2,
814 tmp & ~BIT_SCFG2[nr - 1]);
815 data->sens[nr - 1] = val;
816 break;
817 case W83781D_DEFAULT_BETA: /* thermistor */
818 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
819 w83781d_write_value(client, W83781D_REG_SCFG1,
820 tmp & ~BIT_SCFG1[nr - 1]);
821 data->sens[nr - 1] = val;
822 break;
823 default:
824 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or %d\n",
825 (long) val, W83781D_DEFAULT_BETA);
826 break;
827 }
828
829 up(&data->update_lock);
830 return count;
831}
832
833#define sysfs_sensor(offset) \
Yani Ioannoue404e272005-05-17 06:42:58 -0400834static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{ \
836 return show_sensor_reg(dev, buf, offset); \
837} \
Yani Ioannoue404e272005-05-17 06:42:58 -0400838static ssize_t store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{ \
840 return store_sensor_reg(dev, buf, count, offset); \
841} \
842static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, show_regs_sensor_##offset, store_regs_sensor_##offset);
843
844sysfs_sensor(1);
845sysfs_sensor(2);
846sysfs_sensor(3);
847
848#define device_create_file_sensor(client, offset) \
849do { \
850device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
851} while (0)
852
853/* This function is called when:
854 * w83781d_driver is inserted (when this module is loaded), for each
855 available adapter
856 * when a new adapter is inserted (and w83781d_driver is still present) */
857static int
858w83781d_attach_adapter(struct i2c_adapter *adapter)
859{
860 if (!(adapter->class & I2C_CLASS_HWMON))
861 return 0;
862 return i2c_detect(adapter, &addr_data, w83781d_detect);
863}
864
865/* Assumes that adapter is of I2C, not ISA variety.
866 * OTHERWISE DON'T CALL THIS
867 */
868static int
869w83781d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
870 struct i2c_client *new_client)
871{
872 int i, val1 = 0, id;
873 int err;
874 const char *client_name = "";
875 struct w83781d_data *data = i2c_get_clientdata(new_client);
876
877 data->lm75[0] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
878 if (!(data->lm75[0])) {
879 err = -ENOMEM;
880 goto ERROR_SC_0;
881 }
882 memset(data->lm75[0], 0x00, sizeof (struct i2c_client));
883
884 id = i2c_adapter_id(adapter);
885
886 if (force_subclients[0] == id && force_subclients[1] == address) {
887 for (i = 2; i <= 3; i++) {
888 if (force_subclients[i] < 0x48 ||
889 force_subclients[i] > 0x4f) {
890 dev_err(&new_client->dev, "Invalid subclient "
891 "address %d; must be 0x48-0x4f\n",
892 force_subclients[i]);
893 err = -EINVAL;
894 goto ERROR_SC_1;
895 }
896 }
897 w83781d_write_value(new_client, W83781D_REG_I2C_SUBADDR,
898 (force_subclients[2] & 0x07) |
899 ((force_subclients[3] & 0x07) << 4));
900 data->lm75[0]->addr = force_subclients[2];
901 } else {
902 val1 = w83781d_read_value(new_client, W83781D_REG_I2C_SUBADDR);
903 data->lm75[0]->addr = 0x48 + (val1 & 0x07);
904 }
905
906 if (kind != w83783s) {
907
908 data->lm75[1] = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
909 if (!(data->lm75[1])) {
910 err = -ENOMEM;
911 goto ERROR_SC_1;
912 }
913 memset(data->lm75[1], 0x0, sizeof(struct i2c_client));
914
915 if (force_subclients[0] == id &&
916 force_subclients[1] == address) {
917 data->lm75[1]->addr = force_subclients[3];
918 } else {
919 data->lm75[1]->addr = 0x48 + ((val1 >> 4) & 0x07);
920 }
921 if (data->lm75[0]->addr == data->lm75[1]->addr) {
922 dev_err(&new_client->dev,
923 "Duplicate addresses 0x%x for subclients.\n",
924 data->lm75[0]->addr);
925 err = -EBUSY;
926 goto ERROR_SC_2;
927 }
928 }
929
930 if (kind == w83781d)
931 client_name = "w83781d subclient";
932 else if (kind == w83782d)
933 client_name = "w83782d subclient";
934 else if (kind == w83783s)
935 client_name = "w83783s subclient";
936 else if (kind == w83627hf)
937 client_name = "w83627hf subclient";
938 else if (kind == as99127f)
939 client_name = "as99127f subclient";
940
941 for (i = 0; i <= 1; i++) {
942 /* store all data in w83781d */
943 i2c_set_clientdata(data->lm75[i], NULL);
944 data->lm75[i]->adapter = adapter;
945 data->lm75[i]->driver = &w83781d_driver;
946 data->lm75[i]->flags = 0;
947 strlcpy(data->lm75[i]->name, client_name,
948 I2C_NAME_SIZE);
949 if ((err = i2c_attach_client(data->lm75[i]))) {
950 dev_err(&new_client->dev, "Subclient %d "
951 "registration at address 0x%x "
952 "failed.\n", i, data->lm75[i]->addr);
953 if (i == 1)
954 goto ERROR_SC_3;
955 goto ERROR_SC_2;
956 }
957 if (kind == w83783s)
958 break;
959 }
960
961 return 0;
962
963/* Undo inits in case of errors */
964ERROR_SC_3:
965 i2c_detach_client(data->lm75[0]);
966ERROR_SC_2:
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400967 if (data->lm75[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 kfree(data->lm75[1]);
969ERROR_SC_1:
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400970 if (data->lm75[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 kfree(data->lm75[0]);
972ERROR_SC_0:
973 return err;
974}
975
976static int
977w83781d_detect(struct i2c_adapter *adapter, int address, int kind)
978{
979 int i = 0, val1 = 0, val2;
980 struct i2c_client *new_client;
981 struct w83781d_data *data;
982 int err;
983 const char *client_name = "";
984 int is_isa = i2c_is_isa_adapter(adapter);
985 enum vendor { winbond, asus } vendid;
986
987 if (!is_isa
988 && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
989 err = -EINVAL;
990 goto ERROR0;
991 }
992
993 /* Prevent users from forcing a kind for a bus it isn't supposed
994 to possibly be on */
995 if (is_isa && (kind == as99127f || kind == w83783s)) {
996 dev_err(&adapter->dev,
997 "Cannot force I2C-only chip for ISA address 0x%02x.\n",
998 address);
999 err = -EINVAL;
1000 goto ERROR0;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (is_isa)
1004 if (!request_region(address, W83781D_EXTENT,
1005 w83781d_driver.name)) {
1006 dev_dbg(&adapter->dev, "Request of region "
1007 "0x%x-0x%x for w83781d failed\n", address,
1008 address + W83781D_EXTENT - 1);
1009 err = -EBUSY;
1010 goto ERROR0;
1011 }
1012
1013 /* Probe whether there is anything available on this address. Already
1014 done for SMBus clients */
1015 if (kind < 0) {
1016 if (is_isa) {
1017
1018#define REALLY_SLOW_IO
1019 /* We need the timeouts for at least some LM78-like
1020 chips. But only if we read 'undefined' registers. */
1021 i = inb_p(address + 1);
1022 if (inb_p(address + 2) != i
1023 || inb_p(address + 3) != i
1024 || inb_p(address + 7) != i) {
1025 dev_dbg(&adapter->dev, "Detection of w83781d "
1026 "chip failed at step 1\n");
1027 err = -ENODEV;
1028 goto ERROR1;
1029 }
1030#undef REALLY_SLOW_IO
1031
1032 /* Let's just hope nothing breaks here */
1033 i = inb_p(address + 5) & 0x7f;
1034 outb_p(~i & 0x7f, address + 5);
1035 val2 = inb_p(address + 5) & 0x7f;
1036 if (val2 != (~i & 0x7f)) {
1037 outb_p(i, address + 5);
1038 dev_dbg(&adapter->dev, "Detection of w83781d "
1039 "chip failed at step 2 (0x%x != "
1040 "0x%x at 0x%x)\n", val2, ~i & 0x7f,
1041 address + 5);
1042 err = -ENODEV;
1043 goto ERROR1;
1044 }
1045 }
1046 }
1047
1048 /* OK. For now, we presume we have a valid client. We now create the
1049 client structure, even though we cannot fill it completely yet.
1050 But it allows us to access w83781d_{read,write}_value. */
1051
1052 if (!(data = kmalloc(sizeof(struct w83781d_data), GFP_KERNEL))) {
1053 err = -ENOMEM;
1054 goto ERROR1;
1055 }
1056 memset(data, 0, sizeof(struct w83781d_data));
1057
1058 new_client = &data->client;
1059 i2c_set_clientdata(new_client, data);
1060 new_client->addr = address;
1061 init_MUTEX(&data->lock);
1062 new_client->adapter = adapter;
1063 new_client->driver = &w83781d_driver;
1064 new_client->flags = 0;
1065
1066 /* Now, we do the remaining detection. */
1067
1068 /* The w8378?d may be stuck in some other bank than bank 0. This may
1069 make reading other information impossible. Specify a force=... or
1070 force_*=... parameter, and the Winbond will be reset to the right
1071 bank. */
1072 if (kind < 0) {
1073 if (w83781d_read_value(new_client, W83781D_REG_CONFIG) & 0x80) {
1074 dev_dbg(&new_client->dev, "Detection failed at step "
1075 "3\n");
1076 err = -ENODEV;
1077 goto ERROR2;
1078 }
1079 val1 = w83781d_read_value(new_client, W83781D_REG_BANK);
1080 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1081 /* Check for Winbond or Asus ID if in bank 0 */
1082 if ((!(val1 & 0x07)) &&
1083 (((!(val1 & 0x80)) && (val2 != 0xa3) && (val2 != 0xc3))
1084 || ((val1 & 0x80) && (val2 != 0x5c) && (val2 != 0x12)))) {
1085 dev_dbg(&new_client->dev, "Detection failed at step "
1086 "4\n");
1087 err = -ENODEV;
1088 goto ERROR2;
1089 }
1090 /* If Winbond SMBus, check address at 0x48.
1091 Asus doesn't support, except for as99127f rev.2 */
1092 if ((!is_isa) && (((!(val1 & 0x80)) && (val2 == 0xa3)) ||
1093 ((val1 & 0x80) && (val2 == 0x5c)))) {
1094 if (w83781d_read_value
1095 (new_client, W83781D_REG_I2C_ADDR) != address) {
1096 dev_dbg(&new_client->dev, "Detection failed "
1097 "at step 5\n");
1098 err = -ENODEV;
1099 goto ERROR2;
1100 }
1101 }
1102 }
1103
1104 /* We have either had a force parameter, or we have already detected the
1105 Winbond. Put it now into bank 0 and Vendor ID High Byte */
1106 w83781d_write_value(new_client, W83781D_REG_BANK,
1107 (w83781d_read_value(new_client,
1108 W83781D_REG_BANK) & 0x78) |
1109 0x80);
1110
1111 /* Determine the chip type. */
1112 if (kind <= 0) {
1113 /* get vendor ID */
1114 val2 = w83781d_read_value(new_client, W83781D_REG_CHIPMAN);
1115 if (val2 == 0x5c)
1116 vendid = winbond;
1117 else if (val2 == 0x12)
1118 vendid = asus;
1119 else {
1120 dev_dbg(&new_client->dev, "Chip was made by neither "
1121 "Winbond nor Asus?\n");
1122 err = -ENODEV;
1123 goto ERROR2;
1124 }
1125
1126 val1 = w83781d_read_value(new_client, W83781D_REG_WCHIPID);
1127 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1128 kind = w83781d;
1129 else if (val1 == 0x30 && vendid == winbond)
1130 kind = w83782d;
1131 else if (val1 == 0x40 && vendid == winbond && !is_isa
1132 && address == 0x2d)
1133 kind = w83783s;
Jean Delvare7c7a5302005-06-16 19:24:14 +02001134 else if (val1 == 0x21 && vendid == winbond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 kind = w83627hf;
1136 else if (val1 == 0x31 && !is_isa && address >= 0x28)
1137 kind = as99127f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 else {
1139 if (kind == 0)
1140 dev_warn(&new_client->dev, "Ignoring 'force' "
1141 "parameter for unknown chip at "
1142 "adapter %d, address 0x%02x\n",
1143 i2c_adapter_id(adapter), address);
1144 err = -EINVAL;
1145 goto ERROR2;
1146 }
1147 }
1148
1149 if (kind == w83781d) {
1150 client_name = "w83781d";
1151 } else if (kind == w83782d) {
1152 client_name = "w83782d";
1153 } else if (kind == w83783s) {
1154 client_name = "w83783s";
1155 } else if (kind == w83627hf) {
Jean Delvare7c7a5302005-06-16 19:24:14 +02001156 client_name = "w83627hf";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } else if (kind == as99127f) {
1158 client_name = "as99127f";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
1161 /* Fill in the remaining client fields and put into the global list */
1162 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
1163 data->type = kind;
1164
1165 data->valid = 0;
1166 init_MUTEX(&data->update_lock);
1167
1168 /* Tell the I2C layer a new client has arrived */
1169 if ((err = i2c_attach_client(new_client)))
1170 goto ERROR2;
1171
1172 /* attach secondary i2c lm75-like clients */
1173 if (!is_isa) {
1174 if ((err = w83781d_detect_subclients(adapter, address,
1175 kind, new_client)))
1176 goto ERROR3;
1177 } else {
1178 data->lm75[0] = NULL;
1179 data->lm75[1] = NULL;
1180 }
1181
1182 /* Initialize the chip */
1183 w83781d_init_client(new_client);
1184
1185 /* A few vars need to be filled upon startup */
1186 for (i = 1; i <= 3; i++) {
1187 data->fan_min[i - 1] = w83781d_read_value(new_client,
1188 W83781D_REG_FAN_MIN(i));
1189 }
1190 if (kind != w83781d && kind != as99127f)
1191 for (i = 0; i < 4; i++)
1192 data->pwmenable[i] = 1;
1193
1194 /* Register sysfs hooks */
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001195 data->class_dev = hwmon_device_register(&new_client->dev);
1196 if (IS_ERR(data->class_dev)) {
1197 err = PTR_ERR(data->class_dev);
1198 goto ERROR4;
1199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 device_create_file_in(new_client, 0);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001202 if (kind != w83783s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 device_create_file_in(new_client, 1);
1204 device_create_file_in(new_client, 2);
1205 device_create_file_in(new_client, 3);
1206 device_create_file_in(new_client, 4);
1207 device_create_file_in(new_client, 5);
1208 device_create_file_in(new_client, 6);
1209 if (kind != as99127f && kind != w83781d && kind != w83783s) {
1210 device_create_file_in(new_client, 7);
1211 device_create_file_in(new_client, 8);
1212 }
1213
1214 device_create_file_fan(new_client, 1);
1215 device_create_file_fan(new_client, 2);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001216 device_create_file_fan(new_client, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 device_create_file_temp(new_client, 1);
1219 device_create_file_temp(new_client, 2);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001220 if (kind != w83783s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 device_create_file_temp(new_client, 3);
1222
Jean Delvare7c7a5302005-06-16 19:24:14 +02001223 device_create_file_vid(new_client);
1224 device_create_file_vrm(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 device_create_file_fan_div(new_client, 1);
1227 device_create_file_fan_div(new_client, 2);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001228 device_create_file_fan_div(new_client, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 device_create_file_alarms(new_client);
1231
1232 device_create_file_beep(new_client);
1233
1234 if (kind != w83781d && kind != as99127f) {
1235 device_create_file_pwm(new_client, 1);
1236 device_create_file_pwm(new_client, 2);
1237 device_create_file_pwmenable(new_client, 2);
1238 }
1239 if (kind == w83782d && !is_isa) {
1240 device_create_file_pwm(new_client, 3);
1241 device_create_file_pwm(new_client, 4);
1242 }
1243
1244 if (kind != as99127f && kind != w83781d) {
1245 device_create_file_sensor(new_client, 1);
1246 device_create_file_sensor(new_client, 2);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001247 if (kind != w83783s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 device_create_file_sensor(new_client, 3);
1249 }
1250
1251 return 0;
1252
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001253ERROR4:
1254 if (data->lm75[1]) {
1255 i2c_detach_client(data->lm75[1]);
1256 kfree(data->lm75[1]);
1257 }
1258 if (data->lm75[0]) {
1259 i2c_detach_client(data->lm75[0]);
1260 kfree(data->lm75[0]);
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262ERROR3:
1263 i2c_detach_client(new_client);
1264ERROR2:
1265 kfree(data);
1266ERROR1:
1267 if (is_isa)
1268 release_region(address, W83781D_EXTENT);
1269ERROR0:
1270 return err;
1271}
1272
1273static int
1274w83781d_detach_client(struct i2c_client *client)
1275{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001276 struct w83781d_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int err;
1278
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001279 /* main client */
1280 if (data)
1281 hwmon_device_unregister(data->class_dev);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (i2c_is_isa_client(client))
1284 release_region(client->addr, W83781D_EXTENT);
1285
1286 if ((err = i2c_detach_client(client))) {
1287 dev_err(&client->dev,
1288 "Client deregistration failed, client not detached.\n");
1289 return err;
1290 }
1291
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001292 /* main client */
1293 if (data)
1294 kfree(data);
1295
1296 /* subclient */
1297 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 return 0;
1301}
1302
1303/* The SMBus locks itself, usually, but nothing may access the Winbond between
1304 bank switches. ISA access must always be locked explicitly!
1305 We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1306 would slow down the W83781D access and should not be necessary.
1307 There are some ugly typecasts here, but the good news is - they should
1308 nowhere else be necessary! */
1309static int
1310w83781d_read_value(struct i2c_client *client, u16 reg)
1311{
1312 struct w83781d_data *data = i2c_get_clientdata(client);
1313 int res, word_sized, bank;
1314 struct i2c_client *cl;
1315
1316 down(&data->lock);
1317 if (i2c_is_isa_client(client)) {
1318 word_sized = (((reg & 0xff00) == 0x100)
1319 || ((reg & 0xff00) == 0x200))
1320 && (((reg & 0x00ff) == 0x50)
1321 || ((reg & 0x00ff) == 0x53)
1322 || ((reg & 0x00ff) == 0x55));
1323 if (reg & 0xff00) {
1324 outb_p(W83781D_REG_BANK,
1325 client->addr + W83781D_ADDR_REG_OFFSET);
1326 outb_p(reg >> 8,
1327 client->addr + W83781D_DATA_REG_OFFSET);
1328 }
1329 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1330 res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
1331 if (word_sized) {
1332 outb_p((reg & 0xff) + 1,
1333 client->addr + W83781D_ADDR_REG_OFFSET);
1334 res =
1335 (res << 8) + inb_p(client->addr +
1336 W83781D_DATA_REG_OFFSET);
1337 }
1338 if (reg & 0xff00) {
1339 outb_p(W83781D_REG_BANK,
1340 client->addr + W83781D_ADDR_REG_OFFSET);
1341 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1342 }
1343 } else {
1344 bank = (reg >> 8) & 0x0f;
1345 if (bank > 2)
1346 /* switch banks */
1347 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1348 bank);
1349 if (bank == 0 || bank > 2) {
1350 res = i2c_smbus_read_byte_data(client, reg & 0xff);
1351 } else {
1352 /* switch to subclient */
1353 cl = data->lm75[bank - 1];
1354 /* convert from ISA to LM75 I2C addresses */
1355 switch (reg & 0xff) {
1356 case 0x50: /* TEMP */
1357 res = swab16(i2c_smbus_read_word_data(cl, 0));
1358 break;
1359 case 0x52: /* CONFIG */
1360 res = i2c_smbus_read_byte_data(cl, 1);
1361 break;
1362 case 0x53: /* HYST */
1363 res = swab16(i2c_smbus_read_word_data(cl, 2));
1364 break;
1365 case 0x55: /* OVER */
1366 default:
1367 res = swab16(i2c_smbus_read_word_data(cl, 3));
1368 break;
1369 }
1370 }
1371 if (bank > 2)
1372 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1373 }
1374 up(&data->lock);
1375 return res;
1376}
1377
1378static int
1379w83781d_write_value(struct i2c_client *client, u16 reg, u16 value)
1380{
1381 struct w83781d_data *data = i2c_get_clientdata(client);
1382 int word_sized, bank;
1383 struct i2c_client *cl;
1384
1385 down(&data->lock);
1386 if (i2c_is_isa_client(client)) {
1387 word_sized = (((reg & 0xff00) == 0x100)
1388 || ((reg & 0xff00) == 0x200))
1389 && (((reg & 0x00ff) == 0x53)
1390 || ((reg & 0x00ff) == 0x55));
1391 if (reg & 0xff00) {
1392 outb_p(W83781D_REG_BANK,
1393 client->addr + W83781D_ADDR_REG_OFFSET);
1394 outb_p(reg >> 8,
1395 client->addr + W83781D_DATA_REG_OFFSET);
1396 }
1397 outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
1398 if (word_sized) {
1399 outb_p(value >> 8,
1400 client->addr + W83781D_DATA_REG_OFFSET);
1401 outb_p((reg & 0xff) + 1,
1402 client->addr + W83781D_ADDR_REG_OFFSET);
1403 }
1404 outb_p(value & 0xff, client->addr + W83781D_DATA_REG_OFFSET);
1405 if (reg & 0xff00) {
1406 outb_p(W83781D_REG_BANK,
1407 client->addr + W83781D_ADDR_REG_OFFSET);
1408 outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
1409 }
1410 } else {
1411 bank = (reg >> 8) & 0x0f;
1412 if (bank > 2)
1413 /* switch banks */
1414 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1415 bank);
1416 if (bank == 0 || bank > 2) {
1417 i2c_smbus_write_byte_data(client, reg & 0xff,
1418 value & 0xff);
1419 } else {
1420 /* switch to subclient */
1421 cl = data->lm75[bank - 1];
1422 /* convert from ISA to LM75 I2C addresses */
1423 switch (reg & 0xff) {
1424 case 0x52: /* CONFIG */
1425 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1426 break;
1427 case 0x53: /* HYST */
1428 i2c_smbus_write_word_data(cl, 2, swab16(value));
1429 break;
1430 case 0x55: /* OVER */
1431 i2c_smbus_write_word_data(cl, 3, swab16(value));
1432 break;
1433 }
1434 }
1435 if (bank > 2)
1436 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1437 }
1438 up(&data->lock);
1439 return 0;
1440}
1441
1442/* Called when we have found a new W83781D. It should set limits, etc. */
1443static void
1444w83781d_init_client(struct i2c_client *client)
1445{
1446 struct w83781d_data *data = i2c_get_clientdata(client);
1447 int i, p;
1448 int type = data->type;
1449 u8 tmp;
1450
1451 if (init && type != as99127f) { /* this resets registers we don't have
1452 documentation for on the as99127f */
1453 /* save these registers */
1454 i = w83781d_read_value(client, W83781D_REG_BEEP_CONFIG);
1455 p = w83781d_read_value(client, W83781D_REG_PWMCLK12);
1456 /* Reset all except Watchdog values and last conversion values
1457 This sets fan-divs to 2, among others */
1458 w83781d_write_value(client, W83781D_REG_CONFIG, 0x80);
1459 /* Restore the registers and disable power-on abnormal beep.
1460 This saves FAN 1/2/3 input/output values set by BIOS. */
1461 w83781d_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
1462 w83781d_write_value(client, W83781D_REG_PWMCLK12, p);
1463 /* Disable master beep-enable (reset turns it on).
1464 Individual beep_mask should be reset to off but for some reason
1465 disabling this bit helps some people not get beeped */
1466 w83781d_write_value(client, W83781D_REG_BEEP_INTS2, 0);
1467 }
1468
1469 data->vrm = i2c_which_vrm();
1470
1471 if ((type != w83781d) && (type != as99127f)) {
1472 tmp = w83781d_read_value(client, W83781D_REG_SCFG1);
1473 for (i = 1; i <= 3; i++) {
1474 if (!(tmp & BIT_SCFG1[i - 1])) {
1475 data->sens[i - 1] = W83781D_DEFAULT_BETA;
1476 } else {
1477 if (w83781d_read_value
1478 (client,
1479 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1480 data->sens[i - 1] = 1;
1481 else
1482 data->sens[i - 1] = 2;
1483 }
Jean Delvare7c7a5302005-06-16 19:24:14 +02001484 if (type == w83783s && i == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 }
1487 }
1488
1489 if (init && type != as99127f) {
1490 /* Enable temp2 */
1491 tmp = w83781d_read_value(client, W83781D_REG_TEMP2_CONFIG);
1492 if (tmp & 0x01) {
1493 dev_warn(&client->dev, "Enabling temp2, readings "
1494 "might not make sense\n");
1495 w83781d_write_value(client, W83781D_REG_TEMP2_CONFIG,
1496 tmp & 0xfe);
1497 }
1498
1499 /* Enable temp3 */
Jean Delvare7c7a5302005-06-16 19:24:14 +02001500 if (type != w83783s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 tmp = w83781d_read_value(client,
1502 W83781D_REG_TEMP3_CONFIG);
1503 if (tmp & 0x01) {
1504 dev_warn(&client->dev, "Enabling temp3, "
1505 "readings might not make sense\n");
1506 w83781d_write_value(client,
1507 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1508 }
1509 }
1510
1511 if (type != w83781d) {
1512 /* enable comparator mode for temp2 and temp3 so
1513 alarm indication will work correctly */
1514 i = w83781d_read_value(client, W83781D_REG_IRQ);
1515 if (!(i & 0x40))
1516 w83781d_write_value(client, W83781D_REG_IRQ,
1517 i | 0x40);
1518 }
1519 }
1520
1521 /* Start monitoring */
1522 w83781d_write_value(client, W83781D_REG_CONFIG,
1523 (w83781d_read_value(client,
1524 W83781D_REG_CONFIG) & 0xf7)
1525 | 0x01);
1526}
1527
1528static struct w83781d_data *w83781d_update_device(struct device *dev)
1529{
1530 struct i2c_client *client = to_i2c_client(dev);
1531 struct w83781d_data *data = i2c_get_clientdata(client);
1532 int i;
1533
1534 down(&data->update_lock);
1535
1536 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1537 || !data->valid) {
1538 dev_dbg(dev, "Starting device update\n");
1539
1540 for (i = 0; i <= 8; i++) {
Jean Delvare7c7a5302005-06-16 19:24:14 +02001541 if (data->type == w83783s && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 continue; /* 783S has no in1 */
1543 data->in[i] =
1544 w83781d_read_value(client, W83781D_REG_IN(i));
1545 data->in_min[i] =
1546 w83781d_read_value(client, W83781D_REG_IN_MIN(i));
1547 data->in_max[i] =
1548 w83781d_read_value(client, W83781D_REG_IN_MAX(i));
Jean Delvare7c7a5302005-06-16 19:24:14 +02001549 if ((data->type != w83782d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 && (data->type != w83627hf) && (i == 6))
1551 break;
1552 }
1553 for (i = 1; i <= 3; i++) {
1554 data->fan[i - 1] =
1555 w83781d_read_value(client, W83781D_REG_FAN(i));
1556 data->fan_min[i - 1] =
1557 w83781d_read_value(client, W83781D_REG_FAN_MIN(i));
1558 }
1559 if (data->type != w83781d && data->type != as99127f) {
1560 for (i = 1; i <= 4; i++) {
1561 data->pwm[i - 1] =
1562 w83781d_read_value(client,
1563 W83781D_REG_PWM(i));
1564 if ((data->type != w83782d
1565 || i2c_is_isa_client(client))
1566 && i == 2)
1567 break;
1568 }
1569 /* Only PWM2 can be disabled */
1570 data->pwmenable[1] = (w83781d_read_value(client,
1571 W83781D_REG_PWMCLK12) & 0x08) >> 3;
1572 }
1573
1574 data->temp = w83781d_read_value(client, W83781D_REG_TEMP(1));
1575 data->temp_max =
1576 w83781d_read_value(client, W83781D_REG_TEMP_OVER(1));
1577 data->temp_max_hyst =
1578 w83781d_read_value(client, W83781D_REG_TEMP_HYST(1));
1579 data->temp_add[0] =
1580 w83781d_read_value(client, W83781D_REG_TEMP(2));
1581 data->temp_max_add[0] =
1582 w83781d_read_value(client, W83781D_REG_TEMP_OVER(2));
1583 data->temp_max_hyst_add[0] =
1584 w83781d_read_value(client, W83781D_REG_TEMP_HYST(2));
Jean Delvare7c7a5302005-06-16 19:24:14 +02001585 if (data->type != w83783s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 data->temp_add[1] =
1587 w83781d_read_value(client, W83781D_REG_TEMP(3));
1588 data->temp_max_add[1] =
1589 w83781d_read_value(client,
1590 W83781D_REG_TEMP_OVER(3));
1591 data->temp_max_hyst_add[1] =
1592 w83781d_read_value(client,
1593 W83781D_REG_TEMP_HYST(3));
1594 }
1595 i = w83781d_read_value(client, W83781D_REG_VID_FANDIV);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001596 data->vid = i & 0x0f;
1597 data->vid |= (w83781d_read_value(client,
1598 W83781D_REG_CHIPID) & 0x01) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 data->fan_div[0] = (i >> 4) & 0x03;
1600 data->fan_div[1] = (i >> 6) & 0x03;
Jean Delvare7c7a5302005-06-16 19:24:14 +02001601 data->fan_div[2] = (w83781d_read_value(client,
1602 W83781D_REG_PIN) >> 6) & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if ((data->type != w83781d) && (data->type != as99127f)) {
1604 i = w83781d_read_value(client, W83781D_REG_VBAT);
1605 data->fan_div[0] |= (i >> 3) & 0x04;
1606 data->fan_div[1] |= (i >> 4) & 0x04;
Jean Delvare7c7a5302005-06-16 19:24:14 +02001607 data->fan_div[2] |= (i >> 5) & 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 data->alarms =
1610 w83781d_read_value(client,
1611 W83781D_REG_ALARM1) +
1612 (w83781d_read_value(client, W83781D_REG_ALARM2) << 8);
1613 if ((data->type == w83782d) || (data->type == w83627hf)) {
1614 data->alarms |=
1615 w83781d_read_value(client,
1616 W83781D_REG_ALARM3) << 16;
1617 }
1618 i = w83781d_read_value(client, W83781D_REG_BEEP_INTS2);
1619 data->beep_enable = i >> 7;
1620 data->beep_mask = ((i & 0x7f) << 8) +
1621 w83781d_read_value(client, W83781D_REG_BEEP_INTS1);
1622 if ((data->type != w83781d) && (data->type != as99127f)) {
1623 data->beep_mask |=
1624 w83781d_read_value(client,
1625 W83781D_REG_BEEP_INTS3) << 16;
1626 }
1627 data->last_updated = jiffies;
1628 data->valid = 1;
1629 }
1630
1631 up(&data->update_lock);
1632
1633 return data;
1634}
1635
1636static int __init
1637sensors_w83781d_init(void)
1638{
1639 return i2c_add_driver(&w83781d_driver);
1640}
1641
1642static void __exit
1643sensors_w83781d_exit(void)
1644{
1645 i2c_del_driver(&w83781d_driver);
1646}
1647
1648MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
1649 "Philip Edelbrock <phil@netroedge.com>, "
1650 "and Mark Studebaker <mdsxyz123@yahoo.com>");
1651MODULE_DESCRIPTION("W83781D driver");
1652MODULE_LICENSE("GPL");
1653
1654module_init(sensors_w83781d_init);
1655module_exit(sensors_w83781d_exit);