Blame view

drivers/hwmon/w83781d.c 57.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
  /*
      w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
                  monitoring
      Copyright (c) 1998 - 2001  Frodo Looijaard <frodol@dds.nl>,
7666c13c6   Jean Delvare   hwmon/w83781d: No...
5
6
                                 Philip Edelbrock <phil@netroedge.com>,
                                 and Mark Studebaker <mdsxyz123@yahoo.com>
360782dde   Jean Delvare   hwmon: (w83781d) ...
7
      Copyright (c) 2007 - 2008  Jean Delvare <khali@linux-fr.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
      This program is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation; either version 2 of the License, or
      (at your option) any later version.
  
      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
  
  /*
      Supports following chips:
  
      Chip	#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
      as99127f	7	3	0	3	0x31	0x12c3	yes	no
      as99127f rev.2 (type_name = as99127f)	0x31	0x5ca3	yes	no
      w83781d	7	3	0	3	0x10-1	0x5ca3	yes	yes
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
      w83782d	9	3	2-4	3	0x30	0x5ca3	yes	yes
      w83783s	5-6	3	2	1-2	0x40	0x5ca3	yes	no
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
  
  */
1ca28218e   Joe Perches   hwmon: (w83781d) ...
35
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/jiffies.h>
  #include <linux/i2c.h>
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
41
  #include <linux/hwmon.h>
303760b44   Jean Delvare   [PATCH] hwmon: hw...
42
  #include <linux/hwmon-vid.h>
348753379   Jean Delvare   hwmon/w83781d: Us...
43
  #include <linux/hwmon-sysfs.h>
311ce2efb   Jim Cromie   w83781d: Fix unch...
44
  #include <linux/sysfs.h>
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
45
  #include <linux/err.h>
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
46
  #include <linux/mutex.h>
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
47
48
49
50
  
  #ifdef CONFIG_ISA
  #include <linux/platform_device.h>
  #include <linux/ioport.h>
6055fae8a   H Hartley Sweeten   hwmon: Include <l...
51
  #include <linux/io.h>
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
52
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53

443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
54
  #include "lm75.h"
7666c13c6   Jean Delvare   hwmon/w83781d: No...
55

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
  /* Addresses to scan */
25e9c86d5   Mark M. Hoffman   hwmon: normal_i2c...
57
58
  static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
  						0x2e, 0x2f, I2C_CLIENT_END };
3aed198c3   Jean Delvare   hwmon: Don't over...
59

e5e9f44c2   Jean Delvare   i2c: Drop I2C_CLI...
60
61
62
  enum chips { w83781d, w83782d, w83783s, as99127f };
  
  /* Insmod parameters */
3aed198c3   Jean Delvare   hwmon: Don't over...
63
64
65
  static unsigned short force_subclients[4];
  module_param_array(force_subclients, short, NULL, 0);
  MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  		    "{bus, clientaddr, subclientaddr1, subclientaddr2}");
90ab5ee94   Rusty Russell   module_param: mak...
67
  static bool reset;
fabddcd49   Jean Delvare   [PATCH] w83781d: ...
68
69
  module_param(reset, bool, 0);
  MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
90ab5ee94   Rusty Russell   module_param: mak...
70
  static bool init = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
76
77
78
79
80
81
  module_param(init, bool, 0);
  MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
  
  /* Constants specified below */
  
  /* Length of ISA address segment */
  #define W83781D_EXTENT			8
  
  /* Where are the ISA address/data registers relative to the base address */
  #define W83781D_ADDR_REG_OFFSET		5
  #define W83781D_DATA_REG_OFFSET		6
348753379   Jean Delvare   hwmon/w83781d: Us...
82
83
  /* The device registers */
  /* in nr from 0 to 8 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
87
88
89
  #define W83781D_REG_IN_MAX(nr)		((nr < 7) ? (0x2b + (nr) * 2) : \
  						    (0x554 + (((nr) - 7) * 2)))
  #define W83781D_REG_IN_MIN(nr)		((nr < 7) ? (0x2c + (nr) * 2) : \
  						    (0x555 + (((nr) - 7) * 2)))
  #define W83781D_REG_IN(nr)		((nr < 7) ? (0x20 + (nr)) : \
  						    (0x550 + (nr) - 7))
348753379   Jean Delvare   hwmon/w83781d: Us...
90
91
92
  /* fan nr from 0 to 2 */
  #define W83781D_REG_FAN_MIN(nr)		(0x3b + (nr))
  #define W83781D_REG_FAN(nr)		(0x28 + (nr))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
95
96
  
  #define W83781D_REG_BANK		0x4E
  #define W83781D_REG_TEMP2_CONFIG	0x152
  #define W83781D_REG_TEMP3_CONFIG	0x252
348753379   Jean Delvare   hwmon/w83781d: Us...
97
  /* temp nr from 1 to 3 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
103
104
105
106
107
108
  #define W83781D_REG_TEMP(nr)		((nr == 3) ? (0x0250) : \
  					((nr == 2) ? (0x0150) : \
  						     (0x27)))
  #define W83781D_REG_TEMP_HYST(nr)	((nr == 3) ? (0x253) : \
  					((nr == 2) ? (0x153) : \
  						     (0x3A)))
  #define W83781D_REG_TEMP_OVER(nr)	((nr == 3) ? (0x255) : \
  					((nr == 2) ? (0x155) : \
  						     (0x39)))
  
  #define W83781D_REG_CONFIG		0x40
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
109
110
  
  /* Interrupt status (W83781D, AS99127F) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
  #define W83781D_REG_ALARM1		0x41
  #define W83781D_REG_ALARM2		0x42
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113

05663368d   Jean Delvare   hwmon: (w83781d) ...
114
  /* Real-time status (W83782D, W83783S) */
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
115
116
117
  #define W83782D_REG_ALARM1		0x459
  #define W83782D_REG_ALARM2		0x45A
  #define W83782D_REG_ALARM3		0x45B
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  #define W83781D_REG_BEEP_CONFIG		0x4D
  #define W83781D_REG_BEEP_INTS1		0x56
  #define W83781D_REG_BEEP_INTS2		0x57
  #define W83781D_REG_BEEP_INTS3		0x453	/* not on W83781D */
  
  #define W83781D_REG_VID_FANDIV		0x47
  
  #define W83781D_REG_CHIPID		0x49
  #define W83781D_REG_WCHIPID		0x58
  #define W83781D_REG_CHIPMAN		0x4F
  #define W83781D_REG_PIN			0x4B
  
  /* 782D/783S only */
  #define W83781D_REG_VBAT		0x5D
  
  /* PWM 782D (1-4) and 783S (1-2) only */
348753379   Jean Delvare   hwmon/w83781d: Us...
134
  static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
136
  #define W83781D_REG_PWMCLK12		0x5C
  #define W83781D_REG_PWMCLK34		0x45C
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  
  #define W83781D_REG_I2C_ADDR		0x48
  #define W83781D_REG_I2C_SUBADDR		0x4A
  
  /* The following are undocumented in the data sheets however we
     received the information in an email from Winbond tech support */
  /* Sensor selection - not on 781d */
  #define W83781D_REG_SCFG1		0x5D
  static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
  
  #define W83781D_REG_SCFG2		0x59
  static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
  
  #define W83781D_DEFAULT_BETA		3435
474d00a89   Jean Delvare   hwmon/w83781d: Cl...
151
152
153
  /* Conversions */
  #define IN_TO_REG(val)			SENSORS_LIMIT(((val) + 8) / 16, 0, 255)
  #define IN_FROM_REG(val)		((val) * 16)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
156
157
158
159
160
161
162
  
  static inline u8
  FAN_TO_REG(long rpm, int div)
  {
  	if (rpm == 0)
  		return 255;
  	rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  	return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  }
474d00a89   Jean Delvare   hwmon/w83781d: Cl...
163
164
165
166
167
168
169
170
171
  static inline long
  FAN_FROM_REG(u8 val, int div)
  {
  	if (val == 0)
  		return -1;
  	if (val == 255)
  		return 0;
  	return 1350000 / (val * div);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172

474d00a89   Jean Delvare   hwmon/w83781d: Cl...
173
174
  #define TEMP_TO_REG(val)		SENSORS_LIMIT((val) / 1000, -127, 128)
  #define TEMP_FROM_REG(val)		((val) * 1000)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  #define BEEP_MASK_FROM_REG(val,type)	((type) == as99127f ? \
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
177
  					 (~(val)) & 0x7fff : (val) & 0xff7fff)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  #define BEEP_MASK_TO_REG(val,type)	((type) == as99127f ? \
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
179
  					 (~(val)) & 0x7fff : (val) & 0xff7fff)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
187
188
189
  #define DIV_FROM_REG(val)		(1 << (val))
  
  static inline u8
  DIV_TO_REG(long val, enum chips type)
  {
  	int i;
  	val = SENSORS_LIMIT(val, 1,
  			    ((type == w83781d
  			      || type == as99127f) ? 8 : 128)) >> 1;
abc019224   Grant Coady   [PATCH] I2C: Sett...
190
  	for (i = 0; i < 7; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
192
193
194
  		if (val == 0)
  			break;
  		val >>= 1;
  	}
474d00a89   Jean Delvare   hwmon/w83781d: Cl...
195
  	return i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
  struct w83781d_data {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
198
  	struct i2c_client *client;
1beeffe43   Tony Jones   hwmon: Convert fr...
199
  	struct device *hwmon_dev;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
200
  	struct mutex lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
  	enum chips type;
360782dde   Jean Delvare   hwmon: (w83781d) ...
202
203
204
  	/* For ISA device only */
  	const char *name;
  	int isa_addr;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
205
  	struct mutex update_lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
210
211
212
213
214
215
216
  	char valid;		/* !=0 if following fields are valid */
  	unsigned long last_updated;	/* In jiffies */
  
  	struct i2c_client *lm75[2];	/* for secondary I2C addresses */
  	/* array of 2 pointers to subclients */
  
  	u8 in[9];		/* Register value - 8 & 9 for 782D only */
  	u8 in_max[9];		/* Register value - 8 & 9 for 782D only */
  	u8 in_min[9];		/* Register value - 8 & 9 for 782D only */
  	u8 fan[3];		/* Register value */
  	u8 fan_min[3];		/* Register value */
474d00a89   Jean Delvare   hwmon/w83781d: Cl...
217
218
219
  	s8 temp;		/* Register value */
  	s8 temp_max;		/* Register value */
  	s8 temp_max_hyst;	/* Register value */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
225
226
  	u16 temp_add[2];	/* Register value */
  	u16 temp_max_add[2];	/* Register value */
  	u16 temp_max_hyst_add[2];	/* Register value */
  	u8 fan_div[3];		/* Register encoding, shifted right */
  	u8 vid;			/* Register encoding, combined */
  	u32 alarms;		/* Register encoding, combined */
  	u32 beep_mask;		/* Register encoding, combined */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  	u8 pwm[4];		/* Register value */
348753379   Jean Delvare   hwmon/w83781d: Us...
228
  	u8 pwm2_enable;		/* Boolean */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
  	u16 sens[3];		/* 782D/783S only.
  				   1 = pentium diode; 2 = 3904 diode;
b26f93309   Jean Delvare   hwmon: Don't expo...
231
  				   4 = thermistor */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
  	u8 vrm;
  };
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
234
235
  static struct w83781d_data *w83781d_data_if_isa(void);
  static int w83781d_alias_detect(struct i2c_client *client, u8 chipid);
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
236
237
  static int w83781d_read_value(struct w83781d_data *data, u16 reg);
  static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  static struct w83781d_data *w83781d_update_device(struct device *dev);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
239
  static void w83781d_init_device(struct device *dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
  /* following are the sysfs callback functions */
  #define show_in_reg(reg) \
348753379   Jean Delvare   hwmon/w83781d: Us...
243
244
  static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
  		char *buf) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  { \
348753379   Jean Delvare   hwmon/w83781d: Us...
246
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
  	struct w83781d_data *data = w83781d_update_device(dev); \
348753379   Jean Delvare   hwmon/w83781d: Us...
248
249
250
  	return sprintf(buf, "%ld
  ", \
  		       (long)IN_FROM_REG(data->reg[attr->index])); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
253
254
255
256
  }
  show_in_reg(in);
  show_in_reg(in_min);
  show_in_reg(in_max);
  
  #define store_in_reg(REG, reg) \
348753379   Jean Delvare   hwmon/w83781d: Us...
257
258
  static ssize_t store_in_##reg (struct device *dev, struct device_attribute \
  		*da, const char *buf, size_t count) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
  { \
348753379   Jean Delvare   hwmon/w83781d: Us...
260
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
7666c13c6   Jean Delvare   hwmon/w83781d: No...
261
  	struct w83781d_data *data = dev_get_drvdata(dev); \
348753379   Jean Delvare   hwmon/w83781d: Us...
262
  	int nr = attr->index; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
264
  	u32 val; \
  	 \
474d00a89   Jean Delvare   hwmon/w83781d: Cl...
265
  	val = simple_strtoul(buf, NULL, 10); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  	 \
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
267
  	mutex_lock(&data->update_lock); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  	data->in_##reg[nr] = IN_TO_REG(val); \
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
269
  	w83781d_write_value(data, W83781D_REG_IN_##REG(nr), data->in_##reg[nr]); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
  	 \
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
271
  	mutex_unlock(&data->update_lock); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
  	return count; \
  }
  store_in_reg(MIN, min);
  store_in_reg(MAX, max);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
  #define sysfs_in_offsets(offset) \
348753379   Jean Delvare   hwmon/w83781d: Us...
277
278
279
280
281
282
  static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  		show_in, NULL, offset); \
  static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  		show_in_min, store_in_min, offset); \
  static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  		show_in_max, store_in_max, offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
287
288
289
290
291
292
  
  sysfs_in_offsets(0);
  sysfs_in_offsets(1);
  sysfs_in_offsets(2);
  sysfs_in_offsets(3);
  sysfs_in_offsets(4);
  sysfs_in_offsets(5);
  sysfs_in_offsets(6);
  sysfs_in_offsets(7);
  sysfs_in_offsets(8);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
  #define show_fan_reg(reg) \
348753379   Jean Delvare   hwmon/w83781d: Us...
294
295
  static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
  		char *buf) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  { \
348753379   Jean Delvare   hwmon/w83781d: Us...
297
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
  	struct w83781d_data *data = w83781d_update_device(dev); \
  	return sprintf(buf,"%ld
  ", \
348753379   Jean Delvare   hwmon/w83781d: Us...
301
302
  		FAN_FROM_REG(data->reg[attr->index], \
  			DIV_FROM_REG(data->fan_div[attr->index]))); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
306
307
  }
  show_fan_reg(fan);
  show_fan_reg(fan_min);
  
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
308
309
  store_fan_min(struct device *dev, struct device_attribute *da,
  		const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
311
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
312
  	struct w83781d_data *data = dev_get_drvdata(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
313
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
  	u32 val;
  
  	val = simple_strtoul(buf, NULL, 10);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
317
  	mutex_lock(&data->update_lock);
348753379   Jean Delvare   hwmon/w83781d: Us...
318
319
  	data->fan_min[nr] =
  	    FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
320
  	w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
348753379   Jean Delvare   hwmon/w83781d: Us...
321
  			    data->fan_min[nr]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
323
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
325
  	return count;
  }
348753379   Jean Delvare   hwmon/w83781d: Us...
326
327
328
329
330
331
332
333
334
  static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
  		show_fan_min, store_fan_min, 0);
  static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
  		show_fan_min, store_fan_min, 1);
  static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
  		show_fan_min, store_fan_min, 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
  #define show_temp_reg(reg) \
348753379   Jean Delvare   hwmon/w83781d: Us...
337
338
  static ssize_t show_##reg (struct device *dev, struct device_attribute *da, \
  		char *buf) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
  { \
348753379   Jean Delvare   hwmon/w83781d: Us...
340
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
  	struct w83781d_data *data = w83781d_update_device(dev); \
348753379   Jean Delvare   hwmon/w83781d: Us...
342
  	int nr = attr->index; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  	if (nr >= 2) {	/* TEMP2 and TEMP3 */ \
  		return sprintf(buf,"%d
  ", \
  			LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
  	} else {	/* TEMP1 */ \
  		return sprintf(buf,"%ld
  ", (long)TEMP_FROM_REG(data->reg)); \
  	} \
  }
  show_temp_reg(temp);
  show_temp_reg(temp_max);
  show_temp_reg(temp_max_hyst);
  
  #define store_temp_reg(REG, reg) \
348753379   Jean Delvare   hwmon/w83781d: Us...
357
358
  static ssize_t store_temp_##reg (struct device *dev, \
  		struct device_attribute *da, const char *buf, size_t count) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
  { \
348753379   Jean Delvare   hwmon/w83781d: Us...
360
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
7666c13c6   Jean Delvare   hwmon/w83781d: No...
361
  	struct w83781d_data *data = dev_get_drvdata(dev); \
348753379   Jean Delvare   hwmon/w83781d: Us...
362
  	int nr = attr->index; \
5bfedac04   Christian Hohnstaedt   hwmon: Allow writ...
363
  	long val; \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
  	 \
  	val = simple_strtol(buf, NULL, 10); \
  	 \
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
367
  	mutex_lock(&data->update_lock); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
  	 \
  	if (nr >= 2) {	/* TEMP2 and TEMP3 */ \
  		data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
371
  		w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
373
374
  				data->temp_##reg##_add[nr-2]); \
  	} else {	/* TEMP1 */ \
  		data->temp_##reg = TEMP_TO_REG(val); \
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
375
  		w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
  			data->temp_##reg); \
  	} \
  	 \
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
379
  	mutex_unlock(&data->update_lock); \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
  	return count; \
  }
  store_temp_reg(OVER, max);
  store_temp_reg(HYST, max_hyst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
  #define sysfs_temp_offsets(offset) \
348753379   Jean Delvare   hwmon/w83781d: Us...
385
386
387
388
389
390
  static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  		show_temp, NULL, offset); \
  static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  		show_temp_max, store_temp_max, offset); \
  static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
  		show_temp_max_hyst, store_temp_max_hyst, offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
394
  
  sysfs_temp_offsets(1);
  sysfs_temp_offsets(2);
  sysfs_temp_offsets(3);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395
  static ssize_t
e404e274f   Yani Ioannou   [PATCH] Driver Co...
396
  show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
399
400
401
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
  	return sprintf(buf, "%ld
  ", (long) vid_from_reg(data->vid, data->vrm));
  }
311ce2efb   Jim Cromie   w83781d: Fix unch...
402
  static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
  static ssize_t
e404e274f   Yani Ioannou   [PATCH] Driver Co...
404
  show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
  {
90d6619a9   Jean Delvare   hwmon: VRM is not...
406
  	struct w83781d_data *data = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
410
411
  	return sprintf(buf, "%ld
  ", (long) data->vrm);
  }
  
  static ssize_t
e404e274f   Yani Ioannou   [PATCH] Driver Co...
412
  store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
  {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
414
  	struct w83781d_data *data = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
418
419
420
421
  	u32 val;
  
  	val = simple_strtoul(buf, NULL, 10);
  	data->vrm = val;
  
  	return count;
  }
311ce2efb   Jim Cromie   w83781d: Fix unch...
422
  static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  static ssize_t
e404e274f   Yani Ioannou   [PATCH] Driver Co...
424
  show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
68188ba7d   Jean Delvare   [PATCH] I2C: Kill...
427
428
  	return sprintf(buf, "%u
  ", data->alarms);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
  }
311ce2efb   Jim Cromie   w83781d: Fix unch...
430
  static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  		char *buf)
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
  	int bitnr = to_sensor_dev_attr(attr)->index;
  	return sprintf(buf, "%u
  ", (data->alarms >> bitnr) & 1);
  }
  
  /* The W83781D has a single alarm bit for temp2 and temp3 */
  static ssize_t show_temp3_alarm(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
  	int bitnr = (data->type == w83781d) ? 5 : 13;
  	return sprintf(buf, "%u
  ", (data->alarms >> bitnr) & 1);
  }
  
  static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
  static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
  static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
  static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
  static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
  static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0);
e404e274f   Yani Ioannou   [PATCH] Driver Co...
465
  static ssize_t show_beep_mask (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
470
471
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
  	return sprintf(buf, "%ld
  ",
  		       (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
474
475
  store_beep_mask(struct device *dev, struct device_attribute *attr,
  		const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
477
  	struct w83781d_data *data = dev_get_drvdata(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
478
  	u32 val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
  
  	val = simple_strtoul(buf, NULL, 10);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
481
  	mutex_lock(&data->update_lock);
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
482
483
  	data->beep_mask &= 0x8000; /* preserve beep enable */
  	data->beep_mask |= BEEP_MASK_TO_REG(val, data->type);
348753379   Jean Delvare   hwmon/w83781d: Us...
484
485
486
  	w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
  			    data->beep_mask & 0xff);
  	w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
487
  			    (data->beep_mask >> 8) & 0xff);
348753379   Jean Delvare   hwmon/w83781d: Us...
488
489
490
491
492
  	if (data->type != w83781d && data->type != as99127f) {
  		w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
  				    ((data->beep_mask) >> 16) & 0xff);
  	}
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493

348753379   Jean Delvare   hwmon/w83781d: Us...
494
495
  	return count;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496

348753379   Jean Delvare   hwmon/w83781d: Us...
497
498
  static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
  		show_beep_mask, store_beep_mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499

7d4a13749   Jean Delvare   hwmon: (w83781d) ...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
  		char *buf)
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
  	int bitnr = to_sensor_dev_attr(attr)->index;
  	return sprintf(buf, "%u
  ", (data->beep_mask >> bitnr) & 1);
  }
  
  static ssize_t
  store_beep(struct device *dev, struct device_attribute *attr,
  		const char *buf, size_t count)
  {
  	struct w83781d_data *data = dev_get_drvdata(dev);
  	int bitnr = to_sensor_dev_attr(attr)->index;
  	unsigned long bit;
  	u8 reg;
  
  	bit = simple_strtoul(buf, NULL, 10);
  	if (bit & ~1)
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	if (bit)
  		data->beep_mask |= (1 << bitnr);
  	else
  		data->beep_mask &= ~(1 << bitnr);
  
  	if (bitnr < 8) {
  		reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
  		if (bit)
  			reg |= (1 << bitnr);
  		else
  			reg &= ~(1 << bitnr);
  		w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg);
  	} else if (bitnr < 16) {
  		reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
  		if (bit)
  			reg |= (1 << (bitnr - 8));
  		else
  			reg &= ~(1 << (bitnr - 8));
  		w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg);
  	} else {
  		reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3);
  		if (bit)
  			reg |= (1 << (bitnr - 16));
  		else
  			reg &= ~(1 << (bitnr - 16));
  		w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg);
  	}
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  /* The W83781D has a single beep bit for temp2 and temp3 */
  static ssize_t show_temp3_beep(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
  	int bitnr = (data->type == w83781d) ? 5 : 13;
  	return sprintf(buf, "%u
  ", (data->beep_mask >> bitnr) & 1);
  }
  
  static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 0);
  static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 1);
  static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 2);
  static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 3);
  static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 8);
  static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 9);
  static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 10);
  static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 16);
  static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 17);
  static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 6);
  static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 7);
  static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 11);
  static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 4);
  static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 5);
  static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO,
  			show_temp3_beep, store_beep, 13);
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
595
596
  static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
  			show_beep, store_beep, 15);
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
597

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
598
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
599
  show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
601
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
603
604
  	struct w83781d_data *data = w83781d_update_device(dev);
  	return sprintf(buf, "%ld
  ",
348753379   Jean Delvare   hwmon/w83781d: Us...
605
  		       (long) DIV_FROM_REG(data->fan_div[attr->index]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
606
607
608
609
  }
  
  /* Note: we save and restore the fan minimum here, because its value is
     determined in part by the fan divisor.  This follows the principle of
d6e05edc5   Andreas Mohr   spelling fixes
610
     least surprise; the user doesn't expect the fan minimum to change just
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611
612
     because the divisor changed. */
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
613
614
  store_fan_div(struct device *dev, struct device_attribute *da,
  		const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
615
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
616
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
617
  	struct w83781d_data *data = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
  	unsigned long min;
348753379   Jean Delvare   hwmon/w83781d: Us...
619
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
621
  	u8 reg;
  	unsigned long val = simple_strtoul(buf, NULL, 10);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
622
  	mutex_lock(&data->update_lock);
293c09971   Jean Delvare   hwmon: (w83781d) ...
623

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
625
626
627
628
  	/* Save fan_min */
  	min = FAN_FROM_REG(data->fan_min[nr],
  			   DIV_FROM_REG(data->fan_div[nr]));
  
  	data->fan_div[nr] = DIV_TO_REG(val, data->type);
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
629
  	reg = (w83781d_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
630
631
  	       & (nr==0 ? 0xcf : 0x3f))
  	    | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
632
  	w83781d_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
634
635
  
  	/* w83781d and as99127f don't have extended divisor bits */
  	if (data->type != w83781d && data->type != as99127f) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
636
  		reg = (w83781d_read_value(data, W83781D_REG_VBAT)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637
638
  		       & ~(1 << (5 + nr)))
  		    | ((data->fan_div[nr] & 0x04) << (3 + nr));
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
639
  		w83781d_write_value(data, W83781D_REG_VBAT, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
641
642
643
  	}
  
  	/* Restore fan_min */
  	data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
348753379   Jean Delvare   hwmon/w83781d: Us...
644
  	w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
645

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
646
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
647
648
  	return count;
  }
348753379   Jean Delvare   hwmon/w83781d: Us...
649
650
651
652
653
654
  static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
  		show_fan_div, store_fan_div, 0);
  static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
  		show_fan_div, store_fan_div, 1);
  static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
  		show_fan_div, store_fan_div, 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
657
  show_pwm(struct device *dev, struct device_attribute *da, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
659
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  	struct w83781d_data *data = w83781d_update_device(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
661
662
  	return sprintf(buf, "%d
  ", (int)data->pwm[attr->index]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
664
665
  }
  
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
666
  show_pwm2_enable(struct device *dev, struct device_attribute *da, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
667
668
  {
  	struct w83781d_data *data = w83781d_update_device(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
669
670
  	return sprintf(buf, "%d
  ", (int)data->pwm2_enable);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
  }
  
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
674
675
  store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
  		size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
677
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
678
  	struct w83781d_data *data = dev_get_drvdata(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
679
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
681
682
  	u32 val;
  
  	val = simple_strtoul(buf, NULL, 10);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
683
  	mutex_lock(&data->update_lock);
348753379   Jean Delvare   hwmon/w83781d: Us...
684
685
  	data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
  	w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
686
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
689
690
  	return count;
  }
  
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
691
692
  store_pwm2_enable(struct device *dev, struct device_attribute *da,
  		const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
  {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
694
  	struct w83781d_data *data = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
696
697
  	u32 val, reg;
  
  	val = simple_strtoul(buf, NULL, 10);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
698
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
700
701
702
  
  	switch (val) {
  	case 0:
  	case 1:
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
703
704
  		reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
  		w83781d_write_value(data, W83781D_REG_PWMCLK12,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
705
  				    (reg & 0xf7) | (val << 3));
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
706
707
  		reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
  		w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
  				    (reg & 0xef) | (!val << 4));
348753379   Jean Delvare   hwmon/w83781d: Us...
709
  		data->pwm2_enable = val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
711
712
  		break;
  
  	default:
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
713
  		mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
715
  		return -EINVAL;
  	}
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
716
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
  	return count;
  }
348753379   Jean Delvare   hwmon/w83781d: Us...
719
720
721
722
723
724
725
  static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
  static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
  static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
  static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
  /* only PWM2 can be enabled/disabled */
  static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
  		show_pwm2_enable, store_pwm2_enable);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
726

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
727
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
728
  show_sensor(struct device *dev, struct device_attribute *da, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
730
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
  	struct w83781d_data *data = w83781d_update_device(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
732
733
  	return sprintf(buf, "%d
  ", (int)data->sens[attr->index]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
734
735
736
  }
  
  static ssize_t
348753379   Jean Delvare   hwmon/w83781d: Us...
737
738
  store_sensor(struct device *dev, struct device_attribute *da,
  		const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
  {
348753379   Jean Delvare   hwmon/w83781d: Us...
740
  	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
741
  	struct w83781d_data *data = dev_get_drvdata(dev);
348753379   Jean Delvare   hwmon/w83781d: Us...
742
  	int nr = attr->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743
744
745
  	u32 val, tmp;
  
  	val = simple_strtoul(buf, NULL, 10);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
746
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
748
749
  
  	switch (val) {
  	case 1:		/* PII/Celeron diode */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
750
751
  		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  		w83781d_write_value(data, W83781D_REG_SCFG1,
348753379   Jean Delvare   hwmon/w83781d: Us...
752
  				    tmp | BIT_SCFG1[nr]);
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
753
754
  		tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
  		w83781d_write_value(data, W83781D_REG_SCFG2,
348753379   Jean Delvare   hwmon/w83781d: Us...
755
756
  				    tmp | BIT_SCFG2[nr]);
  		data->sens[nr] = val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
758
  		break;
  	case 2:		/* 3904 */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
759
760
  		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  		w83781d_write_value(data, W83781D_REG_SCFG1,
348753379   Jean Delvare   hwmon/w83781d: Us...
761
  				    tmp | BIT_SCFG1[nr]);
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
762
763
  		tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
  		w83781d_write_value(data, W83781D_REG_SCFG2,
348753379   Jean Delvare   hwmon/w83781d: Us...
764
765
  				    tmp & ~BIT_SCFG2[nr]);
  		data->sens[nr] = val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
  		break;
b26f93309   Jean Delvare   hwmon: Don't expo...
767
768
769
770
771
772
  	case W83781D_DEFAULT_BETA:
  		dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
  			 "instead
  ", W83781D_DEFAULT_BETA);
  		/* fall through */
  	case 4:		/* thermistor */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
773
774
  		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
  		w83781d_write_value(data, W83781D_REG_SCFG1,
348753379   Jean Delvare   hwmon/w83781d: Us...
775
776
  				    tmp & ~BIT_SCFG1[nr]);
  		data->sens[nr] = val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
  		break;
  	default:
b26f93309   Jean Delvare   hwmon: Don't expo...
779
780
781
  		dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4
  ",
  		       (long) val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
  		break;
  	}
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
784
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
786
  	return count;
  }
348753379   Jean Delvare   hwmon/w83781d: Us...
787
788
789
  static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
  	show_sensor, store_sensor, 0);
  static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
393cdad62   Mark M. Hoffman   hwmon: fix w83781...
790
  	show_sensor, store_sensor, 1);
348753379   Jean Delvare   hwmon/w83781d: Us...
791
  static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
393cdad62   Mark M. Hoffman   hwmon: fix w83781...
792
  	show_sensor, store_sensor, 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
796
797
  /* Assumes that adapter is of I2C, not ISA variety.
   * OTHERWISE DON'T CALL THIS
   */
  static int
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
798
  w83781d_detect_subclients(struct i2c_client *new_client)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
799
800
801
  {
  	int i, val1 = 0, id;
  	int err;
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
802
803
804
  	int address = new_client->addr;
  	unsigned short sc_addr[2];
  	struct i2c_adapter *adapter = new_client->adapter;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
  	struct w83781d_data *data = i2c_get_clientdata(new_client);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
806
  	enum chips kind = data->type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
  
  	id = i2c_adapter_id(adapter);
  
  	if (force_subclients[0] == id && force_subclients[1] == address) {
  		for (i = 2; i <= 3; i++) {
  			if (force_subclients[i] < 0x48 ||
  			    force_subclients[i] > 0x4f) {
  				dev_err(&new_client->dev, "Invalid subclient "
  					"address %d; must be 0x48-0x4f
  ",
  					force_subclients[i]);
  				err = -EINVAL;
  				goto ERROR_SC_1;
  			}
  		}
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
822
  		w83781d_write_value(data, W83781D_REG_I2C_SUBADDR,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
  				(force_subclients[2] & 0x07) |
  				((force_subclients[3] & 0x07) << 4));
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
825
  		sc_addr[0] = force_subclients[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
  	} else {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
827
  		val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
828
  		sc_addr[0] = 0x48 + (val1 & 0x07);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
829
830
831
  	}
  
  	if (kind != w83783s) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
833
  		if (force_subclients[0] == id &&
  		    force_subclients[1] == address) {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
834
  			sc_addr[1] = force_subclients[3];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
  		} else {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
836
  			sc_addr[1] = 0x48 + ((val1 >> 4) & 0x07);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
  		}
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
838
  		if (sc_addr[0] == sc_addr[1]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
840
841
  			dev_err(&new_client->dev,
  			       "Duplicate addresses 0x%x for subclients.
  ",
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
842
  			       sc_addr[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
844
845
846
  			err = -EBUSY;
  			goto ERROR_SC_2;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
  	for (i = 0; i <= 1; i++) {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
848
849
  		data->lm75[i] = i2c_new_dummy(adapter, sc_addr[i]);
  		if (!data->lm75[i]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
  			dev_err(&new_client->dev, "Subclient %d "
  				"registration at address 0x%x "
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
852
853
854
  				"failed.
  ", i, sc_addr[i]);
  			err = -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
855
856
857
858
859
860
861
862
863
864
865
866
  			if (i == 1)
  				goto ERROR_SC_3;
  			goto ERROR_SC_2;
  		}
  		if (kind == w83783s)
  			break;
  	}
  
  	return 0;
  
  /* Undo inits in case of errors */
  ERROR_SC_3:
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
867
  	i2c_unregister_device(data->lm75[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
  ERROR_SC_2:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
  ERROR_SC_1:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
870
871
  	return err;
  }
348753379   Jean Delvare   hwmon/w83781d: Us...
872
873
874
  #define IN_UNIT_ATTRS(X)					\
  	&sensor_dev_attr_in##X##_input.dev_attr.attr,		\
  	&sensor_dev_attr_in##X##_min.dev_attr.attr,		\
293c09971   Jean Delvare   hwmon: (w83781d) ...
875
  	&sensor_dev_attr_in##X##_max.dev_attr.attr,		\
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
876
877
  	&sensor_dev_attr_in##X##_alarm.dev_attr.attr,		\
  	&sensor_dev_attr_in##X##_beep.dev_attr.attr
311ce2efb   Jim Cromie   w83781d: Fix unch...
878

348753379   Jean Delvare   hwmon/w83781d: Us...
879
880
881
  #define FAN_UNIT_ATTRS(X)					\
  	&sensor_dev_attr_fan##X##_input.dev_attr.attr,		\
  	&sensor_dev_attr_fan##X##_min.dev_attr.attr,		\
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
882
883
884
  	&sensor_dev_attr_fan##X##_div.dev_attr.attr,		\
  	&sensor_dev_attr_fan##X##_alarm.dev_attr.attr,		\
  	&sensor_dev_attr_fan##X##_beep.dev_attr.attr
311ce2efb   Jim Cromie   w83781d: Fix unch...
885

348753379   Jean Delvare   hwmon/w83781d: Us...
886
887
888
  #define TEMP_UNIT_ATTRS(X)					\
  	&sensor_dev_attr_temp##X##_input.dev_attr.attr,		\
  	&sensor_dev_attr_temp##X##_max.dev_attr.attr,		\
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
889
890
891
  	&sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr,	\
  	&sensor_dev_attr_temp##X##_alarm.dev_attr.attr,		\
  	&sensor_dev_attr_temp##X##_beep.dev_attr.attr
311ce2efb   Jim Cromie   w83781d: Fix unch...
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
  
  static struct attribute* w83781d_attributes[] = {
  	IN_UNIT_ATTRS(0),
  	IN_UNIT_ATTRS(2),
  	IN_UNIT_ATTRS(3),
  	IN_UNIT_ATTRS(4),
  	IN_UNIT_ATTRS(5),
  	IN_UNIT_ATTRS(6),
  	FAN_UNIT_ATTRS(1),
  	FAN_UNIT_ATTRS(2),
  	FAN_UNIT_ATTRS(3),
  	TEMP_UNIT_ATTRS(1),
  	TEMP_UNIT_ATTRS(2),
  	&dev_attr_cpu0_vid.attr,
  	&dev_attr_vrm.attr,
  	&dev_attr_alarms.attr,
  	&dev_attr_beep_mask.attr,
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
909
  	&sensor_dev_attr_beep_enable.dev_attr.attr,
311ce2efb   Jim Cromie   w83781d: Fix unch...
910
911
912
913
914
915
916
917
918
919
920
  	NULL
  };
  static const struct attribute_group w83781d_group = {
  	.attrs = w83781d_attributes,
  };
  
  static struct attribute *w83781d_attributes_opt[] = {
  	IN_UNIT_ATTRS(1),
  	IN_UNIT_ATTRS(7),
  	IN_UNIT_ATTRS(8),
  	TEMP_UNIT_ATTRS(3),
348753379   Jean Delvare   hwmon/w83781d: Us...
921
922
923
924
  	&sensor_dev_attr_pwm1.dev_attr.attr,
  	&sensor_dev_attr_pwm2.dev_attr.attr,
  	&sensor_dev_attr_pwm3.dev_attr.attr,
  	&sensor_dev_attr_pwm4.dev_attr.attr,
311ce2efb   Jim Cromie   w83781d: Fix unch...
925
  	&dev_attr_pwm2_enable.attr,
348753379   Jean Delvare   hwmon/w83781d: Us...
926
927
928
  	&sensor_dev_attr_temp1_type.dev_attr.attr,
  	&sensor_dev_attr_temp2_type.dev_attr.attr,
  	&sensor_dev_attr_temp3_type.dev_attr.attr,
311ce2efb   Jim Cromie   w83781d: Fix unch...
929
930
931
932
933
  	NULL
  };
  static const struct attribute_group w83781d_group_opt = {
  	.attrs = w83781d_attributes_opt,
  };
7666c13c6   Jean Delvare   hwmon/w83781d: No...
934
  /* No clean up is done on error, it's up to the caller */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
  static int
7666c13c6   Jean Delvare   hwmon/w83781d: No...
936
  w83781d_create_files(struct device *dev, int kind, int is_isa)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939

7666c13c6   Jean Delvare   hwmon/w83781d: No...
940
941
942
943
  	if ((err = sysfs_create_group(&dev->kobj, &w83781d_group)))
  		return err;
  
  	if (kind != w83783s) {
348753379   Jean Delvare   hwmon/w83781d: Us...
944
945
946
947
948
  		if ((err = device_create_file(dev,
  				&sensor_dev_attr_in1_input.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in1_min.dev_attr))
  		    || (err = device_create_file(dev,
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
949
950
951
952
953
  				&sensor_dev_attr_in1_max.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in1_alarm.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in1_beep.dev_attr)))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
954
955
956
  			return err;
  	}
  	if (kind != as99127f && kind != w83781d && kind != w83783s) {
348753379   Jean Delvare   hwmon/w83781d: Us...
957
958
959
960
961
962
963
  		if ((err = device_create_file(dev,
  				&sensor_dev_attr_in7_input.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in7_min.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in7_max.dev_attr))
  		    || (err = device_create_file(dev,
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
964
965
966
967
  				&sensor_dev_attr_in7_alarm.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in7_beep.dev_attr))
  		    || (err = device_create_file(dev,
348753379   Jean Delvare   hwmon/w83781d: Us...
968
969
970
971
  				&sensor_dev_attr_in8_input.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in8_min.dev_attr))
  		    || (err = device_create_file(dev,
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
972
973
974
975
976
  				&sensor_dev_attr_in8_max.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in8_alarm.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_in8_beep.dev_attr)))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
977
978
979
  			return err;
  	}
  	if (kind != w83783s) {
348753379   Jean Delvare   hwmon/w83781d: Us...
980
981
982
983
  		if ((err = device_create_file(dev,
  				&sensor_dev_attr_temp3_input.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_temp3_max.dev_attr))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
984
  		    || (err = device_create_file(dev,
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
985
986
987
988
989
  				&sensor_dev_attr_temp3_max_hyst.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_temp3_alarm.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_temp3_beep.dev_attr)))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
990
  			return err;
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
991

7768aa769   Jean Delvare   hwmon: (w83781d) ...
992
  		if (kind != w83781d) {
7d4a13749   Jean Delvare   hwmon: (w83781d) ...
993
994
995
996
997
  			err = sysfs_chmod_file(&dev->kobj,
  				&sensor_dev_attr_temp3_alarm.dev_attr.attr,
  				S_IRUGO | S_IWUSR);
  			if (err)
  				return err;
7768aa769   Jean Delvare   hwmon: (w83781d) ...
998
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
  	}
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1000
  	if (kind != w83781d && kind != as99127f) {
348753379   Jean Delvare   hwmon/w83781d: Us...
1001
1002
1003
1004
  		if ((err = device_create_file(dev,
  				&sensor_dev_attr_pwm1.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_pwm2.dev_attr))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1005
1006
  		    || (err = device_create_file(dev, &dev_attr_pwm2_enable)))
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
  	}
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1008
  	if (kind == w83782d && !is_isa) {
348753379   Jean Delvare   hwmon/w83781d: Us...
1009
1010
1011
1012
  		if ((err = device_create_file(dev,
  				&sensor_dev_attr_pwm3.dev_attr))
  		    || (err = device_create_file(dev,
  				&sensor_dev_attr_pwm4.dev_attr)))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1013
1014
1015
1016
  			return err;
  	}
  
  	if (kind != as99127f && kind != w83781d) {
348753379   Jean Delvare   hwmon/w83781d: Us...
1017
1018
  		if ((err = device_create_file(dev,
  				&sensor_dev_attr_temp1_type.dev_attr))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1019
  		    || (err = device_create_file(dev,
348753379   Jean Delvare   hwmon/w83781d: Us...
1020
  				&sensor_dev_attr_temp2_type.dev_attr)))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1021
1022
1023
  			return err;
  		if (kind != w83783s) {
  			if ((err = device_create_file(dev,
348753379   Jean Delvare   hwmon/w83781d: Us...
1024
  					&sensor_dev_attr_temp3_type.dev_attr)))
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1025
  				return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1026
  		}
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1027
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028

7666c13c6   Jean Delvare   hwmon/w83781d: No...
1029
1030
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1032
  /* Return 0 if detection is successful, -ENODEV otherwise */
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1033
  static int
310ec7921   Jean Delvare   i2c: Drop the kin...
1034
  w83781d_detect(struct i2c_client *client, struct i2c_board_info *info)
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1035
  {
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1036
  	int val1, val2;
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1037
1038
1039
  	struct w83781d_data *isa = w83781d_data_if_isa();
  	struct i2c_adapter *adapter = client->adapter;
  	int address = client->addr;
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1040
  	const char *client_name;
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1041
  	enum vendor { winbond, asus } vendid;
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1042
1043
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1045
1046
1047
1048
1049
  	/* We block updates of the ISA device to minimize the risk of
  	   concurrent access to the same W83781D chip through different
  	   interfaces. */
  	if (isa)
  		mutex_lock(&isa->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1050

bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
  	if (i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG) & 0x80) {
  		dev_dbg(&adapter->dev,
  			"Detection of w83781d chip failed at step 3
  ");
  		goto err_nodev;
  	}
  
  	val1 = i2c_smbus_read_byte_data(client, W83781D_REG_BANK);
  	val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
  	/* Check for Winbond or Asus ID if in bank 0 */
  	if (!(val1 & 0x07) &&
  	    ((!(val1 & 0x80) && val2 != 0xa3 && val2 != 0xc3) ||
  	     ( (val1 & 0x80) && val2 != 0x5c && val2 != 0x12))) {
  		dev_dbg(&adapter->dev,
  			"Detection of w83781d chip failed at step 4
  ");
  		goto err_nodev;
  	}
  	/* If Winbond SMBus, check address at 0x48.
  	   Asus doesn't support, except for as99127f rev.2 */
  	if ((!(val1 & 0x80) && val2 == 0xa3) ||
  	    ( (val1 & 0x80) && val2 == 0x5c)) {
  		if (i2c_smbus_read_byte_data(client, W83781D_REG_I2C_ADDR)
  		    != address) {
  			dev_dbg(&adapter->dev,
  				"Detection of w83781d chip failed at step 5
  ");
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1078
  			goto err_nodev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1079
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
  	}
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1081
  	/* Put it now into bank 0 and Vendor ID High Byte */
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1082
1083
1084
  	i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
  		(i2c_smbus_read_byte_data(client, W83781D_REG_BANK)
  		 & 0x78) | 0x80);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1085

bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  	/* Get the vendor ID */
  	val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
  	if (val2 == 0x5c)
  		vendid = winbond;
  	else if (val2 == 0x12)
  		vendid = asus;
  	else {
  		dev_dbg(&adapter->dev,
  			"w83781d chip vendor is neither Winbond nor Asus
  ");
  		goto err_nodev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1097
  	}
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1098
1099
1100
  	/* Determine the chip type. */
  	val1 = i2c_smbus_read_byte_data(client, W83781D_REG_WCHIPID);
  	if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1101
  		client_name = "w83781d";
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1102
  	else if (val1 == 0x30 && vendid == winbond)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1103
  		client_name = "w83782d";
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1104
  	else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1105
  		client_name = "w83783s";
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1106
  	else if (val1 == 0x31)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1107
  		client_name = "as99127f";
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1108
1109
1110
1111
1112
1113
1114
1115
  	else
  		goto err_nodev;
  
  	if (val1 <= 0x30 && w83781d_alias_detect(client, val1)) {
  		dev_dbg(&adapter->dev, "Device at 0x%02x appears to "
  			"be the same as ISA device
  ", address);
  		goto err_nodev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1116
  	}
bab2bf44f   Jean Delvare   hwmon: (w83781d) ...
1117
1118
  	if (isa)
  		mutex_unlock(&isa->update_lock);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
  	strlcpy(info->type, client_name, I2C_NAME_SIZE);
  
  	return 0;
  
   err_nodev:
  	if (isa)
  		mutex_unlock(&isa->update_lock);
  	return -ENODEV;
  }
  
  static int
  w83781d_probe(struct i2c_client *client, const struct i2c_device_id *id)
  {
  	struct device *dev = &client->dev;
  	struct w83781d_data *data;
  	int err;
  
  	data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL);
  	if (!data) {
  		err = -ENOMEM;
  		goto ERROR1;
  	}
  
  	i2c_set_clientdata(client, data);
  	mutex_init(&data->lock);
  	mutex_init(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1145

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1146
1147
  	data->type = id->driver_data;
  	data->client = client;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1148
1149
  
  	/* attach secondary i2c lm75-like clients */
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1150
1151
  	err = w83781d_detect_subclients(client);
  	if (err)
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1152
  		goto ERROR3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
1154
  
  	/* Initialize the chip */
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1155
  	w83781d_init_device(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1156
1157
  
  	/* Register sysfs hooks */
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1158
  	err = w83781d_create_files(dev, data->type, 0);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1159
  	if (err)
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
1160
  		goto ERROR4;
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
1161

1beeffe43   Tony Jones   hwmon: Convert fr...
1162
1163
1164
  	data->hwmon_dev = hwmon_device_register(dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
311ce2efb   Jim Cromie   w83781d: Fix unch...
1165
  		goto ERROR4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
1167
1168
  	}
  
  	return 0;
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
1169
  ERROR4:
311ce2efb   Jim Cromie   w83781d: Fix unch...
1170
1171
  	sysfs_remove_group(&dev->kobj, &w83781d_group);
  	sysfs_remove_group(&dev->kobj, &w83781d_group_opt);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1172
1173
1174
1175
  	if (data->lm75[0])
  		i2c_unregister_device(data->lm75[0]);
  	if (data->lm75[1])
  		i2c_unregister_device(data->lm75[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1176
  ERROR3:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1177
1178
  	kfree(data);
  ERROR1:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1179
1180
1181
1182
  	return err;
  }
  
  static int
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1183
  w83781d_remove(struct i2c_client *client)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1184
  {
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
1185
  	struct w83781d_data *data = i2c_get_clientdata(client);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1186
  	struct device *dev = &client->dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1187

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1188
  	hwmon_device_unregister(data->hwmon_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1190
1191
  	sysfs_remove_group(&dev->kobj, &w83781d_group);
  	sysfs_remove_group(&dev->kobj, &w83781d_group_opt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1192

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1193
1194
1195
1196
  	if (data->lm75[0])
  		i2c_unregister_device(data->lm75[0]);
  	if (data->lm75[1])
  		i2c_unregister_device(data->lm75[1]);
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
1197

0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1198
  	kfree(data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1199
1200
1201
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1202
  static int
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1203
  w83781d_read_value_i2c(struct w83781d_data *data, u16 reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1204
  {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1205
  	struct i2c_client *client = data->client;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1206
  	int res, bank;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1207
  	struct i2c_client *cl;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1208
1209
1210
1211
1212
1213
1214
  	bank = (reg >> 8) & 0x0f;
  	if (bank > 2)
  		/* switch banks */
  		i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
  					  bank);
  	if (bank == 0 || bank > 2) {
  		res = i2c_smbus_read_byte_data(client, reg & 0xff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1215
  	} else {
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1216
1217
1218
1219
1220
  		/* switch to subclient */
  		cl = data->lm75[bank - 1];
  		/* convert from ISA to LM75 I2C addresses */
  		switch (reg & 0xff) {
  		case 0x50:	/* TEMP */
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
1221
  			res = i2c_smbus_read_word_swapped(cl, 0);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1222
1223
1224
1225
1226
  			break;
  		case 0x52:	/* CONFIG */
  			res = i2c_smbus_read_byte_data(cl, 1);
  			break;
  		case 0x53:	/* HYST */
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
1227
  			res = i2c_smbus_read_word_swapped(cl, 2);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1228
1229
1230
  			break;
  		case 0x55:	/* OVER */
  		default:
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
1231
  			res = i2c_smbus_read_word_swapped(cl, 3);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1232
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1233
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234
  	}
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1235
1236
  	if (bank > 2)
  		i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1237
1238
1239
1240
  	return res;
  }
  
  static int
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1241
  w83781d_write_value_i2c(struct w83781d_data *data, u16 reg, u16 value)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1242
  {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1243
  	struct i2c_client *client = data->client;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1244
  	int bank;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1245
  	struct i2c_client *cl;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1246
1247
1248
1249
1250
1251
1252
1253
  	bank = (reg >> 8) & 0x0f;
  	if (bank > 2)
  		/* switch banks */
  		i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
  					  bank);
  	if (bank == 0 || bank > 2) {
  		i2c_smbus_write_byte_data(client, reg & 0xff,
  					  value & 0xff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1254
  	} else {
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1255
1256
1257
1258
1259
1260
1261
1262
  		/* switch to subclient */
  		cl = data->lm75[bank - 1];
  		/* convert from ISA to LM75 I2C addresses */
  		switch (reg & 0xff) {
  		case 0x52:	/* CONFIG */
  			i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  			break;
  		case 0x53:	/* HYST */
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
1263
  			i2c_smbus_write_word_swapped(cl, 2, value);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1264
1265
  			break;
  		case 0x55:	/* OVER */
90f4102ce   Jean Delvare   hwmon: Use i2c_sm...
1266
  			i2c_smbus_write_word_swapped(cl, 3, value);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1267
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1268
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1269
  	}
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1270
1271
  	if (bank > 2)
  		i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1272
1273
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1274
  static void
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1275
  w83781d_init_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1277
  	struct w83781d_data *data = dev_get_drvdata(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
1279
1280
  	int i, p;
  	int type = data->type;
  	u8 tmp;
fabddcd49   Jean Delvare   [PATCH] w83781d: ...
1281
  	if (reset && type != as99127f) { /* this resets registers we don't have
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1282
  					   documentation for on the as99127f */
fabddcd49   Jean Delvare   [PATCH] w83781d: ...
1283
1284
1285
1286
1287
1288
  		/* Resetting the chip has been the default for a long time,
  		   but it causes the BIOS initializations (fan clock dividers,
  		   thermal sensor types...) to be lost, so it is now optional.
  		   It might even go away if nobody reports it as being useful,
  		   as I see very little reason why this would be needed at
  		   all. */
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1289
  		dev_info(dev, "If reset=1 solved a problem you were "
fabddcd49   Jean Delvare   [PATCH] w83781d: ...
1290
1291
  			 "having, please report!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1292
  		/* save these registers */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1293
1294
  		i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
  		p = w83781d_read_value(data, W83781D_REG_PWMCLK12);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1295
1296
  		/* Reset all except Watchdog values and last conversion values
  		   This sets fan-divs to 2, among others */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1297
  		w83781d_write_value(data, W83781D_REG_CONFIG, 0x80);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1298
1299
  		/* Restore the registers and disable power-on abnormal beep.
  		   This saves FAN 1/2/3 input/output values set by BIOS. */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1300
1301
  		w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
  		w83781d_write_value(data, W83781D_REG_PWMCLK12, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1302
1303
1304
  		/* Disable master beep-enable (reset turns it on).
  		   Individual beep_mask should be reset to off but for some reason
  		   disabling this bit helps some people not get beeped */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1305
  		w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1306
  	}
fabddcd49   Jean Delvare   [PATCH] w83781d: ...
1307
1308
1309
  	/* Disable power-on abnormal beep, as advised by the datasheet.
  	   Already done if reset=1. */
  	if (init && !reset && type != as99127f) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1310
1311
  		i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
  		w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
fabddcd49   Jean Delvare   [PATCH] w83781d: ...
1312
  	}
303760b44   Jean Delvare   [PATCH] hwmon: hw...
1313
  	data->vrm = vid_which_vrm();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1314
1315
  
  	if ((type != w83781d) && (type != as99127f)) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1316
  		tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1317
1318
  		for (i = 1; i <= 3; i++) {
  			if (!(tmp & BIT_SCFG1[i - 1])) {
b26f93309   Jean Delvare   hwmon: Don't expo...
1319
  				data->sens[i - 1] = 4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1320
1321
  			} else {
  				if (w83781d_read_value
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1322
  				    (data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1323
1324
1325
1326
1327
  				     W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
  					data->sens[i - 1] = 1;
  				else
  					data->sens[i - 1] = 2;
  			}
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1328
  			if (type == w83783s && i == 2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1329
1330
1331
1332
1333
1334
  				break;
  		}
  	}
  
  	if (init && type != as99127f) {
  		/* Enable temp2 */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1335
  		tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336
  		if (tmp & 0x01) {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1337
  			dev_warn(dev, "Enabling temp2, readings "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1338
1339
  				 "might not make sense
  ");
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1340
  			w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1341
1342
1343
1344
  				tmp & 0xfe);
  		}
  
  		/* Enable temp3 */
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1345
  		if (type != w83783s) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1346
  			tmp = w83781d_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1347
1348
  				W83781D_REG_TEMP3_CONFIG);
  			if (tmp & 0x01) {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1349
  				dev_warn(dev, "Enabling temp3, "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1350
1351
  					 "readings might not make sense
  ");
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1352
  				w83781d_write_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1353
1354
1355
  					W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1356
1357
1358
  	}
  
  	/* Start monitoring */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1359
1360
  	w83781d_write_value(data, W83781D_REG_CONFIG,
  			    (w83781d_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1361
1362
  						W83781D_REG_CONFIG) & 0xf7)
  			    | 0x01);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1363
1364
  
  	/* A few vars need to be filled upon startup */
348753379   Jean Delvare   hwmon/w83781d: Us...
1365
1366
  	for (i = 0; i < 3; i++) {
  		data->fan_min[i] = w83781d_read_value(data,
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1367
1368
  					W83781D_REG_FAN_MIN(i));
  	}
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1369
1370
  
  	mutex_init(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1371
1372
1373
1374
  }
  
  static struct w83781d_data *w83781d_update_device(struct device *dev)
  {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1375
  	struct w83781d_data *data = dev_get_drvdata(dev);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1376
  	struct i2c_client *client = data->client;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1377
  	int i;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
1378
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1379
1380
1381
1382
1383
1384
1385
  
  	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  	    || !data->valid) {
  		dev_dbg(dev, "Starting device update
  ");
  
  		for (i = 0; i <= 8; i++) {
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1386
  			if (data->type == w83783s && i == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1387
1388
  				continue;	/* 783S has no in1 */
  			data->in[i] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1389
  			    w83781d_read_value(data, W83781D_REG_IN(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1390
  			data->in_min[i] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1391
  			    w83781d_read_value(data, W83781D_REG_IN_MIN(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
  			data->in_max[i] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1393
  			    w83781d_read_value(data, W83781D_REG_IN_MAX(i));
05663368d   Jean Delvare   hwmon: (w83781d) ...
1394
  			if ((data->type != w83782d) && (i == 6))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1395
1396
  				break;
  		}
348753379   Jean Delvare   hwmon/w83781d: Us...
1397
1398
  		for (i = 0; i < 3; i++) {
  			data->fan[i] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1399
  			    w83781d_read_value(data, W83781D_REG_FAN(i));
348753379   Jean Delvare   hwmon/w83781d: Us...
1400
  			data->fan_min[i] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1401
  			    w83781d_read_value(data, W83781D_REG_FAN_MIN(i));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1402
1403
  		}
  		if (data->type != w83781d && data->type != as99127f) {
348753379   Jean Delvare   hwmon/w83781d: Us...
1404
1405
  			for (i = 0; i < 4; i++) {
  				data->pwm[i] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1406
  				    w83781d_read_value(data,
348753379   Jean Delvare   hwmon/w83781d: Us...
1407
  						       W83781D_REG_PWM[i]);
848ddf116   Jean Delvare   hwmon: (w83781d) ...
1408
1409
  				/* Only W83782D on SMBus has PWM3 and PWM4 */
  				if ((data->type != w83782d || !client)
348753379   Jean Delvare   hwmon/w83781d: Us...
1410
  				    && i == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1411
1412
1413
  					break;
  			}
  			/* Only PWM2 can be disabled */
348753379   Jean Delvare   hwmon/w83781d: Us...
1414
  			data->pwm2_enable = (w83781d_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1415
1416
  					      W83781D_REG_PWMCLK12) & 0x08) >> 3;
  		}
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1417
  		data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1418
  		data->temp_max =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1419
  		    w83781d_read_value(data, W83781D_REG_TEMP_OVER(1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1420
  		data->temp_max_hyst =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1421
  		    w83781d_read_value(data, W83781D_REG_TEMP_HYST(1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1422
  		data->temp_add[0] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1423
  		    w83781d_read_value(data, W83781D_REG_TEMP(2));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1424
  		data->temp_max_add[0] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1425
  		    w83781d_read_value(data, W83781D_REG_TEMP_OVER(2));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1426
  		data->temp_max_hyst_add[0] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1427
  		    w83781d_read_value(data, W83781D_REG_TEMP_HYST(2));
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1428
  		if (data->type != w83783s) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1429
  			data->temp_add[1] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1430
  			    w83781d_read_value(data, W83781D_REG_TEMP(3));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1431
  			data->temp_max_add[1] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1432
  			    w83781d_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1433
1434
  					       W83781D_REG_TEMP_OVER(3));
  			data->temp_max_hyst_add[1] =
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1435
  			    w83781d_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1436
1437
  					       W83781D_REG_TEMP_HYST(3));
  		}
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1438
  		i = w83781d_read_value(data, W83781D_REG_VID_FANDIV);
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1439
  		data->vid = i & 0x0f;
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1440
  		data->vid |= (w83781d_read_value(data,
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1441
  					W83781D_REG_CHIPID) & 0x01) << 4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1442
1443
  		data->fan_div[0] = (i >> 4) & 0x03;
  		data->fan_div[1] = (i >> 6) & 0x03;
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1444
  		data->fan_div[2] = (w83781d_read_value(data,
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1445
  					W83781D_REG_PIN) >> 6) & 0x03;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1446
  		if ((data->type != w83781d) && (data->type != as99127f)) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1447
  			i = w83781d_read_value(data, W83781D_REG_VBAT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1448
1449
  			data->fan_div[0] |= (i >> 3) & 0x04;
  			data->fan_div[1] |= (i >> 4) & 0x04;
7c7a53046   Jean Delvare   [PATCH] I2C: w837...
1450
  			data->fan_div[2] |= (i >> 5) & 0x04;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1451
  		}
05663368d   Jean Delvare   hwmon: (w83781d) ...
1452
  		if (data->type == w83782d) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1453
  			data->alarms = w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1454
  						W83782D_REG_ALARM1)
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1455
  				     | (w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1456
  						W83782D_REG_ALARM2) << 8)
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1457
  				     | (w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1458
1459
  						W83782D_REG_ALARM3) << 16);
  		} else if (data->type == w83783s) {
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1460
  			data->alarms = w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1461
  						W83782D_REG_ALARM1)
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1462
  				     | (w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1463
1464
1465
1466
  						W83782D_REG_ALARM2) << 8);
  		} else {
  			/* No real-time status registers, fall back to
  			   interrupt status registers */
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1467
  			data->alarms = w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1468
  						W83781D_REG_ALARM1)
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1469
  				     | (w83781d_read_value(data,
c7f5d7edd   Jean Delvare   [PATCH] w83781d: ...
1470
  						W83781D_REG_ALARM2) << 8);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1471
  		}
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1472
  		i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
2fbbbf148   Jean Delvare   hwmon: (w83781d) ...
1473
  		data->beep_mask = (i << 8) +
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1474
  		    w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1475
1476
  		if ((data->type != w83781d) && (data->type != as99127f)) {
  			data->beep_mask |=
31b8dc4d5   Jean Delvare   hwmon/w83781d: Be...
1477
  			    w83781d_read_value(data,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1478
1479
1480
1481
1482
  					       W83781D_REG_BEEP_INTS3) << 16;
  		}
  		data->last_updated = jiffies;
  		data->valid = 1;
  	}
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
1483
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484
1485
1486
  
  	return data;
  }
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
  static const struct i2c_device_id w83781d_ids[] = {
  	{ "w83781d", w83781d, },
  	{ "w83782d", w83782d, },
  	{ "w83783s", w83783s, },
  	{ "as99127f", as99127f },
  	{ /* LIST END */ }
  };
  MODULE_DEVICE_TABLE(i2c, w83781d_ids);
  
  static struct i2c_driver w83781d_driver = {
  	.class		= I2C_CLASS_HWMON,
  	.driver = {
  		.name = "w83781d",
  	},
  	.probe		= w83781d_probe,
  	.remove		= w83781d_remove,
  	.id_table	= w83781d_ids,
  	.detect		= w83781d_detect,
c3813d6af   Jean Delvare   i2c: Get rid of s...
1505
  	.address_list	= normal_i2c,
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1506
1507
1508
1509
1510
  };
  
  /*
   * ISA related code
   */
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
  #ifdef CONFIG_ISA
  
  /* ISA device, if found */
  static struct platform_device *pdev;
  
  static unsigned short isa_address = 0x290;
  
  /* I2C devices get this name attribute automatically, but for ISA devices
     we must create it by ourselves. */
  static ssize_t
  show_name(struct device *dev, struct device_attribute *devattr, char *buf)
  {
  	struct w83781d_data *data = dev_get_drvdata(dev);
360782dde   Jean Delvare   hwmon: (w83781d) ...
1524
1525
  	return sprintf(buf, "%s
  ", data->name);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
  }
  static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  
  static struct w83781d_data *w83781d_data_if_isa(void)
  {
  	return pdev ? platform_get_drvdata(pdev) : NULL;
  }
  
  /* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
  static int w83781d_alias_detect(struct i2c_client *client, u8 chipid)
  {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1537
  	struct w83781d_data *isa;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1538
1539
1540
1541
  	int i;
  
  	if (!pdev)	/* No ISA chip */
  		return 0;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
  	isa = platform_get_drvdata(pdev);
  
  	if (w83781d_read_value(isa, W83781D_REG_I2C_ADDR) != client->addr)
  		return 0;	/* Address doesn't match */
  	if (w83781d_read_value(isa, W83781D_REG_WCHIPID) != chipid)
  		return 0;	/* Chip type doesn't match */
  
  	/* We compare all the limit registers, the config register and the
  	 * interrupt mask registers */
  	for (i = 0x2b; i <= 0x3d; i++) {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1552
1553
  		if (w83781d_read_value(isa, i) !=
  		    i2c_smbus_read_byte_data(client, i))
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1554
1555
1556
  			return 0;
  	}
  	if (w83781d_read_value(isa, W83781D_REG_CONFIG) !=
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1557
  	    i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG))
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1558
1559
  		return 0;
  	for (i = 0x43; i <= 0x46; i++) {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1560
1561
  		if (w83781d_read_value(isa, i) !=
  		    i2c_smbus_read_byte_data(client, i))
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1562
1563
1564
1565
1566
1567
1568
1569
1570
  			return 0;
  	}
  
  	return 1;
  }
  
  static int
  w83781d_read_value_isa(struct w83781d_data *data, u16 reg)
  {
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1571
1572
1573
1574
1575
1576
1577
1578
1579
  	int word_sized, res;
  
  	word_sized = (((reg & 0xff00) == 0x100)
  		      || ((reg & 0xff00) == 0x200))
  	    && (((reg & 0x00ff) == 0x50)
  		|| ((reg & 0x00ff) == 0x53)
  		|| ((reg & 0x00ff) == 0x55));
  	if (reg & 0xff00) {
  		outb_p(W83781D_REG_BANK,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1580
  		       data->isa_addr + W83781D_ADDR_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1581
  		outb_p(reg >> 8,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1582
  		       data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1583
  	}
360782dde   Jean Delvare   hwmon: (w83781d) ...
1584
1585
  	outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
  	res = inb_p(data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1586
1587
  	if (word_sized) {
  		outb_p((reg & 0xff) + 1,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1588
  		       data->isa_addr + W83781D_ADDR_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1589
  		res =
360782dde   Jean Delvare   hwmon: (w83781d) ...
1590
  		    (res << 8) + inb_p(data->isa_addr +
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1591
1592
1593
1594
  				       W83781D_DATA_REG_OFFSET);
  	}
  	if (reg & 0xff00) {
  		outb_p(W83781D_REG_BANK,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1595
1596
  		       data->isa_addr + W83781D_ADDR_REG_OFFSET);
  		outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1597
1598
1599
1600
1601
1602
1603
  	}
  	return res;
  }
  
  static void
  w83781d_write_value_isa(struct w83781d_data *data, u16 reg, u16 value)
  {
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1604
1605
1606
1607
1608
1609
1610
1611
  	int word_sized;
  
  	word_sized = (((reg & 0xff00) == 0x100)
  		      || ((reg & 0xff00) == 0x200))
  	    && (((reg & 0x00ff) == 0x53)
  		|| ((reg & 0x00ff) == 0x55));
  	if (reg & 0xff00) {
  		outb_p(W83781D_REG_BANK,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1612
  		       data->isa_addr + W83781D_ADDR_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1613
  		outb_p(reg >> 8,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1614
  		       data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1615
  	}
360782dde   Jean Delvare   hwmon: (w83781d) ...
1616
  	outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1617
1618
  	if (word_sized) {
  		outb_p(value >> 8,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1619
  		       data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1620
  		outb_p((reg & 0xff) + 1,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1621
  		       data->isa_addr + W83781D_ADDR_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1622
  	}
360782dde   Jean Delvare   hwmon: (w83781d) ...
1623
  	outb_p(value & 0xff, data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1624
1625
  	if (reg & 0xff00) {
  		outb_p(W83781D_REG_BANK,
360782dde   Jean Delvare   hwmon: (w83781d) ...
1626
1627
  		       data->isa_addr + W83781D_ADDR_REG_OFFSET);
  		outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
  	}
  }
  
  /* The SMBus locks itself, usually, but nothing may access the Winbond between
     bank switches. ISA access must always be locked explicitly!
     We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
     would slow down the W83781D access and should not be necessary.
     There are some ugly typecasts here, but the good news is - they should
     nowhere else be necessary! */
  static int
  w83781d_read_value(struct w83781d_data *data, u16 reg)
  {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1640
  	struct i2c_client *client = data->client;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1641
1642
1643
  	int res;
  
  	mutex_lock(&data->lock);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1644
  	if (client)
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
  		res = w83781d_read_value_i2c(data, reg);
  	else
  		res = w83781d_read_value_isa(data, reg);
  	mutex_unlock(&data->lock);
  	return res;
  }
  
  static int
  w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
  {
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1655
  	struct i2c_client *client = data->client;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1656
1657
  
  	mutex_lock(&data->lock);
0217eae3a   Wolfgang Grandegger   hwmon: (w83781d) ...
1658
  	if (client)
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
  		w83781d_write_value_i2c(data, reg, value);
  	else
  		w83781d_write_value_isa(data, reg, value);
  	mutex_unlock(&data->lock);
  	return 0;
  }
  
  static int __devinit
  w83781d_isa_probe(struct platform_device *pdev)
  {
  	int err, reg;
  	struct w83781d_data *data;
  	struct resource *res;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
  
  	/* Reserve the ISA region */
  	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  	if (!request_region(res->start + W83781D_ADDR_REG_OFFSET, 2,
  			    "w83781d")) {
  		err = -EBUSY;
  		goto exit;
  	}
  
  	data = kzalloc(sizeof(struct w83781d_data), GFP_KERNEL);
  	if (!data) {
  		err = -ENOMEM;
  		goto exit_release_region;
  	}
  	mutex_init(&data->lock);
360782dde   Jean Delvare   hwmon: (w83781d) ...
1687
  	data->isa_addr = res->start;
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1688
1689
1690
1691
1692
1693
  	platform_set_drvdata(pdev, data);
  
  	reg = w83781d_read_value(data, W83781D_REG_WCHIPID);
  	switch (reg) {
  	case 0x30:
  		data->type = w83782d;
360782dde   Jean Delvare   hwmon: (w83781d) ...
1694
  		data->name = "w83782d";
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1695
1696
1697
  		break;
  	default:
  		data->type = w83781d;
360782dde   Jean Delvare   hwmon: (w83781d) ...
1698
  		data->name = "w83781d";
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1699
  	}
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
  
  	/* Initialize the W83781D chip */
  	w83781d_init_device(&pdev->dev);
  
  	/* Register sysfs hooks */
  	err = w83781d_create_files(&pdev->dev, data->type, 1);
  	if (err)
  		goto exit_remove_files;
  
  	err = device_create_file(&pdev->dev, &dev_attr_name);
  	if (err)
  		goto exit_remove_files;
  
  	data->hwmon_dev = hwmon_device_register(&pdev->dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
  		goto exit_remove_files;
  	}
  
  	return 0;
  
   exit_remove_files:
  	sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
  	sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
  	device_remove_file(&pdev->dev, &dev_attr_name);
  	kfree(data);
   exit_release_region:
  	release_region(res->start + W83781D_ADDR_REG_OFFSET, 2);
   exit:
  	return err;
  }
  
  static int __devexit
  w83781d_isa_remove(struct platform_device *pdev)
  {
  	struct w83781d_data *data = platform_get_drvdata(pdev);
  
  	hwmon_device_unregister(data->hwmon_dev);
  	sysfs_remove_group(&pdev->dev.kobj, &w83781d_group);
  	sysfs_remove_group(&pdev->dev.kobj, &w83781d_group_opt);
  	device_remove_file(&pdev->dev, &dev_attr_name);
360782dde   Jean Delvare   hwmon: (w83781d) ...
1741
  	release_region(data->isa_addr + W83781D_ADDR_REG_OFFSET, 2);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
  	kfree(data);
  
  	return 0;
  }
  
  static struct platform_driver w83781d_isa_driver = {
  	.driver = {
  		.owner = THIS_MODULE,
  		.name = "w83781d",
  	},
  	.probe = w83781d_isa_probe,
  	.remove = __devexit_p(w83781d_isa_remove),
  };
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1755
1756
1757
1758
1759
  /* return 1 if a supported chip is found, 0 otherwise */
  static int __init
  w83781d_isa_found(unsigned short address)
  {
  	int val, save, found = 0;
b0bcdd3cd   Jean Delvare   hwmon: (w83781d) ...
1760
1761
1762
1763
1764
1765
1766
  	int port;
  
  	/* Some boards declare base+0 to base+7 as a PNP device, some base+4
  	 * to base+7 and some base+5 to base+6. So we better request each port
  	 * individually for the probing phase. */
  	for (port = address; port < address + W83781D_EXTENT; port++) {
  		if (!request_region(port, 1, "w83781d")) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1767
1768
  			pr_debug("Failed to request port 0x%x
  ", port);
b0bcdd3cd   Jean Delvare   hwmon: (w83781d) ...
1769
1770
  			goto release;
  		}
2961cb22e   Jean Delvare   hwmon: (w83781d) ...
1771
  	}
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1772
1773
1774
1775
1776
1777
1778
1779
  
  #define REALLY_SLOW_IO
  	/* We need the timeouts for at least some W83781D-like
  	   chips. But only if we read 'undefined' registers. */
  	val = inb_p(address + 1);
  	if (inb_p(address + 2) != val
  	 || inb_p(address + 3) != val
  	 || inb_p(address + 7) != val) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1780
1781
  		pr_debug("Detection failed at step %d
  ", 1);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1782
1783
1784
1785
1786
1787
1788
1789
  		goto release;
  	}
  #undef REALLY_SLOW_IO
  
  	/* We should be able to change the 7 LSB of the address port. The
  	   MSB (busy flag) should be clear initially, set after the write. */
  	save = inb_p(address + W83781D_ADDR_REG_OFFSET);
  	if (save & 0x80) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1790
1791
  		pr_debug("Detection failed at step %d
  ", 2);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1792
1793
1794
1795
1796
1797
  		goto release;
  	}
  	val = ~save & 0x7f;
  	outb_p(val, address + W83781D_ADDR_REG_OFFSET);
  	if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
  		outb_p(save, address + W83781D_ADDR_REG_OFFSET);
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1798
1799
  		pr_debug("Detection failed at step %d
  ", 3);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1800
1801
1802
1803
1804
1805
1806
  		goto release;
  	}
  
  	/* We found a device, now see if it could be a W83781D */
  	outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
  	val = inb_p(address + W83781D_DATA_REG_OFFSET);
  	if (val & 0x80) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1807
1808
  		pr_debug("Detection failed at step %d
  ", 4);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1809
1810
1811
1812
1813
1814
1815
1816
  		goto release;
  	}
  	outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
  	save = inb_p(address + W83781D_DATA_REG_OFFSET);
  	outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
  	val = inb_p(address + W83781D_DATA_REG_OFFSET);
  	if ((!(save & 0x80) && (val != 0xa3))
  	 || ((save & 0x80) && (val != 0x5c))) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1817
1818
  		pr_debug("Detection failed at step %d
  ", 5);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1819
1820
1821
1822
1823
  		goto release;
  	}
  	outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
  	val = inb_p(address + W83781D_DATA_REG_OFFSET);
  	if (val < 0x03 || val > 0x77) {	/* Not a valid I2C address */
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1824
1825
  		pr_debug("Detection failed at step %d
  ", 6);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1826
1827
1828
1829
1830
  		goto release;
  	}
  
  	/* The busy flag should be clear again */
  	if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1831
1832
  		pr_debug("Detection failed at step %d
  ", 7);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
  		goto release;
  	}
  
  	/* Determine the chip type */
  	outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
  	save = inb_p(address + W83781D_DATA_REG_OFFSET);
  	outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
  	outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
  	val = inb_p(address + W83781D_DATA_REG_OFFSET);
  	if ((val & 0xfe) == 0x10	/* W83781D */
05663368d   Jean Delvare   hwmon: (w83781d) ...
1843
  	 || val == 0x30)		/* W83782D */
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1844
1845
1846
  		found = 1;
  
  	if (found)
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1847
1848
  		pr_info("Found a %s chip at %#x
  ",
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1849
1850
1851
  			val == 0x30 ? "W83782D" : "W83781D", (int)address);
  
   release:
b0bcdd3cd   Jean Delvare   hwmon: (w83781d) ...
1852
1853
  	for (port--; port >= address; port--)
  		release_region(port, 1);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1854
1855
1856
1857
1858
1859
1860
1861
  	return found;
  }
  
  static int __init
  w83781d_isa_device_add(unsigned short address)
  {
  	struct resource res = {
  		.start	= address,
15bde2f1a   Jean Delvare   hwmon: End of I/O...
1862
  		.end	= address + W83781D_EXTENT - 1,
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1863
1864
1865
1866
1867
1868
1869
1870
  		.name	= "w83781d",
  		.flags	= IORESOURCE_IO,
  	};
  	int err;
  
  	pdev = platform_device_alloc("w83781d", address);
  	if (!pdev) {
  		err = -ENOMEM;
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1871
1872
  		pr_err("Device allocation failed
  ");
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1873
1874
1875
1876
1877
  		goto exit;
  	}
  
  	err = platform_device_add_resources(pdev, &res, 1);
  	if (err) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1878
1879
  		pr_err("Device resource addition failed (%d)
  ", err);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1880
1881
1882
1883
1884
  		goto exit_device_put;
  	}
  
  	err = platform_device_add(pdev);
  	if (err) {
1ca28218e   Joe Perches   hwmon: (w83781d) ...
1885
1886
  		pr_err("Device addition failed (%d)
  ", err);
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
  		goto exit_device_put;
  	}
  
  	return 0;
  
   exit_device_put:
  	platform_device_put(pdev);
   exit:
  	pdev = NULL;
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1898
  static int __init
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1899
  w83781d_isa_register(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1900
  {
fde095090   Jean Delvare   [PATCH] I2C: Sepa...
1901
  	int res;
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1902
1903
1904
  	if (w83781d_isa_found(isa_address)) {
  		res = platform_driver_register(&w83781d_isa_driver);
  		if (res)
c6566206c   Jean Delvare   hwmon: (w83781d) ...
1905
  			goto exit;
fde095090   Jean Delvare   [PATCH] I2C: Sepa...
1906

7666c13c6   Jean Delvare   hwmon/w83781d: No...
1907
1908
1909
1910
1911
  		/* Sets global pdev as a side effect */
  		res = w83781d_isa_device_add(isa_address);
  		if (res)
  			goto exit_unreg_isa_driver;
  	}
fde095090   Jean Delvare   [PATCH] I2C: Sepa...
1912
1913
  
  	return 0;
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1914

443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1915
  exit_unreg_isa_driver:
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1916
  	platform_driver_unregister(&w83781d_isa_driver);
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1917
  exit:
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1918
  	return res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1919
  }
dd56b6389   Geert Uytterhoeven   hwmon: (w83781d) ...
1920
  static void
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1921
  w83781d_isa_unregister(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1922
  {
7666c13c6   Jean Delvare   hwmon/w83781d: No...
1923
1924
1925
1926
  	if (pdev) {
  		platform_device_unregister(pdev);
  		platform_driver_unregister(&w83781d_isa_driver);
  	}
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
  }
  #else /* !CONFIG_ISA */
  
  static struct w83781d_data *w83781d_data_if_isa(void)
  {
  	return NULL;
  }
  
  static int
  w83781d_alias_detect(struct i2c_client *client, u8 chipid)
  {
  	return 0;
  }
  
  static int
  w83781d_read_value(struct w83781d_data *data, u16 reg)
  {
  	int res;
  
  	mutex_lock(&data->lock);
  	res = w83781d_read_value_i2c(data, reg);
  	mutex_unlock(&data->lock);
  
  	return res;
  }
  
  static int
  w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
  {
  	mutex_lock(&data->lock);
  	w83781d_write_value_i2c(data, reg, value);
  	mutex_unlock(&data->lock);
  
  	return 0;
  }
  
  static int __init
  w83781d_isa_register(void)
  {
  	return 0;
  }
dd56b6389   Geert Uytterhoeven   hwmon: (w83781d) ...
1968
  static void
443850ce5   Wolfgang Grandegger   hwmon: (w83781d) ...
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
  w83781d_isa_unregister(void)
  {
  }
  #endif /* CONFIG_ISA */
  
  static int __init
  sensors_w83781d_init(void)
  {
  	int res;
  
  	/* We register the ISA device first, so that we can skip the
  	 * registration of an I2C interface to the same device. */
  	res = w83781d_isa_register();
  	if (res)
  		goto exit;
  
  	res = i2c_add_driver(&w83781d_driver);
  	if (res)
  		goto exit_unreg_isa;
  
  	return 0;
  
   exit_unreg_isa:
  	w83781d_isa_unregister();
   exit:
  	return res;
  }
  
  static void __exit
  sensors_w83781d_exit(void)
  {
  	w83781d_isa_unregister();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
  	i2c_del_driver(&w83781d_driver);
  }
  
  MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  	      "Philip Edelbrock <phil@netroedge.com>, "
  	      "and Mark Studebaker <mdsxyz123@yahoo.com>");
  MODULE_DESCRIPTION("W83781D driver");
  MODULE_LICENSE("GPL");
  
  module_init(sensors_w83781d_init);
  module_exit(sensors_w83781d_exit);