Blame view

drivers/hwmon/adm1031.c 34 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
  /*
    adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
    monitoring
    Based on lm75.c and lm85.c
    Supports adm1030 / adm1031
    Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
    Reworked by Jean Delvare <khali@linux-fr.org>
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
8

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    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.
  */
  
  #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...
29
  #include <linux/hwmon.h>
c801082d7   Jean Delvare   hwmon: (adm1031) ...
30
  #include <linux/hwmon-sysfs.h>
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
31
  #include <linux/err.h>
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
32
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
  
  /* Following macros takes channel parameter starting from 0 to 2 */
  #define ADM1031_REG_FAN_SPEED(nr)	(0x08 + (nr))
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
36
  #define ADM1031_REG_FAN_DIV(nr)		(0x20 + (nr))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
  #define ADM1031_REG_PWM			(0x22)
  #define ADM1031_REG_FAN_MIN(nr)		(0x10 + (nr))
87c33daad   Jean Delvare   hwmon: (adm1031) ...
39
  #define ADM1031_REG_FAN_FILTER		(0x23)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40

49dc9efed   Ira Snyder   hwmon: (adm1031) ...
41
  #define ADM1031_REG_TEMP_OFFSET(nr)	(0x0d + (nr))
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
42
43
44
  #define ADM1031_REG_TEMP_MAX(nr)	(0x14 + 4 * (nr))
  #define ADM1031_REG_TEMP_MIN(nr)	(0x15 + 4 * (nr))
  #define ADM1031_REG_TEMP_CRIT(nr)	(0x16 + 4 * (nr))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45

6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
46
  #define ADM1031_REG_TEMP(nr)		(0x0a + (nr))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
  #define ADM1031_REG_AUTO_TEMP(nr)	(0x24 + (nr))
  
  #define ADM1031_REG_STATUS(nr)		(0x2 + (nr))
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
50
51
52
  #define ADM1031_REG_CONF1		0x00
  #define ADM1031_REG_CONF2		0x01
  #define ADM1031_REG_EXT_TEMP		0x06
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
56
57
58
59
60
61
62
  
  #define ADM1031_CONF1_MONITOR_ENABLE	0x01	/* Monitoring enable */
  #define ADM1031_CONF1_PWM_INVERT	0x08	/* PWM Invert */
  #define ADM1031_CONF1_AUTO_MODE		0x80	/* Auto FAN */
  
  #define ADM1031_CONF2_PWM1_ENABLE	0x01
  #define ADM1031_CONF2_PWM2_ENABLE	0x02
  #define ADM1031_CONF2_TACH1_ENABLE	0x04
  #define ADM1031_CONF2_TACH2_ENABLE	0x08
  #define ADM1031_CONF2_TEMP_ENABLE(chan)	(0x10 << (chan))
87c33daad   Jean Delvare   hwmon: (adm1031) ...
63
64
  #define ADM1031_UPDATE_RATE_MASK	0x1c
  #define ADM1031_UPDATE_RATE_SHIFT	2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
  /* Addresses to scan */
25e9c86d5   Mark M. Hoffman   hwmon: normal_i2c...
66
  static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67

e5e9f44c2   Jean Delvare   i2c: Drop I2C_CLI...
68
  enum chips { adm1030, adm1031 };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
73
  
  typedef u8 auto_chan_table_t[8][2];
  
  /* Each client has this additional data */
  struct adm1031_data {
1beeffe43   Tony Jones   hwmon: Convert fr...
74
  	struct device *hwmon_dev;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
75
  	struct mutex update_lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
  	int chip_type;
  	char valid;		/* !=0 if following fields are valid */
  	unsigned long last_updated;	/* In jiffies */
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
79
  	unsigned int update_interval;	/* In milliseconds */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
  	/* The chan_select_table contains the possible configurations for
  	 * auto fan control.
  	 */
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
83
  	const auto_chan_table_t *chan_select_table;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  	u16 alarm;
  	u8 conf1;
  	u8 conf2;
  	u8 fan[2];
  	u8 fan_div[2];
  	u8 fan_min[2];
  	u8 pwm[2];
  	u8 old_pwm[2];
  	s8 temp[3];
  	u8 ext_temp[3];
  	u8 auto_temp[3];
  	u8 auto_temp_min[3];
  	u8 auto_temp_off[3];
  	u8 auto_temp_max[3];
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
98
  	s8 temp_offset[3];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
  	s8 temp_min[3];
  	s8 temp_max[3];
  	s8 temp_crit[3];
  };
af200f881   Jean Delvare   hwmon: (adm1031) ...
103
104
  static int adm1031_probe(struct i2c_client *client,
  			 const struct i2c_device_id *id);
310ec7921   Jean Delvare   i2c: Drop the kin...
105
  static int adm1031_detect(struct i2c_client *client,
af200f881   Jean Delvare   hwmon: (adm1031) ...
106
  			  struct i2c_board_info *info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  static void adm1031_init_client(struct i2c_client *client);
af200f881   Jean Delvare   hwmon: (adm1031) ...
108
  static int adm1031_remove(struct i2c_client *client);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
  static struct adm1031_data *adm1031_update_device(struct device *dev);
af200f881   Jean Delvare   hwmon: (adm1031) ...
110
111
112
113
114
115
  static const struct i2c_device_id adm1031_id[] = {
  	{ "adm1030", adm1030 },
  	{ "adm1031", adm1031 },
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, adm1031_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
  /* This is the driver that will be inserted */
  static struct i2c_driver adm1031_driver = {
af200f881   Jean Delvare   hwmon: (adm1031) ...
118
  	.class		= I2C_CLASS_HWMON,
cdaf79349   Laurent Riffard   [PATCH] i2c: Drop...
119
  	.driver = {
cdaf79349   Laurent Riffard   [PATCH] i2c: Drop...
120
121
  		.name = "adm1031",
  	},
af200f881   Jean Delvare   hwmon: (adm1031) ...
122
123
124
125
  	.probe		= adm1031_probe,
  	.remove		= adm1031_remove,
  	.id_table	= adm1031_id,
  	.detect		= adm1031_detect,
c3813d6af   Jean Delvare   i2c: Get rid of s...
126
  	.address_list	= normal_i2c,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  };
  
  static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
  {
  	return i2c_smbus_read_byte_data(client, reg);
  }
  
  static inline int
  adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
  {
  	return i2c_smbus_write_byte_data(client, reg, value);
  }
  
  
  #define TEMP_TO_REG(val)		(((val) < 0 ? ((val - 500) / 1000) : \
  					((val + 500) / 1000)))
  
  #define TEMP_FROM_REG(val)		((val) * 1000)
  
  #define TEMP_FROM_REG_EXT(val, ext)	(TEMP_FROM_REG(val) + (ext) * 125)
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
147
148
149
  #define TEMP_OFFSET_TO_REG(val)		(TEMP_TO_REG(val) & 0x8f)
  #define TEMP_OFFSET_FROM_REG(val)	TEMP_FROM_REG((val) < 0 ? \
  						      (val) | 0x70 : (val))
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
150
151
  #define FAN_FROM_REG(reg, div)		((reg) ? \
  					 (11250 * 60) / ((reg) * (div)) : 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  
  static int FAN_TO_REG(int reg, int div)
  {
  	int tmp;
  	tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
  	return tmp > 255 ? 255 : tmp;
  }
  
  #define FAN_DIV_FROM_REG(reg)		(1<<(((reg)&0xc0)>>6))
  
  #define PWM_TO_REG(val)			(SENSORS_LIMIT((val), 0, 255) >> 4)
  #define PWM_FROM_REG(val)		((val) << 4)
  
  #define FAN_CHAN_FROM_REG(reg)		(((reg) >> 5) & 7)
  #define FAN_CHAN_TO_REG(val, reg)	\
  	(((reg) & 0x1F) | (((val) << 5) & 0xe0))
  
  #define AUTO_TEMP_MIN_TO_REG(val, reg)	\
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
170
171
  	((((val) / 500) & 0xf8) | ((reg) & 0x7))
  #define AUTO_TEMP_RANGE_FROM_REG(reg)	(5000 * (1 << ((reg) & 0x7)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  #define AUTO_TEMP_MIN_FROM_REG(reg)	(1000 * ((((reg) >> 3) & 0x1f) << 2))
  
  #define AUTO_TEMP_MIN_FROM_REG_DEG(reg)	((((reg) >> 3) & 0x1f) << 2)
  
  #define AUTO_TEMP_OFF_FROM_REG(reg)		\
  	(AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
  
  #define AUTO_TEMP_MAX_FROM_REG(reg)		\
  	(AUTO_TEMP_RANGE_FROM_REG(reg) +	\
  	AUTO_TEMP_MIN_FROM_REG(reg))
  
  static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
  {
  	int ret;
  	int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
  
  	range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
  	ret = ((reg & 0xf8) |
  	       (range < 10000 ? 0 :
  		range < 20000 ? 1 :
  		range < 40000 ? 2 : range < 80000 ? 3 : 4));
  	return ret;
  }
  
  /* FAN auto control */
  #define GET_FAN_AUTO_BITFIELD(data, idx)	\
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
198
  	(*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199

6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
200
  /* The tables below contains the possible values for the auto fan
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
203
204
   * control bitfields. the index in the table is the register value.
   * MSb is the auto fan control enable bit, so the four first entries
   * in the table disables auto fan control when both bitfields are zero.
   */
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
205
206
207
208
209
210
  static const auto_chan_table_t auto_channel_select_table_adm1031 = {
  	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
  	{ 2 /* 0b010 */ , 4 /* 0b100 */ },
  	{ 2 /* 0b010 */ , 2 /* 0b010 */ },
  	{ 4 /* 0b100 */ , 4 /* 0b100 */ },
  	{ 7 /* 0b111 */ , 7 /* 0b111 */ },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
  };
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
212
213
214
215
216
217
  static const auto_chan_table_t auto_channel_select_table_adm1030 = {
  	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
  	{ 2 /* 0b10 */		, 0 },
  	{ 0xff /* invalid */	, 0 },
  	{ 0xff /* invalid */	, 0 },
  	{ 3 /* 0b11 */		, 0 },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
221
222
223
224
  };
  
  /* That function checks if a bitfield is valid and returns the other bitfield
   * nearest match if no exact match where found.
   */
  static int
  get_fan_auto_nearest(struct adm1031_data *data,
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
225
  		     int chan, u8 val, u8 reg, u8 *new_reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  {
  	int i;
  	int first_match = -1, exact_match = -1;
  	u8 other_reg_val =
  	    (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
  
  	if (val == 0) {
  		*new_reg = 0;
  		return 0;
  	}
  
  	for (i = 0; i < 8; i++) {
  		if ((val == (*data->chan_select_table)[i][chan]) &&
  		    ((*data->chan_select_table)[i][chan ? 0 : 1] ==
  		     other_reg_val)) {
  			/* We found an exact match */
  			exact_match = i;
  			break;
  		} else if (val == (*data->chan_select_table)[i][chan] &&
  			   first_match == -1) {
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
246
247
  			/* Save the first match in case of an exact match has
  			 * not been found
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
249
250
251
  			 */
  			first_match = i;
  		}
  	}
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
252
  	if (exact_match >= 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
  		*new_reg = exact_match;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
254
  	else if (first_match >= 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  		*new_reg = first_match;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
256
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
  		return -EINVAL;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
258

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
  	return 0;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
261
262
  static ssize_t show_fan_auto_channel(struct device *dev,
  				     struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
264
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
270
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", GET_FAN_AUTO_BITFIELD(data, nr));
  }
  
  static ssize_t
c801082d7   Jean Delvare   hwmon: (adm1031) ...
271
272
  set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
  		     const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
275
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
276
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
277
  	long val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
  	u8 reg;
  	int ret;
  	u8 old_fan_mode;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
281
282
283
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  	old_fan_mode = data->conf1;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
285
  	mutex_lock(&data->update_lock);
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
286

1c720093f   Guenter Roeck   hwmon: (adm1031) ...
287
288
  	ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg);
  	if (ret) {
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
289
  		mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
291
  		return ret;
  	}
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
292
293
  	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
  	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
  	    (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
295
  		if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
296
297
  			/* Switch to Auto Fan Mode
  			 * Save PWM registers
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
302
303
304
305
306
  			 * Set PWM registers to 33% Both */
  			data->old_pwm[0] = data->pwm[0];
  			data->old_pwm[1] = data->pwm[1];
  			adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
  		} else {
  			/* Switch to Manual Mode */
  			data->pwm[0] = data->old_pwm[0];
  			data->pwm[1] = data->old_pwm[1];
  			/* Restore PWM registers */
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
307
  			adm1031_write_value(client, ADM1031_REG_PWM,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
311
312
  					    data->pwm[0] | (data->pwm[1] << 4));
  		}
  	}
  	data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
  	adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
313
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
316
317
318
319
  static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
  		show_fan_auto_channel, set_fan_auto_channel, 0);
  static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
  		show_fan_auto_channel, set_fan_auto_channel, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
  
  /* Auto Temps */
c801082d7   Jean Delvare   hwmon: (adm1031) ...
322
323
  static ssize_t show_auto_temp_off(struct device *dev,
  				  struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
325
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  	struct adm1031_data *data = adm1031_update_device(dev);
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
327
328
  	return sprintf(buf, "%d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
330
  		       AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
331
332
  static ssize_t show_auto_temp_min(struct device *dev,
  				  struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
334
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
340
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ",
  		       AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
  }
  static ssize_t
c801082d7   Jean Delvare   hwmon: (adm1031) ...
341
342
  set_auto_temp_min(struct device *dev, struct device_attribute *attr,
  		  const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
346
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
347
348
349
350
351
352
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
354
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
  	data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
  	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  			    data->auto_temp[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
358
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
361
362
  static ssize_t show_auto_temp_max(struct device *dev,
  				  struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
364
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ",
  		       AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
  }
  static ssize_t
c801082d7   Jean Delvare   hwmon: (adm1031) ...
371
372
  set_auto_temp_max(struct device *dev, struct device_attribute *attr,
  		  const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
374
375
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
376
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
377
378
379
380
381
382
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
384
  	mutex_lock(&data->update_lock);
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
385
386
  	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
  						  data->pwm[nr]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
  	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  			    data->temp_max[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
389
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
392
393
394
395
396
397
398
  #define auto_temp_reg(offset)						\
  static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO,		\
  		show_auto_temp_off, NULL, offset - 1);			\
  static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR,	\
  		show_auto_temp_min, set_auto_temp_min, offset - 1);	\
  static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR,	\
  		show_auto_temp_max, set_auto_temp_max, offset - 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
400
401
402
403
404
  
  auto_temp_reg(1);
  auto_temp_reg(2);
  auto_temp_reg(3);
  
  /* pwm */
c801082d7   Jean Delvare   hwmon: (adm1031) ...
405
406
  static ssize_t show_pwm(struct device *dev,
  			struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
408
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
410
411
412
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", PWM_FROM_REG(data->pwm[nr]));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
413
414
  static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  		       const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
418
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
419
420
421
422
423
424
  	long val;
  	int ret, reg;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
426
  	mutex_lock(&data->update_lock);
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
427
  	if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
429
  	    (((val>>4) & 0xf) != 5)) {
  		/* In automatic mode, the only PWM accepted is 33% */
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
430
  		mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
432
433
434
435
436
437
  		return -EINVAL;
  	}
  	data->pwm[nr] = PWM_TO_REG(val);
  	reg = adm1031_read_value(client, ADM1031_REG_PWM);
  	adm1031_write_value(client, ADM1031_REG_PWM,
  			    nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
  			    : (data->pwm[nr] & 0xf) | (reg & 0xf0));
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
438
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
440
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
441
442
443
444
445
446
  static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
  static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
  static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
  		show_pwm, set_pwm, 0);
  static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
  		show_pwm, set_pwm, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
449
450
451
  
  /* Fans */
  
  /*
   * That function checks the cases where the fan reading is not
44bbe87e9   Steven Cole   [PATCH] Spelling ...
452
   * relevant.  It is used to provide 0 as fan reading when the fan is
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
454
455
456
457
458
459
460
   * not supposed to run
   */
  static int trust_fan_readings(struct adm1031_data *data, int chan)
  {
  	int res = 0;
  
  	if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
  		switch (data->conf1 & 0x60) {
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
461
462
463
464
465
  		case 0x00:
  			/*
  			 * remote temp1 controls fan1,
  			 * remote temp2 controls fan2
  			 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  			res = data->temp[chan+1] >=
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
467
  			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  			break;
  		case 0x20:	/* remote temp1 controls both fans */
  			res =
  			    data->temp[1] >=
  			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
  			break;
  		case 0x40:	/* remote temp2 controls both fans */
  			res =
  			    data->temp[2] >=
  			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
  			break;
  		case 0x60:	/* max controls both fans */
  			res =
  			    data->temp[0] >=
  			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
  			    || data->temp[1] >=
  			    AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
485
  			    || (data->chip_type == adm1031
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
488
489
490
491
492
493
494
  				&& data->temp[2] >=
  				AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
  			break;
  		}
  	} else {
  		res = data->pwm[chan] > 0;
  	}
  	return res;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
495
496
  static ssize_t show_fan(struct device *dev,
  			struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
498
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
500
501
502
503
504
505
506
  	struct adm1031_data *data = adm1031_update_device(dev);
  	int value;
  
  	value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
  				 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
  	return sprintf(buf, "%d
  ", value);
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
507
508
  static ssize_t show_fan_div(struct device *dev,
  			    struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
510
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
512
513
514
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", FAN_DIV_FROM_REG(data->fan_div[nr]));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
515
516
  static ssize_t show_fan_min(struct device *dev,
  			    struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
517
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
518
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
523
524
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ",
  		       FAN_FROM_REG(data->fan_min[nr],
  				    FAN_DIV_FROM_REG(data->fan_div[nr])));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
525
526
  static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  			   const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
528
529
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
530
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
531
532
533
534
535
536
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
538
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  	if (val) {
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
540
  		data->fan_min[nr] =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
542
543
544
545
  			FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
  	} else {
  		data->fan_min[nr] = 0xff;
  	}
  	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
546
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
549
550
  static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  			   const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
554
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
555
  	long val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
  	u8 tmp;
  	int old_div;
  	int new_min;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
559
560
561
562
563
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
566
  
  	tmp = val == 8 ? 0xc0 :
  	      val == 4 ? 0x80 :
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
567
568
  	      val == 2 ? 0x40 :
  	      val == 1 ? 0x00 :
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
  	      0xff;
  	if (tmp == 0xff)
  		return -EINVAL;
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
572

9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
573
  	mutex_lock(&data->update_lock);
38a1f0e9a   Jean Delvare   hwmon: (adm1031) ...
574
575
576
577
578
579
580
  	/* Get fresh readings */
  	data->fan_div[nr] = adm1031_read_value(client,
  					       ADM1031_REG_FAN_DIV(nr));
  	data->fan_min[nr] = adm1031_read_value(client,
  					       ADM1031_REG_FAN_MIN(nr));
  
  	/* Write the new clock divider and fan min */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
581
  	old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
582
583
  	data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
  	new_min = data->fan_min[nr] * old_div / val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
  	data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585

6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
586
  	adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587
  			    data->fan_div[nr]);
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
588
  	adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  			    data->fan_min[nr]);
38a1f0e9a   Jean Delvare   hwmon: (adm1031) ...
590
591
592
  
  	/* Invalidate the cache: fan speed is no longer valid */
  	data->valid = 0;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
593
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
  	return count;
  }
  
  #define fan_offset(offset)						\
c801082d7   Jean Delvare   hwmon: (adm1031) ...
598
599
600
601
602
603
  static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,			\
  		show_fan, NULL, offset - 1);				\
  static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\
  		show_fan_min, set_fan_min, offset - 1);			\
  static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,		\
  		show_fan_div, set_fan_div, offset - 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
607
608
609
  
  fan_offset(1);
  fan_offset(2);
  
  
  /* Temps */
c801082d7   Jean Delvare   hwmon: (adm1031) ...
610
611
  static ssize_t show_temp(struct device *dev,
  			 struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
613
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
615
616
617
618
619
620
621
  	struct adm1031_data *data = adm1031_update_device(dev);
  	int ext;
  	ext = nr == 0 ?
  	    ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
  	    (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG_EXT(data->temp[nr], ext));
  }
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
622
623
624
625
626
627
628
629
630
  static ssize_t show_temp_offset(struct device *dev,
  				struct device_attribute *attr, char *buf)
  {
  	int nr = to_sensor_dev_attr(attr)->index;
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ",
  		       TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
631
632
  static ssize_t show_temp_min(struct device *dev,
  			     struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
634
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
636
637
638
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG(data->temp_min[nr]));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
639
640
  static ssize_t show_temp_max(struct device *dev,
  			     struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
642
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643
644
645
646
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG(data->temp_max[nr]));
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
647
648
  static ssize_t show_temp_crit(struct device *dev,
  			      struct device_attribute *attr, char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
  {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
650
  	int nr = to_sensor_dev_attr(attr)->index;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
652
653
654
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", TEMP_FROM_REG(data->temp_crit[nr]));
  }
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
655
656
657
658
659
660
661
  static ssize_t set_temp_offset(struct device *dev,
  			       struct device_attribute *attr, const char *buf,
  			       size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
662
663
664
665
666
667
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
668

49dc9efed   Ira Snyder   hwmon: (adm1031) ...
669
670
671
672
673
674
675
676
  	val = SENSORS_LIMIT(val, -15000, 15000);
  	mutex_lock(&data->update_lock);
  	data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
  			    data->temp_offset[nr]);
  	mutex_unlock(&data->update_lock);
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
677
678
  static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  			    const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
682
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
683
684
685
686
687
688
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
  	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
691
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
693
694
  	data->temp_min[nr] = TEMP_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
  			    data->temp_min[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
695
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
697
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
698
699
  static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  			    const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
700
701
702
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
703
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
704
705
706
707
708
709
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
  	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
712
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
713
714
715
  	data->temp_max[nr] = TEMP_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
  			    data->temp_max[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
716
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
719
720
  static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  			     const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
722
723
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
c801082d7   Jean Delvare   hwmon: (adm1031) ...
724
  	int nr = to_sensor_dev_attr(attr)->index;
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
725
726
727
728
729
730
  	long val;
  	int ret;
  
  	ret = kstrtol(buf, 10, &val);
  	if (ret)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
  	val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
733
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
734
735
736
  	data->temp_crit[nr] = TEMP_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
  			    data->temp_crit[nr]);
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
737
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
739
  	return count;
  }
c801082d7   Jean Delvare   hwmon: (adm1031) ...
740
741
742
  #define temp_reg(offset)						\
  static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,		\
  		show_temp, NULL, offset - 1);				\
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
743
744
  static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR,	\
  		show_temp_offset, set_temp_offset, offset - 1);		\
c801082d7   Jean Delvare   hwmon: (adm1031) ...
745
746
747
748
749
750
  static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\
  		show_temp_min, set_temp_min, offset - 1);		\
  static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\
  		show_temp_max, set_temp_max, offset - 1);		\
  static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,	\
  		show_temp_crit, set_temp_crit, offset - 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
752
753
754
755
756
  
  temp_reg(1);
  temp_reg(2);
  temp_reg(3);
  
  /* Alarms */
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
757
758
  static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  			   char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
760
761
762
763
764
765
  {
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", data->alarm);
  }
  
  static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
050ab8789   Jean Delvare   hwmon: (adm1031) ...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
  static ssize_t show_alarm(struct device *dev,
  			  struct device_attribute *attr, char *buf)
  {
  	int bitnr = to_sensor_dev_attr(attr)->index;
  	struct adm1031_data *data = adm1031_update_device(dev);
  	return sprintf(buf, "%d
  ", (data->alarm >> bitnr) & 1);
  }
  
  static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
  static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
  static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
  static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
  static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
  static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
  static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
  static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
  static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
  static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
  static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
  static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
  static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790

a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
791
792
  /* Update Interval */
  static const unsigned int update_intervals[] = {
87c33daad   Jean Delvare   hwmon: (adm1031) ...
793
794
  	16000, 8000, 4000, 2000, 1000, 500, 250, 125,
  };
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
795
796
  static ssize_t show_update_interval(struct device *dev,
  				    struct device_attribute *attr, char *buf)
87c33daad   Jean Delvare   hwmon: (adm1031) ...
797
798
799
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
800
801
  	return sprintf(buf, "%u
  ", data->update_interval);
87c33daad   Jean Delvare   hwmon: (adm1031) ...
802
  }
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
803
804
805
  static ssize_t set_update_interval(struct device *dev,
  				   struct device_attribute *attr,
  				   const char *buf, size_t count)
87c33daad   Jean Delvare   hwmon: (adm1031) ...
806
807
808
809
810
811
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
  	unsigned long val;
  	int i, err;
  	u8 reg;
179c4fdb5   Frans Meulenbroeks   hwmon: replaced s...
812
  	err = kstrtoul(buf, 10, &val);
87c33daad   Jean Delvare   hwmon: (adm1031) ...
813
814
  	if (err)
  		return err;
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
815
816
817
818
819
820
  	/*
  	 * Find the nearest update interval from the table.
  	 * Use it to determine the matching update rate.
  	 */
  	for (i = 0; i < ARRAY_SIZE(update_intervals) - 1; i++) {
  		if (val >= update_intervals[i])
87c33daad   Jean Delvare   hwmon: (adm1031) ...
821
822
  			break;
  	}
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
823
  	/* if not found, we point to the last entry (lowest update interval) */
87c33daad   Jean Delvare   hwmon: (adm1031) ...
824
825
826
827
828
829
830
831
  
  	/* set the new update rate while preserving other settings */
  	reg = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
  	reg &= ~ADM1031_UPDATE_RATE_MASK;
  	reg |= i << ADM1031_UPDATE_RATE_SHIFT;
  	adm1031_write_value(client, ADM1031_REG_FAN_FILTER, reg);
  
  	mutex_lock(&data->update_lock);
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
832
  	data->update_interval = update_intervals[i];
87c33daad   Jean Delvare   hwmon: (adm1031) ...
833
834
835
836
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
837
838
  static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
  		   set_update_interval);
87c33daad   Jean Delvare   hwmon: (adm1031) ...
839

681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
840
  static struct attribute *adm1031_attributes[] = {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
841
842
843
  	&sensor_dev_attr_fan1_input.dev_attr.attr,
  	&sensor_dev_attr_fan1_div.dev_attr.attr,
  	&sensor_dev_attr_fan1_min.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
844
845
  	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
  	&sensor_dev_attr_fan1_fault.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
846
847
848
  	&sensor_dev_attr_pwm1.dev_attr.attr,
  	&sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
  	&sensor_dev_attr_temp1_input.dev_attr.attr,
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
849
  	&sensor_dev_attr_temp1_offset.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
850
  	&sensor_dev_attr_temp1_min.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
851
  	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
852
  	&sensor_dev_attr_temp1_max.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
853
  	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
854
  	&sensor_dev_attr_temp1_crit.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
855
  	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
856
  	&sensor_dev_attr_temp2_input.dev_attr.attr,
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
857
  	&sensor_dev_attr_temp2_offset.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
858
  	&sensor_dev_attr_temp2_min.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
859
  	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
860
  	&sensor_dev_attr_temp2_max.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
861
  	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
862
  	&sensor_dev_attr_temp2_crit.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
863
864
  	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp2_fault.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
865
866
867
868
869
870
871
872
873
874
  
  	&sensor_dev_attr_auto_temp1_off.dev_attr.attr,
  	&sensor_dev_attr_auto_temp1_min.dev_attr.attr,
  	&sensor_dev_attr_auto_temp1_max.dev_attr.attr,
  
  	&sensor_dev_attr_auto_temp2_off.dev_attr.attr,
  	&sensor_dev_attr_auto_temp2_min.dev_attr.attr,
  	&sensor_dev_attr_auto_temp2_max.dev_attr.attr,
  
  	&sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
875

a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
876
  	&dev_attr_update_interval.attr,
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
877
878
879
880
881
882
883
884
885
886
  	&dev_attr_alarms.attr,
  
  	NULL
  };
  
  static const struct attribute_group adm1031_group = {
  	.attrs = adm1031_attributes,
  };
  
  static struct attribute *adm1031_attributes_opt[] = {
c801082d7   Jean Delvare   hwmon: (adm1031) ...
887
888
889
  	&sensor_dev_attr_fan2_input.dev_attr.attr,
  	&sensor_dev_attr_fan2_div.dev_attr.attr,
  	&sensor_dev_attr_fan2_min.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
890
891
  	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
  	&sensor_dev_attr_fan2_fault.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
892
893
894
  	&sensor_dev_attr_pwm2.dev_attr.attr,
  	&sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
  	&sensor_dev_attr_temp3_input.dev_attr.attr,
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
895
  	&sensor_dev_attr_temp3_offset.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
896
  	&sensor_dev_attr_temp3_min.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
897
  	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
898
  	&sensor_dev_attr_temp3_max.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
899
  	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
900
  	&sensor_dev_attr_temp3_crit.dev_attr.attr,
050ab8789   Jean Delvare   hwmon: (adm1031) ...
901
902
  	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  	&sensor_dev_attr_temp3_fault.dev_attr.attr,
c801082d7   Jean Delvare   hwmon: (adm1031) ...
903
904
905
906
  	&sensor_dev_attr_auto_temp3_off.dev_attr.attr,
  	&sensor_dev_attr_auto_temp3_min.dev_attr.attr,
  	&sensor_dev_attr_auto_temp3_max.dev_attr.attr,
  	&sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
907
908
909
910
911
912
  	NULL
  };
  
  static const struct attribute_group adm1031_group_opt = {
  	.attrs = adm1031_attributes_opt,
  };
af200f881   Jean Delvare   hwmon: (adm1031) ...
913
  /* Return 0 if detection is successful, -ENODEV otherwise */
310ec7921   Jean Delvare   i2c: Drop the kin...
914
  static int adm1031_detect(struct i2c_client *client,
af200f881   Jean Delvare   hwmon: (adm1031) ...
915
  			  struct i2c_board_info *info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916
  {
af200f881   Jean Delvare   hwmon: (adm1031) ...
917
  	struct i2c_adapter *adapter = client->adapter;
52df6440a   Jean Delvare   hwmon: Clean up d...
918
919
  	const char *name;
  	int id, co;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
  
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
af200f881   Jean Delvare   hwmon: (adm1031) ...
922
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923

52df6440a   Jean Delvare   hwmon: Clean up d...
924
925
  	id = i2c_smbus_read_byte_data(client, 0x3d);
  	co = i2c_smbus_read_byte_data(client, 0x3e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
926

52df6440a   Jean Delvare   hwmon: Clean up d...
927
928
929
  	if (!((id == 0x31 || id == 0x30) && co == 0x41))
  		return -ENODEV;
  	name = (id == 0x30) ? "adm1030" : "adm1031";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930

af200f881   Jean Delvare   hwmon: (adm1031) ...
931
  	strlcpy(info->type, name, I2C_NAME_SIZE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
932

af200f881   Jean Delvare   hwmon: (adm1031) ...
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  	return 0;
  }
  
  static int adm1031_probe(struct i2c_client *client,
  			 const struct i2c_device_id *id)
  {
  	struct adm1031_data *data;
  	int err;
  
  	data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
  	if (!data) {
  		err = -ENOMEM;
  		goto exit;
  	}
  
  	i2c_set_clientdata(client, data);
  	data->chip_type = id->driver_data;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
950
  	mutex_init(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
951

af200f881   Jean Delvare   hwmon: (adm1031) ...
952
953
954
955
  	if (data->chip_type == adm1030)
  		data->chan_select_table = &auto_channel_select_table_adm1030;
  	else
  		data->chan_select_table = &auto_channel_select_table_adm1031;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
956
957
  
  	/* Initialize the ADM1031 chip */
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
958
  	adm1031_init_client(client);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
960
  
  	/* Register sysfs hooks */
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
961
962
  	err = sysfs_create_group(&client->dev.kobj, &adm1031_group);
  	if (err)
af200f881   Jean Delvare   hwmon: (adm1031) ...
963
  		goto exit_free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
964

af200f881   Jean Delvare   hwmon: (adm1031) ...
965
  	if (data->chip_type == adm1031) {
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
966
967
  		err = sysfs_create_group(&client->dev.kobj, &adm1031_group_opt);
  		if (err)
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
968
969
  			goto exit_remove;
  	}
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
970
  	data->hwmon_dev = hwmon_device_register(&client->dev);
1beeffe43   Tony Jones   hwmon: Convert fr...
971
972
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
973
  		goto exit_remove;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
974
975
976
  	}
  
  	return 0;
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
977
  exit_remove:
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
978
979
  	sysfs_remove_group(&client->dev.kobj, &adm1031_group);
  	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
980
  exit_free:
1f57ff89f   Alexey Dobriyan   [PATCH] drivers/h...
981
  	kfree(data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
983
984
  exit:
  	return err;
  }
af200f881   Jean Delvare   hwmon: (adm1031) ...
985
  static int adm1031_remove(struct i2c_client *client)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
  {
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
987
  	struct adm1031_data *data = i2c_get_clientdata(client);
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
988

1beeffe43   Tony Jones   hwmon: Convert fr...
989
  	hwmon_device_unregister(data->hwmon_dev);
681c6f7a6   Mark M. Hoffman   hwmon: Fix unchec...
990
991
  	sysfs_remove_group(&client->dev.kobj, &adm1031_group);
  	sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
943b0830c   Mark M. Hoffman   [PATCH] I2C hwmon...
992
  	kfree(data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
997
998
999
  	return 0;
  }
  
  static void adm1031_init_client(struct i2c_client *client)
  {
  	unsigned int read_val;
  	unsigned int mask;
87c33daad   Jean Delvare   hwmon: (adm1031) ...
1000
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1001
1002
1003
1004
1005
1006
  	struct adm1031_data *data = i2c_get_clientdata(client);
  
  	mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
  	if (data->chip_type == adm1031) {
  		mask |= (ADM1031_CONF2_PWM2_ENABLE |
  			ADM1031_CONF2_TACH2_ENABLE);
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
1007
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1008
1009
  	/* Initialize the ADM1031 chip (enables fan speed reading ) */
  	read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1010
1011
  	if ((read_val | mask) != read_val)
  		adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1012
1013
1014
  
  	read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
  	if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1015
1016
  		adm1031_write_value(client, ADM1031_REG_CONF1,
  				    read_val | ADM1031_CONF1_MONITOR_ENABLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1017
  	}
87c33daad   Jean Delvare   hwmon: (adm1031) ...
1018
1019
1020
1021
  	/* Read the chip's update rate */
  	mask = ADM1031_UPDATE_RATE_MASK;
  	read_val = adm1031_read_value(client, ADM1031_REG_FAN_FILTER);
  	i = (read_val & mask) >> ADM1031_UPDATE_RATE_SHIFT;
a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
1022
1023
  	/* Save it as update interval */
  	data->update_interval = update_intervals[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1024
1025
1026
1027
1028
1029
  }
  
  static struct adm1031_data *adm1031_update_device(struct device *dev)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct adm1031_data *data = i2c_get_clientdata(client);
87c33daad   Jean Delvare   hwmon: (adm1031) ...
1030
  	unsigned long next_update;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031
  	int chan;
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
1032
  	mutex_lock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033

a51b9944a   Guenter Roeck   hwmon: (adm1031) ...
1034
1035
  	next_update = data->last_updated
  	  + msecs_to_jiffies(data->update_interval);
87c33daad   Jean Delvare   hwmon: (adm1031) ...
1036
  	if (time_after(jiffies, next_update) || !data->valid) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
  
  		dev_dbg(&client->dev, "Starting adm1031 update
  ");
  		for (chan = 0;
  		     chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
  			u8 oldh, newh;
  
  			oldh =
  			    adm1031_read_value(client, ADM1031_REG_TEMP(chan));
  			data->ext_temp[chan] =
  			    adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
  			newh =
  			    adm1031_read_value(client, ADM1031_REG_TEMP(chan));
  			if (newh != oldh) {
  				data->ext_temp[chan] =
  				    adm1031_read_value(client,
  						       ADM1031_REG_EXT_TEMP);
  #ifdef DEBUG
  				oldh =
  				    adm1031_read_value(client,
  						       ADM1031_REG_TEMP(chan));
  
  				/* oldh is actually newer */
  				if (newh != oldh)
  					dev_warn(&client->dev,
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1062
1063
  					  "Remote temperature may be wrong.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1064
1065
1066
  #endif
  			}
  			data->temp[chan] = newh;
49dc9efed   Ira Snyder   hwmon: (adm1031) ...
1067
1068
1069
  			data->temp_offset[chan] =
  			    adm1031_read_value(client,
  					       ADM1031_REG_TEMP_OFFSET(chan));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  			data->temp_min[chan] =
  			    adm1031_read_value(client,
  					       ADM1031_REG_TEMP_MIN(chan));
  			data->temp_max[chan] =
  			    adm1031_read_value(client,
  					       ADM1031_REG_TEMP_MAX(chan));
  			data->temp_crit[chan] =
  			    adm1031_read_value(client,
  					       ADM1031_REG_TEMP_CRIT(chan));
  			data->auto_temp[chan] =
  			    adm1031_read_value(client,
  					       ADM1031_REG_AUTO_TEMP(chan));
  
  		}
  
  		data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
  		data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
  
  		data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1089
1090
  		    | (adm1031_read_value(client, ADM1031_REG_STATUS(1)) << 8);
  		if (data->chip_type == adm1030)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
  			data->alarm &= 0xc0ff;
6d6006b8d   Jean Delvare   hwmon: (adm1031) ...
1092

1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1093
1094
  		for (chan = 0; chan < (data->chip_type == adm1030 ? 1 : 2);
  		     chan++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
  			data->fan_div[chan] =
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1096
1097
  			    adm1031_read_value(client,
  					       ADM1031_REG_FAN_DIV(chan));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098
  			data->fan_min[chan] =
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1099
1100
  			    adm1031_read_value(client,
  					       ADM1031_REG_FAN_MIN(chan));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1101
  			data->fan[chan] =
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1102
1103
  			    adm1031_read_value(client,
  					       ADM1031_REG_FAN_SPEED(chan));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1104
  			data->pwm[chan] =
1c720093f   Guenter Roeck   hwmon: (adm1031) ...
1105
1106
  			  (adm1031_read_value(client,
  					ADM1031_REG_PWM) >> (4 * chan)) & 0x0f;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1107
1108
1109
1110
  		}
  		data->last_updated = jiffies;
  		data->valid = 1;
  	}
9a61bf630   Ingo Molnar   [PATCH] hwmon: Se...
1111
  	mutex_unlock(&data->update_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
  
  	return data;
  }
  
  static int __init sensors_adm1031_init(void)
  {
  	return i2c_add_driver(&adm1031_driver);
  }
  
  static void __exit sensors_adm1031_exit(void)
  {
  	i2c_del_driver(&adm1031_driver);
  }
  
  MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
  MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
  MODULE_LICENSE("GPL");
  
  module_init(sensors_adm1031_init);
  module_exit(sensors_adm1031_exit);