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 <jdelvare@suse.de>
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))
39 #define ADM1031_REG_FAN_FILTER (0x23)
41 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
42 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
43 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
44 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
46 #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
47 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
49 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
51 #define ADM1031_REG_CONF1 0x00
52 #define ADM1031_REG_CONF2 0x01
53 #define ADM1031_REG_EXT_TEMP 0x06
55 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
56 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
57 #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
59 #define ADM1031_CONF2_PWM1_ENABLE 0x01
60 #define ADM1031_CONF2_PWM2_ENABLE 0x02
61 #define ADM1031_CONF2_TACH1_ENABLE 0x04
62 #define ADM1031_CONF2_TACH2_ENABLE 0x08
63 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
65 #define ADM1031_UPDATE_RATE_MASK 0x1c
66 #define ADM1031_UPDATE_RATE_SHIFT 2
68 /* Addresses to scan */
69 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
71 enum chips
{ adm1030
, adm1031
};
73 typedef u8 auto_chan_table_t
[8][2];
75 /* Each client has this additional data */
77 struct i2c_client
*client
;
78 const struct attribute_group
*groups
[3];
79 struct mutex update_lock
;
81 char valid
; /* !=0 if following fields are valid */
82 unsigned long last_updated
; /* In jiffies */
83 unsigned int update_interval
; /* In milliseconds */
85 * The chan_select_table contains the possible configurations for
88 const auto_chan_table_t
*chan_select_table
;
109 static inline u8
adm1031_read_value(struct i2c_client
*client
, u8 reg
)
111 return i2c_smbus_read_byte_data(client
, reg
);
115 adm1031_write_value(struct i2c_client
*client
, u8 reg
, unsigned int value
)
117 return i2c_smbus_write_byte_data(client
, reg
, value
);
120 static struct adm1031_data
*adm1031_update_device(struct device
*dev
)
122 struct adm1031_data
*data
= dev_get_drvdata(dev
);
123 struct i2c_client
*client
= data
->client
;
124 unsigned long next_update
;
127 mutex_lock(&data
->update_lock
);
129 next_update
= data
->last_updated
130 + msecs_to_jiffies(data
->update_interval
);
131 if (time_after(jiffies
, next_update
) || !data
->valid
) {
133 dev_dbg(&client
->dev
, "Starting adm1031 update\n");
135 chan
< ((data
->chip_type
== adm1031
) ? 3 : 2); chan
++) {
139 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
140 data
->ext_temp
[chan
] =
141 adm1031_read_value(client
, ADM1031_REG_EXT_TEMP
);
143 adm1031_read_value(client
, ADM1031_REG_TEMP(chan
));
145 data
->ext_temp
[chan
] =
146 adm1031_read_value(client
,
147 ADM1031_REG_EXT_TEMP
);
150 adm1031_read_value(client
,
151 ADM1031_REG_TEMP(chan
));
153 /* oldh is actually newer */
155 dev_warn(&client
->dev
,
156 "Remote temperature may be wrong.\n");
159 data
->temp
[chan
] = newh
;
161 data
->temp_offset
[chan
] =
162 adm1031_read_value(client
,
163 ADM1031_REG_TEMP_OFFSET(chan
));
164 data
->temp_min
[chan
] =
165 adm1031_read_value(client
,
166 ADM1031_REG_TEMP_MIN(chan
));
167 data
->temp_max
[chan
] =
168 adm1031_read_value(client
,
169 ADM1031_REG_TEMP_MAX(chan
));
170 data
->temp_crit
[chan
] =
171 adm1031_read_value(client
,
172 ADM1031_REG_TEMP_CRIT(chan
));
173 data
->auto_temp
[chan
] =
174 adm1031_read_value(client
,
175 ADM1031_REG_AUTO_TEMP(chan
));
179 data
->conf1
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
180 data
->conf2
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
182 data
->alarm
= adm1031_read_value(client
, ADM1031_REG_STATUS(0))
183 | (adm1031_read_value(client
, ADM1031_REG_STATUS(1)) << 8);
184 if (data
->chip_type
== adm1030
)
185 data
->alarm
&= 0xc0ff;
187 for (chan
= 0; chan
< (data
->chip_type
== adm1030
? 1 : 2);
189 data
->fan_div
[chan
] =
190 adm1031_read_value(client
,
191 ADM1031_REG_FAN_DIV(chan
));
192 data
->fan_min
[chan
] =
193 adm1031_read_value(client
,
194 ADM1031_REG_FAN_MIN(chan
));
196 adm1031_read_value(client
,
197 ADM1031_REG_FAN_SPEED(chan
));
199 (adm1031_read_value(client
,
200 ADM1031_REG_PWM
) >> (4 * chan
)) & 0x0f;
202 data
->last_updated
= jiffies
;
206 mutex_unlock(&data
->update_lock
);
211 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
212 ((val + 500) / 1000)))
214 #define TEMP_FROM_REG(val) ((val) * 1000)
216 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
218 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
219 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
220 (val) | 0x70 : (val))
222 #define FAN_FROM_REG(reg, div) ((reg) ? \
223 (11250 * 60) / ((reg) * (div)) : 0)
225 static int FAN_TO_REG(int reg
, int div
)
228 tmp
= FAN_FROM_REG(clamp_val(reg
, 0, 65535), div
);
229 return tmp
> 255 ? 255 : tmp
;
232 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
234 #define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
235 #define PWM_FROM_REG(val) ((val) << 4)
237 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
238 #define FAN_CHAN_TO_REG(val, reg) \
239 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
241 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
242 ((((val) / 500) & 0xf8) | ((reg) & 0x7))
243 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1 << ((reg) & 0x7)))
244 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
246 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
248 #define AUTO_TEMP_OFF_FROM_REG(reg) \
249 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
251 #define AUTO_TEMP_MAX_FROM_REG(reg) \
252 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
253 AUTO_TEMP_MIN_FROM_REG(reg))
255 static int AUTO_TEMP_MAX_TO_REG(int val
, int reg
, int pwm
)
258 int range
= val
- AUTO_TEMP_MIN_FROM_REG(reg
);
260 range
= ((val
- AUTO_TEMP_MIN_FROM_REG(reg
))*10)/(16 - pwm
);
261 ret
= ((reg
& 0xf8) |
264 range
< 40000 ? 2 : range
< 80000 ? 3 : 4));
268 /* FAN auto control */
269 #define GET_FAN_AUTO_BITFIELD(data, idx) \
270 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx % 2]
273 * The tables below contains the possible values for the auto fan
274 * control bitfields. the index in the table is the register value.
275 * MSb is the auto fan control enable bit, so the four first entries
276 * in the table disables auto fan control when both bitfields are zero.
278 static const auto_chan_table_t auto_channel_select_table_adm1031
= {
279 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
280 { 2 /* 0b010 */ , 4 /* 0b100 */ },
281 { 2 /* 0b010 */ , 2 /* 0b010 */ },
282 { 4 /* 0b100 */ , 4 /* 0b100 */ },
283 { 7 /* 0b111 */ , 7 /* 0b111 */ },
286 static const auto_chan_table_t auto_channel_select_table_adm1030
= {
287 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
288 { 2 /* 0b10 */ , 0 },
289 { 0xff /* invalid */ , 0 },
290 { 0xff /* invalid */ , 0 },
291 { 3 /* 0b11 */ , 0 },
295 * That function checks if a bitfield is valid and returns the other bitfield
296 * nearest match if no exact match where found.
299 get_fan_auto_nearest(struct adm1031_data
*data
, int chan
, u8 val
, u8 reg
)
302 int first_match
= -1, exact_match
= -1;
304 (*data
->chan_select_table
)[FAN_CHAN_FROM_REG(reg
)][chan
? 0 : 1];
309 for (i
= 0; i
< 8; i
++) {
310 if ((val
== (*data
->chan_select_table
)[i
][chan
]) &&
311 ((*data
->chan_select_table
)[i
][chan
? 0 : 1] ==
313 /* We found an exact match */
316 } else if (val
== (*data
->chan_select_table
)[i
][chan
] &&
319 * Save the first match in case of an exact match has
326 if (exact_match
>= 0)
328 else if (first_match
>= 0)
334 static ssize_t
show_fan_auto_channel(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", GET_FAN_AUTO_BITFIELD(data
, nr
));
343 set_fan_auto_channel(struct device
*dev
, struct device_attribute
*attr
,
344 const char *buf
, size_t count
)
346 struct adm1031_data
*data
= dev_get_drvdata(dev
);
347 struct i2c_client
*client
= data
->client
;
348 int nr
= to_sensor_dev_attr(attr
)->index
;
354 ret
= kstrtol(buf
, 10, &val
);
358 old_fan_mode
= data
->conf1
;
360 mutex_lock(&data
->update_lock
);
362 ret
= get_fan_auto_nearest(data
, nr
, val
, data
->conf1
);
364 mutex_unlock(&data
->update_lock
);
368 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
369 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) ^
370 (old_fan_mode
& ADM1031_CONF1_AUTO_MODE
)) {
371 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
373 * Switch to Auto Fan Mode
375 * Set PWM registers to 33% Both
377 data
->old_pwm
[0] = data
->pwm
[0];
378 data
->old_pwm
[1] = data
->pwm
[1];
379 adm1031_write_value(client
, ADM1031_REG_PWM
, 0x55);
381 /* Switch to Manual Mode */
382 data
->pwm
[0] = data
->old_pwm
[0];
383 data
->pwm
[1] = data
->old_pwm
[1];
384 /* Restore PWM registers */
385 adm1031_write_value(client
, ADM1031_REG_PWM
,
386 data
->pwm
[0] | (data
->pwm
[1] << 4));
389 data
->conf1
= FAN_CHAN_TO_REG(reg
, data
->conf1
);
390 adm1031_write_value(client
, ADM1031_REG_CONF1
, data
->conf1
);
391 mutex_unlock(&data
->update_lock
);
395 static SENSOR_DEVICE_ATTR(auto_fan1_channel
, S_IRUGO
| S_IWUSR
,
396 show_fan_auto_channel
, set_fan_auto_channel
, 0);
397 static SENSOR_DEVICE_ATTR(auto_fan2_channel
, S_IRUGO
| S_IWUSR
,
398 show_fan_auto_channel
, set_fan_auto_channel
, 1);
401 static ssize_t
show_auto_temp_off(struct device
*dev
,
402 struct device_attribute
*attr
, char *buf
)
404 int nr
= to_sensor_dev_attr(attr
)->index
;
405 struct adm1031_data
*data
= adm1031_update_device(dev
);
406 return sprintf(buf
, "%d\n",
407 AUTO_TEMP_OFF_FROM_REG(data
->auto_temp
[nr
]));
409 static ssize_t
show_auto_temp_min(struct device
*dev
,
410 struct device_attribute
*attr
, char *buf
)
412 int nr
= to_sensor_dev_attr(attr
)->index
;
413 struct adm1031_data
*data
= adm1031_update_device(dev
);
414 return sprintf(buf
, "%d\n",
415 AUTO_TEMP_MIN_FROM_REG(data
->auto_temp
[nr
]));
418 set_auto_temp_min(struct device
*dev
, struct device_attribute
*attr
,
419 const char *buf
, size_t count
)
421 struct adm1031_data
*data
= dev_get_drvdata(dev
);
422 struct i2c_client
*client
= data
->client
;
423 int nr
= to_sensor_dev_attr(attr
)->index
;
427 ret
= kstrtol(buf
, 10, &val
);
431 val
= clamp_val(val
, 0, 127000);
432 mutex_lock(&data
->update_lock
);
433 data
->auto_temp
[nr
] = AUTO_TEMP_MIN_TO_REG(val
, data
->auto_temp
[nr
]);
434 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
435 data
->auto_temp
[nr
]);
436 mutex_unlock(&data
->update_lock
);
439 static ssize_t
show_auto_temp_max(struct device
*dev
,
440 struct device_attribute
*attr
, char *buf
)
442 int nr
= to_sensor_dev_attr(attr
)->index
;
443 struct adm1031_data
*data
= adm1031_update_device(dev
);
444 return sprintf(buf
, "%d\n",
445 AUTO_TEMP_MAX_FROM_REG(data
->auto_temp
[nr
]));
448 set_auto_temp_max(struct device
*dev
, struct device_attribute
*attr
,
449 const char *buf
, size_t count
)
451 struct adm1031_data
*data
= dev_get_drvdata(dev
);
452 struct i2c_client
*client
= data
->client
;
453 int nr
= to_sensor_dev_attr(attr
)->index
;
457 ret
= kstrtol(buf
, 10, &val
);
461 val
= clamp_val(val
, 0, 127000);
462 mutex_lock(&data
->update_lock
);
463 data
->temp_max
[nr
] = AUTO_TEMP_MAX_TO_REG(val
, data
->auto_temp
[nr
],
465 adm1031_write_value(client
, ADM1031_REG_AUTO_TEMP(nr
),
467 mutex_unlock(&data
->update_lock
);
471 #define auto_temp_reg(offset) \
472 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
473 show_auto_temp_off, NULL, offset - 1); \
474 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
475 show_auto_temp_min, set_auto_temp_min, offset - 1); \
476 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
477 show_auto_temp_max, set_auto_temp_max, offset - 1)
484 static ssize_t
show_pwm(struct device
*dev
,
485 struct device_attribute
*attr
, char *buf
)
487 int nr
= to_sensor_dev_attr(attr
)->index
;
488 struct adm1031_data
*data
= adm1031_update_device(dev
);
489 return sprintf(buf
, "%d\n", PWM_FROM_REG(data
->pwm
[nr
]));
491 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
492 const char *buf
, size_t count
)
494 struct adm1031_data
*data
= dev_get_drvdata(dev
);
495 struct i2c_client
*client
= data
->client
;
496 int nr
= to_sensor_dev_attr(attr
)->index
;
500 ret
= kstrtol(buf
, 10, &val
);
504 mutex_lock(&data
->update_lock
);
505 if ((data
->conf1
& ADM1031_CONF1_AUTO_MODE
) &&
506 (((val
>>4) & 0xf) != 5)) {
507 /* In automatic mode, the only PWM accepted is 33% */
508 mutex_unlock(&data
->update_lock
);
511 data
->pwm
[nr
] = PWM_TO_REG(val
);
512 reg
= adm1031_read_value(client
, ADM1031_REG_PWM
);
513 adm1031_write_value(client
, ADM1031_REG_PWM
,
514 nr
? ((data
->pwm
[nr
] << 4) & 0xf0) | (reg
& 0xf)
515 : (data
->pwm
[nr
] & 0xf) | (reg
& 0xf0));
516 mutex_unlock(&data
->update_lock
);
520 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 0);
521 static SENSOR_DEVICE_ATTR(pwm2
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, 1);
522 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm
, S_IRUGO
| S_IWUSR
,
523 show_pwm
, set_pwm
, 0);
524 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm
, S_IRUGO
| S_IWUSR
,
525 show_pwm
, set_pwm
, 1);
530 * That function checks the cases where the fan reading is not
531 * relevant. It is used to provide 0 as fan reading when the fan is
532 * not supposed to run
534 static int trust_fan_readings(struct adm1031_data
*data
, int chan
)
538 if (data
->conf1
& ADM1031_CONF1_AUTO_MODE
) {
539 switch (data
->conf1
& 0x60) {
542 * remote temp1 controls fan1,
543 * remote temp2 controls fan2
545 res
= data
->temp
[chan
+1] >=
546 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[chan
+1]);
548 case 0x20: /* remote temp1 controls both fans */
551 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1]);
553 case 0x40: /* remote temp2 controls both fans */
556 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]);
558 case 0x60: /* max controls both fans */
561 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[0])
563 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[1])
564 || (data
->chip_type
== adm1031
566 AUTO_TEMP_MIN_FROM_REG_DEG(data
->auto_temp
[2]));
570 res
= data
->pwm
[chan
] > 0;
576 static ssize_t
show_fan(struct device
*dev
,
577 struct device_attribute
*attr
, char *buf
)
579 int nr
= to_sensor_dev_attr(attr
)->index
;
580 struct adm1031_data
*data
= adm1031_update_device(dev
);
583 value
= trust_fan_readings(data
, nr
) ? FAN_FROM_REG(data
->fan
[nr
],
584 FAN_DIV_FROM_REG(data
->fan_div
[nr
])) : 0;
585 return sprintf(buf
, "%d\n", value
);
588 static ssize_t
show_fan_div(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", FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
595 static ssize_t
show_fan_min(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",
601 FAN_FROM_REG(data
->fan_min
[nr
],
602 FAN_DIV_FROM_REG(data
->fan_div
[nr
])));
604 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
605 const char *buf
, size_t count
)
607 struct adm1031_data
*data
= dev_get_drvdata(dev
);
608 struct i2c_client
*client
= data
->client
;
609 int nr
= to_sensor_dev_attr(attr
)->index
;
613 ret
= kstrtol(buf
, 10, &val
);
617 mutex_lock(&data
->update_lock
);
620 FAN_TO_REG(val
, FAN_DIV_FROM_REG(data
->fan_div
[nr
]));
622 data
->fan_min
[nr
] = 0xff;
624 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
), data
->fan_min
[nr
]);
625 mutex_unlock(&data
->update_lock
);
628 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
629 const char *buf
, size_t count
)
631 struct adm1031_data
*data
= dev_get_drvdata(dev
);
632 struct i2c_client
*client
= data
->client
;
633 int nr
= to_sensor_dev_attr(attr
)->index
;
640 ret
= kstrtol(buf
, 10, &val
);
644 tmp
= val
== 8 ? 0xc0 :
652 mutex_lock(&data
->update_lock
);
653 /* Get fresh readings */
654 data
->fan_div
[nr
] = adm1031_read_value(client
,
655 ADM1031_REG_FAN_DIV(nr
));
656 data
->fan_min
[nr
] = adm1031_read_value(client
,
657 ADM1031_REG_FAN_MIN(nr
));
659 /* Write the new clock divider and fan min */
660 old_div
= FAN_DIV_FROM_REG(data
->fan_div
[nr
]);
661 data
->fan_div
[nr
] = tmp
| (0x3f & data
->fan_div
[nr
]);
662 new_min
= data
->fan_min
[nr
] * old_div
/ val
;
663 data
->fan_min
[nr
] = new_min
> 0xff ? 0xff : new_min
;
665 adm1031_write_value(client
, ADM1031_REG_FAN_DIV(nr
),
667 adm1031_write_value(client
, ADM1031_REG_FAN_MIN(nr
),
670 /* Invalidate the cache: fan speed is no longer valid */
672 mutex_unlock(&data
->update_lock
);
676 #define fan_offset(offset) \
677 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
678 show_fan, NULL, offset - 1); \
679 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
680 show_fan_min, set_fan_min, offset - 1); \
681 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
682 show_fan_div, set_fan_div, offset - 1)
689 static ssize_t
show_temp(struct device
*dev
,
690 struct device_attribute
*attr
, char *buf
)
692 int nr
= to_sensor_dev_attr(attr
)->index
;
693 struct adm1031_data
*data
= adm1031_update_device(dev
);
696 ((data
->ext_temp
[nr
] >> 6) & 0x3) * 2 :
697 (((data
->ext_temp
[nr
] >> ((nr
- 1) * 3)) & 7));
698 return sprintf(buf
, "%d\n", TEMP_FROM_REG_EXT(data
->temp
[nr
], ext
));
700 static ssize_t
show_temp_offset(struct device
*dev
,
701 struct device_attribute
*attr
, char *buf
)
703 int nr
= to_sensor_dev_attr(attr
)->index
;
704 struct adm1031_data
*data
= adm1031_update_device(dev
);
705 return sprintf(buf
, "%d\n",
706 TEMP_OFFSET_FROM_REG(data
->temp_offset
[nr
]));
708 static ssize_t
show_temp_min(struct device
*dev
,
709 struct device_attribute
*attr
, char *buf
)
711 int nr
= to_sensor_dev_attr(attr
)->index
;
712 struct adm1031_data
*data
= adm1031_update_device(dev
);
713 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_min
[nr
]));
715 static ssize_t
show_temp_max(struct device
*dev
,
716 struct device_attribute
*attr
, char *buf
)
718 int nr
= to_sensor_dev_attr(attr
)->index
;
719 struct adm1031_data
*data
= adm1031_update_device(dev
);
720 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_max
[nr
]));
722 static ssize_t
show_temp_crit(struct device
*dev
,
723 struct device_attribute
*attr
, char *buf
)
725 int nr
= to_sensor_dev_attr(attr
)->index
;
726 struct adm1031_data
*data
= adm1031_update_device(dev
);
727 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_crit
[nr
]));
729 static ssize_t
set_temp_offset(struct device
*dev
,
730 struct device_attribute
*attr
, const char *buf
,
733 struct adm1031_data
*data
= dev_get_drvdata(dev
);
734 struct i2c_client
*client
= data
->client
;
735 int nr
= to_sensor_dev_attr(attr
)->index
;
739 ret
= kstrtol(buf
, 10, &val
);
743 val
= clamp_val(val
, -15000, 15000);
744 mutex_lock(&data
->update_lock
);
745 data
->temp_offset
[nr
] = TEMP_OFFSET_TO_REG(val
);
746 adm1031_write_value(client
, ADM1031_REG_TEMP_OFFSET(nr
),
747 data
->temp_offset
[nr
]);
748 mutex_unlock(&data
->update_lock
);
751 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
752 const char *buf
, size_t count
)
754 struct adm1031_data
*data
= dev_get_drvdata(dev
);
755 struct i2c_client
*client
= data
->client
;
756 int nr
= to_sensor_dev_attr(attr
)->index
;
760 ret
= kstrtol(buf
, 10, &val
);
764 val
= clamp_val(val
, -55000, 127000);
765 mutex_lock(&data
->update_lock
);
766 data
->temp_min
[nr
] = TEMP_TO_REG(val
);
767 adm1031_write_value(client
, ADM1031_REG_TEMP_MIN(nr
),
769 mutex_unlock(&data
->update_lock
);
772 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
773 const char *buf
, size_t count
)
775 struct adm1031_data
*data
= dev_get_drvdata(dev
);
776 struct i2c_client
*client
= data
->client
;
777 int nr
= to_sensor_dev_attr(attr
)->index
;
781 ret
= kstrtol(buf
, 10, &val
);
785 val
= clamp_val(val
, -55000, 127000);
786 mutex_lock(&data
->update_lock
);
787 data
->temp_max
[nr
] = TEMP_TO_REG(val
);
788 adm1031_write_value(client
, ADM1031_REG_TEMP_MAX(nr
),
790 mutex_unlock(&data
->update_lock
);
793 static ssize_t
set_temp_crit(struct device
*dev
, struct device_attribute
*attr
,
794 const char *buf
, size_t count
)
796 struct adm1031_data
*data
= dev_get_drvdata(dev
);
797 struct i2c_client
*client
= data
->client
;
798 int nr
= to_sensor_dev_attr(attr
)->index
;
802 ret
= kstrtol(buf
, 10, &val
);
806 val
= clamp_val(val
, -55000, 127000);
807 mutex_lock(&data
->update_lock
);
808 data
->temp_crit
[nr
] = TEMP_TO_REG(val
);
809 adm1031_write_value(client
, ADM1031_REG_TEMP_CRIT(nr
),
810 data
->temp_crit
[nr
]);
811 mutex_unlock(&data
->update_lock
);
815 #define temp_reg(offset) \
816 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
817 show_temp, NULL, offset - 1); \
818 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
819 show_temp_offset, set_temp_offset, offset - 1); \
820 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
821 show_temp_min, set_temp_min, offset - 1); \
822 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
823 show_temp_max, set_temp_max, offset - 1); \
824 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
825 show_temp_crit, set_temp_crit, offset - 1)
832 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
,
835 struct adm1031_data
*data
= adm1031_update_device(dev
);
836 return sprintf(buf
, "%d\n", data
->alarm
);
839 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
841 static ssize_t
show_alarm(struct device
*dev
,
842 struct device_attribute
*attr
, char *buf
)
844 int bitnr
= to_sensor_dev_attr(attr
)->index
;
845 struct adm1031_data
*data
= adm1031_update_device(dev
);
846 return sprintf(buf
, "%d\n", (data
->alarm
>> bitnr
) & 1);
849 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
850 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, show_alarm
, NULL
, 1);
851 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
852 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
853 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
854 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 5);
855 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
856 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 7);
857 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
858 static SENSOR_DEVICE_ATTR(fan2_fault
, S_IRUGO
, show_alarm
, NULL
, 9);
859 static SENSOR_DEVICE_ATTR(temp3_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
860 static SENSOR_DEVICE_ATTR(temp3_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
861 static SENSOR_DEVICE_ATTR(temp3_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
862 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 13);
863 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
865 /* Update Interval */
866 static const unsigned int update_intervals
[] = {
867 16000, 8000, 4000, 2000, 1000, 500, 250, 125,
870 static ssize_t
show_update_interval(struct device
*dev
,
871 struct device_attribute
*attr
, char *buf
)
873 struct adm1031_data
*data
= dev_get_drvdata(dev
);
875 return sprintf(buf
, "%u\n", data
->update_interval
);
878 static ssize_t
set_update_interval(struct device
*dev
,
879 struct device_attribute
*attr
,
880 const char *buf
, size_t count
)
882 struct adm1031_data
*data
= dev_get_drvdata(dev
);
883 struct i2c_client
*client
= data
->client
;
888 err
= kstrtoul(buf
, 10, &val
);
893 * Find the nearest update interval from the table.
894 * Use it to determine the matching update rate.
896 for (i
= 0; i
< ARRAY_SIZE(update_intervals
) - 1; i
++) {
897 if (val
>= update_intervals
[i
])
900 /* if not found, we point to the last entry (lowest update interval) */
902 /* set the new update rate while preserving other settings */
903 reg
= adm1031_read_value(client
, ADM1031_REG_FAN_FILTER
);
904 reg
&= ~ADM1031_UPDATE_RATE_MASK
;
905 reg
|= i
<< ADM1031_UPDATE_RATE_SHIFT
;
906 adm1031_write_value(client
, ADM1031_REG_FAN_FILTER
, reg
);
908 mutex_lock(&data
->update_lock
);
909 data
->update_interval
= update_intervals
[i
];
910 mutex_unlock(&data
->update_lock
);
915 static DEVICE_ATTR(update_interval
, S_IRUGO
| S_IWUSR
, show_update_interval
,
916 set_update_interval
);
918 static struct attribute
*adm1031_attributes
[] = {
919 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
920 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
921 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
922 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
923 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
924 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
925 &sensor_dev_attr_auto_fan1_channel
.dev_attr
.attr
,
926 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
927 &sensor_dev_attr_temp1_offset
.dev_attr
.attr
,
928 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
929 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
930 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
931 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
932 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
933 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
934 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
935 &sensor_dev_attr_temp2_offset
.dev_attr
.attr
,
936 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
937 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
938 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
939 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
940 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
941 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
942 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
944 &sensor_dev_attr_auto_temp1_off
.dev_attr
.attr
,
945 &sensor_dev_attr_auto_temp1_min
.dev_attr
.attr
,
946 &sensor_dev_attr_auto_temp1_max
.dev_attr
.attr
,
948 &sensor_dev_attr_auto_temp2_off
.dev_attr
.attr
,
949 &sensor_dev_attr_auto_temp2_min
.dev_attr
.attr
,
950 &sensor_dev_attr_auto_temp2_max
.dev_attr
.attr
,
952 &sensor_dev_attr_auto_fan1_min_pwm
.dev_attr
.attr
,
954 &dev_attr_update_interval
.attr
,
955 &dev_attr_alarms
.attr
,
960 static const struct attribute_group adm1031_group
= {
961 .attrs
= adm1031_attributes
,
964 static struct attribute
*adm1031_attributes_opt
[] = {
965 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
966 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
967 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
968 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
969 &sensor_dev_attr_fan2_fault
.dev_attr
.attr
,
970 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
971 &sensor_dev_attr_auto_fan2_channel
.dev_attr
.attr
,
972 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
973 &sensor_dev_attr_temp3_offset
.dev_attr
.attr
,
974 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
975 &sensor_dev_attr_temp3_min_alarm
.dev_attr
.attr
,
976 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
977 &sensor_dev_attr_temp3_max_alarm
.dev_attr
.attr
,
978 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
979 &sensor_dev_attr_temp3_crit_alarm
.dev_attr
.attr
,
980 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
981 &sensor_dev_attr_auto_temp3_off
.dev_attr
.attr
,
982 &sensor_dev_attr_auto_temp3_min
.dev_attr
.attr
,
983 &sensor_dev_attr_auto_temp3_max
.dev_attr
.attr
,
984 &sensor_dev_attr_auto_fan2_min_pwm
.dev_attr
.attr
,
988 static const struct attribute_group adm1031_group_opt
= {
989 .attrs
= adm1031_attributes_opt
,
992 /* Return 0 if detection is successful, -ENODEV otherwise */
993 static int adm1031_detect(struct i2c_client
*client
,
994 struct i2c_board_info
*info
)
996 struct i2c_adapter
*adapter
= client
->adapter
;
1000 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1003 id
= i2c_smbus_read_byte_data(client
, 0x3d);
1004 co
= i2c_smbus_read_byte_data(client
, 0x3e);
1006 if (!((id
== 0x31 || id
== 0x30) && co
== 0x41))
1008 name
= (id
== 0x30) ? "adm1030" : "adm1031";
1010 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
1015 static void adm1031_init_client(struct i2c_client
*client
)
1017 unsigned int read_val
;
1020 struct adm1031_data
*data
= i2c_get_clientdata(client
);
1022 mask
= (ADM1031_CONF2_PWM1_ENABLE
| ADM1031_CONF2_TACH1_ENABLE
);
1023 if (data
->chip_type
== adm1031
) {
1024 mask
|= (ADM1031_CONF2_PWM2_ENABLE
|
1025 ADM1031_CONF2_TACH2_ENABLE
);
1027 /* Initialize the ADM1031 chip (enables fan speed reading ) */
1028 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF2
);
1029 if ((read_val
| mask
) != read_val
)
1030 adm1031_write_value(client
, ADM1031_REG_CONF2
, read_val
| mask
);
1032 read_val
= adm1031_read_value(client
, ADM1031_REG_CONF1
);
1033 if ((read_val
| ADM1031_CONF1_MONITOR_ENABLE
) != read_val
) {
1034 adm1031_write_value(client
, ADM1031_REG_CONF1
,
1035 read_val
| ADM1031_CONF1_MONITOR_ENABLE
);
1038 /* Read the chip's update rate */
1039 mask
= ADM1031_UPDATE_RATE_MASK
;
1040 read_val
= adm1031_read_value(client
, ADM1031_REG_FAN_FILTER
);
1041 i
= (read_val
& mask
) >> ADM1031_UPDATE_RATE_SHIFT
;
1042 /* Save it as update interval */
1043 data
->update_interval
= update_intervals
[i
];
1046 static int adm1031_probe(struct i2c_client
*client
,
1047 const struct i2c_device_id
*id
)
1049 struct device
*dev
= &client
->dev
;
1050 struct device
*hwmon_dev
;
1051 struct adm1031_data
*data
;
1053 data
= devm_kzalloc(dev
, sizeof(struct adm1031_data
), GFP_KERNEL
);
1057 i2c_set_clientdata(client
, data
);
1058 data
->client
= client
;
1059 data
->chip_type
= id
->driver_data
;
1060 mutex_init(&data
->update_lock
);
1062 if (data
->chip_type
== adm1030
)
1063 data
->chan_select_table
= &auto_channel_select_table_adm1030
;
1065 data
->chan_select_table
= &auto_channel_select_table_adm1031
;
1067 /* Initialize the ADM1031 chip */
1068 adm1031_init_client(client
);
1071 data
->groups
[0] = &adm1031_group
;
1072 if (data
->chip_type
== adm1031
)
1073 data
->groups
[1] = &adm1031_group_opt
;
1075 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
1076 data
, data
->groups
);
1077 return PTR_ERR_OR_ZERO(hwmon_dev
);
1080 static const struct i2c_device_id adm1031_id
[] = {
1081 { "adm1030", adm1030
},
1082 { "adm1031", adm1031
},
1085 MODULE_DEVICE_TABLE(i2c
, adm1031_id
);
1087 static struct i2c_driver adm1031_driver
= {
1088 .class = I2C_CLASS_HWMON
,
1092 .probe
= adm1031_probe
,
1093 .id_table
= adm1031_id
,
1094 .detect
= adm1031_detect
,
1095 .address_list
= normal_i2c
,
1098 module_i2c_driver(adm1031_driver
);
1100 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1101 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1102 MODULE_LICENSE("GPL");