2 adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
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>
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.
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.
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.
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>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
34 /* Following macros takes channel parameter starting from 0 to 2 */
35 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
36 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
37 #define ADM1031_REG_PWM (0x22)
38 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
40 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
41 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
42 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
43 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
45 #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
46 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
48 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
50 #define ADM1031_REG_CONF1 0x00
51 #define ADM1031_REG_CONF2 0x01
52 #define ADM1031_REG_EXT_TEMP 0x06
54 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
55 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
56 #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
58 #define ADM1031_CONF2_PWM1_ENABLE 0x01
59 #define ADM1031_CONF2_PWM2_ENABLE 0x02
60 #define ADM1031_CONF2_TACH1_ENABLE 0x04
61 #define ADM1031_CONF2_TACH2_ENABLE 0x08
62 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
64 /* Addresses to scan */
65 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
67 enum chips
{ adm1030
, adm1031
};
69 typedef u8 auto_chan_table_t
[8][2];
71 /* Each client has this additional data */
73 struct device
*hwmon_dev
;
74 struct mutex update_lock
;
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
81 const auto_chan_table_t
*chan_select_table
;
102 static int adm1031_probe(struct i2c_client
*client
,
103 const struct i2c_device_id
*id
);
104 static int adm1031_detect(struct i2c_client
*client
,
105 struct i2c_board_info
*info
);
106 static void adm1031_init_client(struct i2c_client
*client
);
107 static int adm1031_remove(struct i2c_client
*client
);
108 static struct adm1031_data
*adm1031_update_device(struct device
*dev
);
110 static const struct i2c_device_id adm1031_id
[] = {
111 { "adm1030", adm1030
},
112 { "adm1031", adm1031
},
115 MODULE_DEVICE_TABLE(i2c
, adm1031_id
);
117 /* This is the driver that will be inserted */
118 static struct i2c_driver adm1031_driver
= {
119 .class = I2C_CLASS_HWMON
,
123 .probe
= adm1031_probe
,
124 .remove
= adm1031_remove
,
125 .id_table
= adm1031_id
,
126 .detect
= adm1031_detect
,
127 .address_list
= normal_i2c
,
130 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
132 return i2c_smbus_read_byte_data(client
, reg
);
136 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
138 return i2c_smbus_write_byte_data(client
, reg
, value
);
142 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
143 ((val + 500) / 1000)))
145 #define TEMP_FROM_REG(val) ((val) * 1000)
147 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
149 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
150 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
151 (val) | 0x70 : (val))
153 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
155 static int FAN_TO_REG(int reg
, int div
)
158 tmp
= FAN_FROM_REG(SENSORS_LIMIT(reg
, 0, 65535), div
);
159 return tmp
> 255 ? 255 : tmp
;
162 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
164 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
165 #define PWM_FROM_REG(val) ((val) << 4)
167 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
168 #define FAN_CHAN_TO_REG(val, reg) \
169 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
171 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
172 ((((val)/500) & 0xf8)|((reg) & 0x7))
173 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
174 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
176 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
178 #define AUTO_TEMP_OFF_FROM_REG(reg) \
179 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
181 #define AUTO_TEMP_MAX_FROM_REG(reg) \
182 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
183 AUTO_TEMP_MIN_FROM_REG(reg))
185 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
188 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
190 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
191 ret
= ((reg
& 0xf8) |
194 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
198 /* FAN auto control */
199 #define GET_FAN_AUTO_BITFIELD(data, idx) \
200 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
202 /* The tables below contains the possible values for the auto fan
203 * control bitfields. the index in the table is the register value.
204 * MSb is the auto fan control enable bit, so the four first entries
205 * in the table disables auto fan control when both bitfields are zero.
207 static const auto_chan_table_t auto_channel_select_table_adm1031
= {
208 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
209 { 2 /* 0b010 */ , 4 /* 0b100 */ },
210 { 2 /* 0b010 */ , 2 /* 0b010 */ },
211 { 4 /* 0b100 */ , 4 /* 0b100 */ },
212 { 7 /* 0b111 */ , 7 /* 0b111 */ },
215 static const auto_chan_table_t auto_channel_select_table_adm1030
= {
216 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
217 { 2 /* 0b10 */ , 0 },
218 { 0xff /* invalid */ , 0 },
219 { 0xff /* invalid */ , 0 },
220 { 3 /* 0b11 */ , 0 },
223 /* That function checks if a bitfield is valid and returns the other bitfield
224 * nearest match if no exact match where found.
227 get_fan_auto_nearest(struct adm1031_data
*data
,
228 int chan
, u8 val
, u8 reg
, u8
* new_reg
)
231 int first_match
= -1, exact_match
= -1;
233 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
240 for (i
= 0; i
< 8; i
++) {
241 if ((val
== (*data
->chan_select_table
)[i
][chan
]) &&
242 ((*data
->chan_select_table
)[i
][chan
? 0 : 1] ==
244 /* We found an exact match */
247 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
249 /* Save the first match in case of an exact match has
256 if (exact_match
>= 0) {
257 *new_reg
= exact_match
;
258 } else if (first_match
>= 0) {
259 *new_reg
= first_match
;
266 static ssize_t
show_fan_auto_channel(struct device
*dev
,
267 struct device_attribute
*attr
, char *buf
)
269 int nr
= to_sensor_dev_attr(attr
)->index
;
270 struct adm1031_data
*data
= adm1031_update_device(dev
);
271 return sprintf(buf
, "%d\n", GET_FAN_AUTO_BITFIELD(data
, nr
));
275 set_fan_auto_channel(struct device
*dev
, struct device_attribute
*attr
,
276 const char *buf
, size_t count
)
278 struct i2c_client
*client
= to_i2c_client(dev
);
279 struct adm1031_data
*data
= i2c_get_clientdata(client
);
280 int nr
= to_sensor_dev_attr(attr
)->index
;
281 int val
= simple_strtol(buf
, NULL
, 10);
286 old_fan_mode
= data
->conf1
;
288 mutex_lock(&data
->update_lock
);
290 if ((ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
, ®
))) {
291 mutex_unlock(&data
->update_lock
);
294 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
295 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) ^
296 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
297 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
){
298 /* Switch to Auto Fan Mode
300 * Set PWM registers to 33% Both */
301 data
->old_pwm
[0] = data
->pwm
[0];
302 data
->old_pwm
[1] = data
->pwm
[1];
303 adm1031_write_value(client
, ADM1031_REG_PWM
, 0x55);
305 /* Switch to Manual Mode */
306 data
->pwm
[0] = data
->old_pwm
[0];
307 data
->pwm
[1] = data
->old_pwm
[1];
308 /* Restore PWM registers */
309 adm1031_write_value(client
, ADM1031_REG_PWM
,
310 data
->pwm
[0] | (data
->pwm
[1] << 4));
313 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
314 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
315 mutex_unlock(&data
->update_lock
);
319 static SENSOR_DEVICE_ATTR(auto_fan1_channel
, S_IRUGO
| S_IWUSR
,
320 show_fan_auto_channel
, set_fan_auto_channel
, 0);
321 static SENSOR_DEVICE_ATTR(auto_fan2_channel
, S_IRUGO
| S_IWUSR
,
322 show_fan_auto_channel
, set_fan_auto_channel
, 1);
325 static ssize_t
show_auto_temp_off(struct device
*dev
,
326 struct device_attribute
*attr
, char *buf
)
328 int nr
= to_sensor_dev_attr(attr
)->index
;
329 struct adm1031_data
*data
= adm1031_update_device(dev
);
330 return sprintf(buf
, "%d\n",
331 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
333 static ssize_t
show_auto_temp_min(struct device
*dev
,
334 struct device_attribute
*attr
, char *buf
)
336 int nr
= to_sensor_dev_attr(attr
)->index
;
337 struct adm1031_data
*data
= adm1031_update_device(dev
);
338 return sprintf(buf
, "%d\n",
339 AUTO_TEMP_MIN_FROM_REG(data
->auto_temp
[nr
]));
342 set_auto_temp_min(struct device
*dev
, struct device_attribute
*attr
,
343 const char *buf
, size_t count
)
345 struct i2c_client
*client
= to_i2c_client(dev
);
346 struct adm1031_data
*data
= i2c_get_clientdata(client
);
347 int nr
= to_sensor_dev_attr(attr
)->index
;
348 int val
= simple_strtol(buf
, NULL
, 10);
350 mutex_lock(&data
->update_lock
);
351 data
->auto_temp
[nr
] = AUTO_TEMP_MIN_TO_REG(val
, data
->auto_temp
[nr
]);
352 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
353 data
->auto_temp
[nr
]);
354 mutex_unlock(&data
->update_lock
);
357 static ssize_t
show_auto_temp_max(struct device
*dev
,
358 struct device_attribute
*attr
, char *buf
)
360 int nr
= to_sensor_dev_attr(attr
)->index
;
361 struct adm1031_data
*data
= adm1031_update_device(dev
);
362 return sprintf(buf
, "%d\n",
363 AUTO_TEMP_MAX_FROM_REG(data
->auto_temp
[nr
]));
366 set_auto_temp_max(struct device
*dev
, struct device_attribute
*attr
,
367 const char *buf
, size_t count
)
369 struct i2c_client
*client
= to_i2c_client(dev
);
370 struct adm1031_data
*data
= i2c_get_clientdata(client
);
371 int nr
= to_sensor_dev_attr(attr
)->index
;
372 int val
= simple_strtol(buf
, NULL
, 10);
374 mutex_lock(&data
->update_lock
);
375 data
->temp_max
[nr
] = AUTO_TEMP_MAX_TO_REG(val
, data
->auto_temp
[nr
], data
->pwm
[nr
]);
376 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
378 mutex_unlock(&data
->update_lock
);
382 #define auto_temp_reg(offset) \
383 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
384 show_auto_temp_off, NULL, offset - 1); \
385 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
386 show_auto_temp_min, set_auto_temp_min, offset - 1); \
387 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
388 show_auto_temp_max, set_auto_temp_max, offset - 1)
395 static ssize_t
show_pwm(struct device
*dev
,
396 struct device_attribute
*attr
, char *buf
)
398 int nr
= to_sensor_dev_attr(attr
)->index
;
399 struct adm1031_data
*data
= adm1031_update_device(dev
);
400 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
402 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
403 const char *buf
, size_t count
)
405 struct i2c_client
*client
= to_i2c_client(dev
);
406 struct adm1031_data
*data
= i2c_get_clientdata(client
);
407 int nr
= to_sensor_dev_attr(attr
)->index
;
408 int val
= simple_strtol(buf
, NULL
, 10);
411 mutex_lock(&data
->update_lock
);
412 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
413 (((val
>>4) & 0xf) != 5)) {
414 /* In automatic mode, the only PWM accepted is 33% */
415 mutex_unlock(&data
->update_lock
);
418 data
->pwm
[nr
] = PWM_TO_REG(val
);
419 reg
= adm1031_read_value(client
, ADM1031_REG_PWM
);
420 adm1031_write_value(client
, ADM1031_REG_PWM
,
421 nr
? ((data
->pwm
[nr
] << 4) & 0xf0) | (reg
& 0xf)
422 : (data
->pwm
[nr
] & 0xf) | (reg
& 0xf0));
423 mutex_unlock(&data
->update_lock
);
427 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 0);
428 static SENSOR_DEVICE_ATTR(pwm2
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 1);
429 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm
, S_IRUGO
| S_IWUSR
,
430 show_pwm
, set_pwm
, 0);
431 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm
, S_IRUGO
| S_IWUSR
,
432 show_pwm
, set_pwm
, 1);
437 * That function checks the cases where the fan reading is not
438 * relevant. It is used to provide 0 as fan reading when the fan is
439 * not supposed to run
441 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
445 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
446 switch (data
->conf1
& 0x60) {
447 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
448 res
= data
->temp
[chan
+1] >=
449 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[chan
+1]);
451 case 0x20: /* remote temp1 controls both fans */
454 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
456 case 0x40: /* remote temp2 controls both fans */
459 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
461 case 0x60: /* max controls both fans */
464 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
466 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
467 || (data
->chip_type
== adm1031
469 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
473 res
= data
->pwm
[chan
] > 0;
479 static ssize_t
show_fan(struct device
*dev
,
480 struct device_attribute
*attr
, char *buf
)
482 int nr
= to_sensor_dev_attr(attr
)->index
;
483 struct adm1031_data
*data
= adm1031_update_device(dev
);
486 value
= trust_fan_readings(data
, nr
) ? FAN_FROM_REG(data
->fan
[nr
],
487 FAN_DIV_FROM_REG(data
->fan_div
[nr
])) : 0;
488 return sprintf(buf
, "%d\n", value
);
491 static ssize_t
show_fan_div(struct device
*dev
,
492 struct device_attribute
*attr
, char *buf
)
494 int nr
= to_sensor_dev_attr(attr
)->index
;
495 struct adm1031_data
*data
= adm1031_update_device(dev
);
496 return sprintf(buf
, "%d\n", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
498 static ssize_t
show_fan_min(struct device
*dev
,
499 struct device_attribute
*attr
, char *buf
)
501 int nr
= to_sensor_dev_attr(attr
)->index
;
502 struct adm1031_data
*data
= adm1031_update_device(dev
);
503 return sprintf(buf
, "%d\n",
504 FAN_FROM_REG(data
->fan_min
[nr
],
505 FAN_DIV_FROM_REG(data
->fan_div
[nr
])));
507 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
508 const char *buf
, size_t count
)
510 struct i2c_client
*client
= to_i2c_client(dev
);
511 struct adm1031_data
*data
= i2c_get_clientdata(client
);
512 int nr
= to_sensor_dev_attr(attr
)->index
;
513 int val
= simple_strtol(buf
, NULL
, 10);
515 mutex_lock(&data
->update_lock
);
518 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
520 data
->fan_min
[nr
] = 0xff;
522 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
523 mutex_unlock(&data
->update_lock
);
526 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
527 const char *buf
, size_t count
)
529 struct i2c_client
*client
= to_i2c_client(dev
);
530 struct adm1031_data
*data
= i2c_get_clientdata(client
);
531 int nr
= to_sensor_dev_attr(attr
)->index
;
532 int val
= simple_strtol(buf
, NULL
, 10);
537 tmp
= val
== 8 ? 0xc0 :
545 mutex_lock(&data
->update_lock
);
546 /* Get fresh readings */
547 data
->fan_div
[nr
] = adm1031_read_value(client
,
548 ADM1031_REG_FAN_DIV(nr
));
549 data
->fan_min
[nr
] = adm1031_read_value(client
,
550 ADM1031_REG_FAN_MIN(nr
));
552 /* Write the new clock divider and fan min */
553 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
554 data
->fan_div
[nr
] = tmp
| (0x3f & data
->fan_div
[nr
]);
555 new_min
= data
->fan_min
[nr
] * old_div
/ val
;
556 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
558 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
560 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
563 /* Invalidate the cache: fan speed is no longer valid */
565 mutex_unlock(&data
->update_lock
);
569 #define fan_offset(offset) \
570 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
571 show_fan, NULL, offset - 1); \
572 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
573 show_fan_min, set_fan_min, offset - 1); \
574 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
575 show_fan_div, set_fan_div, offset - 1)
582 static ssize_t
show_temp(struct device
*dev
,
583 struct device_attribute
*attr
, char *buf
)
585 int nr
= to_sensor_dev_attr(attr
)->index
;
586 struct adm1031_data
*data
= adm1031_update_device(dev
);
589 ((data
->ext_temp
[nr
] >> 6) & 0x3) * 2 :
590 (((data
->ext_temp
[nr
] >> ((nr
- 1) * 3)) & 7));
591 return sprintf(buf
, "%d\n", TEMP_FROM_REG_EXT(data
->temp
[nr
], ext
));
593 static ssize_t
show_temp_offset(struct device
*dev
,
594 struct device_attribute
*attr
, char *buf
)
596 int nr
= to_sensor_dev_attr(attr
)->index
;
597 struct adm1031_data
*data
= adm1031_update_device(dev
);
598 return sprintf(buf
, "%d\n",
599 TEMP_OFFSET_FROM_REG(data
->temp_offset
[nr
]));
601 static ssize_t
show_temp_min(struct device
*dev
,
602 struct device_attribute
*attr
, char *buf
)
604 int nr
= to_sensor_dev_attr(attr
)->index
;
605 struct adm1031_data
*data
= adm1031_update_device(dev
);
606 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
608 static ssize_t
show_temp_max(struct device
*dev
,
609 struct device_attribute
*attr
, char *buf
)
611 int nr
= to_sensor_dev_attr(attr
)->index
;
612 struct adm1031_data
*data
= adm1031_update_device(dev
);
613 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
615 static ssize_t
show_temp_crit(struct device
*dev
,
616 struct device_attribute
*attr
, char *buf
)
618 int nr
= to_sensor_dev_attr(attr
)->index
;
619 struct adm1031_data
*data
= adm1031_update_device(dev
);
620 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[nr
]));
622 static ssize_t
set_temp_offset(struct device
*dev
,
623 struct device_attribute
*attr
, const char *buf
,
626 struct i2c_client
*client
= to_i2c_client(dev
);
627 struct adm1031_data
*data
= i2c_get_clientdata(client
);
628 int nr
= to_sensor_dev_attr(attr
)->index
;
631 val
= simple_strtol(buf
, NULL
, 10);
632 val
= SENSORS_LIMIT(val
, -15000, 15000);
633 mutex_lock(&data
->update_lock
);
634 data
->temp_offset
[nr
] = TEMP_OFFSET_TO_REG(val
);
635 adm1031_write_value(client
, ADM1031_REG_TEMP_OFFSET(nr
),
636 data
->temp_offset
[nr
]);
637 mutex_unlock(&data
->update_lock
);
640 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
641 const char *buf
, size_t count
)
643 struct i2c_client
*client
= to_i2c_client(dev
);
644 struct adm1031_data
*data
= i2c_get_clientdata(client
);
645 int nr
= to_sensor_dev_attr(attr
)->index
;
648 val
= simple_strtol(buf
, NULL
, 10);
649 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
650 mutex_lock(&data
->update_lock
);
651 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
652 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
654 mutex_unlock(&data
->update_lock
);
657 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
658 const char *buf
, size_t count
)
660 struct i2c_client
*client
= to_i2c_client(dev
);
661 struct adm1031_data
*data
= i2c_get_clientdata(client
);
662 int nr
= to_sensor_dev_attr(attr
)->index
;
665 val
= simple_strtol(buf
, NULL
, 10);
666 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
667 mutex_lock(&data
->update_lock
);
668 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
669 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
671 mutex_unlock(&data
->update_lock
);
674 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*attr
,
675 const char *buf
, size_t count
)
677 struct i2c_client
*client
= to_i2c_client(dev
);
678 struct adm1031_data
*data
= i2c_get_clientdata(client
);
679 int nr
= to_sensor_dev_attr(attr
)->index
;
682 val
= simple_strtol(buf
, NULL
, 10);
683 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
684 mutex_lock(&data
->update_lock
);
685 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
686 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
687 data
->temp_crit
[nr
]);
688 mutex_unlock(&data
->update_lock
);
692 #define temp_reg(offset) \
693 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
694 show_temp, NULL, offset - 1); \
695 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
696 show_temp_offset, set_temp_offset, offset - 1); \
697 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
698 show_temp_min, set_temp_min, offset - 1); \
699 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
700 show_temp_max, set_temp_max, offset - 1); \
701 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
702 show_temp_crit, set_temp_crit, offset - 1)
709 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
711 struct adm1031_data
*data
= adm1031_update_device(dev
);
712 return sprintf(buf
, "%d\n", data
->alarm
);
715 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
717 static ssize_t
show_alarm(struct device
*dev
,
718 struct device_attribute
*attr
, char *buf
)
720 int bitnr
= to_sensor_dev_attr(attr
)->index
;
721 struct adm1031_data
*data
= adm1031_update_device(dev
);
722 return sprintf(buf
, "%d\n", (data
->alarm
>> bitnr
) & 1);
725 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
726 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, show_alarm
, NULL
, 1);
727 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
728 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
729 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
730 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 5);
731 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
732 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
733 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
734 static SENSOR_DEVICE_ATTR(fan2_fault
, S_IRUGO
, show_alarm
, NULL
, 9);
735 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
736 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
737 static SENSOR_DEVICE_ATTR(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
738 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 13);
739 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
741 static struct attribute
*adm1031_attributes
[] = {
742 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
743 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
744 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
745 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
746 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
747 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
748 &sensor_dev_attr_auto_fan1_channel
.dev_attr
.attr
,
749 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
750 &sensor_dev_attr_temp1_offset
.dev_attr
.attr
,
751 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
752 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
753 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
754 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
755 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
756 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
757 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
758 &sensor_dev_attr_temp2_offset
.dev_attr
.attr
,
759 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
760 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
761 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
762 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
763 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
764 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
765 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
767 &sensor_dev_attr_auto_temp1_off
.dev_attr
.attr
,
768 &sensor_dev_attr_auto_temp1_min
.dev_attr
.attr
,
769 &sensor_dev_attr_auto_temp1_max
.dev_attr
.attr
,
771 &sensor_dev_attr_auto_temp2_off
.dev_attr
.attr
,
772 &sensor_dev_attr_auto_temp2_min
.dev_attr
.attr
,
773 &sensor_dev_attr_auto_temp2_max
.dev_attr
.attr
,
775 &sensor_dev_attr_auto_fan1_min_pwm
.dev_attr
.attr
,
777 &dev_attr_alarms
.attr
,
782 static const struct attribute_group adm1031_group
= {
783 .attrs
= adm1031_attributes
,
786 static struct attribute
*adm1031_attributes_opt
[] = {
787 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
788 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
789 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
790 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
791 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
792 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
793 &sensor_dev_attr_auto_fan2_channel
.dev_attr
.attr
,
794 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
795 &sensor_dev_attr_temp3_offset
.dev_attr
.attr
,
796 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
797 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
798 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
799 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
800 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
801 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
802 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
803 &sensor_dev_attr_auto_temp3_off
.dev_attr
.attr
,
804 &sensor_dev_attr_auto_temp3_min
.dev_attr
.attr
,
805 &sensor_dev_attr_auto_temp3_max
.dev_attr
.attr
,
806 &sensor_dev_attr_auto_fan2_min_pwm
.dev_attr
.attr
,
810 static const struct attribute_group adm1031_group_opt
= {
811 .attrs
= adm1031_attributes_opt
,
814 /* Return 0 if detection is successful, -ENODEV otherwise */
815 static int adm1031_detect(struct i2c_client
*client
,
816 struct i2c_board_info
*info
)
818 struct i2c_adapter
*adapter
= client
->adapter
;
822 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
825 id
= i2c_smbus_read_byte_data(client
, 0x3d);
826 co
= i2c_smbus_read_byte_data(client
, 0x3e);
828 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
830 name
= (id
== 0x30) ? "adm1030" : "adm1031";
832 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
837 static int adm1031_probe(struct i2c_client
*client
,
838 const struct i2c_device_id
*id
)
840 struct adm1031_data
*data
;
843 data
= kzalloc(sizeof(struct adm1031_data
), GFP_KERNEL
);
849 i2c_set_clientdata(client
, data
);
850 data
->chip_type
= id
->driver_data
;
851 mutex_init(&data
->update_lock
);
853 if (data
->chip_type
== adm1030
)
854 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
856 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
858 /* Initialize the ADM1031 chip */
859 adm1031_init_client(client
);
861 /* Register sysfs hooks */
862 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &adm1031_group
)))
865 if (data
->chip_type
== adm1031
) {
866 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
867 &adm1031_group_opt
)))
871 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
872 if (IS_ERR(data
->hwmon_dev
)) {
873 err
= PTR_ERR(data
->hwmon_dev
);
880 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
881 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
888 static int adm1031_remove(struct i2c_client
*client
)
890 struct adm1031_data
*data
= i2c_get_clientdata(client
);
892 hwmon_device_unregister(data
->hwmon_dev
);
893 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
894 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
899 static void adm1031_init_client(struct i2c_client
*client
)
901 unsigned int read_val
;
903 struct adm1031_data
*data
= i2c_get_clientdata(client
);
905 mask
= (ADM1031_CONF2_PWM1_ENABLE
| ADM1031_CONF2_TACH1_ENABLE
);
906 if (data
->chip_type
== adm1031
) {
907 mask
|= (ADM1031_CONF2_PWM2_ENABLE
|
908 ADM1031_CONF2_TACH2_ENABLE
);
910 /* Initialize the ADM1031 chip (enables fan speed reading ) */
911 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
912 if ((read_val
| mask
) != read_val
) {
913 adm1031_write_value(client
, ADM1031_REG_CONF2
, read_val
| mask
);
916 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
917 if ((read_val
| ADM1031_CONF1_MONITOR_ENABLE
) != read_val
) {
918 adm1031_write_value(client
, ADM1031_REG_CONF1
, read_val
|
919 ADM1031_CONF1_MONITOR_ENABLE
);
924 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
926 struct i2c_client
*client
= to_i2c_client(dev
);
927 struct adm1031_data
*data
= i2c_get_clientdata(client
);
930 mutex_lock(&data
->update_lock
);
932 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
935 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
937 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
941 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
942 data
->ext_temp
[chan
] =
943 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
945 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
947 data
->ext_temp
[chan
] =
948 adm1031_read_value(client
,
949 ADM1031_REG_EXT_TEMP
);
952 adm1031_read_value(client
,
953 ADM1031_REG_TEMP(chan
));
955 /* oldh is actually newer */
957 dev_warn(&client
->dev
,
958 "Remote temperature may be "
962 data
->temp
[chan
] = newh
;
964 data
->temp_offset
[chan
] =
965 adm1031_read_value(client
,
966 ADM1031_REG_TEMP_OFFSET(chan
));
967 data
->temp_min
[chan
] =
968 adm1031_read_value(client
,
969 ADM1031_REG_TEMP_MIN(chan
));
970 data
->temp_max
[chan
] =
971 adm1031_read_value(client
,
972 ADM1031_REG_TEMP_MAX(chan
));
973 data
->temp_crit
[chan
] =
974 adm1031_read_value(client
,
975 ADM1031_REG_TEMP_CRIT(chan
));
976 data
->auto_temp
[chan
] =
977 adm1031_read_value(client
,
978 ADM1031_REG_AUTO_TEMP(chan
));
982 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
983 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
985 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
986 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1))
988 if (data
->chip_type
== adm1030
) {
989 data
->alarm
&= 0xc0ff;
992 for (chan
=0; chan
<(data
->chip_type
== adm1030
? 1 : 2); chan
++) {
993 data
->fan_div
[chan
] =
994 adm1031_read_value(client
, ADM1031_REG_FAN_DIV(chan
));
995 data
->fan_min
[chan
] =
996 adm1031_read_value(client
, ADM1031_REG_FAN_MIN(chan
));
998 adm1031_read_value(client
, ADM1031_REG_FAN_SPEED(chan
));
1000 0xf & (adm1031_read_value(client
, ADM1031_REG_PWM
) >>
1003 data
->last_updated
= jiffies
;
1007 mutex_unlock(&data
->update_lock
);
1012 static int __init
sensors_adm1031_init(void)
1014 return i2c_add_driver(&adm1031_driver
);
1017 static void __exit
sensors_adm1031_exit(void)
1019 i2c_del_driver(&adm1031_driver
);
1022 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1023 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1024 MODULE_LICENSE("GPL");
1026 module_init(sensors_adm1031_init
);
1027 module_exit(sensors_adm1031_exit
);