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