[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / hwmon / adm1031.c
blob56905955352cac894b0c381bad597050be236438
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 /* Insmod parameters */
68 I2C_CLIENT_INSMOD_2(adm1030, adm1031);
70 typedef u8 auto_chan_table_t[8][2];
72 /* Each client has this additional data */
73 struct adm1031_data {
74 struct device *hwmon_dev;
75 struct mutex update_lock;
76 int chip_type;
77 char valid; /* !=0 if following fields are valid */
78 unsigned long last_updated; /* In jiffies */
79 /* The chan_select_table contains the possible configurations for
80 * auto fan control.
82 const auto_chan_table_t *chan_select_table;
83 u16 alarm;
84 u8 conf1;
85 u8 conf2;
86 u8 fan[2];
87 u8 fan_div[2];
88 u8 fan_min[2];
89 u8 pwm[2];
90 u8 old_pwm[2];
91 s8 temp[3];
92 u8 ext_temp[3];
93 u8 auto_temp[3];
94 u8 auto_temp_min[3];
95 u8 auto_temp_off[3];
96 u8 auto_temp_max[3];
97 s8 temp_offset[3];
98 s8 temp_min[3];
99 s8 temp_max[3];
100 s8 temp_crit[3];
103 static int adm1031_probe(struct i2c_client *client,
104 const struct i2c_device_id *id);
105 static int adm1031_detect(struct i2c_client *client, int kind,
106 struct i2c_board_info *info);
107 static void adm1031_init_client(struct i2c_client *client);
108 static int adm1031_remove(struct i2c_client *client);
109 static struct adm1031_data *adm1031_update_device(struct device *dev);
111 static const struct i2c_device_id adm1031_id[] = {
112 { "adm1030", adm1030 },
113 { "adm1031", adm1031 },
116 MODULE_DEVICE_TABLE(i2c, adm1031_id);
118 /* This is the driver that will be inserted */
119 static struct i2c_driver adm1031_driver = {
120 .class = I2C_CLASS_HWMON,
121 .driver = {
122 .name = "adm1031",
124 .probe = adm1031_probe,
125 .remove = adm1031_remove,
126 .id_table = adm1031_id,
127 .detect = adm1031_detect,
128 .address_data = &addr_data,
131 static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
133 return i2c_smbus_read_byte_data(client, reg);
136 static inline int
137 adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
139 return i2c_smbus_write_byte_data(client, reg, value);
143 #define TEMP_TO_REG(val) (((val) < 0 ? ((val - 500) / 1000) : \
144 ((val + 500) / 1000)))
146 #define TEMP_FROM_REG(val) ((val) * 1000)
148 #define TEMP_FROM_REG_EXT(val, ext) (TEMP_FROM_REG(val) + (ext) * 125)
150 #define TEMP_OFFSET_TO_REG(val) (TEMP_TO_REG(val) & 0x8f)
151 #define TEMP_OFFSET_FROM_REG(val) TEMP_FROM_REG((val) < 0 ? \
152 (val) | 0x70 : (val))
154 #define FAN_FROM_REG(reg, div) ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
156 static int FAN_TO_REG(int reg, int div)
158 int tmp;
159 tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
160 return tmp > 255 ? 255 : tmp;
163 #define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))
165 #define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
166 #define PWM_FROM_REG(val) ((val) << 4)
168 #define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
169 #define FAN_CHAN_TO_REG(val, reg) \
170 (((reg) & 0x1F) | (((val) << 5) & 0xe0))
172 #define AUTO_TEMP_MIN_TO_REG(val, reg) \
173 ((((val)/500) & 0xf8)|((reg) & 0x7))
174 #define AUTO_TEMP_RANGE_FROM_REG(reg) (5000 * (1<< ((reg)&0x7)))
175 #define AUTO_TEMP_MIN_FROM_REG(reg) (1000 * ((((reg) >> 3) & 0x1f) << 2))
177 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
179 #define AUTO_TEMP_OFF_FROM_REG(reg) \
180 (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
182 #define AUTO_TEMP_MAX_FROM_REG(reg) \
183 (AUTO_TEMP_RANGE_FROM_REG(reg) + \
184 AUTO_TEMP_MIN_FROM_REG(reg))
186 static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
188 int ret;
189 int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
191 range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
192 ret = ((reg & 0xf8) |
193 (range < 10000 ? 0 :
194 range < 20000 ? 1 :
195 range < 40000 ? 2 : range < 80000 ? 3 : 4));
196 return ret;
199 /* FAN auto control */
200 #define GET_FAN_AUTO_BITFIELD(data, idx) \
201 (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
203 /* The tables below contains the possible values for the auto fan
204 * control bitfields. the index in the table is the register value.
205 * MSb is the auto fan control enable bit, so the four first entries
206 * in the table disables auto fan control when both bitfields are zero.
208 static const auto_chan_table_t auto_channel_select_table_adm1031 = {
209 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
210 { 2 /* 0b010 */ , 4 /* 0b100 */ },
211 { 2 /* 0b010 */ , 2 /* 0b010 */ },
212 { 4 /* 0b100 */ , 4 /* 0b100 */ },
213 { 7 /* 0b111 */ , 7 /* 0b111 */ },
216 static const auto_chan_table_t auto_channel_select_table_adm1030 = {
217 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
218 { 2 /* 0b10 */ , 0 },
219 { 0xff /* invalid */ , 0 },
220 { 0xff /* invalid */ , 0 },
221 { 3 /* 0b11 */ , 0 },
224 /* That function checks if a bitfield is valid and returns the other bitfield
225 * nearest match if no exact match where found.
227 static int
228 get_fan_auto_nearest(struct adm1031_data *data,
229 int chan, u8 val, u8 reg, u8 * new_reg)
231 int i;
232 int first_match = -1, exact_match = -1;
233 u8 other_reg_val =
234 (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
236 if (val == 0) {
237 *new_reg = 0;
238 return 0;
241 for (i = 0; i < 8; i++) {
242 if ((val == (*data->chan_select_table)[i][chan]) &&
243 ((*data->chan_select_table)[i][chan ? 0 : 1] ==
244 other_reg_val)) {
245 /* We found an exact match */
246 exact_match = i;
247 break;
248 } else if (val == (*data->chan_select_table)[i][chan] &&
249 first_match == -1) {
250 /* Save the first match in case of an exact match has
251 * not been found
253 first_match = i;
257 if (exact_match >= 0) {
258 *new_reg = exact_match;
259 } else if (first_match >= 0) {
260 *new_reg = first_match;
261 } else {
262 return -EINVAL;
264 return 0;
267 static ssize_t show_fan_auto_channel(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 int nr = to_sensor_dev_attr(attr)->index;
271 struct adm1031_data *data = adm1031_update_device(dev);
272 return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
275 static ssize_t
276 set_fan_auto_channel(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
279 struct i2c_client *client = to_i2c_client(dev);
280 struct adm1031_data *data = i2c_get_clientdata(client);
281 int nr = to_sensor_dev_attr(attr)->index;
282 int val = simple_strtol(buf, NULL, 10);
283 u8 reg;
284 int ret;
285 u8 old_fan_mode;
287 old_fan_mode = data->conf1;
289 mutex_lock(&data->update_lock);
291 if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
292 mutex_unlock(&data->update_lock);
293 return ret;
295 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
296 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) ^
297 (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
298 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
299 /* Switch to Auto Fan Mode
300 * Save PWM registers
301 * Set PWM registers to 33% Both */
302 data->old_pwm[0] = data->pwm[0];
303 data->old_pwm[1] = data->pwm[1];
304 adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
305 } else {
306 /* Switch to Manual Mode */
307 data->pwm[0] = data->old_pwm[0];
308 data->pwm[1] = data->old_pwm[1];
309 /* Restore PWM registers */
310 adm1031_write_value(client, ADM1031_REG_PWM,
311 data->pwm[0] | (data->pwm[1] << 4));
314 data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
315 adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
316 mutex_unlock(&data->update_lock);
317 return count;
320 static SENSOR_DEVICE_ATTR(auto_fan1_channel, S_IRUGO | S_IWUSR,
321 show_fan_auto_channel, set_fan_auto_channel, 0);
322 static SENSOR_DEVICE_ATTR(auto_fan2_channel, S_IRUGO | S_IWUSR,
323 show_fan_auto_channel, set_fan_auto_channel, 1);
325 /* Auto Temps */
326 static ssize_t show_auto_temp_off(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 int nr = to_sensor_dev_attr(attr)->index;
330 struct adm1031_data *data = adm1031_update_device(dev);
331 return sprintf(buf, "%d\n",
332 AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
334 static ssize_t show_auto_temp_min(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",
340 AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
342 static ssize_t
343 set_auto_temp_min(struct device *dev, struct device_attribute *attr,
344 const char *buf, size_t count)
346 struct i2c_client *client = to_i2c_client(dev);
347 struct adm1031_data *data = i2c_get_clientdata(client);
348 int nr = to_sensor_dev_attr(attr)->index;
349 int val = simple_strtol(buf, NULL, 10);
351 mutex_lock(&data->update_lock);
352 data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
353 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
354 data->auto_temp[nr]);
355 mutex_unlock(&data->update_lock);
356 return count;
358 static ssize_t show_auto_temp_max(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 int nr = to_sensor_dev_attr(attr)->index;
362 struct adm1031_data *data = adm1031_update_device(dev);
363 return sprintf(buf, "%d\n",
364 AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
366 static ssize_t
367 set_auto_temp_max(struct device *dev, struct device_attribute *attr,
368 const char *buf, size_t count)
370 struct i2c_client *client = to_i2c_client(dev);
371 struct adm1031_data *data = i2c_get_clientdata(client);
372 int nr = to_sensor_dev_attr(attr)->index;
373 int val = simple_strtol(buf, NULL, 10);
375 mutex_lock(&data->update_lock);
376 data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
377 adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
378 data->temp_max[nr]);
379 mutex_unlock(&data->update_lock);
380 return count;
383 #define auto_temp_reg(offset) \
384 static SENSOR_DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO, \
385 show_auto_temp_off, NULL, offset - 1); \
386 static SENSOR_DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR, \
387 show_auto_temp_min, set_auto_temp_min, offset - 1); \
388 static SENSOR_DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR, \
389 show_auto_temp_max, set_auto_temp_max, offset - 1)
391 auto_temp_reg(1);
392 auto_temp_reg(2);
393 auto_temp_reg(3);
395 /* pwm */
396 static ssize_t show_pwm(struct device *dev,
397 struct device_attribute *attr, char *buf)
399 int nr = to_sensor_dev_attr(attr)->index;
400 struct adm1031_data *data = adm1031_update_device(dev);
401 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
403 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
404 const char *buf, size_t count)
406 struct i2c_client *client = to_i2c_client(dev);
407 struct adm1031_data *data = i2c_get_clientdata(client);
408 int nr = to_sensor_dev_attr(attr)->index;
409 int val = simple_strtol(buf, NULL, 10);
410 int reg;
412 mutex_lock(&data->update_lock);
413 if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) &&
414 (((val>>4) & 0xf) != 5)) {
415 /* In automatic mode, the only PWM accepted is 33% */
416 mutex_unlock(&data->update_lock);
417 return -EINVAL;
419 data->pwm[nr] = PWM_TO_REG(val);
420 reg = adm1031_read_value(client, ADM1031_REG_PWM);
421 adm1031_write_value(client, ADM1031_REG_PWM,
422 nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
423 : (data->pwm[nr] & 0xf) | (reg & 0xf0));
424 mutex_unlock(&data->update_lock);
425 return count;
428 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
429 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
430 static SENSOR_DEVICE_ATTR(auto_fan1_min_pwm, S_IRUGO | S_IWUSR,
431 show_pwm, set_pwm, 0);
432 static SENSOR_DEVICE_ATTR(auto_fan2_min_pwm, S_IRUGO | S_IWUSR,
433 show_pwm, set_pwm, 1);
435 /* Fans */
438 * That function checks the cases where the fan reading is not
439 * relevant. It is used to provide 0 as fan reading when the fan is
440 * not supposed to run
442 static int trust_fan_readings(struct adm1031_data *data, int chan)
444 int res = 0;
446 if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
447 switch (data->conf1 & 0x60) {
448 case 0x00: /* remote temp1 controls fan1 remote temp2 controls fan2 */
449 res = data->temp[chan+1] >=
450 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
451 break;
452 case 0x20: /* remote temp1 controls both fans */
453 res =
454 data->temp[1] >=
455 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
456 break;
457 case 0x40: /* remote temp2 controls both fans */
458 res =
459 data->temp[2] >=
460 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
461 break;
462 case 0x60: /* max controls both fans */
463 res =
464 data->temp[0] >=
465 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
466 || data->temp[1] >=
467 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
468 || (data->chip_type == adm1031
469 && data->temp[2] >=
470 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
471 break;
473 } else {
474 res = data->pwm[chan] > 0;
476 return res;
480 static ssize_t show_fan(struct device *dev,
481 struct device_attribute *attr, char *buf)
483 int nr = to_sensor_dev_attr(attr)->index;
484 struct adm1031_data *data = adm1031_update_device(dev);
485 int value;
487 value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
488 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
489 return sprintf(buf, "%d\n", value);
492 static ssize_t show_fan_div(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 int nr = to_sensor_dev_attr(attr)->index;
496 struct adm1031_data *data = adm1031_update_device(dev);
497 return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
499 static ssize_t show_fan_min(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 int nr = to_sensor_dev_attr(attr)->index;
503 struct adm1031_data *data = adm1031_update_device(dev);
504 return sprintf(buf, "%d\n",
505 FAN_FROM_REG(data->fan_min[nr],
506 FAN_DIV_FROM_REG(data->fan_div[nr])));
508 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
511 struct i2c_client *client = to_i2c_client(dev);
512 struct adm1031_data *data = i2c_get_clientdata(client);
513 int nr = to_sensor_dev_attr(attr)->index;
514 int val = simple_strtol(buf, NULL, 10);
516 mutex_lock(&data->update_lock);
517 if (val) {
518 data->fan_min[nr] =
519 FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
520 } else {
521 data->fan_min[nr] = 0xff;
523 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
524 mutex_unlock(&data->update_lock);
525 return count;
527 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
528 const char *buf, size_t count)
530 struct i2c_client *client = to_i2c_client(dev);
531 struct adm1031_data *data = i2c_get_clientdata(client);
532 int nr = to_sensor_dev_attr(attr)->index;
533 int val = simple_strtol(buf, NULL, 10);
534 u8 tmp;
535 int old_div;
536 int new_min;
538 tmp = val == 8 ? 0xc0 :
539 val == 4 ? 0x80 :
540 val == 2 ? 0x40 :
541 val == 1 ? 0x00 :
542 0xff;
543 if (tmp == 0xff)
544 return -EINVAL;
546 mutex_lock(&data->update_lock);
547 /* Get fresh readings */
548 data->fan_div[nr] = adm1031_read_value(client,
549 ADM1031_REG_FAN_DIV(nr));
550 data->fan_min[nr] = adm1031_read_value(client,
551 ADM1031_REG_FAN_MIN(nr));
553 /* Write the new clock divider and fan min */
554 old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
555 data->fan_div[nr] = tmp | (0x3f & data->fan_div[nr]);
556 new_min = data->fan_min[nr] * old_div / val;
557 data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
559 adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr),
560 data->fan_div[nr]);
561 adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr),
562 data->fan_min[nr]);
564 /* Invalidate the cache: fan speed is no longer valid */
565 data->valid = 0;
566 mutex_unlock(&data->update_lock);
567 return count;
570 #define fan_offset(offset) \
571 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
572 show_fan, NULL, offset - 1); \
573 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
574 show_fan_min, set_fan_min, offset - 1); \
575 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
576 show_fan_div, set_fan_div, offset - 1)
578 fan_offset(1);
579 fan_offset(2);
582 /* Temps */
583 static ssize_t show_temp(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 int nr = to_sensor_dev_attr(attr)->index;
587 struct adm1031_data *data = adm1031_update_device(dev);
588 int ext;
589 ext = nr == 0 ?
590 ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
591 (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
592 return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
594 static ssize_t show_temp_offset(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",
600 TEMP_OFFSET_FROM_REG(data->temp_offset[nr]));
602 static ssize_t show_temp_min(struct device *dev,
603 struct device_attribute *attr, char *buf)
605 int nr = to_sensor_dev_attr(attr)->index;
606 struct adm1031_data *data = adm1031_update_device(dev);
607 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
609 static ssize_t show_temp_max(struct device *dev,
610 struct device_attribute *attr, char *buf)
612 int nr = to_sensor_dev_attr(attr)->index;
613 struct adm1031_data *data = adm1031_update_device(dev);
614 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
616 static ssize_t show_temp_crit(struct device *dev,
617 struct device_attribute *attr, char *buf)
619 int nr = to_sensor_dev_attr(attr)->index;
620 struct adm1031_data *data = adm1031_update_device(dev);
621 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
623 static ssize_t set_temp_offset(struct device *dev,
624 struct device_attribute *attr, const char *buf,
625 size_t count)
627 struct i2c_client *client = to_i2c_client(dev);
628 struct adm1031_data *data = i2c_get_clientdata(client);
629 int nr = to_sensor_dev_attr(attr)->index;
630 int val;
632 val = simple_strtol(buf, NULL, 10);
633 val = SENSORS_LIMIT(val, -15000, 15000);
634 mutex_lock(&data->update_lock);
635 data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
636 adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
637 data->temp_offset[nr]);
638 mutex_unlock(&data->update_lock);
639 return count;
641 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
642 const char *buf, size_t count)
644 struct i2c_client *client = to_i2c_client(dev);
645 struct adm1031_data *data = i2c_get_clientdata(client);
646 int nr = to_sensor_dev_attr(attr)->index;
647 int val;
649 val = simple_strtol(buf, NULL, 10);
650 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
651 mutex_lock(&data->update_lock);
652 data->temp_min[nr] = TEMP_TO_REG(val);
653 adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
654 data->temp_min[nr]);
655 mutex_unlock(&data->update_lock);
656 return count;
658 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
659 const char *buf, size_t count)
661 struct i2c_client *client = to_i2c_client(dev);
662 struct adm1031_data *data = i2c_get_clientdata(client);
663 int nr = to_sensor_dev_attr(attr)->index;
664 int val;
666 val = simple_strtol(buf, NULL, 10);
667 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
668 mutex_lock(&data->update_lock);
669 data->temp_max[nr] = TEMP_TO_REG(val);
670 adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
671 data->temp_max[nr]);
672 mutex_unlock(&data->update_lock);
673 return count;
675 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
676 const char *buf, size_t count)
678 struct i2c_client *client = to_i2c_client(dev);
679 struct adm1031_data *data = i2c_get_clientdata(client);
680 int nr = to_sensor_dev_attr(attr)->index;
681 int val;
683 val = simple_strtol(buf, NULL, 10);
684 val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
685 mutex_lock(&data->update_lock);
686 data->temp_crit[nr] = TEMP_TO_REG(val);
687 adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
688 data->temp_crit[nr]);
689 mutex_unlock(&data->update_lock);
690 return count;
693 #define temp_reg(offset) \
694 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
695 show_temp, NULL, offset - 1); \
696 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
697 show_temp_offset, set_temp_offset, offset - 1); \
698 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
699 show_temp_min, set_temp_min, offset - 1); \
700 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
701 show_temp_max, set_temp_max, offset - 1); \
702 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
703 show_temp_crit, set_temp_crit, offset - 1)
705 temp_reg(1);
706 temp_reg(2);
707 temp_reg(3);
709 /* Alarms */
710 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
712 struct adm1031_data *data = adm1031_update_device(dev);
713 return sprintf(buf, "%d\n", data->alarm);
716 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
718 static ssize_t show_alarm(struct device *dev,
719 struct device_attribute *attr, char *buf)
721 int bitnr = to_sensor_dev_attr(attr)->index;
722 struct adm1031_data *data = adm1031_update_device(dev);
723 return sprintf(buf, "%d\n", (data->alarm >> bitnr) & 1);
726 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
727 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
728 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
729 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
730 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 4);
731 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 5);
732 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
733 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 7);
734 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 8);
735 static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 9);
736 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 10);
737 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
738 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 12);
739 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 13);
740 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14);
742 static struct attribute *adm1031_attributes[] = {
743 &sensor_dev_attr_fan1_input.dev_attr.attr,
744 &sensor_dev_attr_fan1_div.dev_attr.attr,
745 &sensor_dev_attr_fan1_min.dev_attr.attr,
746 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
747 &sensor_dev_attr_fan1_fault.dev_attr.attr,
748 &sensor_dev_attr_pwm1.dev_attr.attr,
749 &sensor_dev_attr_auto_fan1_channel.dev_attr.attr,
750 &sensor_dev_attr_temp1_input.dev_attr.attr,
751 &sensor_dev_attr_temp1_offset.dev_attr.attr,
752 &sensor_dev_attr_temp1_min.dev_attr.attr,
753 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
754 &sensor_dev_attr_temp1_max.dev_attr.attr,
755 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
756 &sensor_dev_attr_temp1_crit.dev_attr.attr,
757 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
758 &sensor_dev_attr_temp2_input.dev_attr.attr,
759 &sensor_dev_attr_temp2_offset.dev_attr.attr,
760 &sensor_dev_attr_temp2_min.dev_attr.attr,
761 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
762 &sensor_dev_attr_temp2_max.dev_attr.attr,
763 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
764 &sensor_dev_attr_temp2_crit.dev_attr.attr,
765 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
766 &sensor_dev_attr_temp2_fault.dev_attr.attr,
768 &sensor_dev_attr_auto_temp1_off.dev_attr.attr,
769 &sensor_dev_attr_auto_temp1_min.dev_attr.attr,
770 &sensor_dev_attr_auto_temp1_max.dev_attr.attr,
772 &sensor_dev_attr_auto_temp2_off.dev_attr.attr,
773 &sensor_dev_attr_auto_temp2_min.dev_attr.attr,
774 &sensor_dev_attr_auto_temp2_max.dev_attr.attr,
776 &sensor_dev_attr_auto_fan1_min_pwm.dev_attr.attr,
778 &dev_attr_alarms.attr,
780 NULL
783 static const struct attribute_group adm1031_group = {
784 .attrs = adm1031_attributes,
787 static struct attribute *adm1031_attributes_opt[] = {
788 &sensor_dev_attr_fan2_input.dev_attr.attr,
789 &sensor_dev_attr_fan2_div.dev_attr.attr,
790 &sensor_dev_attr_fan2_min.dev_attr.attr,
791 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
792 &sensor_dev_attr_fan2_fault.dev_attr.attr,
793 &sensor_dev_attr_pwm2.dev_attr.attr,
794 &sensor_dev_attr_auto_fan2_channel.dev_attr.attr,
795 &sensor_dev_attr_temp3_input.dev_attr.attr,
796 &sensor_dev_attr_temp3_offset.dev_attr.attr,
797 &sensor_dev_attr_temp3_min.dev_attr.attr,
798 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
799 &sensor_dev_attr_temp3_max.dev_attr.attr,
800 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
801 &sensor_dev_attr_temp3_crit.dev_attr.attr,
802 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
803 &sensor_dev_attr_temp3_fault.dev_attr.attr,
804 &sensor_dev_attr_auto_temp3_off.dev_attr.attr,
805 &sensor_dev_attr_auto_temp3_min.dev_attr.attr,
806 &sensor_dev_attr_auto_temp3_max.dev_attr.attr,
807 &sensor_dev_attr_auto_fan2_min_pwm.dev_attr.attr,
808 NULL
811 static const struct attribute_group adm1031_group_opt = {
812 .attrs = adm1031_attributes_opt,
815 /* Return 0 if detection is successful, -ENODEV otherwise */
816 static int adm1031_detect(struct i2c_client *client, int kind,
817 struct i2c_board_info *info)
819 struct i2c_adapter *adapter = client->adapter;
820 const char *name = "";
822 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
823 return -ENODEV;
825 if (kind < 0) {
826 int id, co;
827 id = i2c_smbus_read_byte_data(client, 0x3d);
828 co = i2c_smbus_read_byte_data(client, 0x3e);
830 if (!((id == 0x31 || id == 0x30) && co == 0x41))
831 return -ENODEV;
832 kind = (id == 0x30) ? adm1030 : adm1031;
835 if (kind <= 0)
836 kind = adm1031;
838 /* Given the detected chip type, set the chip name and the
839 * auto fan control helper table. */
840 if (kind == adm1030) {
841 name = "adm1030";
842 } else if (kind == adm1031) {
843 name = "adm1031";
845 strlcpy(info->type, name, I2C_NAME_SIZE);
847 return 0;
850 static int adm1031_probe(struct i2c_client *client,
851 const struct i2c_device_id *id)
853 struct adm1031_data *data;
854 int err;
856 data = kzalloc(sizeof(struct adm1031_data), GFP_KERNEL);
857 if (!data) {
858 err = -ENOMEM;
859 goto exit;
862 i2c_set_clientdata(client, data);
863 data->chip_type = id->driver_data;
864 mutex_init(&data->update_lock);
866 if (data->chip_type == adm1030)
867 data->chan_select_table = &auto_channel_select_table_adm1030;
868 else
869 data->chan_select_table = &auto_channel_select_table_adm1031;
871 /* Initialize the ADM1031 chip */
872 adm1031_init_client(client);
874 /* Register sysfs hooks */
875 if ((err = sysfs_create_group(&client->dev.kobj, &adm1031_group)))
876 goto exit_free;
878 if (data->chip_type == adm1031) {
879 if ((err = sysfs_create_group(&client->dev.kobj,
880 &adm1031_group_opt)))
881 goto exit_remove;
884 data->hwmon_dev = hwmon_device_register(&client->dev);
885 if (IS_ERR(data->hwmon_dev)) {
886 err = PTR_ERR(data->hwmon_dev);
887 goto exit_remove;
890 return 0;
892 exit_remove:
893 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
894 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
895 exit_free:
896 kfree(data);
897 exit:
898 return err;
901 static int adm1031_remove(struct i2c_client *client)
903 struct adm1031_data *data = i2c_get_clientdata(client);
905 hwmon_device_unregister(data->hwmon_dev);
906 sysfs_remove_group(&client->dev.kobj, &adm1031_group);
907 sysfs_remove_group(&client->dev.kobj, &adm1031_group_opt);
908 kfree(data);
909 return 0;
912 static void adm1031_init_client(struct i2c_client *client)
914 unsigned int read_val;
915 unsigned int mask;
916 struct adm1031_data *data = i2c_get_clientdata(client);
918 mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
919 if (data->chip_type == adm1031) {
920 mask |= (ADM1031_CONF2_PWM2_ENABLE |
921 ADM1031_CONF2_TACH2_ENABLE);
923 /* Initialize the ADM1031 chip (enables fan speed reading ) */
924 read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
925 if ((read_val | mask) != read_val) {
926 adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
929 read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
930 if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
931 adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
932 ADM1031_CONF1_MONITOR_ENABLE);
937 static struct adm1031_data *adm1031_update_device(struct device *dev)
939 struct i2c_client *client = to_i2c_client(dev);
940 struct adm1031_data *data = i2c_get_clientdata(client);
941 int chan;
943 mutex_lock(&data->update_lock);
945 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
946 || !data->valid) {
948 dev_dbg(&client->dev, "Starting adm1031 update\n");
949 for (chan = 0;
950 chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
951 u8 oldh, newh;
953 oldh =
954 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
955 data->ext_temp[chan] =
956 adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
957 newh =
958 adm1031_read_value(client, ADM1031_REG_TEMP(chan));
959 if (newh != oldh) {
960 data->ext_temp[chan] =
961 adm1031_read_value(client,
962 ADM1031_REG_EXT_TEMP);
963 #ifdef DEBUG
964 oldh =
965 adm1031_read_value(client,
966 ADM1031_REG_TEMP(chan));
968 /* oldh is actually newer */
969 if (newh != oldh)
970 dev_warn(&client->dev,
971 "Remote temperature may be "
972 "wrong.\n");
973 #endif
975 data->temp[chan] = newh;
977 data->temp_offset[chan] =
978 adm1031_read_value(client,
979 ADM1031_REG_TEMP_OFFSET(chan));
980 data->temp_min[chan] =
981 adm1031_read_value(client,
982 ADM1031_REG_TEMP_MIN(chan));
983 data->temp_max[chan] =
984 adm1031_read_value(client,
985 ADM1031_REG_TEMP_MAX(chan));
986 data->temp_crit[chan] =
987 adm1031_read_value(client,
988 ADM1031_REG_TEMP_CRIT(chan));
989 data->auto_temp[chan] =
990 adm1031_read_value(client,
991 ADM1031_REG_AUTO_TEMP(chan));
995 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
996 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
998 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
999 | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
1000 << 8);
1001 if (data->chip_type == adm1030) {
1002 data->alarm &= 0xc0ff;
1005 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
1006 data->fan_div[chan] =
1007 adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
1008 data->fan_min[chan] =
1009 adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
1010 data->fan[chan] =
1011 adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
1012 data->pwm[chan] =
1013 0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >>
1014 (4*chan));
1016 data->last_updated = jiffies;
1017 data->valid = 1;
1020 mutex_unlock(&data->update_lock);
1022 return data;
1025 static int __init sensors_adm1031_init(void)
1027 return i2c_add_driver(&adm1031_driver);
1030 static void __exit sensors_adm1031_exit(void)
1032 i2c_del_driver(&adm1031_driver);
1035 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
1036 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
1037 MODULE_LICENSE("GPL");
1039 module_init(sensors_adm1031_init);
1040 module_exit(sensors_adm1031_exit);