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 | |
54 | * Address is fully defined internally and cannot be changed. | |
55 | */ | |
56 | ||
10f2ed31 | 57 | static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; |
1da177e4 | 58 | |
1da177e4 LT |
59 | /* |
60 | * The LM63 registers | |
61 | */ | |
62 | ||
63 | #define LM63_REG_CONFIG1 0x03 | |
04738b2b | 64 | #define LM63_REG_CONVRATE 0x04 |
1da177e4 LT |
65 | #define LM63_REG_CONFIG2 0xBF |
66 | #define LM63_REG_CONFIG_FAN 0x4A | |
67 | ||
68 | #define LM63_REG_TACH_COUNT_MSB 0x47 | |
69 | #define LM63_REG_TACH_COUNT_LSB 0x46 | |
70 | #define LM63_REG_TACH_LIMIT_MSB 0x49 | |
71 | #define LM63_REG_TACH_LIMIT_LSB 0x48 | |
72 | ||
73 | #define LM63_REG_PWM_VALUE 0x4C | |
74 | #define LM63_REG_PWM_FREQ 0x4D | |
75 | ||
76 | #define LM63_REG_LOCAL_TEMP 0x00 | |
77 | #define LM63_REG_LOCAL_HIGH 0x05 | |
78 | ||
79 | #define LM63_REG_REMOTE_TEMP_MSB 0x01 | |
80 | #define LM63_REG_REMOTE_TEMP_LSB 0x10 | |
81 | #define LM63_REG_REMOTE_OFFSET_MSB 0x11 | |
82 | #define LM63_REG_REMOTE_OFFSET_LSB 0x12 | |
83 | #define LM63_REG_REMOTE_HIGH_MSB 0x07 | |
84 | #define LM63_REG_REMOTE_HIGH_LSB 0x13 | |
85 | #define LM63_REG_REMOTE_LOW_MSB 0x08 | |
86 | #define LM63_REG_REMOTE_LOW_LSB 0x14 | |
87 | #define LM63_REG_REMOTE_TCRIT 0x19 | |
88 | #define LM63_REG_REMOTE_TCRIT_HYST 0x21 | |
89 | ||
90 | #define LM63_REG_ALERT_STATUS 0x02 | |
91 | #define LM63_REG_ALERT_MASK 0x16 | |
92 | ||
93 | #define LM63_REG_MAN_ID 0xFE | |
94 | #define LM63_REG_CHIP_ID 0xFF | |
95 | ||
e872c91e GR |
96 | #define LM96163_REG_REMOTE_TEMP_U_MSB 0x31 |
97 | #define LM96163_REG_REMOTE_TEMP_U_LSB 0x32 | |
210961c4 GR |
98 | #define LM96163_REG_CONFIG_ENHANCED 0x45 |
99 | ||
04738b2b GR |
100 | #define LM63_MAX_CONVRATE 9 |
101 | ||
102 | #define LM63_MAX_CONVRATE_HZ 32 | |
103 | #define LM96163_MAX_CONVRATE_HZ 26 | |
104 | ||
1da177e4 LT |
105 | /* |
106 | * Conversions and various macros | |
107 | * For tachometer counts, the LM63 uses 16-bit values. | |
108 | * For local temperature and high limit, remote critical limit and hysteresis | |
44bbe87e | 109 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. |
1da177e4 | 110 | * For remote temperature, low and high limits, it uses signed 11-bit values |
44bbe87e | 111 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. |
2778fb13 DE |
112 | * For LM64 the actual remote diode temperature is 16 degree Celsius higher |
113 | * than the register reading. Remote temperature setpoints have to be | |
114 | * adapted accordingly. | |
1da177e4 LT |
115 | */ |
116 | ||
117 | #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ | |
118 | 5400000 / (reg)) | |
119 | #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ | |
120 | (5400000 / (val)) & 0xFFFC) | |
121 | #define TEMP8_FROM_REG(reg) ((reg) * 1000) | |
122 | #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \ | |
123 | (val) >= 127000 ? 127 : \ | |
124 | (val) < 0 ? ((val) - 500) / 1000 : \ | |
125 | ((val) + 500) / 1000) | |
94e55df4 GR |
126 | #define TEMP8U_TO_REG(val) ((val) <= 0 ? 0 : \ |
127 | (val) >= 255000 ? 255 : \ | |
128 | ((val) + 500) / 1000) | |
1da177e4 LT |
129 | #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) |
130 | #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \ | |
131 | (val) >= 127875 ? 0x7FE0 : \ | |
132 | (val) < 0 ? ((val) - 62) / 125 * 32 : \ | |
133 | ((val) + 62) / 125 * 32) | |
e872c91e GR |
134 | #define TEMP11U_TO_REG(val) ((val) <= 0 ? 0 : \ |
135 | (val) >= 255875 ? 0xFFE0 : \ | |
136 | ((val) + 62) / 125 * 32) | |
1da177e4 LT |
137 | #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ |
138 | (val) >= 127000 ? 127 : \ | |
139 | ((val) + 500) / 1000) | |
140 | ||
04738b2b GR |
141 | #define UPDATE_INTERVAL(max, rate) \ |
142 | ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max)) | |
143 | ||
1da177e4 LT |
144 | /* |
145 | * Functions declaration | |
146 | */ | |
147 | ||
d5957be2 JD |
148 | static int lm63_probe(struct i2c_client *client, |
149 | const struct i2c_device_id *id); | |
150 | static int lm63_remove(struct i2c_client *client); | |
1da177e4 LT |
151 | |
152 | static struct lm63_data *lm63_update_device(struct device *dev); | |
153 | ||
310ec792 | 154 | static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info); |
1da177e4 LT |
155 | static void lm63_init_client(struct i2c_client *client); |
156 | ||
210961c4 | 157 | enum chips { lm63, lm64, lm96163 }; |
10f2ed31 | 158 | |
1da177e4 LT |
159 | /* |
160 | * Driver data (common to all clients) | |
161 | */ | |
162 | ||
d5957be2 | 163 | static const struct i2c_device_id lm63_id[] = { |
10f2ed31 MG |
164 | { "lm63", lm63 }, |
165 | { "lm64", lm64 }, | |
210961c4 | 166 | { "lm96163", lm96163 }, |
d5957be2 JD |
167 | { } |
168 | }; | |
169 | MODULE_DEVICE_TABLE(i2c, lm63_id); | |
170 | ||
1da177e4 | 171 | static struct i2c_driver lm63_driver = { |
d5957be2 | 172 | .class = I2C_CLASS_HWMON, |
cdaf7934 | 173 | .driver = { |
cdaf7934 LR |
174 | .name = "lm63", |
175 | }, | |
d5957be2 JD |
176 | .probe = lm63_probe, |
177 | .remove = lm63_remove, | |
178 | .id_table = lm63_id, | |
179 | .detect = lm63_detect, | |
c3813d6a | 180 | .address_list = normal_i2c, |
1da177e4 LT |
181 | }; |
182 | ||
183 | /* | |
184 | * Client data (each client gets its own) | |
185 | */ | |
186 | ||
187 | struct lm63_data { | |
1beeffe4 | 188 | struct device *hwmon_dev; |
9a61bf63 | 189 | struct mutex update_lock; |
1da177e4 LT |
190 | char valid; /* zero until following fields are valid */ |
191 | unsigned long last_updated; /* in jiffies */ | |
04738b2b | 192 | enum chips kind; |
2778fb13 | 193 | int temp2_offset; |
1da177e4 | 194 | |
04738b2b GR |
195 | int update_interval; /* in milliseconds */ |
196 | int max_convrate_hz; | |
197 | ||
1da177e4 LT |
198 | /* registers values */ |
199 | u8 config, config_fan; | |
bc51ae11 JD |
200 | u16 fan[2]; /* 0: input |
201 | 1: low limit */ | |
1da177e4 LT |
202 | u8 pwm1_freq; |
203 | u8 pwm1_value; | |
bc51ae11 JD |
204 | s8 temp8[3]; /* 0: local input |
205 | 1: local high limit | |
206 | 2: remote critical limit */ | |
786375f7 | 207 | s16 temp11[4]; /* 0: remote input |
bc51ae11 | 208 | 1: remote low limit |
786375f7 GR |
209 | 2: remote high limit |
210 | 3: remote offset */ | |
e872c91e | 211 | u16 temp11u; /* remote input (unsigned) */ |
1da177e4 LT |
212 | u8 temp2_crit_hyst; |
213 | u8 alarms; | |
210961c4 | 214 | bool pwm_highres; |
e872c91e | 215 | bool remote_unsigned; /* true if unsigned remote upper limits */ |
1da177e4 LT |
216 | }; |
217 | ||
e872c91e GR |
218 | static inline int temp8_from_reg(struct lm63_data *data, int nr) |
219 | { | |
220 | if (data->remote_unsigned) | |
221 | return TEMP8_FROM_REG((u8)data->temp8[nr]); | |
222 | return TEMP8_FROM_REG(data->temp8[nr]); | |
223 | } | |
224 | ||
1da177e4 LT |
225 | /* |
226 | * Sysfs callback functions and files | |
227 | */ | |
228 | ||
bc51ae11 JD |
229 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
230 | char *buf) | |
231 | { | |
232 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
233 | struct lm63_data *data = lm63_update_device(dev); | |
234 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); | |
1da177e4 | 235 | } |
1da177e4 | 236 | |
bc51ae11 JD |
237 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
238 | const char *buf, size_t count) | |
1da177e4 LT |
239 | { |
240 | struct i2c_client *client = to_i2c_client(dev); | |
241 | struct lm63_data *data = i2c_get_clientdata(client); | |
662bda28 GR |
242 | unsigned long val; |
243 | int err; | |
244 | ||
245 | err = kstrtoul(buf, 10, &val); | |
246 | if (err) | |
247 | return err; | |
1da177e4 | 248 | |
9a61bf63 | 249 | mutex_lock(&data->update_lock); |
bc51ae11 | 250 | data->fan[1] = FAN_TO_REG(val); |
1da177e4 | 251 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
bc51ae11 | 252 | data->fan[1] & 0xFF); |
1da177e4 | 253 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
bc51ae11 | 254 | data->fan[1] >> 8); |
9a61bf63 | 255 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
256 | return count; |
257 | } | |
258 | ||
bc51ae11 JD |
259 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, |
260 | char *buf) | |
1da177e4 LT |
261 | { |
262 | struct lm63_data *data = lm63_update_device(dev); | |
210961c4 GR |
263 | int pwm; |
264 | ||
265 | if (data->pwm_highres) | |
266 | pwm = data->pwm1_value; | |
267 | else | |
268 | pwm = data->pwm1_value >= 2 * data->pwm1_freq ? | |
1da177e4 | 269 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / |
210961c4 GR |
270 | (2 * data->pwm1_freq); |
271 | ||
272 | return sprintf(buf, "%d\n", pwm); | |
1da177e4 LT |
273 | } |
274 | ||
bc51ae11 JD |
275 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, |
276 | const char *buf, size_t count) | |
1da177e4 LT |
277 | { |
278 | struct i2c_client *client = to_i2c_client(dev); | |
279 | struct lm63_data *data = i2c_get_clientdata(client); | |
280 | unsigned long val; | |
662bda28 GR |
281 | int err; |
282 | ||
1da177e4 LT |
283 | if (!(data->config_fan & 0x20)) /* register is read-only */ |
284 | return -EPERM; | |
285 | ||
662bda28 GR |
286 | err = kstrtoul(buf, 10, &val); |
287 | if (err) | |
288 | return err; | |
289 | ||
210961c4 | 290 | val = SENSORS_LIMIT(val, 0, 255); |
9a61bf63 | 291 | mutex_lock(&data->update_lock); |
210961c4 | 292 | data->pwm1_value = data->pwm_highres ? val : |
1da177e4 LT |
293 | (val * data->pwm1_freq * 2 + 127) / 255; |
294 | i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value); | |
9a61bf63 | 295 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
296 | return count; |
297 | } | |
298 | ||
662bda28 GR |
299 | static ssize_t show_pwm1_enable(struct device *dev, |
300 | struct device_attribute *dummy, char *buf) | |
1da177e4 LT |
301 | { |
302 | struct lm63_data *data = lm63_update_device(dev); | |
303 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); | |
304 | } | |
305 | ||
2778fb13 DE |
306 | /* |
307 | * There are 8bit registers for both local(temp1) and remote(temp2) sensor. | |
308 | * For remote sensor registers temp2_offset has to be considered, | |
309 | * for local sensor it must not. | |
310 | * So we need separate 8bit accessors for local and remote sensor. | |
311 | */ | |
312 | static ssize_t show_local_temp8(struct device *dev, | |
313 | struct device_attribute *devattr, | |
314 | char *buf) | |
bc51ae11 JD |
315 | { |
316 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
317 | struct lm63_data *data = lm63_update_device(dev); | |
318 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); | |
1da177e4 | 319 | } |
bc51ae11 | 320 | |
2778fb13 DE |
321 | static ssize_t show_remote_temp8(struct device *dev, |
322 | struct device_attribute *devattr, | |
323 | char *buf) | |
324 | { | |
325 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
326 | struct lm63_data *data = lm63_update_device(dev); | |
e872c91e | 327 | return sprintf(buf, "%d\n", temp8_from_reg(data, attr->index) |
2778fb13 DE |
328 | + data->temp2_offset); |
329 | } | |
330 | ||
94e55df4 GR |
331 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, |
332 | const char *buf, size_t count) | |
bc51ae11 | 333 | { |
94e55df4 | 334 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
bc51ae11 JD |
335 | struct i2c_client *client = to_i2c_client(dev); |
336 | struct lm63_data *data = i2c_get_clientdata(client); | |
94e55df4 GR |
337 | int nr = attr->index; |
338 | int reg = nr == 2 ? LM63_REG_REMOTE_TCRIT : LM63_REG_LOCAL_HIGH; | |
662bda28 GR |
339 | long val; |
340 | int err; | |
94e55df4 | 341 | int temp; |
662bda28 GR |
342 | |
343 | err = kstrtol(buf, 10, &val); | |
344 | if (err) | |
345 | return err; | |
bc51ae11 | 346 | |
9a61bf63 | 347 | mutex_lock(&data->update_lock); |
94e55df4 GR |
348 | if (nr == 2) { |
349 | if (data->remote_unsigned) | |
350 | temp = TEMP8U_TO_REG(val - data->temp2_offset); | |
351 | else | |
352 | temp = TEMP8_TO_REG(val - data->temp2_offset); | |
353 | } else { | |
354 | temp = TEMP8_TO_REG(val); | |
355 | } | |
356 | data->temp8[nr] = temp; | |
357 | i2c_smbus_write_byte_data(client, reg, temp); | |
9a61bf63 | 358 | mutex_unlock(&data->update_lock); |
bc51ae11 | 359 | return count; |
1da177e4 | 360 | } |
bc51ae11 JD |
361 | |
362 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | |
363 | char *buf) | |
364 | { | |
365 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
366 | struct lm63_data *data = lm63_update_device(dev); | |
e872c91e GR |
367 | int nr = attr->index; |
368 | int temp; | |
369 | ||
370 | if (!nr) { | |
371 | /* | |
372 | * Use unsigned temperature unless its value is zero. | |
373 | * If it is zero, use signed temperature. | |
374 | */ | |
375 | if (data->temp11u) | |
376 | temp = TEMP11_FROM_REG(data->temp11u); | |
377 | else | |
378 | temp = TEMP11_FROM_REG(data->temp11[nr]); | |
379 | } else { | |
380 | if (data->remote_unsigned && nr == 2) | |
381 | temp = TEMP11_FROM_REG((u16)data->temp11[nr]); | |
382 | else | |
383 | temp = TEMP11_FROM_REG(data->temp11[nr]); | |
384 | } | |
385 | return sprintf(buf, "%d\n", temp + data->temp2_offset); | |
1da177e4 | 386 | } |
bc51ae11 JD |
387 | |
388 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | |
389 | const char *buf, size_t count) | |
390 | { | |
786375f7 | 391 | static const u8 reg[6] = { |
bc51ae11 JD |
392 | LM63_REG_REMOTE_LOW_MSB, |
393 | LM63_REG_REMOTE_LOW_LSB, | |
394 | LM63_REG_REMOTE_HIGH_MSB, | |
395 | LM63_REG_REMOTE_HIGH_LSB, | |
786375f7 GR |
396 | LM63_REG_REMOTE_OFFSET_MSB, |
397 | LM63_REG_REMOTE_OFFSET_LSB, | |
bc51ae11 JD |
398 | }; |
399 | ||
400 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
401 | struct i2c_client *client = to_i2c_client(dev); | |
402 | struct lm63_data *data = i2c_get_clientdata(client); | |
662bda28 GR |
403 | long val; |
404 | int err; | |
bc51ae11 JD |
405 | int nr = attr->index; |
406 | ||
662bda28 GR |
407 | err = kstrtol(buf, 10, &val); |
408 | if (err) | |
409 | return err; | |
410 | ||
9a61bf63 | 411 | mutex_lock(&data->update_lock); |
e872c91e GR |
412 | if (data->remote_unsigned && nr == 2) |
413 | data->temp11[nr] = TEMP11U_TO_REG(val - data->temp2_offset); | |
414 | else | |
415 | data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset); | |
416 | ||
bc51ae11 JD |
417 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], |
418 | data->temp11[nr] >> 8); | |
419 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], | |
420 | data->temp11[nr] & 0xff); | |
9a61bf63 | 421 | mutex_unlock(&data->update_lock); |
bc51ae11 | 422 | return count; |
1da177e4 | 423 | } |
1da177e4 | 424 | |
662bda28 GR |
425 | /* |
426 | * Hysteresis register holds a relative value, while we want to present | |
427 | * an absolute to user-space | |
428 | */ | |
429 | static ssize_t show_temp2_crit_hyst(struct device *dev, | |
430 | struct device_attribute *dummy, char *buf) | |
1da177e4 LT |
431 | { |
432 | struct lm63_data *data = lm63_update_device(dev); | |
e872c91e | 433 | return sprintf(buf, "%d\n", temp8_from_reg(data, 2) |
2778fb13 | 434 | + data->temp2_offset |
1da177e4 LT |
435 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); |
436 | } | |
437 | ||
662bda28 GR |
438 | /* |
439 | * And now the other way around, user-space provides an absolute | |
440 | * hysteresis value and we have to store a relative one | |
441 | */ | |
442 | static ssize_t set_temp2_crit_hyst(struct device *dev, | |
443 | struct device_attribute *dummy, | |
bc51ae11 | 444 | const char *buf, size_t count) |
1da177e4 LT |
445 | { |
446 | struct i2c_client *client = to_i2c_client(dev); | |
447 | struct lm63_data *data = i2c_get_clientdata(client); | |
662bda28 GR |
448 | long val; |
449 | int err; | |
1da177e4 LT |
450 | long hyst; |
451 | ||
662bda28 GR |
452 | err = kstrtol(buf, 10, &val); |
453 | if (err) | |
454 | return err; | |
455 | ||
9a61bf63 | 456 | mutex_lock(&data->update_lock); |
e872c91e | 457 | hyst = temp8_from_reg(data, 2) + data->temp2_offset - val; |
1da177e4 LT |
458 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
459 | HYST_TO_REG(hyst)); | |
9a61bf63 | 460 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
461 | return count; |
462 | } | |
463 | ||
04738b2b GR |
464 | /* |
465 | * Set conversion rate. | |
466 | * client->update_lock must be held when calling this function. | |
467 | */ | |
468 | static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data, | |
469 | unsigned int interval) | |
470 | { | |
471 | int i; | |
472 | unsigned int update_interval; | |
473 | ||
474 | /* Shift calculations to avoid rounding errors */ | |
475 | interval <<= 6; | |
476 | ||
477 | /* find the nearest update rate */ | |
478 | update_interval = (1 << (LM63_MAX_CONVRATE + 6)) * 1000 | |
479 | / data->max_convrate_hz; | |
480 | for (i = 0; i < LM63_MAX_CONVRATE; i++, update_interval >>= 1) | |
481 | if (interval >= update_interval * 3 / 4) | |
482 | break; | |
483 | ||
484 | i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i); | |
485 | data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i); | |
486 | } | |
487 | ||
488 | static ssize_t show_update_interval(struct device *dev, | |
489 | struct device_attribute *attr, char *buf) | |
490 | { | |
491 | struct lm63_data *data = dev_get_drvdata(dev); | |
492 | ||
493 | return sprintf(buf, "%u\n", data->update_interval); | |
494 | } | |
495 | ||
496 | static ssize_t set_update_interval(struct device *dev, | |
497 | struct device_attribute *attr, | |
498 | const char *buf, size_t count) | |
499 | { | |
500 | struct i2c_client *client = to_i2c_client(dev); | |
501 | struct lm63_data *data = i2c_get_clientdata(client); | |
502 | unsigned long val; | |
503 | int err; | |
504 | ||
505 | err = kstrtoul(buf, 10, &val); | |
506 | if (err) | |
507 | return err; | |
508 | ||
509 | mutex_lock(&data->update_lock); | |
510 | lm63_set_convrate(client, data, SENSORS_LIMIT(val, 0, 100000)); | |
511 | mutex_unlock(&data->update_lock); | |
512 | ||
513 | return count; | |
514 | } | |
515 | ||
bc51ae11 JD |
516 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
517 | char *buf) | |
1da177e4 LT |
518 | { |
519 | struct lm63_data *data = lm63_update_device(dev); | |
520 | return sprintf(buf, "%u\n", data->alarms); | |
521 | } | |
522 | ||
2d45771e JD |
523 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, |
524 | char *buf) | |
525 | { | |
526 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
527 | struct lm63_data *data = lm63_update_device(dev); | |
528 | int bitnr = attr->index; | |
529 | ||
530 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); | |
531 | } | |
532 | ||
bc51ae11 JD |
533 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
534 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, | |
535 | set_fan, 1); | |
1da177e4 LT |
536 | |
537 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); | |
538 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); | |
539 | ||
2778fb13 DE |
540 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0); |
541 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8, | |
94e55df4 | 542 | set_temp8, 1); |
1da177e4 | 543 | |
bc51ae11 JD |
544 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
545 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | |
546 | set_temp11, 1); | |
547 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | |
548 | set_temp11, 2); | |
786375f7 GR |
549 | static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, |
550 | set_temp11, 3); | |
2778fb13 | 551 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8, |
94e55df4 | 552 | set_temp8, 2); |
1da177e4 LT |
553 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
554 | set_temp2_crit_hyst); | |
555 | ||
2d45771e JD |
556 | /* Individual alarm files */ |
557 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); | |
558 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | |
7817a39e | 559 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
2d45771e JD |
560 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
561 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
562 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | |
563 | /* Raw alarm file for compatibility */ | |
1da177e4 LT |
564 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
565 | ||
04738b2b GR |
566 | static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval, |
567 | set_update_interval); | |
568 | ||
0e39e01c JD |
569 | static struct attribute *lm63_attributes[] = { |
570 | &dev_attr_pwm1.attr, | |
571 | &dev_attr_pwm1_enable.attr, | |
572 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
573 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
574 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
575 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
576 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
786375f7 | 577 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
0e39e01c JD |
578 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
579 | &dev_attr_temp2_crit_hyst.attr, | |
580 | ||
581 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
7817a39e | 582 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
0e39e01c JD |
583 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
584 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
585 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
586 | &dev_attr_alarms.attr, | |
04738b2b | 587 | &dev_attr_update_interval.attr, |
0e39e01c JD |
588 | NULL |
589 | }; | |
590 | ||
94e55df4 GR |
591 | /* |
592 | * On LM63, temp2_crit can be set only once, which should be job | |
593 | * of the bootloader. | |
594 | * On LM64, temp2_crit can always be set. | |
595 | * On LM96163, temp2_crit can be set if bit 1 of the configuration | |
596 | * register is true. | |
597 | */ | |
598 | static umode_t lm63_attribute_mode(struct kobject *kobj, | |
599 | struct attribute *attr, int index) | |
600 | { | |
601 | struct device *dev = container_of(kobj, struct device, kobj); | |
602 | struct i2c_client *client = to_i2c_client(dev); | |
603 | struct lm63_data *data = i2c_get_clientdata(client); | |
604 | ||
605 | if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr | |
606 | && (data->kind == lm64 || | |
607 | (data->kind == lm96163 && (data->config & 0x02)))) | |
608 | return attr->mode | S_IWUSR; | |
609 | ||
610 | return attr->mode; | |
611 | } | |
612 | ||
0e39e01c | 613 | static const struct attribute_group lm63_group = { |
94e55df4 | 614 | .is_visible = lm63_attribute_mode, |
0e39e01c JD |
615 | .attrs = lm63_attributes, |
616 | }; | |
617 | ||
618 | static struct attribute *lm63_attributes_fan1[] = { | |
619 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
620 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
621 | ||
622 | &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, | |
623 | NULL | |
624 | }; | |
625 | ||
626 | static const struct attribute_group lm63_group_fan1 = { | |
627 | .attrs = lm63_attributes_fan1, | |
628 | }; | |
629 | ||
1da177e4 LT |
630 | /* |
631 | * Real code | |
632 | */ | |
633 | ||
d5957be2 | 634 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 635 | static int lm63_detect(struct i2c_client *new_client, |
d5957be2 | 636 | struct i2c_board_info *info) |
1da177e4 | 637 | { |
d5957be2 | 638 | struct i2c_adapter *adapter = new_client->adapter; |
52df6440 JD |
639 | u8 man_id, chip_id, reg_config1, reg_config2; |
640 | u8 reg_alert_status, reg_alert_mask; | |
10f2ed31 | 641 | int address = new_client->addr; |
1da177e4 LT |
642 | |
643 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
d5957be2 | 644 | return -ENODEV; |
1da177e4 | 645 | |
52df6440 JD |
646 | man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID); |
647 | chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID); | |
648 | ||
649 | reg_config1 = i2c_smbus_read_byte_data(new_client, | |
650 | LM63_REG_CONFIG1); | |
651 | reg_config2 = i2c_smbus_read_byte_data(new_client, | |
652 | LM63_REG_CONFIG2); | |
653 | reg_alert_status = i2c_smbus_read_byte_data(new_client, | |
654 | LM63_REG_ALERT_STATUS); | |
655 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, | |
656 | LM63_REG_ALERT_MASK); | |
657 | ||
658 | if (man_id != 0x01 /* National Semiconductor */ | |
52df6440 JD |
659 | || (reg_config1 & 0x18) != 0x00 |
660 | || (reg_config2 & 0xF8) != 0x00 | |
661 | || (reg_alert_status & 0x20) != 0x00 | |
662 | || (reg_alert_mask & 0xA4) != 0xA4) { | |
663 | dev_dbg(&adapter->dev, | |
664 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", | |
665 | man_id, chip_id); | |
666 | return -ENODEV; | |
1da177e4 LT |
667 | } |
668 | ||
10f2ed31 MG |
669 | if (chip_id == 0x41 && address == 0x4c) |
670 | strlcpy(info->type, "lm63", I2C_NAME_SIZE); | |
671 | else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e)) | |
672 | strlcpy(info->type, "lm64", I2C_NAME_SIZE); | |
210961c4 GR |
673 | else if (chip_id == 0x49 && address == 0x4c) |
674 | strlcpy(info->type, "lm96163", I2C_NAME_SIZE); | |
10f2ed31 MG |
675 | else |
676 | return -ENODEV; | |
d5957be2 JD |
677 | |
678 | return 0; | |
679 | } | |
680 | ||
681 | static int lm63_probe(struct i2c_client *new_client, | |
682 | const struct i2c_device_id *id) | |
683 | { | |
684 | struct lm63_data *data; | |
685 | int err; | |
686 | ||
687 | data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL); | |
688 | if (!data) { | |
689 | err = -ENOMEM; | |
690 | goto exit; | |
691 | } | |
692 | ||
693 | i2c_set_clientdata(new_client, data); | |
1da177e4 | 694 | data->valid = 0; |
9a61bf63 | 695 | mutex_init(&data->update_lock); |
1da177e4 | 696 | |
2778fb13 DE |
697 | /* Set the device type */ |
698 | data->kind = id->driver_data; | |
699 | if (data->kind == lm64) | |
700 | data->temp2_offset = 16000; | |
701 | ||
702 | /* Initialize chip */ | |
1da177e4 LT |
703 | lm63_init_client(new_client); |
704 | ||
705 | /* Register sysfs hooks */ | |
662bda28 GR |
706 | err = sysfs_create_group(&new_client->dev.kobj, &lm63_group); |
707 | if (err) | |
d5957be2 | 708 | goto exit_free; |
0e39e01c | 709 | if (data->config & 0x04) { /* tachometer enabled */ |
662bda28 GR |
710 | err = sysfs_create_group(&new_client->dev.kobj, |
711 | &lm63_group_fan1); | |
712 | if (err) | |
0e39e01c | 713 | goto exit_remove_files; |
943b0830 MH |
714 | } |
715 | ||
1beeffe4 TJ |
716 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
717 | if (IS_ERR(data->hwmon_dev)) { | |
718 | err = PTR_ERR(data->hwmon_dev); | |
0e39e01c | 719 | goto exit_remove_files; |
1da177e4 | 720 | } |
1da177e4 LT |
721 | |
722 | return 0; | |
723 | ||
0e39e01c JD |
724 | exit_remove_files: |
725 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group); | |
726 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1); | |
1da177e4 LT |
727 | exit_free: |
728 | kfree(data); | |
729 | exit: | |
730 | return err; | |
731 | } | |
732 | ||
662bda28 GR |
733 | /* |
734 | * Ideally we shouldn't have to initialize anything, since the BIOS | |
735 | * should have taken care of everything | |
736 | */ | |
1da177e4 LT |
737 | static void lm63_init_client(struct i2c_client *client) |
738 | { | |
739 | struct lm63_data *data = i2c_get_clientdata(client); | |
04738b2b | 740 | u8 convrate; |
1da177e4 LT |
741 | |
742 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); | |
743 | data->config_fan = i2c_smbus_read_byte_data(client, | |
744 | LM63_REG_CONFIG_FAN); | |
745 | ||
746 | /* Start converting if needed */ | |
747 | if (data->config & 0x40) { /* standby */ | |
898eb71c | 748 | dev_dbg(&client->dev, "Switching to operational mode\n"); |
1da177e4 LT |
749 | data->config &= 0xA7; |
750 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, | |
751 | data->config); | |
752 | } | |
753 | ||
754 | /* We may need pwm1_freq before ever updating the client data */ | |
755 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); | |
756 | if (data->pwm1_freq == 0) | |
757 | data->pwm1_freq = 1; | |
758 | ||
04738b2b GR |
759 | switch (data->kind) { |
760 | case lm63: | |
761 | case lm64: | |
762 | data->max_convrate_hz = LM63_MAX_CONVRATE_HZ; | |
763 | break; | |
764 | case lm96163: | |
765 | data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ; | |
766 | break; | |
767 | } | |
768 | convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE); | |
769 | if (unlikely(convrate > LM63_MAX_CONVRATE)) | |
770 | convrate = LM63_MAX_CONVRATE; | |
771 | data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, | |
772 | convrate); | |
773 | ||
210961c4 | 774 | /* |
e872c91e GR |
775 | * For LM96163, check if high resolution PWM |
776 | * and unsigned temperature format is enabled. | |
210961c4 GR |
777 | */ |
778 | if (data->kind == lm96163) { | |
779 | u8 config_enhanced | |
780 | = i2c_smbus_read_byte_data(client, | |
781 | LM96163_REG_CONFIG_ENHANCED); | |
782 | if ((config_enhanced & 0x10) | |
783 | && !(data->config_fan & 0x08) && data->pwm1_freq == 8) | |
784 | data->pwm_highres = true; | |
785 | if (config_enhanced & 0x08) | |
e872c91e | 786 | data->remote_unsigned = true; |
210961c4 GR |
787 | } |
788 | ||
1da177e4 LT |
789 | /* Show some debug info about the LM63 configuration */ |
790 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", | |
791 | (data->config & 0x04) ? "tachometer input" : | |
792 | "alert output"); | |
793 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", | |
794 | (data->config_fan & 0x08) ? "1.4" : "360", | |
795 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); | |
796 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", | |
797 | (data->config_fan & 0x10) ? "low" : "high", | |
798 | (data->config_fan & 0x20) ? "manual" : "auto"); | |
799 | } | |
800 | ||
d5957be2 | 801 | static int lm63_remove(struct i2c_client *client) |
1da177e4 | 802 | { |
943b0830 | 803 | struct lm63_data *data = i2c_get_clientdata(client); |
1da177e4 | 804 | |
1beeffe4 | 805 | hwmon_device_unregister(data->hwmon_dev); |
0e39e01c JD |
806 | sysfs_remove_group(&client->dev.kobj, &lm63_group); |
807 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); | |
943b0830 | 808 | |
943b0830 | 809 | kfree(data); |
1da177e4 LT |
810 | return 0; |
811 | } | |
812 | ||
813 | static struct lm63_data *lm63_update_device(struct device *dev) | |
814 | { | |
815 | struct i2c_client *client = to_i2c_client(dev); | |
816 | struct lm63_data *data = i2c_get_clientdata(client); | |
04738b2b | 817 | unsigned long next_update; |
1da177e4 | 818 | |
9a61bf63 | 819 | mutex_lock(&data->update_lock); |
1da177e4 | 820 | |
04738b2b GR |
821 | next_update = data->last_updated |
822 | + msecs_to_jiffies(data->update_interval) + 1; | |
823 | ||
824 | if (time_after(jiffies, next_update) || !data->valid) { | |
1da177e4 LT |
825 | if (data->config & 0x04) { /* tachometer enabled */ |
826 | /* order matters for fan1_input */ | |
bc51ae11 JD |
827 | data->fan[0] = i2c_smbus_read_byte_data(client, |
828 | LM63_REG_TACH_COUNT_LSB) & 0xFC; | |
829 | data->fan[0] |= i2c_smbus_read_byte_data(client, | |
830 | LM63_REG_TACH_COUNT_MSB) << 8; | |
831 | data->fan[1] = (i2c_smbus_read_byte_data(client, | |
832 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) | |
833 | | (i2c_smbus_read_byte_data(client, | |
834 | LM63_REG_TACH_LIMIT_MSB) << 8); | |
1da177e4 LT |
835 | } |
836 | ||
837 | data->pwm1_freq = i2c_smbus_read_byte_data(client, | |
838 | LM63_REG_PWM_FREQ); | |
839 | if (data->pwm1_freq == 0) | |
840 | data->pwm1_freq = 1; | |
841 | data->pwm1_value = i2c_smbus_read_byte_data(client, | |
842 | LM63_REG_PWM_VALUE); | |
843 | ||
bc51ae11 JD |
844 | data->temp8[0] = i2c_smbus_read_byte_data(client, |
845 | LM63_REG_LOCAL_TEMP); | |
846 | data->temp8[1] = i2c_smbus_read_byte_data(client, | |
847 | LM63_REG_LOCAL_HIGH); | |
1da177e4 LT |
848 | |
849 | /* order matters for temp2_input */ | |
bc51ae11 JD |
850 | data->temp11[0] = i2c_smbus_read_byte_data(client, |
851 | LM63_REG_REMOTE_TEMP_MSB) << 8; | |
852 | data->temp11[0] |= i2c_smbus_read_byte_data(client, | |
853 | LM63_REG_REMOTE_TEMP_LSB); | |
854 | data->temp11[1] = (i2c_smbus_read_byte_data(client, | |
1da177e4 LT |
855 | LM63_REG_REMOTE_LOW_MSB) << 8) |
856 | | i2c_smbus_read_byte_data(client, | |
857 | LM63_REG_REMOTE_LOW_LSB); | |
bc51ae11 JD |
858 | data->temp11[2] = (i2c_smbus_read_byte_data(client, |
859 | LM63_REG_REMOTE_HIGH_MSB) << 8) | |
860 | | i2c_smbus_read_byte_data(client, | |
861 | LM63_REG_REMOTE_HIGH_LSB); | |
786375f7 GR |
862 | data->temp11[3] = (i2c_smbus_read_byte_data(client, |
863 | LM63_REG_REMOTE_OFFSET_MSB) << 8) | |
864 | | i2c_smbus_read_byte_data(client, | |
865 | LM63_REG_REMOTE_OFFSET_LSB); | |
e872c91e GR |
866 | |
867 | if (data->kind == lm96163) | |
868 | data->temp11u = (i2c_smbus_read_byte_data(client, | |
869 | LM96163_REG_REMOTE_TEMP_U_MSB) << 8) | |
870 | | i2c_smbus_read_byte_data(client, | |
871 | LM96163_REG_REMOTE_TEMP_U_LSB); | |
872 | ||
bc51ae11 JD |
873 | data->temp8[2] = i2c_smbus_read_byte_data(client, |
874 | LM63_REG_REMOTE_TCRIT); | |
1da177e4 LT |
875 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, |
876 | LM63_REG_REMOTE_TCRIT_HYST); | |
877 | ||
878 | data->alarms = i2c_smbus_read_byte_data(client, | |
879 | LM63_REG_ALERT_STATUS) & 0x7F; | |
880 | ||
881 | data->last_updated = jiffies; | |
882 | data->valid = 1; | |
883 | } | |
884 | ||
9a61bf63 | 885 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
886 | |
887 | return data; | |
888 | } | |
889 | ||
890 | static int __init sensors_lm63_init(void) | |
891 | { | |
892 | return i2c_add_driver(&lm63_driver); | |
893 | } | |
894 | ||
895 | static void __exit sensors_lm63_exit(void) | |
896 | { | |
897 | i2c_del_driver(&lm63_driver); | |
898 | } | |
899 | ||
900 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | |
901 | MODULE_DESCRIPTION("LM63 driver"); | |
902 | MODULE_LICENSE("GPL"); | |
903 | ||
904 | module_init(sensors_lm63_init); | |
905 | module_exit(sensors_lm63_exit); |