ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / hwmon / adt7475.c
blobb5fcd87931cbd755fb1aaffbbeec59ed0e4d75fc
1 /*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org>
8 * Derived from the lm83 driver by Jean Delvare
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/hwmon-vid.h>
22 #include <linux/err.h>
24 /* Indexes for the sysfs hooks */
26 #define INPUT 0
27 #define MIN 1
28 #define MAX 2
29 #define CONTROL 3
30 #define OFFSET 3
31 #define AUTOMIN 4
32 #define THERM 5
33 #define HYSTERSIS 6
35 /* These are unique identifiers for the sysfs functions - unlike the
36 numbers above, these are not also indexes into an array
39 #define ALARM 9
40 #define FAULT 10
42 /* 7475 Common Registers */
44 #define REG_DEVREV2 0x12 /* ADT7490 only */
46 #define REG_VTT 0x1E /* ADT7490 only */
47 #define REG_EXTEND3 0x1F /* ADT7490 only */
49 #define REG_VOLTAGE_BASE 0x20
50 #define REG_TEMP_BASE 0x25
51 #define REG_TACH_BASE 0x28
52 #define REG_PWM_BASE 0x30
53 #define REG_PWM_MAX_BASE 0x38
55 #define REG_DEVID 0x3D
56 #define REG_VENDID 0x3E
57 #define REG_DEVID2 0x3F
59 #define REG_STATUS1 0x41
60 #define REG_STATUS2 0x42
62 #define REG_VID 0x43 /* ADT7476 only */
64 #define REG_VOLTAGE_MIN_BASE 0x44
65 #define REG_VOLTAGE_MAX_BASE 0x45
67 #define REG_TEMP_MIN_BASE 0x4E
68 #define REG_TEMP_MAX_BASE 0x4F
70 #define REG_TACH_MIN_BASE 0x54
72 #define REG_PWM_CONFIG_BASE 0x5C
74 #define REG_TEMP_TRANGE_BASE 0x5F
76 #define REG_PWM_MIN_BASE 0x64
78 #define REG_TEMP_TMIN_BASE 0x67
79 #define REG_TEMP_THERM_BASE 0x6A
81 #define REG_REMOTE1_HYSTERSIS 0x6D
82 #define REG_REMOTE2_HYSTERSIS 0x6E
84 #define REG_TEMP_OFFSET_BASE 0x70
86 #define REG_CONFIG2 0x73
88 #define REG_EXTEND1 0x76
89 #define REG_EXTEND2 0x77
91 #define REG_CONFIG3 0x78
92 #define REG_CONFIG5 0x7C
93 #define REG_CONFIG4 0x7D
95 #define REG_STATUS4 0x81 /* ADT7490 only */
97 #define REG_VTT_MIN 0x84 /* ADT7490 only */
98 #define REG_VTT_MAX 0x86 /* ADT7490 only */
100 #define VID_VIDSEL 0x80 /* ADT7476 only */
102 #define CONFIG2_ATTN 0x20
104 #define CONFIG3_SMBALERT 0x01
105 #define CONFIG3_THERM 0x02
107 #define CONFIG4_PINFUNC 0x03
108 #define CONFIG4_MAXDUTY 0x08
109 #define CONFIG4_ATTN_IN10 0x30
110 #define CONFIG4_ATTN_IN43 0xC0
112 #define CONFIG5_TWOSCOMP 0x01
113 #define CONFIG5_TEMPOFFSET 0x02
114 #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
116 /* ADT7475 Settings */
118 #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
119 #define ADT7475_TEMP_COUNT 3
120 #define ADT7475_TACH_COUNT 4
121 #define ADT7475_PWM_COUNT 3
123 /* Macro to read the registers */
125 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
127 /* Macros to easily index the registers */
129 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
130 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
132 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
133 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
134 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
135 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
137 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
138 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
139 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
141 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
142 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
143 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
144 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
145 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
146 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
147 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
149 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
151 enum chips { adt7473, adt7475, adt7476, adt7490 };
153 static const struct i2c_device_id adt7475_id[] = {
154 { "adt7473", adt7473 },
155 { "adt7475", adt7475 },
156 { "adt7476", adt7476 },
157 { "adt7490", adt7490 },
160 MODULE_DEVICE_TABLE(i2c, adt7475_id);
162 struct adt7475_data {
163 struct device *hwmon_dev;
164 struct mutex lock;
166 unsigned long measure_updated;
167 unsigned long limits_updated;
168 char valid;
170 u8 config4;
171 u8 config5;
172 u8 has_voltage;
173 u8 bypass_attn; /* Bypass voltage attenuator */
174 u8 has_pwm2:1;
175 u8 has_fan4:1;
176 u8 has_vid:1;
177 u32 alarms;
178 u16 voltage[3][6];
179 u16 temp[7][3];
180 u16 tach[2][4];
181 u8 pwm[4][3];
182 u8 range[3];
183 u8 pwmctl[3];
184 u8 pwmchan[3];
186 u8 vid;
187 u8 vrm;
190 static struct i2c_driver adt7475_driver;
191 static struct adt7475_data *adt7475_update_device(struct device *dev);
192 static void adt7475_read_hystersis(struct i2c_client *client);
193 static void adt7475_read_pwm(struct i2c_client *client, int index);
195 /* Given a temp value, convert it to register value */
197 static inline u16 temp2reg(struct adt7475_data *data, long val)
199 u16 ret;
201 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
202 val = SENSORS_LIMIT(val, -64000, 191000);
203 ret = (val + 64500) / 1000;
204 } else {
205 val = SENSORS_LIMIT(val, -128000, 127000);
206 if (val < -500)
207 ret = (256500 + val) / 1000;
208 else
209 ret = (val + 500) / 1000;
212 return ret << 2;
215 /* Given a register value, convert it to a real temp value */
217 static inline int reg2temp(struct adt7475_data *data, u16 reg)
219 if (data->config5 & CONFIG5_TWOSCOMP) {
220 if (reg >= 512)
221 return (reg - 1024) * 250;
222 else
223 return reg * 250;
224 } else
225 return (reg - 256) * 250;
228 static inline int tach2rpm(u16 tach)
230 if (tach == 0 || tach == 0xFFFF)
231 return 0;
233 return (90000 * 60) / tach;
236 static inline u16 rpm2tach(unsigned long rpm)
238 if (rpm == 0)
239 return 0;
241 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
244 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
245 static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
246 { 45, 94 }, /* +2.5V */
247 { 175, 525 }, /* Vccp */
248 { 68, 71 }, /* Vcc */
249 { 93, 47 }, /* +5V */
250 { 120, 20 }, /* +12V */
251 { 45, 45 }, /* Vtt */
254 static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
256 const int *r = adt7473_in_scaling[channel];
258 if (bypass_attn & (1 << channel))
259 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
260 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
263 static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
265 const int *r = adt7473_in_scaling[channel];
266 long reg;
268 if (bypass_attn & (1 << channel))
269 reg = (volt * 1024) / 2250;
270 else
271 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
272 return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
275 static u16 adt7475_read_word(struct i2c_client *client, int reg)
277 u16 val;
279 val = i2c_smbus_read_byte_data(client, reg);
280 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
282 return val;
285 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
287 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
288 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
291 /* Find the nearest value in a table - used for pwm frequency and
292 auto temp range */
293 static int find_nearest(long val, const int *array, int size)
295 int i;
297 if (val < array[0])
298 return 0;
300 if (val > array[size - 1])
301 return size - 1;
303 for (i = 0; i < size - 1; i++) {
304 int a, b;
306 if (val > array[i + 1])
307 continue;
309 a = val - array[i];
310 b = array[i + 1] - val;
312 return (a <= b) ? i : i + 1;
315 return 0;
318 static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
319 char *buf)
321 struct adt7475_data *data = adt7475_update_device(dev);
322 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
323 unsigned short val;
325 switch (sattr->nr) {
326 case ALARM:
327 return sprintf(buf, "%d\n",
328 (data->alarms >> sattr->index) & 1);
329 default:
330 val = data->voltage[sattr->nr][sattr->index];
331 return sprintf(buf, "%d\n",
332 reg2volt(sattr->index, val, data->bypass_attn));
336 static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
340 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
341 struct i2c_client *client = to_i2c_client(dev);
342 struct adt7475_data *data = i2c_get_clientdata(client);
343 unsigned char reg;
344 long val;
346 if (strict_strtol(buf, 10, &val))
347 return -EINVAL;
349 mutex_lock(&data->lock);
351 data->voltage[sattr->nr][sattr->index] =
352 volt2reg(sattr->index, val, data->bypass_attn);
354 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
355 if (sattr->nr == MIN)
356 reg = VOLTAGE_MIN_REG(sattr->index);
357 else
358 reg = VOLTAGE_MAX_REG(sattr->index);
359 } else {
360 if (sattr->nr == MIN)
361 reg = REG_VTT_MIN;
362 else
363 reg = REG_VTT_MAX;
366 i2c_smbus_write_byte_data(client, reg,
367 data->voltage[sattr->nr][sattr->index] >> 2);
368 mutex_unlock(&data->lock);
370 return count;
373 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
374 char *buf)
376 struct adt7475_data *data = adt7475_update_device(dev);
377 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
378 int out;
380 switch (sattr->nr) {
381 case HYSTERSIS:
382 mutex_lock(&data->lock);
383 out = data->temp[sattr->nr][sattr->index];
384 if (sattr->index != 1)
385 out = (out >> 4) & 0xF;
386 else
387 out = (out & 0xF);
388 /* Show the value as an absolute number tied to
389 * THERM */
390 out = reg2temp(data, data->temp[THERM][sattr->index]) -
391 out * 1000;
392 mutex_unlock(&data->lock);
393 break;
395 case OFFSET:
396 /* Offset is always 2's complement, regardless of the
397 * setting in CONFIG5 */
398 mutex_lock(&data->lock);
399 out = (s8)data->temp[sattr->nr][sattr->index];
400 if (data->config5 & CONFIG5_TEMPOFFSET)
401 out *= 1000;
402 else
403 out *= 500;
404 mutex_unlock(&data->lock);
405 break;
407 case ALARM:
408 out = (data->alarms >> (sattr->index + 4)) & 1;
409 break;
411 case FAULT:
412 /* Note - only for remote1 and remote2 */
413 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
414 break;
416 default:
417 /* All other temp values are in the configured format */
418 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
421 return sprintf(buf, "%d\n", out);
424 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
425 const char *buf, size_t count)
427 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
428 struct i2c_client *client = to_i2c_client(dev);
429 struct adt7475_data *data = i2c_get_clientdata(client);
430 unsigned char reg = 0;
431 u8 out;
432 int temp;
433 long val;
435 if (strict_strtol(buf, 10, &val))
436 return -EINVAL;
438 mutex_lock(&data->lock);
440 /* We need the config register in all cases for temp <-> reg conv. */
441 data->config5 = adt7475_read(REG_CONFIG5);
443 switch (sattr->nr) {
444 case OFFSET:
445 if (data->config5 & CONFIG5_TEMPOFFSET) {
446 val = SENSORS_LIMIT(val, -63000, 127000);
447 out = data->temp[OFFSET][sattr->index] = val / 1000;
448 } else {
449 val = SENSORS_LIMIT(val, -63000, 64000);
450 out = data->temp[OFFSET][sattr->index] = val / 500;
452 break;
454 case HYSTERSIS:
455 /* The value will be given as an absolute value, turn it
456 into an offset based on THERM */
458 /* Read fresh THERM and HYSTERSIS values from the chip */
459 data->temp[THERM][sattr->index] =
460 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
461 adt7475_read_hystersis(client);
463 temp = reg2temp(data, data->temp[THERM][sattr->index]);
464 val = SENSORS_LIMIT(val, temp - 15000, temp);
465 val = (temp - val) / 1000;
467 if (sattr->index != 1) {
468 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
469 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
470 } else {
471 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
472 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
475 out = data->temp[HYSTERSIS][sattr->index];
476 break;
478 default:
479 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
481 /* We maintain an extra 2 digits of precision for simplicity
482 * - shift those back off before writing the value */
483 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
486 switch (sattr->nr) {
487 case MIN:
488 reg = TEMP_MIN_REG(sattr->index);
489 break;
490 case MAX:
491 reg = TEMP_MAX_REG(sattr->index);
492 break;
493 case OFFSET:
494 reg = TEMP_OFFSET_REG(sattr->index);
495 break;
496 case AUTOMIN:
497 reg = TEMP_TMIN_REG(sattr->index);
498 break;
499 case THERM:
500 reg = TEMP_THERM_REG(sattr->index);
501 break;
502 case HYSTERSIS:
503 if (sattr->index != 2)
504 reg = REG_REMOTE1_HYSTERSIS;
505 else
506 reg = REG_REMOTE2_HYSTERSIS;
508 break;
511 i2c_smbus_write_byte_data(client, reg, out);
513 mutex_unlock(&data->lock);
514 return count;
517 /* Table of autorange values - the user will write the value in millidegrees,
518 and we'll convert it */
519 static const int autorange_table[] = {
520 2000, 2500, 3330, 4000, 5000, 6670, 8000,
521 10000, 13330, 16000, 20000, 26670, 32000, 40000,
522 53330, 80000
525 static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
526 char *buf)
528 struct adt7475_data *data = adt7475_update_device(dev);
529 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
530 int out, val;
532 mutex_lock(&data->lock);
533 out = (data->range[sattr->index] >> 4) & 0x0F;
534 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
535 mutex_unlock(&data->lock);
537 return sprintf(buf, "%d\n", val + autorange_table[out]);
540 static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
543 struct i2c_client *client = to_i2c_client(dev);
544 struct adt7475_data *data = i2c_get_clientdata(client);
545 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
546 int temp;
547 long val;
549 if (strict_strtol(buf, 10, &val))
550 return -EINVAL;
552 mutex_lock(&data->lock);
554 /* Get a fresh copy of the needed registers */
555 data->config5 = adt7475_read(REG_CONFIG5);
556 data->temp[AUTOMIN][sattr->index] =
557 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
558 data->range[sattr->index] =
559 adt7475_read(TEMP_TRANGE_REG(sattr->index));
561 /* The user will write an absolute value, so subtract the start point
562 to figure the range */
563 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
564 val = SENSORS_LIMIT(val, temp + autorange_table[0],
565 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
566 val -= temp;
568 /* Find the nearest table entry to what the user wrote */
569 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
571 data->range[sattr->index] &= ~0xF0;
572 data->range[sattr->index] |= val << 4;
574 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
575 data->range[sattr->index]);
577 mutex_unlock(&data->lock);
578 return count;
581 static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
582 char *buf)
584 struct adt7475_data *data = adt7475_update_device(dev);
585 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
586 int out;
588 if (sattr->nr == ALARM)
589 out = (data->alarms >> (sattr->index + 10)) & 1;
590 else
591 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
593 return sprintf(buf, "%d\n", out);
596 static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
600 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
601 struct i2c_client *client = to_i2c_client(dev);
602 struct adt7475_data *data = i2c_get_clientdata(client);
603 unsigned long val;
605 if (strict_strtoul(buf, 10, &val))
606 return -EINVAL;
608 mutex_lock(&data->lock);
610 data->tach[MIN][sattr->index] = rpm2tach(val);
612 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
613 data->tach[MIN][sattr->index]);
615 mutex_unlock(&data->lock);
616 return count;
619 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
620 char *buf)
622 struct adt7475_data *data = adt7475_update_device(dev);
623 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
625 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
628 static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
629 char *buf)
631 struct adt7475_data *data = adt7475_update_device(dev);
632 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
634 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
637 static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
638 char *buf)
640 struct adt7475_data *data = adt7475_update_device(dev);
641 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
643 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
646 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
647 const char *buf, size_t count)
650 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
651 struct i2c_client *client = to_i2c_client(dev);
652 struct adt7475_data *data = i2c_get_clientdata(client);
653 unsigned char reg = 0;
654 long val;
656 if (strict_strtol(buf, 10, &val))
657 return -EINVAL;
659 mutex_lock(&data->lock);
661 switch (sattr->nr) {
662 case INPUT:
663 /* Get a fresh value for CONTROL */
664 data->pwm[CONTROL][sattr->index] =
665 adt7475_read(PWM_CONFIG_REG(sattr->index));
667 /* If we are not in manual mode, then we shouldn't allow
668 * the user to set the pwm speed */
669 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
670 mutex_unlock(&data->lock);
671 return count;
674 reg = PWM_REG(sattr->index);
675 break;
677 case MIN:
678 reg = PWM_MIN_REG(sattr->index);
679 break;
681 case MAX:
682 reg = PWM_MAX_REG(sattr->index);
683 break;
686 data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
687 i2c_smbus_write_byte_data(client, reg,
688 data->pwm[sattr->nr][sattr->index]);
690 mutex_unlock(&data->lock);
692 return count;
695 /* Called by set_pwmctrl and set_pwmchan */
697 static int hw_set_pwm(struct i2c_client *client, int index,
698 unsigned int pwmctl, unsigned int pwmchan)
700 struct adt7475_data *data = i2c_get_clientdata(client);
701 long val = 0;
703 switch (pwmctl) {
704 case 0:
705 val = 0x03; /* Run at full speed */
706 break;
707 case 1:
708 val = 0x07; /* Manual mode */
709 break;
710 case 2:
711 switch (pwmchan) {
712 case 1:
713 /* Remote1 controls PWM */
714 val = 0x00;
715 break;
716 case 2:
717 /* local controls PWM */
718 val = 0x01;
719 break;
720 case 4:
721 /* remote2 controls PWM */
722 val = 0x02;
723 break;
724 case 6:
725 /* local/remote2 control PWM */
726 val = 0x05;
727 break;
728 case 7:
729 /* All three control PWM */
730 val = 0x06;
731 break;
732 default:
733 return -EINVAL;
735 break;
736 default:
737 return -EINVAL;
740 data->pwmctl[index] = pwmctl;
741 data->pwmchan[index] = pwmchan;
743 data->pwm[CONTROL][index] &= ~0xE0;
744 data->pwm[CONTROL][index] |= (val & 7) << 5;
746 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
747 data->pwm[CONTROL][index]);
749 return 0;
752 static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
753 const char *buf, size_t count)
755 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
756 struct i2c_client *client = to_i2c_client(dev);
757 struct adt7475_data *data = i2c_get_clientdata(client);
758 int r;
759 long val;
761 if (strict_strtol(buf, 10, &val))
762 return -EINVAL;
764 mutex_lock(&data->lock);
765 /* Read Modify Write PWM values */
766 adt7475_read_pwm(client, sattr->index);
767 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
768 if (r)
769 count = r;
770 mutex_unlock(&data->lock);
772 return count;
775 static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
776 const char *buf, size_t count)
778 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
779 struct i2c_client *client = to_i2c_client(dev);
780 struct adt7475_data *data = i2c_get_clientdata(client);
781 int r;
782 long val;
784 if (strict_strtol(buf, 10, &val))
785 return -EINVAL;
787 mutex_lock(&data->lock);
788 /* Read Modify Write PWM values */
789 adt7475_read_pwm(client, sattr->index);
790 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
791 if (r)
792 count = r;
793 mutex_unlock(&data->lock);
795 return count;
798 /* List of frequencies for the PWM */
799 static const int pwmfreq_table[] = {
800 11, 14, 22, 29, 35, 44, 58, 88
803 static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
804 char *buf)
806 struct adt7475_data *data = adt7475_update_device(dev);
807 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
809 return sprintf(buf, "%d\n",
810 pwmfreq_table[data->range[sattr->index] & 7]);
813 static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
814 const char *buf, size_t count)
816 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
817 struct i2c_client *client = to_i2c_client(dev);
818 struct adt7475_data *data = i2c_get_clientdata(client);
819 int out;
820 long val;
822 if (strict_strtol(buf, 10, &val))
823 return -EINVAL;
825 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
827 mutex_lock(&data->lock);
829 data->range[sattr->index] =
830 adt7475_read(TEMP_TRANGE_REG(sattr->index));
831 data->range[sattr->index] &= ~7;
832 data->range[sattr->index] |= out;
834 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
835 data->range[sattr->index]);
837 mutex_unlock(&data->lock);
838 return count;
841 static ssize_t show_pwm_at_crit(struct device *dev,
842 struct device_attribute *devattr, char *buf)
844 struct adt7475_data *data = adt7475_update_device(dev);
845 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
848 static ssize_t set_pwm_at_crit(struct device *dev,
849 struct device_attribute *devattr,
850 const char *buf, size_t count)
852 struct i2c_client *client = to_i2c_client(dev);
853 struct adt7475_data *data = i2c_get_clientdata(client);
854 long val;
856 if (strict_strtol(buf, 10, &val))
857 return -EINVAL;
858 if (val != 0 && val != 1)
859 return -EINVAL;
861 mutex_lock(&data->lock);
862 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
863 if (val)
864 data->config4 |= CONFIG4_MAXDUTY;
865 else
866 data->config4 &= ~CONFIG4_MAXDUTY;
867 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
868 mutex_unlock(&data->lock);
870 return count;
873 static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
874 char *buf)
876 struct adt7475_data *data = dev_get_drvdata(dev);
877 return sprintf(buf, "%d\n", (int)data->vrm);
880 static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
881 const char *buf, size_t count)
883 struct adt7475_data *data = dev_get_drvdata(dev);
884 long val;
886 if (strict_strtol(buf, 10, &val))
887 return -EINVAL;
888 if (val < 0 || val > 255)
889 return -EINVAL;
890 data->vrm = val;
892 return count;
895 static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
896 char *buf)
898 struct adt7475_data *data = adt7475_update_device(dev);
899 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
902 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
903 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
904 set_voltage, MAX, 0);
905 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
906 set_voltage, MIN, 0);
907 static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
908 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
909 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
910 set_voltage, MAX, 1);
911 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
912 set_voltage, MIN, 1);
913 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
914 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
915 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
916 set_voltage, MAX, 2);
917 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
918 set_voltage, MIN, 2);
919 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
920 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
921 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
922 set_voltage, MAX, 3);
923 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
924 set_voltage, MIN, 3);
925 static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
926 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
927 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
928 set_voltage, MAX, 4);
929 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
930 set_voltage, MIN, 4);
931 static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
932 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
933 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
934 set_voltage, MAX, 5);
935 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
936 set_voltage, MIN, 5);
937 static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
938 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
939 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
940 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
941 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
942 MAX, 0);
943 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
944 MIN, 0);
945 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
946 set_temp, OFFSET, 0);
947 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
948 show_temp, set_temp, AUTOMIN, 0);
949 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
950 show_point2, set_point2, 0, 0);
951 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
952 THERM, 0);
953 static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
954 set_temp, HYSTERSIS, 0);
955 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
956 static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
957 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
958 MAX, 1);
959 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
960 MIN, 1);
961 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
962 set_temp, OFFSET, 1);
963 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
964 show_temp, set_temp, AUTOMIN, 1);
965 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
966 show_point2, set_point2, 0, 1);
967 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
968 THERM, 1);
969 static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
970 set_temp, HYSTERSIS, 1);
971 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
972 static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
973 static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
974 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
975 MAX, 2);
976 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
977 MIN, 2);
978 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
979 set_temp, OFFSET, 2);
980 static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
981 show_temp, set_temp, AUTOMIN, 2);
982 static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
983 show_point2, set_point2, 0, 2);
984 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
985 THERM, 2);
986 static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
987 set_temp, HYSTERSIS, 2);
988 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
989 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
990 MIN, 0);
991 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
992 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
993 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
994 MIN, 1);
995 static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
996 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
997 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
998 MIN, 2);
999 static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1000 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1001 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1002 MIN, 3);
1003 static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1004 static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1006 static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1007 set_pwmfreq, INPUT, 0);
1008 static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1009 set_pwmctrl, INPUT, 0);
1010 static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1011 show_pwmchan, set_pwmchan, INPUT, 0);
1012 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1013 set_pwm, MIN, 0);
1014 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1015 set_pwm, MAX, 0);
1016 static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1018 static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1019 set_pwmfreq, INPUT, 1);
1020 static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1021 set_pwmctrl, INPUT, 1);
1022 static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1023 show_pwmchan, set_pwmchan, INPUT, 1);
1024 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1025 set_pwm, MIN, 1);
1026 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1027 set_pwm, MAX, 1);
1028 static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1030 static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1031 set_pwmfreq, INPUT, 2);
1032 static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1033 set_pwmctrl, INPUT, 2);
1034 static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1035 show_pwmchan, set_pwmchan, INPUT, 2);
1036 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1037 set_pwm, MIN, 2);
1038 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1039 set_pwm, MAX, 2);
1041 /* Non-standard name, might need revisiting */
1042 static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1043 show_pwm_at_crit, set_pwm_at_crit);
1045 static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1046 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1048 static struct attribute *adt7475_attrs[] = {
1049 &sensor_dev_attr_in1_input.dev_attr.attr,
1050 &sensor_dev_attr_in1_max.dev_attr.attr,
1051 &sensor_dev_attr_in1_min.dev_attr.attr,
1052 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1053 &sensor_dev_attr_in2_input.dev_attr.attr,
1054 &sensor_dev_attr_in2_max.dev_attr.attr,
1055 &sensor_dev_attr_in2_min.dev_attr.attr,
1056 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1057 &sensor_dev_attr_temp1_input.dev_attr.attr,
1058 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1059 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1060 &sensor_dev_attr_temp1_max.dev_attr.attr,
1061 &sensor_dev_attr_temp1_min.dev_attr.attr,
1062 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1063 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1064 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1065 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1066 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1067 &sensor_dev_attr_temp2_input.dev_attr.attr,
1068 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1069 &sensor_dev_attr_temp2_max.dev_attr.attr,
1070 &sensor_dev_attr_temp2_min.dev_attr.attr,
1071 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1072 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1073 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1074 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1075 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1076 &sensor_dev_attr_temp3_input.dev_attr.attr,
1077 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1078 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1079 &sensor_dev_attr_temp3_max.dev_attr.attr,
1080 &sensor_dev_attr_temp3_min.dev_attr.attr,
1081 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1082 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1083 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1084 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1085 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1086 &sensor_dev_attr_fan1_input.dev_attr.attr,
1087 &sensor_dev_attr_fan1_min.dev_attr.attr,
1088 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1089 &sensor_dev_attr_fan2_input.dev_attr.attr,
1090 &sensor_dev_attr_fan2_min.dev_attr.attr,
1091 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1092 &sensor_dev_attr_fan3_input.dev_attr.attr,
1093 &sensor_dev_attr_fan3_min.dev_attr.attr,
1094 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1095 &sensor_dev_attr_pwm1.dev_attr.attr,
1096 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1097 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1098 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1099 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1100 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1101 &sensor_dev_attr_pwm3.dev_attr.attr,
1102 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1103 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1104 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1105 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1106 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1107 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
1108 NULL,
1111 static struct attribute *fan4_attrs[] = {
1112 &sensor_dev_attr_fan4_input.dev_attr.attr,
1113 &sensor_dev_attr_fan4_min.dev_attr.attr,
1114 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1115 NULL
1118 static struct attribute *pwm2_attrs[] = {
1119 &sensor_dev_attr_pwm2.dev_attr.attr,
1120 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1121 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1122 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1123 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1124 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1125 NULL
1128 static struct attribute *in0_attrs[] = {
1129 &sensor_dev_attr_in0_input.dev_attr.attr,
1130 &sensor_dev_attr_in0_max.dev_attr.attr,
1131 &sensor_dev_attr_in0_min.dev_attr.attr,
1132 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1133 NULL
1136 static struct attribute *in3_attrs[] = {
1137 &sensor_dev_attr_in3_input.dev_attr.attr,
1138 &sensor_dev_attr_in3_max.dev_attr.attr,
1139 &sensor_dev_attr_in3_min.dev_attr.attr,
1140 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1141 NULL
1144 static struct attribute *in4_attrs[] = {
1145 &sensor_dev_attr_in4_input.dev_attr.attr,
1146 &sensor_dev_attr_in4_max.dev_attr.attr,
1147 &sensor_dev_attr_in4_min.dev_attr.attr,
1148 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1149 NULL
1152 static struct attribute *in5_attrs[] = {
1153 &sensor_dev_attr_in5_input.dev_attr.attr,
1154 &sensor_dev_attr_in5_max.dev_attr.attr,
1155 &sensor_dev_attr_in5_min.dev_attr.attr,
1156 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1157 NULL
1160 static struct attribute *vid_attrs[] = {
1161 &dev_attr_cpu0_vid.attr,
1162 &dev_attr_vrm.attr,
1163 NULL
1166 static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1167 static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1168 static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1169 static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1170 static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1171 static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1172 static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1173 static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
1175 static int adt7475_detect(struct i2c_client *client,
1176 struct i2c_board_info *info)
1178 struct i2c_adapter *adapter = client->adapter;
1179 int vendid, devid, devid2;
1180 const char *name;
1182 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1183 return -ENODEV;
1185 vendid = adt7475_read(REG_VENDID);
1186 devid2 = adt7475_read(REG_DEVID2);
1187 if (vendid != 0x41 || /* Analog Devices */
1188 (devid2 & 0xf8) != 0x68)
1189 return -ENODEV;
1191 devid = adt7475_read(REG_DEVID);
1192 if (devid == 0x73)
1193 name = "adt7473";
1194 else if (devid == 0x75 && client->addr == 0x2e)
1195 name = "adt7475";
1196 else if (devid == 0x76)
1197 name = "adt7476";
1198 else if ((devid2 & 0xfc) == 0x6c)
1199 name = "adt7490";
1200 else {
1201 dev_dbg(&adapter->dev,
1202 "Couldn't detect an ADT7473/75/76/90 part at "
1203 "0x%02x\n", (unsigned int)client->addr);
1204 return -ENODEV;
1207 strlcpy(info->type, name, I2C_NAME_SIZE);
1209 return 0;
1212 static void adt7475_remove_files(struct i2c_client *client,
1213 struct adt7475_data *data)
1215 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1216 if (data->has_fan4)
1217 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1218 if (data->has_pwm2)
1219 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1220 if (data->has_voltage & (1 << 0))
1221 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1222 if (data->has_voltage & (1 << 3))
1223 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1224 if (data->has_voltage & (1 << 4))
1225 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1226 if (data->has_voltage & (1 << 5))
1227 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
1228 if (data->has_vid)
1229 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
1232 static int adt7475_probe(struct i2c_client *client,
1233 const struct i2c_device_id *id)
1235 static const char *names[] = {
1236 [adt7473] = "ADT7473",
1237 [adt7475] = "ADT7475",
1238 [adt7476] = "ADT7476",
1239 [adt7490] = "ADT7490",
1242 struct adt7475_data *data;
1243 int i, ret = 0, revision;
1244 u8 config2, config3;
1246 data = kzalloc(sizeof(*data), GFP_KERNEL);
1247 if (data == NULL)
1248 return -ENOMEM;
1250 mutex_init(&data->lock);
1251 i2c_set_clientdata(client, data);
1253 /* Initialize device-specific values */
1254 switch (id->driver_data) {
1255 case adt7476:
1256 data->has_voltage = 0x0e; /* in1 to in3 */
1257 revision = adt7475_read(REG_DEVID2) & 0x07;
1258 break;
1259 case adt7490:
1260 data->has_voltage = 0x3e; /* in1 to in5 */
1261 revision = adt7475_read(REG_DEVID2) & 0x03;
1262 if (revision == 0x03)
1263 revision += adt7475_read(REG_DEVREV2);
1264 break;
1265 default:
1266 data->has_voltage = 0x06; /* in1, in2 */
1267 revision = adt7475_read(REG_DEVID2) & 0x07;
1270 config3 = adt7475_read(REG_CONFIG3);
1271 /* Pin PWM2 may alternatively be used for ALERT output */
1272 if (!(config3 & CONFIG3_SMBALERT))
1273 data->has_pwm2 = 1;
1274 /* Meaning of this bit is inverted for the ADT7473-1 */
1275 if (id->driver_data == adt7473 && revision >= 1)
1276 data->has_pwm2 = !data->has_pwm2;
1278 data->config4 = adt7475_read(REG_CONFIG4);
1279 /* Pin TACH4 may alternatively be used for THERM */
1280 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1281 data->has_fan4 = 1;
1283 /* THERM configuration is more complex on the ADT7476 and ADT7490,
1284 because 2 different pins (TACH4 and +2.5 Vin) can be used for
1285 this function */
1286 if (id->driver_data == adt7490) {
1287 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1288 !(config3 & CONFIG3_THERM))
1289 data->has_fan4 = 1;
1291 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1292 if (!(config3 & CONFIG3_THERM) ||
1293 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1294 data->has_voltage |= (1 << 0); /* in0 */
1297 /* On the ADT7476, the +12V input pin may instead be used as VID5,
1298 and VID pins may alternatively be used as GPIO */
1299 if (id->driver_data == adt7476) {
1300 u8 vid = adt7475_read(REG_VID);
1301 if (!(vid & VID_VIDSEL))
1302 data->has_voltage |= (1 << 4); /* in4 */
1304 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1307 /* Voltage attenuators can be bypassed, globally or individually */
1308 config2 = adt7475_read(REG_CONFIG2);
1309 if (config2 & CONFIG2_ATTN) {
1310 data->bypass_attn = (0x3 << 3) | 0x3;
1311 } else {
1312 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1313 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1315 data->bypass_attn &= data->has_voltage;
1317 /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1318 pwm's which are disabled to manual mode with 0% duty cycle */
1319 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1320 adt7475_read_pwm(client, i);
1322 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1323 if (ret)
1324 goto efree;
1326 /* Features that can be disabled individually */
1327 if (data->has_fan4) {
1328 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1329 if (ret)
1330 goto eremove;
1332 if (data->has_pwm2) {
1333 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1334 if (ret)
1335 goto eremove;
1337 if (data->has_voltage & (1 << 0)) {
1338 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1339 if (ret)
1340 goto eremove;
1342 if (data->has_voltage & (1 << 3)) {
1343 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1344 if (ret)
1345 goto eremove;
1347 if (data->has_voltage & (1 << 4)) {
1348 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1349 if (ret)
1350 goto eremove;
1352 if (data->has_voltage & (1 << 5)) {
1353 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1354 if (ret)
1355 goto eremove;
1357 if (data->has_vid) {
1358 data->vrm = vid_which_vrm();
1359 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1360 if (ret)
1361 goto eremove;
1364 data->hwmon_dev = hwmon_device_register(&client->dev);
1365 if (IS_ERR(data->hwmon_dev)) {
1366 ret = PTR_ERR(data->hwmon_dev);
1367 goto eremove;
1370 dev_info(&client->dev, "%s device, revision %d\n",
1371 names[id->driver_data], revision);
1372 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
1373 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1374 (data->has_voltage & (1 << 0)) ? " in0" : "",
1375 (data->has_voltage & (1 << 4)) ? " in4" : "",
1376 data->has_fan4 ? " fan4" : "",
1377 data->has_pwm2 ? " pwm2" : "",
1378 data->has_vid ? " vid" : "");
1379 if (data->bypass_attn)
1380 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1381 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1382 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1383 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1384 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1386 return 0;
1388 eremove:
1389 adt7475_remove_files(client, data);
1390 efree:
1391 kfree(data);
1392 return ret;
1395 static int adt7475_remove(struct i2c_client *client)
1397 struct adt7475_data *data = i2c_get_clientdata(client);
1399 hwmon_device_unregister(data->hwmon_dev);
1400 adt7475_remove_files(client, data);
1401 kfree(data);
1403 return 0;
1406 static struct i2c_driver adt7475_driver = {
1407 .class = I2C_CLASS_HWMON,
1408 .driver = {
1409 .name = "adt7475",
1411 .probe = adt7475_probe,
1412 .remove = adt7475_remove,
1413 .id_table = adt7475_id,
1414 .detect = adt7475_detect,
1415 .address_list = normal_i2c,
1418 static void adt7475_read_hystersis(struct i2c_client *client)
1420 struct adt7475_data *data = i2c_get_clientdata(client);
1422 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1423 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1424 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1427 static void adt7475_read_pwm(struct i2c_client *client, int index)
1429 struct adt7475_data *data = i2c_get_clientdata(client);
1430 unsigned int v;
1432 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1434 /* Figure out the internal value for pwmctrl and pwmchan
1435 based on the current settings */
1436 v = (data->pwm[CONTROL][index] >> 5) & 7;
1438 if (v == 3)
1439 data->pwmctl[index] = 0;
1440 else if (v == 7)
1441 data->pwmctl[index] = 1;
1442 else if (v == 4) {
1443 /* The fan is disabled - we don't want to
1444 support that, so change to manual mode and
1445 set the duty cycle to 0 instead
1447 data->pwm[INPUT][index] = 0;
1448 data->pwm[CONTROL][index] &= ~0xE0;
1449 data->pwm[CONTROL][index] |= (7 << 5);
1451 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1452 data->pwm[INPUT][index]);
1454 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1455 data->pwm[CONTROL][index]);
1457 data->pwmctl[index] = 1;
1458 } else {
1459 data->pwmctl[index] = 2;
1461 switch (v) {
1462 case 0:
1463 data->pwmchan[index] = 1;
1464 break;
1465 case 1:
1466 data->pwmchan[index] = 2;
1467 break;
1468 case 2:
1469 data->pwmchan[index] = 4;
1470 break;
1471 case 5:
1472 data->pwmchan[index] = 6;
1473 break;
1474 case 6:
1475 data->pwmchan[index] = 7;
1476 break;
1481 static struct adt7475_data *adt7475_update_device(struct device *dev)
1483 struct i2c_client *client = to_i2c_client(dev);
1484 struct adt7475_data *data = i2c_get_clientdata(client);
1485 u16 ext;
1486 int i;
1488 mutex_lock(&data->lock);
1490 /* Measurement values update every 2 seconds */
1491 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1492 !data->valid) {
1493 data->alarms = adt7475_read(REG_STATUS2) << 8;
1494 data->alarms |= adt7475_read(REG_STATUS1);
1496 ext = (adt7475_read(REG_EXTEND2) << 8) |
1497 adt7475_read(REG_EXTEND1);
1498 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1499 if (!(data->has_voltage & (1 << i)))
1500 continue;
1501 data->voltage[INPUT][i] =
1502 (adt7475_read(VOLTAGE_REG(i)) << 2) |
1503 ((ext >> (i * 2)) & 3);
1506 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1507 data->temp[INPUT][i] =
1508 (adt7475_read(TEMP_REG(i)) << 2) |
1509 ((ext >> ((i + 5) * 2)) & 3);
1511 if (data->has_voltage & (1 << 5)) {
1512 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1513 ext = adt7475_read(REG_EXTEND3);
1514 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1515 ((ext >> 4) & 3);
1518 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1519 if (i == 3 && !data->has_fan4)
1520 continue;
1521 data->tach[INPUT][i] =
1522 adt7475_read_word(client, TACH_REG(i));
1525 /* Updated by hw when in auto mode */
1526 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1527 if (i == 1 && !data->has_pwm2)
1528 continue;
1529 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1532 if (data->has_vid)
1533 data->vid = adt7475_read(REG_VID) & 0x3f;
1535 data->measure_updated = jiffies;
1538 /* Limits and settings, should never change update every 60 seconds */
1539 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
1540 !data->valid) {
1541 data->config4 = adt7475_read(REG_CONFIG4);
1542 data->config5 = adt7475_read(REG_CONFIG5);
1544 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1545 if (!(data->has_voltage & (1 << i)))
1546 continue;
1547 /* Adjust values so they match the input precision */
1548 data->voltage[MIN][i] =
1549 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1550 data->voltage[MAX][i] =
1551 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1554 if (data->has_voltage & (1 << 5)) {
1555 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1556 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1559 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1560 /* Adjust values so they match the input precision */
1561 data->temp[MIN][i] =
1562 adt7475_read(TEMP_MIN_REG(i)) << 2;
1563 data->temp[MAX][i] =
1564 adt7475_read(TEMP_MAX_REG(i)) << 2;
1565 data->temp[AUTOMIN][i] =
1566 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1567 data->temp[THERM][i] =
1568 adt7475_read(TEMP_THERM_REG(i)) << 2;
1569 data->temp[OFFSET][i] =
1570 adt7475_read(TEMP_OFFSET_REG(i));
1572 adt7475_read_hystersis(client);
1574 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1575 if (i == 3 && !data->has_fan4)
1576 continue;
1577 data->tach[MIN][i] =
1578 adt7475_read_word(client, TACH_MIN_REG(i));
1581 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1582 if (i == 1 && !data->has_pwm2)
1583 continue;
1584 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1585 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1586 /* Set the channel and control information */
1587 adt7475_read_pwm(client, i);
1590 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1591 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1592 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1594 data->limits_updated = jiffies;
1595 data->valid = 1;
1598 mutex_unlock(&data->lock);
1600 return data;
1603 static int __init sensors_adt7475_init(void)
1605 return i2c_add_driver(&adt7475_driver);
1608 static void __exit sensors_adt7475_exit(void)
1610 i2c_del_driver(&adt7475_driver);
1613 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1614 MODULE_DESCRIPTION("adt7475 driver");
1615 MODULE_LICENSE("GPL");
1617 module_init(sensors_adt7475_init);
1618 module_exit(sensors_adt7475_exit);