Commit d216f6809eb690b9a888c286cde68cda4d0c4cfa

Authored by Jean Delvare
Committed by Jean Delvare
1 parent d93ab78070

hwmon: (lm63) Expose automatic fan speed control lookup table

The LM63 and compatible devices have a lookup table to control the fan
speed automatically. Expose it in sysfs. Values are cached for 5
seconds, independently of the other register values to avoid slowing
down "sensors". We might make the table values writable in the future.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Tested-by: Guenter Roeck <guenter.roeck@ericsson.com>
Acked-by: Guenter Roeck <guenter.roeck@ericsson.com>

Showing 2 changed files with 136 additions and 15 deletions Inline Diff

Documentation/hwmon/lm63
1 Kernel driver lm63 1 Kernel driver lm63
2 ================== 2 ==================
3 3
4 Supported chips: 4 Supported chips:
5 * National Semiconductor LM63 5 * National Semiconductor LM63
6 Prefix: 'lm63' 6 Prefix: 'lm63'
7 Addresses scanned: I2C 0x4c 7 Addresses scanned: I2C 0x4c
8 Datasheet: Publicly available at the National Semiconductor website 8 Datasheet: Publicly available at the National Semiconductor website
9 http://www.national.com/pf/LM/LM63.html 9 http://www.national.com/pf/LM/LM63.html
10 * National Semiconductor LM64 10 * National Semiconductor LM64
11 Prefix: 'lm64' 11 Prefix: 'lm64'
12 Addresses scanned: I2C 0x18 and 0x4e 12 Addresses scanned: I2C 0x18 and 0x4e
13 Datasheet: Publicly available at the National Semiconductor website 13 Datasheet: Publicly available at the National Semiconductor website
14 http://www.national.com/pf/LM/LM64.html 14 http://www.national.com/pf/LM/LM64.html
15 * National Semiconductor LM96163 15 * National Semiconductor LM96163
16 Prefix: 'lm96163' 16 Prefix: 'lm96163'
17 Addresses scanned: I2C 0x4c 17 Addresses scanned: I2C 0x4c
18 Datasheet: Publicly available at the National Semiconductor website 18 Datasheet: Publicly available at the National Semiconductor website
19 http://www.national.com/pf/LM/LM96163.html 19 http://www.national.com/pf/LM/LM96163.html
20 20
21 Author: Jean Delvare <khali@linux-fr.org> 21 Author: Jean Delvare <khali@linux-fr.org>
22 22
23 Thanks go to Tyan and especially Alex Buckingham for setting up a remote 23 Thanks go to Tyan and especially Alex Buckingham for setting up a remote
24 access to their S4882 test platform for this driver. 24 access to their S4882 test platform for this driver.
25 http://www.tyan.com/ 25 http://www.tyan.com/
26 26
27 Description 27 Description
28 ----------- 28 -----------
29 29
30 The LM63 is a digital temperature sensor with integrated fan monitoring 30 The LM63 is a digital temperature sensor with integrated fan monitoring
31 and control. 31 and control.
32 32
33 The LM63 is basically an LM86 with fan speed monitoring and control 33 The LM63 is basically an LM86 with fan speed monitoring and control
34 capabilities added. It misses some of the LM86 features though: 34 capabilities added. It misses some of the LM86 features though:
35 - No low limit for local temperature. 35 - No low limit for local temperature.
36 - No critical limit for local temperature. 36 - No critical limit for local temperature.
37 - Critical limit for remote temperature can be changed only once. We 37 - Critical limit for remote temperature can be changed only once. We
38 will consider that the critical limit is read-only. 38 will consider that the critical limit is read-only.
39 39
40 The datasheet isn't very clear about what the tachometer reading is. 40 The datasheet isn't very clear about what the tachometer reading is.
41 41
42 An explanation from National Semiconductor: The two lower bits of the read 42 An explanation from National Semiconductor: The two lower bits of the read
43 value have to be masked out. The value is still 16 bit in width. 43 value have to be masked out. The value is still 16 bit in width.
44 44
45 All temperature values are given in degrees Celsius. Resolution is 1.0 45 All temperature values are given in degrees Celsius. Resolution is 1.0
46 degree for the local temperature, 0.125 degree for the remote temperature. 46 degree for the local temperature, 0.125 degree for the remote temperature.
47 47
48 The fan speed is measured using a tachometer. Contrary to most chips which 48 The fan speed is measured using a tachometer. Contrary to most chips which
49 store the value in an 8-bit register and have a selectable clock divider 49 store the value in an 8-bit register and have a selectable clock divider
50 to make sure that the result will fit in the register, the LM63 uses 16-bit 50 to make sure that the result will fit in the register, the LM63 uses 16-bit
51 value for measuring the speed of the fan. It can measure fan speeds down to 51 value for measuring the speed of the fan. It can measure fan speeds down to
52 83 RPM, at least in theory. 52 83 RPM, at least in theory.
53 53
54 Note that the pin used for fan monitoring is shared with an alert out 54 Note that the pin used for fan monitoring is shared with an alert out
55 function. Depending on how the board designer wanted to use the chip, fan 55 function. Depending on how the board designer wanted to use the chip, fan
56 speed monitoring will or will not be possible. The proper chip configuration 56 speed monitoring will or will not be possible. The proper chip configuration
57 is left to the BIOS, and the driver will blindly trust it. Only the original 57 is left to the BIOS, and the driver will blindly trust it. Only the original
58 LM63 suffers from this limitation, the LM64 and LM96163 have separate pins 58 LM63 suffers from this limitation, the LM64 and LM96163 have separate pins
59 for fan monitoring and alert out. On the LM64, monitoring is always enabled; 59 for fan monitoring and alert out. On the LM64, monitoring is always enabled;
60 on the LM96163 it can be disabled. 60 on the LM96163 it can be disabled.
61 61
62 A PWM output can be used to control the speed of the fan. The LM63 has two 62 A PWM output can be used to control the speed of the fan. The LM63 has two
63 PWM modes: manual and automatic. Automatic mode is not fully implemented yet 63 PWM modes: manual and automatic. Automatic mode is not fully implemented yet
64 (you cannot define your custom PWM/temperature curve), and mode change isn't 64 (you cannot define your custom PWM/temperature curve), and mode change isn't
65 supported either. 65 supported either.
66 66
67 The lm63 driver will not update its values more frequently than configured with 67 The lm63 driver will not update its values more frequently than configured with
68 the update_interval sysfs attribute; reading them more often will do no harm, 68 the update_interval sysfs attribute; reading them more often will do no harm,
69 but will return 'old' values. 69 but will return 'old' values. Values in the automatic fan control lookup table
70 (attributes pwm1_auto_*) have their own independent lifetime of 5 seconds.
70 71
71 The LM64 is effectively an LM63 with GPIO lines. The driver does not 72 The LM64 is effectively an LM63 with GPIO lines. The driver does not
72 support these GPIO lines at present. 73 support these GPIO lines at present.
73 74
74 The LM96163 is an enhanced version of LM63 with improved temperature accuracy 75 The LM96163 is an enhanced version of LM63 with improved temperature accuracy
75 and better PWM resolution. For LM96163, the external temperature sensor type is 76 and better PWM resolution. For LM96163, the external temperature sensor type is
76 configurable as CPU embedded diode(1) or 3904 transistor(2). 77 configurable as CPU embedded diode(1) or 3904 transistor(2).
77 78
drivers/hwmon/lm63.c
1 /* 1 /*
2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor 2 * lm63.c - driver for the National Semiconductor LM63 temperature sensor
3 * with integrated fan control 3 * with integrated fan control
4 * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org> 4 * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org>
5 * Based on the lm90 driver. 5 * Based on the lm90 driver.
6 * 6 *
7 * The LM63 is a sensor chip made by National Semiconductor. It measures 7 * The LM63 is a sensor chip made by National Semiconductor. It measures
8 * two temperatures (its own and one external one) and the speed of one 8 * two temperatures (its own and one external one) and the speed of one
9 * fan, those speed it can additionally control. Complete datasheet can be 9 * fan, those speed it can additionally control. Complete datasheet can be
10 * obtained from National's website at: 10 * obtained from National's website at:
11 * http://www.national.com/pf/LM/LM63.html 11 * http://www.national.com/pf/LM/LM63.html
12 * 12 *
13 * The LM63 is basically an LM86 with fan speed monitoring and control 13 * The LM63 is basically an LM86 with fan speed monitoring and control
14 * capabilities added. It misses some of the LM86 features though: 14 * capabilities added. It misses some of the LM86 features though:
15 * - No low limit for local temperature. 15 * - No low limit for local temperature.
16 * - No critical limit for local temperature. 16 * - No critical limit for local temperature.
17 * - Critical limit for remote temperature can be changed only once. We 17 * - Critical limit for remote temperature can be changed only once. We
18 * will consider that the critical limit is read-only. 18 * will consider that the critical limit is read-only.
19 * 19 *
20 * The datasheet isn't very clear about what the tachometer reading is. 20 * The datasheet isn't very clear about what the tachometer reading is.
21 * I had a explanation from National Semiconductor though. The two lower 21 * I had a explanation from National Semiconductor though. The two lower
22 * bits of the read value have to be masked out. The value is still 16 bit 22 * bits of the read value have to be masked out. The value is still 16 bit
23 * in width. 23 * in width.
24 * 24 *
25 * This program is free software; you can redistribute it and/or modify 25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by 26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or 27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version. 28 * (at your option) any later version.
29 * 29 *
30 * This program is distributed in the hope that it will be useful, 30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of 31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details. 33 * GNU General Public License for more details.
34 * 34 *
35 * You should have received a copy of the GNU General Public License 35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software 36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 */ 38 */
39 39
40 #include <linux/module.h> 40 #include <linux/module.h>
41 #include <linux/init.h> 41 #include <linux/init.h>
42 #include <linux/slab.h> 42 #include <linux/slab.h>
43 #include <linux/jiffies.h> 43 #include <linux/jiffies.h>
44 #include <linux/i2c.h> 44 #include <linux/i2c.h>
45 #include <linux/hwmon-sysfs.h> 45 #include <linux/hwmon-sysfs.h>
46 #include <linux/hwmon.h> 46 #include <linux/hwmon.h>
47 #include <linux/err.h> 47 #include <linux/err.h>
48 #include <linux/mutex.h> 48 #include <linux/mutex.h>
49 #include <linux/sysfs.h> 49 #include <linux/sysfs.h>
50 #include <linux/types.h> 50 #include <linux/types.h>
51 51
52 /* 52 /*
53 * Addresses to scan 53 * Addresses to scan
54 * Address is fully defined internally and cannot be changed except for 54 * Address is fully defined internally and cannot be changed except for
55 * LM64 which has one pin dedicated to address selection. 55 * LM64 which has one pin dedicated to address selection.
56 * LM63 and LM96163 have address 0x4c. 56 * LM63 and LM96163 have address 0x4c.
57 * LM64 can have address 0x18 or 0x4e. 57 * LM64 can have address 0x18 or 0x4e.
58 */ 58 */
59 59
60 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; 60 static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END };
61 61
62 /* 62 /*
63 * The LM63 registers 63 * The LM63 registers
64 */ 64 */
65 65
66 #define LM63_REG_CONFIG1 0x03 66 #define LM63_REG_CONFIG1 0x03
67 #define LM63_REG_CONVRATE 0x04 67 #define LM63_REG_CONVRATE 0x04
68 #define LM63_REG_CONFIG2 0xBF 68 #define LM63_REG_CONFIG2 0xBF
69 #define LM63_REG_CONFIG_FAN 0x4A 69 #define LM63_REG_CONFIG_FAN 0x4A
70 70
71 #define LM63_REG_TACH_COUNT_MSB 0x47 71 #define LM63_REG_TACH_COUNT_MSB 0x47
72 #define LM63_REG_TACH_COUNT_LSB 0x46 72 #define LM63_REG_TACH_COUNT_LSB 0x46
73 #define LM63_REG_TACH_LIMIT_MSB 0x49 73 #define LM63_REG_TACH_LIMIT_MSB 0x49
74 #define LM63_REG_TACH_LIMIT_LSB 0x48 74 #define LM63_REG_TACH_LIMIT_LSB 0x48
75 75
76 #define LM63_REG_PWM_VALUE 0x4C 76 #define LM63_REG_PWM_VALUE 0x4C
77 #define LM63_REG_PWM_FREQ 0x4D 77 #define LM63_REG_PWM_FREQ 0x4D
78 #define LM63_REG_LUT_TEMP_HYST 0x4F
79 #define LM63_REG_LUT_TEMP(nr) (0x50 + 2 * (nr))
80 #define LM63_REG_LUT_PWM(nr) (0x51 + 2 * (nr))
78 81
79 #define LM63_REG_LOCAL_TEMP 0x00 82 #define LM63_REG_LOCAL_TEMP 0x00
80 #define LM63_REG_LOCAL_HIGH 0x05 83 #define LM63_REG_LOCAL_HIGH 0x05
81 84
82 #define LM63_REG_REMOTE_TEMP_MSB 0x01 85 #define LM63_REG_REMOTE_TEMP_MSB 0x01
83 #define LM63_REG_REMOTE_TEMP_LSB 0x10 86 #define LM63_REG_REMOTE_TEMP_LSB 0x10
84 #define LM63_REG_REMOTE_OFFSET_MSB 0x11 87 #define LM63_REG_REMOTE_OFFSET_MSB 0x11
85 #define LM63_REG_REMOTE_OFFSET_LSB 0x12 88 #define LM63_REG_REMOTE_OFFSET_LSB 0x12
86 #define LM63_REG_REMOTE_HIGH_MSB 0x07 89 #define LM63_REG_REMOTE_HIGH_MSB 0x07
87 #define LM63_REG_REMOTE_HIGH_LSB 0x13 90 #define LM63_REG_REMOTE_HIGH_LSB 0x13
88 #define LM63_REG_REMOTE_LOW_MSB 0x08 91 #define LM63_REG_REMOTE_LOW_MSB 0x08
89 #define LM63_REG_REMOTE_LOW_LSB 0x14 92 #define LM63_REG_REMOTE_LOW_LSB 0x14
90 #define LM63_REG_REMOTE_TCRIT 0x19 93 #define LM63_REG_REMOTE_TCRIT 0x19
91 #define LM63_REG_REMOTE_TCRIT_HYST 0x21 94 #define LM63_REG_REMOTE_TCRIT_HYST 0x21
92 95
93 #define LM63_REG_ALERT_STATUS 0x02 96 #define LM63_REG_ALERT_STATUS 0x02
94 #define LM63_REG_ALERT_MASK 0x16 97 #define LM63_REG_ALERT_MASK 0x16
95 98
96 #define LM63_REG_MAN_ID 0xFE 99 #define LM63_REG_MAN_ID 0xFE
97 #define LM63_REG_CHIP_ID 0xFF 100 #define LM63_REG_CHIP_ID 0xFF
98 101
99 #define LM96163_REG_TRUTHERM 0x30 102 #define LM96163_REG_TRUTHERM 0x30
100 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31 103 #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31
101 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32 104 #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32
102 #define LM96163_REG_CONFIG_ENHANCED 0x45 105 #define LM96163_REG_CONFIG_ENHANCED 0x45
103 106
104 #define LM63_MAX_CONVRATE 9 107 #define LM63_MAX_CONVRATE 9
105 108
106 #define LM63_MAX_CONVRATE_HZ 32 109 #define LM63_MAX_CONVRATE_HZ 32
107 #define LM96163_MAX_CONVRATE_HZ 26 110 #define LM96163_MAX_CONVRATE_HZ 26
108 111
109 /* 112 /*
110 * Conversions and various macros 113 * Conversions and various macros
111 * For tachometer counts, the LM63 uses 16-bit values. 114 * For tachometer counts, the LM63 uses 16-bit values.
112 * For local temperature and high limit, remote critical limit and hysteresis 115 * For local temperature and high limit, remote critical limit and hysteresis
113 * value, it uses signed 8-bit values with LSB = 1 degree Celsius. 116 * value, it uses signed 8-bit values with LSB = 1 degree Celsius.
114 * For remote temperature, low and high limits, it uses signed 11-bit values 117 * For remote temperature, low and high limits, it uses signed 11-bit values
115 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. 118 * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
116 * For LM64 the actual remote diode temperature is 16 degree Celsius higher 119 * For LM64 the actual remote diode temperature is 16 degree Celsius higher
117 * than the register reading. Remote temperature setpoints have to be 120 * than the register reading. Remote temperature setpoints have to be
118 * adapted accordingly. 121 * adapted accordingly.
119 */ 122 */
120 123
121 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ 124 #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \
122 5400000 / (reg)) 125 5400000 / (reg))
123 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ 126 #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \
124 (5400000 / (val)) & 0xFFFC) 127 (5400000 / (val)) & 0xFFFC)
125 #define TEMP8_FROM_REG(reg) ((reg) * 1000) 128 #define TEMP8_FROM_REG(reg) ((reg) * 1000)
126 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \ 129 #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \
127 (val) >= 127000 ? 127 : \ 130 (val) >= 127000 ? 127 : \
128 (val) < 0 ? ((val) - 500) / 1000 : \ 131 (val) < 0 ? ((val) - 500) / 1000 : \
129 ((val) + 500) / 1000) 132 ((val) + 500) / 1000)
130 #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \ 133 #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \
131 (val) >= 255000 ? 255 : \ 134 (val) >= 255000 ? 255 : \
132 ((val) + 500) / 1000) 135 ((val) + 500) / 1000)
133 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) 136 #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125)
134 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \ 137 #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
135 (val) >= 127875 ? 0x7FE0 : \ 138 (val) >= 127875 ? 0x7FE0 : \
136 (val) < 0 ? ((val) - 62) / 125 * 32 : \ 139 (val) < 0 ? ((val) - 62) / 125 * 32 : \
137 ((val) + 62) / 125 * 32) 140 ((val) + 62) / 125 * 32)
138 #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \ 141 #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \
139 (val) >= 255875 ? 0xFFE0 : \ 142 (val) >= 255875 ? 0xFFE0 : \
140 ((val) + 62) / 125 * 32) 143 ((val) + 62) / 125 * 32)
141 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ 144 #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \
142 (val) >= 127000 ? 127 : \ 145 (val) >= 127000 ? 127 : \
143 ((val) + 500) / 1000) 146 ((val) + 500) / 1000)
144 147
145 #define UPDATE_INTERVAL(max, rate) \ 148 #define UPDATE_INTERVAL(max, rate) \
146 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max)) 149 ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max))
147 150
148 /* 151 /*
149 * Functions declaration 152 * Functions declaration
150 */ 153 */
151 154
152 static int lm63_probe(struct i2c_client *client, 155 static int lm63_probe(struct i2c_client *client,
153 const struct i2c_device_id *id); 156 const struct i2c_device_id *id);
154 static int lm63_remove(struct i2c_client *client); 157 static int lm63_remove(struct i2c_client *client);
155 158
156 static struct lm63_data *lm63_update_device(struct device *dev); 159 static struct lm63_data *lm63_update_device(struct device *dev);
157 160
158 static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info); 161 static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info);
159 static void lm63_init_client(struct i2c_client *client); 162 static void lm63_init_client(struct i2c_client *client);
160 163
161 enum chips { lm63, lm64, lm96163 }; 164 enum chips { lm63, lm64, lm96163 };
162 165
163 /* 166 /*
164 * Driver data (common to all clients) 167 * Driver data (common to all clients)
165 */ 168 */
166 169
167 static const struct i2c_device_id lm63_id[] = { 170 static const struct i2c_device_id lm63_id[] = {
168 { "lm63", lm63 }, 171 { "lm63", lm63 },
169 { "lm64", lm64 }, 172 { "lm64", lm64 },
170 { "lm96163", lm96163 }, 173 { "lm96163", lm96163 },
171 { } 174 { }
172 }; 175 };
173 MODULE_DEVICE_TABLE(i2c, lm63_id); 176 MODULE_DEVICE_TABLE(i2c, lm63_id);
174 177
175 static struct i2c_driver lm63_driver = { 178 static struct i2c_driver lm63_driver = {
176 .class = I2C_CLASS_HWMON, 179 .class = I2C_CLASS_HWMON,
177 .driver = { 180 .driver = {
178 .name = "lm63", 181 .name = "lm63",
179 }, 182 },
180 .probe = lm63_probe, 183 .probe = lm63_probe,
181 .remove = lm63_remove, 184 .remove = lm63_remove,
182 .id_table = lm63_id, 185 .id_table = lm63_id,
183 .detect = lm63_detect, 186 .detect = lm63_detect,
184 .address_list = normal_i2c, 187 .address_list = normal_i2c,
185 }; 188 };
186 189
187 /* 190 /*
188 * Client data (each client gets its own) 191 * Client data (each client gets its own)
189 */ 192 */
190 193
191 struct lm63_data { 194 struct lm63_data {
192 struct device *hwmon_dev; 195 struct device *hwmon_dev;
193 struct mutex update_lock; 196 struct mutex update_lock;
194 char valid; /* zero until following fields are valid */ 197 char valid; /* zero until following fields are valid */
198 char lut_valid; /* zero until lut fields are valid */
195 unsigned long last_updated; /* in jiffies */ 199 unsigned long last_updated; /* in jiffies */
200 unsigned long lut_last_updated; /* in jiffies */
196 enum chips kind; 201 enum chips kind;
197 int temp2_offset; 202 int temp2_offset;
198 203
199 int update_interval; /* in milliseconds */ 204 int update_interval; /* in milliseconds */
200 int max_convrate_hz; 205 int max_convrate_hz;
201 206
202 /* registers values */ 207 /* registers values */
203 u8 config, config_fan; 208 u8 config, config_fan;
204 u16 fan[2]; /* 0: input 209 u16 fan[2]; /* 0: input
205 1: low limit */ 210 1: low limit */
206 u8 pwm1_freq; 211 u8 pwm1_freq;
207 u8 pwm1_value; 212 u8 pwm1[9]; /* 0: current output
208 s8 temp8[3]; /* 0: local input 213 1-8: lookup table */
214 s8 temp8[11]; /* 0: local input
209 1: local high limit 215 1: local high limit
210 2: remote critical limit */ 216 2: remote critical limit
217 3-10: lookup table */
211 s16 temp11[4]; /* 0: remote input 218 s16 temp11[4]; /* 0: remote input
212 1: remote low limit 219 1: remote low limit
213 2: remote high limit 220 2: remote high limit
214 3: remote offset */ 221 3: remote offset */
215 u16 temp11u; /* remote input (unsigned) */ 222 u16 temp11u; /* remote input (unsigned) */
216 u8 temp2_crit_hyst; 223 u8 temp2_crit_hyst;
224 u8 lut_temp_hyst;
217 u8 alarms; 225 u8 alarms;
218 bool pwm_highres; 226 bool pwm_highres;
227 bool lut_temp_highres;
219 bool remote_unsigned; /* true if unsigned remote upper limits */ 228 bool remote_unsigned; /* true if unsigned remote upper limits */
220 bool trutherm; 229 bool trutherm;
221 }; 230 };
222 231
223 static inline int temp8_from_reg(struct lm63_data *data, int nr) 232 static inline int temp8_from_reg(struct lm63_data *data, int nr)
224 { 233 {
225 if (data->remote_unsigned) 234 if (data->remote_unsigned)
226 return TEMP8_FROM_REG((u8)data->temp8[nr]); 235 return TEMP8_FROM_REG((u8)data->temp8[nr]);
227 return TEMP8_FROM_REG(data->temp8[nr]); 236 return TEMP8_FROM_REG(data->temp8[nr]);
228 } 237 }
229 238
239 static inline int lut_temp_from_reg(struct lm63_data *data, int nr)
240 {
241 return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000);
242 }
243
230 /* 244 /*
231 * Sysfs callback functions and files 245 * Sysfs callback functions and files
232 */ 246 */
233 247
234 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, 248 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
235 char *buf) 249 char *buf)
236 { 250 {
237 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 251 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
238 struct lm63_data *data = lm63_update_device(dev); 252 struct lm63_data *data = lm63_update_device(dev);
239 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); 253 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index]));
240 } 254 }
241 255
242 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, 256 static ssize_t set_fan(struct device *dev, struct device_attribute *dummy,
243 const char *buf, size_t count) 257 const char *buf, size_t count)
244 { 258 {
245 struct i2c_client *client = to_i2c_client(dev); 259 struct i2c_client *client = to_i2c_client(dev);
246 struct lm63_data *data = i2c_get_clientdata(client); 260 struct lm63_data *data = i2c_get_clientdata(client);
247 unsigned long val; 261 unsigned long val;
248 int err; 262 int err;
249 263
250 err = kstrtoul(buf, 10, &val); 264 err = kstrtoul(buf, 10, &val);
251 if (err) 265 if (err)
252 return err; 266 return err;
253 267
254 mutex_lock(&data->update_lock); 268 mutex_lock(&data->update_lock);
255 data->fan[1] = FAN_TO_REG(val); 269 data->fan[1] = FAN_TO_REG(val);
256 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, 270 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB,
257 data->fan[1] & 0xFF); 271 data->fan[1] & 0xFF);
258 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, 272 i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB,
259 data->fan[1] >> 8); 273 data->fan[1] >> 8);
260 mutex_unlock(&data->update_lock); 274 mutex_unlock(&data->update_lock);
261 return count; 275 return count;
262 } 276 }
263 277
264 static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, 278 static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr,
265 char *buf) 279 char *buf)
266 { 280 {
281 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
267 struct lm63_data *data = lm63_update_device(dev); 282 struct lm63_data *data = lm63_update_device(dev);
283 int nr = attr->index;
268 int pwm; 284 int pwm;
269 285
270 if (data->pwm_highres) 286 if (data->pwm_highres)
271 pwm = data->pwm1_value; 287 pwm = data->pwm1[nr];
272 else 288 else
273 pwm = data->pwm1_value >= 2 * data->pwm1_freq ? 289 pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ?
274 255 : (data->pwm1_value * 255 + data->pwm1_freq) / 290 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) /
275 (2 * data->pwm1_freq); 291 (2 * data->pwm1_freq);
276 292
277 return sprintf(buf, "%d\n", pwm); 293 return sprintf(buf, "%d\n", pwm);
278 } 294 }
279 295
280 static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, 296 static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy,
281 const char *buf, size_t count) 297 const char *buf, size_t count)
282 { 298 {
283 struct i2c_client *client = to_i2c_client(dev); 299 struct i2c_client *client = to_i2c_client(dev);
284 struct lm63_data *data = i2c_get_clientdata(client); 300 struct lm63_data *data = i2c_get_clientdata(client);
285 unsigned long val; 301 unsigned long val;
286 int err; 302 int err;
287 303
288 if (!(data->config_fan & 0x20)) /* register is read-only */ 304 if (!(data->config_fan & 0x20)) /* register is read-only */
289 return -EPERM; 305 return -EPERM;
290 306
291 err = kstrtoul(buf, 10, &val); 307 err = kstrtoul(buf, 10, &val);
292 if (err) 308 if (err)
293 return err; 309 return err;
294 310
295 val = SENSORS_LIMIT(val, 0, 255); 311 val = SENSORS_LIMIT(val, 0, 255);
296 mutex_lock(&data->update_lock); 312 mutex_lock(&data->update_lock);
297 data->pwm1_value = data->pwm_highres ? val : 313 data->pwm1[0] = data->pwm_highres ? val :
298 (val * data->pwm1_freq * 2 + 127) / 255; 314 (val * data->pwm1_freq * 2 + 127) / 255;
299 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value); 315 i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1[0]);
300 mutex_unlock(&data->update_lock); 316 mutex_unlock(&data->update_lock);
301 return count; 317 return count;
302 } 318 }
303 319
304 static ssize_t show_pwm1_enable(struct device *dev, 320 static ssize_t show_pwm1_enable(struct device *dev,
305 struct device_attribute *dummy, char *buf) 321 struct device_attribute *dummy, char *buf)
306 { 322 {
307 struct lm63_data *data = lm63_update_device(dev); 323 struct lm63_data *data = lm63_update_device(dev);
308 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); 324 return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2);
309 } 325 }
310 326
311 /* 327 /*
312 * There are 8bit registers for both local(temp1) and remote(temp2) sensor. 328 * There are 8bit registers for both local(temp1) and remote(temp2) sensor.
313 * For remote sensor registers temp2_offset has to be considered, 329 * For remote sensor registers temp2_offset has to be considered,
314 * for local sensor it must not. 330 * for local sensor it must not.
315 * So we need separate 8bit accessors for local and remote sensor. 331 * So we need separate 8bit accessors for local and remote sensor.
316 */ 332 */
317 static ssize_t show_local_temp8(struct device *dev, 333 static ssize_t show_local_temp8(struct device *dev,
318 struct device_attribute *devattr, 334 struct device_attribute *devattr,
319 char *buf) 335 char *buf)
320 { 336 {
321 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 337 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
322 struct lm63_data *data = lm63_update_device(dev); 338 struct lm63_data *data = lm63_update_device(dev);
323 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); 339 return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]));
324 } 340 }
325 341
326 static ssize_t show_remote_temp8(struct device *dev, 342 static ssize_t show_remote_temp8(struct device *dev,
327 struct device_attribute *devattr, 343 struct device_attribute *devattr,
328 char *buf) 344 char *buf)
329 { 345 {
330 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
331 struct lm63_data *data = lm63_update_device(dev); 347 struct lm63_data *data = lm63_update_device(dev);
332 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index) 348 return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index)
333 + data->temp2_offset); 349 + data->temp2_offset);
334 } 350 }
335 351
352 static ssize_t show_lut_temp(struct device *dev,
353 struct device_attribute *devattr,
354 char *buf)
355 {
356 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
357 struct lm63_data *data = lm63_update_device(dev);
358 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
359 + data->temp2_offset);
360 }
361
336 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, 362 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
337 const char *buf, size_t count) 363 const char *buf, size_t count)
338 { 364 {
339 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 365 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
340 struct i2c_client *client = to_i2c_client(dev); 366 struct i2c_client *client = to_i2c_client(dev);
341 struct lm63_data *data = i2c_get_clientdata(client); 367 struct lm63_data *data = i2c_get_clientdata(client);
342 int nr = attr->index; 368 int nr = attr->index;
343 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH; 369 int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH;
344 long val; 370 long val;
345 int err; 371 int err;
346 int temp; 372 int temp;
347 373
348 err = kstrtol(buf, 10, &val); 374 err = kstrtol(buf, 10, &val);
349 if (err) 375 if (err)
350 return err; 376 return err;
351 377
352 mutex_lock(&data->update_lock); 378 mutex_lock(&data->update_lock);
353 if (nr == 2) { 379 if (nr == 2) {
354 if (data->remote_unsigned) 380 if (data->remote_unsigned)
355 temp = TEMP8U_TO_REG(val - data->temp2_offset); 381 temp = TEMP8U_TO_REG(val - data->temp2_offset);
356 else 382 else
357 temp = TEMP8_TO_REG(val - data->temp2_offset); 383 temp = TEMP8_TO_REG(val - data->temp2_offset);
358 } else { 384 } else {
359 temp = TEMP8_TO_REG(val); 385 temp = TEMP8_TO_REG(val);
360 } 386 }
361 data->temp8[nr] = temp; 387 data->temp8[nr] = temp;
362 i2c_smbus_write_byte_data(client, reg, temp); 388 i2c_smbus_write_byte_data(client, reg, temp);
363 mutex_unlock(&data->update_lock); 389 mutex_unlock(&data->update_lock);
364 return count; 390 return count;
365 } 391 }
366 392
367 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, 393 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
368 char *buf) 394 char *buf)
369 { 395 {
370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 396 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
371 struct lm63_data *data = lm63_update_device(dev); 397 struct lm63_data *data = lm63_update_device(dev);
372 int nr = attr->index; 398 int nr = attr->index;
373 int temp; 399 int temp;
374 400
375 if (!nr) { 401 if (!nr) {
376 /* 402 /*
377 * Use unsigned temperature unless its value is zero. 403 * Use unsigned temperature unless its value is zero.
378 * If it is zero, use signed temperature. 404 * If it is zero, use signed temperature.
379 */ 405 */
380 if (data->temp11u) 406 if (data->temp11u)
381 temp = TEMP11_FROM_REG(data->temp11u); 407 temp = TEMP11_FROM_REG(data->temp11u);
382 else 408 else
383 temp = TEMP11_FROM_REG(data->temp11[nr]); 409 temp = TEMP11_FROM_REG(data->temp11[nr]);
384 } else { 410 } else {
385 if (data->remote_unsigned && nr == 2) 411 if (data->remote_unsigned && nr == 2)
386 temp = TEMP11_FROM_REG((u16)data->temp11[nr]); 412 temp = TEMP11_FROM_REG((u16)data->temp11[nr]);
387 else 413 else
388 temp = TEMP11_FROM_REG(data->temp11[nr]); 414 temp = TEMP11_FROM_REG(data->temp11[nr]);
389 } 415 }
390 return sprintf(buf, "%d\n", temp + data->temp2_offset); 416 return sprintf(buf, "%d\n", temp + data->temp2_offset);
391 } 417 }
392 418
393 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, 419 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
394 const char *buf, size_t count) 420 const char *buf, size_t count)
395 { 421 {
396 static const u8 reg[6] = { 422 static const u8 reg[6] = {
397 LM63_REG_REMOTE_LOW_MSB, 423 LM63_REG_REMOTE_LOW_MSB,
398 LM63_REG_REMOTE_LOW_LSB, 424 LM63_REG_REMOTE_LOW_LSB,
399 LM63_REG_REMOTE_HIGH_MSB, 425 LM63_REG_REMOTE_HIGH_MSB,
400 LM63_REG_REMOTE_HIGH_LSB, 426 LM63_REG_REMOTE_HIGH_LSB,
401 LM63_REG_REMOTE_OFFSET_MSB, 427 LM63_REG_REMOTE_OFFSET_MSB,
402 LM63_REG_REMOTE_OFFSET_LSB, 428 LM63_REG_REMOTE_OFFSET_LSB,
403 }; 429 };
404 430
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 431 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 struct i2c_client *client = to_i2c_client(dev); 432 struct i2c_client *client = to_i2c_client(dev);
407 struct lm63_data *data = i2c_get_clientdata(client); 433 struct lm63_data *data = i2c_get_clientdata(client);
408 long val; 434 long val;
409 int err; 435 int err;
410 int nr = attr->index; 436 int nr = attr->index;
411 437
412 err = kstrtol(buf, 10, &val); 438 err = kstrtol(buf, 10, &val);
413 if (err) 439 if (err)
414 return err; 440 return err;
415 441
416 mutex_lock(&data->update_lock); 442 mutex_lock(&data->update_lock);
417 if (data->remote_unsigned && nr == 2) 443 if (data->remote_unsigned && nr == 2)
418 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset); 444 data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset);
419 else 445 else
420 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset); 446 data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset);
421 447
422 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], 448 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
423 data->temp11[nr] >> 8); 449 data->temp11[nr] >> 8);
424 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], 450 i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
425 data->temp11[nr] & 0xff); 451 data->temp11[nr] & 0xff);
426 mutex_unlock(&data->update_lock); 452 mutex_unlock(&data->update_lock);
427 return count; 453 return count;
428 } 454 }
429 455
430 /* 456 /*
431 * Hysteresis register holds a relative value, while we want to present 457 * Hysteresis register holds a relative value, while we want to present
432 * an absolute to user-space 458 * an absolute to user-space
433 */ 459 */
434 static ssize_t show_temp2_crit_hyst(struct device *dev, 460 static ssize_t show_temp2_crit_hyst(struct device *dev,
435 struct device_attribute *dummy, char *buf) 461 struct device_attribute *dummy, char *buf)
436 { 462 {
437 struct lm63_data *data = lm63_update_device(dev); 463 struct lm63_data *data = lm63_update_device(dev);
438 return sprintf(buf, "%d\n", temp8_from_reg(data, 2) 464 return sprintf(buf, "%d\n", temp8_from_reg(data, 2)
439 + data->temp2_offset 465 + data->temp2_offset
440 - TEMP8_FROM_REG(data->temp2_crit_hyst)); 466 - TEMP8_FROM_REG(data->temp2_crit_hyst));
441 } 467 }
442 468
469 static ssize_t show_lut_temp_hyst(struct device *dev,
470 struct device_attribute *devattr, char *buf)
471 {
472 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
473 struct lm63_data *data = lm63_update_device(dev);
474
475 return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index)
476 + data->temp2_offset
477 - TEMP8_FROM_REG(data->lut_temp_hyst));
478 }
479
443 /* 480 /*
444 * And now the other way around, user-space provides an absolute 481 * And now the other way around, user-space provides an absolute
445 * hysteresis value and we have to store a relative one 482 * hysteresis value and we have to store a relative one
446 */ 483 */
447 static ssize_t set_temp2_crit_hyst(struct device *dev, 484 static ssize_t set_temp2_crit_hyst(struct device *dev,
448 struct device_attribute *dummy, 485 struct device_attribute *dummy,
449 const char *buf, size_t count) 486 const char *buf, size_t count)
450 { 487 {
451 struct i2c_client *client = to_i2c_client(dev); 488 struct i2c_client *client = to_i2c_client(dev);
452 struct lm63_data *data = i2c_get_clientdata(client); 489 struct lm63_data *data = i2c_get_clientdata(client);
453 long val; 490 long val;
454 int err; 491 int err;
455 long hyst; 492 long hyst;
456 493
457 err = kstrtol(buf, 10, &val); 494 err = kstrtol(buf, 10, &val);
458 if (err) 495 if (err)
459 return err; 496 return err;
460 497
461 mutex_lock(&data->update_lock); 498 mutex_lock(&data->update_lock);
462 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val; 499 hyst = temp8_from_reg(data, 2) + data->temp2_offset - val;
463 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, 500 i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST,
464 HYST_TO_REG(hyst)); 501 HYST_TO_REG(hyst));
465 mutex_unlock(&data->update_lock); 502 mutex_unlock(&data->update_lock);
466 return count; 503 return count;
467 } 504 }
468 505
469 /* 506 /*
470 * Set conversion rate. 507 * Set conversion rate.
471 * client->update_lock must be held when calling this function. 508 * client->update_lock must be held when calling this function.
472 */ 509 */
473 static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data, 510 static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data,
474 unsigned int interval) 511 unsigned int interval)
475 { 512 {
476 int i; 513 int i;
477 unsigned int update_interval; 514 unsigned int update_interval;
478 515
479 /* Shift calculations to avoid rounding errors */ 516 /* Shift calculations to avoid rounding errors */
480 interval <<= 6; 517 interval <<= 6;
481 518
482 /* find the nearest update rate */ 519 /* find the nearest update rate */
483 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000 520 update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000
484 / data->max_convrate_hz; 521 / data->max_convrate_hz;
485 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1) 522 for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1)
486 if (interval >= update_interval * 3 / 4) 523 if (interval >= update_interval * 3 / 4)
487 break; 524 break;
488 525
489 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i); 526 i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i);
490 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i); 527 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i);
491 } 528 }
492 529
493 static ssize_t show_update_interval(struct device *dev, 530 static ssize_t show_update_interval(struct device *dev,
494 struct device_attribute *attr, char *buf) 531 struct device_attribute *attr, char *buf)
495 { 532 {
496 struct lm63_data *data = dev_get_drvdata(dev); 533 struct lm63_data *data = dev_get_drvdata(dev);
497 534
498 return sprintf(buf, "%u\n", data->update_interval); 535 return sprintf(buf, "%u\n", data->update_interval);
499 } 536 }
500 537
501 static ssize_t set_update_interval(struct device *dev, 538 static ssize_t set_update_interval(struct device *dev,
502 struct device_attribute *attr, 539 struct device_attribute *attr,
503 const char *buf, size_t count) 540 const char *buf, size_t count)
504 { 541 {
505 struct i2c_client *client = to_i2c_client(dev); 542 struct i2c_client *client = to_i2c_client(dev);
506 struct lm63_data *data = i2c_get_clientdata(client); 543 struct lm63_data *data = i2c_get_clientdata(client);
507 unsigned long val; 544 unsigned long val;
508 int err; 545 int err;
509 546
510 err = kstrtoul(buf, 10, &val); 547 err = kstrtoul(buf, 10, &val);
511 if (err) 548 if (err)
512 return err; 549 return err;
513 550
514 mutex_lock(&data->update_lock); 551 mutex_lock(&data->update_lock);
515 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000)); 552 lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000));
516 mutex_unlock(&data->update_lock); 553 mutex_unlock(&data->update_lock);
517 554
518 return count; 555 return count;
519 } 556 }
520 557
521 static ssize_t show_type(struct device *dev, struct device_attribute *attr, 558 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
522 char *buf) 559 char *buf)
523 { 560 {
524 struct i2c_client *client = to_i2c_client(dev); 561 struct i2c_client *client = to_i2c_client(dev);
525 struct lm63_data *data = i2c_get_clientdata(client); 562 struct lm63_data *data = i2c_get_clientdata(client);
526 563
527 return sprintf(buf, data->trutherm ? "1\n" : "2\n"); 564 return sprintf(buf, data->trutherm ? "1\n" : "2\n");
528 } 565 }
529 566
530 static ssize_t set_type(struct device *dev, struct device_attribute *attr, 567 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
531 const char *buf, size_t count) 568 const char *buf, size_t count)
532 { 569 {
533 struct i2c_client *client = to_i2c_client(dev); 570 struct i2c_client *client = to_i2c_client(dev);
534 struct lm63_data *data = i2c_get_clientdata(client); 571 struct lm63_data *data = i2c_get_clientdata(client);
535 unsigned long val; 572 unsigned long val;
536 int ret; 573 int ret;
537 u8 reg; 574 u8 reg;
538 575
539 ret = kstrtoul(buf, 10, &val); 576 ret = kstrtoul(buf, 10, &val);
540 if (ret < 0) 577 if (ret < 0)
541 return ret; 578 return ret;
542 if (val != 1 && val != 2) 579 if (val != 1 && val != 2)
543 return -EINVAL; 580 return -EINVAL;
544 581
545 mutex_lock(&data->update_lock); 582 mutex_lock(&data->update_lock);
546 data->trutherm = val == 1; 583 data->trutherm = val == 1;
547 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02; 584 reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02;
548 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM, 585 i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM,
549 reg | (data->trutherm ? 0x02 : 0x00)); 586 reg | (data->trutherm ? 0x02 : 0x00));
550 data->valid = 0; 587 data->valid = 0;
551 mutex_unlock(&data->update_lock); 588 mutex_unlock(&data->update_lock);
552 589
553 return count; 590 return count;
554 } 591 }
555 592
556 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, 593 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
557 char *buf) 594 char *buf)
558 { 595 {
559 struct lm63_data *data = lm63_update_device(dev); 596 struct lm63_data *data = lm63_update_device(dev);
560 return sprintf(buf, "%u\n", data->alarms); 597 return sprintf(buf, "%u\n", data->alarms);
561 } 598 }
562 599
563 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, 600 static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr,
564 char *buf) 601 char *buf)
565 { 602 {
566 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 603 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
567 struct lm63_data *data = lm63_update_device(dev); 604 struct lm63_data *data = lm63_update_device(dev);
568 int bitnr = attr->index; 605 int bitnr = attr->index;
569 606
570 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); 607 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
571 } 608 }
572 609
573 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); 610 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
574 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, 611 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan,
575 set_fan, 1); 612 set_fan, 1);
576 613
577 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); 614 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0);
578 static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); 615 static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL);
616 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO, show_pwm1, NULL, 1);
617 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IRUGO,
618 show_lut_temp, NULL, 3);
619 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO,
620 show_lut_temp_hyst, NULL, 3);
621 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO, show_pwm1, NULL, 2);
622 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO,
623 show_lut_temp, NULL, 4);
624 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO,
625 show_lut_temp_hyst, NULL, 4);
626 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO, show_pwm1, NULL, 3);
627 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IRUGO,
628 show_lut_temp, NULL, 5);
629 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO,
630 show_lut_temp_hyst, NULL, 5);
631 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO, show_pwm1, NULL, 4);
632 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IRUGO,
633 show_lut_temp, NULL, 6);
634 static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO,
635 show_lut_temp_hyst, NULL, 6);
636 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm1, NULL, 5);
637 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IRUGO,
638 show_lut_temp, NULL, 7);
639 static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO,
640 show_lut_temp_hyst, NULL, 7);
641 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IRUGO, show_pwm1, NULL, 6);
642 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IRUGO,
643 show_lut_temp, NULL, 8);
644 static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO,
645 show_lut_temp_hyst, NULL, 8);
646 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IRUGO, show_pwm1, NULL, 7);
647 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IRUGO,
648 show_lut_temp, NULL, 9);
649 static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO,
650 show_lut_temp_hyst, NULL, 9);
651 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IRUGO, show_pwm1, NULL, 8);
652 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IRUGO,
653 show_lut_temp, NULL, 10);
654 static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO,
655 show_lut_temp_hyst, NULL, 10);
579 656
580 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0); 657 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0);
581 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8, 658 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8,
582 set_temp8, 1); 659 set_temp8, 1);
583 660
584 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); 661 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
585 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, 662 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
586 set_temp11, 1); 663 set_temp11, 1);
587 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, 664 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
588 set_temp11, 2); 665 set_temp11, 2);
589 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, 666 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
590 set_temp11, 3); 667 set_temp11, 3);
591 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8, 668 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8,
592 set_temp8, 2); 669 set_temp8, 2);
593 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, 670 static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst,
594 set_temp2_crit_hyst); 671 set_temp2_crit_hyst);
595 672
596 static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type); 673 static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type);
597 674
598 /* Individual alarm files */ 675 /* Individual alarm files */
599 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); 676 static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
600 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); 677 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
601 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); 678 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
602 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); 679 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
603 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); 680 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
604 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); 681 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
605 /* Raw alarm file for compatibility */ 682 /* Raw alarm file for compatibility */
606 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); 683 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
607 684
608 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval, 685 static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval,
609 set_update_interval); 686 set_update_interval);
610 687
611 static struct attribute *lm63_attributes[] = { 688 static struct attribute *lm63_attributes[] = {
612 &dev_attr_pwm1.attr, 689 &sensor_dev_attr_pwm1.dev_attr.attr,
613 &dev_attr_pwm1_enable.attr, 690 &dev_attr_pwm1_enable.attr,
691 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
692 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
693 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
694 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
695 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
696 &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr,
697 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
698 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
699 &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr,
700 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
701 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
702 &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr,
703 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
704 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
705 &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr,
706 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
707 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
708 &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr,
709 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
710 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
711 &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr,
712 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
713 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
714 &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr,
715
614 &sensor_dev_attr_temp1_input.dev_attr.attr, 716 &sensor_dev_attr_temp1_input.dev_attr.attr,
615 &sensor_dev_attr_temp2_input.dev_attr.attr, 717 &sensor_dev_attr_temp2_input.dev_attr.attr,
616 &sensor_dev_attr_temp2_min.dev_attr.attr, 718 &sensor_dev_attr_temp2_min.dev_attr.attr,
617 &sensor_dev_attr_temp1_max.dev_attr.attr, 719 &sensor_dev_attr_temp1_max.dev_attr.attr,
618 &sensor_dev_attr_temp2_max.dev_attr.attr, 720 &sensor_dev_attr_temp2_max.dev_attr.attr,
619 &sensor_dev_attr_temp2_offset.dev_attr.attr, 721 &sensor_dev_attr_temp2_offset.dev_attr.attr,
620 &sensor_dev_attr_temp2_crit.dev_attr.attr, 722 &sensor_dev_attr_temp2_crit.dev_attr.attr,
621 &dev_attr_temp2_crit_hyst.attr, 723 &dev_attr_temp2_crit_hyst.attr,
622 724
623 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 725 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
624 &sensor_dev_attr_temp2_fault.dev_attr.attr, 726 &sensor_dev_attr_temp2_fault.dev_attr.attr,
625 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 727 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
626 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 728 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
627 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 729 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
628 &dev_attr_alarms.attr, 730 &dev_attr_alarms.attr,
629 &dev_attr_update_interval.attr, 731 &dev_attr_update_interval.attr,
630 NULL 732 NULL
631 }; 733 };
632 734
633 /* 735 /*
634 * On LM63, temp2_crit can be set only once, which should be job 736 * On LM63, temp2_crit can be set only once, which should be job
635 * of the bootloader. 737 * of the bootloader.
636 * On LM64, temp2_crit can always be set. 738 * On LM64, temp2_crit can always be set.
637 * On LM96163, temp2_crit can be set if bit 1 of the configuration 739 * On LM96163, temp2_crit can be set if bit 1 of the configuration
638 * register is true. 740 * register is true.
639 */ 741 */
640 static umode_t lm63_attribute_mode(struct kobject *kobj, 742 static umode_t lm63_attribute_mode(struct kobject *kobj,
641 struct attribute *attr, int index) 743 struct attribute *attr, int index)
642 { 744 {
643 struct device *dev = container_of(kobj, struct device, kobj); 745 struct device *dev = container_of(kobj, struct device, kobj);
644 struct i2c_client *client = to_i2c_client(dev); 746 struct i2c_client *client = to_i2c_client(dev);
645 struct lm63_data *data = i2c_get_clientdata(client); 747 struct lm63_data *data = i2c_get_clientdata(client);
646 748
647 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr 749 if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr
648 && (data->kind == lm64 || 750 && (data->kind == lm64 ||
649 (data->kind == lm96163 && (data->config & 0x02)))) 751 (data->kind == lm96163 && (data->config & 0x02))))
650 return attr->mode | S_IWUSR; 752 return attr->mode | S_IWUSR;
651 753
652 return attr->mode; 754 return attr->mode;
653 } 755 }
654 756
655 static const struct attribute_group lm63_group = { 757 static const struct attribute_group lm63_group = {
656 .is_visible = lm63_attribute_mode, 758 .is_visible = lm63_attribute_mode,
657 .attrs = lm63_attributes, 759 .attrs = lm63_attributes,
658 }; 760 };
659 761
660 static struct attribute *lm63_attributes_fan1[] = { 762 static struct attribute *lm63_attributes_fan1[] = {
661 &sensor_dev_attr_fan1_input.dev_attr.attr, 763 &sensor_dev_attr_fan1_input.dev_attr.attr,
662 &sensor_dev_attr_fan1_min.dev_attr.attr, 764 &sensor_dev_attr_fan1_min.dev_attr.attr,
663 765
664 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, 766 &sensor_dev_attr_fan1_min_alarm.dev_attr.attr,
665 NULL 767 NULL
666 }; 768 };
667 769
668 static const struct attribute_group lm63_group_fan1 = { 770 static const struct attribute_group lm63_group_fan1 = {
669 .attrs = lm63_attributes_fan1, 771 .attrs = lm63_attributes_fan1,
670 }; 772 };
671 773
672 /* 774 /*
673 * Real code 775 * Real code
674 */ 776 */
675 777
676 /* Return 0 if detection is successful, -ENODEV otherwise */ 778 /* Return 0 if detection is successful, -ENODEV otherwise */
677 static int lm63_detect(struct i2c_client *new_client, 779 static int lm63_detect(struct i2c_client *new_client,
678 struct i2c_board_info *info) 780 struct i2c_board_info *info)
679 { 781 {
680 struct i2c_adapter *adapter = new_client->adapter; 782 struct i2c_adapter *adapter = new_client->adapter;
681 u8 man_id, chip_id, reg_config1, reg_config2; 783 u8 man_id, chip_id, reg_config1, reg_config2;
682 u8 reg_alert_status, reg_alert_mask; 784 u8 reg_alert_status, reg_alert_mask;
683 int address = new_client->addr; 785 int address = new_client->addr;
684 786
685 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 787 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
686 return -ENODEV; 788 return -ENODEV;
687 789
688 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID); 790 man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID);
689 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID); 791 chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID);
690 792
691 reg_config1 = i2c_smbus_read_byte_data(new_client, 793 reg_config1 = i2c_smbus_read_byte_data(new_client,
692 LM63_REG_CONFIG1); 794 LM63_REG_CONFIG1);
693 reg_config2 = i2c_smbus_read_byte_data(new_client, 795 reg_config2 = i2c_smbus_read_byte_data(new_client,
694 LM63_REG_CONFIG2); 796 LM63_REG_CONFIG2);
695 reg_alert_status = i2c_smbus_read_byte_data(new_client, 797 reg_alert_status = i2c_smbus_read_byte_data(new_client,
696 LM63_REG_ALERT_STATUS); 798 LM63_REG_ALERT_STATUS);
697 reg_alert_mask = i2c_smbus_read_byte_data(new_client, 799 reg_alert_mask = i2c_smbus_read_byte_data(new_client,
698 LM63_REG_ALERT_MASK); 800 LM63_REG_ALERT_MASK);
699 801
700 if (man_id != 0x01 /* National Semiconductor */ 802 if (man_id != 0x01 /* National Semiconductor */
701 || (reg_config1 & 0x18) != 0x00 803 || (reg_config1 & 0x18) != 0x00
702 || (reg_config2 & 0xF8) != 0x00 804 || (reg_config2 & 0xF8) != 0x00
703 || (reg_alert_status & 0x20) != 0x00 805 || (reg_alert_status & 0x20) != 0x00
704 || (reg_alert_mask & 0xA4) != 0xA4) { 806 || (reg_alert_mask & 0xA4) != 0xA4) {
705 dev_dbg(&adapter->dev, 807 dev_dbg(&adapter->dev,
706 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", 808 "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
707 man_id, chip_id); 809 man_id, chip_id);
708 return -ENODEV; 810 return -ENODEV;
709 } 811 }
710 812
711 if (chip_id == 0x41 && address == 0x4c) 813 if (chip_id == 0x41 && address == 0x4c)
712 strlcpy(info->type, "lm63", I2C_NAME_SIZE); 814 strlcpy(info->type, "lm63", I2C_NAME_SIZE);
713 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e)) 815 else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e))
714 strlcpy(info->type, "lm64", I2C_NAME_SIZE); 816 strlcpy(info->type, "lm64", I2C_NAME_SIZE);
715 else if (chip_id == 0x49 && address == 0x4c) 817 else if (chip_id == 0x49 && address == 0x4c)
716 strlcpy(info->type, "lm96163", I2C_NAME_SIZE); 818 strlcpy(info->type, "lm96163", I2C_NAME_SIZE);
717 else 819 else
718 return -ENODEV; 820 return -ENODEV;
719 821
720 return 0; 822 return 0;
721 } 823 }
722 824
723 static int lm63_probe(struct i2c_client *new_client, 825 static int lm63_probe(struct i2c_client *new_client,
724 const struct i2c_device_id *id) 826 const struct i2c_device_id *id)
725 { 827 {
726 struct lm63_data *data; 828 struct lm63_data *data;
727 int err; 829 int err;
728 830
729 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL); 831 data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL);
730 if (!data) { 832 if (!data) {
731 err = -ENOMEM; 833 err = -ENOMEM;
732 goto exit; 834 goto exit;
733 } 835 }
734 836
735 i2c_set_clientdata(new_client, data); 837 i2c_set_clientdata(new_client, data);
736 data->valid = 0; 838 data->valid = 0;
737 mutex_init(&data->update_lock); 839 mutex_init(&data->update_lock);
738 840
739 /* Set the device type */ 841 /* Set the device type */
740 data->kind = id->driver_data; 842 data->kind = id->driver_data;
741 if (data->kind == lm64) 843 if (data->kind == lm64)
742 data->temp2_offset = 16000; 844 data->temp2_offset = 16000;
743 845
744 /* Initialize chip */ 846 /* Initialize chip */
745 lm63_init_client(new_client); 847 lm63_init_client(new_client);
746 848
747 /* Register sysfs hooks */ 849 /* Register sysfs hooks */
748 err = sysfs_create_group(&new_client->dev.kobj, &lm63_group); 850 err = sysfs_create_group(&new_client->dev.kobj, &lm63_group);
749 if (err) 851 if (err)
750 goto exit_free; 852 goto exit_free;
751 if (data->config & 0x04) { /* tachometer enabled */ 853 if (data->config & 0x04) { /* tachometer enabled */
752 err = sysfs_create_group(&new_client->dev.kobj, 854 err = sysfs_create_group(&new_client->dev.kobj,
753 &lm63_group_fan1); 855 &lm63_group_fan1);
754 if (err) 856 if (err)
755 goto exit_remove_files; 857 goto exit_remove_files;
756 } 858 }
757 if (data->kind == lm96163) { 859 if (data->kind == lm96163) {
758 err = device_create_file(&new_client->dev, 860 err = device_create_file(&new_client->dev,
759 &dev_attr_temp2_type); 861 &dev_attr_temp2_type);
760 if (err) 862 if (err)
761 goto exit_remove_files; 863 goto exit_remove_files;
762 } 864 }
763 865
764 data->hwmon_dev = hwmon_device_register(&new_client->dev); 866 data->hwmon_dev = hwmon_device_register(&new_client->dev);
765 if (IS_ERR(data->hwmon_dev)) { 867 if (IS_ERR(data->hwmon_dev)) {
766 err = PTR_ERR(data->hwmon_dev); 868 err = PTR_ERR(data->hwmon_dev);
767 goto exit_remove_files; 869 goto exit_remove_files;
768 } 870 }
769 871
770 return 0; 872 return 0;
771 873
772 exit_remove_files: 874 exit_remove_files:
773 device_remove_file(&new_client->dev, &dev_attr_temp2_type); 875 device_remove_file(&new_client->dev, &dev_attr_temp2_type);
774 sysfs_remove_group(&new_client->dev.kobj, &lm63_group); 876 sysfs_remove_group(&new_client->dev.kobj, &lm63_group);
775 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1); 877 sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1);
776 exit_free: 878 exit_free:
777 kfree(data); 879 kfree(data);
778 exit: 880 exit:
779 return err; 881 return err;
780 } 882 }
781 883
782 /* 884 /*
783 * Ideally we shouldn't have to initialize anything, since the BIOS 885 * Ideally we shouldn't have to initialize anything, since the BIOS
784 * should have taken care of everything 886 * should have taken care of everything
785 */ 887 */
786 static void lm63_init_client(struct i2c_client *client) 888 static void lm63_init_client(struct i2c_client *client)
787 { 889 {
788 struct lm63_data *data = i2c_get_clientdata(client); 890 struct lm63_data *data = i2c_get_clientdata(client);
789 u8 convrate; 891 u8 convrate;
790 892
791 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); 893 data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1);
792 data->config_fan = i2c_smbus_read_byte_data(client, 894 data->config_fan = i2c_smbus_read_byte_data(client,
793 LM63_REG_CONFIG_FAN); 895 LM63_REG_CONFIG_FAN);
794 896
795 /* Start converting if needed */ 897 /* Start converting if needed */
796 if (data->config & 0x40) { /* standby */ 898 if (data->config & 0x40) { /* standby */
797 dev_dbg(&client->dev, "Switching to operational mode\n"); 899 dev_dbg(&client->dev, "Switching to operational mode\n");
798 data->config &= 0xA7; 900 data->config &= 0xA7;
799 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, 901 i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1,
800 data->config); 902 data->config);
801 } 903 }
802 /* Tachometer is always enabled on LM64 */ 904 /* Tachometer is always enabled on LM64 */
803 if (data->kind == lm64) 905 if (data->kind == lm64)
804 data->config |= 0x04; 906 data->config |= 0x04;
805 907
806 /* We may need pwm1_freq before ever updating the client data */ 908 /* We may need pwm1_freq before ever updating the client data */
807 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); 909 data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
808 if (data->pwm1_freq == 0) 910 if (data->pwm1_freq == 0)
809 data->pwm1_freq = 1; 911 data->pwm1_freq = 1;
810 912
811 switch (data->kind) { 913 switch (data->kind) {
812 case lm63: 914 case lm63:
813 case lm64: 915 case lm64:
814 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ; 916 data->max_convrate_hz = LM63_MAX_CONVRATE_HZ;
815 break; 917 break;
816 case lm96163: 918 case lm96163:
817 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ; 919 data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ;
818 data->trutherm 920 data->trutherm
819 = i2c_smbus_read_byte_data(client, 921 = i2c_smbus_read_byte_data(client,
820 LM96163_REG_TRUTHERM) & 0x02; 922 LM96163_REG_TRUTHERM) & 0x02;
821 break; 923 break;
822 } 924 }
823 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE); 925 convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE);
824 if (unlikely(convrate > LM63_MAX_CONVRATE)) 926 if (unlikely(convrate > LM63_MAX_CONVRATE))
825 convrate = LM63_MAX_CONVRATE; 927 convrate = LM63_MAX_CONVRATE;
826 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, 928 data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz,
827 convrate); 929 convrate);
828 930
829 /* 931 /*
830 * For LM96163, check if high resolution PWM 932 * For LM96163, check if high resolution PWM
831 * and unsigned temperature format is enabled. 933 * and unsigned temperature format is enabled.
832 */ 934 */
833 if (data->kind == lm96163) { 935 if (data->kind == lm96163) {
834 u8 config_enhanced 936 u8 config_enhanced
835 = i2c_smbus_read_byte_data(client, 937 = i2c_smbus_read_byte_data(client,
836 LM96163_REG_CONFIG_ENHANCED); 938 LM96163_REG_CONFIG_ENHANCED);
939 if (config_enhanced & 0x20)
940 data->lut_temp_highres = true;
837 if ((config_enhanced & 0x10) 941 if ((config_enhanced & 0x10)
838 && !(data->config_fan & 0x08) && data->pwm1_freq == 8) 942 && !(data->config_fan & 0x08) && data->pwm1_freq == 8)
839 data->pwm_highres = true; 943 data->pwm_highres = true;
840 if (config_enhanced & 0x08) 944 if (config_enhanced & 0x08)
841 data->remote_unsigned = true; 945 data->remote_unsigned = true;
842 } 946 }
843 947
844 /* Show some debug info about the LM63 configuration */ 948 /* Show some debug info about the LM63 configuration */
845 if (data->kind == lm63) 949 if (data->kind == lm63)
846 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", 950 dev_dbg(&client->dev, "Alert/tach pin configured for %s\n",
847 (data->config & 0x04) ? "tachometer input" : 951 (data->config & 0x04) ? "tachometer input" :
848 "alert output"); 952 "alert output");
849 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", 953 dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n",
850 (data->config_fan & 0x08) ? "1.4" : "360", 954 (data->config_fan & 0x08) ? "1.4" : "360",
851 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); 955 ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq);
852 dev_dbg(&client->dev, "PWM output active %s, %s mode\n", 956 dev_dbg(&client->dev, "PWM output active %s, %s mode\n",
853 (data->config_fan & 0x10) ? "low" : "high", 957 (data->config_fan & 0x10) ? "low" : "high",
854 (data->config_fan & 0x20) ? "manual" : "auto"); 958 (data->config_fan & 0x20) ? "manual" : "auto");
855 } 959 }
856 960
857 static int lm63_remove(struct i2c_client *client) 961 static int lm63_remove(struct i2c_client *client)
858 { 962 {
859 struct lm63_data *data = i2c_get_clientdata(client); 963 struct lm63_data *data = i2c_get_clientdata(client);
860 964
861 hwmon_device_unregister(data->hwmon_dev); 965 hwmon_device_unregister(data->hwmon_dev);
862 device_remove_file(&client->dev, &dev_attr_temp2_type); 966 device_remove_file(&client->dev, &dev_attr_temp2_type);
863 sysfs_remove_group(&client->dev.kobj, &lm63_group); 967 sysfs_remove_group(&client->dev.kobj, &lm63_group);
864 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); 968 sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1);
865 969
866 kfree(data); 970 kfree(data);
867 return 0; 971 return 0;
868 } 972 }
869 973
870 static struct lm63_data *lm63_update_device(struct device *dev) 974 static struct lm63_data *lm63_update_device(struct device *dev)
871 { 975 {
872 struct i2c_client *client = to_i2c_client(dev); 976 struct i2c_client *client = to_i2c_client(dev);
873 struct lm63_data *data = i2c_get_clientdata(client); 977 struct lm63_data *data = i2c_get_clientdata(client);
874 unsigned long next_update; 978 unsigned long next_update;
979 int i;
875 980
876 mutex_lock(&data->update_lock); 981 mutex_lock(&data->update_lock);
877 982
878 next_update = data->last_updated 983 next_update = data->last_updated
879 + msecs_to_jiffies(data->update_interval) + 1; 984 + msecs_to_jiffies(data->update_interval) + 1;
880 985
881 if (time_after(jiffies, next_update) || !data->valid) { 986 if (time_after(jiffies, next_update) || !data->valid) {
882 if (data->config & 0x04) { /* tachometer enabled */ 987 if (data->config & 0x04) { /* tachometer enabled */
883 /* order matters for fan1_input */ 988 /* order matters for fan1_input */
884 data->fan[0] = i2c_smbus_read_byte_data(client, 989 data->fan[0] = i2c_smbus_read_byte_data(client,
885 LM63_REG_TACH_COUNT_LSB) & 0xFC; 990 LM63_REG_TACH_COUNT_LSB) & 0xFC;
886 data->fan[0] |= i2c_smbus_read_byte_data(client, 991 data->fan[0] |= i2c_smbus_read_byte_data(client,
887 LM63_REG_TACH_COUNT_MSB) << 8; 992 LM63_REG_TACH_COUNT_MSB) << 8;
888 data->fan[1] = (i2c_smbus_read_byte_data(client, 993 data->fan[1] = (i2c_smbus_read_byte_data(client,
889 LM63_REG_TACH_LIMIT_LSB) & 0xFC) 994 LM63_REG_TACH_LIMIT_LSB) & 0xFC)
890 | (i2c_smbus_read_byte_data(client, 995 | (i2c_smbus_read_byte_data(client,
891 LM63_REG_TACH_LIMIT_MSB) << 8); 996 LM63_REG_TACH_LIMIT_MSB) << 8);
892 } 997 }
893 998
894 data->pwm1_freq = i2c_smbus_read_byte_data(client, 999 data->pwm1_freq = i2c_smbus_read_byte_data(client,
895 LM63_REG_PWM_FREQ); 1000 LM63_REG_PWM_FREQ);
896 if (data->pwm1_freq == 0) 1001 if (data->pwm1_freq == 0)
897 data->pwm1_freq = 1; 1002 data->pwm1_freq = 1;
898 data->pwm1_value = i2c_smbus_read_byte_data(client, 1003 data->pwm1[0] = i2c_smbus_read_byte_data(client,
899 LM63_REG_PWM_VALUE); 1004 LM63_REG_PWM_VALUE);
900 1005
901 data->temp8[0] = i2c_smbus_read_byte_data(client, 1006 data->temp8[0] = i2c_smbus_read_byte_data(client,
902 LM63_REG_LOCAL_TEMP); 1007 LM63_REG_LOCAL_TEMP);
903 data->temp8[1] = i2c_smbus_read_byte_data(client, 1008 data->temp8[1] = i2c_smbus_read_byte_data(client,
904 LM63_REG_LOCAL_HIGH); 1009 LM63_REG_LOCAL_HIGH);
905 1010
906 /* order matters for temp2_input */ 1011 /* order matters for temp2_input */
907 data->temp11[0] = i2c_smbus_read_byte_data(client, 1012 data->temp11[0] = i2c_smbus_read_byte_data(client,
908 LM63_REG_REMOTE_TEMP_MSB) << 8; 1013 LM63_REG_REMOTE_TEMP_MSB) << 8;
909 data->temp11[0] |= i2c_smbus_read_byte_data(client, 1014 data->temp11[0] |= i2c_smbus_read_byte_data(client,
910 LM63_REG_REMOTE_TEMP_LSB); 1015 LM63_REG_REMOTE_TEMP_LSB);
911 data->temp11[1] = (i2c_smbus_read_byte_data(client, 1016 data->temp11[1] = (i2c_smbus_read_byte_data(client,
912 LM63_REG_REMOTE_LOW_MSB) << 8) 1017 LM63_REG_REMOTE_LOW_MSB) << 8)
913 | i2c_smbus_read_byte_data(client, 1018 | i2c_smbus_read_byte_data(client,
914 LM63_REG_REMOTE_LOW_LSB); 1019 LM63_REG_REMOTE_LOW_LSB);
915 data->temp11[2] = (i2c_smbus_read_byte_data(client, 1020 data->temp11[2] = (i2c_smbus_read_byte_data(client,
916 LM63_REG_REMOTE_HIGH_MSB) << 8) 1021 LM63_REG_REMOTE_HIGH_MSB) << 8)
917 | i2c_smbus_read_byte_data(client, 1022 | i2c_smbus_read_byte_data(client,
918 LM63_REG_REMOTE_HIGH_LSB); 1023 LM63_REG_REMOTE_HIGH_LSB);
919 data->temp11[3] = (i2c_smbus_read_byte_data(client, 1024 data->temp11[3] = (i2c_smbus_read_byte_data(client,
920 LM63_REG_REMOTE_OFFSET_MSB) << 8) 1025 LM63_REG_REMOTE_OFFSET_MSB) << 8)
921 | i2c_smbus_read_byte_data(client, 1026 | i2c_smbus_read_byte_data(client,
922 LM63_REG_REMOTE_OFFSET_LSB); 1027 LM63_REG_REMOTE_OFFSET_LSB);
923 1028
924 if (data->kind == lm96163) 1029 if (data->kind == lm96163)
925 data->temp11u = (i2c_smbus_read_byte_data(client, 1030 data->temp11u = (i2c_smbus_read_byte_data(client,
926 LM96163_REG_REMOTE_TEMP_U_MSB) << 8) 1031 LM96163_REG_REMOTE_TEMP_U_MSB) << 8)
927 | i2c_smbus_read_byte_data(client, 1032 | i2c_smbus_read_byte_data(client,
928 LM96163_REG_REMOTE_TEMP_U_LSB); 1033 LM96163_REG_REMOTE_TEMP_U_LSB);
929 1034
930 data->temp8[2] = i2c_smbus_read_byte_data(client, 1035 data->temp8[2] = i2c_smbus_read_byte_data(client,
931 LM63_REG_REMOTE_TCRIT); 1036 LM63_REG_REMOTE_TCRIT);
932 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, 1037 data->temp2_crit_hyst = i2c_smbus_read_byte_data(client,
933 LM63_REG_REMOTE_TCRIT_HYST); 1038 LM63_REG_REMOTE_TCRIT_HYST);
934 1039
935 data->alarms = i2c_smbus_read_byte_data(client, 1040 data->alarms = i2c_smbus_read_byte_data(client,
936 LM63_REG_ALERT_STATUS) & 0x7F; 1041 LM63_REG_ALERT_STATUS) & 0x7F;
937 1042
938 data->last_updated = jiffies; 1043 data->last_updated = jiffies;
939 data->valid = 1; 1044 data->valid = 1;
1045 }
1046
1047 if (time_after(jiffies, data->lut_last_updated + 5 * HZ) ||
1048 !data->lut_valid) {
1049 for (i = 0; i < 8; i++) {
1050 data->pwm1[1 + i] = i2c_smbus_read_byte_data(client,
1051 LM63_REG_LUT_PWM(i));
1052 data->temp8[3 + i] = i2c_smbus_read_byte_data(client,
1053 LM63_REG_LUT_TEMP(i));
1054 }
1055 data->lut_temp_hyst = i2c_smbus_read_byte_data(client,
1056 LM63_REG_LUT_TEMP_HYST);
1057
1058 data->lut_last_updated = jiffies;
1059 data->lut_valid = 1;
940 } 1060 }
941 1061
942 mutex_unlock(&data->update_lock); 1062 mutex_unlock(&data->update_lock);
943 1063
944 return data; 1064 return data;
945 } 1065 }
946 1066
947 static int __init sensors_lm63_init(void) 1067 static int __init sensors_lm63_init(void)
948 { 1068 {
949 return i2c_add_driver(&lm63_driver); 1069 return i2c_add_driver(&lm63_driver);
950 } 1070 }
951 1071
952 static void __exit sensors_lm63_exit(void) 1072 static void __exit sensors_lm63_exit(void)
953 { 1073 {
954 i2c_del_driver(&lm63_driver); 1074 i2c_del_driver(&lm63_driver);
955 } 1075 }
956 1076
957 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); 1077 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
958 MODULE_DESCRIPTION("LM63 driver"); 1078 MODULE_DESCRIPTION("LM63 driver");
959 MODULE_LICENSE("GPL"); 1079 MODULE_LICENSE("GPL");
960 1080
961 module_init(sensors_lm63_init); 1081 module_init(sensors_lm63_init);
962 module_exit(sensors_lm63_exit); 1082 module_exit(sensors_lm63_exit);
963 1083