Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / hwmon / adm1031.c
blob1644b92e7cc47ae8f8507d777cd1ecbe64612484
1 /*
2 adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Based on lm75.c and lm85.c
5 Supports adm1030 / adm1031
6 Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7 Reworked by Jean Delvare <khali@linux-fr.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
34 /* Following macros takes channel parameter starting from 0 to 2 */
35 #define ADM1031_REG_FAN_SPEED(nr) (0x08 + (nr))
36 #define ADM1031_REG_FAN_DIV(nr) (0x20 + (nr))
37 #define ADM1031_REG_PWM (0x22)
38 #define ADM1031_REG_FAN_MIN(nr) (0x10 + (nr))
40 #define ADM1031_REG_TEMP_OFFSET(nr) (0x0d + (nr))
41 #define ADM1031_REG_TEMP_MAX(nr) (0x14 + 4 * (nr))
42 #define ADM1031_REG_TEMP_MIN(nr) (0x15 + 4 * (nr))
43 #define ADM1031_REG_TEMP_CRIT(nr) (0x16 + 4 * (nr))
45 #define ADM1031_REG_TEMP(nr) (0x0a + (nr))
46 #define ADM1031_REG_AUTO_TEMP(nr) (0x24 + (nr))
48 #define ADM1031_REG_STATUS(nr) (0x2 + (nr))
50 #define ADM1031_REG_CONF1 0x00
51 #define ADM1031_REG_CONF2 0x01
52 #define ADM1031_REG_EXT_TEMP 0x06
54 #define ADM1031_CONF1_MONITOR_ENABLE 0x01 /* Monitoring enable */
55 #define ADM1031_CONF1_PWM_INVERT 0x08 /* PWM Invert */
56 #define ADM1031_CONF1_AUTO_MODE 0x80 /* Auto FAN */
58 #define ADM1031_CONF2_PWM1_ENABLE 0x01
59 #define ADM1031_CONF2_PWM2_ENABLE 0x02
60 #define ADM1031_CONF2_TACH1_ENABLE 0x04
61 #define ADM1031_CONF2_TACH2_ENABLE 0x08
62 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
64 /* Addresses to scan */
65 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
67 enum chips { adm1030, adm1031 };
69 typedef u8 auto_chan_table_t[8][2];
71 /* Each client has this additional data */
72 struct adm1031_data {
73 struct device *hwmon_dev;
74 struct mutex update_lock;
75 int chip_type;
76 char valid; /* !=0 if following fields are valid */
77 unsigned long last_updated; /* In jiffies */
78 /* The chan_select_table contains the possible configurations for
79 * auto fan control.
81 const auto_chan_table_t *chan_select_table;
82 u16 alarm;
83 u8 conf1;
84 u8 conf2;
85 u8 fan[2];
86 u8 fan_div[2];
87 u8 fan_min[2];
88 u8 pwm[2];
89 u8 old_pwm[2];
90 s8 temp[3];
91 u8 ext_temp[3];
92 u8 auto_temp[3];
93 u8 auto_temp_min[3];
94 u8 auto_temp_off[3];
95 u8 auto_temp_max[3];
96 s8 temp_offset[3];
97 s8 temp_min[3];
98 s8 temp_max[3];
99 s8 temp_crit[3];
102 static int adm1031_probe(struct i2c_client *client,
103 const struct i2c_device_id *id);
104 static int adm1031_detect(struct i2c_client *client,
105 struct i2c_board_info *info);
106 static void adm1031_init_client(struct i2c_client *client);
107 static int adm1031_remove(struct i2c_client *client);
108 static struct adm1031_data *adm1031_update_device(struct device *dev);
110 static const struct i2c_device_id adm1031_id[] = {
111 { "adm1030", adm1030 },
112 { "adm1031", adm1031 },
115 MODULE_DEVICE_TABLE(i2c, adm1031_id);
117 /* This is the driver that will be inserted */
118 static struct i2c_driver adm1031_driver = {
119 .class = I2C_CLASS_HWMON,
120 .driver = {
121 .name = "adm1031",
123 .probe = adm1031_probe,
124 .remove = adm1031_remove,
125 .id_table = adm1031_id,
126 .detect = adm1031_detect,
127 .address_list = normal_i2c,
130 static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
132 return i2c_smbus_read_byte_data(client, reg);
135 static inline int
136 adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
138 return i2c_smbus_write_byte_data(client, reg, value);
142 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
143 ((val + 500) / 1000)))
145 #define TEMP_FROM_REG(val) ((val) * 1000)
147 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
149 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
150 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
151 (val) | 0x70 : (val))
153 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
155 static int FAN_TO_REG(int reg, int div)
157 int tmp;
158 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
159 return tmp > 255 ? 255 : tmp;
162 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
164 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
165 #define PWM_FROM_REG(val) ((val) << 4)
167 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
168 #define FAN_CHAN_TO_REG(val, reg) \
169 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
171 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
172 ((((val)/500) & 0xf8)|((reg) & 0x7))
173 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
174 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
176 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
178 #define AUTO_TEMP_OFF_FROM_REG(reg) \
179 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
181 #define AUTO_TEMP_MAX_FROM_REG(reg) \
182 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
183 AUTO_TEMP_MIN_FROM_REG(reg))
185 static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
187 int ret;
188 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
190 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
191 ret = ((reg & 0xf8) |
192 (range < 10000 ? 0 :
193 range < 20000 ? 1 :
194 range < 40000 ? 2 : range < 80000 ? 3 : 4));
195 return ret;
198 /* FAN auto control */
199 #define GET_FAN_AUTO_BITFIELD(data, idx) \
200 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
202 /* The tables below contains the possible values for the auto fan
203 * control bitfields. the index in the table is the register value.
204 * MSb is the auto fan control enable bit, so the four first entries
205 * in the table disables auto fan control when both bitfields are zero.
207 static const auto_chan_table_t auto_channel_select_table_adm1031 = {
208 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
209 { 2 /* 0b010 */ , 4 /* 0b100 */ },
210 { 2 /* 0b010 */ , 2 /* 0b010 */ },
211 { 4 /* 0b100 */ , 4 /* 0b100 */ },
212 { 7 /* 0b111 */ , 7 /* 0b111 */ },
215 static const auto_chan_table_t auto_channel_select_table_adm1030 = {
216 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
217 { 2 /* 0b10 */ , 0 },
218 { 0xff /* invalid */ , 0 },
219 { 0xff /* invalid */ , 0 },
220 { 3 /* 0b11 */ , 0 },
223 /* That function checks if a bitfield is valid and returns the other bitfield
224 * nearest match if no exact match where found.
226 static int
227 get_fan_auto_nearest(struct adm1031_data *data,
228 int chan, u8 val, u8 reg, u8 * new_reg)
230 int i;
231 int first_match = -1, exact_match = -1;
232 u8 other_reg_val =
233 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
235 if (val == 0) {
236 *new_reg = 0;
237 return 0;
240 for (i = 0; i < 8; i++) {
241 if ((val == (*data->chan_select_table)[i][chan]) &&
242 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
243 other_reg_val)) {
244 /* We found an exact match */
245 exact_match = i;
246 break;
247 } else if (val == (*data->chan_select_table)[i][chan] &&
248 first_match == -1) {
249 /* Save the first match in case of an exact match has
250 * not been found
252 first_match = i;
256 if (exact_match >= 0) {
257 *new_reg = exact_match;
258 } else if (first_match >= 0) {
259 *new_reg = first_match;
260 } else {
261 return -EINVAL;
263 return 0;
266 static ssize_t show_fan_auto_channel(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 int nr = to_sensor_dev_attr(attr)->index;
270 struct adm1031_data *data = adm1031_update_device(dev);
271 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
274 static ssize_t
275 set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
278 struct i2c_client *client = to_i2c_client(dev);
279 struct adm1031_data *data = i2c_get_clientdata(client);
280 int nr = to_sensor_dev_attr(attr)->index;
281 int val = simple_strtol(buf, NULL, 10);
282 u8 reg;
283 int ret;
284 u8 old_fan_mode;
286 old_fan_mode = data->conf1;
288 mutex_lock(&data->update_lock);
290 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
291 mutex_unlock(&data->update_lock);
292 return ret;
294 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
295 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
296 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
297 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
298 /* Switch to Auto Fan Mode
299 * Save PWM registers
300 * Set PWM registers to 33% Both */
301 data->old_pwm[0] = data->pwm[0];
302 data->old_pwm[1] = data->pwm[1];
303 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
304 } else {
305 /* Switch to Manual Mode */
306 data->pwm[0] = data->old_pwm[0];
307 data->pwm[1] = data->old_pwm[1];
308 /* Restore PWM registers */
309 adm1031_write_value(client, ADM1031_REG_PWM,
310 data->pwm[0] | (data->pwm[1] << 4));
313 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
314 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
315 mutex_unlock(&data->update_lock);
316 return count;
319 static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
320 show_fan_auto_channel, set_fan_auto_channel, 0);
321 static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
322 show_fan_auto_channel, set_fan_auto_channel, 1);
324 /* Auto Temps */
325 static ssize_t show_auto_temp_off(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 int nr = to_sensor_dev_attr(attr)->index;
329 struct adm1031_data *data = adm1031_update_device(dev);
330 return sprintf(buf, "%d\n",
331 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
333 static ssize_t show_auto_temp_min(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 int nr = to_sensor_dev_attr(attr)->index;
337 struct adm1031_data *data = adm1031_update_device(dev);
338 return sprintf(buf, "%d\n",
339 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
341 static ssize_t
342 set_auto_temp_min(struct device *dev, struct device_attribute *attr,
343 const char *buf, size_t count)
345 struct i2c_client *client = to_i2c_client(dev);
346 struct adm1031_data *data = i2c_get_clientdata(client);
347 int nr = to_sensor_dev_attr(attr)->index;
348 int val = simple_strtol(buf, NULL, 10);
350 mutex_lock(&data->update_lock);
351 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
352 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
353 data->auto_temp[nr]);
354 mutex_unlock(&data->update_lock);
355 return count;
357 static ssize_t show_auto_temp_max(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 int nr = to_sensor_dev_attr(attr)->index;
361 struct adm1031_data *data = adm1031_update_device(dev);
362 return sprintf(buf, "%d\n",
363 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
365 static ssize_t
366 set_auto_temp_max(struct device *dev, struct device_attribute *attr,
367 const char *buf, size_t count)
369 struct i2c_client *client = to_i2c_client(dev);
370 struct adm1031_data *data = i2c_get_clientdata(client);
371 int nr = to_sensor_dev_attr(attr)->index;
372 int val = simple_strtol(buf, NULL, 10);
374 mutex_lock(&data->update_lock);
375 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
376 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
377 data->temp_max[nr]);
378 mutex_unlock(&data->update_lock);
379 return count;
382 #define auto_temp_reg(offset) \
383 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
384 show_auto_temp_off, NULL, offset - 1); \
385 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
386 show_auto_temp_min, set_auto_temp_min, offset - 1); \
387 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
388 show_auto_temp_max, set_auto_temp_max, offset - 1)
390 auto_temp_reg(1);
391 auto_temp_reg(2);
392 auto_temp_reg(3);
394 /* pwm */
395 static ssize_t show_pwm(struct device *dev,
396 struct device_attribute *attr, char *buf)
398 int nr = to_sensor_dev_attr(attr)->index;
399 struct adm1031_data *data = adm1031_update_device(dev);
400 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
402 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
405 struct i2c_client *client = to_i2c_client(dev);
406 struct adm1031_data *data = i2c_get_clientdata(client);
407 int nr = to_sensor_dev_attr(attr)->index;
408 int val = simple_strtol(buf, NULL, 10);
409 int reg;
411 mutex_lock(&data->update_lock);
412 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
413 (((val>>4) & 0xf) != 5)) {
414 /* In automatic mode, the only PWM accepted is 33% */
415 mutex_unlock(&data->update_lock);
416 return -EINVAL;
418 data->pwm[nr] = PWM_TO_REG(val);
419 reg = adm1031_read_value(client, ADM1031_REG_PWM);
420 adm1031_write_value(client, ADM1031_REG_PWM,
421 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
422 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
423 mutex_unlock(&data->update_lock);
424 return count;
427 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
428 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
429 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
430 show_pwm, set_pwm, 0);
431 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
432 show_pwm, set_pwm, 1);
434 /* Fans */
437 * That function checks the cases where the fan reading is not
438 * relevant. It is used to provide 0 as fan reading when the fan is
439 * not supposed to run
441 static int trust_fan_readings(struct adm1031_data *data, int chan)
443 int res = 0;
445 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
446 switch (data->conf1 & 0x60) {
447 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
448 res = data->temp[chan+1] >=
449 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
450 break;
451 case 0x20: /* remote temp1 controls both fans */
452 res =
453 data->temp[1] >=
454 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
455 break;
456 case 0x40: /* remote temp2 controls both fans */
457 res =
458 data->temp[2] >=
459 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
460 break;
461 case 0x60: /* max controls both fans */
462 res =
463 data->temp[0] >=
464 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
465 || data->temp[1] >=
466 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
467 || (data->chip_type == adm1031
468 && data->temp[2] >=
469 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
470 break;
472 } else {
473 res = data->pwm[chan] > 0;
475 return res;
479 static ssize_t show_fan(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 int nr = to_sensor_dev_attr(attr)->index;
483 struct adm1031_data *data = adm1031_update_device(dev);
484 int value;
486 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
487 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
488 return sprintf(buf, "%d\n", value);
491 static ssize_t show_fan_div(struct device *dev,
492 struct device_attribute *attr, char *buf)
494 int nr = to_sensor_dev_attr(attr)->index;
495 struct adm1031_data *data = adm1031_update_device(dev);
496 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
498 static ssize_t show_fan_min(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 int nr = to_sensor_dev_attr(attr)->index;
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])));
507 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
508 const char *buf, size_t count)
510 struct i2c_client *client = to_i2c_client(dev);
511 struct adm1031_data *data = i2c_get_clientdata(client);
512 int nr = to_sensor_dev_attr(attr)->index;
513 int val = simple_strtol(buf, NULL, 10);
515 mutex_lock(&data->update_lock);
516 if (val) {
517 data->fan_min[nr] =
518 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
519 } else {
520 data->fan_min[nr] = 0xff;
522 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
523 mutex_unlock(&data->update_lock);
524 return count;
526 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
529 struct i2c_client *client = to_i2c_client(dev);
530 struct adm1031_data *data = i2c_get_clientdata(client);
531 int nr = to_sensor_dev_attr(attr)->index;
532 int val = simple_strtol(buf, NULL, 10);
533 u8 tmp;
534 int old_div;
535 int new_min;
537 tmp = val == 8 ? 0xc0 :
538 val == 4 ? 0x80 :
539 val == 2 ? 0x40 :
540 val == 1 ? 0x00 :
541 0xff;
542 if (tmp == 0xff)
543 return -EINVAL;
545 mutex_lock(&data->update_lock);
546 /* Get fresh readings */
547 data->fan_div[nr] = adm1031_read_value(client,
548 ADM1031_REG_FAN_DIV(nr));
549 data->fan_min[nr] = adm1031_read_value(client,
550 ADM1031_REG_FAN_MIN(nr));
552 /* Write the new clock divider and fan min */
553 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
554 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
555 new_min = data->fan_min[nr] * old_div / val;
556 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
558 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
559 data->fan_div[nr]);
560 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
561 data->fan_min[nr]);
563 /* Invalidate the cache: fan speed is no longer valid */
564 data->valid = 0;
565 mutex_unlock(&data->update_lock);
566 return count;
569 #define fan_offset(offset) \
570 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
571 show_fan, NULL, offset - 1); \
572 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
573 show_fan_min, set_fan_min, offset - 1); \
574 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
575 show_fan_div, set_fan_div, offset - 1)
577 fan_offset(1);
578 fan_offset(2);
581 /* Temps */
582 static ssize_t show_temp(struct device *dev,
583 struct device_attribute *attr, char *buf)
585 int nr = to_sensor_dev_attr(attr)->index;
586 struct adm1031_data *data = adm1031_update_device(dev);
587 int ext;
588 ext = nr == 0 ?
589 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
590 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
591 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
593 static ssize_t show_temp_offset(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 int nr = to_sensor_dev_attr(attr)->index;
597 struct adm1031_data *data = adm1031_update_device(dev);
598 return sprintf(buf, "%d\n",
599 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
601 static ssize_t show_temp_min(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 int nr = to_sensor_dev_attr(attr)->index;
605 struct adm1031_data *data = adm1031_update_device(dev);
606 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
608 static ssize_t show_temp_max(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 int nr = to_sensor_dev_attr(attr)->index;
612 struct adm1031_data *data = adm1031_update_device(dev);
613 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
615 static ssize_t show_temp_crit(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 int nr = to_sensor_dev_attr(attr)->index;
619 struct adm1031_data *data = adm1031_update_device(dev);
620 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
622 static ssize_t set_temp_offset(struct device *dev,
623 struct device_attribute *attr, const char *buf,
624 size_t count)
626 struct i2c_client *client = to_i2c_client(dev);
627 struct adm1031_data *data = i2c_get_clientdata(client);
628 int nr = to_sensor_dev_attr(attr)->index;
629 int val;
631 val = simple_strtol(buf, NULL, 10);
632 val = SENSORS_LIMIT(val, -15000, 15000);
633 mutex_lock(&data->update_lock);
634 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
635 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
636 data->temp_offset[nr]);
637 mutex_unlock(&data->update_lock);
638 return count;
640 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
643 struct i2c_client *client = to_i2c_client(dev);
644 struct adm1031_data *data = i2c_get_clientdata(client);
645 int nr = to_sensor_dev_attr(attr)->index;
646 int val;
648 val = simple_strtol(buf, NULL, 10);
649 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
650 mutex_lock(&data->update_lock);
651 data->temp_min[nr] = TEMP_TO_REG(val);
652 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
653 data->temp_min[nr]);
654 mutex_unlock(&data->update_lock);
655 return count;
657 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
658 const char *buf, size_t count)
660 struct i2c_client *client = to_i2c_client(dev);
661 struct adm1031_data *data = i2c_get_clientdata(client);
662 int nr = to_sensor_dev_attr(attr)->index;
663 int val;
665 val = simple_strtol(buf, NULL, 10);
666 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
667 mutex_lock(&data->update_lock);
668 data->temp_max[nr] = TEMP_TO_REG(val);
669 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
670 data->temp_max[nr]);
671 mutex_unlock(&data->update_lock);
672 return count;
674 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
675 const char *buf, size_t count)
677 struct i2c_client *client = to_i2c_client(dev);
678 struct adm1031_data *data = i2c_get_clientdata(client);
679 int nr = to_sensor_dev_attr(attr)->index;
680 int val;
682 val = simple_strtol(buf, NULL, 10);
683 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
684 mutex_lock(&data->update_lock);
685 data->temp_crit[nr] = TEMP_TO_REG(val);
686 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
687 data->temp_crit[nr]);
688 mutex_unlock(&data->update_lock);
689 return count;
692 #define temp_reg(offset) \
693 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
694 show_temp, NULL, offset - 1); \
695 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
696 show_temp_offset, set_temp_offset, offset - 1); \
697 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
698 show_temp_min, set_temp_min, offset - 1); \
699 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
700 show_temp_max, set_temp_max, offset - 1); \
701 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
702 show_temp_crit, set_temp_crit, offset - 1)
704 temp_reg(1);
705 temp_reg(2);
706 temp_reg(3);
708 /* Alarms */
709 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
711 struct adm1031_data *data = adm1031_update_device(dev);
712 return sprintf(buf, "%d\n", data->alarm);
715 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
717 static ssize_t show_alarm(struct device *dev,
718 struct device_attribute *attr, char *buf)
720 int bitnr = to_sensor_dev_attr(attr)->index;
721 struct adm1031_data *data = adm1031_update_device(dev);
722 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
725 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
726 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
727 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
728 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
729 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
730 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
731 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
732 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
733 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
734 static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
735 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
736 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
737 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
738 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
739 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
741 static struct attribute *adm1031_attributes[] = {
742 &sensor_dev_attr_fan1_input.dev_attr.attr,
743 &sensor_dev_attr_fan1_div.dev_attr.attr,
744 &sensor_dev_attr_fan1_min.dev_attr.attr,
745 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
746 &sensor_dev_attr_fan1_fault.dev_attr.attr,
747 &sensor_dev_attr_pwm1.dev_attr.attr,
748 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
749 &sensor_dev_attr_temp1_input.dev_attr.attr,
750 &sensor_dev_attr_temp1_offset.dev_attr.attr,
751 &sensor_dev_attr_temp1_min.dev_attr.attr,
752 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
753 &sensor_dev_attr_temp1_max.dev_attr.attr,
754 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
755 &sensor_dev_attr_temp1_crit.dev_attr.attr,
756 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
757 &sensor_dev_attr_temp2_input.dev_attr.attr,
758 &sensor_dev_attr_temp2_offset.dev_attr.attr,
759 &sensor_dev_attr_temp2_min.dev_attr.attr,
760 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
761 &sensor_dev_attr_temp2_max.dev_attr.attr,
762 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
763 &sensor_dev_attr_temp2_crit.dev_attr.attr,
764 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
765 &sensor_dev_attr_temp2_fault.dev_attr.attr,
767 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
768 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
769 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
771 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
772 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
773 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
775 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
777 &dev_attr_alarms.attr,
779 NULL
782 static const struct attribute_group adm1031_group = {
783 .attrs = adm1031_attributes,
786 static struct attribute *adm1031_attributes_opt[] = {
787 &sensor_dev_attr_fan2_input.dev_attr.attr,
788 &sensor_dev_attr_fan2_div.dev_attr.attr,
789 &sensor_dev_attr_fan2_min.dev_attr.attr,
790 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
791 &sensor_dev_attr_fan2_fault.dev_attr.attr,
792 &sensor_dev_attr_pwm2.dev_attr.attr,
793 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
794 &sensor_dev_attr_temp3_input.dev_attr.attr,
795 &sensor_dev_attr_temp3_offset.dev_attr.attr,
796 &sensor_dev_attr_temp3_min.dev_attr.attr,
797 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
798 &sensor_dev_attr_temp3_max.dev_attr.attr,
799 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
800 &sensor_dev_attr_temp3_crit.dev_attr.attr,
801 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
802 &sensor_dev_attr_temp3_fault.dev_attr.attr,
803 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
804 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
805 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
806 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
807 NULL
810 static const struct attribute_group adm1031_group_opt = {
811 .attrs = adm1031_attributes_opt,
814 /* Return 0 if detection is successful, -ENODEV otherwise */
815 static int adm1031_detect(struct i2c_client *client,
816 struct i2c_board_info *info)
818 struct i2c_adapter *adapter = client->adapter;
819 const char *name;
820 int id, co;
822 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
823 return -ENODEV;
825 id = i2c_smbus_read_byte_data(client, 0x3d);
826 co = i2c_smbus_read_byte_data(client, 0x3e);
828 if (!((id == 0x31 || id == 0x30) && co == 0x41))
829 return -ENODEV;
830 name = (id == 0x30) ? "adm1030" : "adm1031";
832 strlcpy(info->type, name, I2C_NAME_SIZE);
834 return 0;
837 static int adm1031_probe(struct i2c_client *client,
838 const struct i2c_device_id *id)
840 struct adm1031_data *data;
841 int err;
843 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
844 if (!data) {
845 err = -ENOMEM;
846 goto exit;
849 i2c_set_clientdata(client, data);
850 data->chip_type = id->driver_data;
851 mutex_init(&data->update_lock);
853 if (data->chip_type == adm1030)
854 data->chan_select_table = &auto_channel_select_table_adm1030;
855 else
856 data->chan_select_table = &auto_channel_select_table_adm1031;
858 /* Initialize the ADM1031 chip */
859 adm1031_init_client(client);
861 /* Register sysfs hooks */
862 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
863 goto exit_free;
865 if (data->chip_type == adm1031) {
866 if ((err = sysfs_create_group(&client->dev.kobj,
867 &adm1031_group_opt)))
868 goto exit_remove;
871 data->hwmon_dev = hwmon_device_register(&client->dev);
872 if (IS_ERR(data->hwmon_dev)) {
873 err = PTR_ERR(data->hwmon_dev);
874 goto exit_remove;
877 return 0;
879 exit_remove:
880 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
881 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
882 exit_free:
883 kfree(data);
884 exit:
885 return err;
888 static int adm1031_remove(struct i2c_client *client)
890 struct adm1031_data *data = i2c_get_clientdata(client);
892 hwmon_device_unregister(data->hwmon_dev);
893 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
894 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
895 kfree(data);
896 return 0;
899 static void adm1031_init_client(struct i2c_client *client)
901 unsigned int read_val;
902 unsigned int mask;
903 struct adm1031_data *data = i2c_get_clientdata(client);
905 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
906 if (data->chip_type == adm1031) {
907 mask |= (ADM1031_CONF2_PWM2_ENABLE |
908 ADM1031_CONF2_TACH2_ENABLE);
910 /* Initialize the ADM1031 chip (enables fan speed reading ) */
911 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
912 if ((read_val | mask) != read_val) {
913 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
916 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
917 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
918 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
919 ADM1031_CONF1_MONITOR_ENABLE);
924 static struct adm1031_data *adm1031_update_device(struct device *dev)
926 struct i2c_client *client = to_i2c_client(dev);
927 struct adm1031_data *data = i2c_get_clientdata(client);
928 int chan;
930 mutex_lock(&data->update_lock);
932 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
933 || !data->valid) {
935 dev_dbg(&client->dev, "Starting adm1031 update\n");
936 for (chan = 0;
937 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
938 u8 oldh, newh;
940 oldh =
941 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
942 data->ext_temp[chan] =
943 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
944 newh =
945 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
946 if (newh != oldh) {
947 data->ext_temp[chan] =
948 adm1031_read_value(client,
949 ADM1031_REG_EXT_TEMP);
950 #ifdef DEBUG
951 oldh =
952 adm1031_read_value(client,
953 ADM1031_REG_TEMP(chan));
955 /* oldh is actually newer */
956 if (newh != oldh)
957 dev_warn(&client->dev,
958 "Remote temperature may be "
959 "wrong.\n");
960 #endif
962 data->temp[chan] = newh;
964 data->temp_offset[chan] =
965 adm1031_read_value(client,
966 ADM1031_REG_TEMP_OFFSET(chan));
967 data->temp_min[chan] =
968 adm1031_read_value(client,
969 ADM1031_REG_TEMP_MIN(chan));
970 data->temp_max[chan] =
971 adm1031_read_value(client,
972 ADM1031_REG_TEMP_MAX(chan));
973 data->temp_crit[chan] =
974 adm1031_read_value(client,
975 ADM1031_REG_TEMP_CRIT(chan));
976 data->auto_temp[chan] =
977 adm1031_read_value(client,
978 ADM1031_REG_AUTO_TEMP(chan));
982 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
983 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
985 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
986 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
987 << 8);
988 if (data->chip_type == adm1030) {
989 data->alarm &= 0xc0ff;
992 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
993 data->fan_div[chan] =
994 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
995 data->fan_min[chan] =
996 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
997 data->fan[chan] =
998 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
999 data->pwm[chan] =
1000 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
1001 (4*chan));
1003 data->last_updated = jiffies;
1004 data->valid = 1;
1007 mutex_unlock(&data->update_lock);
1009 return data;
1012 static int __init sensors_adm1031_init(void)
1014 return i2c_add_driver(&adm1031_driver);
1017 static void __exit sensors_adm1031_exit(void)
1019 i2c_del_driver(&adm1031_driver);
1022 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1023 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1024 MODULE_LICENSE("GPL");
1026 module_init(sensors_adm1031_init);
1027 module_exit(sensors_adm1031_exit);