aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2011-03-17 19:59:38 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-03-17 19:59:38 -0400
commit19520fc1ee36164808e6f084bd95e8178e2db231 (patch)
treeabf66f8c2a2b35e574e9452673263614fc50c63f /drivers
parentc8def554d031664e984323f6a5d667f070717776 (diff)
parentd668a8b022a201e65ec5e301a9e6dff78987550c (diff)
Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/staging
* 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/staging: (44 commits) hwmon: (lineage-pem): Fix in1 voltage alarm sysfs attributes hwmon/f71882fg: Add support for f71808e hwmon/f71882fg: Add support for f71869f and f71869e hwmon/f71882fg: Add support for f71889ed hwmon/f71882fg: Break out test for auto pwm's controlled by digital readings hwmon/f71882fg: Separate temp beep sysfs attr from the other temp sysfs attr hwmon/f71882fg: Remove bogus temp2_type for certain models hwmon/f71882fg: Make number of temps configurable hwmon/f71882fg: Make creation of in sysfs attributes more generic hwmon/f71882fg: Only allow negative auto point temps if fan_neg_temp is enabled hwmon/f71882fg: Fix temp1 sensor type reporting hwmon: (w83627ehf) Display correct temperature sensor labels for systems with NCT6775F hwmon: (w83627ehf) Add fan debounce support for NCT6775F and NCT6776F hwmon: (w83627ehf) Update Kconfig for W83677HG-B, NCT6775F and NCT6776F hwmon: (w83627ehf) Store rpm instead of raw fan speed data hwmon: (w83627ehf) Use 16 bit fan count registers if supported hwmon: (w83627ehf) Add support for Nuvoton NCT6775F and NCT6776F hwmon: (w83627ehf) Permit enabling SmartFan IV mode if configured at startup hwmon: (w83627ehf) Convert register arrays to 16 bit, and convert access to pointers hwmon: (w83627ehf) Remove references to datasheets which no longer exist ...
Diffstat (limited to 'drivers')
-rw-r--r--drivers/hwmon/Kconfig92
-rw-r--r--drivers/hwmon/Makefile10
-rw-r--r--drivers/hwmon/f71882fg.c522
-rw-r--r--drivers/hwmon/lineage-pem.c586
-rw-r--r--drivers/hwmon/lis3lv02d_spi.c19
-rw-r--r--drivers/hwmon/lm85.c136
-rw-r--r--drivers/hwmon/ltc4151.c256
-rw-r--r--drivers/hwmon/max16064.c91
-rw-r--r--drivers/hwmon/max34440.c199
-rw-r--r--drivers/hwmon/max6639.c653
-rw-r--r--drivers/hwmon/max8688.c158
-rw-r--r--drivers/hwmon/pmbus.c203
-rw-r--r--drivers/hwmon/pmbus.h313
-rw-r--r--drivers/hwmon/pmbus_core.c1658
-rw-r--r--drivers/hwmon/w83627ehf.c1351
15 files changed, 5683 insertions, 564 deletions
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 297bc9a7d6e6..1bfb4439e4e1 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -467,6 +467,17 @@ config SENSORS_JC42
467 This driver can also be built as a module. If so, the module 467 This driver can also be built as a module. If so, the module
468 will be called jc42. 468 will be called jc42.
469 469
470config SENSORS_LINEAGE
471 tristate "Lineage Compact Power Line Power Entry Module"
472 depends on I2C && EXPERIMENTAL
473 help
474 If you say yes here you get support for the Lineage Compact Power Line
475 series of DC/DC and AC/DC converters such as CP1800, CP2000AC,
476 CP2000DC, CP2725, and others.
477
478 This driver can also be built as a module. If so, the module
479 will be called lineage-pem.
480
470config SENSORS_LM63 481config SENSORS_LM63
471 tristate "National Semiconductor LM63 and LM64" 482 tristate "National Semiconductor LM63 and LM64"
472 depends on I2C 483 depends on I2C
@@ -625,6 +636,17 @@ config SENSORS_LM93
625 This driver can also be built as a module. If so, the module 636 This driver can also be built as a module. If so, the module
626 will be called lm93. 637 will be called lm93.
627 638
639config SENSORS_LTC4151
640 tristate "Linear Technology LTC4151"
641 depends on I2C
642 default n
643 help
644 If you say yes here you get support for Linear Technology LTC4151
645 High Voltage I2C Current and Voltage Monitor interface.
646
647 This driver can also be built as a module. If so, the module will
648 be called ltc4151.
649
628config SENSORS_LTC4215 650config SENSORS_LTC4215
629 tristate "Linear Technology LTC4215" 651 tristate "Linear Technology LTC4215"
630 depends on I2C && EXPERIMENTAL 652 depends on I2C && EXPERIMENTAL
@@ -685,6 +707,16 @@ config SENSORS_MAX1619
685 This driver can also be built as a module. If so, the module 707 This driver can also be built as a module. If so, the module
686 will be called max1619. 708 will be called max1619.
687 709
710config SENSORS_MAX6639
711 tristate "Maxim MAX6639 sensor chip"
712 depends on I2C && EXPERIMENTAL
713 help
714 If you say yes here you get support for the MAX6639
715 sensor chips.
716
717 This driver can also be built as a module. If so, the module
718 will be called max6639.
719
688config SENSORS_MAX6650 720config SENSORS_MAX6650
689 tristate "Maxim MAX6650 sensor chip" 721 tristate "Maxim MAX6650 sensor chip"
690 depends on I2C && EXPERIMENTAL 722 depends on I2C && EXPERIMENTAL
@@ -735,6 +767,61 @@ config SENSORS_PCF8591
735 These devices are hard to detect and rarely found on mainstream 767 These devices are hard to detect and rarely found on mainstream
736 hardware. If unsure, say N. 768 hardware. If unsure, say N.
737 769
770config PMBUS
771 tristate "PMBus support"
772 depends on I2C && EXPERIMENTAL
773 default n
774 help
775 Say yes here if you want to enable PMBus support.
776
777 This driver can also be built as a module. If so, the module will
778 be called pmbus_core.
779
780if PMBUS
781
782config SENSORS_PMBUS
783 tristate "Generic PMBus devices"
784 default n
785 help
786 If you say yes here you get hardware monitoring support for generic
787 PMBus devices, including but not limited to BMR450, BMR451, BMR453,
788 BMR454, and LTC2978.
789
790 This driver can also be built as a module. If so, the module will
791 be called pmbus.
792
793config SENSORS_MAX16064
794 tristate "Maxim MAX16064"
795 default n
796 help
797 If you say yes here you get hardware monitoring support for Maxim
798 MAX16064.
799
800 This driver can also be built as a module. If so, the module will
801 be called max16064.
802
803config SENSORS_MAX34440
804 tristate "Maxim MAX34440/MAX34441"
805 default n
806 help
807 If you say yes here you get hardware monitoring support for Maxim
808 MAX34440 and MAX34441.
809
810 This driver can also be built as a module. If so, the module will
811 be called max34440.
812
813config SENSORS_MAX8688
814 tristate "Maxim MAX8688"
815 default n
816 help
817 If you say yes here you get hardware monitoring support for Maxim
818 MAX8688.
819
820 This driver can also be built as a module. If so, the module will
821 be called max8688.
822
823endif # PMBUS
824
738config SENSORS_SHT15 825config SENSORS_SHT15
739 tristate "Sensiron humidity and temperature sensors. SHT15 and compat." 826 tristate "Sensiron humidity and temperature sensors. SHT15 and compat."
740 depends on GENERIC_GPIO 827 depends on GENERIC_GPIO
@@ -1083,7 +1170,7 @@ config SENSORS_W83627HF
1083 will be called w83627hf. 1170 will be called w83627hf.
1084 1171
1085config SENSORS_W83627EHF 1172config SENSORS_W83627EHF
1086 tristate "Winbond W83627EHF/EHG/DHG, W83667HG" 1173 tristate "Winbond W83627EHF/EHG/DHG, W83667HG, NCT6775F, NCT6776F"
1087 select HWMON_VID 1174 select HWMON_VID
1088 help 1175 help
1089 If you say yes here you get support for the hardware 1176 If you say yes here you get support for the hardware
@@ -1094,7 +1181,8 @@ config SENSORS_W83627EHF
1094 chip suited for specific Intel processors that use PECI such as 1181 chip suited for specific Intel processors that use PECI such as
1095 the Core 2 Duo. 1182 the Core 2 Duo.
1096 1183
1097 This driver also supports the W83667HG chip. 1184 This driver also supports Nuvoton W83667HG, W83667HG-B, NCT6775F
1185 (also known as W83667HG-I), and NCT6776F.
1098 1186
1099 This driver can also be built as a module. If so, the module 1187 This driver can also be built as a module. If so, the module
1100 will be called w83627ehf. 1188 will be called w83627ehf.
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index dde02d99c238..bd0410e4b44f 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -62,6 +62,7 @@ obj-$(CONFIG_SENSORS_JC42) += jc42.o
62obj-$(CONFIG_SENSORS_JZ4740) += jz4740-hwmon.o 62obj-$(CONFIG_SENSORS_JZ4740) += jz4740-hwmon.o
63obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o 63obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o
64obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o 64obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o
65obj-$(CONFIG_SENSORS_LINEAGE) += lineage-pem.o
65obj-$(CONFIG_SENSORS_LIS3LV02D) += lis3lv02d.o hp_accel.o 66obj-$(CONFIG_SENSORS_LIS3LV02D) += lis3lv02d.o hp_accel.o
66obj-$(CONFIG_SENSORS_LIS3_SPI) += lis3lv02d.o lis3lv02d_spi.o 67obj-$(CONFIG_SENSORS_LIS3_SPI) += lis3lv02d.o lis3lv02d_spi.o
67obj-$(CONFIG_SENSORS_LIS3_I2C) += lis3lv02d.o lis3lv02d_i2c.o 68obj-$(CONFIG_SENSORS_LIS3_I2C) += lis3lv02d.o lis3lv02d_i2c.o
@@ -79,11 +80,13 @@ obj-$(CONFIG_SENSORS_LM90) += lm90.o
79obj-$(CONFIG_SENSORS_LM92) += lm92.o 80obj-$(CONFIG_SENSORS_LM92) += lm92.o
80obj-$(CONFIG_SENSORS_LM93) += lm93.o 81obj-$(CONFIG_SENSORS_LM93) += lm93.o
81obj-$(CONFIG_SENSORS_LM95241) += lm95241.o 82obj-$(CONFIG_SENSORS_LM95241) += lm95241.o
83obj-$(CONFIG_SENSORS_LTC4151) += ltc4151.o
82obj-$(CONFIG_SENSORS_LTC4215) += ltc4215.o 84obj-$(CONFIG_SENSORS_LTC4215) += ltc4215.o
83obj-$(CONFIG_SENSORS_LTC4245) += ltc4245.o 85obj-$(CONFIG_SENSORS_LTC4245) += ltc4245.o
84obj-$(CONFIG_SENSORS_LTC4261) += ltc4261.o 86obj-$(CONFIG_SENSORS_LTC4261) += ltc4261.o
85obj-$(CONFIG_SENSORS_MAX1111) += max1111.o 87obj-$(CONFIG_SENSORS_MAX1111) += max1111.o
86obj-$(CONFIG_SENSORS_MAX1619) += max1619.o 88obj-$(CONFIG_SENSORS_MAX1619) += max1619.o
89obj-$(CONFIG_SENSORS_MAX6639) += max6639.o
87obj-$(CONFIG_SENSORS_MAX6650) += max6650.o 90obj-$(CONFIG_SENSORS_MAX6650) += max6650.o
88obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o 91obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
89obj-$(CONFIG_SENSORS_PC87360) += pc87360.o 92obj-$(CONFIG_SENSORS_PC87360) += pc87360.o
@@ -112,6 +115,13 @@ obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
112obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o 115obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
113obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o 116obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
114 117
118# PMBus drivers
119obj-$(CONFIG_PMBUS) += pmbus_core.o
120obj-$(CONFIG_SENSORS_PMBUS) += pmbus.o
121obj-$(CONFIG_SENSORS_MAX16064) += max16064.o
122obj-$(CONFIG_SENSORS_MAX34440) += max34440.o
123obj-$(CONFIG_SENSORS_MAX8688) += max8688.o
124
115ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y) 125ifeq ($(CONFIG_HWMON_DEBUG_CHIP),y)
116EXTRA_CFLAGS += -DDEBUG 126EXTRA_CFLAGS += -DDEBUG
117endif 127endif
diff --git a/drivers/hwmon/f71882fg.c b/drivers/hwmon/f71882fg.c
index 6e06019015a5..a4d430ee7e20 100644
--- a/drivers/hwmon/f71882fg.c
+++ b/drivers/hwmon/f71882fg.c
@@ -1,6 +1,6 @@
1/*************************************************************************** 1/***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> * 2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007-2009 Hans de Goede <hdegoede@redhat.com> * 3 * Copyright (C) 2007-2011 Hans de Goede <hdegoede@redhat.com> *
4 * * 4 * *
5 * This program is free software; you can redistribute it and/or modify * 5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by * 6 * it under the terms of the GNU General Public License as published by *
@@ -47,22 +47,23 @@
47#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */ 47#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
48 48
49#define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */ 49#define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
50#define SIO_F71808E_ID 0x0901 /* Chipset ID */
50#define SIO_F71858_ID 0x0507 /* Chipset ID */ 51#define SIO_F71858_ID 0x0507 /* Chipset ID */
51#define SIO_F71862_ID 0x0601 /* Chipset ID */ 52#define SIO_F71862_ID 0x0601 /* Chipset ID */
53#define SIO_F71869_ID 0x0814 /* Chipset ID */
52#define SIO_F71882_ID 0x0541 /* Chipset ID */ 54#define SIO_F71882_ID 0x0541 /* Chipset ID */
53#define SIO_F71889_ID 0x0723 /* Chipset ID */ 55#define SIO_F71889_ID 0x0723 /* Chipset ID */
56#define SIO_F71889E_ID 0x0909 /* Chipset ID */
54#define SIO_F8000_ID 0x0581 /* Chipset ID */ 57#define SIO_F8000_ID 0x0581 /* Chipset ID */
55 58
56#define REGION_LENGTH 8 59#define REGION_LENGTH 8
57#define ADDR_REG_OFFSET 5 60#define ADDR_REG_OFFSET 5
58#define DATA_REG_OFFSET 6 61#define DATA_REG_OFFSET 6
59 62
60#define F71882FG_REG_PECI 0x0A 63#define F71882FG_REG_IN_STATUS 0x12 /* f7188x only */
61 64#define F71882FG_REG_IN_BEEP 0x13 /* f7188x only */
62#define F71882FG_REG_IN_STATUS 0x12 /* f71882fg only */
63#define F71882FG_REG_IN_BEEP 0x13 /* f71882fg only */
64#define F71882FG_REG_IN(nr) (0x20 + (nr)) 65#define F71882FG_REG_IN(nr) (0x20 + (nr))
65#define F71882FG_REG_IN1_HIGH 0x32 /* f71882fg only */ 66#define F71882FG_REG_IN1_HIGH 0x32 /* f7188x only */
66 67
67#define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr))) 68#define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
68#define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr))) 69#define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
@@ -86,28 +87,71 @@
86 87
87#define F71882FG_REG_FAN_HYST(nr) (0x98 + (nr)) 88#define F71882FG_REG_FAN_HYST(nr) (0x98 + (nr))
88 89
90#define F71882FG_REG_FAN_FAULT_T 0x9F
91#define F71882FG_FAN_NEG_TEMP_EN 0x20
92#define F71882FG_FAN_PROG_SEL 0x80
93
89#define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm))) 94#define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
90#define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm))) 95#define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
91#define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr)) 96#define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
92 97
93#define F71882FG_REG_START 0x01 98#define F71882FG_REG_START 0x01
94 99
100#define F71882FG_MAX_INS 9
101
95#define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */ 102#define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
96 103
97static unsigned short force_id; 104static unsigned short force_id;
98module_param(force_id, ushort, 0); 105module_param(force_id, ushort, 0);
99MODULE_PARM_DESC(force_id, "Override the detected device ID"); 106MODULE_PARM_DESC(force_id, "Override the detected device ID");
100 107
101enum chips { f71858fg, f71862fg, f71882fg, f71889fg, f8000 }; 108enum chips { f71808e, f71858fg, f71862fg, f71869, f71882fg, f71889fg,
109 f71889ed, f8000 };
102 110
103static const char *f71882fg_names[] = { 111static const char *f71882fg_names[] = {
112 "f71808e",
104 "f71858fg", 113 "f71858fg",
105 "f71862fg", 114 "f71862fg",
115 "f71869", /* Both f71869f and f71869e, reg. compatible and same id */
106 "f71882fg", 116 "f71882fg",
107 "f71889fg", 117 "f71889fg",
118 "f71889ed",
108 "f8000", 119 "f8000",
109}; 120};
110 121
122static const char f71882fg_has_in[8][F71882FG_MAX_INS] = {
123 { 1, 1, 1, 1, 1, 1, 0, 1, 1 }, /* f71808e */
124 { 1, 1, 1, 0, 0, 0, 0, 0, 0 }, /* f71858fg */
125 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71862fg */
126 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71869 */
127 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71882fg */
128 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71889fg */
129 { 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* f71889ed */
130 { 1, 1, 1, 0, 0, 0, 0, 0, 0 }, /* f8000 */
131};
132
133static const char f71882fg_has_in1_alarm[8] = {
134 0, /* f71808e */
135 0, /* f71858fg */
136 0, /* f71862fg */
137 0, /* f71869 */
138 1, /* f71882fg */
139 1, /* f71889fg */
140 1, /* f71889ed */
141 0, /* f8000 */
142};
143
144static const char f71882fg_has_beep[8] = {
145 0, /* f71808e */
146 0, /* f71858fg */
147 1, /* f71862fg */
148 1, /* f71869 */
149 1, /* f71882fg */
150 1, /* f71889fg */
151 1, /* f71889ed */
152 0, /* f8000 */
153};
154
111static struct platform_device *f71882fg_pdev; 155static struct platform_device *f71882fg_pdev;
112 156
113/* Super-I/O Function prototypes */ 157/* Super-I/O Function prototypes */
@@ -129,11 +173,12 @@ struct f71882fg_data {
129 struct mutex update_lock; 173 struct mutex update_lock;
130 int temp_start; /* temp numbering start (0 or 1) */ 174 int temp_start; /* temp numbering start (0 or 1) */
131 char valid; /* !=0 if following fields are valid */ 175 char valid; /* !=0 if following fields are valid */
176 char auto_point_temp_signed;
132 unsigned long last_updated; /* In jiffies */ 177 unsigned long last_updated; /* In jiffies */
133 unsigned long last_limits; /* In jiffies */ 178 unsigned long last_limits; /* In jiffies */
134 179
135 /* Register Values */ 180 /* Register Values */
136 u8 in[9]; 181 u8 in[F71882FG_MAX_INS];
137 u8 in1_max; 182 u8 in1_max;
138 u8 in_status; 183 u8 in_status;
139 u8 in_beep; 184 u8 in_beep;
@@ -142,7 +187,7 @@ struct f71882fg_data {
142 u16 fan_full_speed[4]; 187 u16 fan_full_speed[4];
143 u8 fan_status; 188 u8 fan_status;
144 u8 fan_beep; 189 u8 fan_beep;
145 /* Note: all models have only 3 temperature channels, but on some 190 /* Note: all models have max 3 temperature channels, but on some
146 they are addressed as 0-2 and on others as 1-3, so for coding 191 they are addressed as 0-2 and on others as 1-3, so for coding
147 convenience we reserve space for 4 channels */ 192 convenience we reserve space for 4 channels */
148 u16 temp[4]; 193 u16 temp[4];
@@ -262,13 +307,9 @@ static struct platform_driver f71882fg_driver = {
262 307
263static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 308static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
264 309
265/* Temp and in attr for the f71858fg, the f71858fg is special as it 310/* Temp attr for the f71858fg, the f71858fg is special as it has its
266 has its temperature indexes start at 0 (the others start at 1) and 311 temperature indexes start at 0 (the others start at 1) */
267 it only has 3 voltage inputs */ 312static struct sensor_device_attribute_2 f71858fg_temp_attr[] = {
268static struct sensor_device_attribute_2 f71858fg_in_temp_attr[] = {
269 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
270 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
271 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
272 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0), 313 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
273 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max, 314 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
274 store_temp_max, 0, 0), 315 store_temp_max, 0, 0),
@@ -292,7 +333,6 @@ static struct sensor_device_attribute_2 f71858fg_in_temp_attr[] = {
292 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 333 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
293 0, 1), 334 0, 1),
294 SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5), 335 SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
295 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 1),
296 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1), 336 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
297 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2), 337 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
298 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max, 338 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
@@ -308,17 +348,8 @@ static struct sensor_device_attribute_2 f71858fg_in_temp_attr[] = {
308 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2), 348 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
309}; 349};
310 350
311/* Temp and in attr common to the f71862fg, f71882fg and f71889fg */ 351/* Temp attr for the standard models */
312static struct sensor_device_attribute_2 fxxxx_in_temp_attr[] = { 352static struct sensor_device_attribute_2 fxxxx_temp_attr[3][9] = { {
313 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
314 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
315 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
316 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
317 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
318 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
319 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
320 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
321 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
322 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1), 353 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
323 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max, 354 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
324 store_temp_max, 0, 1), 355 store_temp_max, 0, 1),
@@ -328,17 +359,14 @@ static struct sensor_device_attribute_2 fxxxx_in_temp_attr[] = {
328 the max and crit alarms separately and lm_sensors v2 depends on the 359 the max and crit alarms separately and lm_sensors v2 depends on the
329 presence of temp#_alarm files. The same goes for temp2/3 _alarm. */ 360 presence of temp#_alarm files. The same goes for temp2/3 _alarm. */
330 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1), 361 SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
331 SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
332 store_temp_beep, 0, 1),
333 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit, 362 SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
334 store_temp_crit, 0, 1), 363 store_temp_crit, 0, 1),
335 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 364 SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
336 0, 1), 365 0, 1),
337 SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5), 366 SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
338 SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
339 store_temp_beep, 0, 5),
340 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1), 367 SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
341 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1), 368 SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
369}, {
342 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2), 370 SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
343 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max, 371 SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
344 store_temp_max, 0, 2), 372 store_temp_max, 0, 2),
@@ -346,17 +374,14 @@ static struct sensor_device_attribute_2 fxxxx_in_temp_attr[] = {
346 store_temp_max_hyst, 0, 2), 374 store_temp_max_hyst, 0, 2),
347 /* Should be temp2_max_alarm, see temp1_alarm note */ 375 /* Should be temp2_max_alarm, see temp1_alarm note */
348 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2), 376 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
349 SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
350 store_temp_beep, 0, 2),
351 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit, 377 SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
352 store_temp_crit, 0, 2), 378 store_temp_crit, 0, 2),
353 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 379 SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
354 0, 2), 380 0, 2),
355 SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6), 381 SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
356 SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
357 store_temp_beep, 0, 6),
358 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2), 382 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
359 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2), 383 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
384}, {
360 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3), 385 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
361 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max, 386 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
362 store_temp_max, 0, 3), 387 store_temp_max, 0, 3),
@@ -364,37 +389,39 @@ static struct sensor_device_attribute_2 fxxxx_in_temp_attr[] = {
364 store_temp_max_hyst, 0, 3), 389 store_temp_max_hyst, 0, 3),
365 /* Should be temp3_max_alarm, see temp1_alarm note */ 390 /* Should be temp3_max_alarm, see temp1_alarm note */
366 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3), 391 SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
367 SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
368 store_temp_beep, 0, 3),
369 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit, 392 SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
370 store_temp_crit, 0, 3), 393 store_temp_crit, 0, 3),
371 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 394 SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
372 0, 3), 395 0, 3),
373 SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 7), 396 SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 7),
374 SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
375 store_temp_beep, 0, 7),
376 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3), 397 SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
377 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3), 398 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
378}; 399} };
379 400
380/* For models with in1 alarm capability */ 401/* Temp attr for models which can beep on temp alarm */
381static struct sensor_device_attribute_2 fxxxx_in1_alarm_attr[] = { 402static struct sensor_device_attribute_2 fxxxx_temp_beep_attr[3][2] = { {
382 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max, 403 SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
383 0, 1), 404 store_temp_beep, 0, 1),
384 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep, 405 SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
385 0, 1), 406 store_temp_beep, 0, 5),
386 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1), 407}, {
387}; 408 SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
409 store_temp_beep, 0, 2),
410 SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
411 store_temp_beep, 0, 6),
412}, {
413 SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
414 store_temp_beep, 0, 3),
415 SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
416 store_temp_beep, 0, 7),
417} };
388 418
389/* Temp and in attr for the f8000 419/* Temp attr for the f8000
390 Note on the f8000 temp_ovt (crit) is used as max, and temp_high (max) 420 Note on the f8000 temp_ovt (crit) is used as max, and temp_high (max)
391 is used as hysteresis value to clear alarms 421 is used as hysteresis value to clear alarms
392 Also like the f71858fg its temperature indexes start at 0 422 Also like the f71858fg its temperature indexes start at 0
393 */ 423 */
394static struct sensor_device_attribute_2 f8000_in_temp_attr[] = { 424static struct sensor_device_attribute_2 f8000_temp_attr[] = {
395 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
396 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
397 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
398 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0), 425 SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
399 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_crit, 426 SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_crit,
400 store_temp_crit, 0, 0), 427 store_temp_crit, 0, 0),
@@ -408,7 +435,6 @@ static struct sensor_device_attribute_2 f8000_in_temp_attr[] = {
408 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max, 435 SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
409 store_temp_max, 0, 1), 436 store_temp_max, 0, 1),
410 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5), 437 SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
411 SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 1),
412 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1), 438 SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
413 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2), 439 SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
414 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_crit, 440 SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_crit,
@@ -419,6 +445,28 @@ static struct sensor_device_attribute_2 f8000_in_temp_attr[] = {
419 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2), 445 SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
420}; 446};
421 447
448/* in attr for all models */
449static struct sensor_device_attribute_2 fxxxx_in_attr[] = {
450 SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
451 SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
452 SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
453 SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
454 SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
455 SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
456 SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
457 SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
458 SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
459};
460
461/* For models with in1 alarm capability */
462static struct sensor_device_attribute_2 fxxxx_in1_alarm_attr[] = {
463 SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
464 0, 1),
465 SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
466 0, 1),
467 SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
468};
469
422/* Fan / PWM attr common to all models */ 470/* Fan / PWM attr common to all models */
423static struct sensor_device_attribute_2 fxxxx_fan_attr[4][6] = { { 471static struct sensor_device_attribute_2 fxxxx_fan_attr[4][6] = { {
424 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0), 472 SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
@@ -479,7 +527,7 @@ static struct sensor_device_attribute_2 fxxxx_fan_beep_attr[] = {
479}; 527};
480 528
481/* PWM attr for the f71862fg, fewer pwms and fewer zones per pwm than the 529/* PWM attr for the f71862fg, fewer pwms and fewer zones per pwm than the
482 f71858fg / f71882fg / f71889fg */ 530 standard models */
483static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[] = { 531static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[] = {
484 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR, 532 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
485 show_pwm_auto_point_channel, 533 show_pwm_auto_point_channel,
@@ -548,7 +596,87 @@ static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[] = {
548 show_pwm_auto_point_temp_hyst, NULL, 3, 2), 596 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
549}; 597};
550 598
551/* PWM attr common to the f71858fg, f71882fg and f71889fg */ 599/* PWM attr for the f71808e/f71869, almost identical to the f71862fg, but the
600 pwm setting when the temperature is above the pwmX_auto_point1_temp can be
601 programmed instead of being hardcoded to 0xff */
602static struct sensor_device_attribute_2 f71869_auto_pwm_attr[] = {
603 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
604 show_pwm_auto_point_channel,
605 store_pwm_auto_point_channel, 0, 0),
606 SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
607 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
608 0, 0),
609 SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
610 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
611 1, 0),
612 SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
613 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
614 4, 0),
615 SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
616 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
617 0, 0),
618 SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
619 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
620 3, 0),
621 SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
622 show_pwm_auto_point_temp_hyst,
623 store_pwm_auto_point_temp_hyst,
624 0, 0),
625 SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
626 show_pwm_auto_point_temp_hyst, NULL, 3, 0),
627
628 SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
629 show_pwm_auto_point_channel,
630 store_pwm_auto_point_channel, 0, 1),
631 SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
632 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
633 0, 1),
634 SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
635 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
636 1, 1),
637 SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
638 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
639 4, 1),
640 SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
641 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
642 0, 1),
643 SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
644 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
645 3, 1),
646 SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
647 show_pwm_auto_point_temp_hyst,
648 store_pwm_auto_point_temp_hyst,
649 0, 1),
650 SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
651 show_pwm_auto_point_temp_hyst, NULL, 3, 1),
652
653 SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
654 show_pwm_auto_point_channel,
655 store_pwm_auto_point_channel, 0, 2),
656 SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
657 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
658 0, 2),
659 SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
660 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
661 1, 2),
662 SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
663 show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
664 4, 2),
665 SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
666 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
667 0, 2),
668 SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
669 show_pwm_auto_point_temp, store_pwm_auto_point_temp,
670 3, 2),
671 SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
672 show_pwm_auto_point_temp_hyst,
673 store_pwm_auto_point_temp_hyst,
674 0, 2),
675 SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
676 show_pwm_auto_point_temp_hyst, NULL, 3, 2),
677};
678
679/* PWM attr for the standard models */
552static struct sensor_device_attribute_2 fxxxx_auto_pwm_attr[4][14] = { { 680static struct sensor_device_attribute_2 fxxxx_auto_pwm_attr[4][14] = { {
553 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR, 681 SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
554 show_pwm_auto_point_channel, 682 show_pwm_auto_point_channel,
@@ -943,16 +1071,16 @@ static u16 f71882fg_read_temp(struct f71882fg_data *data, int nr)
943static struct f71882fg_data *f71882fg_update_device(struct device *dev) 1071static struct f71882fg_data *f71882fg_update_device(struct device *dev)
944{ 1072{
945 struct f71882fg_data *data = dev_get_drvdata(dev); 1073 struct f71882fg_data *data = dev_get_drvdata(dev);
946 int nr, reg = 0, reg2; 1074 int nr, reg, point;
947 int nr_fans = (data->type == f71882fg) ? 4 : 3; 1075 int nr_fans = (data->type == f71882fg) ? 4 : 3;
948 int nr_ins = (data->type == f71858fg || data->type == f8000) ? 3 : 9; 1076 int nr_temps = (data->type == f71808e) ? 2 : 3;
949 1077
950 mutex_lock(&data->update_lock); 1078 mutex_lock(&data->update_lock);
951 1079
952 /* Update once every 60 seconds */ 1080 /* Update once every 60 seconds */
953 if (time_after(jiffies, data->last_limits + 60 * HZ) || 1081 if (time_after(jiffies, data->last_limits + 60 * HZ) ||
954 !data->valid) { 1082 !data->valid) {
955 if (data->type == f71882fg || data->type == f71889fg) { 1083 if (f71882fg_has_in1_alarm[data->type]) {
956 data->in1_max = 1084 data->in1_max =
957 f71882fg_read8(data, F71882FG_REG_IN1_HIGH); 1085 f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
958 data->in_beep = 1086 data->in_beep =
@@ -960,7 +1088,8 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
960 } 1088 }
961 1089
962 /* Get High & boundary temps*/ 1090 /* Get High & boundary temps*/
963 for (nr = data->temp_start; nr < 3 + data->temp_start; nr++) { 1091 for (nr = data->temp_start; nr < nr_temps + data->temp_start;
1092 nr++) {
964 data->temp_ovt[nr] = f71882fg_read8(data, 1093 data->temp_ovt[nr] = f71882fg_read8(data,
965 F71882FG_REG_TEMP_OVT(nr)); 1094 F71882FG_REG_TEMP_OVT(nr));
966 data->temp_high[nr] = f71882fg_read8(data, 1095 data->temp_high[nr] = f71882fg_read8(data,
@@ -973,44 +1102,19 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
973 data->temp_hyst[1] = f71882fg_read8(data, 1102 data->temp_hyst[1] = f71882fg_read8(data,
974 F71882FG_REG_TEMP_HYST(1)); 1103 F71882FG_REG_TEMP_HYST(1));
975 } 1104 }
1105 /* All but the f71858fg / f8000 have this register */
1106 if ((data->type != f71858fg) && (data->type != f8000)) {
1107 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
1108 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
1109 data->temp_type[2] = (reg & 0x04) ? 2 : 4;
1110 data->temp_type[3] = (reg & 0x08) ? 2 : 4;
1111 }
976 1112
977 if (data->type == f71862fg || data->type == f71882fg || 1113 if (f71882fg_has_beep[data->type]) {
978 data->type == f71889fg) {
979 data->fan_beep = f71882fg_read8(data, 1114 data->fan_beep = f71882fg_read8(data,
980 F71882FG_REG_FAN_BEEP); 1115 F71882FG_REG_FAN_BEEP);
981 data->temp_beep = f71882fg_read8(data, 1116 data->temp_beep = f71882fg_read8(data,
982 F71882FG_REG_TEMP_BEEP); 1117 F71882FG_REG_TEMP_BEEP);
983 /* Have to hardcode type, because temp1 is special */
984 reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
985 data->temp_type[2] = (reg & 0x04) ? 2 : 4;
986 data->temp_type[3] = (reg & 0x08) ? 2 : 4;
987 }
988 /* Determine temp index 1 sensor type */
989 if (data->type == f71889fg) {
990 reg2 = f71882fg_read8(data, F71882FG_REG_START);
991 switch ((reg2 & 0x60) >> 5) {
992 case 0x00: /* BJT / Thermistor */
993 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
994 break;
995 case 0x01: /* AMDSI */
996 data->temp_type[1] = 5;
997 break;
998 case 0x02: /* PECI */
999 case 0x03: /* Ibex Peak ?? Report as PECI for now */
1000 data->temp_type[1] = 6;
1001 break;
1002 }
1003 } else {
1004 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
1005 if ((reg2 & 0x03) == 0x01)
1006 data->temp_type[1] = 6; /* PECI */
1007 else if ((reg2 & 0x03) == 0x02)
1008 data->temp_type[1] = 5; /* AMDSI */
1009 else if (data->type == f71862fg ||
1010 data->type == f71882fg)
1011 data->temp_type[1] = (reg & 0x02) ? 2 : 4;
1012 else /* f71858fg and f8000 only support BJT */
1013 data->temp_type[1] = 2;
1014 } 1118 }
1015 1119
1016 data->pwm_enable = f71882fg_read8(data, 1120 data->pwm_enable = f71882fg_read8(data,
@@ -1025,8 +1129,8 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1025 f71882fg_read8(data, 1129 f71882fg_read8(data,
1026 F71882FG_REG_POINT_MAPPING(nr)); 1130 F71882FG_REG_POINT_MAPPING(nr));
1027 1131
1028 if (data->type != f71862fg) { 1132 switch (data->type) {
1029 int point; 1133 default:
1030 for (point = 0; point < 5; point++) { 1134 for (point = 0; point < 5; point++) {
1031 data->pwm_auto_point_pwm[nr][point] = 1135 data->pwm_auto_point_pwm[nr][point] =
1032 f71882fg_read8(data, 1136 f71882fg_read8(data,
@@ -1039,7 +1143,14 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1039 F71882FG_REG_POINT_TEMP 1143 F71882FG_REG_POINT_TEMP
1040 (nr, point)); 1144 (nr, point));
1041 } 1145 }
1042 } else { 1146 break;
1147 case f71808e:
1148 case f71869:
1149 data->pwm_auto_point_pwm[nr][0] =
1150 f71882fg_read8(data,
1151 F71882FG_REG_POINT_PWM(nr, 0));
1152 /* Fall through */
1153 case f71862fg:
1043 data->pwm_auto_point_pwm[nr][1] = 1154 data->pwm_auto_point_pwm[nr][1] =
1044 f71882fg_read8(data, 1155 f71882fg_read8(data,
1045 F71882FG_REG_POINT_PWM 1156 F71882FG_REG_POINT_PWM
@@ -1056,6 +1167,7 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1056 f71882fg_read8(data, 1167 f71882fg_read8(data,
1057 F71882FG_REG_POINT_TEMP 1168 F71882FG_REG_POINT_TEMP
1058 (nr, 3)); 1169 (nr, 3));
1170 break;
1059 } 1171 }
1060 } 1172 }
1061 data->last_limits = jiffies; 1173 data->last_limits = jiffies;
@@ -1067,7 +1179,8 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1067 F71882FG_REG_TEMP_STATUS); 1179 F71882FG_REG_TEMP_STATUS);
1068 data->temp_diode_open = f71882fg_read8(data, 1180 data->temp_diode_open = f71882fg_read8(data,
1069 F71882FG_REG_TEMP_DIODE_OPEN); 1181 F71882FG_REG_TEMP_DIODE_OPEN);
1070 for (nr = data->temp_start; nr < 3 + data->temp_start; nr++) 1182 for (nr = data->temp_start; nr < nr_temps + data->temp_start;
1183 nr++)
1071 data->temp[nr] = f71882fg_read_temp(data, nr); 1184 data->temp[nr] = f71882fg_read_temp(data, nr);
1072 1185
1073 data->fan_status = f71882fg_read8(data, 1186 data->fan_status = f71882fg_read8(data,
@@ -1083,17 +1196,18 @@ static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1083 data->pwm[nr] = 1196 data->pwm[nr] =
1084 f71882fg_read8(data, F71882FG_REG_PWM(nr)); 1197 f71882fg_read8(data, F71882FG_REG_PWM(nr));
1085 } 1198 }
1086
1087 /* The f8000 can monitor 1 more fan, but has no pwm for it */ 1199 /* The f8000 can monitor 1 more fan, but has no pwm for it */
1088 if (data->type == f8000) 1200 if (data->type == f8000)
1089 data->fan[3] = f71882fg_read16(data, 1201 data->fan[3] = f71882fg_read16(data,
1090 F71882FG_REG_FAN(3)); 1202 F71882FG_REG_FAN(3));
1091 if (data->type == f71882fg || data->type == f71889fg) 1203
1204 if (f71882fg_has_in1_alarm[data->type])
1092 data->in_status = f71882fg_read8(data, 1205 data->in_status = f71882fg_read8(data,
1093 F71882FG_REG_IN_STATUS); 1206 F71882FG_REG_IN_STATUS);
1094 for (nr = 0; nr < nr_ins; nr++) 1207 for (nr = 0; nr < F71882FG_MAX_INS; nr++)
1095 data->in[nr] = f71882fg_read8(data, 1208 if (f71882fg_has_in[data->type][nr])
1096 F71882FG_REG_IN(nr)); 1209 data->in[nr] = f71882fg_read8(data,
1210 F71882FG_REG_IN(nr));
1097 1211
1098 data->last_updated = jiffies; 1212 data->last_updated = jiffies;
1099 data->valid = 1; 1213 data->valid = 1;
@@ -1882,7 +1996,7 @@ static ssize_t store_pwm_auto_point_temp(struct device *dev,
1882 1996
1883 val /= 1000; 1997 val /= 1000;
1884 1998
1885 if (data->type == f71889fg) 1999 if (data->auto_point_temp_signed)
1886 val = SENSORS_LIMIT(val, -128, 127); 2000 val = SENSORS_LIMIT(val, -128, 127);
1887 else 2001 else
1888 val = SENSORS_LIMIT(val, 0, 127); 2002 val = SENSORS_LIMIT(val, 0, 127);
@@ -1929,7 +2043,8 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
1929 struct f71882fg_data *data; 2043 struct f71882fg_data *data;
1930 struct f71882fg_sio_data *sio_data = pdev->dev.platform_data; 2044 struct f71882fg_sio_data *sio_data = pdev->dev.platform_data;
1931 int err, i, nr_fans = (sio_data->type == f71882fg) ? 4 : 3; 2045 int err, i, nr_fans = (sio_data->type == f71882fg) ? 4 : 3;
1932 u8 start_reg; 2046 int nr_temps = (sio_data->type == f71808e) ? 2 : 3;
2047 u8 start_reg, reg;
1933 2048
1934 data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL); 2049 data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL);
1935 if (!data) 2050 if (!data)
@@ -1968,37 +2083,72 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
1968 /* The f71858fg temperature alarms behave as 2083 /* The f71858fg temperature alarms behave as
1969 the f8000 alarms in this mode */ 2084 the f8000 alarms in this mode */
1970 err = f71882fg_create_sysfs_files(pdev, 2085 err = f71882fg_create_sysfs_files(pdev,
1971 f8000_in_temp_attr, 2086 f8000_temp_attr,
1972 ARRAY_SIZE(f8000_in_temp_attr)); 2087 ARRAY_SIZE(f8000_temp_attr));
1973 else 2088 else
1974 err = f71882fg_create_sysfs_files(pdev, 2089 err = f71882fg_create_sysfs_files(pdev,
1975 f71858fg_in_temp_attr, 2090 f71858fg_temp_attr,
1976 ARRAY_SIZE(f71858fg_in_temp_attr)); 2091 ARRAY_SIZE(f71858fg_temp_attr));
1977 break;
1978 case f71882fg:
1979 case f71889fg:
1980 err = f71882fg_create_sysfs_files(pdev,
1981 fxxxx_in1_alarm_attr,
1982 ARRAY_SIZE(fxxxx_in1_alarm_attr));
1983 if (err)
1984 goto exit_unregister_sysfs;
1985 /* fall through! */
1986 case f71862fg:
1987 err = f71882fg_create_sysfs_files(pdev,
1988 fxxxx_in_temp_attr,
1989 ARRAY_SIZE(fxxxx_in_temp_attr));
1990 break; 2092 break;
1991 case f8000: 2093 case f8000:
1992 err = f71882fg_create_sysfs_files(pdev, 2094 err = f71882fg_create_sysfs_files(pdev,
1993 f8000_in_temp_attr, 2095 f8000_temp_attr,
1994 ARRAY_SIZE(f8000_in_temp_attr)); 2096 ARRAY_SIZE(f8000_temp_attr));
1995 break; 2097 break;
2098 default:
2099 err = f71882fg_create_sysfs_files(pdev,
2100 &fxxxx_temp_attr[0][0],
2101 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
1996 } 2102 }
1997 if (err) 2103 if (err)
1998 goto exit_unregister_sysfs; 2104 goto exit_unregister_sysfs;
2105
2106 if (f71882fg_has_beep[data->type]) {
2107 err = f71882fg_create_sysfs_files(pdev,
2108 &fxxxx_temp_beep_attr[0][0],
2109 ARRAY_SIZE(fxxxx_temp_beep_attr[0])
2110 * nr_temps);
2111 if (err)
2112 goto exit_unregister_sysfs;
2113 }
2114
2115 for (i = 0; i < F71882FG_MAX_INS; i++) {
2116 if (f71882fg_has_in[data->type][i]) {
2117 err = device_create_file(&pdev->dev,
2118 &fxxxx_in_attr[i].dev_attr);
2119 if (err)
2120 goto exit_unregister_sysfs;
2121 }
2122 }
2123 if (f71882fg_has_in1_alarm[data->type]) {
2124 err = f71882fg_create_sysfs_files(pdev,
2125 fxxxx_in1_alarm_attr,
2126 ARRAY_SIZE(fxxxx_in1_alarm_attr));
2127 if (err)
2128 goto exit_unregister_sysfs;
2129 }
1999 } 2130 }
2000 2131
2001 if (start_reg & 0x02) { 2132 if (start_reg & 0x02) {
2133 switch (data->type) {
2134 case f71808e:
2135 case f71869:
2136 /* These always have signed auto point temps */
2137 data->auto_point_temp_signed = 1;
2138 /* Fall through to select correct fan/pwm reg bank! */
2139 case f71889fg:
2140 case f71889ed:
2141 reg = f71882fg_read8(data, F71882FG_REG_FAN_FAULT_T);
2142 if (reg & F71882FG_FAN_NEG_TEMP_EN)
2143 data->auto_point_temp_signed = 1;
2144 /* Ensure banked pwm registers point to right bank */
2145 reg &= ~F71882FG_FAN_PROG_SEL;
2146 f71882fg_write8(data, F71882FG_REG_FAN_FAULT_T, reg);
2147 break;
2148 default:
2149 break;
2150 }
2151
2002 data->pwm_enable = 2152 data->pwm_enable =
2003 f71882fg_read8(data, F71882FG_REG_PWM_ENABLE); 2153 f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
2004 2154
@@ -2013,8 +2163,11 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
2013 case f71862fg: 2163 case f71862fg:
2014 err = (data->pwm_enable & 0x15) != 0x15; 2164 err = (data->pwm_enable & 0x15) != 0x15;
2015 break; 2165 break;
2166 case f71808e:
2167 case f71869:
2016 case f71882fg: 2168 case f71882fg:
2017 case f71889fg: 2169 case f71889fg:
2170 case f71889ed:
2018 err = 0; 2171 err = 0;
2019 break; 2172 break;
2020 case f8000: 2173 case f8000:
@@ -2034,8 +2187,7 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
2034 if (err) 2187 if (err)
2035 goto exit_unregister_sysfs; 2188 goto exit_unregister_sysfs;
2036 2189
2037 if (data->type == f71862fg || data->type == f71882fg || 2190 if (f71882fg_has_beep[data->type]) {
2038 data->type == f71889fg) {
2039 err = f71882fg_create_sysfs_files(pdev, 2191 err = f71882fg_create_sysfs_files(pdev,
2040 fxxxx_fan_beep_attr, nr_fans); 2192 fxxxx_fan_beep_attr, nr_fans);
2041 if (err) 2193 if (err)
@@ -2043,11 +2195,42 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
2043 } 2195 }
2044 2196
2045 switch (data->type) { 2197 switch (data->type) {
2198 case f71808e:
2199 case f71869:
2200 case f71889fg:
2201 case f71889ed:
2202 for (i = 0; i < nr_fans; i++) {
2203 data->pwm_auto_point_mapping[i] =
2204 f71882fg_read8(data,
2205 F71882FG_REG_POINT_MAPPING(i));
2206 if ((data->pwm_auto_point_mapping[i] & 0x80) ||
2207 (data->pwm_auto_point_mapping[i] & 3) == 0)
2208 break;
2209 }
2210 if (i != nr_fans) {
2211 dev_warn(&pdev->dev,
2212 "Auto pwm controlled by raw digital "
2213 "data, disabling pwm auto_point "
2214 "sysfs attributes\n");
2215 goto no_pwm_auto_point;
2216 }
2217 break;
2218 default:
2219 break;
2220 }
2221
2222 switch (data->type) {
2046 case f71862fg: 2223 case f71862fg:
2047 err = f71882fg_create_sysfs_files(pdev, 2224 err = f71882fg_create_sysfs_files(pdev,
2048 f71862fg_auto_pwm_attr, 2225 f71862fg_auto_pwm_attr,
2049 ARRAY_SIZE(f71862fg_auto_pwm_attr)); 2226 ARRAY_SIZE(f71862fg_auto_pwm_attr));
2050 break; 2227 break;
2228 case f71808e:
2229 case f71869:
2230 err = f71882fg_create_sysfs_files(pdev,
2231 f71869_auto_pwm_attr,
2232 ARRAY_SIZE(f71869_auto_pwm_attr));
2233 break;
2051 case f8000: 2234 case f8000:
2052 err = f71882fg_create_sysfs_files(pdev, 2235 err = f71882fg_create_sysfs_files(pdev,
2053 f8000_fan_attr, 2236 f8000_fan_attr,
@@ -2058,23 +2241,7 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
2058 f8000_auto_pwm_attr, 2241 f8000_auto_pwm_attr,
2059 ARRAY_SIZE(f8000_auto_pwm_attr)); 2242 ARRAY_SIZE(f8000_auto_pwm_attr));
2060 break; 2243 break;
2061 case f71889fg: 2244 default:
2062 for (i = 0; i < nr_fans; i++) {
2063 data->pwm_auto_point_mapping[i] =
2064 f71882fg_read8(data,
2065 F71882FG_REG_POINT_MAPPING(i));
2066 if (data->pwm_auto_point_mapping[i] & 0x80)
2067 break;
2068 }
2069 if (i != nr_fans) {
2070 dev_warn(&pdev->dev,
2071 "Auto pwm controlled by raw digital "
2072 "data, disabling pwm auto_point "
2073 "sysfs attributes\n");
2074 break;
2075 }
2076 /* fall through */
2077 default: /* f71858fg / f71882fg */
2078 err = f71882fg_create_sysfs_files(pdev, 2245 err = f71882fg_create_sysfs_files(pdev,
2079 &fxxxx_auto_pwm_attr[0][0], 2246 &fxxxx_auto_pwm_attr[0][0],
2080 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans); 2247 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
@@ -2082,6 +2249,7 @@ static int __devinit f71882fg_probe(struct platform_device *pdev)
2082 if (err) 2249 if (err)
2083 goto exit_unregister_sysfs; 2250 goto exit_unregister_sysfs;
2084 2251
2252no_pwm_auto_point:
2085 for (i = 0; i < nr_fans; i++) 2253 for (i = 0; i < nr_fans; i++)
2086 dev_info(&pdev->dev, "Fan: %d is in %s mode\n", i + 1, 2254 dev_info(&pdev->dev, "Fan: %d is in %s mode\n", i + 1,
2087 (data->pwm_enable & (1 << 2 * i)) ? 2255 (data->pwm_enable & (1 << 2 * i)) ?
@@ -2108,7 +2276,8 @@ exit_free:
2108static int f71882fg_remove(struct platform_device *pdev) 2276static int f71882fg_remove(struct platform_device *pdev)
2109{ 2277{
2110 struct f71882fg_data *data = platform_get_drvdata(pdev); 2278 struct f71882fg_data *data = platform_get_drvdata(pdev);
2111 int nr_fans = (data->type == f71882fg) ? 4 : 3; 2279 int i, nr_fans = (data->type == f71882fg) ? 4 : 3;
2280 int nr_temps = (data->type == f71808e) ? 2 : 3;
2112 u8 start_reg = f71882fg_read8(data, F71882FG_REG_START); 2281 u8 start_reg = f71882fg_read8(data, F71882FG_REG_START);
2113 2282
2114 if (data->hwmon_dev) 2283 if (data->hwmon_dev)
@@ -2121,29 +2290,39 @@ static int f71882fg_remove(struct platform_device *pdev)
2121 case f71858fg: 2290 case f71858fg:
2122 if (data->temp_config & 0x10) 2291 if (data->temp_config & 0x10)
2123 f71882fg_remove_sysfs_files(pdev, 2292 f71882fg_remove_sysfs_files(pdev,
2124 f8000_in_temp_attr, 2293 f8000_temp_attr,
2125 ARRAY_SIZE(f8000_in_temp_attr)); 2294 ARRAY_SIZE(f8000_temp_attr));
2126 else 2295 else
2127 f71882fg_remove_sysfs_files(pdev, 2296 f71882fg_remove_sysfs_files(pdev,
2128 f71858fg_in_temp_attr, 2297 f71858fg_temp_attr,
2129 ARRAY_SIZE(f71858fg_in_temp_attr)); 2298 ARRAY_SIZE(f71858fg_temp_attr));
2130 break;
2131 case f71882fg:
2132 case f71889fg:
2133 f71882fg_remove_sysfs_files(pdev,
2134 fxxxx_in1_alarm_attr,
2135 ARRAY_SIZE(fxxxx_in1_alarm_attr));
2136 /* fall through! */
2137 case f71862fg:
2138 f71882fg_remove_sysfs_files(pdev,
2139 fxxxx_in_temp_attr,
2140 ARRAY_SIZE(fxxxx_in_temp_attr));
2141 break; 2299 break;
2142 case f8000: 2300 case f8000:
2143 f71882fg_remove_sysfs_files(pdev, 2301 f71882fg_remove_sysfs_files(pdev,
2144 f8000_in_temp_attr, 2302 f8000_temp_attr,
2145 ARRAY_SIZE(f8000_in_temp_attr)); 2303 ARRAY_SIZE(f8000_temp_attr));
2146 break; 2304 break;
2305 default:
2306 f71882fg_remove_sysfs_files(pdev,
2307 &fxxxx_temp_attr[0][0],
2308 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
2309 }
2310 if (f71882fg_has_beep[data->type]) {
2311 f71882fg_remove_sysfs_files(pdev,
2312 &fxxxx_temp_beep_attr[0][0],
2313 ARRAY_SIZE(fxxxx_temp_beep_attr[0]) * nr_temps);
2314 }
2315
2316 for (i = 0; i < F71882FG_MAX_INS; i++) {
2317 if (f71882fg_has_in[data->type][i]) {
2318 device_remove_file(&pdev->dev,
2319 &fxxxx_in_attr[i].dev_attr);
2320 }
2321 }
2322 if (f71882fg_has_in1_alarm[data->type]) {
2323 f71882fg_remove_sysfs_files(pdev,
2324 fxxxx_in1_alarm_attr,
2325 ARRAY_SIZE(fxxxx_in1_alarm_attr));
2147 } 2326 }
2148 } 2327 }
2149 2328
@@ -2151,10 +2330,10 @@ static int f71882fg_remove(struct platform_device *pdev)
2151 f71882fg_remove_sysfs_files(pdev, &fxxxx_fan_attr[0][0], 2330 f71882fg_remove_sysfs_files(pdev, &fxxxx_fan_attr[0][0],
2152 ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans); 2331 ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans);
2153 2332
2154 if (data->type == f71862fg || data->type == f71882fg || 2333 if (f71882fg_has_beep[data->type]) {
2155 data->type == f71889fg)
2156 f71882fg_remove_sysfs_files(pdev, 2334 f71882fg_remove_sysfs_files(pdev,
2157 fxxxx_fan_beep_attr, nr_fans); 2335 fxxxx_fan_beep_attr, nr_fans);
2336 }
2158 2337
2159 switch (data->type) { 2338 switch (data->type) {
2160 case f71862fg: 2339 case f71862fg:
@@ -2162,6 +2341,12 @@ static int f71882fg_remove(struct platform_device *pdev)
2162 f71862fg_auto_pwm_attr, 2341 f71862fg_auto_pwm_attr,
2163 ARRAY_SIZE(f71862fg_auto_pwm_attr)); 2342 ARRAY_SIZE(f71862fg_auto_pwm_attr));
2164 break; 2343 break;
2344 case f71808e:
2345 case f71869:
2346 f71882fg_remove_sysfs_files(pdev,
2347 f71869_auto_pwm_attr,
2348 ARRAY_SIZE(f71869_auto_pwm_attr));
2349 break;
2165 case f8000: 2350 case f8000:
2166 f71882fg_remove_sysfs_files(pdev, 2351 f71882fg_remove_sysfs_files(pdev,
2167 f8000_fan_attr, 2352 f8000_fan_attr,
@@ -2170,7 +2355,7 @@ static int f71882fg_remove(struct platform_device *pdev)
2170 f8000_auto_pwm_attr, 2355 f8000_auto_pwm_attr,
2171 ARRAY_SIZE(f8000_auto_pwm_attr)); 2356 ARRAY_SIZE(f8000_auto_pwm_attr));
2172 break; 2357 break;
2173 default: /* f71858fg / f71882fg / f71889fg */ 2358 default:
2174 f71882fg_remove_sysfs_files(pdev, 2359 f71882fg_remove_sysfs_files(pdev,
2175 &fxxxx_auto_pwm_attr[0][0], 2360 &fxxxx_auto_pwm_attr[0][0],
2176 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans); 2361 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
@@ -2200,18 +2385,27 @@ static int __init f71882fg_find(int sioaddr, unsigned short *address,
2200 2385
2201 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID); 2386 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
2202 switch (devid) { 2387 switch (devid) {
2388 case SIO_F71808E_ID:
2389 sio_data->type = f71808e;
2390 break;
2203 case SIO_F71858_ID: 2391 case SIO_F71858_ID:
2204 sio_data->type = f71858fg; 2392 sio_data->type = f71858fg;
2205 break; 2393 break;
2206 case SIO_F71862_ID: 2394 case SIO_F71862_ID:
2207 sio_data->type = f71862fg; 2395 sio_data->type = f71862fg;
2208 break; 2396 break;
2397 case SIO_F71869_ID:
2398 sio_data->type = f71869;
2399 break;
2209 case SIO_F71882_ID: 2400 case SIO_F71882_ID:
2210 sio_data->type = f71882fg; 2401 sio_data->type = f71882fg;
2211 break; 2402 break;
2212 case SIO_F71889_ID: 2403 case SIO_F71889_ID:
2213 sio_data->type = f71889fg; 2404 sio_data->type = f71889fg;
2214 break; 2405 break;
2406 case SIO_F71889E_ID:
2407 sio_data->type = f71889ed;
2408 break;
2215 case SIO_F8000_ID: 2409 case SIO_F8000_ID:
2216 sio_data->type = f8000; 2410 sio_data->type = f8000;
2217 break; 2411 break;
diff --git a/drivers/hwmon/lineage-pem.c b/drivers/hwmon/lineage-pem.c
new file mode 100644
index 000000000000..58eded27f385
--- /dev/null
+++ b/drivers/hwmon/lineage-pem.c
@@ -0,0 +1,586 @@
1/*
2 * Driver for Lineage Compact Power Line series of power entry modules.
3 *
4 * Copyright (C) 2010, 2011 Ericsson AB.
5 *
6 * Documentation:
7 * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
8 *
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.
13 *
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.
18 *
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.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32
33/*
34 * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
35 * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
36 *
37 * The devices are nominally PMBus compliant. However, most standard PMBus
38 * commands are not supported. Specifically, all hardware monitoring and
39 * status reporting commands are non-standard. For this reason, a standard
40 * PMBus driver can not be used.
41 *
42 * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
43 * To ensure device access, this driver should only be used as client driver
44 * to the pca9541 I2C master selector driver.
45 */
46
47/* Command codes */
48#define PEM_OPERATION 0x01
49#define PEM_CLEAR_INFO_FLAGS 0x03
50#define PEM_VOUT_COMMAND 0x21
51#define PEM_VOUT_OV_FAULT_LIMIT 0x40
52#define PEM_READ_DATA_STRING 0xd0
53#define PEM_READ_INPUT_STRING 0xdc
54#define PEM_READ_FIRMWARE_REV 0xdd
55#define PEM_READ_RUN_TIMER 0xde
56#define PEM_FAN_HI_SPEED 0xdf
57#define PEM_FAN_NORMAL_SPEED 0xe0
58#define PEM_READ_FAN_SPEED 0xe1
59
60/* offsets in data string */
61#define PEM_DATA_STATUS_2 0
62#define PEM_DATA_STATUS_1 1
63#define PEM_DATA_ALARM_2 2
64#define PEM_DATA_ALARM_1 3
65#define PEM_DATA_VOUT_LSB 4
66#define PEM_DATA_VOUT_MSB 5
67#define PEM_DATA_CURRENT 6
68#define PEM_DATA_TEMP 7
69
70/* Virtual entries, to report constants */
71#define PEM_DATA_TEMP_MAX 10
72#define PEM_DATA_TEMP_CRIT 11
73
74/* offsets in input string */
75#define PEM_INPUT_VOLTAGE 0
76#define PEM_INPUT_POWER_LSB 1
77#define PEM_INPUT_POWER_MSB 2
78
79/* offsets in fan data */
80#define PEM_FAN_ADJUSTMENT 0
81#define PEM_FAN_FAN1 1
82#define PEM_FAN_FAN2 2
83#define PEM_FAN_FAN3 3
84
85/* Status register bits */
86#define STS1_OUTPUT_ON (1 << 0)
87#define STS1_LEDS_FLASHING (1 << 1)
88#define STS1_EXT_FAULT (1 << 2)
89#define STS1_SERVICE_LED_ON (1 << 3)
90#define STS1_SHUTDOWN_OCCURRED (1 << 4)
91#define STS1_INT_FAULT (1 << 5)
92#define STS1_ISOLATION_TEST_OK (1 << 6)
93
94#define STS2_ENABLE_PIN_HI (1 << 0)
95#define STS2_DATA_OUT_RANGE (1 << 1)
96#define STS2_RESTARTED_OK (1 << 1)
97#define STS2_ISOLATION_TEST_FAIL (1 << 3)
98#define STS2_HIGH_POWER_CAP (1 << 4)
99#define STS2_INVALID_INSTR (1 << 5)
100#define STS2_WILL_RESTART (1 << 6)
101#define STS2_PEC_ERR (1 << 7)
102
103/* Alarm register bits */
104#define ALRM1_VIN_OUT_LIMIT (1 << 0)
105#define ALRM1_VOUT_OUT_LIMIT (1 << 1)
106#define ALRM1_OV_VOLT_SHUTDOWN (1 << 2)
107#define ALRM1_VIN_OVERCURRENT (1 << 3)
108#define ALRM1_TEMP_WARNING (1 << 4)
109#define ALRM1_TEMP_SHUTDOWN (1 << 5)
110#define ALRM1_PRIMARY_FAULT (1 << 6)
111#define ALRM1_POWER_LIMIT (1 << 7)
112
113#define ALRM2_5V_OUT_LIMIT (1 << 1)
114#define ALRM2_TEMP_FAULT (1 << 2)
115#define ALRM2_OV_LOW (1 << 3)
116#define ALRM2_DCDC_TEMP_HIGH (1 << 4)
117#define ALRM2_PRI_TEMP_HIGH (1 << 5)
118#define ALRM2_NO_PRIMARY (1 << 6)
119#define ALRM2_FAN_FAULT (1 << 7)
120
121#define FIRMWARE_REV_LEN 4
122#define DATA_STRING_LEN 9
123#define INPUT_STRING_LEN 5 /* 4 for most devices */
124#define FAN_SPEED_LEN 5
125
126struct pem_data {
127 struct device *hwmon_dev;
128
129 struct mutex update_lock;
130 bool valid;
131 bool fans_supported;
132 int input_length;
133 unsigned long last_updated; /* in jiffies */
134
135 u8 firmware_rev[FIRMWARE_REV_LEN];
136 u8 data_string[DATA_STRING_LEN];
137 u8 input_string[INPUT_STRING_LEN];
138 u8 fan_speed[FAN_SPEED_LEN];
139};
140
141static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
142 int data_len)
143{
144 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
145 int result;
146
147 result = i2c_smbus_read_block_data(client, command, block_buffer);
148 if (unlikely(result < 0))
149 goto abort;
150 if (unlikely(result == 0xff || result != data_len)) {
151 result = -EIO;
152 goto abort;
153 }
154 memcpy(data, block_buffer, data_len);
155 result = 0;
156abort:
157 return result;
158}
159
160static struct pem_data *pem_update_device(struct device *dev)
161{
162 struct i2c_client *client = to_i2c_client(dev);
163 struct pem_data *data = i2c_get_clientdata(client);
164 struct pem_data *ret = data;
165
166 mutex_lock(&data->update_lock);
167
168 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
169 int result;
170
171 /* Read data string */
172 result = pem_read_block(client, PEM_READ_DATA_STRING,
173 data->data_string,
174 sizeof(data->data_string));
175 if (unlikely(result < 0)) {
176 ret = ERR_PTR(result);
177 goto abort;
178 }
179
180 /* Read input string */
181 if (data->input_length) {
182 result = pem_read_block(client, PEM_READ_INPUT_STRING,
183 data->input_string,
184 data->input_length);
185 if (unlikely(result < 0)) {
186 ret = ERR_PTR(result);
187 goto abort;
188 }
189 }
190
191 /* Read fan speeds */
192 if (data->fans_supported) {
193 result = pem_read_block(client, PEM_READ_FAN_SPEED,
194 data->fan_speed,
195 sizeof(data->fan_speed));
196 if (unlikely(result < 0)) {
197 ret = ERR_PTR(result);
198 goto abort;
199 }
200 }
201
202 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
203
204 data->last_updated = jiffies;
205 data->valid = 1;
206 }
207abort:
208 mutex_unlock(&data->update_lock);
209 return ret;
210}
211
212static long pem_get_data(u8 *data, int len, int index)
213{
214 long val;
215
216 switch (index) {
217 case PEM_DATA_VOUT_LSB:
218 val = (data[index] + (data[index+1] << 8)) * 5 / 2;
219 break;
220 case PEM_DATA_CURRENT:
221 val = data[index] * 200;
222 break;
223 case PEM_DATA_TEMP:
224 val = data[index] * 1000;
225 break;
226 case PEM_DATA_TEMP_MAX:
227 val = 97 * 1000; /* 97 degrees C per datasheet */
228 break;
229 case PEM_DATA_TEMP_CRIT:
230 val = 107 * 1000; /* 107 degrees C per datasheet */
231 break;
232 default:
233 WARN_ON_ONCE(1);
234 val = 0;
235 }
236 return val;
237}
238
239static long pem_get_input(u8 *data, int len, int index)
240{
241 long val;
242
243 switch (index) {
244 case PEM_INPUT_VOLTAGE:
245 if (len == INPUT_STRING_LEN)
246 val = (data[index] + (data[index+1] << 8) - 75) * 1000;
247 else
248 val = (data[index] - 75) * 1000;
249 break;
250 case PEM_INPUT_POWER_LSB:
251 if (len == INPUT_STRING_LEN)
252 index++;
253 val = (data[index] + (data[index+1] << 8)) * 1000000L;
254 break;
255 default:
256 WARN_ON_ONCE(1);
257 val = 0;
258 }
259 return val;
260}
261
262static long pem_get_fan(u8 *data, int len, int index)
263{
264 long val;
265
266 switch (index) {
267 case PEM_FAN_FAN1:
268 case PEM_FAN_FAN2:
269 case PEM_FAN_FAN3:
270 val = data[index] * 100;
271 break;
272 default:
273 WARN_ON_ONCE(1);
274 val = 0;
275 }
276 return val;
277}
278
279/*
280 * Show boolean, either a fault or an alarm.
281 * .nr points to the register, .index is the bit mask to check
282 */
283static ssize_t pem_show_bool(struct device *dev,
284 struct device_attribute *da, char *buf)
285{
286 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
287 struct pem_data *data = pem_update_device(dev);
288 u8 status;
289
290 if (IS_ERR(data))
291 return PTR_ERR(data);
292
293 status = data->data_string[attr->nr] & attr->index;
294 return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
295}
296
297static ssize_t pem_show_data(struct device *dev, struct device_attribute *da,
298 char *buf)
299{
300 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
301 struct pem_data *data = pem_update_device(dev);
302 long value;
303
304 if (IS_ERR(data))
305 return PTR_ERR(data);
306
307 value = pem_get_data(data->data_string, sizeof(data->data_string),
308 attr->index);
309
310 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
311}
312
313static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
314 char *buf)
315{
316 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
317 struct pem_data *data = pem_update_device(dev);
318 long value;
319
320 if (IS_ERR(data))
321 return PTR_ERR(data);
322
323 value = pem_get_input(data->input_string, sizeof(data->input_string),
324 attr->index);
325
326 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
327}
328
329static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
330 char *buf)
331{
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
333 struct pem_data *data = pem_update_device(dev);
334 long value;
335
336 if (IS_ERR(data))
337 return PTR_ERR(data);
338
339 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
340 attr->index);
341
342 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
343}
344
345/* Voltages */
346static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
347 PEM_DATA_VOUT_LSB);
348static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL,
349 PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT);
350static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
351 PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN);
352static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL,
353 PEM_INPUT_VOLTAGE);
354static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL,
355 PEM_DATA_ALARM_1,
356 ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
357
358/* Currents */
359static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
360 PEM_DATA_CURRENT);
361static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
362 PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
363
364/* Power */
365static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL,
366 PEM_INPUT_POWER_LSB);
367static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL,
368 PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT);
369
370/* Fans */
371static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
372 PEM_FAN_FAN1);
373static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
374 PEM_FAN_FAN2);
375static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
376 PEM_FAN_FAN3);
377static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
378 PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
379
380/* Temperatures */
381static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
382 PEM_DATA_TEMP);
383static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
384 PEM_DATA_TEMP_MAX);
385static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
386 PEM_DATA_TEMP_CRIT);
387static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL,
388 PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING);
389static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
390 PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN);
391static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL,
392 PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT);
393
394static struct attribute *pem_attributes[] = {
395 &sensor_dev_attr_in1_input.dev_attr.attr,
396 &sensor_dev_attr_in1_alarm.dev_attr.attr,
397 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
398 &sensor_dev_attr_in2_alarm.dev_attr.attr,
399
400 &sensor_dev_attr_curr1_alarm.dev_attr.attr,
401
402 &sensor_dev_attr_power1_alarm.dev_attr.attr,
403
404 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
405
406 &sensor_dev_attr_temp1_input.dev_attr.attr,
407 &sensor_dev_attr_temp1_max.dev_attr.attr,
408 &sensor_dev_attr_temp1_crit.dev_attr.attr,
409 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
410 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
411 &sensor_dev_attr_temp1_fault.dev_attr.attr,
412
413 NULL,
414};
415
416static const struct attribute_group pem_group = {
417 .attrs = pem_attributes,
418};
419
420static struct attribute *pem_input_attributes[] = {
421 &sensor_dev_attr_in2_input.dev_attr.attr,
422 &sensor_dev_attr_curr1_input.dev_attr.attr,
423 &sensor_dev_attr_power1_input.dev_attr.attr,
424};
425
426static const struct attribute_group pem_input_group = {
427 .attrs = pem_input_attributes,
428};
429
430static struct attribute *pem_fan_attributes[] = {
431 &sensor_dev_attr_fan1_input.dev_attr.attr,
432 &sensor_dev_attr_fan2_input.dev_attr.attr,
433 &sensor_dev_attr_fan3_input.dev_attr.attr,
434};
435
436static const struct attribute_group pem_fan_group = {
437 .attrs = pem_fan_attributes,
438};
439
440static int pem_probe(struct i2c_client *client,
441 const struct i2c_device_id *id)
442{
443 struct i2c_adapter *adapter = client->adapter;
444 struct pem_data *data;
445 int ret;
446
447 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
448 | I2C_FUNC_SMBUS_WRITE_BYTE))
449 return -ENODEV;
450
451 data = kzalloc(sizeof(*data), GFP_KERNEL);
452 if (!data)
453 return -ENOMEM;
454
455 i2c_set_clientdata(client, data);
456 mutex_init(&data->update_lock);
457
458 /*
459 * We use the next two commands to determine if the device is really
460 * there.
461 */
462 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
463 data->firmware_rev, sizeof(data->firmware_rev));
464 if (ret < 0)
465 goto out_kfree;
466
467 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
468 if (ret < 0)
469 goto out_kfree;
470
471 dev_info(&client->dev, "Firmware revision %d.%d.%d\n",
472 data->firmware_rev[0], data->firmware_rev[1],
473 data->firmware_rev[2]);
474
475 /* Register sysfs hooks */
476 ret = sysfs_create_group(&client->dev.kobj, &pem_group);
477 if (ret)
478 goto out_kfree;
479
480 /*
481 * Check if input readings are supported.
482 * This is the case if we can read input data,
483 * and if the returned data is not all zeros.
484 * Note that input alarms are always supported.
485 */
486 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
487 data->input_string,
488 sizeof(data->input_string) - 1);
489 if (!ret && (data->input_string[0] || data->input_string[1] ||
490 data->input_string[2]))
491 data->input_length = sizeof(data->input_string) - 1;
492 else if (ret < 0) {
493 /* Input string is one byte longer for some devices */
494 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
495 data->input_string,
496 sizeof(data->input_string));
497 if (!ret && (data->input_string[0] || data->input_string[1] ||
498 data->input_string[2] || data->input_string[3]))
499 data->input_length = sizeof(data->input_string);
500 }
501 ret = 0;
502 if (data->input_length) {
503 ret = sysfs_create_group(&client->dev.kobj, &pem_input_group);
504 if (ret)
505 goto out_remove_groups;
506 }
507
508 /*
509 * Check if fan speed readings are supported.
510 * This is the case if we can read fan speed data,
511 * and if the returned data is not all zeros.
512 * Note that the fan alarm is always supported.
513 */
514 ret = pem_read_block(client, PEM_READ_FAN_SPEED,
515 data->fan_speed,
516 sizeof(data->fan_speed));
517 if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
518 data->fan_speed[2] || data->fan_speed[3])) {
519 data->fans_supported = true;
520 ret = sysfs_create_group(&client->dev.kobj, &pem_fan_group);
521 if (ret)
522 goto out_remove_groups;
523 }
524
525 data->hwmon_dev = hwmon_device_register(&client->dev);
526 if (IS_ERR(data->hwmon_dev)) {
527 ret = PTR_ERR(data->hwmon_dev);
528 goto out_remove_groups;
529 }
530
531 return 0;
532
533out_remove_groups:
534 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
535 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
536 sysfs_remove_group(&client->dev.kobj, &pem_group);
537out_kfree:
538 kfree(data);
539 return ret;
540}
541
542static int pem_remove(struct i2c_client *client)
543{
544 struct pem_data *data = i2c_get_clientdata(client);
545
546 hwmon_device_unregister(data->hwmon_dev);
547
548 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
549 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
550 sysfs_remove_group(&client->dev.kobj, &pem_group);
551
552 kfree(data);
553 return 0;
554}
555
556static const struct i2c_device_id pem_id[] = {
557 {"lineage_pem", 0},
558 {}
559};
560MODULE_DEVICE_TABLE(i2c, pem_id);
561
562static struct i2c_driver pem_driver = {
563 .driver = {
564 .name = "lineage_pem",
565 },
566 .probe = pem_probe,
567 .remove = pem_remove,
568 .id_table = pem_id,
569};
570
571static int __init pem_init(void)
572{
573 return i2c_add_driver(&pem_driver);
574}
575
576static void __exit pem_exit(void)
577{
578 i2c_del_driver(&pem_driver);
579}
580
581MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
582MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
583MODULE_LICENSE("GPL");
584
585module_init(pem_init);
586module_exit(pem_exit);
diff --git a/drivers/hwmon/lis3lv02d_spi.c b/drivers/hwmon/lis3lv02d_spi.c
index 2549de1de4e2..c1f8a8fbf694 100644
--- a/drivers/hwmon/lis3lv02d_spi.c
+++ b/drivers/hwmon/lis3lv02d_spi.c
@@ -16,6 +16,7 @@
16#include <linux/interrupt.h> 16#include <linux/interrupt.h>
17#include <linux/workqueue.h> 17#include <linux/workqueue.h>
18#include <linux/spi/spi.h> 18#include <linux/spi/spi.h>
19#include <linux/pm.h>
19 20
20#include "lis3lv02d.h" 21#include "lis3lv02d.h"
21 22
@@ -88,9 +89,10 @@ static int __devexit lis302dl_spi_remove(struct spi_device *spi)
88 return lis3lv02d_remove_fs(&lis3_dev); 89 return lis3lv02d_remove_fs(&lis3_dev);
89} 90}
90 91
91#ifdef CONFIG_PM 92#ifdef CONFIG_PM_SLEEP
92static int lis3lv02d_spi_suspend(struct spi_device *spi, pm_message_t mesg) 93static int lis3lv02d_spi_suspend(struct device *dev)
93{ 94{
95 struct spi_device *spi = to_spi_device(dev);
94 struct lis3lv02d *lis3 = spi_get_drvdata(spi); 96 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
95 97
96 if (!lis3->pdata || !lis3->pdata->wakeup_flags) 98 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
@@ -99,8 +101,9 @@ static int lis3lv02d_spi_suspend(struct spi_device *spi, pm_message_t mesg)
99 return 0; 101 return 0;
100} 102}
101 103
102static int lis3lv02d_spi_resume(struct spi_device *spi) 104static int lis3lv02d_spi_resume(struct device *dev)
103{ 105{
106 struct spi_device *spi = to_spi_device(dev);
104 struct lis3lv02d *lis3 = spi_get_drvdata(spi); 107 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
105 108
106 if (!lis3->pdata || !lis3->pdata->wakeup_flags) 109 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
@@ -108,21 +111,19 @@ static int lis3lv02d_spi_resume(struct spi_device *spi)
108 111
109 return 0; 112 return 0;
110} 113}
111
112#else
113#define lis3lv02d_spi_suspend NULL
114#define lis3lv02d_spi_resume NULL
115#endif 114#endif
116 115
116static SIMPLE_DEV_PM_OPS(lis3lv02d_spi_pm, lis3lv02d_spi_suspend,
117 lis3lv02d_spi_resume);
118
117static struct spi_driver lis302dl_spi_driver = { 119static struct spi_driver lis302dl_spi_driver = {
118 .driver = { 120 .driver = {
119 .name = DRV_NAME, 121 .name = DRV_NAME,
120 .owner = THIS_MODULE, 122 .owner = THIS_MODULE,
123 .pm = &lis3lv02d_spi_pm,
121 }, 124 },
122 .probe = lis302dl_spi_probe, 125 .probe = lis302dl_spi_probe,
123 .remove = __devexit_p(lis302dl_spi_remove), 126 .remove = __devexit_p(lis302dl_spi_remove),
124 .suspend = lis3lv02d_spi_suspend,
125 .resume = lis3lv02d_spi_resume,
126}; 127};
127 128
128static int __init lis302dl_init(void) 129static int __init lis302dl_init(void)
diff --git a/drivers/hwmon/lm85.c b/drivers/hwmon/lm85.c
index d2cc28660816..cf47e6e476ed 100644
--- a/drivers/hwmon/lm85.c
+++ b/drivers/hwmon/lm85.c
@@ -41,7 +41,7 @@ static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
41enum chips { 41enum chips {
42 any_chip, lm85b, lm85c, 42 any_chip, lm85b, lm85c,
43 adm1027, adt7463, adt7468, 43 adm1027, adt7463, adt7468,
44 emc6d100, emc6d102, emc6d103 44 emc6d100, emc6d102, emc6d103, emc6d103s
45}; 45};
46 46
47/* The LM85 registers */ 47/* The LM85 registers */
@@ -283,10 +283,6 @@ struct lm85_zone {
283 u8 hyst; /* Low limit hysteresis. (0-15) */ 283 u8 hyst; /* Low limit hysteresis. (0-15) */
284 u8 range; /* Temp range, encoded */ 284 u8 range; /* Temp range, encoded */
285 s8 critical; /* "All fans ON" temp limit */ 285 s8 critical; /* "All fans ON" temp limit */
286 u8 off_desired; /* Actual "off" temperature specified. Preserved
287 * to prevent "drift" as other autofan control
288 * values change.
289 */
290 u8 max_desired; /* Actual "max" temperature specified. Preserved 286 u8 max_desired; /* Actual "max" temperature specified. Preserved
291 * to prevent "drift" as other autofan control 287 * to prevent "drift" as other autofan control
292 * values change. 288 * values change.
@@ -306,6 +302,8 @@ struct lm85_data {
306 const int *freq_map; 302 const int *freq_map;
307 enum chips type; 303 enum chips type;
308 304
305 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
306
309 struct mutex update_lock; 307 struct mutex update_lock;
310 int valid; /* !=0 if following fields are valid */ 308 int valid; /* !=0 if following fields are valid */
311 unsigned long last_reading; /* In jiffies */ 309 unsigned long last_reading; /* In jiffies */
@@ -352,6 +350,7 @@ static const struct i2c_device_id lm85_id[] = {
352 { "emc6d101", emc6d100 }, 350 { "emc6d101", emc6d100 },
353 { "emc6d102", emc6d102 }, 351 { "emc6d102", emc6d102 },
354 { "emc6d103", emc6d103 }, 352 { "emc6d103", emc6d103 },
353 { "emc6d103s", emc6d103s },
355 { } 354 { }
356}; 355};
357MODULE_DEVICE_TABLE(i2c, lm85_id); 356MODULE_DEVICE_TABLE(i2c, lm85_id);
@@ -420,8 +419,7 @@ static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
420 struct lm85_data *data = lm85_update_device(dev); 419 struct lm85_data *data = lm85_update_device(dev);
421 int vid; 420 int vid;
422 421
423 if ((data->type == adt7463 || data->type == adt7468) && 422 if (data->has_vid5) {
424 (data->vid & 0x80)) {
425 /* 6-pin VID (VRM 10) */ 423 /* 6-pin VID (VRM 10) */
426 vid = vid_from_reg(data->vid & 0x3f, data->vrm); 424 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
427 } else { 425 } else {
@@ -891,7 +889,6 @@ static ssize_t set_temp_auto_temp_off(struct device *dev,
891 889
892 mutex_lock(&data->update_lock); 890 mutex_lock(&data->update_lock);
893 min = TEMP_FROM_REG(data->zone[nr].limit); 891 min = TEMP_FROM_REG(data->zone[nr].limit);
894 data->zone[nr].off_desired = TEMP_TO_REG(val);
895 data->zone[nr].hyst = HYST_TO_REG(min - val); 892 data->zone[nr].hyst = HYST_TO_REG(min - val);
896 if (nr == 0 || nr == 1) { 893 if (nr == 0 || nr == 1) {
897 lm85_write_value(client, LM85_REG_AFAN_HYST1, 894 lm85_write_value(client, LM85_REG_AFAN_HYST1,
@@ -934,18 +931,6 @@ static ssize_t set_temp_auto_temp_min(struct device *dev,
934 ((data->zone[nr].range & 0x0f) << 4) 931 ((data->zone[nr].range & 0x0f) << 4)
935 | (data->pwm_freq[nr] & 0x07)); 932 | (data->pwm_freq[nr] & 0x07));
936 933
937/* Update temp_auto_hyst and temp_auto_off */
938 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
939 data->zone[nr].limit) - TEMP_FROM_REG(
940 data->zone[nr].off_desired));
941 if (nr == 0 || nr == 1) {
942 lm85_write_value(client, LM85_REG_AFAN_HYST1,
943 (data->zone[0].hyst << 4)
944 | data->zone[1].hyst);
945 } else {
946 lm85_write_value(client, LM85_REG_AFAN_HYST2,
947 (data->zone[2].hyst << 4));
948 }
949 mutex_unlock(&data->update_lock); 934 mutex_unlock(&data->update_lock);
950 return count; 935 return count;
951} 936}
@@ -1084,13 +1069,7 @@ static struct attribute *lm85_attributes[] = {
1084 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr, 1069 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1085 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr, 1070 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1086 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr, 1071 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1087 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1088 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1089 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1090 1072
1091 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1092 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1093 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1094 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr, 1073 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1095 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr, 1074 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1096 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr, 1075 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
@@ -1111,6 +1090,26 @@ static const struct attribute_group lm85_group = {
1111 .attrs = lm85_attributes, 1090 .attrs = lm85_attributes,
1112}; 1091};
1113 1092
1093static struct attribute *lm85_attributes_minctl[] = {
1094 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1095 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1096 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1097};
1098
1099static const struct attribute_group lm85_group_minctl = {
1100 .attrs = lm85_attributes_minctl,
1101};
1102
1103static struct attribute *lm85_attributes_temp_off[] = {
1104 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1105 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1106 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1107};
1108
1109static const struct attribute_group lm85_group_temp_off = {
1110 .attrs = lm85_attributes_temp_off,
1111};
1112
1114static struct attribute *lm85_attributes_in4[] = { 1113static struct attribute *lm85_attributes_in4[] = {
1115 &sensor_dev_attr_in4_input.dev_attr.attr, 1114 &sensor_dev_attr_in4_input.dev_attr.attr,
1116 &sensor_dev_attr_in4_min.dev_attr.attr, 1115 &sensor_dev_attr_in4_min.dev_attr.attr,
@@ -1258,16 +1257,9 @@ static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1258 case LM85_VERSTEP_EMC6D103_A1: 1257 case LM85_VERSTEP_EMC6D103_A1:
1259 type_name = "emc6d103"; 1258 type_name = "emc6d103";
1260 break; 1259 break;
1261 /*
1262 * Registers apparently missing in EMC6D103S/EMC6D103:A2
1263 * compared to EMC6D103:A0, EMC6D103:A1, and EMC6D102
1264 * (according to the data sheets), but used unconditionally
1265 * in the driver: 62[5:7], 6D[0:7], and 6E[0:7].
1266 * So skip EMC6D103S for now.
1267 case LM85_VERSTEP_EMC6D103S: 1260 case LM85_VERSTEP_EMC6D103S:
1268 type_name = "emc6d103s"; 1261 type_name = "emc6d103s";
1269 break; 1262 break;
1270 */
1271 } 1263 }
1272 } else { 1264 } else {
1273 dev_dbg(&adapter->dev, 1265 dev_dbg(&adapter->dev,
@@ -1280,6 +1272,19 @@ static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1280 return 0; 1272 return 0;
1281} 1273}
1282 1274
1275static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1276{
1277 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1278 if (data->type != emc6d103s) {
1279 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1280 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1281 }
1282 if (!data->has_vid5)
1283 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1284 if (data->type == emc6d100)
1285 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1286}
1287
1283static int lm85_probe(struct i2c_client *client, 1288static int lm85_probe(struct i2c_client *client,
1284 const struct i2c_device_id *id) 1289 const struct i2c_device_id *id)
1285{ 1290{
@@ -1302,6 +1307,7 @@ static int lm85_probe(struct i2c_client *client,
1302 case emc6d100: 1307 case emc6d100:
1303 case emc6d102: 1308 case emc6d102:
1304 case emc6d103: 1309 case emc6d103:
1310 case emc6d103s:
1305 data->freq_map = adm1027_freq_map; 1311 data->freq_map = adm1027_freq_map;
1306 break; 1312 break;
1307 default: 1313 default:
@@ -1319,11 +1325,26 @@ static int lm85_probe(struct i2c_client *client,
1319 if (err) 1325 if (err)
1320 goto err_kfree; 1326 goto err_kfree;
1321 1327
1328 /* minctl and temp_off exist on all chips except emc6d103s */
1329 if (data->type != emc6d103s) {
1330 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1331 if (err)
1332 goto err_kfree;
1333 err = sysfs_create_group(&client->dev.kobj,
1334 &lm85_group_temp_off);
1335 if (err)
1336 goto err_kfree;
1337 }
1338
1322 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used 1339 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1323 as a sixth digital VID input rather than an analog input. */ 1340 as a sixth digital VID input rather than an analog input. */
1324 data->vid = lm85_read_value(client, LM85_REG_VID); 1341 if (data->type == adt7463 || data->type == adt7468) {
1325 if (!((data->type == adt7463 || data->type == adt7468) && 1342 u8 vid = lm85_read_value(client, LM85_REG_VID);
1326 (data->vid & 0x80))) 1343 if (vid & 0x80)
1344 data->has_vid5 = true;
1345 }
1346
1347 if (!data->has_vid5)
1327 if ((err = sysfs_create_group(&client->dev.kobj, 1348 if ((err = sysfs_create_group(&client->dev.kobj,
1328 &lm85_group_in4))) 1349 &lm85_group_in4)))
1329 goto err_remove_files; 1350 goto err_remove_files;
@@ -1344,10 +1365,7 @@ static int lm85_probe(struct i2c_client *client,
1344 1365
1345 /* Error out and cleanup code */ 1366 /* Error out and cleanup code */
1346 err_remove_files: 1367 err_remove_files:
1347 sysfs_remove_group(&client->dev.kobj, &lm85_group); 1368 lm85_remove_files(client, data);
1348 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1349 if (data->type == emc6d100)
1350 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1351 err_kfree: 1369 err_kfree:
1352 kfree(data); 1370 kfree(data);
1353 return err; 1371 return err;
@@ -1357,10 +1375,7 @@ static int lm85_remove(struct i2c_client *client)
1357{ 1375{
1358 struct lm85_data *data = i2c_get_clientdata(client); 1376 struct lm85_data *data = i2c_get_clientdata(client);
1359 hwmon_device_unregister(data->hwmon_dev); 1377 hwmon_device_unregister(data->hwmon_dev);
1360 sysfs_remove_group(&client->dev.kobj, &lm85_group); 1378 lm85_remove_files(client, data);
1361 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1362 if (data->type == emc6d100)
1363 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1364 kfree(data); 1379 kfree(data);
1365 return 0; 1380 return 0;
1366} 1381}
@@ -1457,11 +1472,8 @@ static struct lm85_data *lm85_update_device(struct device *dev)
1457 lm85_read_value(client, LM85_REG_FAN(i)); 1472 lm85_read_value(client, LM85_REG_FAN(i));
1458 } 1473 }
1459 1474
1460 if (!((data->type == adt7463 || data->type == adt7468) && 1475 if (!data->has_vid5)
1461 (data->vid & 0x80))) { 1476 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
1462 data->in[4] = lm85_read_value(client,
1463 LM85_REG_IN(4));
1464 }
1465 1477
1466 if (data->type == adt7468) 1478 if (data->type == adt7468)
1467 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5); 1479 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
@@ -1487,7 +1499,8 @@ static struct lm85_data *lm85_update_device(struct device *dev)
1487 /* More alarm bits */ 1499 /* More alarm bits */
1488 data->alarms |= lm85_read_value(client, 1500 data->alarms |= lm85_read_value(client,
1489 EMC6D100_REG_ALARM3) << 16; 1501 EMC6D100_REG_ALARM3) << 16;
1490 } else if (data->type == emc6d102 || data->type == emc6d103) { 1502 } else if (data->type == emc6d102 || data->type == emc6d103 ||
1503 data->type == emc6d103s) {
1491 /* Have to read LSB bits after the MSB ones because 1504 /* Have to read LSB bits after the MSB ones because
1492 the reading of the MSB bits has frozen the 1505 the reading of the MSB bits has frozen the
1493 LSBs (backward from the ADM1027). 1506 LSBs (backward from the ADM1027).
@@ -1528,8 +1541,7 @@ static struct lm85_data *lm85_update_device(struct device *dev)
1528 lm85_read_value(client, LM85_REG_FAN_MIN(i)); 1541 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1529 } 1542 }
1530 1543
1531 if (!((data->type == adt7463 || data->type == adt7468) && 1544 if (!data->has_vid5) {
1532 (data->vid & 0x80))) {
1533 data->in_min[4] = lm85_read_value(client, 1545 data->in_min[4] = lm85_read_value(client,
1534 LM85_REG_IN_MIN(4)); 1546 LM85_REG_IN_MIN(4));
1535 data->in_max[4] = lm85_read_value(client, 1547 data->in_max[4] = lm85_read_value(client,
@@ -1573,17 +1585,19 @@ static struct lm85_data *lm85_update_device(struct device *dev)
1573 } 1585 }
1574 } 1586 }
1575 1587
1576 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1); 1588 if (data->type != emc6d103s) {
1577 data->autofan[0].min_off = (i & 0x20) != 0; 1589 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1578 data->autofan[1].min_off = (i & 0x40) != 0; 1590 data->autofan[0].min_off = (i & 0x20) != 0;
1579 data->autofan[2].min_off = (i & 0x80) != 0; 1591 data->autofan[1].min_off = (i & 0x40) != 0;
1592 data->autofan[2].min_off = (i & 0x80) != 0;
1580 1593
1581 i = lm85_read_value(client, LM85_REG_AFAN_HYST1); 1594 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1582 data->zone[0].hyst = i >> 4; 1595 data->zone[0].hyst = i >> 4;
1583 data->zone[1].hyst = i & 0x0f; 1596 data->zone[1].hyst = i & 0x0f;
1584 1597
1585 i = lm85_read_value(client, LM85_REG_AFAN_HYST2); 1598 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1586 data->zone[2].hyst = i >> 4; 1599 data->zone[2].hyst = i >> 4;
1600 }
1587 1601
1588 data->last_config = jiffies; 1602 data->last_config = jiffies;
1589 } /* last_config */ 1603 } /* last_config */
diff --git a/drivers/hwmon/ltc4151.c b/drivers/hwmon/ltc4151.c
new file mode 100644
index 000000000000..4ac06b75aa60
--- /dev/null
+++ b/drivers/hwmon/ltc4151.c
@@ -0,0 +1,256 @@
1/*
2 * Driver for Linear Technology LTC4151 High Voltage I2C Current
3 * and Voltage Monitor
4 *
5 * Copyright (C) 2011 AppearTV AS
6 *
7 * Derived from:
8 *
9 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
10 * Swap Controller
11 * Copyright (C) 2010 Ericsson AB.
12 *
13 * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/err.h>
35#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/hwmon.h>
38#include <linux/hwmon-sysfs.h>
39
40/* chip registers */
41#define LTC4151_SENSE_H 0x00
42#define LTC4151_SENSE_L 0x01
43#define LTC4151_VIN_H 0x02
44#define LTC4151_VIN_L 0x03
45#define LTC4151_ADIN_H 0x04
46#define LTC4151_ADIN_L 0x05
47
48struct ltc4151_data {
49 struct device *hwmon_dev;
50
51 struct mutex update_lock;
52 bool valid;
53 unsigned long last_updated; /* in jiffies */
54
55 /* Registers */
56 u8 regs[6];
57};
58
59static struct ltc4151_data *ltc4151_update_device(struct device *dev)
60{
61 struct i2c_client *client = to_i2c_client(dev);
62 struct ltc4151_data *data = i2c_get_clientdata(client);
63 struct ltc4151_data *ret = data;
64
65 mutex_lock(&data->update_lock);
66
67 /*
68 * The chip's A/D updates 6 times per second
69 * (Conversion Rate 6 - 9 Hz)
70 */
71 if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
72 int i;
73
74 dev_dbg(&client->dev, "Starting ltc4151 update\n");
75
76 /* Read all registers */
77 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
78 int val;
79
80 val = i2c_smbus_read_byte_data(client, i);
81 if (unlikely(val < 0)) {
82 dev_dbg(dev,
83 "Failed to read ADC value: error %d\n",
84 val);
85 ret = ERR_PTR(val);
86 goto abort;
87 }
88 data->regs[i] = val;
89 }
90 data->last_updated = jiffies;
91 data->valid = 1;
92 }
93abort:
94 mutex_unlock(&data->update_lock);
95 return ret;
96}
97
98/* Return the voltage from the given register in mV */
99static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
100{
101 u32 val;
102
103 val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
104
105 switch (reg) {
106 case LTC4151_ADIN_H:
107 /* 500uV resolution. Convert to mV. */
108 val = val * 500 / 1000;
109 break;
110 case LTC4151_SENSE_H:
111 /*
112 * 20uV resolution. Convert to current as measured with
113 * an 1 mOhm sense resistor, in mA.
114 */
115 val = val * 20;
116 break;
117 case LTC4151_VIN_H:
118 /* 25 mV per increment */
119 val = val * 25;
120 break;
121 default:
122 /* If we get here, the developer messed up */
123 WARN_ON_ONCE(1);
124 val = 0;
125 break;
126 }
127
128 return val;
129}
130
131static ssize_t ltc4151_show_value(struct device *dev,
132 struct device_attribute *da, char *buf)
133{
134 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
135 struct ltc4151_data *data = ltc4151_update_device(dev);
136 int value;
137
138 if (IS_ERR(data))
139 return PTR_ERR(data);
140
141 value = ltc4151_get_value(data, attr->index);
142 return snprintf(buf, PAGE_SIZE, "%d\n", value);
143}
144
145/*
146 * Input voltages.
147 */
148static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, \
149 ltc4151_show_value, NULL, LTC4151_VIN_H);
150static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, \
151 ltc4151_show_value, NULL, LTC4151_ADIN_H);
152
153/* Currents (via sense resistor) */
154static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, \
155 ltc4151_show_value, NULL, LTC4151_SENSE_H);
156
157/* Finally, construct an array of pointers to members of the above objects,
158 * as required for sysfs_create_group()
159 */
160static struct attribute *ltc4151_attributes[] = {
161 &sensor_dev_attr_in1_input.dev_attr.attr,
162 &sensor_dev_attr_in2_input.dev_attr.attr,
163
164 &sensor_dev_attr_curr1_input.dev_attr.attr,
165
166 NULL,
167};
168
169static const struct attribute_group ltc4151_group = {
170 .attrs = ltc4151_attributes,
171};
172
173static int ltc4151_probe(struct i2c_client *client,
174 const struct i2c_device_id *id)
175{
176 struct i2c_adapter *adapter = client->adapter;
177 struct ltc4151_data *data;
178 int ret;
179
180 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
181 return -ENODEV;
182
183 data = kzalloc(sizeof(*data), GFP_KERNEL);
184 if (!data) {
185 ret = -ENOMEM;
186 goto out_kzalloc;
187 }
188
189 i2c_set_clientdata(client, data);
190 mutex_init(&data->update_lock);
191
192 /* Register sysfs hooks */
193 ret = sysfs_create_group(&client->dev.kobj, &ltc4151_group);
194 if (ret)
195 goto out_sysfs_create_group;
196
197 data->hwmon_dev = hwmon_device_register(&client->dev);
198 if (IS_ERR(data->hwmon_dev)) {
199 ret = PTR_ERR(data->hwmon_dev);
200 goto out_hwmon_device_register;
201 }
202
203 return 0;
204
205out_hwmon_device_register:
206 sysfs_remove_group(&client->dev.kobj, &ltc4151_group);
207out_sysfs_create_group:
208 kfree(data);
209out_kzalloc:
210 return ret;
211}
212
213static int ltc4151_remove(struct i2c_client *client)
214{
215 struct ltc4151_data *data = i2c_get_clientdata(client);
216
217 hwmon_device_unregister(data->hwmon_dev);
218 sysfs_remove_group(&client->dev.kobj, &ltc4151_group);
219
220 kfree(data);
221
222 return 0;
223}
224
225static const struct i2c_device_id ltc4151_id[] = {
226 { "ltc4151", 0 },
227 { }
228};
229MODULE_DEVICE_TABLE(i2c, ltc4151_id);
230
231/* This is the driver that will be inserted */
232static struct i2c_driver ltc4151_driver = {
233 .driver = {
234 .name = "ltc4151",
235 },
236 .probe = ltc4151_probe,
237 .remove = ltc4151_remove,
238 .id_table = ltc4151_id,
239};
240
241static int __init ltc4151_init(void)
242{
243 return i2c_add_driver(&ltc4151_driver);
244}
245
246static void __exit ltc4151_exit(void)
247{
248 i2c_del_driver(&ltc4151_driver);
249}
250
251MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
252MODULE_DESCRIPTION("LTC4151 driver");
253MODULE_LICENSE("GPL");
254
255module_init(ltc4151_init);
256module_exit(ltc4151_exit);
diff --git a/drivers/hwmon/max16064.c b/drivers/hwmon/max16064.c
new file mode 100644
index 000000000000..1d6d717060d3
--- /dev/null
+++ b/drivers/hwmon/max16064.c
@@ -0,0 +1,91 @@
1/*
2 * Hardware monitoring driver for Maxim MAX16064
3 *
4 * Copyright (c) 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/i2c.h>
26#include "pmbus.h"
27
28static struct pmbus_driver_info max16064_info = {
29 .pages = 4,
30 .direct[PSC_VOLTAGE_IN] = true,
31 .direct[PSC_VOLTAGE_OUT] = true,
32 .direct[PSC_TEMPERATURE] = true,
33 .m[PSC_VOLTAGE_IN] = 19995,
34 .b[PSC_VOLTAGE_IN] = 0,
35 .R[PSC_VOLTAGE_IN] = -1,
36 .m[PSC_VOLTAGE_OUT] = 19995,
37 .b[PSC_VOLTAGE_OUT] = 0,
38 .R[PSC_VOLTAGE_OUT] = -1,
39 .m[PSC_TEMPERATURE] = -7612,
40 .b[PSC_TEMPERATURE] = 335,
41 .R[PSC_TEMPERATURE] = -3,
42 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_TEMP
43 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_TEMP,
44 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT,
45 .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT,
46 .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT,
47};
48
49static int max16064_probe(struct i2c_client *client,
50 const struct i2c_device_id *id)
51{
52 return pmbus_do_probe(client, id, &max16064_info);
53}
54
55static int max16064_remove(struct i2c_client *client)
56{
57 return pmbus_do_remove(client);
58}
59
60static const struct i2c_device_id max16064_id[] = {
61 {"max16064", 0},
62 {}
63};
64
65MODULE_DEVICE_TABLE(i2c, max16064_id);
66
67/* This is the driver that will be inserted */
68static struct i2c_driver max16064_driver = {
69 .driver = {
70 .name = "max16064",
71 },
72 .probe = max16064_probe,
73 .remove = max16064_remove,
74 .id_table = max16064_id,
75};
76
77static int __init max16064_init(void)
78{
79 return i2c_add_driver(&max16064_driver);
80}
81
82static void __exit max16064_exit(void)
83{
84 i2c_del_driver(&max16064_driver);
85}
86
87MODULE_AUTHOR("Guenter Roeck");
88MODULE_DESCRIPTION("PMBus driver for Maxim MAX16064");
89MODULE_LICENSE("GPL");
90module_init(max16064_init);
91module_exit(max16064_exit);
diff --git a/drivers/hwmon/max34440.c b/drivers/hwmon/max34440.c
new file mode 100644
index 000000000000..992b701b4c5e
--- /dev/null
+++ b/drivers/hwmon/max34440.c
@@ -0,0 +1,199 @@
1/*
2 * Hardware monitoring driver for Maxim MAX34440/MAX34441
3 *
4 * Copyright (c) 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/i2c.h>
26#include "pmbus.h"
27
28enum chips { max34440, max34441 };
29
30#define MAX34440_STATUS_OC_WARN (1 << 0)
31#define MAX34440_STATUS_OC_FAULT (1 << 1)
32#define MAX34440_STATUS_OT_FAULT (1 << 5)
33#define MAX34440_STATUS_OT_WARN (1 << 6)
34
35static int max34440_get_status(struct i2c_client *client, int page, int reg)
36{
37 int ret;
38 int mfg_status;
39
40 ret = pmbus_set_page(client, page);
41 if (ret < 0)
42 return ret;
43
44 switch (reg) {
45 case PMBUS_STATUS_IOUT:
46 mfg_status = pmbus_read_word_data(client, 0,
47 PMBUS_STATUS_MFR_SPECIFIC);
48 if (mfg_status < 0)
49 return mfg_status;
50 if (mfg_status & MAX34440_STATUS_OC_WARN)
51 ret |= PB_IOUT_OC_WARNING;
52 if (mfg_status & MAX34440_STATUS_OC_FAULT)
53 ret |= PB_IOUT_OC_FAULT;
54 break;
55 case PMBUS_STATUS_TEMPERATURE:
56 mfg_status = pmbus_read_word_data(client, 0,
57 PMBUS_STATUS_MFR_SPECIFIC);
58 if (mfg_status < 0)
59 return mfg_status;
60 if (mfg_status & MAX34440_STATUS_OT_WARN)
61 ret |= PB_TEMP_OT_WARNING;
62 if (mfg_status & MAX34440_STATUS_OT_FAULT)
63 ret |= PB_TEMP_OT_FAULT;
64 break;
65 default:
66 ret = -ENODATA;
67 break;
68 }
69 return ret;
70}
71
72static struct pmbus_driver_info max34440_info[] = {
73 [max34440] = {
74 .pages = 14,
75 .direct[PSC_VOLTAGE_IN] = true,
76 .direct[PSC_VOLTAGE_OUT] = true,
77 .direct[PSC_TEMPERATURE] = true,
78 .direct[PSC_CURRENT_OUT] = true,
79 .m[PSC_VOLTAGE_IN] = 1,
80 .b[PSC_VOLTAGE_IN] = 0,
81 .R[PSC_VOLTAGE_IN] = 3, /* R = 0 in datasheet reflects mV */
82 .m[PSC_VOLTAGE_OUT] = 1,
83 .b[PSC_VOLTAGE_OUT] = 0,
84 .R[PSC_VOLTAGE_OUT] = 3, /* R = 0 in datasheet reflects mV */
85 .m[PSC_CURRENT_OUT] = 1,
86 .b[PSC_CURRENT_OUT] = 0,
87 .R[PSC_CURRENT_OUT] = 3, /* R = 0 in datasheet reflects mA */
88 .m[PSC_TEMPERATURE] = 1,
89 .b[PSC_TEMPERATURE] = 0,
90 .R[PSC_TEMPERATURE] = 2,
91 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
92 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
93 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
94 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
95 .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
96 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
97 .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
98 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
99 .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
100 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
101 .func[5] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
102 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
103 .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
104 .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
105 .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
106 .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
107 .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
108 .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
109 .func[12] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
110 .func[13] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
111 .get_status = max34440_get_status,
112 },
113 [max34441] = {
114 .pages = 12,
115 .direct[PSC_VOLTAGE_IN] = true,
116 .direct[PSC_VOLTAGE_OUT] = true,
117 .direct[PSC_TEMPERATURE] = true,
118 .direct[PSC_CURRENT_OUT] = true,
119 .direct[PSC_FAN] = true,
120 .m[PSC_VOLTAGE_IN] = 1,
121 .b[PSC_VOLTAGE_IN] = 0,
122 .R[PSC_VOLTAGE_IN] = 3,
123 .m[PSC_VOLTAGE_OUT] = 1,
124 .b[PSC_VOLTAGE_OUT] = 0,
125 .R[PSC_VOLTAGE_OUT] = 3,
126 .m[PSC_CURRENT_OUT] = 1,
127 .b[PSC_CURRENT_OUT] = 0,
128 .R[PSC_CURRENT_OUT] = 3,
129 .m[PSC_TEMPERATURE] = 1,
130 .b[PSC_TEMPERATURE] = 0,
131 .R[PSC_TEMPERATURE] = 2,
132 .m[PSC_FAN] = 1,
133 .b[PSC_FAN] = 0,
134 .R[PSC_FAN] = 0,
135 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
136 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
137 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
138 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
139 .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
140 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
141 .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
142 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
143 .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
144 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
145 .func[5] = PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12,
146 .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
147 .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
148 .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
149 .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
150 .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
151 .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
152 .get_status = max34440_get_status,
153 },
154};
155
156static int max34440_probe(struct i2c_client *client,
157 const struct i2c_device_id *id)
158{
159 return pmbus_do_probe(client, id, &max34440_info[id->driver_data]);
160}
161
162static int max34440_remove(struct i2c_client *client)
163{
164 return pmbus_do_remove(client);
165}
166
167static const struct i2c_device_id max34440_id[] = {
168 {"max34440", max34440},
169 {"max34441", max34441},
170 {}
171};
172
173MODULE_DEVICE_TABLE(i2c, max34440_id);
174
175/* This is the driver that will be inserted */
176static struct i2c_driver max34440_driver = {
177 .driver = {
178 .name = "max34440",
179 },
180 .probe = max34440_probe,
181 .remove = max34440_remove,
182 .id_table = max34440_id,
183};
184
185static int __init max34440_init(void)
186{
187 return i2c_add_driver(&max34440_driver);
188}
189
190static void __exit max34440_exit(void)
191{
192 i2c_del_driver(&max34440_driver);
193}
194
195MODULE_AUTHOR("Guenter Roeck");
196MODULE_DESCRIPTION("PMBus driver for Maxim MAX34440/MAX34441");
197MODULE_LICENSE("GPL");
198module_init(max34440_init);
199module_exit(max34440_exit);
diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c
new file mode 100644
index 000000000000..f20d9978ee78
--- /dev/null
+++ b/drivers/hwmon/max6639.c
@@ -0,0 +1,653 @@
1/*
2 * max6639.c - Support for Maxim MAX6639
3 *
4 * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
5 *
6 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
7 *
8 * based on the initial MAX6639 support from semptian.net
9 * by He Changqing <hechangqing@semptian.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
31#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
33#include <linux/err.h>
34#include <linux/mutex.h>
35#include <linux/i2c/max6639.h>
36
37/* Addresses to scan */
38static unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
39
40/* The MAX6639 registers, valid channel numbers: 0, 1 */
41#define MAX6639_REG_TEMP(ch) (0x00 + (ch))
42#define MAX6639_REG_STATUS 0x02
43#define MAX6639_REG_OUTPUT_MASK 0x03
44#define MAX6639_REG_GCONFIG 0x04
45#define MAX6639_REG_TEMP_EXT(ch) (0x05 + (ch))
46#define MAX6639_REG_ALERT_LIMIT(ch) (0x08 + (ch))
47#define MAX6639_REG_OT_LIMIT(ch) (0x0A + (ch))
48#define MAX6639_REG_THERM_LIMIT(ch) (0x0C + (ch))
49#define MAX6639_REG_FAN_CONFIG1(ch) (0x10 + (ch) * 4)
50#define MAX6639_REG_FAN_CONFIG2a(ch) (0x11 + (ch) * 4)
51#define MAX6639_REG_FAN_CONFIG2b(ch) (0x12 + (ch) * 4)
52#define MAX6639_REG_FAN_CONFIG3(ch) (0x13 + (ch) * 4)
53#define MAX6639_REG_FAN_CNT(ch) (0x20 + (ch))
54#define MAX6639_REG_TARGET_CNT(ch) (0x22 + (ch))
55#define MAX6639_REG_FAN_PPR(ch) (0x24 + (ch))
56#define MAX6639_REG_TARGTDUTY(ch) (0x26 + (ch))
57#define MAX6639_REG_FAN_START_TEMP(ch) (0x28 + (ch))
58#define MAX6639_REG_DEVID 0x3D
59#define MAX6639_REG_MANUID 0x3E
60#define MAX6639_REG_DEVREV 0x3F
61
62/* Register bits */
63#define MAX6639_GCONFIG_STANDBY 0x80
64#define MAX6639_GCONFIG_POR 0x40
65#define MAX6639_GCONFIG_DISABLE_TIMEOUT 0x20
66#define MAX6639_GCONFIG_CH2_LOCAL 0x10
67#define MAX6639_GCONFIG_PWM_FREQ_HI 0x08
68
69#define MAX6639_FAN_CONFIG1_PWM 0x80
70
71#define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40
72
73static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
74
75#define FAN_FROM_REG(val, div, rpm_range) ((val) == 0 ? -1 : \
76 (val) == 255 ? 0 : (rpm_ranges[rpm_range] * 30) / ((div + 1) * (val)))
77#define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) / 1000, 0, 255)
78
79/*
80 * Client data (each client gets its own)
81 */
82struct max6639_data {
83 struct device *hwmon_dev;
84 struct mutex update_lock;
85 char valid; /* !=0 if following fields are valid */
86 unsigned long last_updated; /* In jiffies */
87
88 /* Register values sampled regularly */
89 u16 temp[2]; /* Temperature, in 1/8 C, 0..255 C */
90 bool temp_fault[2]; /* Detected temperature diode failure */
91 u8 fan[2]; /* Register value: TACH count for fans >=30 */
92 u8 status; /* Detected channel alarms and fan failures */
93
94 /* Register values only written to */
95 u8 pwm[2]; /* Register value: Duty cycle 0..120 */
96 u8 temp_therm[2]; /* THERM Temperature, 0..255 C (->_max) */
97 u8 temp_alert[2]; /* ALERT Temperature, 0..255 C (->_crit) */
98 u8 temp_ot[2]; /* OT Temperature, 0..255 C (->_emergency) */
99
100 /* Register values initialized only once */
101 u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */
102 u8 rpm_range; /* Index in above rpm_ranges table */
103};
104
105static struct max6639_data *max6639_update_device(struct device *dev)
106{
107 struct i2c_client *client = to_i2c_client(dev);
108 struct max6639_data *data = i2c_get_clientdata(client);
109 struct max6639_data *ret = data;
110 int i;
111 int status_reg;
112
113 mutex_lock(&data->update_lock);
114
115 if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
116 int res;
117
118 dev_dbg(&client->dev, "Starting max6639 update\n");
119
120 status_reg = i2c_smbus_read_byte_data(client,
121 MAX6639_REG_STATUS);
122 if (status_reg < 0) {
123 ret = ERR_PTR(status_reg);
124 goto abort;
125 }
126
127 data->status = status_reg;
128
129 for (i = 0; i < 2; i++) {
130 res = i2c_smbus_read_byte_data(client,
131 MAX6639_REG_FAN_CNT(i));
132 if (res < 0) {
133 ret = ERR_PTR(res);
134 goto abort;
135 }
136 data->fan[i] = res;
137
138 res = i2c_smbus_read_byte_data(client,
139 MAX6639_REG_TEMP_EXT(i));
140 if (res < 0) {
141 ret = ERR_PTR(res);
142 goto abort;
143 }
144 data->temp[i] = res >> 5;
145 data->temp_fault[i] = res & 0x01;
146
147 res = i2c_smbus_read_byte_data(client,
148 MAX6639_REG_TEMP(i));
149 if (res < 0) {
150 ret = ERR_PTR(res);
151 goto abort;
152 }
153 data->temp[i] |= res << 3;
154 }
155
156 data->last_updated = jiffies;
157 data->valid = 1;
158 }
159abort:
160 mutex_unlock(&data->update_lock);
161
162 return ret;
163}
164
165static ssize_t show_temp_input(struct device *dev,
166 struct device_attribute *dev_attr, char *buf)
167{
168 long temp;
169 struct max6639_data *data = max6639_update_device(dev);
170 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
171
172 if (IS_ERR(data))
173 return PTR_ERR(data);
174
175 temp = data->temp[attr->index] * 125;
176 return sprintf(buf, "%ld\n", temp);
177}
178
179static ssize_t show_temp_fault(struct device *dev,
180 struct device_attribute *dev_attr, char *buf)
181{
182 struct max6639_data *data = max6639_update_device(dev);
183 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
184
185 if (IS_ERR(data))
186 return PTR_ERR(data);
187
188 return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
189}
190
191static ssize_t show_temp_max(struct device *dev,
192 struct device_attribute *dev_attr, char *buf)
193{
194 struct i2c_client *client = to_i2c_client(dev);
195 struct max6639_data *data = i2c_get_clientdata(client);
196 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
197
198 return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
199}
200
201static ssize_t set_temp_max(struct device *dev,
202 struct device_attribute *dev_attr,
203 const char *buf, size_t count)
204{
205 struct i2c_client *client = to_i2c_client(dev);
206 struct max6639_data *data = i2c_get_clientdata(client);
207 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
208 unsigned long val;
209 int res;
210
211 res = strict_strtoul(buf, 10, &val);
212 if (res)
213 return res;
214
215 mutex_lock(&data->update_lock);
216 data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
217 i2c_smbus_write_byte_data(client,
218 MAX6639_REG_THERM_LIMIT(attr->index),
219 data->temp_therm[attr->index]);
220 mutex_unlock(&data->update_lock);
221 return count;
222}
223
224static ssize_t show_temp_crit(struct device *dev,
225 struct device_attribute *dev_attr, char *buf)
226{
227 struct i2c_client *client = to_i2c_client(dev);
228 struct max6639_data *data = i2c_get_clientdata(client);
229 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
230
231 return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
232}
233
234static ssize_t set_temp_crit(struct device *dev,
235 struct device_attribute *dev_attr,
236 const char *buf, size_t count)
237{
238 struct i2c_client *client = to_i2c_client(dev);
239 struct max6639_data *data = i2c_get_clientdata(client);
240 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
241 unsigned long val;
242 int res;
243
244 res = strict_strtoul(buf, 10, &val);
245 if (res)
246 return res;
247
248 mutex_lock(&data->update_lock);
249 data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
250 i2c_smbus_write_byte_data(client,
251 MAX6639_REG_ALERT_LIMIT(attr->index),
252 data->temp_alert[attr->index]);
253 mutex_unlock(&data->update_lock);
254 return count;
255}
256
257static ssize_t show_temp_emergency(struct device *dev,
258 struct device_attribute *dev_attr,
259 char *buf)
260{
261 struct i2c_client *client = to_i2c_client(dev);
262 struct max6639_data *data = i2c_get_clientdata(client);
263 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
264
265 return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
266}
267
268static ssize_t set_temp_emergency(struct device *dev,
269 struct device_attribute *dev_attr,
270 const char *buf, size_t count)
271{
272 struct i2c_client *client = to_i2c_client(dev);
273 struct max6639_data *data = i2c_get_clientdata(client);
274 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
275 unsigned long val;
276 int res;
277
278 res = strict_strtoul(buf, 10, &val);
279 if (res)
280 return res;
281
282 mutex_lock(&data->update_lock);
283 data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
284 i2c_smbus_write_byte_data(client,
285 MAX6639_REG_OT_LIMIT(attr->index),
286 data->temp_ot[attr->index]);
287 mutex_unlock(&data->update_lock);
288 return count;
289}
290
291static ssize_t show_pwm(struct device *dev,
292 struct device_attribute *dev_attr, char *buf)
293{
294 struct i2c_client *client = to_i2c_client(dev);
295 struct max6639_data *data = i2c_get_clientdata(client);
296 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
297
298 return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
299}
300
301static ssize_t set_pwm(struct device *dev,
302 struct device_attribute *dev_attr,
303 const char *buf, size_t count)
304{
305 struct i2c_client *client = to_i2c_client(dev);
306 struct max6639_data *data = i2c_get_clientdata(client);
307 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
308 unsigned long val;
309 int res;
310
311 res = strict_strtoul(buf, 10, &val);
312 if (res)
313 return res;
314
315 val = SENSORS_LIMIT(val, 0, 255);
316
317 mutex_lock(&data->update_lock);
318 data->pwm[attr->index] = (u8)(val * 120 / 255);
319 i2c_smbus_write_byte_data(client,
320 MAX6639_REG_TARGTDUTY(attr->index),
321 data->pwm[attr->index]);
322 mutex_unlock(&data->update_lock);
323 return count;
324}
325
326static ssize_t show_fan_input(struct device *dev,
327 struct device_attribute *dev_attr, char *buf)
328{
329 struct max6639_data *data = max6639_update_device(dev);
330 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
331
332 if (IS_ERR(data))
333 return PTR_ERR(data);
334
335 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
336 data->ppr, data->rpm_range));
337}
338
339static ssize_t show_alarm(struct device *dev,
340 struct device_attribute *dev_attr, char *buf)
341{
342 struct max6639_data *data = max6639_update_device(dev);
343 struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
344
345 if (IS_ERR(data))
346 return PTR_ERR(data);
347
348 return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
349}
350
351static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
352static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
353static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
354static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
355static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
356 set_temp_max, 0);
357static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
358 set_temp_max, 1);
359static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
360 set_temp_crit, 0);
361static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
362 set_temp_crit, 1);
363static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO,
364 show_temp_emergency, set_temp_emergency, 0);
365static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO,
366 show_temp_emergency, set_temp_emergency, 1);
367static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
368static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
369static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
370static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
371static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
372static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 0);
373static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 3);
374static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
375static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 7);
376static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 6);
377static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 5);
378static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 4);
379
380
381static struct attribute *max6639_attributes[] = {
382 &sensor_dev_attr_temp1_input.dev_attr.attr,
383 &sensor_dev_attr_temp2_input.dev_attr.attr,
384 &sensor_dev_attr_temp1_fault.dev_attr.attr,
385 &sensor_dev_attr_temp2_fault.dev_attr.attr,
386 &sensor_dev_attr_temp1_max.dev_attr.attr,
387 &sensor_dev_attr_temp2_max.dev_attr.attr,
388 &sensor_dev_attr_temp1_crit.dev_attr.attr,
389 &sensor_dev_attr_temp2_crit.dev_attr.attr,
390 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
391 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
392 &sensor_dev_attr_pwm1.dev_attr.attr,
393 &sensor_dev_attr_pwm2.dev_attr.attr,
394 &sensor_dev_attr_fan1_input.dev_attr.attr,
395 &sensor_dev_attr_fan2_input.dev_attr.attr,
396 &sensor_dev_attr_fan1_fault.dev_attr.attr,
397 &sensor_dev_attr_fan2_fault.dev_attr.attr,
398 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
399 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
400 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
401 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
402 &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
403 &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
404 NULL
405};
406
407static const struct attribute_group max6639_group = {
408 .attrs = max6639_attributes,
409};
410
411/*
412 * returns respective index in rpm_ranges table
413 * 1 by default on invalid range
414 */
415static int rpm_range_to_reg(int range)
416{
417 int i;
418
419 for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
420 if (rpm_ranges[i] == range)
421 return i;
422 }
423
424 return 1; /* default: 4000 RPM */
425}
426
427static int max6639_init_client(struct i2c_client *client)
428{
429 struct max6639_data *data = i2c_get_clientdata(client);
430 struct max6639_platform_data *max6639_info =
431 client->dev.platform_data;
432 int i = 0;
433 int rpm_range = 1; /* default: 4000 RPM */
434 int err = 0;
435
436 /* Reset chip to default values, see below for GCONFIG setup */
437 err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
438 MAX6639_GCONFIG_POR);
439 if (err)
440 goto exit;
441
442 /* Fans pulse per revolution is 2 by default */
443 if (max6639_info && max6639_info->ppr > 0 &&
444 max6639_info->ppr < 5)
445 data->ppr = max6639_info->ppr;
446 else
447 data->ppr = 2;
448 data->ppr -= 1;
449 err = i2c_smbus_write_byte_data(client,
450 MAX6639_REG_FAN_PPR(i),
451 data->ppr << 5);
452 if (err)
453 goto exit;
454
455 if (max6639_info)
456 rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
457 data->rpm_range = rpm_range;
458
459 for (i = 0; i < 2; i++) {
460
461 /* Fans config PWM, RPM */
462 err = i2c_smbus_write_byte_data(client,
463 MAX6639_REG_FAN_CONFIG1(i),
464 MAX6639_FAN_CONFIG1_PWM | rpm_range);
465 if (err)
466 goto exit;
467
468 /* Fans PWM polarity high by default */
469 if (max6639_info && max6639_info->pwm_polarity == 0)
470 err = i2c_smbus_write_byte_data(client,
471 MAX6639_REG_FAN_CONFIG2a(i), 0x00);
472 else
473 err = i2c_smbus_write_byte_data(client,
474 MAX6639_REG_FAN_CONFIG2a(i), 0x02);
475 if (err)
476 goto exit;
477
478 /*
479 * /THERM full speed enable,
480 * PWM frequency 25kHz, see also GCONFIG below
481 */
482 err = i2c_smbus_write_byte_data(client,
483 MAX6639_REG_FAN_CONFIG3(i),
484 MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
485 if (err)
486 goto exit;
487
488 /* Max. temp. 80C/90C/100C */
489 data->temp_therm[i] = 80;
490 data->temp_alert[i] = 90;
491 data->temp_ot[i] = 100;
492 err = i2c_smbus_write_byte_data(client,
493 MAX6639_REG_THERM_LIMIT(i),
494 data->temp_therm[i]);
495 if (err)
496 goto exit;
497 err = i2c_smbus_write_byte_data(client,
498 MAX6639_REG_ALERT_LIMIT(i),
499 data->temp_alert[i]);
500 if (err)
501 goto exit;
502 err = i2c_smbus_write_byte_data(client,
503 MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
504 if (err)
505 goto exit;
506
507 /* PWM 120/120 (i.e. 100%) */
508 data->pwm[i] = 120;
509 err = i2c_smbus_write_byte_data(client,
510 MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
511 if (err)
512 goto exit;
513 }
514 /* Start monitoring */
515 err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
516 MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
517 MAX6639_GCONFIG_PWM_FREQ_HI);
518exit:
519 return err;
520}
521
522/* Return 0 if detection is successful, -ENODEV otherwise */
523static int max6639_detect(struct i2c_client *client,
524 struct i2c_board_info *info)
525{
526 struct i2c_adapter *adapter = client->adapter;
527 int dev_id, manu_id;
528
529 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
530 return -ENODEV;
531
532 /* Actual detection via device and manufacturer ID */
533 dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
534 manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
535 if (dev_id != 0x58 || manu_id != 0x4D)
536 return -ENODEV;
537
538 strlcpy(info->type, "max6639", I2C_NAME_SIZE);
539
540 return 0;
541}
542
543static int max6639_probe(struct i2c_client *client,
544 const struct i2c_device_id *id)
545{
546 struct max6639_data *data;
547 int err;
548
549 data = kzalloc(sizeof(struct max6639_data), GFP_KERNEL);
550 if (!data) {
551 err = -ENOMEM;
552 goto exit;
553 }
554
555 i2c_set_clientdata(client, data);
556 mutex_init(&data->update_lock);
557
558 /* Initialize the max6639 chip */
559 err = max6639_init_client(client);
560 if (err < 0)
561 goto error_free;
562
563 /* Register sysfs hooks */
564 err = sysfs_create_group(&client->dev.kobj, &max6639_group);
565 if (err)
566 goto error_free;
567
568 data->hwmon_dev = hwmon_device_register(&client->dev);
569 if (IS_ERR(data->hwmon_dev)) {
570 err = PTR_ERR(data->hwmon_dev);
571 goto error_remove;
572 }
573
574 dev_info(&client->dev, "temperature sensor and fan control found\n");
575
576 return 0;
577
578error_remove:
579 sysfs_remove_group(&client->dev.kobj, &max6639_group);
580error_free:
581 kfree(data);
582exit:
583 return err;
584}
585
586static int max6639_remove(struct i2c_client *client)
587{
588 struct max6639_data *data = i2c_get_clientdata(client);
589
590 hwmon_device_unregister(data->hwmon_dev);
591 sysfs_remove_group(&client->dev.kobj, &max6639_group);
592
593 kfree(data);
594 return 0;
595}
596
597static int max6639_suspend(struct i2c_client *client, pm_message_t mesg)
598{
599 int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
600 if (data < 0)
601 return data;
602
603 return i2c_smbus_write_byte_data(client,
604 MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
605}
606
607static int max6639_resume(struct i2c_client *client)
608{
609 int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
610 if (data < 0)
611 return data;
612
613 return i2c_smbus_write_byte_data(client,
614 MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
615}
616
617static const struct i2c_device_id max6639_id[] = {
618 {"max6639", 0},
619 { }
620};
621
622MODULE_DEVICE_TABLE(i2c, max6639_id);
623
624static struct i2c_driver max6639_driver = {
625 .class = I2C_CLASS_HWMON,
626 .driver = {
627 .name = "max6639",
628 },
629 .probe = max6639_probe,
630 .remove = max6639_remove,
631 .suspend = max6639_suspend,
632 .resume = max6639_resume,
633 .id_table = max6639_id,
634 .detect = max6639_detect,
635 .address_list = normal_i2c,
636};
637
638static int __init max6639_init(void)
639{
640 return i2c_add_driver(&max6639_driver);
641}
642
643static void __exit max6639_exit(void)
644{
645 i2c_del_driver(&max6639_driver);
646}
647
648MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
649MODULE_DESCRIPTION("max6639 driver");
650MODULE_LICENSE("GPL");
651
652module_init(max6639_init);
653module_exit(max6639_exit);
diff --git a/drivers/hwmon/max8688.c b/drivers/hwmon/max8688.c
new file mode 100644
index 000000000000..8ebfef2ecf26
--- /dev/null
+++ b/drivers/hwmon/max8688.c
@@ -0,0 +1,158 @@
1/*
2 * Hardware monitoring driver for Maxim MAX8688
3 *
4 * Copyright (c) 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/i2c.h>
26#include "pmbus.h"
27
28#define MAX8688_MFG_STATUS 0xd8
29
30#define MAX8688_STATUS_OC_FAULT (1 << 4)
31#define MAX8688_STATUS_OV_FAULT (1 << 5)
32#define MAX8688_STATUS_OV_WARNING (1 << 8)
33#define MAX8688_STATUS_UV_FAULT (1 << 9)
34#define MAX8688_STATUS_UV_WARNING (1 << 10)
35#define MAX8688_STATUS_UC_FAULT (1 << 11)
36#define MAX8688_STATUS_OC_WARNING (1 << 12)
37#define MAX8688_STATUS_OT_FAULT (1 << 13)
38#define MAX8688_STATUS_OT_WARNING (1 << 14)
39
40static int max8688_get_status(struct i2c_client *client, int page, int reg)
41{
42 int ret = 0;
43 int mfg_status;
44
45 if (page)
46 return -EINVAL;
47
48 switch (reg) {
49 case PMBUS_STATUS_VOUT:
50 mfg_status = pmbus_read_word_data(client, 0,
51 MAX8688_MFG_STATUS);
52 if (mfg_status < 0)
53 return mfg_status;
54 if (mfg_status & MAX8688_STATUS_UV_WARNING)
55 ret |= PB_VOLTAGE_UV_WARNING;
56 if (mfg_status & MAX8688_STATUS_UV_FAULT)
57 ret |= PB_VOLTAGE_UV_FAULT;
58 if (mfg_status & MAX8688_STATUS_OV_WARNING)
59 ret |= PB_VOLTAGE_OV_WARNING;
60 if (mfg_status & MAX8688_STATUS_OV_FAULT)
61 ret |= PB_VOLTAGE_OV_FAULT;
62 break;
63 case PMBUS_STATUS_IOUT:
64 mfg_status = pmbus_read_word_data(client, 0,
65 MAX8688_MFG_STATUS);
66 if (mfg_status < 0)
67 return mfg_status;
68 if (mfg_status & MAX8688_STATUS_UC_FAULT)
69 ret |= PB_IOUT_UC_FAULT;
70 if (mfg_status & MAX8688_STATUS_OC_WARNING)
71 ret |= PB_IOUT_OC_WARNING;
72 if (mfg_status & MAX8688_STATUS_OC_FAULT)
73 ret |= PB_IOUT_OC_FAULT;
74 break;
75 case PMBUS_STATUS_TEMPERATURE:
76 mfg_status = pmbus_read_word_data(client, 0,
77 MAX8688_MFG_STATUS);
78 if (mfg_status < 0)
79 return mfg_status;
80 if (mfg_status & MAX8688_STATUS_OT_WARNING)
81 ret |= PB_TEMP_OT_WARNING;
82 if (mfg_status & MAX8688_STATUS_OT_FAULT)
83 ret |= PB_TEMP_OT_FAULT;
84 break;
85 default:
86 ret = -ENODATA;
87 break;
88 }
89 return ret;
90}
91
92static struct pmbus_driver_info max8688_info = {
93 .pages = 1,
94 .direct[PSC_VOLTAGE_IN] = true,
95 .direct[PSC_VOLTAGE_OUT] = true,
96 .direct[PSC_TEMPERATURE] = true,
97 .direct[PSC_CURRENT_OUT] = true,
98 .m[PSC_VOLTAGE_IN] = 19995,
99 .b[PSC_VOLTAGE_IN] = 0,
100 .R[PSC_VOLTAGE_IN] = -1,
101 .m[PSC_VOLTAGE_OUT] = 19995,
102 .b[PSC_VOLTAGE_OUT] = 0,
103 .R[PSC_VOLTAGE_OUT] = -1,
104 .m[PSC_CURRENT_OUT] = 23109,
105 .b[PSC_CURRENT_OUT] = 0,
106 .R[PSC_CURRENT_OUT] = -2,
107 .m[PSC_TEMPERATURE] = -7612,
108 .b[PSC_TEMPERATURE] = 335,
109 .R[PSC_TEMPERATURE] = -3,
110 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP
111 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
112 | PMBUS_HAVE_STATUS_TEMP,
113 .get_status = max8688_get_status,
114};
115
116static int max8688_probe(struct i2c_client *client,
117 const struct i2c_device_id *id)
118{
119 return pmbus_do_probe(client, id, &max8688_info);
120}
121
122static int max8688_remove(struct i2c_client *client)
123{
124 return pmbus_do_remove(client);
125}
126
127static const struct i2c_device_id max8688_id[] = {
128 {"max8688", 0},
129 { }
130};
131
132MODULE_DEVICE_TABLE(i2c, max8688_id);
133
134/* This is the driver that will be inserted */
135static struct i2c_driver max8688_driver = {
136 .driver = {
137 .name = "max8688",
138 },
139 .probe = max8688_probe,
140 .remove = max8688_remove,
141 .id_table = max8688_id,
142};
143
144static int __init max8688_init(void)
145{
146 return i2c_add_driver(&max8688_driver);
147}
148
149static void __exit max8688_exit(void)
150{
151 i2c_del_driver(&max8688_driver);
152}
153
154MODULE_AUTHOR("Guenter Roeck");
155MODULE_DESCRIPTION("PMBus driver for Maxim MAX8688");
156MODULE_LICENSE("GPL");
157module_init(max8688_init);
158module_exit(max8688_exit);
diff --git a/drivers/hwmon/pmbus.c b/drivers/hwmon/pmbus.c
new file mode 100644
index 000000000000..98e2e28899e2
--- /dev/null
+++ b/drivers/hwmon/pmbus.c
@@ -0,0 +1,203 @@
1/*
2 * Hardware monitoring driver for PMBus devices
3 *
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/mutex.h>
27#include <linux/i2c.h>
28#include "pmbus.h"
29
30/*
31 * Find sensor groups and status registers on each page.
32 */
33static void pmbus_find_sensor_groups(struct i2c_client *client,
34 struct pmbus_driver_info *info)
35{
36 int page;
37
38 /* Sensors detected on page 0 only */
39 if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
40 info->func[0] |= PMBUS_HAVE_VIN;
41 if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
42 info->func[0] |= PMBUS_HAVE_VCAP;
43 if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
44 info->func[0] |= PMBUS_HAVE_IIN;
45 if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
46 info->func[0] |= PMBUS_HAVE_PIN;
47 if (info->func[0]
48 && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
49 info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
50 if (pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
51 info->func[0] |= PMBUS_HAVE_FAN12;
52 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
53 info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
54 }
55 if (pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
56 info->func[0] |= PMBUS_HAVE_FAN34;
57 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
58 info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
59 }
60 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1)) {
61 info->func[0] |= PMBUS_HAVE_TEMP;
62 if (pmbus_check_byte_register(client, 0,
63 PMBUS_STATUS_TEMPERATURE))
64 info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
65 }
66
67 /* Sensors detected on all pages */
68 for (page = 0; page < info->pages; page++) {
69 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
70 info->func[page] |= PMBUS_HAVE_VOUT;
71 if (pmbus_check_byte_register(client, page,
72 PMBUS_STATUS_VOUT))
73 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
74 }
75 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
76 info->func[page] |= PMBUS_HAVE_IOUT;
77 if (pmbus_check_byte_register(client, 0,
78 PMBUS_STATUS_IOUT))
79 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
80 }
81 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
82 info->func[page] |= PMBUS_HAVE_POUT;
83 }
84}
85
86/*
87 * Identify chip parameters.
88 */
89static int pmbus_identify(struct i2c_client *client,
90 struct pmbus_driver_info *info)
91{
92 if (!info->pages) {
93 /*
94 * Check if the PAGE command is supported. If it is,
95 * keep setting the page number until it fails or until the
96 * maximum number of pages has been reached. Assume that
97 * this is the number of pages supported by the chip.
98 */
99 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
100 int page;
101
102 for (page = 1; page < PMBUS_PAGES; page++) {
103 if (pmbus_set_page(client, page) < 0)
104 break;
105 }
106 pmbus_set_page(client, 0);
107 info->pages = page;
108 } else {
109 info->pages = 1;
110 }
111 }
112
113 /*
114 * We should check if the COEFFICIENTS register is supported.
115 * If it is, and the chip is configured for direct mode, we can read
116 * the coefficients from the chip, one set per group of sensor
117 * registers.
118 *
119 * To do this, we will need access to a chip which actually supports the
120 * COEFFICIENTS command, since the command is too complex to implement
121 * without testing it.
122 */
123
124 /* Try to find sensor groups */
125 pmbus_find_sensor_groups(client, info);
126
127 return 0;
128}
129
130static int pmbus_probe(struct i2c_client *client,
131 const struct i2c_device_id *id)
132{
133 struct pmbus_driver_info *info;
134 int ret;
135
136 info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
137 if (!info)
138 return -ENOMEM;
139
140 info->pages = id->driver_data;
141 info->identify = pmbus_identify;
142
143 ret = pmbus_do_probe(client, id, info);
144 if (ret < 0)
145 goto out;
146 return 0;
147
148out:
149 kfree(info);
150 return ret;
151}
152
153static int pmbus_remove(struct i2c_client *client)
154{
155 int ret;
156 const struct pmbus_driver_info *info;
157
158 info = pmbus_get_driver_info(client);
159 ret = pmbus_do_remove(client);
160 kfree(info);
161 return ret;
162}
163
164/*
165 * Use driver_data to set the number of pages supported by the chip.
166 */
167static const struct i2c_device_id pmbus_id[] = {
168 {"bmr450", 1},
169 {"bmr451", 1},
170 {"bmr453", 1},
171 {"bmr454", 1},
172 {"ltc2978", 8},
173 {"pmbus", 0},
174 {}
175};
176
177MODULE_DEVICE_TABLE(i2c, pmbus_id);
178
179/* This is the driver that will be inserted */
180static struct i2c_driver pmbus_driver = {
181 .driver = {
182 .name = "pmbus",
183 },
184 .probe = pmbus_probe,
185 .remove = pmbus_remove,
186 .id_table = pmbus_id,
187};
188
189static int __init pmbus_init(void)
190{
191 return i2c_add_driver(&pmbus_driver);
192}
193
194static void __exit pmbus_exit(void)
195{
196 i2c_del_driver(&pmbus_driver);
197}
198
199MODULE_AUTHOR("Guenter Roeck");
200MODULE_DESCRIPTION("Generic PMBus driver");
201MODULE_LICENSE("GPL");
202module_init(pmbus_init);
203module_exit(pmbus_exit);
diff --git a/drivers/hwmon/pmbus.h b/drivers/hwmon/pmbus.h
new file mode 100644
index 000000000000..a81f7f228762
--- /dev/null
+++ b/drivers/hwmon/pmbus.h
@@ -0,0 +1,313 @@
1/*
2 * pmbus.h - Common defines and structures for PMBus devices
3 *
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#ifndef PMBUS_H
22#define PMBUS_H
23
24/*
25 * Registers
26 */
27#define PMBUS_PAGE 0x00
28#define PMBUS_OPERATION 0x01
29#define PMBUS_ON_OFF_CONFIG 0x02
30#define PMBUS_CLEAR_FAULTS 0x03
31#define PMBUS_PHASE 0x04
32
33#define PMBUS_CAPABILITY 0x19
34#define PMBUS_QUERY 0x1A
35
36#define PMBUS_VOUT_MODE 0x20
37#define PMBUS_VOUT_COMMAND 0x21
38#define PMBUS_VOUT_TRIM 0x22
39#define PMBUS_VOUT_CAL_OFFSET 0x23
40#define PMBUS_VOUT_MAX 0x24
41#define PMBUS_VOUT_MARGIN_HIGH 0x25
42#define PMBUS_VOUT_MARGIN_LOW 0x26
43#define PMBUS_VOUT_TRANSITION_RATE 0x27
44#define PMBUS_VOUT_DROOP 0x28
45#define PMBUS_VOUT_SCALE_LOOP 0x29
46#define PMBUS_VOUT_SCALE_MONITOR 0x2A
47
48#define PMBUS_COEFFICIENTS 0x30
49#define PMBUS_POUT_MAX 0x31
50
51#define PMBUS_FAN_CONFIG_12 0x3A
52#define PMBUS_FAN_COMMAND_1 0x3B
53#define PMBUS_FAN_COMMAND_2 0x3C
54#define PMBUS_FAN_CONFIG_34 0x3D
55#define PMBUS_FAN_COMMAND_3 0x3E
56#define PMBUS_FAN_COMMAND_4 0x3F
57
58#define PMBUS_VOUT_OV_FAULT_LIMIT 0x40
59#define PMBUS_VOUT_OV_FAULT_RESPONSE 0x41
60#define PMBUS_VOUT_OV_WARN_LIMIT 0x42
61#define PMBUS_VOUT_UV_WARN_LIMIT 0x43
62#define PMBUS_VOUT_UV_FAULT_LIMIT 0x44
63#define PMBUS_VOUT_UV_FAULT_RESPONSE 0x45
64#define PMBUS_IOUT_OC_FAULT_LIMIT 0x46
65#define PMBUS_IOUT_OC_FAULT_RESPONSE 0x47
66#define PMBUS_IOUT_OC_LV_FAULT_LIMIT 0x48
67#define PMBUS_IOUT_OC_LV_FAULT_RESPONSE 0x49
68#define PMBUS_IOUT_OC_WARN_LIMIT 0x4A
69#define PMBUS_IOUT_UC_FAULT_LIMIT 0x4B
70#define PMBUS_IOUT_UC_FAULT_RESPONSE 0x4C
71
72#define PMBUS_OT_FAULT_LIMIT 0x4F
73#define PMBUS_OT_FAULT_RESPONSE 0x50
74#define PMBUS_OT_WARN_LIMIT 0x51
75#define PMBUS_UT_WARN_LIMIT 0x52
76#define PMBUS_UT_FAULT_LIMIT 0x53
77#define PMBUS_UT_FAULT_RESPONSE 0x54
78#define PMBUS_VIN_OV_FAULT_LIMIT 0x55
79#define PMBUS_VIN_OV_FAULT_RESPONSE 0x56
80#define PMBUS_VIN_OV_WARN_LIMIT 0x57
81#define PMBUS_VIN_UV_WARN_LIMIT 0x58
82#define PMBUS_VIN_UV_FAULT_LIMIT 0x59
83
84#define PMBUS_IIN_OC_FAULT_LIMIT 0x5B
85#define PMBUS_IIN_OC_WARN_LIMIT 0x5D
86
87#define PMBUS_POUT_OP_FAULT_LIMIT 0x68
88#define PMBUS_POUT_OP_WARN_LIMIT 0x6A
89#define PMBUS_PIN_OP_WARN_LIMIT 0x6B
90
91#define PMBUS_STATUS_BYTE 0x78
92#define PMBUS_STATUS_WORD 0x79
93#define PMBUS_STATUS_VOUT 0x7A
94#define PMBUS_STATUS_IOUT 0x7B
95#define PMBUS_STATUS_INPUT 0x7C
96#define PMBUS_STATUS_TEMPERATURE 0x7D
97#define PMBUS_STATUS_CML 0x7E
98#define PMBUS_STATUS_OTHER 0x7F
99#define PMBUS_STATUS_MFR_SPECIFIC 0x80
100#define PMBUS_STATUS_FAN_12 0x81
101#define PMBUS_STATUS_FAN_34 0x82
102
103#define PMBUS_READ_VIN 0x88
104#define PMBUS_READ_IIN 0x89
105#define PMBUS_READ_VCAP 0x8A
106#define PMBUS_READ_VOUT 0x8B
107#define PMBUS_READ_IOUT 0x8C
108#define PMBUS_READ_TEMPERATURE_1 0x8D
109#define PMBUS_READ_TEMPERATURE_2 0x8E
110#define PMBUS_READ_TEMPERATURE_3 0x8F
111#define PMBUS_READ_FAN_SPEED_1 0x90
112#define PMBUS_READ_FAN_SPEED_2 0x91
113#define PMBUS_READ_FAN_SPEED_3 0x92
114#define PMBUS_READ_FAN_SPEED_4 0x93
115#define PMBUS_READ_DUTY_CYCLE 0x94
116#define PMBUS_READ_FREQUENCY 0x95
117#define PMBUS_READ_POUT 0x96
118#define PMBUS_READ_PIN 0x97
119
120#define PMBUS_REVISION 0x98
121#define PMBUS_MFR_ID 0x99
122#define PMBUS_MFR_MODEL 0x9A
123#define PMBUS_MFR_REVISION 0x9B
124#define PMBUS_MFR_LOCATION 0x9C
125#define PMBUS_MFR_DATE 0x9D
126#define PMBUS_MFR_SERIAL 0x9E
127
128/*
129 * CAPABILITY
130 */
131#define PB_CAPABILITY_SMBALERT (1<<4)
132#define PB_CAPABILITY_ERROR_CHECK (1<<7)
133
134/*
135 * VOUT_MODE
136 */
137#define PB_VOUT_MODE_MODE_MASK 0xe0
138#define PB_VOUT_MODE_PARAM_MASK 0x1f
139
140#define PB_VOUT_MODE_LINEAR 0x00
141#define PB_VOUT_MODE_VID 0x20
142#define PB_VOUT_MODE_DIRECT 0x40
143
144/*
145 * Fan configuration
146 */
147#define PB_FAN_2_PULSE_MASK ((1 << 0) | (1 << 1))
148#define PB_FAN_2_RPM (1 << 2)
149#define PB_FAN_2_INSTALLED (1 << 3)
150#define PB_FAN_1_PULSE_MASK ((1 << 4) | (1 << 5))
151#define PB_FAN_1_RPM (1 << 6)
152#define PB_FAN_1_INSTALLED (1 << 7)
153
154/*
155 * STATUS_BYTE, STATUS_WORD (lower)
156 */
157#define PB_STATUS_NONE_ABOVE (1<<0)
158#define PB_STATUS_CML (1<<1)
159#define PB_STATUS_TEMPERATURE (1<<2)
160#define PB_STATUS_VIN_UV (1<<3)
161#define PB_STATUS_IOUT_OC (1<<4)
162#define PB_STATUS_VOUT_OV (1<<5)
163#define PB_STATUS_OFF (1<<6)
164#define PB_STATUS_BUSY (1<<7)
165
166/*
167 * STATUS_WORD (upper)
168 */
169#define PB_STATUS_UNKNOWN (1<<8)
170#define PB_STATUS_OTHER (1<<9)
171#define PB_STATUS_FANS (1<<10)
172#define PB_STATUS_POWER_GOOD_N (1<<11)
173#define PB_STATUS_WORD_MFR (1<<12)
174#define PB_STATUS_INPUT (1<<13)
175#define PB_STATUS_IOUT_POUT (1<<14)
176#define PB_STATUS_VOUT (1<<15)
177
178/*
179 * STATUS_IOUT
180 */
181#define PB_POUT_OP_WARNING (1<<0)
182#define PB_POUT_OP_FAULT (1<<1)
183#define PB_POWER_LIMITING (1<<2)
184#define PB_CURRENT_SHARE_FAULT (1<<3)
185#define PB_IOUT_UC_FAULT (1<<4)
186#define PB_IOUT_OC_WARNING (1<<5)
187#define PB_IOUT_OC_LV_FAULT (1<<6)
188#define PB_IOUT_OC_FAULT (1<<7)
189
190/*
191 * STATUS_VOUT, STATUS_INPUT
192 */
193#define PB_VOLTAGE_UV_FAULT (1<<4)
194#define PB_VOLTAGE_UV_WARNING (1<<5)
195#define PB_VOLTAGE_OV_WARNING (1<<6)
196#define PB_VOLTAGE_OV_FAULT (1<<7)
197
198/*
199 * STATUS_INPUT
200 */
201#define PB_PIN_OP_WARNING (1<<0)
202#define PB_IIN_OC_WARNING (1<<1)
203#define PB_IIN_OC_FAULT (1<<2)
204
205/*
206 * STATUS_TEMPERATURE
207 */
208#define PB_TEMP_UT_FAULT (1<<4)
209#define PB_TEMP_UT_WARNING (1<<5)
210#define PB_TEMP_OT_WARNING (1<<6)
211#define PB_TEMP_OT_FAULT (1<<7)
212
213/*
214 * STATUS_FAN
215 */
216#define PB_FAN_AIRFLOW_WARNING (1<<0)
217#define PB_FAN_AIRFLOW_FAULT (1<<1)
218#define PB_FAN_FAN2_SPEED_OVERRIDE (1<<2)
219#define PB_FAN_FAN1_SPEED_OVERRIDE (1<<3)
220#define PB_FAN_FAN2_WARNING (1<<4)
221#define PB_FAN_FAN1_WARNING (1<<5)
222#define PB_FAN_FAN2_FAULT (1<<6)
223#define PB_FAN_FAN1_FAULT (1<<7)
224
225/*
226 * CML_FAULT_STATUS
227 */
228#define PB_CML_FAULT_OTHER_MEM_LOGIC (1<<0)
229#define PB_CML_FAULT_OTHER_COMM (1<<1)
230#define PB_CML_FAULT_PROCESSOR (1<<3)
231#define PB_CML_FAULT_MEMORY (1<<4)
232#define PB_CML_FAULT_PACKET_ERROR (1<<5)
233#define PB_CML_FAULT_INVALID_DATA (1<<6)
234#define PB_CML_FAULT_INVALID_COMMAND (1<<7)
235
236enum pmbus_sensor_classes {
237 PSC_VOLTAGE_IN = 0,
238 PSC_VOLTAGE_OUT,
239 PSC_CURRENT_IN,
240 PSC_CURRENT_OUT,
241 PSC_POWER,
242 PSC_TEMPERATURE,
243 PSC_FAN,
244 PSC_NUM_CLASSES /* Number of power sensor classes */
245};
246
247#define PMBUS_PAGES 32 /* Per PMBus specification */
248
249/* Functionality bit mask */
250#define PMBUS_HAVE_VIN (1 << 0)
251#define PMBUS_HAVE_VCAP (1 << 1)
252#define PMBUS_HAVE_VOUT (1 << 2)
253#define PMBUS_HAVE_IIN (1 << 3)
254#define PMBUS_HAVE_IOUT (1 << 4)
255#define PMBUS_HAVE_PIN (1 << 5)
256#define PMBUS_HAVE_POUT (1 << 6)
257#define PMBUS_HAVE_FAN12 (1 << 7)
258#define PMBUS_HAVE_FAN34 (1 << 8)
259#define PMBUS_HAVE_TEMP (1 << 9)
260#define PMBUS_HAVE_TEMP2 (1 << 10)
261#define PMBUS_HAVE_TEMP3 (1 << 11)
262#define PMBUS_HAVE_STATUS_VOUT (1 << 12)
263#define PMBUS_HAVE_STATUS_IOUT (1 << 13)
264#define PMBUS_HAVE_STATUS_INPUT (1 << 14)
265#define PMBUS_HAVE_STATUS_TEMP (1 << 15)
266#define PMBUS_HAVE_STATUS_FAN12 (1 << 16)
267#define PMBUS_HAVE_STATUS_FAN34 (1 << 17)
268
269struct pmbus_driver_info {
270 int pages; /* Total number of pages */
271 bool direct[PSC_NUM_CLASSES];
272 /* true if device uses direct data format
273 for the given sensor class */
274 /*
275 * Support one set of coefficients for each sensor type
276 * Used for chips providing data in direct mode.
277 */
278 int m[PSC_NUM_CLASSES]; /* mantissa for direct data format */
279 int b[PSC_NUM_CLASSES]; /* offset */
280 int R[PSC_NUM_CLASSES]; /* exponent */
281
282 u32 func[PMBUS_PAGES]; /* Functionality, per page */
283 /*
284 * The get_status function maps manufacturing specific status values
285 * into PMBus standard status values.
286 * This function is optional and only necessary if chip specific status
287 * register values have to be mapped into standard PMBus status register
288 * values.
289 */
290 int (*get_status)(struct i2c_client *client, int page, int reg);
291 /*
292 * The identify function determines supported PMBus functionality.
293 * This function is only necessary if a chip driver supports multiple
294 * chips, and the chip functionality is not pre-determined.
295 */
296 int (*identify)(struct i2c_client *client,
297 struct pmbus_driver_info *info);
298};
299
300/* Function declarations */
301
302int pmbus_set_page(struct i2c_client *client, u8 page);
303int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg);
304void pmbus_clear_faults(struct i2c_client *client);
305bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg);
306bool pmbus_check_word_register(struct i2c_client *client, int page, int reg);
307int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
308 struct pmbus_driver_info *info);
309int pmbus_do_remove(struct i2c_client *client);
310const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client
311 *client);
312
313#endif /* PMBUS_H */
diff --git a/drivers/hwmon/pmbus_core.c b/drivers/hwmon/pmbus_core.c
new file mode 100644
index 000000000000..6474512f49b0
--- /dev/null
+++ b/drivers/hwmon/pmbus_core.c
@@ -0,0 +1,1658 @@
1/*
2 * Hardware monitoring driver for PMBus devices
3 *
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/delay.h>
30#include <linux/i2c/pmbus.h>
31#include "pmbus.h"
32
33/*
34 * Constants needed to determine number of sensors, booleans, and labels.
35 */
36#define PMBUS_MAX_INPUT_SENSORS 11 /* 6*volt, 3*curr, 2*power */
37#define PMBUS_VOUT_SENSORS_PER_PAGE 5 /* input, min, max, lcrit,
38 crit */
39#define PMBUS_IOUT_SENSORS_PER_PAGE 4 /* input, min, max, crit */
40#define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */
41#define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
42#define PMBUS_MAX_SENSORS_PER_TEMP 5 /* input, min, max, lcrit,
43 crit */
44
45#define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
46 lcrit_alarm, crit_alarm;
47 c: alarm, crit_alarm;
48 p: crit_alarm */
49#define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
50 lcrit_alarm, crit_alarm */
51#define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
52 crit_alarm */
53#define PMBUS_POUT_BOOLEANS_PER_PAGE 2 /* alarm, crit_alarm */
54#define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
55#define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
56 lcrit_alarm, crit_alarm */
57
58#define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
59
60/*
61 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
62 * are paged. status_input is unpaged.
63 */
64#define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
65
66/*
67 * Index into status register array, per status register group
68 */
69#define PB_STATUS_BASE 0
70#define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
71#define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
72#define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
73#define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
74#define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
75#define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
76
77struct pmbus_sensor {
78 char name[I2C_NAME_SIZE]; /* sysfs sensor name */
79 struct sensor_device_attribute attribute;
80 u8 page; /* page number */
81 u8 reg; /* register */
82 enum pmbus_sensor_classes class; /* sensor class */
83 bool update; /* runtime sensor update needed */
84 int data; /* Sensor data.
85 Negative if there was a read error */
86};
87
88struct pmbus_boolean {
89 char name[I2C_NAME_SIZE]; /* sysfs boolean name */
90 struct sensor_device_attribute attribute;
91};
92
93struct pmbus_label {
94 char name[I2C_NAME_SIZE]; /* sysfs label name */
95 struct sensor_device_attribute attribute;
96 char label[I2C_NAME_SIZE]; /* label */
97};
98
99struct pmbus_data {
100 struct device *hwmon_dev;
101
102 u32 flags; /* from platform data */
103
104 int exponent; /* linear mode: exponent for output voltages */
105
106 const struct pmbus_driver_info *info;
107
108 int max_attributes;
109 int num_attributes;
110 struct attribute **attributes;
111 struct attribute_group group;
112
113 /*
114 * Sensors cover both sensor and limit registers.
115 */
116 int max_sensors;
117 int num_sensors;
118 struct pmbus_sensor *sensors;
119 /*
120 * Booleans are used for alarms.
121 * Values are determined from status registers.
122 */
123 int max_booleans;
124 int num_booleans;
125 struct pmbus_boolean *booleans;
126 /*
127 * Labels are used to map generic names (e.g., "in1")
128 * to PMBus specific names (e.g., "vin" or "vout1").
129 */
130 int max_labels;
131 int num_labels;
132 struct pmbus_label *labels;
133
134 struct mutex update_lock;
135 bool valid;
136 unsigned long last_updated; /* in jiffies */
137
138 /*
139 * A single status register covers multiple attributes,
140 * so we keep them all together.
141 */
142 u8 status_bits;
143 u8 status[PB_NUM_STATUS_REG];
144
145 u8 currpage;
146};
147
148int pmbus_set_page(struct i2c_client *client, u8 page)
149{
150 struct pmbus_data *data = i2c_get_clientdata(client);
151 int rv = 0;
152 int newpage;
153
154 if (page != data->currpage) {
155 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
156 newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
157 if (newpage != page)
158 rv = -EINVAL;
159 else
160 data->currpage = page;
161 }
162 return rv;
163}
164EXPORT_SYMBOL_GPL(pmbus_set_page);
165
166static int pmbus_write_byte(struct i2c_client *client, u8 page, u8 value)
167{
168 int rv;
169
170 rv = pmbus_set_page(client, page);
171 if (rv < 0)
172 return rv;
173
174 return i2c_smbus_write_byte(client, value);
175}
176
177static int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg,
178 u16 word)
179{
180 int rv;
181
182 rv = pmbus_set_page(client, page);
183 if (rv < 0)
184 return rv;
185
186 return i2c_smbus_write_word_data(client, reg, word);
187}
188
189int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
190{
191 int rv;
192
193 rv = pmbus_set_page(client, page);
194 if (rv < 0)
195 return rv;
196
197 return i2c_smbus_read_word_data(client, reg);
198}
199EXPORT_SYMBOL_GPL(pmbus_read_word_data);
200
201static int pmbus_read_byte_data(struct i2c_client *client, u8 page, u8 reg)
202{
203 int rv;
204
205 rv = pmbus_set_page(client, page);
206 if (rv < 0)
207 return rv;
208
209 return i2c_smbus_read_byte_data(client, reg);
210}
211
212static void pmbus_clear_fault_page(struct i2c_client *client, int page)
213{
214 pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
215}
216
217void pmbus_clear_faults(struct i2c_client *client)
218{
219 struct pmbus_data *data = i2c_get_clientdata(client);
220 int i;
221
222 for (i = 0; i < data->info->pages; i++)
223 pmbus_clear_fault_page(client, i);
224}
225EXPORT_SYMBOL_GPL(pmbus_clear_faults);
226
227static int pmbus_check_status_cml(struct i2c_client *client, int page)
228{
229 int status, status2;
230
231 status = pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
232 if (status < 0 || (status & PB_STATUS_CML)) {
233 status2 = pmbus_read_byte_data(client, page, PMBUS_STATUS_CML);
234 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
235 return -EINVAL;
236 }
237 return 0;
238}
239
240bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
241{
242 int rv;
243 struct pmbus_data *data = i2c_get_clientdata(client);
244
245 rv = pmbus_read_byte_data(client, page, reg);
246 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
247 rv = pmbus_check_status_cml(client, page);
248 pmbus_clear_fault_page(client, page);
249 return rv >= 0;
250}
251EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
252
253bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
254{
255 int rv;
256 struct pmbus_data *data = i2c_get_clientdata(client);
257
258 rv = pmbus_read_word_data(client, page, reg);
259 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
260 rv = pmbus_check_status_cml(client, page);
261 pmbus_clear_fault_page(client, page);
262 return rv >= 0;
263}
264EXPORT_SYMBOL_GPL(pmbus_check_word_register);
265
266const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
267{
268 struct pmbus_data *data = i2c_get_clientdata(client);
269
270 return data->info;
271}
272EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
273
274static int pmbus_get_status(struct i2c_client *client, int page, int reg)
275{
276 struct pmbus_data *data = i2c_get_clientdata(client);
277 const struct pmbus_driver_info *info = data->info;
278 int status;
279
280 if (info->get_status) {
281 status = info->get_status(client, page, reg);
282 if (status != -ENODATA)
283 return status;
284 }
285 return pmbus_read_byte_data(client, page, reg);
286}
287
288static struct pmbus_data *pmbus_update_device(struct device *dev)
289{
290 struct i2c_client *client = to_i2c_client(dev);
291 struct pmbus_data *data = i2c_get_clientdata(client);
292 const struct pmbus_driver_info *info = data->info;
293
294 mutex_lock(&data->update_lock);
295 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
296 int i;
297
298 for (i = 0; i < info->pages; i++)
299 data->status[PB_STATUS_BASE + i]
300 = pmbus_read_byte_data(client, i,
301 PMBUS_STATUS_BYTE);
302 for (i = 0; i < info->pages; i++) {
303 if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT))
304 continue;
305 data->status[PB_STATUS_VOUT_BASE + i]
306 = pmbus_get_status(client, i, PMBUS_STATUS_VOUT);
307 }
308 for (i = 0; i < info->pages; i++) {
309 if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT))
310 continue;
311 data->status[PB_STATUS_IOUT_BASE + i]
312 = pmbus_get_status(client, i, PMBUS_STATUS_IOUT);
313 }
314 for (i = 0; i < info->pages; i++) {
315 if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP))
316 continue;
317 data->status[PB_STATUS_TEMP_BASE + i]
318 = pmbus_get_status(client, i,
319 PMBUS_STATUS_TEMPERATURE);
320 }
321 for (i = 0; i < info->pages; i++) {
322 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12))
323 continue;
324 data->status[PB_STATUS_FAN_BASE + i]
325 = pmbus_get_status(client, i, PMBUS_STATUS_FAN_12);
326 }
327
328 for (i = 0; i < info->pages; i++) {
329 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34))
330 continue;
331 data->status[PB_STATUS_FAN34_BASE + i]
332 = pmbus_get_status(client, i, PMBUS_STATUS_FAN_34);
333 }
334
335 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
336 data->status[PB_STATUS_INPUT_BASE]
337 = pmbus_get_status(client, 0, PMBUS_STATUS_INPUT);
338
339 for (i = 0; i < data->num_sensors; i++) {
340 struct pmbus_sensor *sensor = &data->sensors[i];
341
342 if (!data->valid || sensor->update)
343 sensor->data
344 = pmbus_read_word_data(client, sensor->page,
345 sensor->reg);
346 }
347 pmbus_clear_faults(client);
348 data->last_updated = jiffies;
349 data->valid = 1;
350 }
351 mutex_unlock(&data->update_lock);
352 return data;
353}
354
355/*
356 * Convert linear sensor values to milli- or micro-units
357 * depending on sensor type.
358 */
359static int pmbus_reg2data_linear(struct pmbus_data *data,
360 struct pmbus_sensor *sensor)
361{
362 s16 exponent;
363 s32 mantissa;
364 long val;
365
366 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
367 exponent = data->exponent;
368 mantissa = (u16) sensor->data;
369 } else { /* LINEAR11 */
370 exponent = (sensor->data >> 11) & 0x001f;
371 mantissa = sensor->data & 0x07ff;
372
373 if (exponent > 0x0f)
374 exponent |= 0xffe0; /* sign extend exponent */
375 if (mantissa > 0x03ff)
376 mantissa |= 0xfffff800; /* sign extend mantissa */
377 }
378
379 val = mantissa;
380
381 /* scale result to milli-units for all sensors except fans */
382 if (sensor->class != PSC_FAN)
383 val = val * 1000L;
384
385 /* scale result to micro-units for power sensors */
386 if (sensor->class == PSC_POWER)
387 val = val * 1000L;
388
389 if (exponent >= 0)
390 val <<= exponent;
391 else
392 val >>= -exponent;
393
394 return (int)val;
395}
396
397/*
398 * Convert direct sensor values to milli- or micro-units
399 * depending on sensor type.
400 */
401static int pmbus_reg2data_direct(struct pmbus_data *data,
402 struct pmbus_sensor *sensor)
403{
404 long val = (s16) sensor->data;
405 long m, b, R;
406
407 m = data->info->m[sensor->class];
408 b = data->info->b[sensor->class];
409 R = data->info->R[sensor->class];
410
411 if (m == 0)
412 return 0;
413
414 /* X = 1/m * (Y * 10^-R - b) */
415 R = -R;
416 /* scale result to milli-units for everything but fans */
417 if (sensor->class != PSC_FAN) {
418 R += 3;
419 b *= 1000;
420 }
421
422 /* scale result to micro-units for power sensors */
423 if (sensor->class == PSC_POWER) {
424 R += 3;
425 b *= 1000;
426 }
427
428 while (R > 0) {
429 val *= 10;
430 R--;
431 }
432 while (R < 0) {
433 val = DIV_ROUND_CLOSEST(val, 10);
434 R++;
435 }
436
437 return (int)((val - b) / m);
438}
439
440static int pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
441{
442 int val;
443
444 if (data->info->direct[sensor->class])
445 val = pmbus_reg2data_direct(data, sensor);
446 else
447 val = pmbus_reg2data_linear(data, sensor);
448
449 return val;
450}
451
452#define MAX_MANTISSA (1023 * 1000)
453#define MIN_MANTISSA (511 * 1000)
454
455static u16 pmbus_data2reg_linear(struct pmbus_data *data,
456 enum pmbus_sensor_classes class, long val)
457{
458 s16 exponent = 0, mantissa;
459 bool negative = false;
460
461 /* simple case */
462 if (val == 0)
463 return 0;
464
465 if (class == PSC_VOLTAGE_OUT) {
466 /* LINEAR16 does not support negative voltages */
467 if (val < 0)
468 return 0;
469
470 /*
471 * For a static exponents, we don't have a choice
472 * but to adjust the value to it.
473 */
474 if (data->exponent < 0)
475 val <<= -data->exponent;
476 else
477 val >>= data->exponent;
478 val = DIV_ROUND_CLOSEST(val, 1000);
479 return val & 0xffff;
480 }
481
482 if (val < 0) {
483 negative = true;
484 val = -val;
485 }
486
487 /* Power is in uW. Convert to mW before converting. */
488 if (class == PSC_POWER)
489 val = DIV_ROUND_CLOSEST(val, 1000L);
490
491 /*
492 * For simplicity, convert fan data to milli-units
493 * before calculating the exponent.
494 */
495 if (class == PSC_FAN)
496 val = val * 1000;
497
498 /* Reduce large mantissa until it fits into 10 bit */
499 while (val >= MAX_MANTISSA && exponent < 15) {
500 exponent++;
501 val >>= 1;
502 }
503 /* Increase small mantissa to improve precision */
504 while (val < MIN_MANTISSA && exponent > -15) {
505 exponent--;
506 val <<= 1;
507 }
508
509 /* Convert mantissa from milli-units to units */
510 mantissa = DIV_ROUND_CLOSEST(val, 1000);
511
512 /* Ensure that resulting number is within range */
513 if (mantissa > 0x3ff)
514 mantissa = 0x3ff;
515
516 /* restore sign */
517 if (negative)
518 mantissa = -mantissa;
519
520 /* Convert to 5 bit exponent, 11 bit mantissa */
521 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
522}
523
524static u16 pmbus_data2reg_direct(struct pmbus_data *data,
525 enum pmbus_sensor_classes class, long val)
526{
527 long m, b, R;
528
529 m = data->info->m[class];
530 b = data->info->b[class];
531 R = data->info->R[class];
532
533 /* Power is in uW. Adjust R and b. */
534 if (class == PSC_POWER) {
535 R -= 3;
536 b *= 1000;
537 }
538
539 /* Calculate Y = (m * X + b) * 10^R */
540 if (class != PSC_FAN) {
541 R -= 3; /* Adjust R and b for data in milli-units */
542 b *= 1000;
543 }
544 val = val * m + b;
545
546 while (R > 0) {
547 val *= 10;
548 R--;
549 }
550 while (R < 0) {
551 val = DIV_ROUND_CLOSEST(val, 10);
552 R++;
553 }
554
555 return val;
556}
557
558static u16 pmbus_data2reg(struct pmbus_data *data,
559 enum pmbus_sensor_classes class, long val)
560{
561 u16 regval;
562
563 if (data->info->direct[class])
564 regval = pmbus_data2reg_direct(data, class, val);
565 else
566 regval = pmbus_data2reg_linear(data, class, val);
567
568 return regval;
569}
570
571/*
572 * Return boolean calculated from converted data.
573 * <index> defines a status register index and mask, and optionally
574 * two sensor indexes.
575 * The upper half-word references the two sensors,
576 * two sensor indices.
577 * The upper half-word references the two optional sensors,
578 * the lower half word references status register and mask.
579 * The function returns true if (status[reg] & mask) is true and,
580 * if specified, if v1 >= v2.
581 * To determine if an object exceeds upper limits, specify <v, limit>.
582 * To determine if an object exceeds lower limits, specify <limit, v>.
583 *
584 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
585 * index are set. s1 and s2 (the sensor index values) are zero in this case.
586 * The function returns true if (status[reg] & mask) is true.
587 *
588 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
589 * a specified limit has to be performed to determine the boolean result.
590 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
591 * sensor values referenced by sensor indices s1 and s2).
592 *
593 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
594 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
595 *
596 * If a negative value is stored in any of the referenced registers, this value
597 * reflects an error code which will be returned.
598 */
599static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val)
600{
601 u8 s1 = (index >> 24) & 0xff;
602 u8 s2 = (index >> 16) & 0xff;
603 u8 reg = (index >> 8) & 0xff;
604 u8 mask = index & 0xff;
605 int status;
606 u8 regval;
607
608 status = data->status[reg];
609 if (status < 0)
610 return status;
611
612 regval = status & mask;
613 if (!s1 && !s2)
614 *val = !!regval;
615 else {
616 int v1, v2;
617 struct pmbus_sensor *sensor1, *sensor2;
618
619 sensor1 = &data->sensors[s1];
620 if (sensor1->data < 0)
621 return sensor1->data;
622 sensor2 = &data->sensors[s2];
623 if (sensor2->data < 0)
624 return sensor2->data;
625
626 v1 = pmbus_reg2data(data, sensor1);
627 v2 = pmbus_reg2data(data, sensor2);
628 *val = !!(regval && v1 >= v2);
629 }
630 return 0;
631}
632
633static ssize_t pmbus_show_boolean(struct device *dev,
634 struct device_attribute *da, char *buf)
635{
636 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
637 struct pmbus_data *data = pmbus_update_device(dev);
638 int val;
639 int err;
640
641 err = pmbus_get_boolean(data, attr->index, &val);
642 if (err)
643 return err;
644 return snprintf(buf, PAGE_SIZE, "%d\n", val);
645}
646
647static ssize_t pmbus_show_sensor(struct device *dev,
648 struct device_attribute *da, char *buf)
649{
650 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
651 struct pmbus_data *data = pmbus_update_device(dev);
652 struct pmbus_sensor *sensor;
653
654 sensor = &data->sensors[attr->index];
655 if (sensor->data < 0)
656 return sensor->data;
657
658 return snprintf(buf, PAGE_SIZE, "%d\n", pmbus_reg2data(data, sensor));
659}
660
661static ssize_t pmbus_set_sensor(struct device *dev,
662 struct device_attribute *devattr,
663 const char *buf, size_t count)
664{
665 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
666 struct i2c_client *client = to_i2c_client(dev);
667 struct pmbus_data *data = i2c_get_clientdata(client);
668 struct pmbus_sensor *sensor = &data->sensors[attr->index];
669 ssize_t rv = count;
670 long val = 0;
671 int ret;
672 u16 regval;
673
674 if (strict_strtol(buf, 10, &val) < 0)
675 return -EINVAL;
676
677 mutex_lock(&data->update_lock);
678 regval = pmbus_data2reg(data, sensor->class, val);
679 ret = pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
680 if (ret < 0)
681 rv = ret;
682 else
683 data->sensors[attr->index].data = regval;
684 mutex_unlock(&data->update_lock);
685 return rv;
686}
687
688static ssize_t pmbus_show_label(struct device *dev,
689 struct device_attribute *da, char *buf)
690{
691 struct i2c_client *client = to_i2c_client(dev);
692 struct pmbus_data *data = i2c_get_clientdata(client);
693 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
694
695 return snprintf(buf, PAGE_SIZE, "%s\n",
696 data->labels[attr->index].label);
697}
698
699#define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
700do { \
701 struct sensor_device_attribute *a \
702 = &data->_type##s[data->num_##_type##s].attribute; \
703 BUG_ON(data->num_attributes >= data->max_attributes); \
704 a->dev_attr.attr.name = _name; \
705 a->dev_attr.attr.mode = _mode; \
706 a->dev_attr.show = _show; \
707 a->dev_attr.store = _set; \
708 a->index = _idx; \
709 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
710 data->num_attributes++; \
711} while (0)
712
713#define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
714 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
715 pmbus_show_##_type, NULL)
716
717#define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
718 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
719 pmbus_show_##_type, pmbus_set_##_type)
720
721static void pmbus_add_boolean(struct pmbus_data *data,
722 const char *name, const char *type, int seq,
723 int idx)
724{
725 struct pmbus_boolean *boolean;
726
727 BUG_ON(data->num_booleans >= data->max_booleans);
728
729 boolean = &data->booleans[data->num_booleans];
730
731 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
732 name, seq, type);
733 PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx);
734 data->num_booleans++;
735}
736
737static void pmbus_add_boolean_reg(struct pmbus_data *data,
738 const char *name, const char *type,
739 int seq, int reg, int bit)
740{
741 pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit);
742}
743
744static void pmbus_add_boolean_cmp(struct pmbus_data *data,
745 const char *name, const char *type,
746 int seq, int i1, int i2, int reg, int mask)
747{
748 pmbus_add_boolean(data, name, type, seq,
749 (i1 << 24) | (i2 << 16) | (reg << 8) | mask);
750}
751
752static void pmbus_add_sensor(struct pmbus_data *data,
753 const char *name, const char *type, int seq,
754 int page, int reg, enum pmbus_sensor_classes class,
755 bool update)
756{
757 struct pmbus_sensor *sensor;
758
759 BUG_ON(data->num_sensors >= data->max_sensors);
760
761 sensor = &data->sensors[data->num_sensors];
762 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
763 name, seq, type);
764 sensor->page = page;
765 sensor->reg = reg;
766 sensor->class = class;
767 sensor->update = update;
768 if (update)
769 PMBUS_ADD_GET_ATTR(data, sensor->name, sensor,
770 data->num_sensors);
771 else
772 PMBUS_ADD_SET_ATTR(data, sensor->name, sensor,
773 data->num_sensors);
774 data->num_sensors++;
775}
776
777static void pmbus_add_label(struct pmbus_data *data,
778 const char *name, int seq,
779 const char *lstring, int index)
780{
781 struct pmbus_label *label;
782
783 BUG_ON(data->num_labels >= data->max_labels);
784
785 label = &data->labels[data->num_labels];
786 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
787 if (!index)
788 strncpy(label->label, lstring, sizeof(label->label) - 1);
789 else
790 snprintf(label->label, sizeof(label->label), "%s%d", lstring,
791 index);
792
793 PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels);
794 data->num_labels++;
795}
796
797static const int pmbus_temp_registers[] = {
798 PMBUS_READ_TEMPERATURE_1,
799 PMBUS_READ_TEMPERATURE_2,
800 PMBUS_READ_TEMPERATURE_3
801};
802
803static const int pmbus_temp_flags[] = {
804 PMBUS_HAVE_TEMP,
805 PMBUS_HAVE_TEMP2,
806 PMBUS_HAVE_TEMP3
807};
808
809static const int pmbus_fan_registers[] = {
810 PMBUS_READ_FAN_SPEED_1,
811 PMBUS_READ_FAN_SPEED_2,
812 PMBUS_READ_FAN_SPEED_3,
813 PMBUS_READ_FAN_SPEED_4
814};
815
816static const int pmbus_fan_config_registers[] = {
817 PMBUS_FAN_CONFIG_12,
818 PMBUS_FAN_CONFIG_12,
819 PMBUS_FAN_CONFIG_34,
820 PMBUS_FAN_CONFIG_34
821};
822
823static const int pmbus_fan_status_registers[] = {
824 PMBUS_STATUS_FAN_12,
825 PMBUS_STATUS_FAN_12,
826 PMBUS_STATUS_FAN_34,
827 PMBUS_STATUS_FAN_34
828};
829
830static const u32 pmbus_fan_flags[] = {
831 PMBUS_HAVE_FAN12,
832 PMBUS_HAVE_FAN12,
833 PMBUS_HAVE_FAN34,
834 PMBUS_HAVE_FAN34
835};
836
837static const u32 pmbus_fan_status_flags[] = {
838 PMBUS_HAVE_STATUS_FAN12,
839 PMBUS_HAVE_STATUS_FAN12,
840 PMBUS_HAVE_STATUS_FAN34,
841 PMBUS_HAVE_STATUS_FAN34
842};
843
844/*
845 * Determine maximum number of sensors, booleans, and labels.
846 * To keep things simple, only make a rough high estimate.
847 */
848static void pmbus_find_max_attr(struct i2c_client *client,
849 struct pmbus_data *data)
850{
851 const struct pmbus_driver_info *info = data->info;
852 int page, max_sensors, max_booleans, max_labels;
853
854 max_sensors = PMBUS_MAX_INPUT_SENSORS;
855 max_booleans = PMBUS_MAX_INPUT_BOOLEANS;
856 max_labels = PMBUS_MAX_INPUT_LABELS;
857
858 for (page = 0; page < info->pages; page++) {
859 if (info->func[page] & PMBUS_HAVE_VOUT) {
860 max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE;
861 max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE;
862 max_labels++;
863 }
864 if (info->func[page] & PMBUS_HAVE_IOUT) {
865 max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE;
866 max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE;
867 max_labels++;
868 }
869 if (info->func[page] & PMBUS_HAVE_POUT) {
870 max_sensors += PMBUS_POUT_SENSORS_PER_PAGE;
871 max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE;
872 max_labels++;
873 }
874 if (info->func[page] & PMBUS_HAVE_FAN12) {
875 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
876 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
877 }
878 if (info->func[page] & PMBUS_HAVE_FAN34) {
879 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
880 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
881 }
882 if (info->func[page] & PMBUS_HAVE_TEMP) {
883 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
884 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
885 }
886 if (info->func[page] & PMBUS_HAVE_TEMP2) {
887 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
888 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
889 }
890 if (info->func[page] & PMBUS_HAVE_TEMP3) {
891 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
892 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
893 }
894 }
895 data->max_sensors = max_sensors;
896 data->max_booleans = max_booleans;
897 data->max_labels = max_labels;
898 data->max_attributes = max_sensors + max_booleans + max_labels;
899}
900
901/*
902 * Search for attributes. Allocate sensors, booleans, and labels as needed.
903 */
904static void pmbus_find_attributes(struct i2c_client *client,
905 struct pmbus_data *data)
906{
907 const struct pmbus_driver_info *info = data->info;
908 int page, i0, i1, in_index;
909
910 /*
911 * Input voltage sensors
912 */
913 in_index = 1;
914 if (info->func[0] & PMBUS_HAVE_VIN) {
915 bool have_alarm = false;
916
917 i0 = data->num_sensors;
918 pmbus_add_label(data, "in", in_index, "vin", 0);
919 pmbus_add_sensor(data, "in", "input", in_index,
920 0, PMBUS_READ_VIN, PSC_VOLTAGE_IN, true);
921 if (pmbus_check_word_register(client, 0,
922 PMBUS_VIN_UV_WARN_LIMIT)) {
923 i1 = data->num_sensors;
924 pmbus_add_sensor(data, "in", "min", in_index,
925 0, PMBUS_VIN_UV_WARN_LIMIT,
926 PSC_VOLTAGE_IN, false);
927 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) {
928 pmbus_add_boolean_reg(data, "in", "min_alarm",
929 in_index,
930 PB_STATUS_INPUT_BASE,
931 PB_VOLTAGE_UV_WARNING);
932 have_alarm = true;
933 }
934 }
935 if (pmbus_check_word_register(client, 0,
936 PMBUS_VIN_UV_FAULT_LIMIT)) {
937 i1 = data->num_sensors;
938 pmbus_add_sensor(data, "in", "lcrit", in_index,
939 0, PMBUS_VIN_UV_FAULT_LIMIT,
940 PSC_VOLTAGE_IN, false);
941 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) {
942 pmbus_add_boolean_reg(data, "in", "lcrit_alarm",
943 in_index,
944 PB_STATUS_INPUT_BASE,
945 PB_VOLTAGE_UV_FAULT);
946 have_alarm = true;
947 }
948 }
949 if (pmbus_check_word_register(client, 0,
950 PMBUS_VIN_OV_WARN_LIMIT)) {
951 i1 = data->num_sensors;
952 pmbus_add_sensor(data, "in", "max", in_index,
953 0, PMBUS_VIN_OV_WARN_LIMIT,
954 PSC_VOLTAGE_IN, false);
955 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) {
956 pmbus_add_boolean_reg(data, "in", "max_alarm",
957 in_index,
958 PB_STATUS_INPUT_BASE,
959 PB_VOLTAGE_OV_WARNING);
960 have_alarm = true;
961 }
962 }
963 if (pmbus_check_word_register(client, 0,
964 PMBUS_VIN_OV_FAULT_LIMIT)) {
965 i1 = data->num_sensors;
966 pmbus_add_sensor(data, "in", "crit", in_index,
967 0, PMBUS_VIN_OV_FAULT_LIMIT,
968 PSC_VOLTAGE_IN, false);
969 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) {
970 pmbus_add_boolean_reg(data, "in", "crit_alarm",
971 in_index,
972 PB_STATUS_INPUT_BASE,
973 PB_VOLTAGE_OV_FAULT);
974 have_alarm = true;
975 }
976 }
977 /*
978 * Add generic alarm attribute only if there are no individual
979 * attributes.
980 */
981 if (!have_alarm)
982 pmbus_add_boolean_reg(data, "in", "alarm",
983 in_index,
984 PB_STATUS_BASE,
985 PB_STATUS_VIN_UV);
986 in_index++;
987 }
988 if (info->func[0] & PMBUS_HAVE_VCAP) {
989 pmbus_add_label(data, "in", in_index, "vcap", 0);
990 pmbus_add_sensor(data, "in", "input", in_index, 0,
991 PMBUS_READ_VCAP, PSC_VOLTAGE_IN, true);
992 in_index++;
993 }
994
995 /*
996 * Output voltage sensors
997 */
998 for (page = 0; page < info->pages; page++) {
999 bool have_alarm = false;
1000
1001 if (!(info->func[page] & PMBUS_HAVE_VOUT))
1002 continue;
1003
1004 i0 = data->num_sensors;
1005 pmbus_add_label(data, "in", in_index, "vout", page + 1);
1006 pmbus_add_sensor(data, "in", "input", in_index, page,
1007 PMBUS_READ_VOUT, PSC_VOLTAGE_OUT, true);
1008 if (pmbus_check_word_register(client, page,
1009 PMBUS_VOUT_UV_WARN_LIMIT)) {
1010 i1 = data->num_sensors;
1011 pmbus_add_sensor(data, "in", "min", in_index, page,
1012 PMBUS_VOUT_UV_WARN_LIMIT,
1013 PSC_VOLTAGE_OUT, false);
1014 if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) {
1015 pmbus_add_boolean_reg(data, "in", "min_alarm",
1016 in_index,
1017 PB_STATUS_VOUT_BASE +
1018 page,
1019 PB_VOLTAGE_UV_WARNING);
1020 have_alarm = true;
1021 }
1022 }
1023 if (pmbus_check_word_register(client, page,
1024 PMBUS_VOUT_UV_FAULT_LIMIT)) {
1025 i1 = data->num_sensors;
1026 pmbus_add_sensor(data, "in", "lcrit", in_index, page,
1027 PMBUS_VOUT_UV_FAULT_LIMIT,
1028 PSC_VOLTAGE_OUT, false);
1029 if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) {
1030 pmbus_add_boolean_reg(data, "in", "lcrit_alarm",
1031 in_index,
1032 PB_STATUS_VOUT_BASE +
1033 page,
1034 PB_VOLTAGE_UV_FAULT);
1035 have_alarm = true;
1036 }
1037 }
1038 if (pmbus_check_word_register(client, page,
1039 PMBUS_VOUT_OV_WARN_LIMIT)) {
1040 i1 = data->num_sensors;
1041 pmbus_add_sensor(data, "in", "max", in_index, page,
1042 PMBUS_VOUT_OV_WARN_LIMIT,
1043 PSC_VOLTAGE_OUT, false);
1044 if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) {
1045 pmbus_add_boolean_reg(data, "in", "max_alarm",
1046 in_index,
1047 PB_STATUS_VOUT_BASE +
1048 page,
1049 PB_VOLTAGE_OV_WARNING);
1050 have_alarm = true;
1051 }
1052 }
1053 if (pmbus_check_word_register(client, page,
1054 PMBUS_VOUT_OV_FAULT_LIMIT)) {
1055 i1 = data->num_sensors;
1056 pmbus_add_sensor(data, "in", "crit", in_index, page,
1057 PMBUS_VOUT_OV_FAULT_LIMIT,
1058 PSC_VOLTAGE_OUT, false);
1059 if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) {
1060 pmbus_add_boolean_reg(data, "in", "crit_alarm",
1061 in_index,
1062 PB_STATUS_VOUT_BASE +
1063 page,
1064 PB_VOLTAGE_OV_FAULT);
1065 have_alarm = true;
1066 }
1067 }
1068 /*
1069 * Add generic alarm attribute only if there are no individual
1070 * attributes.
1071 */
1072 if (!have_alarm)
1073 pmbus_add_boolean_reg(data, "in", "alarm",
1074 in_index,
1075 PB_STATUS_BASE + page,
1076 PB_STATUS_VOUT_OV);
1077 in_index++;
1078 }
1079
1080 /*
1081 * Current sensors
1082 */
1083
1084 /*
1085 * Input current sensors
1086 */
1087 in_index = 1;
1088 if (info->func[0] & PMBUS_HAVE_IIN) {
1089 i0 = data->num_sensors;
1090 pmbus_add_label(data, "curr", in_index, "iin", 0);
1091 pmbus_add_sensor(data, "curr", "input", in_index,
1092 0, PMBUS_READ_IIN, PSC_CURRENT_IN, true);
1093 if (pmbus_check_word_register(client, 0,
1094 PMBUS_IIN_OC_WARN_LIMIT)) {
1095 i1 = data->num_sensors;
1096 pmbus_add_sensor(data, "curr", "max", in_index,
1097 0, PMBUS_IIN_OC_WARN_LIMIT,
1098 PSC_CURRENT_IN, false);
1099 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) {
1100 pmbus_add_boolean_reg(data, "curr", "max_alarm",
1101 in_index,
1102 PB_STATUS_INPUT_BASE,
1103 PB_IIN_OC_WARNING);
1104 }
1105 }
1106 if (pmbus_check_word_register(client, 0,
1107 PMBUS_IIN_OC_FAULT_LIMIT)) {
1108 i1 = data->num_sensors;
1109 pmbus_add_sensor(data, "curr", "crit", in_index,
1110 0, PMBUS_IIN_OC_FAULT_LIMIT,
1111 PSC_CURRENT_IN, false);
1112 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
1113 pmbus_add_boolean_reg(data, "curr",
1114 "crit_alarm",
1115 in_index,
1116 PB_STATUS_INPUT_BASE,
1117 PB_IIN_OC_FAULT);
1118 }
1119 in_index++;
1120 }
1121
1122 /*
1123 * Output current sensors
1124 */
1125 for (page = 0; page < info->pages; page++) {
1126 bool have_alarm = false;
1127
1128 if (!(info->func[page] & PMBUS_HAVE_IOUT))
1129 continue;
1130
1131 i0 = data->num_sensors;
1132 pmbus_add_label(data, "curr", in_index, "iout", page + 1);
1133 pmbus_add_sensor(data, "curr", "input", in_index, page,
1134 PMBUS_READ_IOUT, PSC_CURRENT_OUT, true);
1135 if (pmbus_check_word_register(client, page,
1136 PMBUS_IOUT_OC_WARN_LIMIT)) {
1137 i1 = data->num_sensors;
1138 pmbus_add_sensor(data, "curr", "max", in_index, page,
1139 PMBUS_IOUT_OC_WARN_LIMIT,
1140 PSC_CURRENT_OUT, false);
1141 if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) {
1142 pmbus_add_boolean_reg(data, "curr", "max_alarm",
1143 in_index,
1144 PB_STATUS_IOUT_BASE +
1145 page, PB_IOUT_OC_WARNING);
1146 have_alarm = true;
1147 }
1148 }
1149 if (pmbus_check_word_register(client, page,
1150 PMBUS_IOUT_UC_FAULT_LIMIT)) {
1151 i1 = data->num_sensors;
1152 pmbus_add_sensor(data, "curr", "lcrit", in_index, page,
1153 PMBUS_IOUT_UC_FAULT_LIMIT,
1154 PSC_CURRENT_OUT, false);
1155 if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) {
1156 pmbus_add_boolean_reg(data, "curr",
1157 "lcrit_alarm",
1158 in_index,
1159 PB_STATUS_IOUT_BASE +
1160 page, PB_IOUT_UC_FAULT);
1161 have_alarm = true;
1162 }
1163 }
1164 if (pmbus_check_word_register(client, page,
1165 PMBUS_IOUT_OC_FAULT_LIMIT)) {
1166 i1 = data->num_sensors;
1167 pmbus_add_sensor(data, "curr", "crit", in_index, page,
1168 PMBUS_IOUT_OC_FAULT_LIMIT,
1169 PSC_CURRENT_OUT, false);
1170 if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) {
1171 pmbus_add_boolean_reg(data, "curr",
1172 "crit_alarm",
1173 in_index,
1174 PB_STATUS_IOUT_BASE +
1175 page, PB_IOUT_OC_FAULT);
1176 have_alarm = true;
1177 }
1178 }
1179 /*
1180 * Add generic alarm attribute only if there are no individual
1181 * attributes.
1182 */
1183 if (!have_alarm)
1184 pmbus_add_boolean_reg(data, "curr", "alarm",
1185 in_index,
1186 PB_STATUS_BASE + page,
1187 PB_STATUS_IOUT_OC);
1188 in_index++;
1189 }
1190
1191 /*
1192 * Power sensors
1193 */
1194 /*
1195 * Input Power sensors
1196 */
1197 in_index = 1;
1198 if (info->func[0] & PMBUS_HAVE_PIN) {
1199 i0 = data->num_sensors;
1200 pmbus_add_label(data, "power", in_index, "pin", 0);
1201 pmbus_add_sensor(data, "power", "input", in_index,
1202 0, PMBUS_READ_PIN, PSC_POWER, true);
1203 if (pmbus_check_word_register(client, 0,
1204 PMBUS_PIN_OP_WARN_LIMIT)) {
1205 i1 = data->num_sensors;
1206 pmbus_add_sensor(data, "power", "max", in_index,
1207 0, PMBUS_PIN_OP_WARN_LIMIT, PSC_POWER,
1208 false);
1209 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
1210 pmbus_add_boolean_reg(data, "power",
1211 "alarm",
1212 in_index,
1213 PB_STATUS_INPUT_BASE,
1214 PB_PIN_OP_WARNING);
1215 }
1216 in_index++;
1217 }
1218
1219 /*
1220 * Output Power sensors
1221 */
1222 for (page = 0; page < info->pages; page++) {
1223 bool need_alarm = false;
1224
1225 if (!(info->func[page] & PMBUS_HAVE_POUT))
1226 continue;
1227
1228 i0 = data->num_sensors;
1229 pmbus_add_label(data, "power", in_index, "pout", page + 1);
1230 pmbus_add_sensor(data, "power", "input", in_index, page,
1231 PMBUS_READ_POUT, PSC_POWER, true);
1232 /*
1233 * Per hwmon sysfs API, power_cap is to be used to limit output
1234 * power.
1235 * We have two registers related to maximum output power,
1236 * PMBUS_POUT_MAX and PMBUS_POUT_OP_WARN_LIMIT.
1237 * PMBUS_POUT_MAX matches the powerX_cap attribute definition.
1238 * There is no attribute in the API to match
1239 * PMBUS_POUT_OP_WARN_LIMIT. We use powerX_max for now.
1240 */
1241 if (pmbus_check_word_register(client, page, PMBUS_POUT_MAX)) {
1242 i1 = data->num_sensors;
1243 pmbus_add_sensor(data, "power", "cap", in_index, page,
1244 PMBUS_POUT_MAX, PSC_POWER, false);
1245 need_alarm = true;
1246 }
1247 if (pmbus_check_word_register(client, page,
1248 PMBUS_POUT_OP_WARN_LIMIT)) {
1249 i1 = data->num_sensors;
1250 pmbus_add_sensor(data, "power", "max", in_index, page,
1251 PMBUS_POUT_OP_WARN_LIMIT, PSC_POWER,
1252 false);
1253 need_alarm = true;
1254 }
1255 if (need_alarm && (info->func[page] & PMBUS_HAVE_STATUS_IOUT))
1256 pmbus_add_boolean_reg(data, "power", "alarm",
1257 in_index,
1258 PB_STATUS_IOUT_BASE + page,
1259 PB_POUT_OP_WARNING
1260 | PB_POWER_LIMITING);
1261
1262 if (pmbus_check_word_register(client, page,
1263 PMBUS_POUT_OP_FAULT_LIMIT)) {
1264 i1 = data->num_sensors;
1265 pmbus_add_sensor(data, "power", "crit", in_index, page,
1266 PMBUS_POUT_OP_FAULT_LIMIT, PSC_POWER,
1267 false);
1268 if (info->func[page] & PMBUS_HAVE_STATUS_IOUT)
1269 pmbus_add_boolean_reg(data, "power",
1270 "crit_alarm",
1271 in_index,
1272 PB_STATUS_IOUT_BASE
1273 + page,
1274 PB_POUT_OP_FAULT);
1275 }
1276 in_index++;
1277 }
1278
1279 /*
1280 * Temperature sensors
1281 */
1282 in_index = 1;
1283 for (page = 0; page < info->pages; page++) {
1284 int t;
1285
1286 for (t = 0; t < ARRAY_SIZE(pmbus_temp_registers); t++) {
1287 bool have_alarm = false;
1288
1289 /*
1290 * A PMBus chip may support any combination of
1291 * temperature registers on any page. So we can not
1292 * abort after a failure to detect a register, but have
1293 * to continue checking for all registers on all pages.
1294 */
1295 if (!(info->func[page] & pmbus_temp_flags[t]))
1296 continue;
1297
1298 if (!pmbus_check_word_register
1299 (client, page, pmbus_temp_registers[t]))
1300 continue;
1301
1302 i0 = data->num_sensors;
1303 pmbus_add_sensor(data, "temp", "input", in_index, page,
1304 pmbus_temp_registers[t],
1305 PSC_TEMPERATURE, true);
1306
1307 /*
1308 * PMBus provides only one status register for TEMP1-3.
1309 * Thus, we can not use the status register to determine
1310 * which of the three sensors actually caused an alarm.
1311 * Always compare current temperature against the limit
1312 * registers to determine alarm conditions for a
1313 * specific sensor.
1314 *
1315 * Since there is only one set of limit registers for
1316 * up to three temperature sensors, we need to update
1317 * all limit registers after the limit was changed for
1318 * one of the sensors. This ensures that correct limits
1319 * are reported for all temperature sensors.
1320 */
1321 if (pmbus_check_word_register
1322 (client, page, PMBUS_UT_WARN_LIMIT)) {
1323 i1 = data->num_sensors;
1324 pmbus_add_sensor(data, "temp", "min", in_index,
1325 page, PMBUS_UT_WARN_LIMIT,
1326 PSC_TEMPERATURE, true);
1327 if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) {
1328 pmbus_add_boolean_cmp(data, "temp",
1329 "min_alarm", in_index, i1, i0,
1330 PB_STATUS_TEMP_BASE + page,
1331 PB_TEMP_UT_WARNING);
1332 have_alarm = true;
1333 }
1334 }
1335 if (pmbus_check_word_register(client, page,
1336 PMBUS_UT_FAULT_LIMIT)) {
1337 i1 = data->num_sensors;
1338 pmbus_add_sensor(data, "temp", "lcrit",
1339 in_index, page,
1340 PMBUS_UT_FAULT_LIMIT,
1341 PSC_TEMPERATURE, true);
1342 if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) {
1343 pmbus_add_boolean_cmp(data, "temp",
1344 "lcrit_alarm", in_index, i1, i0,
1345 PB_STATUS_TEMP_BASE + page,
1346 PB_TEMP_UT_FAULT);
1347 have_alarm = true;
1348 }
1349 }
1350 if (pmbus_check_word_register
1351 (client, page, PMBUS_OT_WARN_LIMIT)) {
1352 i1 = data->num_sensors;
1353 pmbus_add_sensor(data, "temp", "max", in_index,
1354 page, PMBUS_OT_WARN_LIMIT,
1355 PSC_TEMPERATURE, true);
1356 if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) {
1357 pmbus_add_boolean_cmp(data, "temp",
1358 "max_alarm", in_index, i0, i1,
1359 PB_STATUS_TEMP_BASE + page,
1360 PB_TEMP_OT_WARNING);
1361 have_alarm = true;
1362 }
1363 }
1364 if (pmbus_check_word_register(client, page,
1365 PMBUS_OT_FAULT_LIMIT)) {
1366 i1 = data->num_sensors;
1367 pmbus_add_sensor(data, "temp", "crit", in_index,
1368 page, PMBUS_OT_FAULT_LIMIT,
1369 PSC_TEMPERATURE, true);
1370 if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) {
1371 pmbus_add_boolean_cmp(data, "temp",
1372 "crit_alarm", in_index, i0, i1,
1373 PB_STATUS_TEMP_BASE + page,
1374 PB_TEMP_OT_FAULT);
1375 have_alarm = true;
1376 }
1377 }
1378 /*
1379 * Last resort - we were not able to create any alarm
1380 * registers. Report alarm for all sensors using the
1381 * status register temperature alarm bit.
1382 */
1383 if (!have_alarm)
1384 pmbus_add_boolean_reg(data, "temp", "alarm",
1385 in_index,
1386 PB_STATUS_BASE + page,
1387 PB_STATUS_TEMPERATURE);
1388 in_index++;
1389 }
1390 }
1391
1392 /*
1393 * Fans
1394 */
1395 in_index = 1;
1396 for (page = 0; page < info->pages; page++) {
1397 int f;
1398
1399 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1400 int regval;
1401
1402 if (!(info->func[page] & pmbus_fan_flags[f]))
1403 break;
1404
1405 if (!pmbus_check_word_register(client, page,
1406 pmbus_fan_registers[f])
1407 || !pmbus_check_byte_register(client, page,
1408 pmbus_fan_config_registers[f]))
1409 break;
1410
1411 /*
1412 * Skip fan if not installed.
1413 * Each fan configuration register covers multiple fans,
1414 * so we have to do some magic.
1415 */
1416 regval = pmbus_read_byte_data(client, page,
1417 pmbus_fan_config_registers[f]);
1418 if (regval < 0 ||
1419 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1420 continue;
1421
1422 i0 = data->num_sensors;
1423 pmbus_add_sensor(data, "fan", "input", in_index, page,
1424 pmbus_fan_registers[f], PSC_FAN, true);
1425
1426 /*
1427 * Each fan status register covers multiple fans,
1428 * so we have to do some magic.
1429 */
1430 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1431 pmbus_check_byte_register(client,
1432 page, pmbus_fan_status_registers[f])) {
1433 int base;
1434
1435 if (f > 1) /* fan 3, 4 */
1436 base = PB_STATUS_FAN34_BASE + page;
1437 else
1438 base = PB_STATUS_FAN_BASE + page;
1439 pmbus_add_boolean_reg(data, "fan", "alarm",
1440 in_index, base,
1441 PB_FAN_FAN1_WARNING >> (f & 1));
1442 pmbus_add_boolean_reg(data, "fan", "fault",
1443 in_index, base,
1444 PB_FAN_FAN1_FAULT >> (f & 1));
1445 }
1446 in_index++;
1447 }
1448 }
1449}
1450
1451/*
1452 * Identify chip parameters.
1453 * This function is called for all chips.
1454 */
1455static int pmbus_identify_common(struct i2c_client *client,
1456 struct pmbus_data *data)
1457{
1458 int vout_mode = -1, exponent;
1459
1460 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
1461 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1462 if (vout_mode >= 0 && vout_mode != 0xff) {
1463 /*
1464 * Not all chips support the VOUT_MODE command,
1465 * so a failure to read it is not an error.
1466 */
1467 switch (vout_mode >> 5) {
1468 case 0: /* linear mode */
1469 if (data->info->direct[PSC_VOLTAGE_OUT])
1470 return -ENODEV;
1471
1472 exponent = vout_mode & 0x1f;
1473 /* and sign-extend it */
1474 if (exponent & 0x10)
1475 exponent |= ~0x1f;
1476 data->exponent = exponent;
1477 break;
1478 case 2: /* direct mode */
1479 if (!data->info->direct[PSC_VOLTAGE_OUT])
1480 return -ENODEV;
1481 break;
1482 default:
1483 return -ENODEV;
1484 }
1485 }
1486
1487 /* Determine maximum number of sensors, booleans, and labels */
1488 pmbus_find_max_attr(client, data);
1489 pmbus_clear_fault_page(client, 0);
1490 return 0;
1491}
1492
1493int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
1494 struct pmbus_driver_info *info)
1495{
1496 const struct pmbus_platform_data *pdata = client->dev.platform_data;
1497 struct pmbus_data *data;
1498 int ret;
1499
1500 if (!info) {
1501 dev_err(&client->dev, "Missing chip information");
1502 return -ENODEV;
1503 }
1504
1505 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
1506 | I2C_FUNC_SMBUS_BYTE_DATA
1507 | I2C_FUNC_SMBUS_WORD_DATA))
1508 return -ENODEV;
1509
1510 data = kzalloc(sizeof(*data), GFP_KERNEL);
1511 if (!data) {
1512 dev_err(&client->dev, "No memory to allocate driver data\n");
1513 return -ENOMEM;
1514 }
1515
1516 i2c_set_clientdata(client, data);
1517 mutex_init(&data->update_lock);
1518
1519 /*
1520 * Bail out if status register or PMBus revision register
1521 * does not exist.
1522 */
1523 if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0
1524 || i2c_smbus_read_byte_data(client, PMBUS_REVISION) < 0) {
1525 dev_err(&client->dev,
1526 "Status or revision register not found\n");
1527 ret = -ENODEV;
1528 goto out_data;
1529 }
1530
1531 if (pdata)
1532 data->flags = pdata->flags;
1533 data->info = info;
1534
1535 pmbus_clear_faults(client);
1536
1537 if (info->identify) {
1538 ret = (*info->identify)(client, info);
1539 if (ret < 0) {
1540 dev_err(&client->dev, "Chip identification failed\n");
1541 goto out_data;
1542 }
1543 }
1544
1545 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1546 dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
1547 info->pages);
1548 ret = -EINVAL;
1549 goto out_data;
1550 }
1551 /*
1552 * Bail out if more than one page was configured, but we can not
1553 * select the highest page. This is an indication that the wrong
1554 * chip type was selected. Better bail out now than keep
1555 * returning errors later on.
1556 */
1557 if (info->pages > 1 && pmbus_set_page(client, info->pages - 1) < 0) {
1558 dev_err(&client->dev, "Failed to select page %d\n",
1559 info->pages - 1);
1560 ret = -EINVAL;
1561 goto out_data;
1562 }
1563
1564 ret = pmbus_identify_common(client, data);
1565 if (ret < 0) {
1566 dev_err(&client->dev, "Failed to identify chip capabilities\n");
1567 goto out_data;
1568 }
1569
1570 ret = -ENOMEM;
1571 data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors,
1572 GFP_KERNEL);
1573 if (!data->sensors) {
1574 dev_err(&client->dev, "No memory to allocate sensor data\n");
1575 goto out_data;
1576 }
1577
1578 data->booleans = kzalloc(sizeof(struct pmbus_boolean)
1579 * data->max_booleans, GFP_KERNEL);
1580 if (!data->booleans) {
1581 dev_err(&client->dev, "No memory to allocate boolean data\n");
1582 goto out_sensors;
1583 }
1584
1585 data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels,
1586 GFP_KERNEL);
1587 if (!data->labels) {
1588 dev_err(&client->dev, "No memory to allocate label data\n");
1589 goto out_booleans;
1590 }
1591
1592 data->attributes = kzalloc(sizeof(struct attribute *)
1593 * data->max_attributes, GFP_KERNEL);
1594 if (!data->attributes) {
1595 dev_err(&client->dev, "No memory to allocate attribute data\n");
1596 goto out_labels;
1597 }
1598
1599 pmbus_find_attributes(client, data);
1600
1601 /*
1602 * If there are no attributes, something is wrong.
1603 * Bail out instead of trying to register nothing.
1604 */
1605 if (!data->num_attributes) {
1606 dev_err(&client->dev, "No attributes found\n");
1607 ret = -ENODEV;
1608 goto out_attributes;
1609 }
1610
1611 /* Register sysfs hooks */
1612 data->group.attrs = data->attributes;
1613 ret = sysfs_create_group(&client->dev.kobj, &data->group);
1614 if (ret) {
1615 dev_err(&client->dev, "Failed to create sysfs entries\n");
1616 goto out_attributes;
1617 }
1618 data->hwmon_dev = hwmon_device_register(&client->dev);
1619 if (IS_ERR(data->hwmon_dev)) {
1620 ret = PTR_ERR(data->hwmon_dev);
1621 dev_err(&client->dev, "Failed to register hwmon device\n");
1622 goto out_hwmon_device_register;
1623 }
1624 return 0;
1625
1626out_hwmon_device_register:
1627 sysfs_remove_group(&client->dev.kobj, &data->group);
1628out_attributes:
1629 kfree(data->attributes);
1630out_labels:
1631 kfree(data->labels);
1632out_booleans:
1633 kfree(data->booleans);
1634out_sensors:
1635 kfree(data->sensors);
1636out_data:
1637 kfree(data);
1638 return ret;
1639}
1640EXPORT_SYMBOL_GPL(pmbus_do_probe);
1641
1642int pmbus_do_remove(struct i2c_client *client)
1643{
1644 struct pmbus_data *data = i2c_get_clientdata(client);
1645 hwmon_device_unregister(data->hwmon_dev);
1646 sysfs_remove_group(&client->dev.kobj, &data->group);
1647 kfree(data->attributes);
1648 kfree(data->labels);
1649 kfree(data->booleans);
1650 kfree(data->sensors);
1651 kfree(data);
1652 return 0;
1653}
1654EXPORT_SYMBOL_GPL(pmbus_do_remove);
1655
1656MODULE_AUTHOR("Guenter Roeck");
1657MODULE_DESCRIPTION("PMBus core driver");
1658MODULE_LICENSE("GPL");
diff --git a/drivers/hwmon/w83627ehf.c b/drivers/hwmon/w83627ehf.c
index 073eabedc432..f2b377c56a3a 100644
--- a/drivers/hwmon/w83627ehf.c
+++ b/drivers/hwmon/w83627ehf.c
@@ -1,11 +1,12 @@
1/* 1/*
2 w83627ehf - Driver for the hardware monitoring functionality of 2 w83627ehf - Driver for the hardware monitoring functionality of
3 the Winbond W83627EHF Super-I/O chip 3 the Winbond W83627EHF Super-I/O chip
4 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org> 4 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
5 Copyright (C) 2006 Yuan Mu (Winbond), 5 Copyright (C) 2006 Yuan Mu (Winbond),
6 Rudolf Marek <r.marek@assembler.cz> 6 Rudolf Marek <r.marek@assembler.cz>
7 David Hubbard <david.c.hubbard@gmail.com> 7 David Hubbard <david.c.hubbard@gmail.com>
8 Daniel J Blueman <daniel.blueman@gmail.com> 8 Daniel J Blueman <daniel.blueman@gmail.com>
9 Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
9 10
10 Shamelessly ripped from the w83627hf driver 11 Shamelessly ripped from the w83627hf driver
11 Copyright (C) 2003 Mark Studebaker 12 Copyright (C) 2003 Mark Studebaker
@@ -35,11 +36,13 @@
35 36
36 Chip #vin #fan #pwm #temp chip IDs man ID 37 Chip #vin #fan #pwm #temp chip IDs man ID
37 w83627ehf 10 5 4 3 0x8850 0x88 0x5ca3 38 w83627ehf 10 5 4 3 0x8850 0x88 0x5ca3
38 0x8860 0xa1 39 0x8860 0xa1
39 w83627dhg 9 5 4 3 0xa020 0xc1 0x5ca3 40 w83627dhg 9 5 4 3 0xa020 0xc1 0x5ca3
40 w83627dhg-p 9 5 4 3 0xb070 0xc1 0x5ca3 41 w83627dhg-p 9 5 4 3 0xb070 0xc1 0x5ca3
41 w83667hg 9 5 3 3 0xa510 0xc1 0x5ca3 42 w83667hg 9 5 3 3 0xa510 0xc1 0x5ca3
42 w83667hg-b 9 5 3 3 0xb350 0xc1 0x5ca3 43 w83667hg-b 9 5 3 4 0xb350 0xc1 0x5ca3
44 nct6775f 9 4 3 9 0xb470 0xc1 0x5ca3
45 nct6776f 9 5 3 9 0xC330 0xc1 0x5ca3
43*/ 46*/
44 47
45#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 48#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -58,21 +61,28 @@
58#include <linux/io.h> 61#include <linux/io.h>
59#include "lm75.h" 62#include "lm75.h"
60 63
61enum kinds { w83627ehf, w83627dhg, w83627dhg_p, w83667hg, w83667hg_b }; 64enum kinds { w83627ehf, w83627dhg, w83627dhg_p, w83667hg, w83667hg_b, nct6775,
65 nct6776 };
62 66
63/* used to set data->name = w83627ehf_device_names[data->sio_kind] */ 67/* used to set data->name = w83627ehf_device_names[data->sio_kind] */
64static const char * w83627ehf_device_names[] = { 68static const char * const w83627ehf_device_names[] = {
65 "w83627ehf", 69 "w83627ehf",
66 "w83627dhg", 70 "w83627dhg",
67 "w83627dhg", 71 "w83627dhg",
68 "w83667hg", 72 "w83667hg",
69 "w83667hg", 73 "w83667hg",
74 "nct6775",
75 "nct6776",
70}; 76};
71 77
72static unsigned short force_id; 78static unsigned short force_id;
73module_param(force_id, ushort, 0); 79module_param(force_id, ushort, 0);
74MODULE_PARM_DESC(force_id, "Override the detected device ID"); 80MODULE_PARM_DESC(force_id, "Override the detected device ID");
75 81
82static unsigned short fan_debounce;
83module_param(fan_debounce, ushort, 0);
84MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
85
76#define DRVNAME "w83627ehf" 86#define DRVNAME "w83627ehf"
77 87
78/* 88/*
@@ -80,7 +90,7 @@ MODULE_PARM_DESC(force_id, "Override the detected device ID");
80 */ 90 */
81 91
82#define W83627EHF_LD_HWM 0x0b 92#define W83627EHF_LD_HWM 0x0b
83#define W83667HG_LD_VID 0x0d 93#define W83667HG_LD_VID 0x0d
84 94
85#define SIO_REG_LDSEL 0x07 /* Logical device select */ 95#define SIO_REG_LDSEL 0x07 /* Logical device select */
86#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */ 96#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
@@ -94,8 +104,10 @@ MODULE_PARM_DESC(force_id, "Override the detected device ID");
94#define SIO_W83627EHG_ID 0x8860 104#define SIO_W83627EHG_ID 0x8860
95#define SIO_W83627DHG_ID 0xa020 105#define SIO_W83627DHG_ID 0xa020
96#define SIO_W83627DHG_P_ID 0xb070 106#define SIO_W83627DHG_P_ID 0xb070
97#define SIO_W83667HG_ID 0xa510 107#define SIO_W83667HG_ID 0xa510
98#define SIO_W83667HG_B_ID 0xb350 108#define SIO_W83667HG_B_ID 0xb350
109#define SIO_NCT6775_ID 0xb470
110#define SIO_NCT6776_ID 0xc330
99#define SIO_ID_MASK 0xFFF0 111#define SIO_ID_MASK 0xFFF0
100 112
101static inline void 113static inline void
@@ -138,7 +150,7 @@ superio_exit(int ioreg)
138 * ISA constants 150 * ISA constants
139 */ 151 */
140 152
141#define IOREGION_ALIGNMENT ~7 153#define IOREGION_ALIGNMENT (~7)
142#define IOREGION_OFFSET 5 154#define IOREGION_OFFSET 5
143#define IOREGION_LENGTH 2 155#define IOREGION_LENGTH 2
144#define ADDR_REG_OFFSET 0 156#define ADDR_REG_OFFSET 0
@@ -164,13 +176,10 @@ static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
164#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \ 176#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
165 (0x550 + (nr) - 7)) 177 (0x550 + (nr) - 7))
166 178
167#define W83627EHF_REG_TEMP1 0x27 179static const u16 W83627EHF_REG_TEMP[] = { 0x27, 0x150, 0x250, 0x7e };
168#define W83627EHF_REG_TEMP1_HYST 0x3a 180static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x3a, 0x153, 0x253, 0 };
169#define W83627EHF_REG_TEMP1_OVER 0x39 181static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x39, 0x155, 0x255, 0 };
170static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 }; 182static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0, 0x152, 0x252, 0 };
171static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
172static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
173static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
174 183
175/* Fan clock dividers are spread over the following five registers */ 184/* Fan clock dividers are spread over the following five registers */
176#define W83627EHF_REG_FANDIV1 0x47 185#define W83627EHF_REG_FANDIV1 0x47
@@ -179,6 +188,11 @@ static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
179#define W83627EHF_REG_DIODE 0x59 188#define W83627EHF_REG_DIODE 0x59
180#define W83627EHF_REG_SMI_OVT 0x4C 189#define W83627EHF_REG_SMI_OVT 0x4C
181 190
191/* NCT6775F has its own fan divider registers */
192#define NCT6775_REG_FANDIV1 0x506
193#define NCT6775_REG_FANDIV2 0x507
194#define NCT6775_REG_FAN_DEBOUNCE 0xf0
195
182#define W83627EHF_REG_ALARM1 0x459 196#define W83627EHF_REG_ALARM1 0x459
183#define W83627EHF_REG_ALARM2 0x45A 197#define W83627EHF_REG_ALARM2 0x45A
184#define W83627EHF_REG_ALARM3 0x45B 198#define W83627EHF_REG_ALARM3 0x45B
@@ -199,22 +213,123 @@ static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
199static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 }; 213static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
200 214
201/* FAN Duty Cycle, be used to control */ 215/* FAN Duty Cycle, be used to control */
202static const u8 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 }; 216static const u16 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
203static const u8 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 }; 217static const u16 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
204static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 }; 218static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
205 219
206/* Advanced Fan control, some values are common for all fans */ 220/* Advanced Fan control, some values are common for all fans */
207static const u8 W83627EHF_REG_FAN_START_OUTPUT[] = { 0x0a, 0x0b, 0x16, 0x65 }; 221static const u16 W83627EHF_REG_FAN_START_OUTPUT[] = { 0x0a, 0x0b, 0x16, 0x65 };
208static const u8 W83627EHF_REG_FAN_STOP_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 }; 222static const u16 W83627EHF_REG_FAN_STOP_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
209static const u8 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0c, 0x0d, 0x17, 0x66 }; 223static const u16 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0c, 0x0d, 0x17, 0x66 };
210 224
211static const u8 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON[] 225static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON[]
212 = { 0xff, 0x67, 0xff, 0x69 }; 226 = { 0xff, 0x67, 0xff, 0x69 };
213static const u8 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON[] 227static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_COMMON[]
214 = { 0xff, 0x68, 0xff, 0x6a }; 228 = { 0xff, 0x68, 0xff, 0x6a };
215 229
216static const u8 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B[] = { 0x67, 0x69, 0x6b }; 230static const u16 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B[] = { 0x67, 0x69, 0x6b };
217static const u8 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B[] = { 0x68, 0x6a, 0x6c }; 231static const u16 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B[]
232 = { 0x68, 0x6a, 0x6c };
233
234static const u16 NCT6775_REG_TARGET[] = { 0x101, 0x201, 0x301 };
235static const u16 NCT6775_REG_FAN_MODE[] = { 0x102, 0x202, 0x302 };
236static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = { 0x105, 0x205, 0x305 };
237static const u16 NCT6775_REG_FAN_START_OUTPUT[] = { 0x106, 0x206, 0x306 };
238static const u16 NCT6775_REG_FAN_STOP_TIME[] = { 0x107, 0x207, 0x307 };
239static const u16 NCT6775_REG_PWM[] = { 0x109, 0x209, 0x309 };
240static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
241static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
242static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
243static const u16 NCT6776_REG_FAN_MIN[] = { 0x63a, 0x63c, 0x63e, 0x640, 0x642};
244
245static const u16 NCT6775_REG_TEMP[]
246 = { 0x27, 0x150, 0x250, 0x73, 0x75, 0x77, 0x62b, 0x62c, 0x62d };
247static const u16 NCT6775_REG_TEMP_CONFIG[]
248 = { 0, 0x152, 0x252, 0, 0, 0, 0x628, 0x629, 0x62A };
249static const u16 NCT6775_REG_TEMP_HYST[]
250 = { 0x3a, 0x153, 0x253, 0, 0, 0, 0x673, 0x678, 0x67D };
251static const u16 NCT6775_REG_TEMP_OVER[]
252 = { 0x39, 0x155, 0x255, 0, 0, 0, 0x672, 0x677, 0x67C };
253static const u16 NCT6775_REG_TEMP_SOURCE[]
254 = { 0x621, 0x622, 0x623, 0x100, 0x200, 0x300, 0x624, 0x625, 0x626 };
255
256static const char *const w83667hg_b_temp_label[] = {
257 "SYSTIN",
258 "CPUTIN",
259 "AUXTIN",
260 "AMDTSI",
261 "PECI Agent 1",
262 "PECI Agent 2",
263 "PECI Agent 3",
264 "PECI Agent 4"
265};
266
267static const char *const nct6775_temp_label[] = {
268 "",
269 "SYSTIN",
270 "CPUTIN",
271 "AUXTIN",
272 "AMD SB-TSI",
273 "PECI Agent 0",
274 "PECI Agent 1",
275 "PECI Agent 2",
276 "PECI Agent 3",
277 "PECI Agent 4",
278 "PECI Agent 5",
279 "PECI Agent 6",
280 "PECI Agent 7",
281 "PCH_CHIP_CPU_MAX_TEMP",
282 "PCH_CHIP_TEMP",
283 "PCH_CPU_TEMP",
284 "PCH_MCH_TEMP",
285 "PCH_DIM0_TEMP",
286 "PCH_DIM1_TEMP",
287 "PCH_DIM2_TEMP",
288 "PCH_DIM3_TEMP"
289};
290
291static const char *const nct6776_temp_label[] = {
292 "",
293 "SYSTIN",
294 "CPUTIN",
295 "AUXTIN",
296 "SMBUSMASTER 0",
297 "SMBUSMASTER 1",
298 "SMBUSMASTER 2",
299 "SMBUSMASTER 3",
300 "SMBUSMASTER 4",
301 "SMBUSMASTER 5",
302 "SMBUSMASTER 6",
303 "SMBUSMASTER 7",
304 "PECI Agent 0",
305 "PECI Agent 1",
306 "PCH_CHIP_CPU_MAX_TEMP",
307 "PCH_CHIP_TEMP",
308 "PCH_CPU_TEMP",
309 "PCH_MCH_TEMP",
310 "PCH_DIM0_TEMP",
311 "PCH_DIM1_TEMP",
312 "PCH_DIM2_TEMP",
313 "PCH_DIM3_TEMP",
314 "BYTE_TEMP"
315};
316
317#define NUM_REG_TEMP ARRAY_SIZE(NCT6775_REG_TEMP)
318
319static inline int is_word_sized(u16 reg)
320{
321 return ((((reg & 0xff00) == 0x100
322 || (reg & 0xff00) == 0x200)
323 && ((reg & 0x00ff) == 0x50
324 || (reg & 0x00ff) == 0x53
325 || (reg & 0x00ff) == 0x55))
326 || (reg & 0xfff0) == 0x630
327 || reg == 0x640 || reg == 0x642
328 || ((reg & 0xfff0) == 0x650
329 && (reg & 0x000f) >= 0x06)
330 || reg == 0x73 || reg == 0x75 || reg == 0x77
331 );
332}
218 333
219/* 334/*
220 * Conversions 335 * Conversions
@@ -232,12 +347,36 @@ static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
232 (msec + 200) / 400), 1, 255); 347 (msec + 200) / 400), 1, 255);
233} 348}
234 349
235static inline unsigned int 350static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
236fan_from_reg(u8 reg, unsigned int div)
237{ 351{
238 if (reg == 0 || reg == 255) 352 if (reg == 0 || reg == 255)
239 return 0; 353 return 0;
240 return 1350000U / (reg * div); 354 return 1350000U / (reg << divreg);
355}
356
357static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
358{
359 if ((reg & 0xff1f) == 0xff1f)
360 return 0;
361
362 reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
363
364 if (reg == 0)
365 return 0;
366
367 return 1350000U / reg;
368}
369
370static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
371{
372 if (reg == 0 || reg == 0xffff)
373 return 0;
374
375 /*
376 * Even though the registers are 16 bit wide, the fan divisor
377 * still applies.
378 */
379 return 1350000U / (reg << divreg);
241} 380}
242 381
243static inline unsigned int 382static inline unsigned int
@@ -247,21 +386,19 @@ div_from_reg(u8 reg)
247} 386}
248 387
249static inline int 388static inline int
250temp1_from_reg(s8 reg) 389temp_from_reg(u16 reg, s16 regval)
251{ 390{
252 return reg * 1000; 391 if (is_word_sized(reg))
392 return LM75_TEMP_FROM_REG(regval);
393 return regval * 1000;
253} 394}
254 395
255static inline s8 396static inline u16
256temp1_to_reg(long temp, int min, int max) 397temp_to_reg(u16 reg, long temp)
257{ 398{
258 if (temp <= min) 399 if (is_word_sized(reg))
259 return min / 1000; 400 return LM75_TEMP_TO_REG(temp);
260 if (temp >= max) 401 return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, -127000, 128000), 1000);
261 return max / 1000;
262 if (temp < 0)
263 return (temp - 500) / 1000;
264 return (temp + 500) / 1000;
265} 402}
266 403
267/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */ 404/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
@@ -275,7 +412,8 @@ static inline long in_from_reg(u8 reg, u8 nr)
275 412
276static inline u8 in_to_reg(u32 val, u8 nr) 413static inline u8 in_to_reg(u32 val, u8 nr)
277{ 414{
278 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0, 255); 415 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0,
416 255);
279} 417}
280 418
281/* 419/*
@@ -289,38 +427,57 @@ struct w83627ehf_data {
289 struct device *hwmon_dev; 427 struct device *hwmon_dev;
290 struct mutex lock; 428 struct mutex lock;
291 429
292 const u8 *REG_FAN_START_OUTPUT; 430 u16 reg_temp[NUM_REG_TEMP];
293 const u8 *REG_FAN_STOP_OUTPUT; 431 u16 reg_temp_over[NUM_REG_TEMP];
294 const u8 *REG_FAN_MAX_OUTPUT; 432 u16 reg_temp_hyst[NUM_REG_TEMP];
295 const u8 *REG_FAN_STEP_OUTPUT; 433 u16 reg_temp_config[NUM_REG_TEMP];
434 u8 temp_src[NUM_REG_TEMP];
435 const char * const *temp_label;
436
437 const u16 *REG_PWM;
438 const u16 *REG_TARGET;
439 const u16 *REG_FAN;
440 const u16 *REG_FAN_MIN;
441 const u16 *REG_FAN_START_OUTPUT;
442 const u16 *REG_FAN_STOP_OUTPUT;
443 const u16 *REG_FAN_STOP_TIME;
444 const u16 *REG_FAN_MAX_OUTPUT;
445 const u16 *REG_FAN_STEP_OUTPUT;
446
447 unsigned int (*fan_from_reg)(u16 reg, unsigned int divreg);
448 unsigned int (*fan_from_reg_min)(u16 reg, unsigned int divreg);
296 449
297 struct mutex update_lock; 450 struct mutex update_lock;
298 char valid; /* !=0 if following fields are valid */ 451 char valid; /* !=0 if following fields are valid */
299 unsigned long last_updated; /* In jiffies */ 452 unsigned long last_updated; /* In jiffies */
300 453
301 /* Register values */ 454 /* Register values */
455 u8 bank; /* current register bank */
302 u8 in_num; /* number of in inputs we have */ 456 u8 in_num; /* number of in inputs we have */
303 u8 in[10]; /* Register value */ 457 u8 in[10]; /* Register value */
304 u8 in_max[10]; /* Register value */ 458 u8 in_max[10]; /* Register value */
305 u8 in_min[10]; /* Register value */ 459 u8 in_min[10]; /* Register value */
306 u8 fan[5]; 460 unsigned int rpm[5];
307 u8 fan_min[5]; 461 u16 fan_min[5];
308 u8 fan_div[5]; 462 u8 fan_div[5];
309 u8 has_fan; /* some fan inputs can be disabled */ 463 u8 has_fan; /* some fan inputs can be disabled */
464 u8 has_fan_min; /* some fans don't have min register */
465 bool has_fan_div;
310 u8 temp_type[3]; 466 u8 temp_type[3];
311 s8 temp1; 467 s16 temp[9];
312 s8 temp1_max; 468 s16 temp_max[9];
313 s8 temp1_max_hyst; 469 s16 temp_max_hyst[9];
314 s16 temp[2];
315 s16 temp_max[2];
316 s16 temp_max_hyst[2];
317 u32 alarms; 470 u32 alarms;
318 471
319 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */ 472 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
320 u8 pwm_enable[4]; /* 1->manual 473 u8 pwm_enable[4]; /* 1->manual
321 2->thermal cruise mode (also called SmartFan I) 474 2->thermal cruise mode (also called SmartFan I)
322 3->fan speed cruise mode 475 3->fan speed cruise mode
323 4->variable thermal cruise (also called SmartFan III) */ 476 4->variable thermal cruise (also called
477 SmartFan III)
478 5->enhanced variable thermal cruise (also called
479 SmartFan IV) */
480 u8 pwm_enable_orig[4]; /* original value of pwm_enable */
324 u8 pwm_num; /* number of pwm */ 481 u8 pwm_num; /* number of pwm */
325 u8 pwm[4]; 482 u8 pwm[4];
326 u8 target_temp[4]; 483 u8 target_temp[4];
@@ -335,7 +492,7 @@ struct w83627ehf_data {
335 u8 vid; 492 u8 vid;
336 u8 vrm; 493 u8 vrm;
337 494
338 u8 temp3_disable; 495 u16 have_temp;
339 u8 in6_skip; 496 u8 in6_skip;
340}; 497};
341 498
@@ -344,30 +501,19 @@ struct w83627ehf_sio_data {
344 enum kinds kind; 501 enum kinds kind;
345}; 502};
346 503
347static inline int is_word_sized(u16 reg) 504/*
348{ 505 * On older chips, only registers 0x50-0x5f are banked.
349 return (((reg & 0xff00) == 0x100 506 * On more recent chips, all registers are banked.
350 || (reg & 0xff00) == 0x200) 507 * Assume that is the case and set the bank number for each access.
351 && ((reg & 0x00ff) == 0x50 508 * Cache the bank number so it only needs to be set if it changes.
352 || (reg & 0x00ff) == 0x53 509 */
353 || (reg & 0x00ff) == 0x55));
354}
355
356/* Registers 0x50-0x5f are banked */
357static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg) 510static inline void w83627ehf_set_bank(struct w83627ehf_data *data, u16 reg)
358{ 511{
359 if ((reg & 0x00f0) == 0x50) { 512 u8 bank = reg >> 8;
513 if (data->bank != bank) {
360 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET); 514 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
361 outb_p(reg >> 8, data->addr + DATA_REG_OFFSET); 515 outb_p(bank, data->addr + DATA_REG_OFFSET);
362 } 516 data->bank = bank;
363}
364
365/* Not strictly necessary, but play it safe for now */
366static inline void w83627ehf_reset_bank(struct w83627ehf_data *data, u16 reg)
367{
368 if (reg & 0xff00) {
369 outb_p(W83627EHF_REG_BANK, data->addr + ADDR_REG_OFFSET);
370 outb_p(0, data->addr + DATA_REG_OFFSET);
371 } 517 }
372} 518}
373 519
@@ -385,14 +531,13 @@ static u16 w83627ehf_read_value(struct w83627ehf_data *data, u16 reg)
385 data->addr + ADDR_REG_OFFSET); 531 data->addr + ADDR_REG_OFFSET);
386 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET); 532 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
387 } 533 }
388 w83627ehf_reset_bank(data, reg);
389 534
390 mutex_unlock(&data->lock); 535 mutex_unlock(&data->lock);
391
392 return res; 536 return res;
393} 537}
394 538
395static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg, u16 value) 539static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg,
540 u16 value)
396{ 541{
397 int word_sized = is_word_sized(reg); 542 int word_sized = is_word_sized(reg);
398 543
@@ -406,13 +551,40 @@ static int w83627ehf_write_value(struct w83627ehf_data *data, u16 reg, u16 value
406 data->addr + ADDR_REG_OFFSET); 551 data->addr + ADDR_REG_OFFSET);
407 } 552 }
408 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET); 553 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
409 w83627ehf_reset_bank(data, reg);
410 554
411 mutex_unlock(&data->lock); 555 mutex_unlock(&data->lock);
412 return 0; 556 return 0;
413} 557}
414 558
415/* This function assumes that the caller holds data->update_lock */ 559/* This function assumes that the caller holds data->update_lock */
560static void nct6775_write_fan_div(struct w83627ehf_data *data, int nr)
561{
562 u8 reg;
563
564 switch (nr) {
565 case 0:
566 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x70)
567 | (data->fan_div[0] & 0x7);
568 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
569 break;
570 case 1:
571 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV1) & 0x7)
572 | ((data->fan_div[1] << 4) & 0x70);
573 w83627ehf_write_value(data, NCT6775_REG_FANDIV1, reg);
574 case 2:
575 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x70)
576 | (data->fan_div[2] & 0x7);
577 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
578 break;
579 case 3:
580 reg = (w83627ehf_read_value(data, NCT6775_REG_FANDIV2) & 0x7)
581 | ((data->fan_div[3] << 4) & 0x70);
582 w83627ehf_write_value(data, NCT6775_REG_FANDIV2, reg);
583 break;
584 }
585}
586
587/* This function assumes that the caller holds data->update_lock */
416static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr) 588static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
417{ 589{
418 u8 reg; 590 u8 reg;
@@ -463,6 +635,32 @@ static void w83627ehf_write_fan_div(struct w83627ehf_data *data, int nr)
463 } 635 }
464} 636}
465 637
638static void w83627ehf_write_fan_div_common(struct device *dev,
639 struct w83627ehf_data *data, int nr)
640{
641 struct w83627ehf_sio_data *sio_data = dev->platform_data;
642
643 if (sio_data->kind == nct6776)
644 ; /* no dividers, do nothing */
645 else if (sio_data->kind == nct6775)
646 nct6775_write_fan_div(data, nr);
647 else
648 w83627ehf_write_fan_div(data, nr);
649}
650
651static void nct6775_update_fan_div(struct w83627ehf_data *data)
652{
653 u8 i;
654
655 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV1);
656 data->fan_div[0] = i & 0x7;
657 data->fan_div[1] = (i & 0x70) >> 4;
658 i = w83627ehf_read_value(data, NCT6775_REG_FANDIV2);
659 data->fan_div[2] = i & 0x7;
660 if (data->has_fan & (1<<3))
661 data->fan_div[3] = (i & 0x70) >> 4;
662}
663
466static void w83627ehf_update_fan_div(struct w83627ehf_data *data) 664static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
467{ 665{
468 int i; 666 int i;
@@ -488,10 +686,79 @@ static void w83627ehf_update_fan_div(struct w83627ehf_data *data)
488 } 686 }
489} 687}
490 688
689static void w83627ehf_update_fan_div_common(struct device *dev,
690 struct w83627ehf_data *data)
691{
692 struct w83627ehf_sio_data *sio_data = dev->platform_data;
693
694 if (sio_data->kind == nct6776)
695 ; /* no dividers, do nothing */
696 else if (sio_data->kind == nct6775)
697 nct6775_update_fan_div(data);
698 else
699 w83627ehf_update_fan_div(data);
700}
701
702static void nct6775_update_pwm(struct w83627ehf_data *data)
703{
704 int i;
705 int pwmcfg, fanmodecfg;
706
707 for (i = 0; i < data->pwm_num; i++) {
708 pwmcfg = w83627ehf_read_value(data,
709 W83627EHF_REG_PWM_ENABLE[i]);
710 fanmodecfg = w83627ehf_read_value(data,
711 NCT6775_REG_FAN_MODE[i]);
712 data->pwm_mode[i] =
713 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
714 data->pwm_enable[i] = ((fanmodecfg >> 4) & 7) + 1;
715 data->tolerance[i] = fanmodecfg & 0x0f;
716 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
717 }
718}
719
720static void w83627ehf_update_pwm(struct w83627ehf_data *data)
721{
722 int i;
723 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
724
725 for (i = 0; i < data->pwm_num; i++) {
726 if (!(data->has_fan & (1 << i)))
727 continue;
728
729 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */
730 if (i != 1) {
731 pwmcfg = w83627ehf_read_value(data,
732 W83627EHF_REG_PWM_ENABLE[i]);
733 tolerance = w83627ehf_read_value(data,
734 W83627EHF_REG_TOLERANCE[i]);
735 }
736 data->pwm_mode[i] =
737 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) ? 0 : 1;
738 data->pwm_enable[i] = ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
739 & 3) + 1;
740 data->pwm[i] = w83627ehf_read_value(data, data->REG_PWM[i]);
741
742 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0)) & 0x0f;
743 }
744}
745
746static void w83627ehf_update_pwm_common(struct device *dev,
747 struct w83627ehf_data *data)
748{
749 struct w83627ehf_sio_data *sio_data = dev->platform_data;
750
751 if (sio_data->kind == nct6775 || sio_data->kind == nct6776)
752 nct6775_update_pwm(data);
753 else
754 w83627ehf_update_pwm(data);
755}
756
491static struct w83627ehf_data *w83627ehf_update_device(struct device *dev) 757static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
492{ 758{
493 struct w83627ehf_data *data = dev_get_drvdata(dev); 759 struct w83627ehf_data *data = dev_get_drvdata(dev);
494 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */ 760 struct w83627ehf_sio_data *sio_data = dev->platform_data;
761
495 int i; 762 int i;
496 763
497 mutex_lock(&data->update_lock); 764 mutex_lock(&data->update_lock);
@@ -499,7 +766,7 @@ static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
499 if (time_after(jiffies, data->last_updated + HZ + HZ/2) 766 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
500 || !data->valid) { 767 || !data->valid) {
501 /* Fan clock dividers */ 768 /* Fan clock dividers */
502 w83627ehf_update_fan_div(data); 769 w83627ehf_update_fan_div_common(dev, data);
503 770
504 /* Measured voltages and limits */ 771 /* Measured voltages and limits */
505 for (i = 0; i < data->in_num; i++) { 772 for (i = 0; i < data->in_num; i++) {
@@ -513,92 +780,90 @@ static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
513 780
514 /* Measured fan speeds and limits */ 781 /* Measured fan speeds and limits */
515 for (i = 0; i < 5; i++) { 782 for (i = 0; i < 5; i++) {
783 u16 reg;
784
516 if (!(data->has_fan & (1 << i))) 785 if (!(data->has_fan & (1 << i)))
517 continue; 786 continue;
518 787
519 data->fan[i] = w83627ehf_read_value(data, 788 reg = w83627ehf_read_value(data, data->REG_FAN[i]);
520 W83627EHF_REG_FAN[i]); 789 data->rpm[i] = data->fan_from_reg(reg,
521 data->fan_min[i] = w83627ehf_read_value(data, 790 data->fan_div[i]);
522 W83627EHF_REG_FAN_MIN[i]); 791
792 if (data->has_fan_min & (1 << i))
793 data->fan_min[i] = w83627ehf_read_value(data,
794 data->REG_FAN_MIN[i]);
523 795
524 /* If we failed to measure the fan speed and clock 796 /* If we failed to measure the fan speed and clock
525 divider can be increased, let's try that for next 797 divider can be increased, let's try that for next
526 time */ 798 time */
527 if (data->fan[i] == 0xff 799 if (data->has_fan_div
528 && data->fan_div[i] < 0x07) { 800 && (reg >= 0xff || (sio_data->kind == nct6775
529 dev_dbg(dev, "Increasing fan%d " 801 && reg == 0x00))
802 && data->fan_div[i] < 0x07) {
803 dev_dbg(dev, "Increasing fan%d "
530 "clock divider from %u to %u\n", 804 "clock divider from %u to %u\n",
531 i + 1, div_from_reg(data->fan_div[i]), 805 i + 1, div_from_reg(data->fan_div[i]),
532 div_from_reg(data->fan_div[i] + 1)); 806 div_from_reg(data->fan_div[i] + 1));
533 data->fan_div[i]++; 807 data->fan_div[i]++;
534 w83627ehf_write_fan_div(data, i); 808 w83627ehf_write_fan_div_common(dev, data, i);
535 /* Preserve min limit if possible */ 809 /* Preserve min limit if possible */
536 if (data->fan_min[i] >= 2 810 if ((data->has_fan_min & (1 << i))
811 && data->fan_min[i] >= 2
537 && data->fan_min[i] != 255) 812 && data->fan_min[i] != 255)
538 w83627ehf_write_value(data, 813 w83627ehf_write_value(data,
539 W83627EHF_REG_FAN_MIN[i], 814 data->REG_FAN_MIN[i],
540 (data->fan_min[i] /= 2)); 815 (data->fan_min[i] /= 2));
541 } 816 }
542 } 817 }
543 818
819 w83627ehf_update_pwm_common(dev, data);
820
544 for (i = 0; i < data->pwm_num; i++) { 821 for (i = 0; i < data->pwm_num; i++) {
545 if (!(data->has_fan & (1 << i))) 822 if (!(data->has_fan & (1 << i)))
546 continue; 823 continue;
547 824
548 /* pwmcfg, tolerance mapped for i=0, i=1 to same reg */ 825 data->fan_start_output[i] =
549 if (i != 1) { 826 w83627ehf_read_value(data,
550 pwmcfg = w83627ehf_read_value(data, 827 data->REG_FAN_START_OUTPUT[i]);
551 W83627EHF_REG_PWM_ENABLE[i]); 828 data->fan_stop_output[i] =
552 tolerance = w83627ehf_read_value(data, 829 w83627ehf_read_value(data,
553 W83627EHF_REG_TOLERANCE[i]); 830 data->REG_FAN_STOP_OUTPUT[i]);
554 } 831 data->fan_stop_time[i] =
555 data->pwm_mode[i] = 832 w83627ehf_read_value(data,
556 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1) 833 data->REG_FAN_STOP_TIME[i]);
557 ? 0 : 1; 834
558 data->pwm_enable[i] = 835 if (data->REG_FAN_MAX_OUTPUT &&
559 ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i]) 836 data->REG_FAN_MAX_OUTPUT[i] != 0xff)
560 & 3) + 1;
561 data->pwm[i] = w83627ehf_read_value(data,
562 W83627EHF_REG_PWM[i]);
563 data->fan_start_output[i] = w83627ehf_read_value(data,
564 W83627EHF_REG_FAN_START_OUTPUT[i]);
565 data->fan_stop_output[i] = w83627ehf_read_value(data,
566 W83627EHF_REG_FAN_STOP_OUTPUT[i]);
567 data->fan_stop_time[i] = w83627ehf_read_value(data,
568 W83627EHF_REG_FAN_STOP_TIME[i]);
569
570 if (data->REG_FAN_MAX_OUTPUT[i] != 0xff)
571 data->fan_max_output[i] = 837 data->fan_max_output[i] =
572 w83627ehf_read_value(data, 838 w83627ehf_read_value(data,
573 data->REG_FAN_MAX_OUTPUT[i]); 839 data->REG_FAN_MAX_OUTPUT[i]);
574 840
575 if (data->REG_FAN_STEP_OUTPUT[i] != 0xff) 841 if (data->REG_FAN_STEP_OUTPUT &&
842 data->REG_FAN_STEP_OUTPUT[i] != 0xff)
576 data->fan_step_output[i] = 843 data->fan_step_output[i] =
577 w83627ehf_read_value(data, 844 w83627ehf_read_value(data,
578 data->REG_FAN_STEP_OUTPUT[i]); 845 data->REG_FAN_STEP_OUTPUT[i]);
579 846
580 data->target_temp[i] = 847 data->target_temp[i] =
581 w83627ehf_read_value(data, 848 w83627ehf_read_value(data,
582 W83627EHF_REG_TARGET[i]) & 849 data->REG_TARGET[i]) &
583 (data->pwm_mode[i] == 1 ? 0x7f : 0xff); 850 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
584 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0))
585 & 0x0f;
586 } 851 }
587 852
588 /* Measured temperatures and limits */ 853 /* Measured temperatures and limits */
589 data->temp1 = w83627ehf_read_value(data, 854 for (i = 0; i < NUM_REG_TEMP; i++) {
590 W83627EHF_REG_TEMP1); 855 if (!(data->have_temp & (1 << i)))
591 data->temp1_max = w83627ehf_read_value(data, 856 continue;
592 W83627EHF_REG_TEMP1_OVER);
593 data->temp1_max_hyst = w83627ehf_read_value(data,
594 W83627EHF_REG_TEMP1_HYST);
595 for (i = 0; i < 2; i++) {
596 data->temp[i] = w83627ehf_read_value(data, 857 data->temp[i] = w83627ehf_read_value(data,
597 W83627EHF_REG_TEMP[i]); 858 data->reg_temp[i]);
598 data->temp_max[i] = w83627ehf_read_value(data, 859 if (data->reg_temp_over[i])
599 W83627EHF_REG_TEMP_OVER[i]); 860 data->temp_max[i]
600 data->temp_max_hyst[i] = w83627ehf_read_value(data, 861 = w83627ehf_read_value(data,
601 W83627EHF_REG_TEMP_HYST[i]); 862 data->reg_temp_over[i]);
863 if (data->reg_temp_hyst[i])
864 data->temp_max_hyst[i]
865 = w83627ehf_read_value(data,
866 data->reg_temp_hyst[i]);
602 } 867 }
603 868
604 data->alarms = w83627ehf_read_value(data, 869 data->alarms = w83627ehf_read_value(data,
@@ -625,7 +890,8 @@ show_##reg(struct device *dev, struct device_attribute *attr, \
625 char *buf) \ 890 char *buf) \
626{ \ 891{ \
627 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 892 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
628 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 893 struct sensor_device_attribute *sensor_attr = \
894 to_sensor_dev_attr(attr); \
629 int nr = sensor_attr->index; \ 895 int nr = sensor_attr->index; \
630 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \ 896 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
631} 897}
@@ -635,14 +901,18 @@ show_in_reg(in_max)
635 901
636#define store_in_reg(REG, reg) \ 902#define store_in_reg(REG, reg) \
637static ssize_t \ 903static ssize_t \
638store_in_##reg (struct device *dev, struct device_attribute *attr, \ 904store_in_##reg(struct device *dev, struct device_attribute *attr, \
639 const char *buf, size_t count) \ 905 const char *buf, size_t count) \
640{ \ 906{ \
641 struct w83627ehf_data *data = dev_get_drvdata(dev); \ 907 struct w83627ehf_data *data = dev_get_drvdata(dev); \
642 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 908 struct sensor_device_attribute *sensor_attr = \
909 to_sensor_dev_attr(attr); \
643 int nr = sensor_attr->index; \ 910 int nr = sensor_attr->index; \
644 u32 val = simple_strtoul(buf, NULL, 10); \ 911 unsigned long val; \
645 \ 912 int err; \
913 err = strict_strtoul(buf, 10, &val); \
914 if (err < 0) \
915 return err; \
646 mutex_lock(&data->update_lock); \ 916 mutex_lock(&data->update_lock); \
647 data->in_##reg[nr] = in_to_reg(val, nr); \ 917 data->in_##reg[nr] = in_to_reg(val, nr); \
648 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \ 918 w83627ehf_write_value(data, W83627EHF_REG_IN_##REG(nr), \
@@ -654,7 +924,8 @@ store_in_##reg (struct device *dev, struct device_attribute *attr, \
654store_in_reg(MIN, min) 924store_in_reg(MIN, min)
655store_in_reg(MAX, max) 925store_in_reg(MAX, max)
656 926
657static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, char *buf) 927static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
928 char *buf)
658{ 929{
659 struct w83627ehf_data *data = w83627ehf_update_device(dev); 930 struct w83627ehf_data *data = w83627ehf_update_device(dev);
660 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 931 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
@@ -689,45 +960,50 @@ static struct sensor_device_attribute sda_in_alarm[] = {
689}; 960};
690 961
691static struct sensor_device_attribute sda_in_min[] = { 962static struct sensor_device_attribute sda_in_min[] = {
692 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0), 963 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
693 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1), 964 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
694 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2), 965 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
695 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3), 966 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
696 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4), 967 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
697 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5), 968 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
698 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6), 969 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
699 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7), 970 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
700 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8), 971 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
701 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9), 972 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
702}; 973};
703 974
704static struct sensor_device_attribute sda_in_max[] = { 975static struct sensor_device_attribute sda_in_max[] = {
705 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0), 976 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
706 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1), 977 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
707 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2), 978 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
708 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3), 979 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
709 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4), 980 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
710 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5), 981 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
711 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6), 982 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
712 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7), 983 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
713 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8), 984 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
714 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9), 985 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
715}; 986};
716 987
717#define show_fan_reg(reg) \ 988static ssize_t
718static ssize_t \ 989show_fan(struct device *dev, struct device_attribute *attr, char *buf)
719show_##reg(struct device *dev, struct device_attribute *attr, \ 990{
720 char *buf) \ 991 struct w83627ehf_data *data = w83627ehf_update_device(dev);
721{ \ 992 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 993 int nr = sensor_attr->index;
723 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 994 return sprintf(buf, "%d\n", data->rpm[nr]);
724 int nr = sensor_attr->index; \ 995}
725 return sprintf(buf, "%d\n", \ 996
726 fan_from_reg(data->reg[nr], \ 997static ssize_t
727 div_from_reg(data->fan_div[nr]))); \ 998show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
999{
1000 struct w83627ehf_data *data = w83627ehf_update_device(dev);
1001 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1002 int nr = sensor_attr->index;
1003 return sprintf(buf, "%d\n",
1004 data->fan_from_reg_min(data->fan_min[nr],
1005 data->fan_div[nr]));
728} 1006}
729show_fan_reg(fan);
730show_fan_reg(fan_min);
731 1007
732static ssize_t 1008static ssize_t
733show_fan_div(struct device *dev, struct device_attribute *attr, 1009show_fan_div(struct device *dev, struct device_attribute *attr,
@@ -746,11 +1022,32 @@ store_fan_min(struct device *dev, struct device_attribute *attr,
746 struct w83627ehf_data *data = dev_get_drvdata(dev); 1022 struct w83627ehf_data *data = dev_get_drvdata(dev);
747 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1023 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
748 int nr = sensor_attr->index; 1024 int nr = sensor_attr->index;
749 unsigned int val = simple_strtoul(buf, NULL, 10); 1025 unsigned long val;
1026 int err;
750 unsigned int reg; 1027 unsigned int reg;
751 u8 new_div; 1028 u8 new_div;
752 1029
1030 err = strict_strtoul(buf, 10, &val);
1031 if (err < 0)
1032 return err;
1033
753 mutex_lock(&data->update_lock); 1034 mutex_lock(&data->update_lock);
1035 if (!data->has_fan_div) {
1036 /*
1037 * Only NCT6776F for now, so we know that this is a 13 bit
1038 * register
1039 */
1040 if (!val) {
1041 val = 0xff1f;
1042 } else {
1043 if (val > 1350000U)
1044 val = 135000U;
1045 val = 1350000U / val;
1046 val = (val & 0x1f) | ((val << 3) & 0xff00);
1047 }
1048 data->fan_min[nr] = val;
1049 goto done; /* Leave fan divider alone */
1050 }
754 if (!val) { 1051 if (!val) {
755 /* No min limit, alarm disabled */ 1052 /* No min limit, alarm disabled */
756 data->fan_min[nr] = 255; 1053 data->fan_min[nr] = 255;
@@ -761,15 +1058,17 @@ store_fan_min(struct device *dev, struct device_attribute *attr,
761 even with the highest divider (128) */ 1058 even with the highest divider (128) */
762 data->fan_min[nr] = 254; 1059 data->fan_min[nr] = 254;
763 new_div = 7; /* 128 == (1 << 7) */ 1060 new_div = 7; /* 128 == (1 << 7) */
764 dev_warn(dev, "fan%u low limit %u below minimum %u, set to " 1061 dev_warn(dev, "fan%u low limit %lu below minimum %u, set to "
765 "minimum\n", nr + 1, val, fan_from_reg(254, 128)); 1062 "minimum\n", nr + 1, val,
1063 data->fan_from_reg_min(254, 7));
766 } else if (!reg) { 1064 } else if (!reg) {
767 /* Speed above this value cannot possibly be represented, 1065 /* Speed above this value cannot possibly be represented,
768 even with the lowest divider (1) */ 1066 even with the lowest divider (1) */
769 data->fan_min[nr] = 1; 1067 data->fan_min[nr] = 1;
770 new_div = 0; /* 1 == (1 << 0) */ 1068 new_div = 0; /* 1 == (1 << 0) */
771 dev_warn(dev, "fan%u low limit %u above maximum %u, set to " 1069 dev_warn(dev, "fan%u low limit %lu above maximum %u, set to "
772 "maximum\n", nr + 1, val, fan_from_reg(1, 1)); 1070 "maximum\n", nr + 1, val,
1071 data->fan_from_reg_min(1, 0));
773 } else { 1072 } else {
774 /* Automatically pick the best divider, i.e. the one such 1073 /* Automatically pick the best divider, i.e. the one such
775 that the min limit will correspond to a register value 1074 that the min limit will correspond to a register value
@@ -785,25 +1084,16 @@ store_fan_min(struct device *dev, struct device_attribute *attr,
785 /* Write both the fan clock divider (if it changed) and the new 1084 /* Write both the fan clock divider (if it changed) and the new
786 fan min (unconditionally) */ 1085 fan min (unconditionally) */
787 if (new_div != data->fan_div[nr]) { 1086 if (new_div != data->fan_div[nr]) {
788 /* Preserve the fan speed reading */
789 if (data->fan[nr] != 0xff) {
790 if (new_div > data->fan_div[nr])
791 data->fan[nr] >>= new_div - data->fan_div[nr];
792 else if (data->fan[nr] & 0x80)
793 data->fan[nr] = 0xff;
794 else
795 data->fan[nr] <<= data->fan_div[nr] - new_div;
796 }
797
798 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n", 1087 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
799 nr + 1, div_from_reg(data->fan_div[nr]), 1088 nr + 1, div_from_reg(data->fan_div[nr]),
800 div_from_reg(new_div)); 1089 div_from_reg(new_div));
801 data->fan_div[nr] = new_div; 1090 data->fan_div[nr] = new_div;
802 w83627ehf_write_fan_div(data, nr); 1091 w83627ehf_write_fan_div_common(dev, data, nr);
803 /* Give the chip time to sample a new speed value */ 1092 /* Give the chip time to sample a new speed value */
804 data->last_updated = jiffies; 1093 data->last_updated = jiffies;
805 } 1094 }
806 w83627ehf_write_value(data, W83627EHF_REG_FAN_MIN[nr], 1095done:
1096 w83627ehf_write_value(data, data->REG_FAN_MIN[nr],
807 data->fan_min[nr]); 1097 data->fan_min[nr]);
808 mutex_unlock(&data->update_lock); 1098 mutex_unlock(&data->update_lock);
809 1099
@@ -847,70 +1137,54 @@ static struct sensor_device_attribute sda_fan_div[] = {
847 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4), 1137 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
848}; 1138};
849 1139
850#define show_temp1_reg(reg) \ 1140static ssize_t
851static ssize_t \ 1141show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
852show_##reg(struct device *dev, struct device_attribute *attr, \ 1142{
853 char *buf) \ 1143 struct w83627ehf_data *data = w83627ehf_update_device(dev);
854{ \ 1144 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
855 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 1145 int nr = sensor_attr->index;
856 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \ 1146 return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
857}
858show_temp1_reg(temp1);
859show_temp1_reg(temp1_max);
860show_temp1_reg(temp1_max_hyst);
861
862#define store_temp1_reg(REG, reg) \
863static ssize_t \
864store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
865 const char *buf, size_t count) \
866{ \
867 struct w83627ehf_data *data = dev_get_drvdata(dev); \
868 long val = simple_strtol(buf, NULL, 10); \
869 \
870 mutex_lock(&data->update_lock); \
871 data->temp1_##reg = temp1_to_reg(val, -128000, 127000); \
872 w83627ehf_write_value(data, W83627EHF_REG_TEMP1_##REG, \
873 data->temp1_##reg); \
874 mutex_unlock(&data->update_lock); \
875 return count; \
876} 1147}
877store_temp1_reg(OVER, max);
878store_temp1_reg(HYST, max_hyst);
879 1148
880#define show_temp_reg(reg) \ 1149#define show_temp_reg(addr, reg) \
881static ssize_t \ 1150static ssize_t \
882show_##reg(struct device *dev, struct device_attribute *attr, \ 1151show_##reg(struct device *dev, struct device_attribute *attr, \
883 char *buf) \ 1152 char *buf) \
884{ \ 1153{ \
885 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 1154 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
886 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1155 struct sensor_device_attribute *sensor_attr = \
1156 to_sensor_dev_attr(attr); \
887 int nr = sensor_attr->index; \ 1157 int nr = sensor_attr->index; \
888 return sprintf(buf, "%d\n", \ 1158 return sprintf(buf, "%d\n", \
889 LM75_TEMP_FROM_REG(data->reg[nr])); \ 1159 temp_from_reg(data->addr[nr], data->reg[nr])); \
890} 1160}
891show_temp_reg(temp); 1161show_temp_reg(reg_temp, temp);
892show_temp_reg(temp_max); 1162show_temp_reg(reg_temp_over, temp_max);
893show_temp_reg(temp_max_hyst); 1163show_temp_reg(reg_temp_hyst, temp_max_hyst);
894 1164
895#define store_temp_reg(REG, reg) \ 1165#define store_temp_reg(addr, reg) \
896static ssize_t \ 1166static ssize_t \
897store_##reg(struct device *dev, struct device_attribute *attr, \ 1167store_##reg(struct device *dev, struct device_attribute *attr, \
898 const char *buf, size_t count) \ 1168 const char *buf, size_t count) \
899{ \ 1169{ \
900 struct w83627ehf_data *data = dev_get_drvdata(dev); \ 1170 struct w83627ehf_data *data = dev_get_drvdata(dev); \
901 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1171 struct sensor_device_attribute *sensor_attr = \
1172 to_sensor_dev_attr(attr); \
902 int nr = sensor_attr->index; \ 1173 int nr = sensor_attr->index; \
903 long val = simple_strtol(buf, NULL, 10); \ 1174 int err; \
904 \ 1175 long val; \
1176 err = strict_strtol(buf, 10, &val); \
1177 if (err < 0) \
1178 return err; \
905 mutex_lock(&data->update_lock); \ 1179 mutex_lock(&data->update_lock); \
906 data->reg[nr] = LM75_TEMP_TO_REG(val); \ 1180 data->reg[nr] = temp_to_reg(data->addr[nr], val); \
907 w83627ehf_write_value(data, W83627EHF_REG_TEMP_##REG[nr], \ 1181 w83627ehf_write_value(data, data->addr[nr], \
908 data->reg[nr]); \ 1182 data->reg[nr]); \
909 mutex_unlock(&data->update_lock); \ 1183 mutex_unlock(&data->update_lock); \
910 return count; \ 1184 return count; \
911} 1185}
912store_temp_reg(OVER, temp_max); 1186store_temp_reg(reg_temp_over, temp_max);
913store_temp_reg(HYST, temp_max_hyst); 1187store_temp_reg(reg_temp_hyst, temp_max_hyst);
914 1188
915static ssize_t 1189static ssize_t
916show_temp_type(struct device *dev, struct device_attribute *attr, char *buf) 1190show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
@@ -922,27 +1196,69 @@ show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
922} 1196}
923 1197
924static struct sensor_device_attribute sda_temp_input[] = { 1198static struct sensor_device_attribute sda_temp_input[] = {
925 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp1, NULL, 0), 1199 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0),
926 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0), 1200 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1),
927 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 1), 1201 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2),
1202 SENSOR_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3),
1203 SENSOR_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4),
1204 SENSOR_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5),
1205 SENSOR_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6),
1206 SENSOR_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7),
1207 SENSOR_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8),
1208};
1209
1210static struct sensor_device_attribute sda_temp_label[] = {
1211 SENSOR_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0),
1212 SENSOR_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1),
1213 SENSOR_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2),
1214 SENSOR_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3),
1215 SENSOR_ATTR(temp5_label, S_IRUGO, show_temp_label, NULL, 4),
1216 SENSOR_ATTR(temp6_label, S_IRUGO, show_temp_label, NULL, 5),
1217 SENSOR_ATTR(temp7_label, S_IRUGO, show_temp_label, NULL, 6),
1218 SENSOR_ATTR(temp8_label, S_IRUGO, show_temp_label, NULL, 7),
1219 SENSOR_ATTR(temp9_label, S_IRUGO, show_temp_label, NULL, 8),
928}; 1220};
929 1221
930static struct sensor_device_attribute sda_temp_max[] = { 1222static struct sensor_device_attribute sda_temp_max[] = {
931 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp1_max, 1223 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
932 store_temp1_max, 0),
933 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
934 store_temp_max, 0), 1224 store_temp_max, 0),
935 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max, 1225 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
936 store_temp_max, 1), 1226 store_temp_max, 1),
1227 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
1228 store_temp_max, 2),
1229 SENSOR_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
1230 store_temp_max, 3),
1231 SENSOR_ATTR(temp5_max, S_IRUGO | S_IWUSR, show_temp_max,
1232 store_temp_max, 4),
1233 SENSOR_ATTR(temp6_max, S_IRUGO | S_IWUSR, show_temp_max,
1234 store_temp_max, 5),
1235 SENSOR_ATTR(temp7_max, S_IRUGO | S_IWUSR, show_temp_max,
1236 store_temp_max, 6),
1237 SENSOR_ATTR(temp8_max, S_IRUGO | S_IWUSR, show_temp_max,
1238 store_temp_max, 7),
1239 SENSOR_ATTR(temp9_max, S_IRUGO | S_IWUSR, show_temp_max,
1240 store_temp_max, 8),
937}; 1241};
938 1242
939static struct sensor_device_attribute sda_temp_max_hyst[] = { 1243static struct sensor_device_attribute sda_temp_max_hyst[] = {
940 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp1_max_hyst, 1244 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
941 store_temp1_max_hyst, 0),
942 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
943 store_temp_max_hyst, 0), 1245 store_temp_max_hyst, 0),
944 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst, 1246 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
945 store_temp_max_hyst, 1), 1247 store_temp_max_hyst, 1),
1248 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1249 store_temp_max_hyst, 2),
1250 SENSOR_ATTR(temp4_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1251 store_temp_max_hyst, 3),
1252 SENSOR_ATTR(temp5_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1253 store_temp_max_hyst, 4),
1254 SENSOR_ATTR(temp6_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1255 store_temp_max_hyst, 5),
1256 SENSOR_ATTR(temp7_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1257 store_temp_max_hyst, 6),
1258 SENSOR_ATTR(temp8_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1259 store_temp_max_hyst, 7),
1260 SENSOR_ATTR(temp9_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
1261 store_temp_max_hyst, 8),
946}; 1262};
947 1263
948static struct sensor_device_attribute sda_temp_alarm[] = { 1264static struct sensor_device_attribute sda_temp_alarm[] = {
@@ -958,11 +1274,12 @@ static struct sensor_device_attribute sda_temp_type[] = {
958}; 1274};
959 1275
960#define show_pwm_reg(reg) \ 1276#define show_pwm_reg(reg) \
961static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \ 1277static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
962 char *buf) \ 1278 char *buf) \
963{ \ 1279{ \
964 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 1280 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
965 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1281 struct sensor_device_attribute *sensor_attr = \
1282 to_sensor_dev_attr(attr); \
966 int nr = sensor_attr->index; \ 1283 int nr = sensor_attr->index; \
967 return sprintf(buf, "%d\n", data->reg[nr]); \ 1284 return sprintf(buf, "%d\n", data->reg[nr]); \
968} 1285}
@@ -978,9 +1295,14 @@ store_pwm_mode(struct device *dev, struct device_attribute *attr,
978 struct w83627ehf_data *data = dev_get_drvdata(dev); 1295 struct w83627ehf_data *data = dev_get_drvdata(dev);
979 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1296 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
980 int nr = sensor_attr->index; 1297 int nr = sensor_attr->index;
981 u32 val = simple_strtoul(buf, NULL, 10); 1298 unsigned long val;
1299 int err;
982 u16 reg; 1300 u16 reg;
983 1301
1302 err = strict_strtoul(buf, 10, &val);
1303 if (err < 0)
1304 return err;
1305
984 if (val > 1) 1306 if (val > 1)
985 return -EINVAL; 1307 return -EINVAL;
986 mutex_lock(&data->update_lock); 1308 mutex_lock(&data->update_lock);
@@ -1001,11 +1323,18 @@ store_pwm(struct device *dev, struct device_attribute *attr,
1001 struct w83627ehf_data *data = dev_get_drvdata(dev); 1323 struct w83627ehf_data *data = dev_get_drvdata(dev);
1002 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1324 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1003 int nr = sensor_attr->index; 1325 int nr = sensor_attr->index;
1004 u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 255); 1326 unsigned long val;
1327 int err;
1328
1329 err = strict_strtoul(buf, 10, &val);
1330 if (err < 0)
1331 return err;
1332
1333 val = SENSORS_LIMIT(val, 0, 255);
1005 1334
1006 mutex_lock(&data->update_lock); 1335 mutex_lock(&data->update_lock);
1007 data->pwm[nr] = val; 1336 data->pwm[nr] = val;
1008 w83627ehf_write_value(data, W83627EHF_REG_PWM[nr], val); 1337 w83627ehf_write_value(data, data->REG_PWM[nr], val);
1009 mutex_unlock(&data->update_lock); 1338 mutex_unlock(&data->update_lock);
1010 return count; 1339 return count;
1011} 1340}
@@ -1015,19 +1344,38 @@ store_pwm_enable(struct device *dev, struct device_attribute *attr,
1015 const char *buf, size_t count) 1344 const char *buf, size_t count)
1016{ 1345{
1017 struct w83627ehf_data *data = dev_get_drvdata(dev); 1346 struct w83627ehf_data *data = dev_get_drvdata(dev);
1347 struct w83627ehf_sio_data *sio_data = dev->platform_data;
1018 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1019 int nr = sensor_attr->index; 1349 int nr = sensor_attr->index;
1020 u32 val = simple_strtoul(buf, NULL, 10); 1350 unsigned long val;
1351 int err;
1021 u16 reg; 1352 u16 reg;
1022 1353
1023 if (!val || (val > 4)) 1354 err = strict_strtoul(buf, 10, &val);
1355 if (err < 0)
1356 return err;
1357
1358 if (!val || (val > 4 && val != data->pwm_enable_orig[nr]))
1024 return -EINVAL; 1359 return -EINVAL;
1360 /* SmartFan III mode is not supported on NCT6776F */
1361 if (sio_data->kind == nct6776 && val == 4)
1362 return -EINVAL;
1363
1025 mutex_lock(&data->update_lock); 1364 mutex_lock(&data->update_lock);
1026 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
1027 data->pwm_enable[nr] = val; 1365 data->pwm_enable[nr] = val;
1028 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]); 1366 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1029 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr]; 1367 reg = w83627ehf_read_value(data,
1030 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg); 1368 NCT6775_REG_FAN_MODE[nr]);
1369 reg &= 0x0f;
1370 reg |= (val - 1) << 4;
1371 w83627ehf_write_value(data,
1372 NCT6775_REG_FAN_MODE[nr], reg);
1373 } else {
1374 reg = w83627ehf_read_value(data, W83627EHF_REG_PWM_ENABLE[nr]);
1375 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
1376 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
1377 w83627ehf_write_value(data, W83627EHF_REG_PWM_ENABLE[nr], reg);
1378 }
1031 mutex_unlock(&data->update_lock); 1379 mutex_unlock(&data->update_lock);
1032 return count; 1380 return count;
1033} 1381}
@@ -1038,9 +1386,10 @@ static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1038 char *buf) \ 1386 char *buf) \
1039{ \ 1387{ \
1040 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 1388 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1041 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1389 struct sensor_device_attribute *sensor_attr = \
1390 to_sensor_dev_attr(attr); \
1042 int nr = sensor_attr->index; \ 1391 int nr = sensor_attr->index; \
1043 return sprintf(buf, "%d\n", temp1_from_reg(data->reg[nr])); \ 1392 return sprintf(buf, "%d\n", data->reg[nr] * 1000); \
1044} 1393}
1045 1394
1046show_tol_temp(tolerance) 1395show_tol_temp(tolerance)
@@ -1053,11 +1402,18 @@ store_target_temp(struct device *dev, struct device_attribute *attr,
1053 struct w83627ehf_data *data = dev_get_drvdata(dev); 1402 struct w83627ehf_data *data = dev_get_drvdata(dev);
1054 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1403 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1055 int nr = sensor_attr->index; 1404 int nr = sensor_attr->index;
1056 u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 127000); 1405 long val;
1406 int err;
1407
1408 err = strict_strtol(buf, 10, &val);
1409 if (err < 0)
1410 return err;
1411
1412 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
1057 1413
1058 mutex_lock(&data->update_lock); 1414 mutex_lock(&data->update_lock);
1059 data->target_temp[nr] = val; 1415 data->target_temp[nr] = val;
1060 w83627ehf_write_value(data, W83627EHF_REG_TARGET[nr], val); 1416 w83627ehf_write_value(data, data->REG_TARGET[nr], val);
1061 mutex_unlock(&data->update_lock); 1417 mutex_unlock(&data->update_lock);
1062 return count; 1418 return count;
1063} 1419}
@@ -1067,20 +1423,37 @@ store_tolerance(struct device *dev, struct device_attribute *attr,
1067 const char *buf, size_t count) 1423 const char *buf, size_t count)
1068{ 1424{
1069 struct w83627ehf_data *data = dev_get_drvdata(dev); 1425 struct w83627ehf_data *data = dev_get_drvdata(dev);
1426 struct w83627ehf_sio_data *sio_data = dev->platform_data;
1070 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); 1427 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1071 int nr = sensor_attr->index; 1428 int nr = sensor_attr->index;
1072 u16 reg; 1429 u16 reg;
1430 long val;
1431 int err;
1432
1433 err = strict_strtol(buf, 10, &val);
1434 if (err < 0)
1435 return err;
1436
1073 /* Limit the temp to 0C - 15C */ 1437 /* Limit the temp to 0C - 15C */
1074 u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 15000); 1438 val = SENSORS_LIMIT(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
1075 1439
1076 mutex_lock(&data->update_lock); 1440 mutex_lock(&data->update_lock);
1077 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]); 1441 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1078 data->tolerance[nr] = val; 1442 /* Limit tolerance further for NCT6776F */
1079 if (nr == 1) 1443 if (sio_data->kind == nct6776 && val > 7)
1080 reg = (reg & 0x0f) | (val << 4); 1444 val = 7;
1081 else 1445 reg = w83627ehf_read_value(data, NCT6775_REG_FAN_MODE[nr]);
1082 reg = (reg & 0xf0) | val; 1446 reg = (reg & 0xf0) | val;
1083 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg); 1447 w83627ehf_write_value(data, NCT6775_REG_FAN_MODE[nr], reg);
1448 } else {
1449 reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
1450 if (nr == 1)
1451 reg = (reg & 0x0f) | (val << 4);
1452 else
1453 reg = (reg & 0xf0) | val;
1454 w83627ehf_write_value(data, W83627EHF_REG_TOLERANCE[nr], reg);
1455 }
1456 data->tolerance[nr] = val;
1084 mutex_unlock(&data->update_lock); 1457 mutex_unlock(&data->update_lock);
1085 return count; 1458 return count;
1086} 1459}
@@ -1143,18 +1516,25 @@ static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1143 char *buf) \ 1516 char *buf) \
1144{ \ 1517{ \
1145 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 1518 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1146 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1519 struct sensor_device_attribute *sensor_attr = \
1520 to_sensor_dev_attr(attr); \
1147 int nr = sensor_attr->index; \ 1521 int nr = sensor_attr->index; \
1148 return sprintf(buf, "%d\n", data->reg[nr]); \ 1522 return sprintf(buf, "%d\n", data->reg[nr]); \
1149}\ 1523} \
1150static ssize_t \ 1524static ssize_t \
1151store_##reg(struct device *dev, struct device_attribute *attr, \ 1525store_##reg(struct device *dev, struct device_attribute *attr, \
1152 const char *buf, size_t count) \ 1526 const char *buf, size_t count) \
1153{\ 1527{ \
1154 struct w83627ehf_data *data = dev_get_drvdata(dev); \ 1528 struct w83627ehf_data *data = dev_get_drvdata(dev); \
1155 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1529 struct sensor_device_attribute *sensor_attr = \
1530 to_sensor_dev_attr(attr); \
1156 int nr = sensor_attr->index; \ 1531 int nr = sensor_attr->index; \
1157 u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 1, 255); \ 1532 unsigned long val; \
1533 int err; \
1534 err = strict_strtoul(buf, 10, &val); \
1535 if (err < 0) \
1536 return err; \
1537 val = SENSORS_LIMIT(val, 1, 255); \
1158 mutex_lock(&data->update_lock); \ 1538 mutex_lock(&data->update_lock); \
1159 data->reg[nr] = val; \ 1539 data->reg[nr] = val; \
1160 w83627ehf_write_value(data, data->REG_##REG[nr], val); \ 1540 w83627ehf_write_value(data, data->REG_##REG[nr], val); \
@@ -1172,10 +1552,12 @@ static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1172 char *buf) \ 1552 char *buf) \
1173{ \ 1553{ \
1174 struct w83627ehf_data *data = w83627ehf_update_device(dev); \ 1554 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1175 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1555 struct sensor_device_attribute *sensor_attr = \
1556 to_sensor_dev_attr(attr); \
1176 int nr = sensor_attr->index; \ 1557 int nr = sensor_attr->index; \
1177 return sprintf(buf, "%d\n", \ 1558 return sprintf(buf, "%d\n", \
1178 step_time_from_reg(data->reg[nr], data->pwm_mode[nr])); \ 1559 step_time_from_reg(data->reg[nr], \
1560 data->pwm_mode[nr])); \
1179} \ 1561} \
1180\ 1562\
1181static ssize_t \ 1563static ssize_t \
@@ -1183,10 +1565,15 @@ store_##reg(struct device *dev, struct device_attribute *attr, \
1183 const char *buf, size_t count) \ 1565 const char *buf, size_t count) \
1184{ \ 1566{ \
1185 struct w83627ehf_data *data = dev_get_drvdata(dev); \ 1567 struct w83627ehf_data *data = dev_get_drvdata(dev); \
1186 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \ 1568 struct sensor_device_attribute *sensor_attr = \
1569 to_sensor_dev_attr(attr); \
1187 int nr = sensor_attr->index; \ 1570 int nr = sensor_attr->index; \
1188 u8 val = step_time_to_reg(simple_strtoul(buf, NULL, 10), \ 1571 unsigned long val; \
1189 data->pwm_mode[nr]); \ 1572 int err; \
1573 err = strict_strtoul(buf, 10, &val); \
1574 if (err < 0) \
1575 return err; \
1576 val = step_time_to_reg(val, data->pwm_mode[nr]); \
1190 mutex_lock(&data->update_lock); \ 1577 mutex_lock(&data->update_lock); \
1191 data->reg[nr] = val; \ 1578 data->reg[nr] = val; \
1192 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \ 1579 w83627ehf_write_value(data, W83627EHF_REG_##REG[nr], val); \
@@ -1283,7 +1670,8 @@ static void w83627ehf_device_remove_files(struct device *dev)
1283 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) { 1670 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1284 struct sensor_device_attribute *attr = 1671 struct sensor_device_attribute *attr =
1285 &sda_sf3_max_step_arrays[i]; 1672 &sda_sf3_max_step_arrays[i];
1286 if (data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) 1673 if (data->REG_FAN_STEP_OUTPUT &&
1674 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff)
1287 device_remove_file(dev, &attr->dev_attr); 1675 device_remove_file(dev, &attr->dev_attr);
1288 } 1676 }
1289 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) 1677 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
@@ -1309,12 +1697,15 @@ static void w83627ehf_device_remove_files(struct device *dev)
1309 device_remove_file(dev, &sda_target_temp[i].dev_attr); 1697 device_remove_file(dev, &sda_target_temp[i].dev_attr);
1310 device_remove_file(dev, &sda_tolerance[i].dev_attr); 1698 device_remove_file(dev, &sda_tolerance[i].dev_attr);
1311 } 1699 }
1312 for (i = 0; i < 3; i++) { 1700 for (i = 0; i < NUM_REG_TEMP; i++) {
1313 if ((i == 2) && data->temp3_disable) 1701 if (!(data->have_temp & (1 << i)))
1314 continue; 1702 continue;
1315 device_remove_file(dev, &sda_temp_input[i].dev_attr); 1703 device_remove_file(dev, &sda_temp_input[i].dev_attr);
1704 device_remove_file(dev, &sda_temp_label[i].dev_attr);
1316 device_remove_file(dev, &sda_temp_max[i].dev_attr); 1705 device_remove_file(dev, &sda_temp_max[i].dev_attr);
1317 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr); 1706 device_remove_file(dev, &sda_temp_max_hyst[i].dev_attr);
1707 if (i > 2)
1708 continue;
1318 device_remove_file(dev, &sda_temp_alarm[i].dev_attr); 1709 device_remove_file(dev, &sda_temp_alarm[i].dev_attr);
1319 device_remove_file(dev, &sda_temp_type[i].dev_attr); 1710 device_remove_file(dev, &sda_temp_type[i].dev_attr);
1320 } 1711 }
@@ -1335,15 +1726,17 @@ static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data)
1335 w83627ehf_write_value(data, W83627EHF_REG_CONFIG, 1726 w83627ehf_write_value(data, W83627EHF_REG_CONFIG,
1336 tmp | 0x01); 1727 tmp | 0x01);
1337 1728
1338 /* Enable temp2 and temp3 if needed */ 1729 /* Enable temperature sensors if needed */
1339 for (i = 0; i < 2; i++) { 1730 for (i = 0; i < NUM_REG_TEMP; i++) {
1340 tmp = w83627ehf_read_value(data, 1731 if (!(data->have_temp & (1 << i)))
1341 W83627EHF_REG_TEMP_CONFIG[i]); 1732 continue;
1342 if ((i == 1) && data->temp3_disable) 1733 if (!data->reg_temp_config[i])
1343 continue; 1734 continue;
1735 tmp = w83627ehf_read_value(data,
1736 data->reg_temp_config[i]);
1344 if (tmp & 0x01) 1737 if (tmp & 0x01)
1345 w83627ehf_write_value(data, 1738 w83627ehf_write_value(data,
1346 W83627EHF_REG_TEMP_CONFIG[i], 1739 data->reg_temp_config[i],
1347 tmp & 0xfe); 1740 tmp & 0xfe);
1348 } 1741 }
1349 1742
@@ -1362,13 +1755,39 @@ static inline void __devinit w83627ehf_init_device(struct w83627ehf_data *data)
1362 } 1755 }
1363} 1756}
1364 1757
1758static void w82627ehf_swap_tempreg(struct w83627ehf_data *data,
1759 int r1, int r2)
1760{
1761 u16 tmp;
1762
1763 tmp = data->temp_src[r1];
1764 data->temp_src[r1] = data->temp_src[r2];
1765 data->temp_src[r2] = tmp;
1766
1767 tmp = data->reg_temp[r1];
1768 data->reg_temp[r1] = data->reg_temp[r2];
1769 data->reg_temp[r2] = tmp;
1770
1771 tmp = data->reg_temp_over[r1];
1772 data->reg_temp_over[r1] = data->reg_temp_over[r2];
1773 data->reg_temp_over[r2] = tmp;
1774
1775 tmp = data->reg_temp_hyst[r1];
1776 data->reg_temp_hyst[r1] = data->reg_temp_hyst[r2];
1777 data->reg_temp_hyst[r2] = tmp;
1778
1779 tmp = data->reg_temp_config[r1];
1780 data->reg_temp_config[r1] = data->reg_temp_config[r2];
1781 data->reg_temp_config[r2] = tmp;
1782}
1783
1365static int __devinit w83627ehf_probe(struct platform_device *pdev) 1784static int __devinit w83627ehf_probe(struct platform_device *pdev)
1366{ 1785{
1367 struct device *dev = &pdev->dev; 1786 struct device *dev = &pdev->dev;
1368 struct w83627ehf_sio_data *sio_data = dev->platform_data; 1787 struct w83627ehf_sio_data *sio_data = dev->platform_data;
1369 struct w83627ehf_data *data; 1788 struct w83627ehf_data *data;
1370 struct resource *res; 1789 struct resource *res;
1371 u8 fan4pin, fan5pin, en_vrm10; 1790 u8 fan3pin, fan4pin, fan4min, fan5pin, en_vrm10;
1372 int i, err = 0; 1791 int i, err = 0;
1373 1792
1374 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 1793 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
@@ -1380,7 +1799,8 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1380 goto exit; 1799 goto exit;
1381 } 1800 }
1382 1801
1383 if (!(data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) { 1802 data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL);
1803 if (!data) {
1384 err = -ENOMEM; 1804 err = -ENOMEM;
1385 goto exit_release; 1805 goto exit_release;
1386 } 1806 }
@@ -1393,25 +1813,202 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1393 1813
1394 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */ 1814 /* 627EHG and 627EHF have 10 voltage inputs; 627DHG and 667HG have 9 */
1395 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9; 1815 data->in_num = (sio_data->kind == w83627ehf) ? 10 : 9;
1396 /* 667HG has 3 pwms */ 1816 /* 667HG, NCT6775F, and NCT6776F have 3 pwms */
1397 data->pwm_num = (sio_data->kind == w83667hg 1817 data->pwm_num = (sio_data->kind == w83667hg
1398 || sio_data->kind == w83667hg_b) ? 3 : 4; 1818 || sio_data->kind == w83667hg_b
1819 || sio_data->kind == nct6775
1820 || sio_data->kind == nct6776) ? 3 : 4;
1399 1821
1822 data->have_temp = 0x07;
1400 /* Check temp3 configuration bit for 667HG */ 1823 /* Check temp3 configuration bit for 667HG */
1401 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) { 1824 if (sio_data->kind == w83667hg) {
1402 data->temp3_disable = w83627ehf_read_value(data, 1825 u8 reg;
1403 W83627EHF_REG_TEMP_CONFIG[1]) & 0x01; 1826
1404 data->in6_skip = !data->temp3_disable; 1827 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1828 if (reg & 0x01)
1829 data->have_temp &= ~(1 << 2);
1830 else
1831 data->in6_skip = 1; /* either temp3 or in6 */
1832 }
1833
1834 /* Deal with temperature register setup first. */
1835 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1836 int mask = 0;
1837
1838 /*
1839 * Display temperature sensor output only if it monitors
1840 * a source other than one already reported. Always display
1841 * first three temperature registers, though.
1842 */
1843 for (i = 0; i < NUM_REG_TEMP; i++) {
1844 u8 src;
1845
1846 data->reg_temp[i] = NCT6775_REG_TEMP[i];
1847 data->reg_temp_over[i] = NCT6775_REG_TEMP_OVER[i];
1848 data->reg_temp_hyst[i] = NCT6775_REG_TEMP_HYST[i];
1849 data->reg_temp_config[i] = NCT6775_REG_TEMP_CONFIG[i];
1850
1851 src = w83627ehf_read_value(data,
1852 NCT6775_REG_TEMP_SOURCE[i]);
1853 src &= 0x1f;
1854 if (src && !(mask & (1 << src))) {
1855 data->have_temp |= 1 << i;
1856 mask |= 1 << src;
1857 }
1858
1859 data->temp_src[i] = src;
1860
1861 /*
1862 * Now do some register swapping if index 0..2 don't
1863 * point to SYSTIN(1), CPUIN(2), and AUXIN(3).
1864 * Idea is to have the first three attributes
1865 * report SYSTIN, CPUIN, and AUXIN if possible
1866 * without overriding the basic system configuration.
1867 */
1868 if (i > 0 && data->temp_src[0] != 1
1869 && data->temp_src[i] == 1)
1870 w82627ehf_swap_tempreg(data, 0, i);
1871 if (i > 1 && data->temp_src[1] != 2
1872 && data->temp_src[i] == 2)
1873 w82627ehf_swap_tempreg(data, 1, i);
1874 if (i > 2 && data->temp_src[2] != 3
1875 && data->temp_src[i] == 3)
1876 w82627ehf_swap_tempreg(data, 2, i);
1877 }
1878 if (sio_data->kind == nct6776) {
1879 /*
1880 * On NCT6776, AUXTIN and VIN3 pins are shared.
1881 * Only way to detect it is to check if AUXTIN is used
1882 * as a temperature source, and if that source is
1883 * enabled.
1884 *
1885 * If that is the case, disable in6, which reports VIN3.
1886 * Otherwise disable temp3.
1887 */
1888 if (data->temp_src[2] == 3) {
1889 u8 reg;
1890
1891 if (data->reg_temp_config[2])
1892 reg = w83627ehf_read_value(data,
1893 data->reg_temp_config[2]);
1894 else
1895 reg = 0; /* Assume AUXTIN is used */
1896
1897 if (reg & 0x01)
1898 data->have_temp &= ~(1 << 2);
1899 else
1900 data->in6_skip = 1;
1901 }
1902 data->temp_label = nct6776_temp_label;
1903 } else {
1904 data->temp_label = nct6775_temp_label;
1905 }
1906 } else if (sio_data->kind == w83667hg_b) {
1907 u8 reg;
1908
1909 /*
1910 * Temperature sources are selected with bank 0, registers 0x49
1911 * and 0x4a.
1912 */
1913 for (i = 0; i < ARRAY_SIZE(W83627EHF_REG_TEMP); i++) {
1914 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1915 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1916 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1917 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1918 }
1919 reg = w83627ehf_read_value(data, 0x4a);
1920 data->temp_src[0] = reg >> 5;
1921 reg = w83627ehf_read_value(data, 0x49);
1922 data->temp_src[1] = reg & 0x07;
1923 data->temp_src[2] = (reg >> 4) & 0x07;
1924
1925 /*
1926 * W83667HG-B has another temperature register at 0x7e.
1927 * The temperature source is selected with register 0x7d.
1928 * Support it if the source differs from already reported
1929 * sources.
1930 */
1931 reg = w83627ehf_read_value(data, 0x7d);
1932 reg &= 0x07;
1933 if (reg != data->temp_src[0] && reg != data->temp_src[1]
1934 && reg != data->temp_src[2]) {
1935 data->temp_src[3] = reg;
1936 data->have_temp |= 1 << 3;
1937 }
1938
1939 /*
1940 * Chip supports either AUXTIN or VIN3. Try to find out which
1941 * one.
1942 */
1943 reg = w83627ehf_read_value(data, W83627EHF_REG_TEMP_CONFIG[2]);
1944 if (data->temp_src[2] == 2 && (reg & 0x01))
1945 data->have_temp &= ~(1 << 2);
1946
1947 if ((data->temp_src[2] == 2 && (data->have_temp & (1 << 2)))
1948 || (data->temp_src[3] == 2 && (data->have_temp & (1 << 3))))
1949 data->in6_skip = 1;
1950
1951 data->temp_label = w83667hg_b_temp_label;
1952 } else {
1953 /* Temperature sources are fixed */
1954 for (i = 0; i < 3; i++) {
1955 data->reg_temp[i] = W83627EHF_REG_TEMP[i];
1956 data->reg_temp_over[i] = W83627EHF_REG_TEMP_OVER[i];
1957 data->reg_temp_hyst[i] = W83627EHF_REG_TEMP_HYST[i];
1958 data->reg_temp_config[i] = W83627EHF_REG_TEMP_CONFIG[i];
1959 }
1405 } 1960 }
1406 1961
1407 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT; 1962 if (sio_data->kind == nct6775) {
1408 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT; 1963 data->has_fan_div = true;
1409 if (sio_data->kind == w83667hg_b) { 1964 data->fan_from_reg = fan_from_reg16;
1965 data->fan_from_reg_min = fan_from_reg8;
1966 data->REG_PWM = NCT6775_REG_PWM;
1967 data->REG_TARGET = NCT6775_REG_TARGET;
1968 data->REG_FAN = NCT6775_REG_FAN;
1969 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
1970 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1971 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1972 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1973 data->REG_FAN_MAX_OUTPUT = NCT6775_REG_FAN_MAX_OUTPUT;
1974 data->REG_FAN_STEP_OUTPUT = NCT6775_REG_FAN_STEP_OUTPUT;
1975 } else if (sio_data->kind == nct6776) {
1976 data->has_fan_div = false;
1977 data->fan_from_reg = fan_from_reg13;
1978 data->fan_from_reg_min = fan_from_reg13;
1979 data->REG_PWM = NCT6775_REG_PWM;
1980 data->REG_TARGET = NCT6775_REG_TARGET;
1981 data->REG_FAN = NCT6775_REG_FAN;
1982 data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
1983 data->REG_FAN_START_OUTPUT = NCT6775_REG_FAN_START_OUTPUT;
1984 data->REG_FAN_STOP_OUTPUT = NCT6775_REG_FAN_STOP_OUTPUT;
1985 data->REG_FAN_STOP_TIME = NCT6775_REG_FAN_STOP_TIME;
1986 } else if (sio_data->kind == w83667hg_b) {
1987 data->has_fan_div = true;
1988 data->fan_from_reg = fan_from_reg8;
1989 data->fan_from_reg_min = fan_from_reg8;
1990 data->REG_PWM = W83627EHF_REG_PWM;
1991 data->REG_TARGET = W83627EHF_REG_TARGET;
1992 data->REG_FAN = W83627EHF_REG_FAN;
1993 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
1994 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
1995 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
1996 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
1410 data->REG_FAN_MAX_OUTPUT = 1997 data->REG_FAN_MAX_OUTPUT =
1411 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B; 1998 W83627EHF_REG_FAN_MAX_OUTPUT_W83667_B;
1412 data->REG_FAN_STEP_OUTPUT = 1999 data->REG_FAN_STEP_OUTPUT =
1413 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B; 2000 W83627EHF_REG_FAN_STEP_OUTPUT_W83667_B;
1414 } else { 2001 } else {
2002 data->has_fan_div = true;
2003 data->fan_from_reg = fan_from_reg8;
2004 data->fan_from_reg_min = fan_from_reg8;
2005 data->REG_PWM = W83627EHF_REG_PWM;
2006 data->REG_TARGET = W83627EHF_REG_TARGET;
2007 data->REG_FAN = W83627EHF_REG_FAN;
2008 data->REG_FAN_MIN = W83627EHF_REG_FAN_MIN;
2009 data->REG_FAN_START_OUTPUT = W83627EHF_REG_FAN_START_OUTPUT;
2010 data->REG_FAN_STOP_OUTPUT = W83627EHF_REG_FAN_STOP_OUTPUT;
2011 data->REG_FAN_STOP_TIME = W83627EHF_REG_FAN_STOP_TIME;
1415 data->REG_FAN_MAX_OUTPUT = 2012 data->REG_FAN_MAX_OUTPUT =
1416 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON; 2013 W83627EHF_REG_FAN_MAX_OUTPUT_COMMON;
1417 data->REG_FAN_STEP_OUTPUT = 2014 data->REG_FAN_STEP_OUTPUT =
@@ -1424,7 +2021,8 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1424 data->vrm = vid_which_vrm(); 2021 data->vrm = vid_which_vrm();
1425 superio_enter(sio_data->sioreg); 2022 superio_enter(sio_data->sioreg);
1426 /* Read VID value */ 2023 /* Read VID value */
1427 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) { 2024 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b ||
2025 sio_data->kind == nct6775 || sio_data->kind == nct6776) {
1428 /* W83667HG has different pins for VID input and output, so 2026 /* W83667HG has different pins for VID input and output, so
1429 we can get the VID input values directly at logical device D 2027 we can get the VID input values directly at logical device D
1430 0xe3. */ 2028 0xe3. */
@@ -1475,13 +2073,44 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1475 } 2073 }
1476 2074
1477 /* fan4 and fan5 share some pins with the GPIO and serial flash */ 2075 /* fan4 and fan5 share some pins with the GPIO and serial flash */
1478 if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) { 2076 if (sio_data->kind == nct6775) {
1479 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20; 2077 /* On NCT6775, fan4 shares pins with the fdc interface */
2078 fan3pin = 1;
2079 fan4pin = !(superio_inb(sio_data->sioreg, 0x2A) & 0x80);
2080 fan4min = 0;
2081 fan5pin = 0;
2082 } else if (sio_data->kind == nct6776) {
2083 fan3pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x40);
2084 fan4pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x01);
2085 fan5pin = !!(superio_inb(sio_data->sioreg, 0x1C) & 0x02);
2086 fan4min = fan4pin;
2087 } else if (sio_data->kind == w83667hg || sio_data->kind == w83667hg_b) {
2088 fan3pin = 1;
1480 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40; 2089 fan4pin = superio_inb(sio_data->sioreg, 0x27) & 0x40;
2090 fan5pin = superio_inb(sio_data->sioreg, 0x27) & 0x20;
2091 fan4min = fan4pin;
1481 } else { 2092 } else {
1482 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02); 2093 fan3pin = 1;
1483 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06); 2094 fan4pin = !(superio_inb(sio_data->sioreg, 0x29) & 0x06);
2095 fan5pin = !(superio_inb(sio_data->sioreg, 0x24) & 0x02);
2096 fan4min = fan4pin;
1484 } 2097 }
2098
2099 if (fan_debounce &&
2100 (sio_data->kind == nct6775 || sio_data->kind == nct6776)) {
2101 u8 tmp;
2102
2103 superio_select(sio_data->sioreg, W83627EHF_LD_HWM);
2104 tmp = superio_inb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE);
2105 if (sio_data->kind == nct6776)
2106 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2107 0x3e | tmp);
2108 else
2109 superio_outb(sio_data->sioreg, NCT6775_REG_FAN_DEBOUNCE,
2110 0x1e | tmp);
2111 pr_info("Enabled fan debounce for chip %s\n", data->name);
2112 }
2113
1485 superio_exit(sio_data->sioreg); 2114 superio_exit(sio_data->sioreg);
1486 2115
1487 /* It looks like fan4 and fan5 pins can be alternatively used 2116 /* It looks like fan4 and fan5 pins can be alternatively used
@@ -1490,26 +2119,54 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1490 connected fan5 as input unless they are emitting log 1, which 2119 connected fan5 as input unless they are emitting log 1, which
1491 is not the default. */ 2120 is not the default. */
1492 2121
1493 data->has_fan = 0x07; /* fan1, fan2 and fan3 */ 2122 data->has_fan = data->has_fan_min = 0x03; /* fan1 and fan2 */
1494 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1); 2123
1495 if ((i & (1 << 2)) && fan4pin) 2124 data->has_fan |= (fan3pin << 2);
1496 data->has_fan |= (1 << 3); 2125 data->has_fan_min |= (fan3pin << 2);
1497 if (!(i & (1 << 1)) && fan5pin) 2126
1498 data->has_fan |= (1 << 4); 2127 /*
2128 * NCT6775F and NCT6776F don't have the W83627EHF_REG_FANDIV1 register
2129 */
2130 if (sio_data->kind == nct6775 || sio_data->kind == nct6776) {
2131 data->has_fan |= (fan4pin << 3) | (fan5pin << 4);
2132 data->has_fan_min |= (fan4min << 3) | (fan5pin << 4);
2133 } else {
2134 i = w83627ehf_read_value(data, W83627EHF_REG_FANDIV1);
2135 if ((i & (1 << 2)) && fan4pin) {
2136 data->has_fan |= (1 << 3);
2137 data->has_fan_min |= (1 << 3);
2138 }
2139 if (!(i & (1 << 1)) && fan5pin) {
2140 data->has_fan |= (1 << 4);
2141 data->has_fan_min |= (1 << 4);
2142 }
2143 }
1499 2144
1500 /* Read fan clock dividers immediately */ 2145 /* Read fan clock dividers immediately */
1501 w83627ehf_update_fan_div(data); 2146 w83627ehf_update_fan_div_common(dev, data);
2147
2148 /* Read pwm data to save original values */
2149 w83627ehf_update_pwm_common(dev, data);
2150 for (i = 0; i < data->pwm_num; i++)
2151 data->pwm_enable_orig[i] = data->pwm_enable[i];
2152
2153 /* Read pwm data to save original values */
2154 w83627ehf_update_pwm_common(dev, data);
2155 for (i = 0; i < data->pwm_num; i++)
2156 data->pwm_enable_orig[i] = data->pwm_enable[i];
1502 2157
1503 /* Register sysfs hooks */ 2158 /* Register sysfs hooks */
1504 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) 2159 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++) {
1505 if ((err = device_create_file(dev, 2160 err = device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
1506 &sda_sf3_arrays[i].dev_attr))) 2161 if (err)
1507 goto exit_remove; 2162 goto exit_remove;
2163 }
1508 2164
1509 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) { 2165 for (i = 0; i < ARRAY_SIZE(sda_sf3_max_step_arrays); i++) {
1510 struct sensor_device_attribute *attr = 2166 struct sensor_device_attribute *attr =
1511 &sda_sf3_max_step_arrays[i]; 2167 &sda_sf3_max_step_arrays[i];
1512 if (data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) { 2168 if (data->REG_FAN_STEP_OUTPUT &&
2169 data->REG_FAN_STEP_OUTPUT[attr->index] != 0xff) {
1513 err = device_create_file(dev, &attr->dev_attr); 2170 err = device_create_file(dev, &attr->dev_attr);
1514 if (err) 2171 if (err)
1515 goto exit_remove; 2172 goto exit_remove;
@@ -1518,8 +2175,9 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1518 /* if fan4 is enabled create the sf3 files for it */ 2175 /* if fan4 is enabled create the sf3 files for it */
1519 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4) 2176 if ((data->has_fan & (1 << 3)) && data->pwm_num >= 4)
1520 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) { 2177 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++) {
1521 if ((err = device_create_file(dev, 2178 err = device_create_file(dev,
1522 &sda_sf3_arrays_fan4[i].dev_attr))) 2179 &sda_sf3_arrays_fan4[i].dev_attr);
2180 if (err)
1523 goto exit_remove; 2181 goto exit_remove;
1524 } 2182 }
1525 2183
@@ -1541,12 +2199,20 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1541 if ((err = device_create_file(dev, 2199 if ((err = device_create_file(dev,
1542 &sda_fan_input[i].dev_attr)) 2200 &sda_fan_input[i].dev_attr))
1543 || (err = device_create_file(dev, 2201 || (err = device_create_file(dev,
1544 &sda_fan_alarm[i].dev_attr)) 2202 &sda_fan_alarm[i].dev_attr)))
1545 || (err = device_create_file(dev,
1546 &sda_fan_div[i].dev_attr))
1547 || (err = device_create_file(dev,
1548 &sda_fan_min[i].dev_attr)))
1549 goto exit_remove; 2203 goto exit_remove;
2204 if (sio_data->kind != nct6776) {
2205 err = device_create_file(dev,
2206 &sda_fan_div[i].dev_attr);
2207 if (err)
2208 goto exit_remove;
2209 }
2210 if (data->has_fan_min & (1 << i)) {
2211 err = device_create_file(dev,
2212 &sda_fan_min[i].dev_attr);
2213 if (err)
2214 goto exit_remove;
2215 }
1550 if (i < data->pwm_num && 2216 if (i < data->pwm_num &&
1551 ((err = device_create_file(dev, 2217 ((err = device_create_file(dev,
1552 &sda_pwm[i].dev_attr)) 2218 &sda_pwm[i].dev_attr))
@@ -1562,16 +2228,33 @@ static int __devinit w83627ehf_probe(struct platform_device *pdev)
1562 } 2228 }
1563 } 2229 }
1564 2230
1565 for (i = 0; i < 3; i++) { 2231 for (i = 0; i < NUM_REG_TEMP; i++) {
1566 if ((i == 2) && data->temp3_disable) 2232 if (!(data->have_temp & (1 << i)))
2233 continue;
2234 err = device_create_file(dev, &sda_temp_input[i].dev_attr);
2235 if (err)
2236 goto exit_remove;
2237 if (data->temp_label) {
2238 err = device_create_file(dev,
2239 &sda_temp_label[i].dev_attr);
2240 if (err)
2241 goto exit_remove;
2242 }
2243 if (data->reg_temp_over[i]) {
2244 err = device_create_file(dev,
2245 &sda_temp_max[i].dev_attr);
2246 if (err)
2247 goto exit_remove;
2248 }
2249 if (data->reg_temp_hyst[i]) {
2250 err = device_create_file(dev,
2251 &sda_temp_max_hyst[i].dev_attr);
2252 if (err)
2253 goto exit_remove;
2254 }
2255 if (i > 2)
1567 continue; 2256 continue;
1568 if ((err = device_create_file(dev, 2257 if ((err = device_create_file(dev,
1569 &sda_temp_input[i].dev_attr))
1570 || (err = device_create_file(dev,
1571 &sda_temp_max[i].dev_attr))
1572 || (err = device_create_file(dev,
1573 &sda_temp_max_hyst[i].dev_attr))
1574 || (err = device_create_file(dev,
1575 &sda_temp_alarm[i].dev_attr)) 2258 &sda_temp_alarm[i].dev_attr))
1576 || (err = device_create_file(dev, 2259 || (err = device_create_file(dev,
1577 &sda_temp_type[i].dev_attr))) 2260 &sda_temp_type[i].dev_attr)))
@@ -1632,6 +2315,8 @@ static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
1632 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P"; 2315 static const char __initdata sio_name_W83627DHG_P[] = "W83627DHG-P";
1633 static const char __initdata sio_name_W83667HG[] = "W83667HG"; 2316 static const char __initdata sio_name_W83667HG[] = "W83667HG";
1634 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B"; 2317 static const char __initdata sio_name_W83667HG_B[] = "W83667HG-B";
2318 static const char __initdata sio_name_NCT6775[] = "NCT6775F";
2319 static const char __initdata sio_name_NCT6776[] = "NCT6776F";
1635 2320
1636 u16 val; 2321 u16 val;
1637 const char *sio_name; 2322 const char *sio_name;
@@ -1668,6 +2353,14 @@ static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
1668 sio_data->kind = w83667hg_b; 2353 sio_data->kind = w83667hg_b;
1669 sio_name = sio_name_W83667HG_B; 2354 sio_name = sio_name_W83667HG_B;
1670 break; 2355 break;
2356 case SIO_NCT6775_ID:
2357 sio_data->kind = nct6775;
2358 sio_name = sio_name_NCT6775;
2359 break;
2360 case SIO_NCT6776_ID:
2361 sio_data->kind = nct6776;
2362 sio_name = sio_name_NCT6776;
2363 break;
1671 default: 2364 default:
1672 if (val != 0xffff) 2365 if (val != 0xffff)
1673 pr_debug("unsupported chip ID: 0x%04x\n", val); 2366 pr_debug("unsupported chip ID: 0x%04x\n", val);
@@ -1689,7 +2382,8 @@ static int __init w83627ehf_find(int sioaddr, unsigned short *addr,
1689 /* Activate logical device if needed */ 2382 /* Activate logical device if needed */
1690 val = superio_inb(sioaddr, SIO_REG_ENABLE); 2383 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1691 if (!(val & 0x01)) { 2384 if (!(val & 0x01)) {
1692 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n"); 2385 pr_warn("Forcibly enabling Super-I/O. "
2386 "Sensor is probably unusable.\n");
1693 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01); 2387 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1694 } 2388 }
1695 2389
@@ -1726,7 +2420,8 @@ static int __init sensors_w83627ehf_init(void)
1726 if (err) 2420 if (err)
1727 goto exit; 2421 goto exit;
1728 2422
1729 if (!(pdev = platform_device_alloc(DRVNAME, address))) { 2423 pdev = platform_device_alloc(DRVNAME, address);
2424 if (!pdev) {
1730 err = -ENOMEM; 2425 err = -ENOMEM;
1731 pr_err("Device allocation failed\n"); 2426 pr_err("Device allocation failed\n");
1732 goto exit_unregister; 2427 goto exit_unregister;