Merge branches 'release', 'bugzilla-12011', 'bugzilla-12632', 'misc' and 'suspend...
[linux-2.6-block.git] / drivers / hwmon / adm1031.c
CommitLineData
1da177e4
LT
1/*
2 adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Based on lm75.c and lm85.c
5 Supports adm1030 / adm1031
6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7 Reworked by Jean Delvare <khali@linux-fr.org>
6d6006b8 8
1da177e4
LT
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/jiffies.h>
28#include <linux/i2c.h>
943b0830 29#include <linux/hwmon.h>
c801082d 30#include <linux/hwmon-sysfs.h>
943b0830 31#include <linux/err.h>
9a61bf63 32#include <linux/mutex.h>
1da177e4
LT
33
34/* Following macros takes channel parameter starting from 0 to 2 */
35#define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
6d6006b8 36#define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
1da177e4
LT
37#define ADM1031_REG_PWM (0x22)
38#define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
39
6d6006b8
JD
40#define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
41#define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
42#define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
1da177e4 43
6d6006b8 44#define ADM1031_REG_TEMP(nr) (0x0a + (nr))
1da177e4
LT
45#define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
46
47#define ADM1031_REG_STATUS(nr) (0x2 + (nr))
48
6d6006b8
JD
49#define ADM1031_REG_CONF1 0x00
50#define ADM1031_REG_CONF2 0x01
51#define ADM1031_REG_EXT_TEMP 0x06
1da177e4
LT
52
53#define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
54#define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
55#define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
56
57#define ADM1031_CONF2_PWM1_ENABLE 0x01
58#define ADM1031_CONF2_PWM2_ENABLE 0x02
59#define ADM1031_CONF2_TACH1_ENABLE 0x04
60#define ADM1031_CONF2_TACH2_ENABLE 0x08
61#define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
62
63/* Addresses to scan */
25e9c86d 64static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1da177e4
LT
65
66/* Insmod parameters */
f4b50261 67I2C_CLIENT_INSMOD_2(adm1030, adm1031);
1da177e4
LT
68
69typedef u8 auto_chan_table_t[8][2];
70
71/* Each client has this additional data */
72struct adm1031_data {
1beeffe4 73 struct device *hwmon_dev;
9a61bf63 74 struct mutex update_lock;
1da177e4
LT
75 int chip_type;
76 char valid; /* !=0 if following fields are valid */
77 unsigned long last_updated; /* In jiffies */
78 /* The chan_select_table contains the possible configurations for
79 * auto fan control.
80 */
6d6006b8 81 const auto_chan_table_t *chan_select_table;
1da177e4
LT
82 u16 alarm;
83 u8 conf1;
84 u8 conf2;
85 u8 fan[2];
86 u8 fan_div[2];
87 u8 fan_min[2];
88 u8 pwm[2];
89 u8 old_pwm[2];
90 s8 temp[3];
91 u8 ext_temp[3];
92 u8 auto_temp[3];
93 u8 auto_temp_min[3];
94 u8 auto_temp_off[3];
95 u8 auto_temp_max[3];
96 s8 temp_min[3];
97 s8 temp_max[3];
98 s8 temp_crit[3];
99};
100
af200f88
JD
101static int adm1031_probe(struct i2c_client *client,
102 const struct i2c_device_id *id);
103static int adm1031_detect(struct i2c_client *client, int kind,
104 struct i2c_board_info *info);
1da177e4 105static void adm1031_init_client(struct i2c_client *client);
af200f88 106static int adm1031_remove(struct i2c_client *client);
1da177e4
LT
107static struct adm1031_data *adm1031_update_device(struct device *dev);
108
af200f88
JD
109static const struct i2c_device_id adm1031_id[] = {
110 { "adm1030", adm1030 },
111 { "adm1031", adm1031 },
112 { }
113};
114MODULE_DEVICE_TABLE(i2c, adm1031_id);
115
1da177e4
LT
116/* This is the driver that will be inserted */
117static struct i2c_driver adm1031_driver = {
af200f88 118 .class = I2C_CLASS_HWMON,
cdaf7934 119 .driver = {
cdaf7934
LR
120 .name = "adm1031",
121 },
af200f88
JD
122 .probe = adm1031_probe,
123 .remove = adm1031_remove,
124 .id_table = adm1031_id,
125 .detect = adm1031_detect,
126 .address_data = &addr_data,
1da177e4
LT
127};
128
129static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
130{
131 return i2c_smbus_read_byte_data(client, reg);
132}
133
134static inline int
135adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
136{
137 return i2c_smbus_write_byte_data(client, reg, value);
138}
139
140
141#define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
142 ((val + 500) / 1000)))
143
144#define TEMP_FROM_REG(val) ((val) * 1000)
145
146#define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
147
148#define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
149
150static int FAN_TO_REG(int reg, int div)
151{
152 int tmp;
153 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
154 return tmp > 255 ? 255 : tmp;
155}
156
157#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
158
159#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
160#define PWM_FROM_REG(val) ((val) << 4)
161
162#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
163#define FAN_CHAN_TO_REG(val, reg) \
164 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
165
166#define AUTO_TEMP_MIN_TO_REG(val, reg) \
167 ((((val)/500) & 0xf8)|((reg) & 0x7))
168#define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
169#define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
170
171#define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
172
173#define AUTO_TEMP_OFF_FROM_REG(reg) \
174 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
175
176#define AUTO_TEMP_MAX_FROM_REG(reg) \
177 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
178 AUTO_TEMP_MIN_FROM_REG(reg))
179
180static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
181{
182 int ret;
183 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
184
185 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
186 ret = ((reg & 0xf8) |
187 (range < 10000 ? 0 :
188 range < 20000 ? 1 :
189 range < 40000 ? 2 : range < 80000 ? 3 : 4));
190 return ret;
191}
192
193/* FAN auto control */
194#define GET_FAN_AUTO_BITFIELD(data, idx) \
195 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
196
6d6006b8 197/* The tables below contains the possible values for the auto fan
1da177e4
LT
198 * control bitfields. the index in the table is the register value.
199 * MSb is the auto fan control enable bit, so the four first entries
200 * in the table disables auto fan control when both bitfields are zero.
201 */
6d6006b8
JD
202static const auto_chan_table_t auto_channel_select_table_adm1031 = {
203 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
204 { 2 /* 0b010 */ , 4 /* 0b100 */ },
205 { 2 /* 0b010 */ , 2 /* 0b010 */ },
206 { 4 /* 0b100 */ , 4 /* 0b100 */ },
207 { 7 /* 0b111 */ , 7 /* 0b111 */ },
1da177e4
LT
208};
209
6d6006b8
JD
210static const auto_chan_table_t auto_channel_select_table_adm1030 = {
211 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
212 { 2 /* 0b10 */ , 0 },
213 { 0xff /* invalid */ , 0 },
214 { 0xff /* invalid */ , 0 },
215 { 3 /* 0b11 */ , 0 },
1da177e4
LT
216};
217
218/* That function checks if a bitfield is valid and returns the other bitfield
219 * nearest match if no exact match where found.
220 */
221static int
222get_fan_auto_nearest(struct adm1031_data *data,
223 int chan, u8 val, u8 reg, u8 * new_reg)
224{
225 int i;
226 int first_match = -1, exact_match = -1;
227 u8 other_reg_val =
228 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
229
230 if (val == 0) {
231 *new_reg = 0;
232 return 0;
233 }
234
235 for (i = 0; i < 8; i++) {
236 if ((val == (*data->chan_select_table)[i][chan]) &&
237 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
238 other_reg_val)) {
239 /* We found an exact match */
240 exact_match = i;
241 break;
242 } else if (val == (*data->chan_select_table)[i][chan] &&
243 first_match == -1) {
6d6006b8
JD
244 /* Save the first match in case of an exact match has
245 * not been found
1da177e4
LT
246 */
247 first_match = i;
248 }
249 }
250
251 if (exact_match >= 0) {
252 *new_reg = exact_match;
253 } else if (first_match >= 0) {
254 *new_reg = first_match;
255 } else {
256 return -EINVAL;
257 }
258 return 0;
259}
260
c801082d
JD
261static ssize_t show_fan_auto_channel(struct device *dev,
262 struct device_attribute *attr, char *buf)
1da177e4 263{
c801082d 264 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
265 struct adm1031_data *data = adm1031_update_device(dev);
266 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
267}
268
269static ssize_t
c801082d
JD
270set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
1da177e4
LT
272{
273 struct i2c_client *client = to_i2c_client(dev);
274 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 275 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
276 int val = simple_strtol(buf, NULL, 10);
277 u8 reg;
278 int ret;
279 u8 old_fan_mode;
280
281 old_fan_mode = data->conf1;
282
9a61bf63 283 mutex_lock(&data->update_lock);
6d6006b8 284
1da177e4 285 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
9a61bf63 286 mutex_unlock(&data->update_lock);
1da177e4
LT
287 return ret;
288 }
6d6006b8
JD
289 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
290 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
1da177e4
LT
291 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
292 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
6d6006b8
JD
293 /* Switch to Auto Fan Mode
294 * Save PWM registers
1da177e4
LT
295 * Set PWM registers to 33% Both */
296 data->old_pwm[0] = data->pwm[0];
297 data->old_pwm[1] = data->pwm[1];
298 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
299 } else {
300 /* Switch to Manual Mode */
301 data->pwm[0] = data->old_pwm[0];
302 data->pwm[1] = data->old_pwm[1];
303 /* Restore PWM registers */
6d6006b8 304 adm1031_write_value(client, ADM1031_REG_PWM,
1da177e4
LT
305 data->pwm[0] | (data->pwm[1] << 4));
306 }
307 }
308 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
309 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
9a61bf63 310 mutex_unlock(&data->update_lock);
1da177e4
LT
311 return count;
312}
313
c801082d
JD
314static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
315 show_fan_auto_channel, set_fan_auto_channel, 0);
316static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
317 show_fan_auto_channel, set_fan_auto_channel, 1);
1da177e4
LT
318
319/* Auto Temps */
c801082d
JD
320static ssize_t show_auto_temp_off(struct device *dev,
321 struct device_attribute *attr, char *buf)
1da177e4 322{
c801082d 323 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 324 struct adm1031_data *data = adm1031_update_device(dev);
6d6006b8 325 return sprintf(buf, "%d\n",
1da177e4
LT
326 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
327}
c801082d
JD
328static ssize_t show_auto_temp_min(struct device *dev,
329 struct device_attribute *attr, char *buf)
1da177e4 330{
c801082d 331 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
332 struct adm1031_data *data = adm1031_update_device(dev);
333 return sprintf(buf, "%d\n",
334 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
335}
336static ssize_t
c801082d
JD
337set_auto_temp_min(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
1da177e4
LT
339{
340 struct i2c_client *client = to_i2c_client(dev);
341 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 342 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
343 int val = simple_strtol(buf, NULL, 10);
344
9a61bf63 345 mutex_lock(&data->update_lock);
1da177e4
LT
346 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
347 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
348 data->auto_temp[nr]);
9a61bf63 349 mutex_unlock(&data->update_lock);
1da177e4
LT
350 return count;
351}
c801082d
JD
352static ssize_t show_auto_temp_max(struct device *dev,
353 struct device_attribute *attr, char *buf)
1da177e4 354{
c801082d 355 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
356 struct adm1031_data *data = adm1031_update_device(dev);
357 return sprintf(buf, "%d\n",
358 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
359}
360static ssize_t
c801082d
JD
361set_auto_temp_max(struct device *dev, struct device_attribute *attr,
362 const char *buf, size_t count)
1da177e4
LT
363{
364 struct i2c_client *client = to_i2c_client(dev);
365 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 366 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
367 int val = simple_strtol(buf, NULL, 10);
368
9a61bf63 369 mutex_lock(&data->update_lock);
1da177e4
LT
370 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
371 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
372 data->temp_max[nr]);
9a61bf63 373 mutex_unlock(&data->update_lock);
1da177e4
LT
374 return count;
375}
376
c801082d
JD
377#define auto_temp_reg(offset) \
378static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
379 show_auto_temp_off, NULL, offset - 1); \
380static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
381 show_auto_temp_min, set_auto_temp_min, offset - 1); \
382static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
383 show_auto_temp_max, set_auto_temp_max, offset - 1)
1da177e4
LT
384
385auto_temp_reg(1);
386auto_temp_reg(2);
387auto_temp_reg(3);
388
389/* pwm */
c801082d
JD
390static ssize_t show_pwm(struct device *dev,
391 struct device_attribute *attr, char *buf)
1da177e4 392{
c801082d 393 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
394 struct adm1031_data *data = adm1031_update_device(dev);
395 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
396}
c801082d
JD
397static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
398 const char *buf, size_t count)
1da177e4
LT
399{
400 struct i2c_client *client = to_i2c_client(dev);
401 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 402 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
403 int val = simple_strtol(buf, NULL, 10);
404 int reg;
405
9a61bf63 406 mutex_lock(&data->update_lock);
6d6006b8 407 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
1da177e4
LT
408 (((val>>4) & 0xf) != 5)) {
409 /* In automatic mode, the only PWM accepted is 33% */
9a61bf63 410 mutex_unlock(&data->update_lock);
1da177e4
LT
411 return -EINVAL;
412 }
413 data->pwm[nr] = PWM_TO_REG(val);
414 reg = adm1031_read_value(client, ADM1031_REG_PWM);
415 adm1031_write_value(client, ADM1031_REG_PWM,
416 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
417 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
9a61bf63 418 mutex_unlock(&data->update_lock);
1da177e4
LT
419 return count;
420}
421
c801082d
JD
422static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
423static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
424static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
425 show_pwm, set_pwm, 0);
426static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
427 show_pwm, set_pwm, 1);
1da177e4
LT
428
429/* Fans */
430
431/*
432 * That function checks the cases where the fan reading is not
44bbe87e 433 * relevant. It is used to provide 0 as fan reading when the fan is
1da177e4
LT
434 * not supposed to run
435 */
436static int trust_fan_readings(struct adm1031_data *data, int chan)
437{
438 int res = 0;
439
440 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
441 switch (data->conf1 & 0x60) {
442 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
443 res = data->temp[chan+1] >=
444 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
445 break;
446 case 0x20: /* remote temp1 controls both fans */
447 res =
448 data->temp[1] >=
449 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
450 break;
451 case 0x40: /* remote temp2 controls both fans */
452 res =
453 data->temp[2] >=
454 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
455 break;
456 case 0x60: /* max controls both fans */
457 res =
458 data->temp[0] >=
459 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
460 || data->temp[1] >=
461 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
6d6006b8 462 || (data->chip_type == adm1031
1da177e4
LT
463 && data->temp[2] >=
464 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
465 break;
466 }
467 } else {
468 res = data->pwm[chan] > 0;
469 }
470 return res;
471}
472
473
c801082d
JD
474static ssize_t show_fan(struct device *dev,
475 struct device_attribute *attr, char *buf)
1da177e4 476{
c801082d 477 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
478 struct adm1031_data *data = adm1031_update_device(dev);
479 int value;
480
481 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
482 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
483 return sprintf(buf, "%d\n", value);
484}
485
c801082d
JD
486static ssize_t show_fan_div(struct device *dev,
487 struct device_attribute *attr, char *buf)
1da177e4 488{
c801082d 489 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
490 struct adm1031_data *data = adm1031_update_device(dev);
491 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
492}
c801082d
JD
493static ssize_t show_fan_min(struct device *dev,
494 struct device_attribute *attr, char *buf)
1da177e4 495{
c801082d 496 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
497 struct adm1031_data *data = adm1031_update_device(dev);
498 return sprintf(buf, "%d\n",
499 FAN_FROM_REG(data->fan_min[nr],
500 FAN_DIV_FROM_REG(data->fan_div[nr])));
501}
c801082d
JD
502static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
1da177e4
LT
504{
505 struct i2c_client *client = to_i2c_client(dev);
506 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 507 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
508 int val = simple_strtol(buf, NULL, 10);
509
9a61bf63 510 mutex_lock(&data->update_lock);
1da177e4 511 if (val) {
6d6006b8 512 data->fan_min[nr] =
1da177e4
LT
513 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
514 } else {
515 data->fan_min[nr] = 0xff;
516 }
517 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 518 mutex_unlock(&data->update_lock);
1da177e4
LT
519 return count;
520}
c801082d
JD
521static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
1da177e4
LT
523{
524 struct i2c_client *client = to_i2c_client(dev);
525 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 526 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
527 int val = simple_strtol(buf, NULL, 10);
528 u8 tmp;
529 int old_div;
530 int new_min;
531
532 tmp = val == 8 ? 0xc0 :
533 val == 4 ? 0x80 :
6d6006b8
JD
534 val == 2 ? 0x40 :
535 val == 1 ? 0x00 :
1da177e4
LT
536 0xff;
537 if (tmp == 0xff)
538 return -EINVAL;
6d6006b8 539
9a61bf63 540 mutex_lock(&data->update_lock);
38a1f0e9
JD
541 /* Get fresh readings */
542 data->fan_div[nr] = adm1031_read_value(client,
543 ADM1031_REG_FAN_DIV(nr));
544 data->fan_min[nr] = adm1031_read_value(client,
545 ADM1031_REG_FAN_MIN(nr));
546
547 /* Write the new clock divider and fan min */
1da177e4 548 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
6d6006b8
JD
549 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
550 new_min = data->fan_min[nr] * old_div / val;
1da177e4 551 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
1da177e4 552
6d6006b8 553 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
1da177e4 554 data->fan_div[nr]);
6d6006b8 555 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
1da177e4 556 data->fan_min[nr]);
38a1f0e9
JD
557
558 /* Invalidate the cache: fan speed is no longer valid */
559 data->valid = 0;
9a61bf63 560 mutex_unlock(&data->update_lock);
1da177e4
LT
561 return count;
562}
563
564#define fan_offset(offset) \
c801082d
JD
565static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
566 show_fan, NULL, offset - 1); \
567static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
568 show_fan_min, set_fan_min, offset - 1); \
569static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
570 show_fan_div, set_fan_div, offset - 1)
1da177e4
LT
571
572fan_offset(1);
573fan_offset(2);
574
575
576/* Temps */
c801082d
JD
577static ssize_t show_temp(struct device *dev,
578 struct device_attribute *attr, char *buf)
1da177e4 579{
c801082d 580 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
581 struct adm1031_data *data = adm1031_update_device(dev);
582 int ext;
583 ext = nr == 0 ?
584 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
585 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
586 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
587}
c801082d
JD
588static ssize_t show_temp_min(struct device *dev,
589 struct device_attribute *attr, char *buf)
1da177e4 590{
c801082d 591 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
592 struct adm1031_data *data = adm1031_update_device(dev);
593 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
594}
c801082d
JD
595static ssize_t show_temp_max(struct device *dev,
596 struct device_attribute *attr, char *buf)
1da177e4 597{
c801082d 598 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
599 struct adm1031_data *data = adm1031_update_device(dev);
600 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
601}
c801082d
JD
602static ssize_t show_temp_crit(struct device *dev,
603 struct device_attribute *attr, char *buf)
1da177e4 604{
c801082d 605 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
606 struct adm1031_data *data = adm1031_update_device(dev);
607 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
608}
c801082d
JD
609static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
610 const char *buf, size_t count)
1da177e4
LT
611{
612 struct i2c_client *client = to_i2c_client(dev);
613 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 614 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
615 int val;
616
617 val = simple_strtol(buf, NULL, 10);
618 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
9a61bf63 619 mutex_lock(&data->update_lock);
1da177e4
LT
620 data->temp_min[nr] = TEMP_TO_REG(val);
621 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
622 data->temp_min[nr]);
9a61bf63 623 mutex_unlock(&data->update_lock);
1da177e4
LT
624 return count;
625}
c801082d
JD
626static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
1da177e4
LT
628{
629 struct i2c_client *client = to_i2c_client(dev);
630 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 631 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
632 int val;
633
634 val = simple_strtol(buf, NULL, 10);
635 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
9a61bf63 636 mutex_lock(&data->update_lock);
1da177e4
LT
637 data->temp_max[nr] = TEMP_TO_REG(val);
638 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
639 data->temp_max[nr]);
9a61bf63 640 mutex_unlock(&data->update_lock);
1da177e4
LT
641 return count;
642}
c801082d
JD
643static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
644 const char *buf, size_t count)
1da177e4
LT
645{
646 struct i2c_client *client = to_i2c_client(dev);
647 struct adm1031_data *data = i2c_get_clientdata(client);
c801082d 648 int nr = to_sensor_dev_attr(attr)->index;
1da177e4
LT
649 int val;
650
651 val = simple_strtol(buf, NULL, 10);
652 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
9a61bf63 653 mutex_lock(&data->update_lock);
1da177e4
LT
654 data->temp_crit[nr] = TEMP_TO_REG(val);
655 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
656 data->temp_crit[nr]);
9a61bf63 657 mutex_unlock(&data->update_lock);
1da177e4
LT
658 return count;
659}
660
c801082d
JD
661#define temp_reg(offset) \
662static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
663 show_temp, NULL, offset - 1); \
664static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
665 show_temp_min, set_temp_min, offset - 1); \
666static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
667 show_temp_max, set_temp_max, offset - 1); \
668static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
669 show_temp_crit, set_temp_crit, offset - 1)
1da177e4
LT
670
671temp_reg(1);
672temp_reg(2);
673temp_reg(3);
674
675/* Alarms */
30f74292 676static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
677{
678 struct adm1031_data *data = adm1031_update_device(dev);
679 return sprintf(buf, "%d\n", data->alarm);
680}
681
682static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
683
050ab878
JD
684static ssize_t show_alarm(struct device *dev,
685 struct device_attribute *attr, char *buf)
686{
687 int bitnr = to_sensor_dev_attr(attr)->index;
688 struct adm1031_data *data = adm1031_update_device(dev);
689 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
690}
691
692static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
693static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
694static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
695static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
696static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
697static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
698static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
699static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
700static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
701static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
702static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
703static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
704static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
705static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
706static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
1da177e4 707
681c6f7a 708static struct attribute *adm1031_attributes[] = {
c801082d
JD
709 &sensor_dev_attr_fan1_input.dev_attr.attr,
710 &sensor_dev_attr_fan1_div.dev_attr.attr,
711 &sensor_dev_attr_fan1_min.dev_attr.attr,
050ab878
JD
712 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
713 &sensor_dev_attr_fan1_fault.dev_attr.attr,
c801082d
JD
714 &sensor_dev_attr_pwm1.dev_attr.attr,
715 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
716 &sensor_dev_attr_temp1_input.dev_attr.attr,
717 &sensor_dev_attr_temp1_min.dev_attr.attr,
050ab878 718 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
c801082d 719 &sensor_dev_attr_temp1_max.dev_attr.attr,
050ab878 720 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
c801082d 721 &sensor_dev_attr_temp1_crit.dev_attr.attr,
050ab878 722 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
c801082d
JD
723 &sensor_dev_attr_temp2_input.dev_attr.attr,
724 &sensor_dev_attr_temp2_min.dev_attr.attr,
050ab878 725 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
c801082d 726 &sensor_dev_attr_temp2_max.dev_attr.attr,
050ab878 727 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
c801082d 728 &sensor_dev_attr_temp2_crit.dev_attr.attr,
050ab878
JD
729 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
730 &sensor_dev_attr_temp2_fault.dev_attr.attr,
c801082d
JD
731
732 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
733 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
734 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
735
736 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
737 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
738 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
739
740 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
681c6f7a
MH
741
742 &dev_attr_alarms.attr,
743
744 NULL
745};
746
747static const struct attribute_group adm1031_group = {
748 .attrs = adm1031_attributes,
749};
750
751static struct attribute *adm1031_attributes_opt[] = {
c801082d
JD
752 &sensor_dev_attr_fan2_input.dev_attr.attr,
753 &sensor_dev_attr_fan2_div.dev_attr.attr,
754 &sensor_dev_attr_fan2_min.dev_attr.attr,
050ab878
JD
755 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
756 &sensor_dev_attr_fan2_fault.dev_attr.attr,
c801082d
JD
757 &sensor_dev_attr_pwm2.dev_attr.attr,
758 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
759 &sensor_dev_attr_temp3_input.dev_attr.attr,
760 &sensor_dev_attr_temp3_min.dev_attr.attr,
050ab878 761 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
c801082d 762 &sensor_dev_attr_temp3_max.dev_attr.attr,
050ab878 763 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
c801082d 764 &sensor_dev_attr_temp3_crit.dev_attr.attr,
050ab878
JD
765 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
766 &sensor_dev_attr_temp3_fault.dev_attr.attr,
c801082d
JD
767 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
768 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
769 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
770 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
681c6f7a
MH
771 NULL
772};
773
774static const struct attribute_group adm1031_group_opt = {
775 .attrs = adm1031_attributes_opt,
776};
777
af200f88
JD
778/* Return 0 if detection is successful, -ENODEV otherwise */
779static int adm1031_detect(struct i2c_client *client, int kind,
780 struct i2c_board_info *info)
1da177e4 781{
af200f88 782 struct i2c_adapter *adapter = client->adapter;
1da177e4
LT
783 const char *name = "";
784
785 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
af200f88 786 return -ENODEV;
1da177e4
LT
787
788 if (kind < 0) {
789 int id, co;
6d6006b8
JD
790 id = i2c_smbus_read_byte_data(client, 0x3d);
791 co = i2c_smbus_read_byte_data(client, 0x3e);
1da177e4
LT
792
793 if (!((id == 0x31 || id == 0x30) && co == 0x41))
af200f88 794 return -ENODEV;
1da177e4
LT
795 kind = (id == 0x30) ? adm1030 : adm1031;
796 }
797
798 if (kind <= 0)
799 kind = adm1031;
800
801 /* Given the detected chip type, set the chip name and the
802 * auto fan control helper table. */
803 if (kind == adm1030) {
804 name = "adm1030";
1da177e4
LT
805 } else if (kind == adm1031) {
806 name = "adm1031";
1da177e4 807 }
af200f88 808 strlcpy(info->type, name, I2C_NAME_SIZE);
1da177e4 809
af200f88
JD
810 return 0;
811}
812
813static int adm1031_probe(struct i2c_client *client,
814 const struct i2c_device_id *id)
815{
816 struct adm1031_data *data;
817 int err;
818
819 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
820 if (!data) {
821 err = -ENOMEM;
822 goto exit;
823 }
824
825 i2c_set_clientdata(client, data);
826 data->chip_type = id->driver_data;
9a61bf63 827 mutex_init(&data->update_lock);
1da177e4 828
af200f88
JD
829 if (data->chip_type == adm1030)
830 data->chan_select_table = &auto_channel_select_table_adm1030;
831 else
832 data->chan_select_table = &auto_channel_select_table_adm1031;
1da177e4
LT
833
834 /* Initialize the ADM1031 chip */
6d6006b8 835 adm1031_init_client(client);
1da177e4
LT
836
837 /* Register sysfs hooks */
6d6006b8 838 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
af200f88 839 goto exit_free;
1da177e4 840
af200f88 841 if (data->chip_type == adm1031) {
6d6006b8 842 if ((err = sysfs_create_group(&client->dev.kobj,
681c6f7a
MH
843 &adm1031_group_opt)))
844 goto exit_remove;
845 }
846
6d6006b8 847 data->hwmon_dev = hwmon_device_register(&client->dev);
1beeffe4
TJ
848 if (IS_ERR(data->hwmon_dev)) {
849 err = PTR_ERR(data->hwmon_dev);
681c6f7a 850 goto exit_remove;
1da177e4
LT
851 }
852
853 return 0;
854
681c6f7a 855exit_remove:
6d6006b8
JD
856 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
857 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
1da177e4 858exit_free:
1f57ff89 859 kfree(data);
1da177e4
LT
860exit:
861 return err;
862}
863
af200f88 864static int adm1031_remove(struct i2c_client *client)
1da177e4 865{
943b0830 866 struct adm1031_data *data = i2c_get_clientdata(client);
943b0830 867
1beeffe4 868 hwmon_device_unregister(data->hwmon_dev);
681c6f7a
MH
869 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
870 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
943b0830 871 kfree(data);
1da177e4
LT
872 return 0;
873}
874
875static void adm1031_init_client(struct i2c_client *client)
876{
877 unsigned int read_val;
878 unsigned int mask;
879 struct adm1031_data *data = i2c_get_clientdata(client);
880
881 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
882 if (data->chip_type == adm1031) {
883 mask |= (ADM1031_CONF2_PWM2_ENABLE |
884 ADM1031_CONF2_TACH2_ENABLE);
6d6006b8 885 }
1da177e4
LT
886 /* Initialize the ADM1031 chip (enables fan speed reading ) */
887 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
888 if ((read_val | mask) != read_val) {
889 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
890 }
891
892 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
893 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
894 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
895 ADM1031_CONF1_MONITOR_ENABLE);
896 }
897
898}
899
900static struct adm1031_data *adm1031_update_device(struct device *dev)
901{
902 struct i2c_client *client = to_i2c_client(dev);
903 struct adm1031_data *data = i2c_get_clientdata(client);
904 int chan;
905
9a61bf63 906 mutex_lock(&data->update_lock);
1da177e4
LT
907
908 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
909 || !data->valid) {
910
911 dev_dbg(&client->dev, "Starting adm1031 update\n");
912 for (chan = 0;
913 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
914 u8 oldh, newh;
915
916 oldh =
917 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
918 data->ext_temp[chan] =
919 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
920 newh =
921 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
922 if (newh != oldh) {
923 data->ext_temp[chan] =
924 adm1031_read_value(client,
925 ADM1031_REG_EXT_TEMP);
926#ifdef DEBUG
927 oldh =
928 adm1031_read_value(client,
929 ADM1031_REG_TEMP(chan));
930
931 /* oldh is actually newer */
932 if (newh != oldh)
933 dev_warn(&client->dev,
934 "Remote temperature may be "
935 "wrong.\n");
936#endif
937 }
938 data->temp[chan] = newh;
939
940 data->temp_min[chan] =
941 adm1031_read_value(client,
942 ADM1031_REG_TEMP_MIN(chan));
943 data->temp_max[chan] =
944 adm1031_read_value(client,
945 ADM1031_REG_TEMP_MAX(chan));
946 data->temp_crit[chan] =
947 adm1031_read_value(client,
948 ADM1031_REG_TEMP_CRIT(chan));
949 data->auto_temp[chan] =
950 adm1031_read_value(client,
951 ADM1031_REG_AUTO_TEMP(chan));
952
953 }
954
955 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
956 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
957
958 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
959 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
960 << 8);
961 if (data->chip_type == adm1030) {
962 data->alarm &= 0xc0ff;
963 }
6d6006b8 964
1da177e4
LT
965 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
966 data->fan_div[chan] =
967 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
968 data->fan_min[chan] =
969 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
970 data->fan[chan] =
971 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
972 data->pwm[chan] =
6d6006b8 973 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
1da177e4
LT
974 (4*chan));
975 }
976 data->last_updated = jiffies;
977 data->valid = 1;
978 }
979
9a61bf63 980 mutex_unlock(&data->update_lock);
1da177e4
LT
981
982 return data;
983}
984
985static int __init sensors_adm1031_init(void)
986{
987 return i2c_add_driver(&adm1031_driver);
988}
989
990static void __exit sensors_adm1031_exit(void)
991{
992 i2c_del_driver(&adm1031_driver);
993}
994
995MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
996MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
997MODULE_LICENSE("GPL");
998
999module_init(sensors_adm1031_init);
1000module_exit(sensors_adm1031_exit);