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 /* Insmod parameters */
68 I2C_CLIENT_INSMOD_2(adm1030
, adm1031
);
70 typedef u8 auto_chan_table_t
[8][2];
72 /* Each client has this additional data */
74 struct device
*hwmon_dev
;
75 struct mutex update_lock
;
77 char valid
; /* !=0 if following fields are valid */
78 unsigned long last_updated
; /* In jiffies */
79 /* The chan_select_table contains the possible configurations for
82 const auto_chan_table_t
*chan_select_table
;
103 static int adm1031_probe(struct i2c_client
*client
,
104 const struct i2c_device_id
*id
);
105 static int adm1031_detect(struct i2c_client
*client
, int kind
,
106 struct i2c_board_info
*info
);
107 static void adm1031_init_client(struct i2c_client
*client
);
108 static int adm1031_remove(struct i2c_client
*client
);
109 static struct adm1031_data
*adm1031_update_device(struct device
*dev
);
111 static const struct i2c_device_id adm1031_id
[] = {
112 { "adm1030", adm1030
},
113 { "adm1031", adm1031
},
116 MODULE_DEVICE_TABLE(i2c
, adm1031_id
);
118 /* This is the driver that will be inserted */
119 static struct i2c_driver adm1031_driver
= {
120 .class = I2C_CLASS_HWMON
,
124 .probe
= adm1031_probe
,
125 .remove
= adm1031_remove
,
126 .id_table
= adm1031_id
,
127 .detect
= adm1031_detect
,
128 .address_data
= &addr_data
,
131 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
133 return i2c_smbus_read_byte_data(client
, reg
);
137 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
139 return i2c_smbus_write_byte_data(client
, reg
, value
);
143 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
144 ((val + 500) / 1000)))
146 #define TEMP_FROM_REG(val) ((val) * 1000)
148 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
150 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
151 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
152 (val) | 0x70 : (val))
154 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
156 static int FAN_TO_REG(int reg
, int div
)
159 tmp
= FAN_FROM_REG(SENSORS_LIMIT(reg
, 0, 65535), div
);
160 return tmp
> 255 ? 255 : tmp
;
163 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
165 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
166 #define PWM_FROM_REG(val) ((val) << 4)
168 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
169 #define FAN_CHAN_TO_REG(val, reg) \
170 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
172 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
173 ((((val)/500) & 0xf8)|((reg) & 0x7))
174 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
175 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
177 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
179 #define AUTO_TEMP_OFF_FROM_REG(reg) \
180 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
182 #define AUTO_TEMP_MAX_FROM_REG(reg) \
183 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
184 AUTO_TEMP_MIN_FROM_REG(reg))
186 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
189 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
191 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
192 ret
= ((reg
& 0xf8) |
195 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
199 /* FAN auto control */
200 #define GET_FAN_AUTO_BITFIELD(data, idx) \
201 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
203 /* The tables below contains the possible values for the auto fan
204 * control bitfields. the index in the table is the register value.
205 * MSb is the auto fan control enable bit, so the four first entries
206 * in the table disables auto fan control when both bitfields are zero.
208 static const auto_chan_table_t auto_channel_select_table_adm1031
= {
209 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
210 { 2 /* 0b010 */ , 4 /* 0b100 */ },
211 { 2 /* 0b010 */ , 2 /* 0b010 */ },
212 { 4 /* 0b100 */ , 4 /* 0b100 */ },
213 { 7 /* 0b111 */ , 7 /* 0b111 */ },
216 static const auto_chan_table_t auto_channel_select_table_adm1030
= {
217 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
218 { 2 /* 0b10 */ , 0 },
219 { 0xff /* invalid */ , 0 },
220 { 0xff /* invalid */ , 0 },
221 { 3 /* 0b11 */ , 0 },
224 /* That function checks if a bitfield is valid and returns the other bitfield
225 * nearest match if no exact match where found.
228 get_fan_auto_nearest(struct adm1031_data
*data
,
229 int chan
, u8 val
, u8 reg
, u8
* new_reg
)
232 int first_match
= -1, exact_match
= -1;
234 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
241 for (i
= 0; i
< 8; i
++) {
242 if ((val
== (*data
->chan_select_table
)[i
][chan
]) &&
243 ((*data
->chan_select_table
)[i
][chan
? 0 : 1] ==
245 /* We found an exact match */
248 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
250 /* Save the first match in case of an exact match has
257 if (exact_match
>= 0) {
258 *new_reg
= exact_match
;
259 } else if (first_match
>= 0) {
260 *new_reg
= first_match
;
267 static ssize_t
show_fan_auto_channel(struct device
*dev
,
268 struct device_attribute
*attr
, char *buf
)
270 int nr
= to_sensor_dev_attr(attr
)->index
;
271 struct adm1031_data
*data
= adm1031_update_device(dev
);
272 return sprintf(buf
, "%d\n", GET_FAN_AUTO_BITFIELD(data
, nr
));
276 set_fan_auto_channel(struct device
*dev
, struct device_attribute
*attr
,
277 const char *buf
, size_t count
)
279 struct i2c_client
*client
= to_i2c_client(dev
);
280 struct adm1031_data
*data
= i2c_get_clientdata(client
);
281 int nr
= to_sensor_dev_attr(attr
)->index
;
282 int val
= simple_strtol(buf
, NULL
, 10);
287 old_fan_mode
= data
->conf1
;
289 mutex_lock(&data
->update_lock
);
291 if ((ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
, ®
))) {
292 mutex_unlock(&data
->update_lock
);
295 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
296 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) ^
297 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
298 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
){
299 /* Switch to Auto Fan Mode
301 * Set PWM registers to 33% Both */
302 data
->old_pwm
[0] = data
->pwm
[0];
303 data
->old_pwm
[1] = data
->pwm
[1];
304 adm1031_write_value(client
, ADM1031_REG_PWM
, 0x55);
306 /* Switch to Manual Mode */
307 data
->pwm
[0] = data
->old_pwm
[0];
308 data
->pwm
[1] = data
->old_pwm
[1];
309 /* Restore PWM registers */
310 adm1031_write_value(client
, ADM1031_REG_PWM
,
311 data
->pwm
[0] | (data
->pwm
[1] << 4));
314 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
315 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
316 mutex_unlock(&data
->update_lock
);
320 static SENSOR_DEVICE_ATTR(auto_fan1_channel
, S_IRUGO
| S_IWUSR
,
321 show_fan_auto_channel
, set_fan_auto_channel
, 0);
322 static SENSOR_DEVICE_ATTR(auto_fan2_channel
, S_IRUGO
| S_IWUSR
,
323 show_fan_auto_channel
, set_fan_auto_channel
, 1);
326 static ssize_t
show_auto_temp_off(struct device
*dev
,
327 struct device_attribute
*attr
, char *buf
)
329 int nr
= to_sensor_dev_attr(attr
)->index
;
330 struct adm1031_data
*data
= adm1031_update_device(dev
);
331 return sprintf(buf
, "%d\n",
332 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
334 static ssize_t
show_auto_temp_min(struct device
*dev
,
335 struct device_attribute
*attr
, char *buf
)
337 int nr
= to_sensor_dev_attr(attr
)->index
;
338 struct adm1031_data
*data
= adm1031_update_device(dev
);
339 return sprintf(buf
, "%d\n",
340 AUTO_TEMP_MIN_FROM_REG(data
->auto_temp
[nr
]));
343 set_auto_temp_min(struct device
*dev
, struct device_attribute
*attr
,
344 const char *buf
, size_t count
)
346 struct i2c_client
*client
= to_i2c_client(dev
);
347 struct adm1031_data
*data
= i2c_get_clientdata(client
);
348 int nr
= to_sensor_dev_attr(attr
)->index
;
349 int val
= simple_strtol(buf
, NULL
, 10);
351 mutex_lock(&data
->update_lock
);
352 data
->auto_temp
[nr
] = AUTO_TEMP_MIN_TO_REG(val
, data
->auto_temp
[nr
]);
353 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
354 data
->auto_temp
[nr
]);
355 mutex_unlock(&data
->update_lock
);
358 static ssize_t
show_auto_temp_max(struct device
*dev
,
359 struct device_attribute
*attr
, char *buf
)
361 int nr
= to_sensor_dev_attr(attr
)->index
;
362 struct adm1031_data
*data
= adm1031_update_device(dev
);
363 return sprintf(buf
, "%d\n",
364 AUTO_TEMP_MAX_FROM_REG(data
->auto_temp
[nr
]));
367 set_auto_temp_max(struct device
*dev
, struct device_attribute
*attr
,
368 const char *buf
, size_t count
)
370 struct i2c_client
*client
= to_i2c_client(dev
);
371 struct adm1031_data
*data
= i2c_get_clientdata(client
);
372 int nr
= to_sensor_dev_attr(attr
)->index
;
373 int val
= simple_strtol(buf
, NULL
, 10);
375 mutex_lock(&data
->update_lock
);
376 data
->temp_max
[nr
] = AUTO_TEMP_MAX_TO_REG(val
, data
->auto_temp
[nr
], data
->pwm
[nr
]);
377 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
379 mutex_unlock(&data
->update_lock
);
383 #define auto_temp_reg(offset) \
384 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
385 show_auto_temp_off, NULL, offset - 1); \
386 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
387 show_auto_temp_min, set_auto_temp_min, offset - 1); \
388 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
389 show_auto_temp_max, set_auto_temp_max, offset - 1)
396 static ssize_t
show_pwm(struct device
*dev
,
397 struct device_attribute
*attr
, char *buf
)
399 int nr
= to_sensor_dev_attr(attr
)->index
;
400 struct adm1031_data
*data
= adm1031_update_device(dev
);
401 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
403 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
404 const char *buf
, size_t count
)
406 struct i2c_client
*client
= to_i2c_client(dev
);
407 struct adm1031_data
*data
= i2c_get_clientdata(client
);
408 int nr
= to_sensor_dev_attr(attr
)->index
;
409 int val
= simple_strtol(buf
, NULL
, 10);
412 mutex_lock(&data
->update_lock
);
413 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
414 (((val
>>4) & 0xf) != 5)) {
415 /* In automatic mode, the only PWM accepted is 33% */
416 mutex_unlock(&data
->update_lock
);
419 data
->pwm
[nr
] = PWM_TO_REG(val
);
420 reg
= adm1031_read_value(client
, ADM1031_REG_PWM
);
421 adm1031_write_value(client
, ADM1031_REG_PWM
,
422 nr
? ((data
->pwm
[nr
] << 4) & 0xf0) | (reg
& 0xf)
423 : (data
->pwm
[nr
] & 0xf) | (reg
& 0xf0));
424 mutex_unlock(&data
->update_lock
);
428 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 0);
429 static SENSOR_DEVICE_ATTR(pwm2
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 1);
430 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm
, S_IRUGO
| S_IWUSR
,
431 show_pwm
, set_pwm
, 0);
432 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm
, S_IRUGO
| S_IWUSR
,
433 show_pwm
, set_pwm
, 1);
438 * That function checks the cases where the fan reading is not
439 * relevant. It is used to provide 0 as fan reading when the fan is
440 * not supposed to run
442 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
446 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
447 switch (data
->conf1
& 0x60) {
448 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
449 res
= data
->temp
[chan
+1] >=
450 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[chan
+1]);
452 case 0x20: /* remote temp1 controls both fans */
455 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
457 case 0x40: /* remote temp2 controls both fans */
460 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
462 case 0x60: /* max controls both fans */
465 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
467 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
468 || (data
->chip_type
== adm1031
470 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
474 res
= data
->pwm
[chan
] > 0;
480 static ssize_t
show_fan(struct device
*dev
,
481 struct device_attribute
*attr
, char *buf
)
483 int nr
= to_sensor_dev_attr(attr
)->index
;
484 struct adm1031_data
*data
= adm1031_update_device(dev
);
487 value
= trust_fan_readings(data
, nr
) ? FAN_FROM_REG(data
->fan
[nr
],
488 FAN_DIV_FROM_REG(data
->fan_div
[nr
])) : 0;
489 return sprintf(buf
, "%d\n", value
);
492 static ssize_t
show_fan_div(struct device
*dev
,
493 struct device_attribute
*attr
, char *buf
)
495 int nr
= to_sensor_dev_attr(attr
)->index
;
496 struct adm1031_data
*data
= adm1031_update_device(dev
);
497 return sprintf(buf
, "%d\n", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
499 static ssize_t
show_fan_min(struct device
*dev
,
500 struct device_attribute
*attr
, char *buf
)
502 int nr
= to_sensor_dev_attr(attr
)->index
;
503 struct adm1031_data
*data
= adm1031_update_device(dev
);
504 return sprintf(buf
, "%d\n",
505 FAN_FROM_REG(data
->fan_min
[nr
],
506 FAN_DIV_FROM_REG(data
->fan_div
[nr
])));
508 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
509 const char *buf
, size_t count
)
511 struct i2c_client
*client
= to_i2c_client(dev
);
512 struct adm1031_data
*data
= i2c_get_clientdata(client
);
513 int nr
= to_sensor_dev_attr(attr
)->index
;
514 int val
= simple_strtol(buf
, NULL
, 10);
516 mutex_lock(&data
->update_lock
);
519 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
521 data
->fan_min
[nr
] = 0xff;
523 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
524 mutex_unlock(&data
->update_lock
);
527 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
528 const char *buf
, size_t count
)
530 struct i2c_client
*client
= to_i2c_client(dev
);
531 struct adm1031_data
*data
= i2c_get_clientdata(client
);
532 int nr
= to_sensor_dev_attr(attr
)->index
;
533 int val
= simple_strtol(buf
, NULL
, 10);
538 tmp
= val
== 8 ? 0xc0 :
546 mutex_lock(&data
->update_lock
);
547 /* Get fresh readings */
548 data
->fan_div
[nr
] = adm1031_read_value(client
,
549 ADM1031_REG_FAN_DIV(nr
));
550 data
->fan_min
[nr
] = adm1031_read_value(client
,
551 ADM1031_REG_FAN_MIN(nr
));
553 /* Write the new clock divider and fan min */
554 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
555 data
->fan_div
[nr
] = tmp
| (0x3f & data
->fan_div
[nr
]);
556 new_min
= data
->fan_min
[nr
] * old_div
/ val
;
557 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
559 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
561 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
564 /* Invalidate the cache: fan speed is no longer valid */
566 mutex_unlock(&data
->update_lock
);
570 #define fan_offset(offset) \
571 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
572 show_fan, NULL, offset - 1); \
573 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
574 show_fan_min, set_fan_min, offset - 1); \
575 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
576 show_fan_div, set_fan_div, offset - 1)
583 static ssize_t
show_temp(struct device
*dev
,
584 struct device_attribute
*attr
, char *buf
)
586 int nr
= to_sensor_dev_attr(attr
)->index
;
587 struct adm1031_data
*data
= adm1031_update_device(dev
);
590 ((data
->ext_temp
[nr
] >> 6) & 0x3) * 2 :
591 (((data
->ext_temp
[nr
] >> ((nr
- 1) * 3)) & 7));
592 return sprintf(buf
, "%d\n", TEMP_FROM_REG_EXT(data
->temp
[nr
], ext
));
594 static ssize_t
show_temp_offset(struct device
*dev
,
595 struct device_attribute
*attr
, char *buf
)
597 int nr
= to_sensor_dev_attr(attr
)->index
;
598 struct adm1031_data
*data
= adm1031_update_device(dev
);
599 return sprintf(buf
, "%d\n",
600 TEMP_OFFSET_FROM_REG(data
->temp_offset
[nr
]));
602 static ssize_t
show_temp_min(struct device
*dev
,
603 struct device_attribute
*attr
, char *buf
)
605 int nr
= to_sensor_dev_attr(attr
)->index
;
606 struct adm1031_data
*data
= adm1031_update_device(dev
);
607 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
609 static ssize_t
show_temp_max(struct device
*dev
,
610 struct device_attribute
*attr
, char *buf
)
612 int nr
= to_sensor_dev_attr(attr
)->index
;
613 struct adm1031_data
*data
= adm1031_update_device(dev
);
614 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
616 static ssize_t
show_temp_crit(struct device
*dev
,
617 struct device_attribute
*attr
, char *buf
)
619 int nr
= to_sensor_dev_attr(attr
)->index
;
620 struct adm1031_data
*data
= adm1031_update_device(dev
);
621 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[nr
]));
623 static ssize_t
set_temp_offset(struct device
*dev
,
624 struct device_attribute
*attr
, const char *buf
,
627 struct i2c_client
*client
= to_i2c_client(dev
);
628 struct adm1031_data
*data
= i2c_get_clientdata(client
);
629 int nr
= to_sensor_dev_attr(attr
)->index
;
632 val
= simple_strtol(buf
, NULL
, 10);
633 val
= SENSORS_LIMIT(val
, -15000, 15000);
634 mutex_lock(&data
->update_lock
);
635 data
->temp_offset
[nr
] = TEMP_OFFSET_TO_REG(val
);
636 adm1031_write_value(client
, ADM1031_REG_TEMP_OFFSET(nr
),
637 data
->temp_offset
[nr
]);
638 mutex_unlock(&data
->update_lock
);
641 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
642 const char *buf
, size_t count
)
644 struct i2c_client
*client
= to_i2c_client(dev
);
645 struct adm1031_data
*data
= i2c_get_clientdata(client
);
646 int nr
= to_sensor_dev_attr(attr
)->index
;
649 val
= simple_strtol(buf
, NULL
, 10);
650 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
651 mutex_lock(&data
->update_lock
);
652 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
653 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
655 mutex_unlock(&data
->update_lock
);
658 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
659 const char *buf
, size_t count
)
661 struct i2c_client
*client
= to_i2c_client(dev
);
662 struct adm1031_data
*data
= i2c_get_clientdata(client
);
663 int nr
= to_sensor_dev_attr(attr
)->index
;
666 val
= simple_strtol(buf
, NULL
, 10);
667 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
668 mutex_lock(&data
->update_lock
);
669 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
670 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
672 mutex_unlock(&data
->update_lock
);
675 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*attr
,
676 const char *buf
, size_t count
)
678 struct i2c_client
*client
= to_i2c_client(dev
);
679 struct adm1031_data
*data
= i2c_get_clientdata(client
);
680 int nr
= to_sensor_dev_attr(attr
)->index
;
683 val
= simple_strtol(buf
, NULL
, 10);
684 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
685 mutex_lock(&data
->update_lock
);
686 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
687 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
688 data
->temp_crit
[nr
]);
689 mutex_unlock(&data
->update_lock
);
693 #define temp_reg(offset) \
694 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
695 show_temp, NULL, offset - 1); \
696 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
697 show_temp_offset, set_temp_offset, offset - 1); \
698 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
699 show_temp_min, set_temp_min, offset - 1); \
700 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
701 show_temp_max, set_temp_max, offset - 1); \
702 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
703 show_temp_crit, set_temp_crit, offset - 1)
710 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
712 struct adm1031_data
*data
= adm1031_update_device(dev
);
713 return sprintf(buf
, "%d\n", data
->alarm
);
716 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
718 static ssize_t
show_alarm(struct device
*dev
,
719 struct device_attribute
*attr
, char *buf
)
721 int bitnr
= to_sensor_dev_attr(attr
)->index
;
722 struct adm1031_data
*data
= adm1031_update_device(dev
);
723 return sprintf(buf
, "%d\n", (data
->alarm
>> bitnr
) & 1);
726 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
727 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, show_alarm
, NULL
, 1);
728 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
729 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
730 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
731 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 5);
732 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
733 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
734 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
735 static SENSOR_DEVICE_ATTR(fan2_fault
, S_IRUGO
, show_alarm
, NULL
, 9);
736 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
737 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
738 static SENSOR_DEVICE_ATTR(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
739 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 13);
740 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
742 static struct attribute
*adm1031_attributes
[] = {
743 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
744 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
745 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
746 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
747 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
748 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
749 &sensor_dev_attr_auto_fan1_channel
.dev_attr
.attr
,
750 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
751 &sensor_dev_attr_temp1_offset
.dev_attr
.attr
,
752 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
753 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
754 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
755 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
756 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
757 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
758 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
759 &sensor_dev_attr_temp2_offset
.dev_attr
.attr
,
760 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
761 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
762 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
763 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
764 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
765 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
766 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
768 &sensor_dev_attr_auto_temp1_off
.dev_attr
.attr
,
769 &sensor_dev_attr_auto_temp1_min
.dev_attr
.attr
,
770 &sensor_dev_attr_auto_temp1_max
.dev_attr
.attr
,
772 &sensor_dev_attr_auto_temp2_off
.dev_attr
.attr
,
773 &sensor_dev_attr_auto_temp2_min
.dev_attr
.attr
,
774 &sensor_dev_attr_auto_temp2_max
.dev_attr
.attr
,
776 &sensor_dev_attr_auto_fan1_min_pwm
.dev_attr
.attr
,
778 &dev_attr_alarms
.attr
,
783 static const struct attribute_group adm1031_group
= {
784 .attrs
= adm1031_attributes
,
787 static struct attribute
*adm1031_attributes_opt
[] = {
788 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
789 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
790 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
791 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
792 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
793 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
794 &sensor_dev_attr_auto_fan2_channel
.dev_attr
.attr
,
795 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
796 &sensor_dev_attr_temp3_offset
.dev_attr
.attr
,
797 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
798 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
799 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
800 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
801 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
802 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
803 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
804 &sensor_dev_attr_auto_temp3_off
.dev_attr
.attr
,
805 &sensor_dev_attr_auto_temp3_min
.dev_attr
.attr
,
806 &sensor_dev_attr_auto_temp3_max
.dev_attr
.attr
,
807 &sensor_dev_attr_auto_fan2_min_pwm
.dev_attr
.attr
,
811 static const struct attribute_group adm1031_group_opt
= {
812 .attrs
= adm1031_attributes_opt
,
815 /* Return 0 if detection is successful, -ENODEV otherwise */
816 static int adm1031_detect(struct i2c_client
*client
, int kind
,
817 struct i2c_board_info
*info
)
819 struct i2c_adapter
*adapter
= client
->adapter
;
820 const char *name
= "";
822 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
827 id
= i2c_smbus_read_byte_data(client
, 0x3d);
828 co
= i2c_smbus_read_byte_data(client
, 0x3e);
830 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
832 kind
= (id
== 0x30) ? adm1030
: adm1031
;
838 /* Given the detected chip type, set the chip name and the
839 * auto fan control helper table. */
840 if (kind
== adm1030
) {
842 } else if (kind
== adm1031
) {
845 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
850 static int adm1031_probe(struct i2c_client
*client
,
851 const struct i2c_device_id
*id
)
853 struct adm1031_data
*data
;
856 data
= kzalloc(sizeof(struct adm1031_data
), GFP_KERNEL
);
862 i2c_set_clientdata(client
, data
);
863 data
->chip_type
= id
->driver_data
;
864 mutex_init(&data
->update_lock
);
866 if (data
->chip_type
== adm1030
)
867 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
869 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
871 /* Initialize the ADM1031 chip */
872 adm1031_init_client(client
);
874 /* Register sysfs hooks */
875 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &adm1031_group
)))
878 if (data
->chip_type
== adm1031
) {
879 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
880 &adm1031_group_opt
)))
884 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
885 if (IS_ERR(data
->hwmon_dev
)) {
886 err
= PTR_ERR(data
->hwmon_dev
);
893 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
894 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
901 static int adm1031_remove(struct i2c_client
*client
)
903 struct adm1031_data
*data
= i2c_get_clientdata(client
);
905 hwmon_device_unregister(data
->hwmon_dev
);
906 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
907 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
912 static void adm1031_init_client(struct i2c_client
*client
)
914 unsigned int read_val
;
916 struct adm1031_data
*data
= i2c_get_clientdata(client
);
918 mask
= (ADM1031_CONF2_PWM1_ENABLE
| ADM1031_CONF2_TACH1_ENABLE
);
919 if (data
->chip_type
== adm1031
) {
920 mask
|= (ADM1031_CONF2_PWM2_ENABLE
|
921 ADM1031_CONF2_TACH2_ENABLE
);
923 /* Initialize the ADM1031 chip (enables fan speed reading ) */
924 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
925 if ((read_val
| mask
) != read_val
) {
926 adm1031_write_value(client
, ADM1031_REG_CONF2
, read_val
| mask
);
929 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
930 if ((read_val
| ADM1031_CONF1_MONITOR_ENABLE
) != read_val
) {
931 adm1031_write_value(client
, ADM1031_REG_CONF1
, read_val
|
932 ADM1031_CONF1_MONITOR_ENABLE
);
937 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
939 struct i2c_client
*client
= to_i2c_client(dev
);
940 struct adm1031_data
*data
= i2c_get_clientdata(client
);
943 mutex_lock(&data
->update_lock
);
945 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
948 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
950 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
954 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
955 data
->ext_temp
[chan
] =
956 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
958 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
960 data
->ext_temp
[chan
] =
961 adm1031_read_value(client
,
962 ADM1031_REG_EXT_TEMP
);
965 adm1031_read_value(client
,
966 ADM1031_REG_TEMP(chan
));
968 /* oldh is actually newer */
970 dev_warn(&client
->dev
,
971 "Remote temperature may be "
975 data
->temp
[chan
] = newh
;
977 data
->temp_offset
[chan
] =
978 adm1031_read_value(client
,
979 ADM1031_REG_TEMP_OFFSET(chan
));
980 data
->temp_min
[chan
] =
981 adm1031_read_value(client
,
982 ADM1031_REG_TEMP_MIN(chan
));
983 data
->temp_max
[chan
] =
984 adm1031_read_value(client
,
985 ADM1031_REG_TEMP_MAX(chan
));
986 data
->temp_crit
[chan
] =
987 adm1031_read_value(client
,
988 ADM1031_REG_TEMP_CRIT(chan
));
989 data
->auto_temp
[chan
] =
990 adm1031_read_value(client
,
991 ADM1031_REG_AUTO_TEMP(chan
));
995 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
996 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
998 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
999 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1))
1001 if (data
->chip_type
== adm1030
) {
1002 data
->alarm
&= 0xc0ff;
1005 for (chan
=0; chan
<(data
->chip_type
== adm1030
? 1 : 2); chan
++) {
1006 data
->fan_div
[chan
] =
1007 adm1031_read_value(client
, ADM1031_REG_FAN_DIV(chan
));
1008 data
->fan_min
[chan
] =
1009 adm1031_read_value(client
, ADM1031_REG_FAN_MIN(chan
));
1011 adm1031_read_value(client
, ADM1031_REG_FAN_SPEED(chan
));
1013 0xf & (adm1031_read_value(client
, ADM1031_REG_PWM
) >>
1016 data
->last_updated
= jiffies
;
1020 mutex_unlock(&data
->update_lock
);
1025 static int __init
sensors_adm1031_init(void)
1027 return i2c_add_driver(&adm1031_driver
);
1030 static void __exit
sensors_adm1031_exit(void)
1032 i2c_del_driver(&adm1031_driver
);
1035 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1036 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1037 MODULE_LICENSE("GPL");
1039 module_init(sensors_adm1031_init
);
1040 module_exit(sensors_adm1031_exit
);