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/err.h>
32 /* Following macros takes channel parameter starting from 0 to 2 */
33 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
34 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
35 #define ADM1031_REG_PWM (0x22)
36 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
38 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4*(nr))
39 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4*(nr))
40 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4*(nr))
42 #define ADM1031_REG_TEMP(nr) (0xa + (nr))
43 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
45 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
47 #define ADM1031_REG_CONF1 0x0
48 #define ADM1031_REG_CONF2 0x1
49 #define ADM1031_REG_EXT_TEMP 0x6
51 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
52 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
53 #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
55 #define ADM1031_CONF2_PWM1_ENABLE 0x01
56 #define ADM1031_CONF2_PWM2_ENABLE 0x02
57 #define ADM1031_CONF2_TACH1_ENABLE 0x04
58 #define ADM1031_CONF2_TACH2_ENABLE 0x08
59 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
61 /* Addresses to scan */
62 static unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
64 /* Insmod parameters */
65 I2C_CLIENT_INSMOD_2(adm1030
, adm1031
);
67 typedef u8 auto_chan_table_t
[8][2];
69 /* Each client has this additional data */
71 struct i2c_client client
;
72 struct class_device
*class_dev
;
73 struct semaphore update_lock
;
75 char valid
; /* !=0 if following fields are valid */
76 unsigned long last_updated
; /* In jiffies */
77 /* The chan_select_table contains the possible configurations for
80 auto_chan_table_t
*chan_select_table
;
100 static int adm1031_attach_adapter(struct i2c_adapter
*adapter
);
101 static int adm1031_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
102 static void adm1031_init_client(struct i2c_client
*client
);
103 static int adm1031_detach_client(struct i2c_client
*client
);
104 static struct adm1031_data
*adm1031_update_device(struct device
*dev
);
106 /* This is the driver that will be inserted */
107 static struct i2c_driver adm1031_driver
= {
108 .owner
= THIS_MODULE
,
110 .flags
= I2C_DF_NOTIFY
,
111 .attach_adapter
= adm1031_attach_adapter
,
112 .detach_client
= adm1031_detach_client
,
115 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
117 return i2c_smbus_read_byte_data(client
, reg
);
121 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
123 return i2c_smbus_write_byte_data(client
, reg
, value
);
127 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
128 ((val + 500) / 1000)))
130 #define TEMP_FROM_REG(val) ((val) * 1000)
132 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
134 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
136 static int FAN_TO_REG(int reg
, int div
)
139 tmp
= FAN_FROM_REG(SENSORS_LIMIT(reg
, 0, 65535), div
);
140 return tmp
> 255 ? 255 : tmp
;
143 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
145 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
146 #define PWM_FROM_REG(val) ((val) << 4)
148 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
149 #define FAN_CHAN_TO_REG(val, reg) \
150 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
152 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
153 ((((val)/500) & 0xf8)|((reg) & 0x7))
154 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
155 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
157 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
159 #define AUTO_TEMP_OFF_FROM_REG(reg) \
160 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
162 #define AUTO_TEMP_MAX_FROM_REG(reg) \
163 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
164 AUTO_TEMP_MIN_FROM_REG(reg))
166 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
169 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
171 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
172 ret
= ((reg
& 0xf8) |
175 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
179 /* FAN auto control */
180 #define GET_FAN_AUTO_BITFIELD(data, idx) \
181 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
183 /* The tables below contains the possible values for the auto fan
184 * control bitfields. the index in the table is the register value.
185 * MSb is the auto fan control enable bit, so the four first entries
186 * in the table disables auto fan control when both bitfields are zero.
188 static auto_chan_table_t auto_channel_select_table_adm1031
= {
189 {0, 0}, {0, 0}, {0, 0}, {0, 0},
190 {2 /*0b010 */ , 4 /*0b100 */ },
191 {2 /*0b010 */ , 2 /*0b010 */ },
192 {4 /*0b100 */ , 4 /*0b100 */ },
193 {7 /*0b111 */ , 7 /*0b111 */ },
196 static auto_chan_table_t auto_channel_select_table_adm1030
= {
197 {0, 0}, {0, 0}, {0, 0}, {0, 0},
199 {0xff /*invalid */ , 0},
200 {0xff /*invalid */ , 0},
204 /* That function checks if a bitfield is valid and returns the other bitfield
205 * nearest match if no exact match where found.
208 get_fan_auto_nearest(struct adm1031_data
*data
,
209 int chan
, u8 val
, u8 reg
, u8
* new_reg
)
212 int first_match
= -1, exact_match
= -1;
214 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
221 for (i
= 0; i
< 8; i
++) {
222 if ((val
== (*data
->chan_select_table
)[i
][chan
]) &&
223 ((*data
->chan_select_table
)[i
][chan
? 0 : 1] ==
225 /* We found an exact match */
228 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
230 /* Save the first match in case of an exact match has not been
237 if (exact_match
>= 0) {
238 *new_reg
= exact_match
;
239 } else if (first_match
>= 0) {
240 *new_reg
= first_match
;
247 static ssize_t
show_fan_auto_channel(struct device
*dev
, char *buf
, int nr
)
249 struct adm1031_data
*data
= adm1031_update_device(dev
);
250 return sprintf(buf
, "%d\n", GET_FAN_AUTO_BITFIELD(data
, nr
));
254 set_fan_auto_channel(struct device
*dev
, const char *buf
, size_t count
, int nr
)
256 struct i2c_client
*client
= to_i2c_client(dev
);
257 struct adm1031_data
*data
= i2c_get_clientdata(client
);
258 int val
= simple_strtol(buf
, NULL
, 10);
263 old_fan_mode
= data
->conf1
;
265 down(&data
->update_lock
);
267 if ((ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
, ®
))) {
268 up(&data
->update_lock
);
271 if (((data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
)) & ADM1031_CONF1_AUTO_MODE
) ^
272 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
273 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
){
274 /* Switch to Auto Fan Mode
276 * Set PWM registers to 33% Both */
277 data
->old_pwm
[0] = data
->pwm
[0];
278 data
->old_pwm
[1] = data
->pwm
[1];
279 adm1031_write_value(client
, ADM1031_REG_PWM
, 0x55);
281 /* Switch to Manual Mode */
282 data
->pwm
[0] = data
->old_pwm
[0];
283 data
->pwm
[1] = data
->old_pwm
[1];
284 /* Restore PWM registers */
285 adm1031_write_value(client
, ADM1031_REG_PWM
,
286 data
->pwm
[0] | (data
->pwm
[1] << 4));
289 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
290 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
291 up(&data
->update_lock
);
295 #define fan_auto_channel_offset(offset) \
296 static ssize_t show_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
298 return show_fan_auto_channel(dev, buf, offset - 1); \
300 static ssize_t set_fan_auto_channel_##offset (struct device *dev, struct device_attribute *attr, \
301 const char *buf, size_t count) \
303 return set_fan_auto_channel(dev, buf, count, offset - 1); \
305 static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR, \
306 show_fan_auto_channel_##offset, \
307 set_fan_auto_channel_##offset)
309 fan_auto_channel_offset(1);
310 fan_auto_channel_offset(2);
313 static ssize_t
show_auto_temp_off(struct device
*dev
, char *buf
, int nr
)
315 struct adm1031_data
*data
= adm1031_update_device(dev
);
316 return sprintf(buf
, "%d\n",
317 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
319 static ssize_t
show_auto_temp_min(struct device
*dev
, char *buf
, int nr
)
321 struct adm1031_data
*data
= adm1031_update_device(dev
);
322 return sprintf(buf
, "%d\n",
323 AUTO_TEMP_MIN_FROM_REG(data
->auto_temp
[nr
]));
326 set_auto_temp_min(struct device
*dev
, const char *buf
, size_t count
, int nr
)
328 struct i2c_client
*client
= to_i2c_client(dev
);
329 struct adm1031_data
*data
= i2c_get_clientdata(client
);
330 int val
= simple_strtol(buf
, NULL
, 10);
332 down(&data
->update_lock
);
333 data
->auto_temp
[nr
] = AUTO_TEMP_MIN_TO_REG(val
, data
->auto_temp
[nr
]);
334 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
335 data
->auto_temp
[nr
]);
336 up(&data
->update_lock
);
339 static ssize_t
show_auto_temp_max(struct device
*dev
, char *buf
, int nr
)
341 struct adm1031_data
*data
= adm1031_update_device(dev
);
342 return sprintf(buf
, "%d\n",
343 AUTO_TEMP_MAX_FROM_REG(data
->auto_temp
[nr
]));
346 set_auto_temp_max(struct device
*dev
, const char *buf
, size_t count
, int nr
)
348 struct i2c_client
*client
= to_i2c_client(dev
);
349 struct adm1031_data
*data
= i2c_get_clientdata(client
);
350 int val
= simple_strtol(buf
, NULL
, 10);
352 down(&data
->update_lock
);
353 data
->temp_max
[nr
] = AUTO_TEMP_MAX_TO_REG(val
, data
->auto_temp
[nr
], data
->pwm
[nr
]);
354 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
356 up(&data
->update_lock
);
360 #define auto_temp_reg(offset) \
361 static ssize_t show_auto_temp_##offset##_off (struct device *dev, struct device_attribute *attr, char *buf) \
363 return show_auto_temp_off(dev, buf, offset - 1); \
365 static ssize_t show_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
367 return show_auto_temp_min(dev, buf, offset - 1); \
369 static ssize_t show_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
371 return show_auto_temp_max(dev, buf, offset - 1); \
373 static ssize_t set_auto_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
374 const char *buf, size_t count) \
376 return set_auto_temp_min(dev, buf, count, offset - 1); \
378 static ssize_t set_auto_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
379 const char *buf, size_t count) \
381 return set_auto_temp_max(dev, buf, count, offset - 1); \
383 static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
384 show_auto_temp_##offset##_off, NULL); \
385 static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
386 show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
387 static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
388 show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
395 static ssize_t
show_pwm(struct device
*dev
, char *buf
, int nr
)
397 struct adm1031_data
*data
= adm1031_update_device(dev
);
398 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
401 set_pwm(struct device
*dev
, const char *buf
, size_t count
, int nr
)
403 struct i2c_client
*client
= to_i2c_client(dev
);
404 struct adm1031_data
*data
= i2c_get_clientdata(client
);
405 int val
= simple_strtol(buf
, NULL
, 10);
408 down(&data
->update_lock
);
409 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
410 (((val
>>4) & 0xf) != 5)) {
411 /* In automatic mode, the only PWM accepted is 33% */
412 up(&data
->update_lock
);
415 data
->pwm
[nr
] = PWM_TO_REG(val
);
416 reg
= adm1031_read_value(client
, ADM1031_REG_PWM
);
417 adm1031_write_value(client
, ADM1031_REG_PWM
,
418 nr
? ((data
->pwm
[nr
] << 4) & 0xf0) | (reg
& 0xf)
419 : (data
->pwm
[nr
] & 0xf) | (reg
& 0xf0));
420 up(&data
->update_lock
);
424 #define pwm_reg(offset) \
425 static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
427 return show_pwm(dev, buf, offset - 1); \
429 static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr, \
430 const char *buf, size_t count) \
432 return set_pwm(dev, buf, count, offset - 1); \
434 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
435 show_pwm_##offset, set_pwm_##offset)
443 * That function checks the cases where the fan reading is not
444 * relevant. It is used to provide 0 as fan reading when the fan is
445 * not supposed to run
447 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
451 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
452 switch (data
->conf1
& 0x60) {
453 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
454 res
= data
->temp
[chan
+1] >=
455 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[chan
+1]);
457 case 0x20: /* remote temp1 controls both fans */
460 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
462 case 0x40: /* remote temp2 controls both fans */
465 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
467 case 0x60: /* max controls both fans */
470 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
472 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
473 || (data
->chip_type
== adm1031
475 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
479 res
= data
->pwm
[chan
] > 0;
485 static ssize_t
show_fan(struct device
*dev
, char *buf
, int nr
)
487 struct adm1031_data
*data
= adm1031_update_device(dev
);
490 value
= trust_fan_readings(data
, nr
) ? FAN_FROM_REG(data
->fan
[nr
],
491 FAN_DIV_FROM_REG(data
->fan_div
[nr
])) : 0;
492 return sprintf(buf
, "%d\n", value
);
495 static ssize_t
show_fan_div(struct device
*dev
, char *buf
, int nr
)
497 struct adm1031_data
*data
= adm1031_update_device(dev
);
498 return sprintf(buf
, "%d\n", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
500 static ssize_t
show_fan_min(struct device
*dev
, char *buf
, int nr
)
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
])));
508 set_fan_min(struct device
*dev
, const char *buf
, size_t count
, int nr
)
510 struct i2c_client
*client
= to_i2c_client(dev
);
511 struct adm1031_data
*data
= i2c_get_clientdata(client
);
512 int val
= simple_strtol(buf
, NULL
, 10);
514 down(&data
->update_lock
);
517 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
519 data
->fan_min
[nr
] = 0xff;
521 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
522 up(&data
->update_lock
);
526 set_fan_div(struct device
*dev
, const char *buf
, size_t count
, int nr
)
528 struct i2c_client
*client
= to_i2c_client(dev
);
529 struct adm1031_data
*data
= i2c_get_clientdata(client
);
530 int val
= simple_strtol(buf
, NULL
, 10);
535 tmp
= val
== 8 ? 0xc0 :
543 down(&data
->update_lock
);
544 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
545 data
->fan_div
[nr
] = (tmp
& 0xC0) | (0x3f & data
->fan_div
[nr
]);
546 new_min
= data
->fan_min
[nr
] * old_div
/
547 FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
548 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
549 data
->fan
[nr
] = data
->fan
[nr
] * old_div
/
550 FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
552 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
554 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
556 up(&data
->update_lock
);
560 #define fan_offset(offset) \
561 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
563 return show_fan(dev, buf, offset - 1); \
565 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
567 return show_fan_min(dev, buf, offset - 1); \
569 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
571 return show_fan_div(dev, buf, offset - 1); \
573 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
574 const char *buf, size_t count) \
576 return set_fan_min(dev, buf, count, offset - 1); \
578 static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
579 const char *buf, size_t count) \
581 return set_fan_div(dev, buf, count, offset - 1); \
583 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, \
585 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
586 show_fan_##offset##_min, set_fan_##offset##_min); \
587 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
588 show_fan_##offset##_div, set_fan_##offset##_div); \
589 static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR, \
590 show_pwm_##offset, set_pwm_##offset)
597 static ssize_t
show_temp(struct device
*dev
, char *buf
, int nr
)
599 struct adm1031_data
*data
= adm1031_update_device(dev
);
602 ((data
->ext_temp
[nr
] >> 6) & 0x3) * 2 :
603 (((data
->ext_temp
[nr
] >> ((nr
- 1) * 3)) & 7));
604 return sprintf(buf
, "%d\n", TEMP_FROM_REG_EXT(data
->temp
[nr
], ext
));
606 static ssize_t
show_temp_min(struct device
*dev
, char *buf
, int nr
)
608 struct adm1031_data
*data
= adm1031_update_device(dev
);
609 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
611 static ssize_t
show_temp_max(struct device
*dev
, char *buf
, int nr
)
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
, char *buf
, int nr
)
618 struct adm1031_data
*data
= adm1031_update_device(dev
);
619 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[nr
]));
622 set_temp_min(struct device
*dev
, const char *buf
, size_t count
, int nr
)
624 struct i2c_client
*client
= to_i2c_client(dev
);
625 struct adm1031_data
*data
= i2c_get_clientdata(client
);
628 val
= simple_strtol(buf
, NULL
, 10);
629 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
630 down(&data
->update_lock
);
631 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
632 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
634 up(&data
->update_lock
);
638 set_temp_max(struct device
*dev
, const char *buf
, size_t count
, int nr
)
640 struct i2c_client
*client
= to_i2c_client(dev
);
641 struct adm1031_data
*data
= i2c_get_clientdata(client
);
644 val
= simple_strtol(buf
, NULL
, 10);
645 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
646 down(&data
->update_lock
);
647 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
648 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
650 up(&data
->update_lock
);
654 set_temp_crit(struct device
*dev
, const char *buf
, size_t count
, int nr
)
656 struct i2c_client
*client
= to_i2c_client(dev
);
657 struct adm1031_data
*data
= i2c_get_clientdata(client
);
660 val
= simple_strtol(buf
, NULL
, 10);
661 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
662 down(&data
->update_lock
);
663 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
664 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
665 data
->temp_crit
[nr
]);
666 up(&data
->update_lock
);
670 #define temp_reg(offset) \
671 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
673 return show_temp(dev, buf, offset - 1); \
675 static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
677 return show_temp_min(dev, buf, offset - 1); \
679 static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
681 return show_temp_max(dev, buf, offset - 1); \
683 static ssize_t show_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, char *buf) \
685 return show_temp_crit(dev, buf, offset - 1); \
687 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
688 const char *buf, size_t count) \
690 return set_temp_min(dev, buf, count, offset - 1); \
692 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
693 const char *buf, size_t count) \
695 return set_temp_max(dev, buf, count, offset - 1); \
697 static ssize_t set_temp_##offset##_crit (struct device *dev, struct device_attribute *attr, \
698 const char *buf, size_t count) \
700 return set_temp_crit(dev, buf, count, offset - 1); \
702 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, \
704 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
705 show_temp_##offset##_min, set_temp_##offset##_min); \
706 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
707 show_temp_##offset##_max, set_temp_##offset##_max); \
708 static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
709 show_temp_##offset##_crit, set_temp_##offset##_crit)
716 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
718 struct adm1031_data
*data
= adm1031_update_device(dev
);
719 return sprintf(buf
, "%d\n", data
->alarm
);
722 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
725 static int adm1031_attach_adapter(struct i2c_adapter
*adapter
)
727 if (!(adapter
->class & I2C_CLASS_HWMON
))
729 return i2c_probe(adapter
, &addr_data
, adm1031_detect
);
732 /* This function is called by i2c_probe */
733 static int adm1031_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
735 struct i2c_client
*new_client
;
736 struct adm1031_data
*data
;
738 const char *name
= "";
740 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
743 if (!(data
= kzalloc(sizeof(struct adm1031_data
), GFP_KERNEL
))) {
748 new_client
= &data
->client
;
749 i2c_set_clientdata(new_client
, data
);
750 new_client
->addr
= address
;
751 new_client
->adapter
= adapter
;
752 new_client
->driver
= &adm1031_driver
;
753 new_client
->flags
= 0;
757 id
= i2c_smbus_read_byte_data(new_client
, 0x3d);
758 co
= i2c_smbus_read_byte_data(new_client
, 0x3e);
760 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
762 kind
= (id
== 0x30) ? adm1030
: adm1031
;
768 /* Given the detected chip type, set the chip name and the
769 * auto fan control helper table. */
770 if (kind
== adm1030
) {
772 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
773 } else if (kind
== adm1031
) {
775 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
777 data
->chip_type
= kind
;
779 strlcpy(new_client
->name
, name
, I2C_NAME_SIZE
);
781 init_MUTEX(&data
->update_lock
);
783 /* Tell the I2C layer a new client has arrived */
784 if ((err
= i2c_attach_client(new_client
)))
787 /* Initialize the ADM1031 chip */
788 adm1031_init_client(new_client
);
790 /* Register sysfs hooks */
791 data
->class_dev
= hwmon_device_register(&new_client
->dev
);
792 if (IS_ERR(data
->class_dev
)) {
793 err
= PTR_ERR(data
->class_dev
);
797 device_create_file(&new_client
->dev
, &dev_attr_fan1_input
);
798 device_create_file(&new_client
->dev
, &dev_attr_fan1_div
);
799 device_create_file(&new_client
->dev
, &dev_attr_fan1_min
);
800 device_create_file(&new_client
->dev
, &dev_attr_pwm1
);
801 device_create_file(&new_client
->dev
, &dev_attr_auto_fan1_channel
);
802 device_create_file(&new_client
->dev
, &dev_attr_temp1_input
);
803 device_create_file(&new_client
->dev
, &dev_attr_temp1_min
);
804 device_create_file(&new_client
->dev
, &dev_attr_temp1_max
);
805 device_create_file(&new_client
->dev
, &dev_attr_temp1_crit
);
806 device_create_file(&new_client
->dev
, &dev_attr_temp2_input
);
807 device_create_file(&new_client
->dev
, &dev_attr_temp2_min
);
808 device_create_file(&new_client
->dev
, &dev_attr_temp2_max
);
809 device_create_file(&new_client
->dev
, &dev_attr_temp2_crit
);
811 device_create_file(&new_client
->dev
, &dev_attr_auto_temp1_off
);
812 device_create_file(&new_client
->dev
, &dev_attr_auto_temp1_min
);
813 device_create_file(&new_client
->dev
, &dev_attr_auto_temp1_max
);
815 device_create_file(&new_client
->dev
, &dev_attr_auto_temp2_off
);
816 device_create_file(&new_client
->dev
, &dev_attr_auto_temp2_min
);
817 device_create_file(&new_client
->dev
, &dev_attr_auto_temp2_max
);
819 device_create_file(&new_client
->dev
, &dev_attr_auto_fan1_min_pwm
);
821 device_create_file(&new_client
->dev
, &dev_attr_alarms
);
823 if (kind
== adm1031
) {
824 device_create_file(&new_client
->dev
, &dev_attr_fan2_input
);
825 device_create_file(&new_client
->dev
, &dev_attr_fan2_div
);
826 device_create_file(&new_client
->dev
, &dev_attr_fan2_min
);
827 device_create_file(&new_client
->dev
, &dev_attr_pwm2
);
828 device_create_file(&new_client
->dev
,
829 &dev_attr_auto_fan2_channel
);
830 device_create_file(&new_client
->dev
, &dev_attr_temp3_input
);
831 device_create_file(&new_client
->dev
, &dev_attr_temp3_min
);
832 device_create_file(&new_client
->dev
, &dev_attr_temp3_max
);
833 device_create_file(&new_client
->dev
, &dev_attr_temp3_crit
);
834 device_create_file(&new_client
->dev
, &dev_attr_auto_temp3_off
);
835 device_create_file(&new_client
->dev
, &dev_attr_auto_temp3_min
);
836 device_create_file(&new_client
->dev
, &dev_attr_auto_temp3_max
);
837 device_create_file(&new_client
->dev
, &dev_attr_auto_fan2_min_pwm
);
843 i2c_detach_client(new_client
);
850 static int adm1031_detach_client(struct i2c_client
*client
)
852 struct adm1031_data
*data
= i2c_get_clientdata(client
);
855 hwmon_device_unregister(data
->class_dev
);
856 if ((ret
= i2c_detach_client(client
)) != 0) {
863 static void adm1031_init_client(struct i2c_client
*client
)
865 unsigned int read_val
;
867 struct adm1031_data
*data
= i2c_get_clientdata(client
);
869 mask
= (ADM1031_CONF2_PWM1_ENABLE
| ADM1031_CONF2_TACH1_ENABLE
);
870 if (data
->chip_type
== adm1031
) {
871 mask
|= (ADM1031_CONF2_PWM2_ENABLE
|
872 ADM1031_CONF2_TACH2_ENABLE
);
874 /* Initialize the ADM1031 chip (enables fan speed reading ) */
875 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
876 if ((read_val
| mask
) != read_val
) {
877 adm1031_write_value(client
, ADM1031_REG_CONF2
, read_val
| mask
);
880 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
881 if ((read_val
| ADM1031_CONF1_MONITOR_ENABLE
) != read_val
) {
882 adm1031_write_value(client
, ADM1031_REG_CONF1
, read_val
|
883 ADM1031_CONF1_MONITOR_ENABLE
);
888 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
890 struct i2c_client
*client
= to_i2c_client(dev
);
891 struct adm1031_data
*data
= i2c_get_clientdata(client
);
894 down(&data
->update_lock
);
896 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
899 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
901 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
905 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
906 data
->ext_temp
[chan
] =
907 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
909 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
911 data
->ext_temp
[chan
] =
912 adm1031_read_value(client
,
913 ADM1031_REG_EXT_TEMP
);
916 adm1031_read_value(client
,
917 ADM1031_REG_TEMP(chan
));
919 /* oldh is actually newer */
921 dev_warn(&client
->dev
,
922 "Remote temperature may be "
926 data
->temp
[chan
] = newh
;
928 data
->temp_min
[chan
] =
929 adm1031_read_value(client
,
930 ADM1031_REG_TEMP_MIN(chan
));
931 data
->temp_max
[chan
] =
932 adm1031_read_value(client
,
933 ADM1031_REG_TEMP_MAX(chan
));
934 data
->temp_crit
[chan
] =
935 adm1031_read_value(client
,
936 ADM1031_REG_TEMP_CRIT(chan
));
937 data
->auto_temp
[chan
] =
938 adm1031_read_value(client
,
939 ADM1031_REG_AUTO_TEMP(chan
));
943 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
944 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
946 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
947 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1))
949 if (data
->chip_type
== adm1030
) {
950 data
->alarm
&= 0xc0ff;
953 for (chan
=0; chan
<(data
->chip_type
== adm1030
? 1 : 2); chan
++) {
954 data
->fan_div
[chan
] =
955 adm1031_read_value(client
, ADM1031_REG_FAN_DIV(chan
));
956 data
->fan_min
[chan
] =
957 adm1031_read_value(client
, ADM1031_REG_FAN_MIN(chan
));
959 adm1031_read_value(client
, ADM1031_REG_FAN_SPEED(chan
));
961 0xf & (adm1031_read_value(client
, ADM1031_REG_PWM
) >>
964 data
->last_updated
= jiffies
;
968 up(&data
->update_lock
);
973 static int __init
sensors_adm1031_init(void)
975 return i2c_add_driver(&adm1031_driver
);
978 static void __exit
sensors_adm1031_exit(void)
980 i2c_del_driver(&adm1031_driver
);
983 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
984 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
985 MODULE_LICENSE("GPL");
987 module_init(sensors_adm1031_init
);
988 module_exit(sensors_adm1031_exit
);