Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / adm1031.c
blob8b85ddcc63f85edf12de9e6aea7dc8621ee251ee
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_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 };
66 =======
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 */
76 struct adm1031_data {
77 struct i2c_client client;
78 struct device *hwmon_dev;
79 struct mutex update_lock;
80 int chip_type;
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
84 * auto fan control.
86 const auto_chan_table_t *chan_select_table;
87 u16 alarm;
88 u8 conf1;
89 u8 conf2;
90 u8 fan[2];
91 u8 fan_div[2];
92 u8 fan_min[2];
93 u8 pwm[2];
94 u8 old_pwm[2];
95 s8 temp[3];
96 u8 ext_temp[3];
97 u8 auto_temp[3];
98 u8 auto_temp_min[3];
99 u8 auto_temp_off[3];
100 u8 auto_temp_max[3];
101 s8 temp_min[3];
102 s8 temp_max[3];
103 s8 temp_crit[3];
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 = {
114 .driver = {
115 .name = "adm1031",
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);
126 static inline int
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)
144 int tmp;
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)
174 int ret;
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) |
179 (range < 10000 ? 0 :
180 range < 20000 ? 1 :
181 range < 40000 ? 2 : range < 80000 ? 3 : 4));
182 return ret;
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.
213 static int
214 get_fan_auto_nearest(struct adm1031_data *data,
215 int chan, u8 val, u8 reg, u8 * new_reg)
217 int i;
218 int first_match = -1, exact_match = -1;
219 u8 other_reg_val =
220 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
222 if (val == 0) {
223 *new_reg = 0;
224 return 0;
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] ==
230 other_reg_val)) {
231 /* We found an exact match */
232 exact_match = i;
233 break;
234 } else if (val == (*data->chan_select_table)[i][chan] &&
235 first_match == -1) {
236 /* Save the first match in case of an exact match has
237 * not been found
239 first_match = i;
243 if (exact_match >= 0) {
244 *new_reg = exact_match;
245 } else if (first_match >= 0) {
246 *new_reg = first_match;
247 } else {
248 return -EINVAL;
250 return 0;
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));
261 static ssize_t
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);
269 u8 reg;
270 int ret;
271 u8 old_fan_mode;
273 old_fan_mode = data->conf1;
275 mutex_lock(&data->update_lock);
277 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
278 mutex_unlock(&data->update_lock);
279 return ret;
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
286 * Save PWM registers
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);
291 } else {
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);
303 return count;
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);
311 /* Auto Temps */
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]));
328 static ssize_t
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);
342 return count;
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]));
352 static ssize_t
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),
364 data->temp_max[nr]);
365 mutex_unlock(&data->update_lock);
366 return count;
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)
377 auto_temp_reg(1);
378 auto_temp_reg(2);
379 auto_temp_reg(3);
381 /* pwm */
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);
396 int reg;
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);
403 return -EINVAL;
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);
411 return count;
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);
421 /* Fans */
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)
430 int res = 0;
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]);
437 break;
438 case 0x20: /* remote temp1 controls both fans */
439 res =
440 data->temp[1] >=
441 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
442 break;
443 case 0x40: /* remote temp2 controls both fans */
444 res =
445 data->temp[2] >=
446 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
447 break;
448 case 0x60: /* max controls both fans */
449 res =
450 data->temp[0] >=
451 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
452 || data->temp[1] >=
453 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
454 || (data->chip_type == adm1031
455 && data->temp[2] >=
456 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
457 break;
459 } else {
460 res = data->pwm[chan] > 0;
462 return res;
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);
471 int value;
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);
503 if (val) {
504 data->fan_min[nr] =
505 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
506 } else {
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);
511 return count;
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);
520 u8 tmp;
521 int old_div;
522 int new_min;
524 tmp = val == 8 ? 0xc0 :
525 val == 4 ? 0x80 :
526 val == 2 ? 0x40 :
527 val == 1 ? 0x00 :
528 0xff;
529 if (tmp == 0xff)
530 return -EINVAL;
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),
546 data->fan_div[nr]);
547 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
548 data->fan_min[nr]);
550 /* Invalidate the cache: fan speed is no longer valid */
551 data->valid = 0;
552 mutex_unlock(&data->update_lock);
553 return count;
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)
564 fan_offset(1);
565 fan_offset(2);
568 /* Temps */
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);
574 int ext;
575 ext = nr == 0 ?
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;
607 int val;
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),
614 data->temp_min[nr]);
615 mutex_unlock(&data->update_lock);
616 return count;
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;
624 int val;
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),
631 data->temp_max[nr]);
632 mutex_unlock(&data->update_lock);
633 return count;
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;
641 int val;
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);
650 return count;
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)
663 temp_reg(1);
664 temp_reg(2);
665 temp_reg(3);
667 /* Alarms */
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))
703 return 0;
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,
743 NULL
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,
770 NULL
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;
782 int err = 0;
783 const char *name = "";
785 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
786 goto exit;
788 if (!(data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
789 err = -ENOMEM;
790 goto exit;
793 client = &data->client;
794 i2c_set_clientdata(client, data);
795 client->addr = address;
796 client->adapter = adapter;
797 client->driver = &adm1031_driver;
799 if (kind < 0) {
800 int id, co;
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))
805 goto exit_free;
806 kind = (id == 0x30) ? adm1030 : adm1031;
809 if (kind <= 0)
810 kind = adm1031;
812 /* Given the detected chip type, set the chip name and the
813 * auto fan control helper table. */
814 if (kind == adm1030) {
815 name = "adm1030";
816 data->chan_select_table = &auto_channel_select_table_adm1030;
817 } else if (kind == adm1031) {
818 name = "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)))
828 goto exit_free;
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)))
835 goto exit_detach;
837 if (kind == adm1031) {
838 if ((err = sysfs_create_group(&client->dev.kobj,
839 &adm1031_group_opt)))
840 goto exit_remove;
843 data->hwmon_dev = hwmon_device_register(&client->dev);
844 if (IS_ERR(data->hwmon_dev)) {
845 err = PTR_ERR(data->hwmon_dev);
846 goto exit_remove;
849 return 0;
851 exit_remove:
852 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
853 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
854 exit_detach:
855 i2c_detach_client(client);
856 exit_free:
857 kfree(data);
858 exit:
859 return err;
862 static int adm1031_detach_client(struct i2c_client *client)
864 struct adm1031_data *data = i2c_get_clientdata(client);
865 int ret;
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) {
871 return ret;
873 kfree(data);
874 return 0;
877 static void adm1031_init_client(struct i2c_client *client)
879 unsigned int read_val;
880 unsigned int mask;
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);
906 int chan;
908 mutex_lock(&data->update_lock);
910 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
911 || !data->valid) {
913 dev_dbg(&client->dev, "Starting adm1031 update\n");
914 for (chan = 0;
915 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
916 u8 oldh, newh;
918 oldh =
919 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
920 data->ext_temp[chan] =
921 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
922 newh =
923 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
924 if (newh != oldh) {
925 data->ext_temp[chan] =
926 adm1031_read_value(client,
927 ADM1031_REG_EXT_TEMP);
928 #ifdef DEBUG
929 oldh =
930 adm1031_read_value(client,
931 ADM1031_REG_TEMP(chan));
933 /* oldh is actually newer */
934 if (newh != oldh)
935 dev_warn(&client->dev,
936 "Remote temperature may be "
937 "wrong.\n");
938 #endif
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))
962 << 8);
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));
972 data->fan[chan] =
973 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
974 data->pwm[chan] =
975 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
976 (4*chan));
978 data->last_updated = jiffies;
979 data->valid = 1;
982 mutex_unlock(&data->update_lock);
984 return data;
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);