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 <<<<<<< HEAD
:drivers
/hwmon
/adm1031
.c
65 static unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
67 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
68 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/adm1031
.c
70 /* Insmod parameters */
71 I2C_CLIENT_INSMOD_2(adm1030
, adm1031
);
73 typedef u8 auto_chan_table_t
[8][2];
75 /* Each client has this additional data */
77 struct i2c_client client
;
78 struct device
*hwmon_dev
;
79 struct mutex update_lock
;
81 char valid
; /* !=0 if following fields are valid */
82 unsigned long last_updated
; /* In jiffies */
83 /* The chan_select_table contains the possible configurations for
86 const auto_chan_table_t
*chan_select_table
;
106 static int adm1031_attach_adapter(struct i2c_adapter
*adapter
);
107 static int adm1031_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
108 static void adm1031_init_client(struct i2c_client
*client
);
109 static int adm1031_detach_client(struct i2c_client
*client
);
110 static struct adm1031_data
*adm1031_update_device(struct device
*dev
);
112 /* This is the driver that will be inserted */
113 static struct i2c_driver adm1031_driver
= {
117 .attach_adapter
= adm1031_attach_adapter
,
118 .detach_client
= adm1031_detach_client
,
121 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
123 return i2c_smbus_read_byte_data(client
, reg
);
127 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
129 return i2c_smbus_write_byte_data(client
, reg
, value
);
133 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
134 ((val + 500) / 1000)))
136 #define TEMP_FROM_REG(val) ((val) * 1000)
138 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
140 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
142 static int FAN_TO_REG(int reg
, int div
)
145 tmp
= FAN_FROM_REG(SENSORS_LIMIT(reg
, 0, 65535), div
);
146 return tmp
> 255 ? 255 : tmp
;
149 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
151 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
152 #define PWM_FROM_REG(val) ((val) << 4)
154 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
155 #define FAN_CHAN_TO_REG(val, reg) \
156 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
158 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
159 ((((val)/500) & 0xf8)|((reg) & 0x7))
160 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
161 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
163 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
165 #define AUTO_TEMP_OFF_FROM_REG(reg) \
166 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
168 #define AUTO_TEMP_MAX_FROM_REG(reg) \
169 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
170 AUTO_TEMP_MIN_FROM_REG(reg))
172 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
175 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
177 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
178 ret
= ((reg
& 0xf8) |
181 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
185 /* FAN auto control */
186 #define GET_FAN_AUTO_BITFIELD(data, idx) \
187 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
189 /* The tables below contains the possible values for the auto fan
190 * control bitfields. the index in the table is the register value.
191 * MSb is the auto fan control enable bit, so the four first entries
192 * in the table disables auto fan control when both bitfields are zero.
194 static const auto_chan_table_t auto_channel_select_table_adm1031
= {
195 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
196 { 2 /* 0b010 */ , 4 /* 0b100 */ },
197 { 2 /* 0b010 */ , 2 /* 0b010 */ },
198 { 4 /* 0b100 */ , 4 /* 0b100 */ },
199 { 7 /* 0b111 */ , 7 /* 0b111 */ },
202 static const auto_chan_table_t auto_channel_select_table_adm1030
= {
203 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
204 { 2 /* 0b10 */ , 0 },
205 { 0xff /* invalid */ , 0 },
206 { 0xff /* invalid */ , 0 },
207 { 3 /* 0b11 */ , 0 },
210 /* That function checks if a bitfield is valid and returns the other bitfield
211 * nearest match if no exact match where found.
214 get_fan_auto_nearest(struct adm1031_data
*data
,
215 int chan
, u8 val
, u8 reg
, u8
* new_reg
)
218 int first_match
= -1, exact_match
= -1;
220 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
227 for (i
= 0; i
< 8; i
++) {
228 if ((val
== (*data
->chan_select_table
)[i
][chan
]) &&
229 ((*data
->chan_select_table
)[i
][chan
? 0 : 1] ==
231 /* We found an exact match */
234 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
236 /* Save the first match in case of an exact match has
243 if (exact_match
>= 0) {
244 *new_reg
= exact_match
;
245 } else if (first_match
>= 0) {
246 *new_reg
= first_match
;
253 static ssize_t
show_fan_auto_channel(struct device
*dev
,
254 struct device_attribute
*attr
, char *buf
)
256 int nr
= to_sensor_dev_attr(attr
)->index
;
257 struct adm1031_data
*data
= adm1031_update_device(dev
);
258 return sprintf(buf
, "%d\n", GET_FAN_AUTO_BITFIELD(data
, nr
));
262 set_fan_auto_channel(struct device
*dev
, struct device_attribute
*attr
,
263 const char *buf
, size_t count
)
265 struct i2c_client
*client
= to_i2c_client(dev
);
266 struct adm1031_data
*data
= i2c_get_clientdata(client
);
267 int nr
= to_sensor_dev_attr(attr
)->index
;
268 int val
= simple_strtol(buf
, NULL
, 10);
273 old_fan_mode
= data
->conf1
;
275 mutex_lock(&data
->update_lock
);
277 if ((ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
, ®
))) {
278 mutex_unlock(&data
->update_lock
);
281 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
282 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) ^
283 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
284 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
){
285 /* Switch to Auto Fan Mode
287 * Set PWM registers to 33% Both */
288 data
->old_pwm
[0] = data
->pwm
[0];
289 data
->old_pwm
[1] = data
->pwm
[1];
290 adm1031_write_value(client
, ADM1031_REG_PWM
, 0x55);
292 /* Switch to Manual Mode */
293 data
->pwm
[0] = data
->old_pwm
[0];
294 data
->pwm
[1] = data
->old_pwm
[1];
295 /* Restore PWM registers */
296 adm1031_write_value(client
, ADM1031_REG_PWM
,
297 data
->pwm
[0] | (data
->pwm
[1] << 4));
300 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
301 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
302 mutex_unlock(&data
->update_lock
);
306 static SENSOR_DEVICE_ATTR(auto_fan1_channel
, S_IRUGO
| S_IWUSR
,
307 show_fan_auto_channel
, set_fan_auto_channel
, 0);
308 static SENSOR_DEVICE_ATTR(auto_fan2_channel
, S_IRUGO
| S_IWUSR
,
309 show_fan_auto_channel
, set_fan_auto_channel
, 1);
312 static ssize_t
show_auto_temp_off(struct device
*dev
,
313 struct device_attribute
*attr
, char *buf
)
315 int nr
= to_sensor_dev_attr(attr
)->index
;
316 struct adm1031_data
*data
= adm1031_update_device(dev
);
317 return sprintf(buf
, "%d\n",
318 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
320 static ssize_t
show_auto_temp_min(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_MIN_FROM_REG(data
->auto_temp
[nr
]));
329 set_auto_temp_min(struct device
*dev
, struct device_attribute
*attr
,
330 const char *buf
, size_t count
)
332 struct i2c_client
*client
= to_i2c_client(dev
);
333 struct adm1031_data
*data
= i2c_get_clientdata(client
);
334 int nr
= to_sensor_dev_attr(attr
)->index
;
335 int val
= simple_strtol(buf
, NULL
, 10);
337 mutex_lock(&data
->update_lock
);
338 data
->auto_temp
[nr
] = AUTO_TEMP_MIN_TO_REG(val
, data
->auto_temp
[nr
]);
339 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
340 data
->auto_temp
[nr
]);
341 mutex_unlock(&data
->update_lock
);
344 static ssize_t
show_auto_temp_max(struct device
*dev
,
345 struct device_attribute
*attr
, char *buf
)
347 int nr
= to_sensor_dev_attr(attr
)->index
;
348 struct adm1031_data
*data
= adm1031_update_device(dev
);
349 return sprintf(buf
, "%d\n",
350 AUTO_TEMP_MAX_FROM_REG(data
->auto_temp
[nr
]));
353 set_auto_temp_max(struct device
*dev
, struct device_attribute
*attr
,
354 const char *buf
, size_t count
)
356 struct i2c_client
*client
= to_i2c_client(dev
);
357 struct adm1031_data
*data
= i2c_get_clientdata(client
);
358 int nr
= to_sensor_dev_attr(attr
)->index
;
359 int val
= simple_strtol(buf
, NULL
, 10);
361 mutex_lock(&data
->update_lock
);
362 data
->temp_max
[nr
] = AUTO_TEMP_MAX_TO_REG(val
, data
->auto_temp
[nr
], data
->pwm
[nr
]);
363 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
365 mutex_unlock(&data
->update_lock
);
369 #define auto_temp_reg(offset) \
370 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
371 show_auto_temp_off, NULL, offset - 1); \
372 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
373 show_auto_temp_min, set_auto_temp_min, offset - 1); \
374 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
375 show_auto_temp_max, set_auto_temp_max, offset - 1)
382 static ssize_t
show_pwm(struct device
*dev
,
383 struct device_attribute
*attr
, char *buf
)
385 int nr
= to_sensor_dev_attr(attr
)->index
;
386 struct adm1031_data
*data
= adm1031_update_device(dev
);
387 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
389 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
390 const char *buf
, size_t count
)
392 struct i2c_client
*client
= to_i2c_client(dev
);
393 struct adm1031_data
*data
= i2c_get_clientdata(client
);
394 int nr
= to_sensor_dev_attr(attr
)->index
;
395 int val
= simple_strtol(buf
, NULL
, 10);
398 mutex_lock(&data
->update_lock
);
399 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
400 (((val
>>4) & 0xf) != 5)) {
401 /* In automatic mode, the only PWM accepted is 33% */
402 mutex_unlock(&data
->update_lock
);
405 data
->pwm
[nr
] = PWM_TO_REG(val
);
406 reg
= adm1031_read_value(client
, ADM1031_REG_PWM
);
407 adm1031_write_value(client
, ADM1031_REG_PWM
,
408 nr
? ((data
->pwm
[nr
] << 4) & 0xf0) | (reg
& 0xf)
409 : (data
->pwm
[nr
] & 0xf) | (reg
& 0xf0));
410 mutex_unlock(&data
->update_lock
);
414 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 0);
415 static SENSOR_DEVICE_ATTR(pwm2
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 1);
416 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm
, S_IRUGO
| S_IWUSR
,
417 show_pwm
, set_pwm
, 0);
418 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm
, S_IRUGO
| S_IWUSR
,
419 show_pwm
, set_pwm
, 1);
424 * That function checks the cases where the fan reading is not
425 * relevant. It is used to provide 0 as fan reading when the fan is
426 * not supposed to run
428 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
432 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
433 switch (data
->conf1
& 0x60) {
434 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
435 res
= data
->temp
[chan
+1] >=
436 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[chan
+1]);
438 case 0x20: /* remote temp1 controls both fans */
441 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
443 case 0x40: /* remote temp2 controls both fans */
446 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
448 case 0x60: /* max controls both fans */
451 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
453 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
454 || (data
->chip_type
== adm1031
456 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
460 res
= data
->pwm
[chan
] > 0;
466 static ssize_t
show_fan(struct device
*dev
,
467 struct device_attribute
*attr
, char *buf
)
469 int nr
= to_sensor_dev_attr(attr
)->index
;
470 struct adm1031_data
*data
= adm1031_update_device(dev
);
473 value
= trust_fan_readings(data
, nr
) ? FAN_FROM_REG(data
->fan
[nr
],
474 FAN_DIV_FROM_REG(data
->fan_div
[nr
])) : 0;
475 return sprintf(buf
, "%d\n", value
);
478 static ssize_t
show_fan_div(struct device
*dev
,
479 struct device_attribute
*attr
, char *buf
)
481 int nr
= to_sensor_dev_attr(attr
)->index
;
482 struct adm1031_data
*data
= adm1031_update_device(dev
);
483 return sprintf(buf
, "%d\n", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
485 static ssize_t
show_fan_min(struct device
*dev
,
486 struct device_attribute
*attr
, char *buf
)
488 int nr
= to_sensor_dev_attr(attr
)->index
;
489 struct adm1031_data
*data
= adm1031_update_device(dev
);
490 return sprintf(buf
, "%d\n",
491 FAN_FROM_REG(data
->fan_min
[nr
],
492 FAN_DIV_FROM_REG(data
->fan_div
[nr
])));
494 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
495 const char *buf
, size_t count
)
497 struct i2c_client
*client
= to_i2c_client(dev
);
498 struct adm1031_data
*data
= i2c_get_clientdata(client
);
499 int nr
= to_sensor_dev_attr(attr
)->index
;
500 int val
= simple_strtol(buf
, NULL
, 10);
502 mutex_lock(&data
->update_lock
);
505 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
507 data
->fan_min
[nr
] = 0xff;
509 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
510 mutex_unlock(&data
->update_lock
);
513 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
514 const char *buf
, size_t count
)
516 struct i2c_client
*client
= to_i2c_client(dev
);
517 struct adm1031_data
*data
= i2c_get_clientdata(client
);
518 int nr
= to_sensor_dev_attr(attr
)->index
;
519 int val
= simple_strtol(buf
, NULL
, 10);
524 tmp
= val
== 8 ? 0xc0 :
532 mutex_lock(&data
->update_lock
);
533 /* Get fresh readings */
534 data
->fan_div
[nr
] = adm1031_read_value(client
,
535 ADM1031_REG_FAN_DIV(nr
));
536 data
->fan_min
[nr
] = adm1031_read_value(client
,
537 ADM1031_REG_FAN_MIN(nr
));
539 /* Write the new clock divider and fan min */
540 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
541 data
->fan_div
[nr
] = tmp
| (0x3f & data
->fan_div
[nr
]);
542 new_min
= data
->fan_min
[nr
] * old_div
/ val
;
543 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
545 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
547 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
550 /* Invalidate the cache: fan speed is no longer valid */
552 mutex_unlock(&data
->update_lock
);
556 #define fan_offset(offset) \
557 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
558 show_fan, NULL, offset - 1); \
559 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
560 show_fan_min, set_fan_min, offset - 1); \
561 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
562 show_fan_div, set_fan_div, offset - 1)
569 static ssize_t
show_temp(struct device
*dev
,
570 struct device_attribute
*attr
, char *buf
)
572 int nr
= to_sensor_dev_attr(attr
)->index
;
573 struct adm1031_data
*data
= adm1031_update_device(dev
);
576 ((data
->ext_temp
[nr
] >> 6) & 0x3) * 2 :
577 (((data
->ext_temp
[nr
] >> ((nr
- 1) * 3)) & 7));
578 return sprintf(buf
, "%d\n", TEMP_FROM_REG_EXT(data
->temp
[nr
], ext
));
580 static ssize_t
show_temp_min(struct device
*dev
,
581 struct device_attribute
*attr
, char *buf
)
583 int nr
= to_sensor_dev_attr(attr
)->index
;
584 struct adm1031_data
*data
= adm1031_update_device(dev
);
585 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
587 static ssize_t
show_temp_max(struct device
*dev
,
588 struct device_attribute
*attr
, char *buf
)
590 int nr
= to_sensor_dev_attr(attr
)->index
;
591 struct adm1031_data
*data
= adm1031_update_device(dev
);
592 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
594 static ssize_t
show_temp_crit(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", TEMP_FROM_REG(data
->temp_crit
[nr
]));
601 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
602 const char *buf
, size_t count
)
604 struct i2c_client
*client
= to_i2c_client(dev
);
605 struct adm1031_data
*data
= i2c_get_clientdata(client
);
606 int nr
= to_sensor_dev_attr(attr
)->index
;
609 val
= simple_strtol(buf
, NULL
, 10);
610 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
611 mutex_lock(&data
->update_lock
);
612 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
613 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
615 mutex_unlock(&data
->update_lock
);
618 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
619 const char *buf
, size_t count
)
621 struct i2c_client
*client
= to_i2c_client(dev
);
622 struct adm1031_data
*data
= i2c_get_clientdata(client
);
623 int nr
= to_sensor_dev_attr(attr
)->index
;
626 val
= simple_strtol(buf
, NULL
, 10);
627 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
628 mutex_lock(&data
->update_lock
);
629 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
630 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
632 mutex_unlock(&data
->update_lock
);
635 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*attr
,
636 const char *buf
, size_t count
)
638 struct i2c_client
*client
= to_i2c_client(dev
);
639 struct adm1031_data
*data
= i2c_get_clientdata(client
);
640 int nr
= to_sensor_dev_attr(attr
)->index
;
643 val
= simple_strtol(buf
, NULL
, 10);
644 val
= SENSORS_LIMIT(val
, -55000, nr
== 0 ? 127750 : 127875);
645 mutex_lock(&data
->update_lock
);
646 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
647 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
648 data
->temp_crit
[nr
]);
649 mutex_unlock(&data
->update_lock
);
653 #define temp_reg(offset) \
654 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
655 show_temp, NULL, offset - 1); \
656 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
657 show_temp_min, set_temp_min, offset - 1); \
658 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
659 show_temp_max, set_temp_max, offset - 1); \
660 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
661 show_temp_crit, set_temp_crit, offset - 1)
668 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
670 struct adm1031_data
*data
= adm1031_update_device(dev
);
671 return sprintf(buf
, "%d\n", data
->alarm
);
674 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
676 static ssize_t
show_alarm(struct device
*dev
,
677 struct device_attribute
*attr
, char *buf
)
679 int bitnr
= to_sensor_dev_attr(attr
)->index
;
680 struct adm1031_data
*data
= adm1031_update_device(dev
);
681 return sprintf(buf
, "%d\n", (data
->alarm
>> bitnr
) & 1);
684 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
685 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, show_alarm
, NULL
, 1);
686 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
687 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
688 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
689 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 5);
690 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
691 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
692 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
693 static SENSOR_DEVICE_ATTR(fan2_fault
, S_IRUGO
, show_alarm
, NULL
, 9);
694 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
695 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
696 static SENSOR_DEVICE_ATTR(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
697 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 13);
698 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
700 static int adm1031_attach_adapter(struct i2c_adapter
*adapter
)
702 if (!(adapter
->class & I2C_CLASS_HWMON
))
704 return i2c_probe(adapter
, &addr_data
, adm1031_detect
);
707 static struct attribute
*adm1031_attributes
[] = {
708 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
709 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
710 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
711 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
712 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
713 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
714 &sensor_dev_attr_auto_fan1_channel
.dev_attr
.attr
,
715 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
716 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
717 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
718 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
719 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
720 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
721 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
722 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
723 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
724 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
725 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
726 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
727 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
728 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
729 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
731 &sensor_dev_attr_auto_temp1_off
.dev_attr
.attr
,
732 &sensor_dev_attr_auto_temp1_min
.dev_attr
.attr
,
733 &sensor_dev_attr_auto_temp1_max
.dev_attr
.attr
,
735 &sensor_dev_attr_auto_temp2_off
.dev_attr
.attr
,
736 &sensor_dev_attr_auto_temp2_min
.dev_attr
.attr
,
737 &sensor_dev_attr_auto_temp2_max
.dev_attr
.attr
,
739 &sensor_dev_attr_auto_fan1_min_pwm
.dev_attr
.attr
,
741 &dev_attr_alarms
.attr
,
746 static const struct attribute_group adm1031_group
= {
747 .attrs
= adm1031_attributes
,
750 static struct attribute
*adm1031_attributes_opt
[] = {
751 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
752 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
753 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
754 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
755 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
756 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
757 &sensor_dev_attr_auto_fan2_channel
.dev_attr
.attr
,
758 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
759 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
760 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
761 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
762 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
763 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
764 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
765 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
766 &sensor_dev_attr_auto_temp3_off
.dev_attr
.attr
,
767 &sensor_dev_attr_auto_temp3_min
.dev_attr
.attr
,
768 &sensor_dev_attr_auto_temp3_max
.dev_attr
.attr
,
769 &sensor_dev_attr_auto_fan2_min_pwm
.dev_attr
.attr
,
773 static const struct attribute_group adm1031_group_opt
= {
774 .attrs
= adm1031_attributes_opt
,
777 /* This function is called by i2c_probe */
778 static int adm1031_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
780 struct i2c_client
*client
;
781 struct adm1031_data
*data
;
783 const char *name
= "";
785 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
788 if (!(data
= kzalloc(sizeof(struct adm1031_data
), GFP_KERNEL
))) {
793 client
= &data
->client
;
794 i2c_set_clientdata(client
, data
);
795 client
->addr
= address
;
796 client
->adapter
= adapter
;
797 client
->driver
= &adm1031_driver
;
801 id
= i2c_smbus_read_byte_data(client
, 0x3d);
802 co
= i2c_smbus_read_byte_data(client
, 0x3e);
804 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
806 kind
= (id
== 0x30) ? adm1030
: adm1031
;
812 /* Given the detected chip type, set the chip name and the
813 * auto fan control helper table. */
814 if (kind
== adm1030
) {
816 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
817 } else if (kind
== adm1031
) {
819 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
821 data
->chip_type
= kind
;
823 strlcpy(client
->name
, name
, I2C_NAME_SIZE
);
824 mutex_init(&data
->update_lock
);
826 /* Tell the I2C layer a new client has arrived */
827 if ((err
= i2c_attach_client(client
)))
830 /* Initialize the ADM1031 chip */
831 adm1031_init_client(client
);
833 /* Register sysfs hooks */
834 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &adm1031_group
)))
837 if (kind
== adm1031
) {
838 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
839 &adm1031_group_opt
)))
843 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
844 if (IS_ERR(data
->hwmon_dev
)) {
845 err
= PTR_ERR(data
->hwmon_dev
);
852 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
853 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
855 i2c_detach_client(client
);
862 static int adm1031_detach_client(struct i2c_client
*client
)
864 struct adm1031_data
*data
= i2c_get_clientdata(client
);
867 hwmon_device_unregister(data
->hwmon_dev
);
868 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group
);
869 sysfs_remove_group(&client
->dev
.kobj
, &adm1031_group_opt
);
870 if ((ret
= i2c_detach_client(client
)) != 0) {
877 static void adm1031_init_client(struct i2c_client
*client
)
879 unsigned int read_val
;
881 struct adm1031_data
*data
= i2c_get_clientdata(client
);
883 mask
= (ADM1031_CONF2_PWM1_ENABLE
| ADM1031_CONF2_TACH1_ENABLE
);
884 if (data
->chip_type
== adm1031
) {
885 mask
|= (ADM1031_CONF2_PWM2_ENABLE
|
886 ADM1031_CONF2_TACH2_ENABLE
);
888 /* Initialize the ADM1031 chip (enables fan speed reading ) */
889 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
890 if ((read_val
| mask
) != read_val
) {
891 adm1031_write_value(client
, ADM1031_REG_CONF2
, read_val
| mask
);
894 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
895 if ((read_val
| ADM1031_CONF1_MONITOR_ENABLE
) != read_val
) {
896 adm1031_write_value(client
, ADM1031_REG_CONF1
, read_val
|
897 ADM1031_CONF1_MONITOR_ENABLE
);
902 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
904 struct i2c_client
*client
= to_i2c_client(dev
);
905 struct adm1031_data
*data
= i2c_get_clientdata(client
);
908 mutex_lock(&data
->update_lock
);
910 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
913 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
915 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
919 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
920 data
->ext_temp
[chan
] =
921 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
923 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
925 data
->ext_temp
[chan
] =
926 adm1031_read_value(client
,
927 ADM1031_REG_EXT_TEMP
);
930 adm1031_read_value(client
,
931 ADM1031_REG_TEMP(chan
));
933 /* oldh is actually newer */
935 dev_warn(&client
->dev
,
936 "Remote temperature may be "
940 data
->temp
[chan
] = newh
;
942 data
->temp_min
[chan
] =
943 adm1031_read_value(client
,
944 ADM1031_REG_TEMP_MIN(chan
));
945 data
->temp_max
[chan
] =
946 adm1031_read_value(client
,
947 ADM1031_REG_TEMP_MAX(chan
));
948 data
->temp_crit
[chan
] =
949 adm1031_read_value(client
,
950 ADM1031_REG_TEMP_CRIT(chan
));
951 data
->auto_temp
[chan
] =
952 adm1031_read_value(client
,
953 ADM1031_REG_AUTO_TEMP(chan
));
957 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
958 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
960 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
961 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1))
963 if (data
->chip_type
== adm1030
) {
964 data
->alarm
&= 0xc0ff;
967 for (chan
=0; chan
<(data
->chip_type
== adm1030
? 1 : 2); chan
++) {
968 data
->fan_div
[chan
] =
969 adm1031_read_value(client
, ADM1031_REG_FAN_DIV(chan
));
970 data
->fan_min
[chan
] =
971 adm1031_read_value(client
, ADM1031_REG_FAN_MIN(chan
));
973 adm1031_read_value(client
, ADM1031_REG_FAN_SPEED(chan
));
975 0xf & (adm1031_read_value(client
, ADM1031_REG_PWM
) >>
978 data
->last_updated
= jiffies
;
982 mutex_unlock(&data
->update_lock
);
987 static int __init
sensors_adm1031_init(void)
989 return i2c_add_driver(&adm1031_driver
);
992 static void __exit
sensors_adm1031_exit(void)
994 i2c_del_driver(&adm1031_driver
);
997 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
998 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
999 MODULE_LICENSE("GPL");
1001 module_init(sensors_adm1031_init
);
1002 module_exit(sensors_adm1031_exit
);