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_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))
44 #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
45 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
47 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
49 #define ADM1031_REG_CONF1 0x00
50 #define ADM1031_REG_CONF2 0x01
51 #define ADM1031_REG_EXT_TEMP 0x06
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 */
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))
63 /* Addresses to scan */
64 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
66 /* Insmod parameters */
67 I2C_CLIENT_INSMOD_2(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
;
101 static int adm1031_probe(struct i2c_client
*client
,
102 const struct i2c_device_id
*id
);
103 static int adm1031_detect(struct i2c_client
*client
, int kind
,
104 struct i2c_board_info
*info
);
105 static void adm1031_init_client(struct i2c_client
*client
);
106 static int adm1031_remove(struct i2c_client
*client
);
107 static struct adm1031_data
*adm1031_update_device(struct device
*dev
);
109 static const struct i2c_device_id adm1031_id
[] = {
110 { "adm1030", adm1030
},
111 { "adm1031", adm1031
},
114 MODULE_DEVICE_TABLE(i2c
, adm1031_id
);
116 /* This is the driver that will be inserted */
117 static struct i2c_driver adm1031_driver
= {
118 .class = I2C_CLASS_HWMON
,
122 .probe
= adm1031_probe
,
123 .remove
= adm1031_remove
,
124 .id_table
= adm1031_id
,
125 .detect
= adm1031_detect
,
126 .address_data
= &addr_data
,
129 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
131 return i2c_smbus_read_byte_data(client
, reg
);
135 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
137 return i2c_smbus_write_byte_data(client
, reg
, value
);
141 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
142 ((val + 500) / 1000)))
144 #define TEMP_FROM_REG(val) ((val) * 1000)
146 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
148 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
150 static int FAN_TO_REG(int reg
, int div
)
153 tmp
= FAN_FROM_REG(SENSORS_LIMIT(reg
, 0, 65535), div
);
154 return tmp
> 255 ? 255 : tmp
;
157 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
159 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
160 #define PWM_FROM_REG(val) ((val) << 4)
162 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
163 #define FAN_CHAN_TO_REG(val, reg) \
164 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
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))
171 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
173 #define AUTO_TEMP_OFF_FROM_REG(reg) \
174 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
176 #define AUTO_TEMP_MAX_FROM_REG(reg) \
177 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
178 AUTO_TEMP_MIN_FROM_REG(reg))
180 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
183 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
185 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
186 ret
= ((reg
& 0xf8) |
189 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
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]
197 /* The tables below contains the possible values for the auto fan
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.
202 static 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 */ },
210 static 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 },
218 /* That function checks if a bitfield is valid and returns the other bitfield
219 * nearest match if no exact match where found.
222 get_fan_auto_nearest(struct adm1031_data
*data
,
223 int chan
, u8 val
, u8 reg
, u8
* new_reg
)
226 int first_match
= -1, exact_match
= -1;
228 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
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] ==
239 /* We found an exact match */
242 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
244 /* Save the first match in case of an exact match has
251 if (exact_match
>= 0) {
252 *new_reg
= exact_match
;
253 } else if (first_match
>= 0) {
254 *new_reg
= first_match
;
261 static ssize_t
show_fan_auto_channel(struct device
*dev
,
262 struct device_attribute
*attr
, char *buf
)
264 int nr
= to_sensor_dev_attr(attr
)->index
;
265 struct adm1031_data
*data
= adm1031_update_device(dev
);
266 return sprintf(buf
, "%d\n", GET_FAN_AUTO_BITFIELD(data
, nr
));
270 set_fan_auto_channel(struct device
*dev
, struct device_attribute
*attr
,
271 const char *buf
, size_t count
)
273 struct i2c_client
*client
= to_i2c_client(dev
);
274 struct adm1031_data
*data
= i2c_get_clientdata(client
);
275 int nr
= to_sensor_dev_attr(attr
)->index
;
276 int val
= simple_strtol(buf
, NULL
, 10);
281 old_fan_mode
= data
->conf1
;
283 mutex_lock(&data
->update_lock
);
285 if ((ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
, ®
))) {
286 mutex_unlock(&data
->update_lock
);
289 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
290 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) ^
291 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
292 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
){
293 /* Switch to Auto Fan Mode
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);
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 */
304 adm1031_write_value(client
, ADM1031_REG_PWM
,
305 data
->pwm
[0] | (data
->pwm
[1] << 4));
308 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
309 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
310 mutex_unlock(&data
->update_lock
);
314 static SENSOR_DEVICE_ATTR(auto_fan1_channel
, S_IRUGO
| S_IWUSR
,
315 show_fan_auto_channel
, set_fan_auto_channel
, 0);
316 static SENSOR_DEVICE_ATTR(auto_fan2_channel
, S_IRUGO
| S_IWUSR
,
317 show_fan_auto_channel
, set_fan_auto_channel
, 1);
320 static ssize_t
show_auto_temp_off(struct device
*dev
,
321 struct device_attribute
*attr
, char *buf
)
323 int nr
= to_sensor_dev_attr(attr
)->index
;
324 struct adm1031_data
*data
= adm1031_update_device(dev
);
325 return sprintf(buf
, "%d\n",
326 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
328 static ssize_t
show_auto_temp_min(struct device
*dev
,
329 struct device_attribute
*attr
, char *buf
)
331 int nr
= to_sensor_dev_attr(attr
)->index
;
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
]));
337 set_auto_temp_min(struct device
*dev
, struct device_attribute
*attr
,
338 const char *buf
, size_t count
)
340 struct i2c_client
*client
= to_i2c_client(dev
);
341 struct adm1031_data
*data
= i2c_get_clientdata(client
);
342 int nr
= to_sensor_dev_attr(attr
)->index
;
343 int val
= simple_strtol(buf
, NULL
, 10);
345 mutex_lock(&data
->update_lock
);
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
]);
349 mutex_unlock(&data
->update_lock
);
352 static ssize_t
show_auto_temp_max(struct device
*dev
,
353 struct device_attribute
*attr
, char *buf
)
355 int nr
= to_sensor_dev_attr(attr
)->index
;
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
]));
361 set_auto_temp_max(struct device
*dev
, struct device_attribute
*attr
,
362 const char *buf
, size_t count
)
364 struct i2c_client
*client
= to_i2c_client(dev
);
365 struct adm1031_data
*data
= i2c_get_clientdata(client
);
366 int nr
= to_sensor_dev_attr(attr
)->index
;
367 int val
= simple_strtol(buf
, NULL
, 10);
369 mutex_lock(&data
->update_lock
);
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
),
373 mutex_unlock(&data
->update_lock
);
377 #define auto_temp_reg(offset) \
378 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
379 show_auto_temp_off, NULL, offset - 1); \
380 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
381 show_auto_temp_min, set_auto_temp_min, offset - 1); \
382 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
383 show_auto_temp_max, set_auto_temp_max, offset - 1)
390 static ssize_t
show_pwm(struct device
*dev
,
391 struct device_attribute
*attr
, char *buf
)
393 int nr
= to_sensor_dev_attr(attr
)->index
;
394 struct adm1031_data
*data
= adm1031_update_device(dev
);
395 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
397 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
398 const char *buf
, size_t count
)
400 struct i2c_client
*client
= to_i2c_client(dev
);
401 struct adm1031_data
*data
= i2c_get_clientdata(client
);
402 int nr
= to_sensor_dev_attr(attr
)->index
;
403 int val
= simple_strtol(buf
, NULL
, 10);
406 mutex_lock(&data
->update_lock
);
407 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
408 (((val
>>4) & 0xf) != 5)) {
409 /* In automatic mode, the only PWM accepted is 33% */
410 mutex_unlock(&data
->update_lock
);
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));
418 mutex_unlock(&data
->update_lock
);
422 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 0);
423 static SENSOR_DEVICE_ATTR(pwm2
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 1);
424 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm
, S_IRUGO
| S_IWUSR
,
425 show_pwm
, set_pwm
, 0);
426 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm
, S_IRUGO
| S_IWUSR
,
427 show_pwm
, set_pwm
, 1);
432 * That function checks the cases where the fan reading is not
433 * relevant. It is used to provide 0 as fan reading when the fan is
434 * not supposed to run
436 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
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]);
446 case 0x20: /* remote temp1 controls both fans */
449 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
451 case 0x40: /* remote temp2 controls both fans */
454 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
456 case 0x60: /* max controls both fans */
459 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
461 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
462 || (data
->chip_type
== adm1031
464 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
468 res
= data
->pwm
[chan
] > 0;
474 static ssize_t
show_fan(struct device
*dev
,
475 struct device_attribute
*attr
, char *buf
)
477 int nr
= to_sensor_dev_attr(attr
)->index
;
478 struct adm1031_data
*data
= adm1031_update_device(dev
);
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
);
486 static ssize_t
show_fan_div(struct device
*dev
,
487 struct device_attribute
*attr
, char *buf
)
489 int nr
= to_sensor_dev_attr(attr
)->index
;
490 struct adm1031_data
*data
= adm1031_update_device(dev
);
491 return sprintf(buf
, "%d\n", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
493 static ssize_t
show_fan_min(struct device
*dev
,
494 struct device_attribute
*attr
, char *buf
)
496 int nr
= to_sensor_dev_attr(attr
)->index
;
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
])));
502 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
503 const char *buf
, size_t count
)
505 struct i2c_client
*client
= to_i2c_client(dev
);
506 struct adm1031_data
*data
= i2c_get_clientdata(client
);
507 int nr
= to_sensor_dev_attr(attr
)->index
;
508 int val
= simple_strtol(buf
, NULL
, 10);
510 mutex_lock(&data
->update_lock
);
513 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
515 data
->fan_min
[nr
] = 0xff;
517 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
518 mutex_unlock(&data
->update_lock
);
521 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
522 const char *buf
, size_t count
)
524 struct i2c_client
*client
= to_i2c_client(dev
);
525 struct adm1031_data
*data
= i2c_get_clientdata(client
);
526 int nr
= to_sensor_dev_attr(attr
)->index
;
527 int val
= simple_strtol(buf
, NULL
, 10);
532 tmp
= val
== 8 ? 0xc0 :
540 mutex_lock(&data
->update_lock
);
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
));
547 /* Write the new clock divider and fan min */
548 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
549 data
->fan_div
[nr
] = tmp
| (0x3f & data
->fan_div
[nr
]);
550 new_min
= data
->fan_min
[nr
] * old_div
/ val
;
551 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
553 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
555 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
558 /* Invalidate the cache: fan speed is no longer valid */
560 mutex_unlock(&data
->update_lock
);
564 #define fan_offset(offset) \
565 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
566 show_fan, NULL, offset - 1); \
567 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
568 show_fan_min, set_fan_min, offset - 1); \
569 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
570 show_fan_div, set_fan_div, offset - 1)
577 static ssize_t
show_temp(struct device
*dev
,
578 struct device_attribute
*attr
, char *buf
)
580 int nr
= to_sensor_dev_attr(attr
)->index
;
581 struct adm1031_data
*data
= adm1031_update_device(dev
);
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
));
588 static ssize_t
show_temp_min(struct device
*dev
,
589 struct device_attribute
*attr
, char *buf
)
591 int nr
= to_sensor_dev_attr(attr
)->index
;
592 struct adm1031_data
*data
= adm1031_update_device(dev
);
593 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
595 static ssize_t
show_temp_max(struct device
*dev
,
596 struct device_attribute
*attr
, char *buf
)
598 int nr
= to_sensor_dev_attr(attr
)->index
;
599 struct adm1031_data
*data
= adm1031_update_device(dev
);
600 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
602 static ssize_t
show_temp_crit(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_crit
[nr
]));
609 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
610 const char *buf
, size_t count
)
612 struct i2c_client
*client
= to_i2c_client(dev
);
613 struct adm1031_data
*data
= i2c_get_clientdata(client
);
614 int nr
= to_sensor_dev_attr(attr
)->index
;
617 val
= simple_strtol(buf
, NULL
, 10);
618 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
619 mutex_lock(&data
->update_lock
);
620 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
621 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
623 mutex_unlock(&data
->update_lock
);
626 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
627 const char *buf
, size_t count
)
629 struct i2c_client
*client
= to_i2c_client(dev
);
630 struct adm1031_data
*data
= i2c_get_clientdata(client
);
631 int nr
= to_sensor_dev_attr(attr
)->index
;
634 val
= simple_strtol(buf
, NULL
, 10);
635 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
636 mutex_lock(&data
->update_lock
);
637 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
638 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
640 mutex_unlock(&data
->update_lock
);
643 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*attr
,
644 const char *buf
, size_t count
)
646 struct i2c_client
*client
= to_i2c_client(dev
);
647 struct adm1031_data
*data
= i2c_get_clientdata(client
);
648 int nr
= to_sensor_dev_attr(attr
)->index
;
651 val
= simple_strtol(buf
, NULL
, 10);
652 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
653 mutex_lock(&data
->update_lock
);
654 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
655 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
656 data
->temp_crit
[nr
]);
657 mutex_unlock(&data
->update_lock
);
661 #define temp_reg(offset) \
662 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
663 show_temp, NULL, offset - 1); \
664 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
665 show_temp_min, set_temp_min, offset - 1); \
666 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
667 show_temp_max, set_temp_max, offset - 1); \
668 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
669 show_temp_crit, set_temp_crit, offset - 1)
676 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
678 struct adm1031_data
*data
= adm1031_update_device(dev
);
679 return sprintf(buf
, "%d\n", data
->alarm
);
682 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
684 static ssize_t
show_alarm(struct device
*dev
,
685 struct device_attribute
*attr
, char *buf
)
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);
692 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
693 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, show_alarm
, NULL
, 1);
694 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
695 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
696 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
697 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 5);
698 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
699 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
700 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
701 static SENSOR_DEVICE_ATTR(fan2_fault
, S_IRUGO
, show_alarm
, NULL
, 9);
702 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
703 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
704 static SENSOR_DEVICE_ATTR(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
705 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 13);
706 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
708 static struct attribute
*adm1031_attributes
[] = {
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
,
712 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
713 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
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
,
718 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
719 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
720 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
721 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
722 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
723 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
724 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
725 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
726 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
727 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
728 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
729 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
730 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
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
,
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
,
740 &sensor_dev_attr_auto_fan1_min_pwm
.dev_attr
.attr
,
742 &dev_attr_alarms
.attr
,
747 static const struct attribute_group adm1031_group
= {
748 .attrs
= adm1031_attributes
,
751 static struct attribute
*adm1031_attributes_opt
[] = {
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
,
755 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
756 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
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
,
761 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
762 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
763 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
764 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
765 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
766 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
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
,
774 static const struct attribute_group adm1031_group_opt
= {
775 .attrs
= adm1031_attributes_opt
,
778 /* Return 0 if detection is successful, -ENODEV otherwise */
779 static int adm1031_detect(struct i2c_client
*client
, int kind
,
780 struct i2c_board_info
*info
)
782 struct i2c_adapter
*adapter
= client
->adapter
;
783 const char *name
= "";
785 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
790 id
= i2c_smbus_read_byte_data(client
, 0x3d);
791 co
= i2c_smbus_read_byte_data(client
, 0x3e);
793 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
795 kind
= (id
== 0x30) ? adm1030
: adm1031
;
801 /* Given the detected chip type, set the chip name and the
802 * auto fan control helper table. */
803 if (kind
== adm1030
) {
805 } else if (kind
== adm1031
) {
808 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
813 static int adm1031_probe(struct i2c_client
*client
,
814 const struct i2c_device_id
*id
)
816 struct adm1031_data
*data
;
819 data
= kzalloc(sizeof(struct adm1031_data
), GFP_KERNEL
);
825 i2c_set_clientdata(client
, data
);
826 data
->chip_type
= id
->driver_data
;
827 mutex_init(&data
->update_lock
);
829 if (data
->chip_type
== adm1030
)
830 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
832 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
834 /* Initialize the ADM1031 chip */
835 adm1031_init_client(client
);
837 /* Register sysfs hooks */
838 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &adm1031_group
)))
841 if (data
->chip_type
== adm1031
) {
842 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
843 &adm1031_group_opt
)))
847 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
848 if (IS_ERR(data
->hwmon_dev
)) {
849 err
= PTR_ERR(data
->hwmon_dev
);
856 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
857 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
864 static int adm1031_remove(struct i2c_client
*client
)
866 struct adm1031_data
*data
= i2c_get_clientdata(client
);
868 hwmon_device_unregister(data
->hwmon_dev
);
869 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
870 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
875 static void adm1031_init_client(struct i2c_client
*client
)
877 unsigned int read_val
;
879 struct adm1031_data
*data
= i2c_get_clientdata(client
);
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
);
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
);
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
);
900 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
902 struct i2c_client
*client
= to_i2c_client(dev
);
903 struct adm1031_data
*data
= i2c_get_clientdata(client
);
906 mutex_lock(&data
->update_lock
);
908 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
911 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
913 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
917 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
918 data
->ext_temp
[chan
] =
919 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
921 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
923 data
->ext_temp
[chan
] =
924 adm1031_read_value(client
,
925 ADM1031_REG_EXT_TEMP
);
928 adm1031_read_value(client
,
929 ADM1031_REG_TEMP(chan
));
931 /* oldh is actually newer */
933 dev_warn(&client
->dev
,
934 "Remote temperature may be "
938 data
->temp
[chan
] = newh
;
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
));
955 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
956 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
958 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
959 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1))
961 if (data
->chip_type
== adm1030
) {
962 data
->alarm
&= 0xc0ff;
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
));
971 adm1031_read_value(client
, ADM1031_REG_FAN_SPEED(chan
));
973 0xf & (adm1031_read_value(client
, ADM1031_REG_PWM
) >>
976 data
->last_updated
= jiffies
;
980 mutex_unlock(&data
->update_lock
);
985 static int __init
sensors_adm1031_init(void)
987 return i2c_add_driver(&adm1031_driver
);
990 static void __exit
sensors_adm1031_exit(void)
992 i2c_del_driver(&adm1031_driver
);
995 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
996 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
997 MODULE_LICENSE("GPL");
999 module_init(sensors_adm1031_init
);
1000 module_exit(sensors_adm1031_exit
);