]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * lm63.c - driver for the National Semiconductor LM63 temperature sensor | |
3 | * with integrated fan control | |
d5957be2 | 4 | * Copyright (C) 2004-2008 Jean Delvare <khali@linux-fr.org> |
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; |
1da177e4 | 160 | char valid; /* zero until following fields are valid */ |
d216f680 | 161 | char lut_valid; /* zero until lut fields are valid */ |
1da177e4 | 162 | unsigned long last_updated; /* in jiffies */ |
d216f680 | 163 | unsigned long lut_last_updated; /* in jiffies */ |
04738b2b | 164 | enum chips kind; |
2778fb13 | 165 | int temp2_offset; |
1da177e4 | 166 | |
04738b2b GR |
167 | int update_interval; /* in milliseconds */ |
168 | int max_convrate_hz; | |
2fe28ab5 | 169 | int lut_size; /* 8 or 12 */ |
04738b2b | 170 | |
1da177e4 LT |
171 | /* registers values */ |
172 | u8 config, config_fan; | |
bc51ae11 JD |
173 | u16 fan[2]; /* 0: input |
174 | 1: low limit */ | |
1da177e4 | 175 | u8 pwm1_freq; |
2fe28ab5 JD |
176 | u8 pwm1[13]; /* 0: current output |
177 | 1-12: lookup table */ | |
178 | s8 temp8[15]; /* 0: local input | |
bc51ae11 | 179 | 1: local high limit |
d216f680 | 180 | 2: remote critical limit |
2fe28ab5 | 181 | 3-14: lookup table */ |
786375f7 | 182 | s16 temp11[4]; /* 0: remote input |
bc51ae11 | 183 | 1: remote low limit |
786375f7 GR |
184 | 2: remote high limit |
185 | 3: remote offset */ | |
e872c91e | 186 | u16 temp11u; /* remote input (unsigned) */ |
1da177e4 | 187 | u8 temp2_crit_hyst; |
d216f680 | 188 | u8 lut_temp_hyst; |
1da177e4 | 189 | u8 alarms; |
210961c4 | 190 | bool pwm_highres; |
d216f680 | 191 | bool lut_temp_highres; |
e872c91e | 192 | bool remote_unsigned; /* true if unsigned remote upper limits */ |
f496b2d4 | 193 | bool trutherm; |
1da177e4 LT |
194 | }; |
195 | ||
e872c91e GR |
196 | static inline int temp8_from_reg(struct lm63_data *data, int nr) |
197 | { | |
198 | if (data->remote_unsigned) | |
199 | return TEMP8_FROM_REG((u8)data->temp8[nr]); | |
200 | return TEMP8_FROM_REG(data->temp8[nr]); | |
201 | } | |
202 | ||
d216f680 JD |
203 | static inline int lut_temp_from_reg(struct lm63_data *data, int nr) |
204 | { | |
205 | return data->temp8[nr] * (data->lut_temp_highres ? 500 : 1000); | |
206 | } | |
207 | ||
dac27dce JD |
208 | static struct lm63_data *lm63_update_device(struct device *dev) |
209 | { | |
210 | struct i2c_client *client = to_i2c_client(dev); | |
211 | struct lm63_data *data = i2c_get_clientdata(client); | |
212 | unsigned long next_update; | |
213 | int i; | |
214 | ||
215 | mutex_lock(&data->update_lock); | |
216 | ||
217 | next_update = data->last_updated | |
218 | + msecs_to_jiffies(data->update_interval) + 1; | |
219 | ||
220 | if (time_after(jiffies, next_update) || !data->valid) { | |
221 | if (data->config & 0x04) { /* tachometer enabled */ | |
222 | /* order matters for fan1_input */ | |
223 | data->fan[0] = i2c_smbus_read_byte_data(client, | |
224 | LM63_REG_TACH_COUNT_LSB) & 0xFC; | |
225 | data->fan[0] |= i2c_smbus_read_byte_data(client, | |
226 | LM63_REG_TACH_COUNT_MSB) << 8; | |
227 | data->fan[1] = (i2c_smbus_read_byte_data(client, | |
228 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) | |
229 | | (i2c_smbus_read_byte_data(client, | |
230 | LM63_REG_TACH_LIMIT_MSB) << 8); | |
231 | } | |
232 | ||
233 | data->pwm1_freq = i2c_smbus_read_byte_data(client, | |
234 | LM63_REG_PWM_FREQ); | |
235 | if (data->pwm1_freq == 0) | |
236 | data->pwm1_freq = 1; | |
237 | data->pwm1[0] = i2c_smbus_read_byte_data(client, | |
238 | LM63_REG_PWM_VALUE); | |
239 | ||
240 | data->temp8[0] = i2c_smbus_read_byte_data(client, | |
241 | LM63_REG_LOCAL_TEMP); | |
242 | data->temp8[1] = i2c_smbus_read_byte_data(client, | |
243 | LM63_REG_LOCAL_HIGH); | |
244 | ||
245 | /* order matters for temp2_input */ | |
246 | data->temp11[0] = i2c_smbus_read_byte_data(client, | |
247 | LM63_REG_REMOTE_TEMP_MSB) << 8; | |
248 | data->temp11[0] |= i2c_smbus_read_byte_data(client, | |
249 | LM63_REG_REMOTE_TEMP_LSB); | |
250 | data->temp11[1] = (i2c_smbus_read_byte_data(client, | |
251 | LM63_REG_REMOTE_LOW_MSB) << 8) | |
252 | | i2c_smbus_read_byte_data(client, | |
253 | LM63_REG_REMOTE_LOW_LSB); | |
254 | data->temp11[2] = (i2c_smbus_read_byte_data(client, | |
255 | LM63_REG_REMOTE_HIGH_MSB) << 8) | |
256 | | i2c_smbus_read_byte_data(client, | |
257 | LM63_REG_REMOTE_HIGH_LSB); | |
258 | data->temp11[3] = (i2c_smbus_read_byte_data(client, | |
259 | LM63_REG_REMOTE_OFFSET_MSB) << 8) | |
260 | | i2c_smbus_read_byte_data(client, | |
261 | LM63_REG_REMOTE_OFFSET_LSB); | |
262 | ||
263 | if (data->kind == lm96163) | |
264 | data->temp11u = (i2c_smbus_read_byte_data(client, | |
265 | LM96163_REG_REMOTE_TEMP_U_MSB) << 8) | |
266 | | i2c_smbus_read_byte_data(client, | |
267 | LM96163_REG_REMOTE_TEMP_U_LSB); | |
268 | ||
269 | data->temp8[2] = i2c_smbus_read_byte_data(client, | |
270 | LM63_REG_REMOTE_TCRIT); | |
271 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, | |
272 | LM63_REG_REMOTE_TCRIT_HYST); | |
273 | ||
274 | data->alarms = i2c_smbus_read_byte_data(client, | |
275 | LM63_REG_ALERT_STATUS) & 0x7F; | |
276 | ||
277 | data->last_updated = jiffies; | |
278 | data->valid = 1; | |
279 | } | |
280 | ||
281 | if (time_after(jiffies, data->lut_last_updated + 5 * HZ) || | |
282 | !data->lut_valid) { | |
283 | for (i = 0; i < data->lut_size; i++) { | |
284 | data->pwm1[1 + i] = i2c_smbus_read_byte_data(client, | |
285 | LM63_REG_LUT_PWM(i)); | |
286 | data->temp8[3 + i] = i2c_smbus_read_byte_data(client, | |
287 | LM63_REG_LUT_TEMP(i)); | |
288 | } | |
289 | data->lut_temp_hyst = i2c_smbus_read_byte_data(client, | |
290 | LM63_REG_LUT_TEMP_HYST); | |
291 | ||
292 | data->lut_last_updated = jiffies; | |
293 | data->lut_valid = 1; | |
294 | } | |
295 | ||
296 | mutex_unlock(&data->update_lock); | |
297 | ||
298 | return data; | |
299 | } | |
300 | ||
1da177e4 LT |
301 | /* |
302 | * Sysfs callback functions and files | |
303 | */ | |
304 | ||
bc51ae11 JD |
305 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
306 | char *buf) | |
307 | { | |
308 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
309 | struct lm63_data *data = lm63_update_device(dev); | |
310 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); | |
1da177e4 | 311 | } |
1da177e4 | 312 | |
bc51ae11 JD |
313 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
314 | const char *buf, size_t count) | |
1da177e4 LT |
315 | { |
316 | struct i2c_client *client = to_i2c_client(dev); | |
317 | struct lm63_data *data = i2c_get_clientdata(client); | |
662bda28 GR |
318 | unsigned long val; |
319 | int err; | |
320 | ||
321 | err = kstrtoul(buf, 10, &val); | |
322 | if (err) | |
323 | return err; | |
1da177e4 | 324 | |
9a61bf63 | 325 | mutex_lock(&data->update_lock); |
bc51ae11 | 326 | data->fan[1] = FAN_TO_REG(val); |
1da177e4 | 327 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
bc51ae11 | 328 | data->fan[1] & 0xFF); |
1da177e4 | 329 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
bc51ae11 | 330 | data->fan[1] >> 8); |
9a61bf63 | 331 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
332 | return count; |
333 | } | |
334 | ||
d216f680 | 335 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr, |
bc51ae11 | 336 | char *buf) |
1da177e4 | 337 | { |
d216f680 | 338 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
1da177e4 | 339 | struct lm63_data *data = lm63_update_device(dev); |
d216f680 | 340 | int nr = attr->index; |
210961c4 GR |
341 | int pwm; |
342 | ||
343 | if (data->pwm_highres) | |
d216f680 | 344 | pwm = data->pwm1[nr]; |
210961c4 | 345 | else |
d216f680 JD |
346 | pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ? |
347 | 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) / | |
210961c4 GR |
348 | (2 * data->pwm1_freq); |
349 | ||
350 | return sprintf(buf, "%d\n", pwm); | |
1da177e4 LT |
351 | } |
352 | ||
bc51ae11 JD |
353 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, |
354 | const char *buf, size_t count) | |
1da177e4 LT |
355 | { |
356 | struct i2c_client *client = to_i2c_client(dev); | |
357 | struct lm63_data *data = i2c_get_clientdata(client); | |
358 | unsigned long val; | |
662bda28 GR |
359 | int err; |
360 | ||
1da177e4 LT |
361 | if (!(data->config_fan & 0x20)) /* register is read-only */ |
362 | return -EPERM; | |
363 | ||
662bda28 GR |
364 | err = kstrtoul(buf, 10, &val); |
365 | if (err) | |
366 | return err; | |
367 | ||
210961c4 | 368 | val = SENSORS_LIMIT(val, 0, 255); |
9a61bf63 | 369 | mutex_lock(&data->update_lock); |
d216f680 JD |
370 | data->pwm1[0] = data->pwm_highres ? val : |
371 | (val * data->pwm1_freq * 2 + 127) / 255; | |
372 | i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1[0]); | |
9a61bf63 | 373 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
374 | return count; |
375 | } | |
376 | ||
662bda28 GR |
377 | static ssize_t show_pwm1_enable(struct device *dev, |
378 | struct device_attribute *dummy, char *buf) | |
1da177e4 LT |
379 | { |
380 | struct lm63_data *data = lm63_update_device(dev); | |
381 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); | |
382 | } | |
383 | ||
2778fb13 DE |
384 | /* |
385 | * There are 8bit registers for both local(temp1) and remote(temp2) sensor. | |
386 | * For remote sensor registers temp2_offset has to be considered, | |
387 | * for local sensor it must not. | |
388 | * So we need separate 8bit accessors for local and remote sensor. | |
389 | */ | |
390 | static ssize_t show_local_temp8(struct device *dev, | |
391 | struct device_attribute *devattr, | |
392 | char *buf) | |
bc51ae11 JD |
393 | { |
394 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
395 | struct lm63_data *data = lm63_update_device(dev); | |
396 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); | |
1da177e4 | 397 | } |
bc51ae11 | 398 | |
2778fb13 DE |
399 | static ssize_t show_remote_temp8(struct device *dev, |
400 | struct device_attribute *devattr, | |
401 | char *buf) | |
402 | { | |
403 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
404 | struct lm63_data *data = lm63_update_device(dev); | |
e872c91e | 405 | return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index) |
2778fb13 DE |
406 | + data->temp2_offset); |
407 | } | |
408 | ||
d216f680 JD |
409 | static ssize_t show_lut_temp(struct device *dev, |
410 | struct device_attribute *devattr, | |
411 | char *buf) | |
412 | { | |
413 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
414 | struct lm63_data *data = lm63_update_device(dev); | |
415 | return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index) | |
416 | + data->temp2_offset); | |
417 | } | |
418 | ||
94e55df4 GR |
419 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, |
420 | const char *buf, size_t count) | |
bc51ae11 | 421 | { |
94e55df4 | 422 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
bc51ae11 JD |
423 | struct i2c_client *client = to_i2c_client(dev); |
424 | struct lm63_data *data = i2c_get_clientdata(client); | |
94e55df4 GR |
425 | int nr = attr->index; |
426 | int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH; | |
662bda28 GR |
427 | long val; |
428 | int err; | |
94e55df4 | 429 | int temp; |
662bda28 GR |
430 | |
431 | err = kstrtol(buf, 10, &val); | |
432 | if (err) | |
433 | return err; | |
bc51ae11 | 434 | |
9a61bf63 | 435 | mutex_lock(&data->update_lock); |
94e55df4 GR |
436 | if (nr == 2) { |
437 | if (data->remote_unsigned) | |
438 | temp = TEMP8U_TO_REG(val - data->temp2_offset); | |
439 | else | |
440 | temp = TEMP8_TO_REG(val - data->temp2_offset); | |
441 | } else { | |
442 | temp = TEMP8_TO_REG(val); | |
443 | } | |
444 | data->temp8[nr] = temp; | |
445 | i2c_smbus_write_byte_data(client, reg, temp); | |
9a61bf63 | 446 | mutex_unlock(&data->update_lock); |
bc51ae11 | 447 | return count; |
1da177e4 | 448 | } |
bc51ae11 JD |
449 | |
450 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | |
451 | char *buf) | |
452 | { | |
453 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
454 | struct lm63_data *data = lm63_update_device(dev); | |
e872c91e GR |
455 | int nr = attr->index; |
456 | int temp; | |
457 | ||
458 | if (!nr) { | |
459 | /* | |
460 | * Use unsigned temperature unless its value is zero. | |
461 | * If it is zero, use signed temperature. | |
462 | */ | |
463 | if (data->temp11u) | |
464 | temp = TEMP11_FROM_REG(data->temp11u); | |
465 | else | |
466 | temp = TEMP11_FROM_REG(data->temp11[nr]); | |
467 | } else { | |
468 | if (data->remote_unsigned && nr == 2) | |
469 | temp = TEMP11_FROM_REG((u16)data->temp11[nr]); | |
470 | else | |
471 | temp = TEMP11_FROM_REG(data->temp11[nr]); | |
472 | } | |
473 | return sprintf(buf, "%d\n", temp + data->temp2_offset); | |
1da177e4 | 474 | } |
bc51ae11 JD |
475 | |
476 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | |
477 | const char *buf, size_t count) | |
478 | { | |
786375f7 | 479 | static const u8 reg[6] = { |
bc51ae11 JD |
480 | LM63_REG_REMOTE_LOW_MSB, |
481 | LM63_REG_REMOTE_LOW_LSB, | |
482 | LM63_REG_REMOTE_HIGH_MSB, | |
483 | LM63_REG_REMOTE_HIGH_LSB, | |
786375f7 GR |
484 | LM63_REG_REMOTE_OFFSET_MSB, |
485 | LM63_REG_REMOTE_OFFSET_LSB, | |
bc51ae11 JD |
486 | }; |
487 | ||
488 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
489 | struct i2c_client *client = to_i2c_client(dev); | |
490 | struct lm63_data *data = i2c_get_clientdata(client); | |
662bda28 GR |
491 | long val; |
492 | int err; | |
bc51ae11 JD |
493 | int nr = attr->index; |
494 | ||
662bda28 GR |
495 | err = kstrtol(buf, 10, &val); |
496 | if (err) | |
497 | return err; | |
498 | ||
9a61bf63 | 499 | mutex_lock(&data->update_lock); |
e872c91e GR |
500 | if (data->remote_unsigned && nr == 2) |
501 | data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset); | |
502 | else | |
503 | data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset); | |
504 | ||
bc51ae11 JD |
505 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], |
506 | data->temp11[nr] >> 8); | |
507 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], | |
508 | data->temp11[nr] & 0xff); | |
9a61bf63 | 509 | mutex_unlock(&data->update_lock); |
bc51ae11 | 510 | return count; |
1da177e4 | 511 | } |
1da177e4 | 512 | |
662bda28 GR |
513 | /* |
514 | * Hysteresis register holds a relative value, while we want to present | |
515 | * an absolute to user-space | |
516 | */ | |
517 | static ssize_t show_temp2_crit_hyst(struct device *dev, | |
518 | struct device_attribute *dummy, char *buf) | |
1da177e4 LT |
519 | { |
520 | struct lm63_data *data = lm63_update_device(dev); | |
e872c91e | 521 | return sprintf(buf, "%d\n", temp8_from_reg(data, 2) |
2778fb13 | 522 | + data->temp2_offset |
1da177e4 LT |
523 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); |
524 | } | |
525 | ||
d216f680 JD |
526 | static ssize_t show_lut_temp_hyst(struct device *dev, |
527 | struct device_attribute *devattr, char *buf) | |
528 | { | |
529 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
530 | struct lm63_data *data = lm63_update_device(dev); | |
531 | ||
532 | return sprintf(buf, "%d\n", lut_temp_from_reg(data, attr->index) | |
533 | + data->temp2_offset | |
534 | - TEMP8_FROM_REG(data->lut_temp_hyst)); | |
535 | } | |
536 | ||
662bda28 GR |
537 | /* |
538 | * And now the other way around, user-space provides an absolute | |
539 | * hysteresis value and we have to store a relative one | |
540 | */ | |
541 | static ssize_t set_temp2_crit_hyst(struct device *dev, | |
542 | struct device_attribute *dummy, | |
bc51ae11 | 543 | const char *buf, size_t count) |
1da177e4 LT |
544 | { |
545 | struct i2c_client *client = to_i2c_client(dev); | |
546 | struct lm63_data *data = i2c_get_clientdata(client); | |
662bda28 GR |
547 | long val; |
548 | int err; | |
1da177e4 LT |
549 | long hyst; |
550 | ||
662bda28 GR |
551 | err = kstrtol(buf, 10, &val); |
552 | if (err) | |
553 | return err; | |
554 | ||
9a61bf63 | 555 | mutex_lock(&data->update_lock); |
e872c91e | 556 | hyst = temp8_from_reg(data, 2) + data->temp2_offset - val; |
1da177e4 LT |
557 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
558 | HYST_TO_REG(hyst)); | |
9a61bf63 | 559 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
560 | return count; |
561 | } | |
562 | ||
04738b2b GR |
563 | /* |
564 | * Set conversion rate. | |
565 | * client->update_lock must be held when calling this function. | |
566 | */ | |
567 | static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data, | |
568 | unsigned int interval) | |
569 | { | |
570 | int i; | |
571 | unsigned int update_interval; | |
572 | ||
573 | /* Shift calculations to avoid rounding errors */ | |
574 | interval <<= 6; | |
575 | ||
576 | /* find the nearest update rate */ | |
577 | update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000 | |
578 | / data->max_convrate_hz; | |
579 | for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1) | |
580 | if (interval >= update_interval * 3 / 4) | |
581 | break; | |
582 | ||
583 | i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i); | |
584 | data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i); | |
585 | } | |
586 | ||
587 | static ssize_t show_update_interval(struct device *dev, | |
588 | struct device_attribute *attr, char *buf) | |
589 | { | |
590 | struct lm63_data *data = dev_get_drvdata(dev); | |
591 | ||
592 | return sprintf(buf, "%u\n", data->update_interval); | |
593 | } | |
594 | ||
595 | static ssize_t set_update_interval(struct device *dev, | |
596 | struct device_attribute *attr, | |
597 | const char *buf, size_t count) | |
598 | { | |
599 | struct i2c_client *client = to_i2c_client(dev); | |
600 | struct lm63_data *data = i2c_get_clientdata(client); | |
601 | unsigned long val; | |
602 | int err; | |
603 | ||
604 | err = kstrtoul(buf, 10, &val); | |
605 | if (err) | |
606 | return err; | |
607 | ||
608 | mutex_lock(&data->update_lock); | |
609 | lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000)); | |
610 | mutex_unlock(&data->update_lock); | |
611 | ||
612 | return count; | |
613 | } | |
614 | ||
f496b2d4 GR |
615 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, |
616 | char *buf) | |
617 | { | |
618 | struct i2c_client *client = to_i2c_client(dev); | |
619 | struct lm63_data *data = i2c_get_clientdata(client); | |
620 | ||
621 | return sprintf(buf, data->trutherm ? "1\n" : "2\n"); | |
622 | } | |
623 | ||
624 | static ssize_t set_type(struct device *dev, struct device_attribute *attr, | |
625 | const char *buf, size_t count) | |
626 | { | |
627 | struct i2c_client *client = to_i2c_client(dev); | |
628 | struct lm63_data *data = i2c_get_clientdata(client); | |
629 | unsigned long val; | |
630 | int ret; | |
631 | u8 reg; | |
632 | ||
633 | ret = kstrtoul(buf, 10, &val); | |
634 | if (ret < 0) | |
635 | return ret; | |
636 | if (val != 1 && val != 2) | |
637 | return -EINVAL; | |
638 | ||
639 | mutex_lock(&data->update_lock); | |
640 | data->trutherm = val == 1; | |
641 | reg = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM) & ~0x02; | |
642 | i2c_smbus_write_byte_data(client, LM96163_REG_TRUTHERM, | |
643 | reg | (data->trutherm ? 0x02 : 0x00)); | |
644 | data->valid = 0; | |
645 | mutex_unlock(&data->update_lock); | |
646 | ||
647 | return count; | |
648 | } | |
649 | ||
bc51ae11 JD |
650 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
651 | char *buf) | |
1da177e4 LT |
652 | { |
653 | struct lm63_data *data = lm63_update_device(dev); | |
654 | return sprintf(buf, "%u\n", data->alarms); | |
655 | } | |
656 | ||
2d45771e JD |
657 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, |
658 | char *buf) | |
659 | { | |
660 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
661 | struct lm63_data *data = lm63_update_device(dev); | |
662 | int bitnr = attr->index; | |
663 | ||
664 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
665 | } | |
666 | ||
bc51ae11 JD |
667 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
668 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, | |
669 | set_fan, 1); | |
1da177e4 | 670 | |
d216f680 | 671 | static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1, 0); |
1da177e4 | 672 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); |
d216f680 JD |
673 | static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO, show_pwm1, NULL, 1); |
674 | static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IRUGO, | |
675 | show_lut_temp, NULL, 3); | |
676 | static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp_hyst, S_IRUGO, | |
677 | show_lut_temp_hyst, NULL, 3); | |
678 | static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO, show_pwm1, NULL, 2); | |
679 | static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, | |
680 | show_lut_temp, NULL, 4); | |
681 | static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp_hyst, S_IRUGO, | |
682 | show_lut_temp_hyst, NULL, 4); | |
683 | static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO, show_pwm1, NULL, 3); | |
684 | static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp, S_IRUGO, | |
685 | show_lut_temp, NULL, 5); | |
686 | static SENSOR_DEVICE_ATTR(pwm1_auto_point3_temp_hyst, S_IRUGO, | |
687 | show_lut_temp_hyst, NULL, 5); | |
688 | static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO, show_pwm1, NULL, 4); | |
689 | static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp, S_IRUGO, | |
690 | show_lut_temp, NULL, 6); | |
691 | static SENSOR_DEVICE_ATTR(pwm1_auto_point4_temp_hyst, S_IRUGO, | |
692 | show_lut_temp_hyst, NULL, 6); | |
693 | static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm1, NULL, 5); | |
694 | static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp, S_IRUGO, | |
695 | show_lut_temp, NULL, 7); | |
696 | static SENSOR_DEVICE_ATTR(pwm1_auto_point5_temp_hyst, S_IRUGO, | |
697 | show_lut_temp_hyst, NULL, 7); | |
698 | static SENSOR_DEVICE_ATTR(pwm1_auto_point6_pwm, S_IRUGO, show_pwm1, NULL, 6); | |
699 | static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp, S_IRUGO, | |
700 | show_lut_temp, NULL, 8); | |
701 | static SENSOR_DEVICE_ATTR(pwm1_auto_point6_temp_hyst, S_IRUGO, | |
702 | show_lut_temp_hyst, NULL, 8); | |
703 | static SENSOR_DEVICE_ATTR(pwm1_auto_point7_pwm, S_IRUGO, show_pwm1, NULL, 7); | |
704 | static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp, S_IRUGO, | |
705 | show_lut_temp, NULL, 9); | |
706 | static SENSOR_DEVICE_ATTR(pwm1_auto_point7_temp_hyst, S_IRUGO, | |
707 | show_lut_temp_hyst, NULL, 9); | |
708 | static SENSOR_DEVICE_ATTR(pwm1_auto_point8_pwm, S_IRUGO, show_pwm1, NULL, 8); | |
709 | static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp, S_IRUGO, | |
710 | show_lut_temp, NULL, 10); | |
711 | static SENSOR_DEVICE_ATTR(pwm1_auto_point8_temp_hyst, S_IRUGO, | |
712 | show_lut_temp_hyst, NULL, 10); | |
2fe28ab5 JD |
713 | static SENSOR_DEVICE_ATTR(pwm1_auto_point9_pwm, S_IRUGO, show_pwm1, NULL, 9); |
714 | static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp, S_IRUGO, | |
715 | show_lut_temp, NULL, 11); | |
716 | static SENSOR_DEVICE_ATTR(pwm1_auto_point9_temp_hyst, S_IRUGO, | |
717 | show_lut_temp_hyst, NULL, 11); | |
718 | static SENSOR_DEVICE_ATTR(pwm1_auto_point10_pwm, S_IRUGO, show_pwm1, NULL, 10); | |
719 | static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp, S_IRUGO, | |
720 | show_lut_temp, NULL, 12); | |
721 | static SENSOR_DEVICE_ATTR(pwm1_auto_point10_temp_hyst, S_IRUGO, | |
722 | show_lut_temp_hyst, NULL, 12); | |
723 | static SENSOR_DEVICE_ATTR(pwm1_auto_point11_pwm, S_IRUGO, show_pwm1, NULL, 11); | |
724 | static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp, S_IRUGO, | |
725 | show_lut_temp, NULL, 13); | |
726 | static SENSOR_DEVICE_ATTR(pwm1_auto_point11_temp_hyst, S_IRUGO, | |
727 | show_lut_temp_hyst, NULL, 13); | |
728 | static SENSOR_DEVICE_ATTR(pwm1_auto_point12_pwm, S_IRUGO, show_pwm1, NULL, 12); | |
729 | static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp, S_IRUGO, | |
730 | show_lut_temp, NULL, 14); | |
731 | static SENSOR_DEVICE_ATTR(pwm1_auto_point12_temp_hyst, S_IRUGO, | |
732 | show_lut_temp_hyst, NULL, 14); | |
1da177e4 | 733 | |
2778fb13 DE |
734 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0); |
735 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8, | |
94e55df4 | 736 | set_temp8, 1); |
1da177e4 | 737 | |
bc51ae11 JD |
738 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
739 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | |
740 | set_temp11, 1); | |
741 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | |
742 | set_temp11, 2); | |
786375f7 GR |
743 | static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, |
744 | set_temp11, 3); | |
2778fb13 | 745 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8, |
94e55df4 | 746 | set_temp8, 2); |
1da177e4 LT |
747 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
748 | set_temp2_crit_hyst); | |
749 | ||
f496b2d4 GR |
750 | static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type); |
751 | ||
2d45771e JD |
752 | /* Individual alarm files */ |
753 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); | |
754 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | |
7817a39e | 755 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
2d45771e JD |
756 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
757 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
758 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | |
759 | /* Raw alarm file for compatibility */ | |
1da177e4 LT |
760 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
761 | ||
04738b2b GR |
762 | static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval, |
763 | set_update_interval); | |
764 | ||
0e39e01c | 765 | static struct attribute *lm63_attributes[] = { |
d216f680 | 766 | &sensor_dev_attr_pwm1.dev_attr.attr, |
0e39e01c | 767 | &dev_attr_pwm1_enable.attr, |
d216f680 JD |
768 | &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr, |
769 | &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr, | |
770 | &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr, | |
771 | &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr, | |
772 | &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr, | |
773 | &sensor_dev_attr_pwm1_auto_point2_temp_hyst.dev_attr.attr, | |
774 | &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr, | |
775 | &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr, | |
776 | &sensor_dev_attr_pwm1_auto_point3_temp_hyst.dev_attr.attr, | |
777 | &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr, | |
778 | &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr, | |
779 | &sensor_dev_attr_pwm1_auto_point4_temp_hyst.dev_attr.attr, | |
780 | &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr, | |
781 | &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr, | |
782 | &sensor_dev_attr_pwm1_auto_point5_temp_hyst.dev_attr.attr, | |
783 | &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr, | |
784 | &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr, | |
785 | &sensor_dev_attr_pwm1_auto_point6_temp_hyst.dev_attr.attr, | |
786 | &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr, | |
787 | &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr, | |
788 | &sensor_dev_attr_pwm1_auto_point7_temp_hyst.dev_attr.attr, | |
789 | &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr, | |
790 | &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr, | |
791 | &sensor_dev_attr_pwm1_auto_point8_temp_hyst.dev_attr.attr, | |
792 | ||
0e39e01c JD |
793 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
794 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
795 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
796 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
797 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
786375f7 | 798 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
0e39e01c JD |
799 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
800 | &dev_attr_temp2_crit_hyst.attr, | |
801 | ||
802 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
7817a39e | 803 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
0e39e01c JD |
804 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
805 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
806 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
807 | &dev_attr_alarms.attr, | |
04738b2b | 808 | &dev_attr_update_interval.attr, |
0e39e01c JD |
809 | NULL |
810 | }; | |
811 | ||
2fe28ab5 JD |
812 | static struct attribute *lm63_attributes_extra_lut[] = { |
813 | &sensor_dev_attr_pwm1_auto_point9_pwm.dev_attr.attr, | |
814 | &sensor_dev_attr_pwm1_auto_point9_temp.dev_attr.attr, | |
815 | &sensor_dev_attr_pwm1_auto_point9_temp_hyst.dev_attr.attr, | |
816 | &sensor_dev_attr_pwm1_auto_point10_pwm.dev_attr.attr, | |
817 | &sensor_dev_attr_pwm1_auto_point10_temp.dev_attr.attr, | |
818 | &sensor_dev_attr_pwm1_auto_point10_temp_hyst.dev_attr.attr, | |
819 | &sensor_dev_attr_pwm1_auto_point11_pwm.dev_attr.attr, | |
820 | &sensor_dev_attr_pwm1_auto_point11_temp.dev_attr.attr, | |
821 | &sensor_dev_attr_pwm1_auto_point11_temp_hyst.dev_attr.attr, | |
822 | &sensor_dev_attr_pwm1_auto_point12_pwm.dev_attr.attr, | |
823 | &sensor_dev_attr_pwm1_auto_point12_temp.dev_attr.attr, | |
824 | &sensor_dev_attr_pwm1_auto_point12_temp_hyst.dev_attr.attr, | |
825 | NULL | |
826 | }; | |
827 | ||
828 | static const struct attribute_group lm63_group_extra_lut = { | |
829 | .attrs = lm63_attributes_extra_lut, | |
830 | }; | |
831 | ||
94e55df4 GR |
832 | /* |
833 | * On LM63, temp2_crit can be set only once, which should be job | |
834 | * of the bootloader. | |
835 | * On LM64, temp2_crit can always be set. | |
836 | * On LM96163, temp2_crit can be set if bit 1 of the configuration | |
837 | * register is true. | |
838 | */ | |
839 | static umode_t lm63_attribute_mode(struct kobject *kobj, | |
840 | struct attribute *attr, int index) | |
841 | { | |
842 | struct device *dev = container_of(kobj, struct device, kobj); | |
843 | struct i2c_client *client = to_i2c_client(dev); | |
844 | struct lm63_data *data = i2c_get_clientdata(client); | |
845 | ||
846 | if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr | |
847 | && (data->kind == lm64 || | |
848 | (data->kind == lm96163 && (data->config & 0x02)))) | |
849 | return attr->mode | S_IWUSR; | |
850 | ||
851 | return attr->mode; | |
852 | } | |
853 | ||
0e39e01c | 854 | static const struct attribute_group lm63_group = { |
94e55df4 | 855 | .is_visible = lm63_attribute_mode, |
0e39e01c JD |
856 | .attrs = lm63_attributes, |
857 | }; | |
858 | ||
859 | static struct attribute *lm63_attributes_fan1[] = { | |
860 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
861 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
862 | ||
863 | &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, | |
864 | NULL | |
865 | }; | |
866 | ||
867 | static const struct attribute_group lm63_group_fan1 = { | |
868 | .attrs = lm63_attributes_fan1, | |
869 | }; | |
870 | ||
1da177e4 LT |
871 | /* |
872 | * Real code | |
873 | */ | |
874 | ||
d5957be2 | 875 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
dac27dce | 876 | static int lm63_detect(struct i2c_client *client, |
d5957be2 | 877 | struct i2c_board_info *info) |
1da177e4 | 878 | { |
dac27dce | 879 | struct i2c_adapter *adapter = client->adapter; |
52df6440 JD |
880 | u8 man_id, chip_id, reg_config1, reg_config2; |
881 | u8 reg_alert_status, reg_alert_mask; | |
dac27dce | 882 | int address = client->addr; |
1da177e4 LT |
883 | |
884 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
d5957be2 | 885 | return -ENODEV; |
1da177e4 | 886 | |
dac27dce JD |
887 | man_id = i2c_smbus_read_byte_data(client, LM63_REG_MAN_ID); |
888 | chip_id = i2c_smbus_read_byte_data(client, LM63_REG_CHIP_ID); | |
52df6440 | 889 | |
dac27dce JD |
890 | reg_config1 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); |
891 | reg_config2 = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG2); | |
892 | reg_alert_status = i2c_smbus_read_byte_data(client, | |
52df6440 | 893 | LM63_REG_ALERT_STATUS); |
dac27dce | 894 | reg_alert_mask = i2c_smbus_read_byte_data(client, LM63_REG_ALERT_MASK); |
52df6440 JD |
895 | |
896 | if (man_id != 0x01 /* National Semiconductor */ | |
52df6440 JD |
897 | || (reg_config1 & 0x18) != 0x00 |
898 | || (reg_config2 & 0xF8) != 0x00 | |
899 | || (reg_alert_status & 0x20) != 0x00 | |
900 | || (reg_alert_mask & 0xA4) != 0xA4) { | |
901 | dev_dbg(&adapter->dev, | |
902 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", | |
903 | man_id, chip_id); | |
904 | return -ENODEV; | |
1da177e4 LT |
905 | } |
906 | ||
10f2ed31 MG |
907 | if (chip_id == 0x41 && address == 0x4c) |
908 | strlcpy(info->type, "lm63", I2C_NAME_SIZE); | |
909 | else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e)) | |
910 | strlcpy(info->type, "lm64", I2C_NAME_SIZE); | |
210961c4 GR |
911 | else if (chip_id == 0x49 && address == 0x4c) |
912 | strlcpy(info->type, "lm96163", I2C_NAME_SIZE); | |
10f2ed31 MG |
913 | else |
914 | return -ENODEV; | |
d5957be2 JD |
915 | |
916 | return 0; | |
917 | } | |
918 | ||
662bda28 GR |
919 | /* |
920 | * Ideally we shouldn't have to initialize anything, since the BIOS | |
921 | * should have taken care of everything | |
922 | */ | |
1da177e4 LT |
923 | static void lm63_init_client(struct i2c_client *client) |
924 | { | |
925 | struct lm63_data *data = i2c_get_clientdata(client); | |
04738b2b | 926 | u8 convrate; |
1da177e4 LT |
927 | |
928 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); | |
929 | data->config_fan = i2c_smbus_read_byte_data(client, | |
930 | LM63_REG_CONFIG_FAN); | |
931 | ||
932 | /* Start converting if needed */ | |
933 | if (data->config & 0x40) { /* standby */ | |
898eb71c | 934 | dev_dbg(&client->dev, "Switching to operational mode\n"); |
1da177e4 LT |
935 | data->config &= 0xA7; |
936 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, | |
937 | data->config); | |
938 | } | |
409c0b5b JD |
939 | /* Tachometer is always enabled on LM64 */ |
940 | if (data->kind == lm64) | |
941 | data->config |= 0x04; | |
1da177e4 LT |
942 | |
943 | /* We may need pwm1_freq before ever updating the client data */ | |
944 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); | |
945 | if (data->pwm1_freq == 0) | |
946 | data->pwm1_freq = 1; | |
947 | ||
04738b2b GR |
948 | switch (data->kind) { |
949 | case lm63: | |
950 | case lm64: | |
951 | data->max_convrate_hz = LM63_MAX_CONVRATE_HZ; | |
2fe28ab5 | 952 | data->lut_size = 8; |
04738b2b GR |
953 | break; |
954 | case lm96163: | |
955 | data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ; | |
2fe28ab5 | 956 | data->lut_size = 12; |
f496b2d4 GR |
957 | data->trutherm |
958 | = i2c_smbus_read_byte_data(client, | |
959 | LM96163_REG_TRUTHERM) & 0x02; | |
04738b2b GR |
960 | break; |
961 | } | |
962 | convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE); | |
963 | if (unlikely(convrate > LM63_MAX_CONVRATE)) | |
964 | convrate = LM63_MAX_CONVRATE; | |
965 | data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, | |
966 | convrate); | |
967 | ||
210961c4 | 968 | /* |
e872c91e GR |
969 | * For LM96163, check if high resolution PWM |
970 | * and unsigned temperature format is enabled. | |
210961c4 GR |
971 | */ |
972 | if (data->kind == lm96163) { | |
973 | u8 config_enhanced | |
974 | = i2c_smbus_read_byte_data(client, | |
975 | LM96163_REG_CONFIG_ENHANCED); | |
d216f680 JD |
976 | if (config_enhanced & 0x20) |
977 | data->lut_temp_highres = true; | |
210961c4 GR |
978 | if ((config_enhanced & 0x10) |
979 | && !(data->config_fan & 0x08) && data->pwm1_freq == 8) | |
980 | data->pwm_highres = true; | |
981 | if (config_enhanced & 0x08) | |
e872c91e | 982 | data->remote_unsigned = true; |
210961c4 GR |
983 | } |
984 | ||
1da177e4 | 985 | /* Show some debug info about the LM63 configuration */ |
409c0b5b JD |
986 | if (data->kind == lm63) |
987 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", | |
988 | (data->config & 0x04) ? "tachometer input" : | |
989 | "alert output"); | |
1da177e4 LT |
990 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", |
991 | (data->config_fan & 0x08) ? "1.4" : "360", | |
992 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); | |
993 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", | |
994 | (data->config_fan & 0x10) ? "low" : "high", | |
995 | (data->config_fan & 0x20) ? "manual" : "auto"); | |
996 | } | |
997 | ||
dac27dce JD |
998 | static int lm63_probe(struct i2c_client *client, |
999 | const struct i2c_device_id *id) | |
1da177e4 | 1000 | { |
dac27dce JD |
1001 | struct lm63_data *data; |
1002 | int err; | |
1da177e4 | 1003 | |
dac27dce JD |
1004 | data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL); |
1005 | if (!data) { | |
1006 | err = -ENOMEM; | |
1007 | goto exit; | |
1008 | } | |
1009 | ||
1010 | i2c_set_clientdata(client, data); | |
1011 | data->valid = 0; | |
1012 | mutex_init(&data->update_lock); | |
1013 | ||
1014 | /* Set the device type */ | |
1015 | data->kind = id->driver_data; | |
1016 | if (data->kind == lm64) | |
1017 | data->temp2_offset = 16000; | |
1018 | ||
1019 | /* Initialize chip */ | |
1020 | lm63_init_client(client); | |
1021 | ||
1022 | /* Register sysfs hooks */ | |
1023 | err = sysfs_create_group(&client->dev.kobj, &lm63_group); | |
1024 | if (err) | |
1025 | goto exit_free; | |
1026 | if (data->config & 0x04) { /* tachometer enabled */ | |
1027 | err = sysfs_create_group(&client->dev.kobj, &lm63_group_fan1); | |
1028 | if (err) | |
1029 | goto exit_remove_files; | |
1030 | } | |
1031 | if (data->kind == lm96163) { | |
1032 | err = device_create_file(&client->dev, &dev_attr_temp2_type); | |
1033 | if (err) | |
1034 | goto exit_remove_files; | |
1035 | ||
1036 | err = sysfs_create_group(&client->dev.kobj, | |
1037 | &lm63_group_extra_lut); | |
1038 | if (err) | |
1039 | goto exit_remove_files; | |
1040 | } | |
1041 | ||
1042 | data->hwmon_dev = hwmon_device_register(&client->dev); | |
1043 | if (IS_ERR(data->hwmon_dev)) { | |
1044 | err = PTR_ERR(data->hwmon_dev); | |
1045 | goto exit_remove_files; | |
1046 | } | |
1047 | ||
1048 | return 0; | |
1049 | ||
1050 | exit_remove_files: | |
0e39e01c JD |
1051 | sysfs_remove_group(&client->dev.kobj, &lm63_group); |
1052 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); | |
2fe28ab5 JD |
1053 | if (data->kind == lm96163) { |
1054 | device_remove_file(&client->dev, &dev_attr_temp2_type); | |
1055 | sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut); | |
1056 | } | |
dac27dce | 1057 | exit_free: |
943b0830 | 1058 | kfree(data); |
dac27dce JD |
1059 | exit: |
1060 | return err; | |
1da177e4 LT |
1061 | } |
1062 | ||
dac27dce | 1063 | static int lm63_remove(struct i2c_client *client) |
1da177e4 | 1064 | { |
1da177e4 | 1065 | struct lm63_data *data = i2c_get_clientdata(client); |
1da177e4 | 1066 | |
dac27dce JD |
1067 | hwmon_device_unregister(data->hwmon_dev); |
1068 | sysfs_remove_group(&client->dev.kobj, &lm63_group); | |
1069 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); | |
1070 | if (data->kind == lm96163) { | |
1071 | device_remove_file(&client->dev, &dev_attr_temp2_type); | |
1072 | sysfs_remove_group(&client->dev.kobj, &lm63_group_extra_lut); | |
1da177e4 LT |
1073 | } |
1074 | ||
dac27dce JD |
1075 | kfree(data); |
1076 | return 0; | |
1077 | } | |
d216f680 | 1078 | |
dac27dce JD |
1079 | /* |
1080 | * Driver data (common to all clients) | |
1081 | */ | |
d216f680 | 1082 | |
dac27dce JD |
1083 | static const struct i2c_device_id lm63_id[] = { |
1084 | { "lm63", lm63 }, | |
1085 | { "lm64", lm64 }, | |
1086 | { "lm96163", lm96163 }, | |
1087 | { } | |
1088 | }; | |
1089 | MODULE_DEVICE_TABLE(i2c, lm63_id); | |
1da177e4 | 1090 | |
dac27dce JD |
1091 | static struct i2c_driver lm63_driver = { |
1092 | .class = I2C_CLASS_HWMON, | |
1093 | .driver = { | |
1094 | .name = "lm63", | |
1095 | }, | |
1096 | .probe = lm63_probe, | |
1097 | .remove = lm63_remove, | |
1098 | .id_table = lm63_id, | |
1099 | .detect = lm63_detect, | |
1100 | .address_list = normal_i2c, | |
1101 | }; | |
1da177e4 | 1102 | |
f0967eea | 1103 | module_i2c_driver(lm63_driver); |
1da177e4 LT |
1104 | |
1105 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | |
1106 | MODULE_DESCRIPTION("LM63 driver"); | |
1107 | MODULE_LICENSE("GPL"); |