]>
Commit | Line | Data |
---|---|---|
74ba9207 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * lm80.c - From lm_sensors, Linux kernel modules for hardware | |
1160631b | 4 | * monitoring |
1da177e4 | 5 | * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
1160631b | 6 | * and Philip Edelbrock <phil@netroedge.com> |
1da177e4 LT |
7 | * |
8 | * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org> | |
1da177e4 LT |
9 | */ |
10 | ||
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/init.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/jiffies.h> | |
15 | #include <linux/i2c.h> | |
943b0830 | 16 | #include <linux/hwmon.h> |
f8181762 | 17 | #include <linux/hwmon-sysfs.h> |
943b0830 | 18 | #include <linux/err.h> |
9a61bf63 | 19 | #include <linux/mutex.h> |
1da177e4 LT |
20 | |
21 | /* Addresses to scan */ | |
25e9c86d MH |
22 | static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, |
23 | 0x2e, 0x2f, I2C_CLIENT_END }; | |
1da177e4 | 24 | |
1da177e4 LT |
25 | /* Many LM80 constants specified below */ |
26 | ||
27 | /* The LM80 registers */ | |
28 | #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2) | |
29 | #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2) | |
30 | #define LM80_REG_IN(nr) (0x20 + (nr)) | |
31 | ||
32 | #define LM80_REG_FAN1 0x28 | |
33 | #define LM80_REG_FAN2 0x29 | |
34 | #define LM80_REG_FAN_MIN(nr) (0x3b + (nr)) | |
35 | ||
36 | #define LM80_REG_TEMP 0x27 | |
37 | #define LM80_REG_TEMP_HOT_MAX 0x38 | |
38 | #define LM80_REG_TEMP_HOT_HYST 0x39 | |
39 | #define LM80_REG_TEMP_OS_MAX 0x3a | |
40 | #define LM80_REG_TEMP_OS_HYST 0x3b | |
41 | ||
42 | #define LM80_REG_CONFIG 0x00 | |
43 | #define LM80_REG_ALARM1 0x01 | |
44 | #define LM80_REG_ALARM2 0x02 | |
45 | #define LM80_REG_MASK1 0x03 | |
46 | #define LM80_REG_MASK2 0x04 | |
47 | #define LM80_REG_FANDIV 0x05 | |
48 | #define LM80_REG_RES 0x06 | |
49 | ||
9908ad4c JD |
50 | #define LM96080_REG_CONV_RATE 0x07 |
51 | #define LM96080_REG_MAN_ID 0x3e | |
52 | #define LM96080_REG_DEV_ID 0x3f | |
53 | ||
1da177e4 | 54 | |
1160631b GR |
55 | /* |
56 | * Conversions. Rounding and limit checking is only done on the TO_REG | |
57 | * variants. Note that you should be a bit careful with which arguments | |
58 | * these macros are called: arguments may be evaluated more than once. | |
59 | * Fixing this is just not worth it. | |
60 | */ | |
1da177e4 | 61 | |
2a844c14 | 62 | #define IN_TO_REG(val) (clamp_val(((val) + 5) / 10, 0, 255)) |
66b3b1f7 | 63 | #define IN_FROM_REG(val) ((val) * 10) |
1da177e4 LT |
64 | |
65 | static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div) | |
66 | { | |
67 | if (rpm == 0) | |
68 | return 255; | |
2a844c14 GR |
69 | rpm = clamp_val(rpm, 1, 1000000); |
70 | return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); | |
1da177e4 LT |
71 | } |
72 | ||
66b3b1f7 FM |
73 | #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \ |
74 | (val) == 255 ? 0 : 1350000/((div) * (val))) | |
1da177e4 | 75 | |
688a3174 GR |
76 | #define TEMP_FROM_REG(reg) ((reg) * 125 / 32) |
77 | #define TEMP_TO_REG(temp) (DIV_ROUND_CLOSEST(clamp_val((temp), \ | |
78 | -128000, 127000), 1000) << 8) | |
1da177e4 LT |
79 | |
80 | #define DIV_FROM_REG(val) (1 << (val)) | |
81 | ||
9711bcdd GR |
82 | enum temp_index { |
83 | t_input = 0, | |
84 | t_hot_max, | |
85 | t_hot_hyst, | |
86 | t_os_max, | |
87 | t_os_hyst, | |
88 | t_num_temp | |
89 | }; | |
90 | ||
91 | static const u8 temp_regs[t_num_temp] = { | |
92 | [t_input] = LM80_REG_TEMP, | |
93 | [t_hot_max] = LM80_REG_TEMP_HOT_MAX, | |
94 | [t_hot_hyst] = LM80_REG_TEMP_HOT_HYST, | |
95 | [t_os_max] = LM80_REG_TEMP_OS_MAX, | |
96 | [t_os_hyst] = LM80_REG_TEMP_OS_HYST, | |
97 | }; | |
98 | ||
1adf3a3e GR |
99 | enum in_index { |
100 | i_input = 0, | |
101 | i_max, | |
102 | i_min, | |
103 | i_num_in | |
104 | }; | |
105 | ||
85b3ee07 GR |
106 | enum fan_index { |
107 | f_input, | |
108 | f_min, | |
109 | f_num_fan | |
110 | }; | |
111 | ||
1da177e4 LT |
112 | /* |
113 | * Client data (each client gets its own) | |
114 | */ | |
115 | ||
116 | struct lm80_data { | |
118c9a61 | 117 | struct i2c_client *client; |
9a61bf63 | 118 | struct mutex update_lock; |
96585f1a | 119 | char error; /* !=0 if error occurred during last update */ |
1da177e4 LT |
120 | char valid; /* !=0 if following fields are valid */ |
121 | unsigned long last_updated; /* In jiffies */ | |
122 | ||
1adf3a3e | 123 | u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */ |
85b3ee07 | 124 | u8 fan[f_num_fan][2]; /* Register value, 1st index enum fan_index */ |
1da177e4 | 125 | u8 fan_div[2]; /* Register encoding, shifted right */ |
9711bcdd | 126 | s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */ |
1da177e4 LT |
127 | u16 alarms; /* Register encoding, combined */ |
128 | }; | |
129 | ||
c254ffde GR |
130 | static int lm80_read_value(struct i2c_client *client, u8 reg) |
131 | { | |
132 | return i2c_smbus_read_byte_data(client, reg); | |
133 | } | |
1da177e4 | 134 | |
c254ffde GR |
135 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value) |
136 | { | |
137 | return i2c_smbus_write_byte_data(client, reg, value); | |
138 | } | |
1da177e4 | 139 | |
c254ffde GR |
140 | /* Called when we have found a new LM80 and after read errors */ |
141 | static void lm80_init_client(struct i2c_client *client) | |
142 | { | |
143 | /* | |
144 | * Reset all except Watchdog values and last conversion values | |
145 | * This sets fan-divs to 2, among others. This makes most other | |
146 | * initializations unnecessary | |
147 | */ | |
148 | lm80_write_value(client, LM80_REG_CONFIG, 0x80); | |
149 | /* Set 11-bit temperature resolution */ | |
150 | lm80_write_value(client, LM80_REG_RES, 0x08); | |
1da177e4 | 151 | |
c254ffde GR |
152 | /* Start monitoring */ |
153 | lm80_write_value(client, LM80_REG_CONFIG, 0x01); | |
154 | } | |
8c8bacc8 | 155 | |
c254ffde GR |
156 | static struct lm80_data *lm80_update_device(struct device *dev) |
157 | { | |
158 | struct lm80_data *data = dev_get_drvdata(dev); | |
159 | struct i2c_client *client = data->client; | |
160 | int i; | |
161 | int rv; | |
162 | int prev_rv; | |
163 | struct lm80_data *ret = data; | |
164 | ||
165 | mutex_lock(&data->update_lock); | |
166 | ||
167 | if (data->error) | |
168 | lm80_init_client(client); | |
169 | ||
170 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { | |
171 | dev_dbg(dev, "Starting lm80 update\n"); | |
172 | for (i = 0; i <= 6; i++) { | |
173 | rv = lm80_read_value(client, LM80_REG_IN(i)); | |
174 | if (rv < 0) | |
175 | goto abort; | |
176 | data->in[i_input][i] = rv; | |
177 | ||
178 | rv = lm80_read_value(client, LM80_REG_IN_MIN(i)); | |
179 | if (rv < 0) | |
180 | goto abort; | |
181 | data->in[i_min][i] = rv; | |
182 | ||
183 | rv = lm80_read_value(client, LM80_REG_IN_MAX(i)); | |
184 | if (rv < 0) | |
185 | goto abort; | |
186 | data->in[i_max][i] = rv; | |
187 | } | |
188 | ||
189 | rv = lm80_read_value(client, LM80_REG_FAN1); | |
190 | if (rv < 0) | |
191 | goto abort; | |
192 | data->fan[f_input][0] = rv; | |
193 | ||
194 | rv = lm80_read_value(client, LM80_REG_FAN_MIN(1)); | |
195 | if (rv < 0) | |
196 | goto abort; | |
197 | data->fan[f_min][0] = rv; | |
198 | ||
199 | rv = lm80_read_value(client, LM80_REG_FAN2); | |
200 | if (rv < 0) | |
201 | goto abort; | |
202 | data->fan[f_input][1] = rv; | |
203 | ||
204 | rv = lm80_read_value(client, LM80_REG_FAN_MIN(2)); | |
205 | if (rv < 0) | |
206 | goto abort; | |
207 | data->fan[f_min][1] = rv; | |
208 | ||
209 | prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP); | |
210 | if (rv < 0) | |
211 | goto abort; | |
212 | rv = lm80_read_value(client, LM80_REG_RES); | |
213 | if (rv < 0) | |
214 | goto abort; | |
215 | data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0); | |
216 | ||
217 | for (i = t_input + 1; i < t_num_temp; i++) { | |
218 | rv = lm80_read_value(client, temp_regs[i]); | |
219 | if (rv < 0) | |
220 | goto abort; | |
221 | data->temp[i] = rv << 8; | |
222 | } | |
223 | ||
224 | rv = lm80_read_value(client, LM80_REG_FANDIV); | |
225 | if (rv < 0) | |
226 | goto abort; | |
227 | data->fan_div[0] = (rv >> 2) & 0x03; | |
228 | data->fan_div[1] = (rv >> 4) & 0x03; | |
229 | ||
230 | prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1); | |
231 | if (rv < 0) | |
232 | goto abort; | |
233 | rv = lm80_read_value(client, LM80_REG_ALARM2); | |
234 | if (rv < 0) | |
235 | goto abort; | |
236 | data->alarms = prev_rv + (rv << 8); | |
237 | ||
238 | data->last_updated = jiffies; | |
239 | data->valid = 1; | |
240 | data->error = 0; | |
241 | } | |
242 | goto done; | |
243 | ||
244 | abort: | |
245 | ret = ERR_PTR(rv); | |
246 | data->valid = 0; | |
247 | data->error = 1; | |
248 | ||
249 | done: | |
250 | mutex_unlock(&data->update_lock); | |
251 | ||
252 | return ret; | |
253 | } | |
1da177e4 LT |
254 | |
255 | /* | |
256 | * Sysfs stuff | |
257 | */ | |
258 | ||
fdb97a02 | 259 | static ssize_t in_show(struct device *dev, struct device_attribute *attr, |
1adf3a3e GR |
260 | char *buf) |
261 | { | |
262 | struct lm80_data *data = lm80_update_device(dev); | |
263 | int index = to_sensor_dev_attr_2(attr)->index; | |
264 | int nr = to_sensor_dev_attr_2(attr)->nr; | |
265 | ||
266 | if (IS_ERR(data)) | |
267 | return PTR_ERR(data); | |
268 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index])); | |
1da177e4 | 269 | } |
1da177e4 | 270 | |
fdb97a02 GR |
271 | static ssize_t in_store(struct device *dev, struct device_attribute *attr, |
272 | const char *buf, size_t count) | |
1adf3a3e GR |
273 | { |
274 | struct lm80_data *data = dev_get_drvdata(dev); | |
275 | struct i2c_client *client = data->client; | |
276 | int index = to_sensor_dev_attr_2(attr)->index; | |
277 | int nr = to_sensor_dev_attr_2(attr)->nr; | |
278 | long val; | |
279 | u8 reg; | |
280 | int err = kstrtol(buf, 10, &val); | |
281 | if (err < 0) | |
282 | return err; | |
283 | ||
284 | reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index); | |
285 | ||
286 | mutex_lock(&data->update_lock); | |
287 | data->in[nr][index] = IN_TO_REG(val); | |
288 | lm80_write_value(client, reg, data->in[nr][index]); | |
289 | mutex_unlock(&data->update_lock); | |
290 | return count; | |
1da177e4 | 291 | } |
f8181762 | 292 | |
fdb97a02 | 293 | static ssize_t fan_show(struct device *dev, struct device_attribute *attr, |
85b3ee07 GR |
294 | char *buf) |
295 | { | |
296 | int index = to_sensor_dev_attr_2(attr)->index; | |
297 | int nr = to_sensor_dev_attr_2(attr)->nr; | |
298 | struct lm80_data *data = lm80_update_device(dev); | |
299 | if (IS_ERR(data)) | |
300 | return PTR_ERR(data); | |
301 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index], | |
302 | DIV_FROM_REG(data->fan_div[index]))); | |
1da177e4 | 303 | } |
1da177e4 | 304 | |
fdb97a02 GR |
305 | static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr, |
306 | char *buf) | |
f8181762 JD |
307 | { |
308 | int nr = to_sensor_dev_attr(attr)->index; | |
309 | struct lm80_data *data = lm80_update_device(dev); | |
2faaa931 FM |
310 | if (IS_ERR(data)) |
311 | return PTR_ERR(data); | |
f8181762 | 312 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
1da177e4 | 313 | } |
1da177e4 | 314 | |
fdb97a02 GR |
315 | static ssize_t fan_store(struct device *dev, struct device_attribute *attr, |
316 | const char *buf, size_t count) | |
f8181762 | 317 | { |
85b3ee07 GR |
318 | int index = to_sensor_dev_attr_2(attr)->index; |
319 | int nr = to_sensor_dev_attr_2(attr)->nr; | |
118c9a61 GR |
320 | struct lm80_data *data = dev_get_drvdata(dev); |
321 | struct i2c_client *client = data->client; | |
6a9e7c4c FM |
322 | unsigned long val; |
323 | int err = kstrtoul(buf, 10, &val); | |
324 | if (err < 0) | |
325 | return err; | |
f8181762 JD |
326 | |
327 | mutex_lock(&data->update_lock); | |
85b3ee07 GR |
328 | data->fan[nr][index] = FAN_TO_REG(val, |
329 | DIV_FROM_REG(data->fan_div[index])); | |
330 | lm80_write_value(client, LM80_REG_FAN_MIN(index + 1), | |
331 | data->fan[nr][index]); | |
f8181762 JD |
332 | mutex_unlock(&data->update_lock); |
333 | return count; | |
1da177e4 | 334 | } |
1da177e4 | 335 | |
1160631b GR |
336 | /* |
337 | * Note: we save and restore the fan minimum here, because its value is | |
338 | * determined in part by the fan divisor. This follows the principle of | |
339 | * least surprise; the user doesn't expect the fan minimum to change just | |
340 | * because the divisor changed. | |
341 | */ | |
fdb97a02 GR |
342 | static ssize_t fan_div_store(struct device *dev, |
343 | struct device_attribute *attr, const char *buf, | |
344 | size_t count) | |
1da177e4 | 345 | { |
f8181762 | 346 | int nr = to_sensor_dev_attr(attr)->index; |
118c9a61 GR |
347 | struct lm80_data *data = dev_get_drvdata(dev); |
348 | struct i2c_client *client = data->client; | |
6a9e7c4c | 349 | unsigned long min, val; |
1da177e4 | 350 | u8 reg; |
c9c63915 KL |
351 | int rv; |
352 | ||
353 | rv = kstrtoul(buf, 10, &val); | |
354 | if (rv < 0) | |
355 | return rv; | |
1da177e4 LT |
356 | |
357 | /* Save fan_min */ | |
9a61bf63 | 358 | mutex_lock(&data->update_lock); |
85b3ee07 | 359 | min = FAN_FROM_REG(data->fan[f_min][nr], |
1da177e4 LT |
360 | DIV_FROM_REG(data->fan_div[nr])); |
361 | ||
362 | switch (val) { | |
66b3b1f7 FM |
363 | case 1: |
364 | data->fan_div[nr] = 0; | |
365 | break; | |
366 | case 2: | |
367 | data->fan_div[nr] = 1; | |
368 | break; | |
369 | case 4: | |
370 | data->fan_div[nr] = 2; | |
371 | break; | |
372 | case 8: | |
373 | data->fan_div[nr] = 3; | |
374 | break; | |
1da177e4 | 375 | default: |
118c9a61 | 376 | dev_err(dev, |
b55f3757 GR |
377 | "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", |
378 | val); | |
9a61bf63 | 379 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
380 | return -EINVAL; |
381 | } | |
382 | ||
c9c63915 | 383 | rv = lm80_read_value(client, LM80_REG_FANDIV); |
07bd14cc WY |
384 | if (rv < 0) { |
385 | mutex_unlock(&data->update_lock); | |
c9c63915 | 386 | return rv; |
07bd14cc | 387 | } |
c9c63915 KL |
388 | reg = (rv & ~(3 << (2 * (nr + 1)))) |
389 | | (data->fan_div[nr] << (2 * (nr + 1))); | |
1da177e4 LT |
390 | lm80_write_value(client, LM80_REG_FANDIV, reg); |
391 | ||
392 | /* Restore fan_min */ | |
85b3ee07 GR |
393 | data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
394 | lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), | |
395 | data->fan[f_min][nr]); | |
9a61bf63 | 396 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
397 | |
398 | return count; | |
399 | } | |
400 | ||
fdb97a02 | 401 | static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, |
9711bcdd | 402 | char *buf) |
1da177e4 | 403 | { |
9711bcdd | 404 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
1da177e4 | 405 | struct lm80_data *data = lm80_update_device(dev); |
2faaa931 FM |
406 | if (IS_ERR(data)) |
407 | return PTR_ERR(data); | |
9711bcdd | 408 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); |
1da177e4 LT |
409 | } |
410 | ||
fdb97a02 GR |
411 | static ssize_t temp_store(struct device *dev, |
412 | struct device_attribute *devattr, const char *buf, | |
413 | size_t count) | |
9711bcdd GR |
414 | { |
415 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | |
416 | struct lm80_data *data = dev_get_drvdata(dev); | |
417 | struct i2c_client *client = data->client; | |
418 | int nr = attr->index; | |
419 | long val; | |
420 | int err = kstrtol(buf, 10, &val); | |
421 | if (err < 0) | |
422 | return err; | |
1da177e4 | 423 | |
9711bcdd GR |
424 | mutex_lock(&data->update_lock); |
425 | data->temp[nr] = TEMP_TO_REG(val); | |
426 | lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8); | |
427 | mutex_unlock(&data->update_lock); | |
428 | return count; | |
1da177e4 | 429 | } |
1da177e4 | 430 | |
1547690d | 431 | static ssize_t alarms_show(struct device *dev, struct device_attribute *attr, |
6cc37ee5 | 432 | char *buf) |
1da177e4 LT |
433 | { |
434 | struct lm80_data *data = lm80_update_device(dev); | |
2faaa931 FM |
435 | if (IS_ERR(data)) |
436 | return PTR_ERR(data); | |
1da177e4 LT |
437 | return sprintf(buf, "%u\n", data->alarms); |
438 | } | |
439 | ||
fdb97a02 | 440 | static ssize_t alarm_show(struct device *dev, struct device_attribute *attr, |
e84542f5 JD |
441 | char *buf) |
442 | { | |
443 | int bitnr = to_sensor_dev_attr(attr)->index; | |
444 | struct lm80_data *data = lm80_update_device(dev); | |
2faaa931 FM |
445 | if (IS_ERR(data)) |
446 | return PTR_ERR(data); | |
e84542f5 JD |
447 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
448 | } | |
449 | ||
fdb97a02 GR |
450 | static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, i_min, 0); |
451 | static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, i_min, 1); | |
452 | static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, i_min, 2); | |
453 | static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, i_min, 3); | |
454 | static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, i_min, 4); | |
455 | static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, i_min, 5); | |
456 | static SENSOR_DEVICE_ATTR_2_RW(in6_min, in, i_min, 6); | |
457 | static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, i_max, 0); | |
458 | static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, i_max, 1); | |
459 | static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, i_max, 2); | |
460 | static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, i_max, 3); | |
461 | static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, i_max, 4); | |
462 | static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, i_max, 5); | |
463 | static SENSOR_DEVICE_ATTR_2_RW(in6_max, in, i_max, 6); | |
464 | static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, i_input, 0); | |
465 | static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, i_input, 1); | |
466 | static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, i_input, 2); | |
467 | static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, i_input, 3); | |
468 | static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, i_input, 4); | |
469 | static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, i_input, 5); | |
470 | static SENSOR_DEVICE_ATTR_2_RO(in6_input, in, i_input, 6); | |
471 | static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, f_min, 0); | |
472 | static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, f_min, 1); | |
473 | static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, f_input, 0); | |
474 | static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, f_input, 1); | |
475 | static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0); | |
476 | static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1); | |
477 | static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input); | |
478 | static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_hot_max); | |
479 | static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, t_hot_hyst); | |
480 | static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_os_max); | |
481 | static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp, t_os_hyst); | |
1547690d | 482 | static DEVICE_ATTR_RO(alarms); |
fdb97a02 GR |
483 | static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0); |
484 | static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1); | |
485 | static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2); | |
486 | static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3); | |
487 | static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 4); | |
488 | static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 5); | |
489 | static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 6); | |
490 | static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 10); | |
491 | static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 11); | |
492 | static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 8); | |
493 | static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 13); | |
1da177e4 LT |
494 | |
495 | /* | |
496 | * Real code | |
497 | */ | |
498 | ||
118c9a61 | 499 | static struct attribute *lm80_attrs[] = { |
f8181762 JD |
500 | &sensor_dev_attr_in0_min.dev_attr.attr, |
501 | &sensor_dev_attr_in1_min.dev_attr.attr, | |
502 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
503 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
504 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
505 | &sensor_dev_attr_in5_min.dev_attr.attr, | |
506 | &sensor_dev_attr_in6_min.dev_attr.attr, | |
507 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
508 | &sensor_dev_attr_in1_max.dev_attr.attr, | |
509 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
510 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
511 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
512 | &sensor_dev_attr_in5_max.dev_attr.attr, | |
513 | &sensor_dev_attr_in6_max.dev_attr.attr, | |
514 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
515 | &sensor_dev_attr_in1_input.dev_attr.attr, | |
516 | &sensor_dev_attr_in2_input.dev_attr.attr, | |
517 | &sensor_dev_attr_in3_input.dev_attr.attr, | |
518 | &sensor_dev_attr_in4_input.dev_attr.attr, | |
519 | &sensor_dev_attr_in5_input.dev_attr.attr, | |
520 | &sensor_dev_attr_in6_input.dev_attr.attr, | |
521 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
522 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
523 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
524 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
525 | &sensor_dev_attr_fan1_div.dev_attr.attr, | |
526 | &sensor_dev_attr_fan2_div.dev_attr.attr, | |
9711bcdd GR |
527 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
528 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
529 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | |
530 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
531 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | |
0501a381 | 532 | &dev_attr_alarms.attr, |
e84542f5 JD |
533 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
534 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | |
535 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
536 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
537 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | |
538 | &sensor_dev_attr_in5_alarm.dev_attr.attr, | |
539 | &sensor_dev_attr_in6_alarm.dev_attr.attr, | |
540 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | |
541 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
542 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
543 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
0501a381 MH |
544 | NULL |
545 | }; | |
118c9a61 | 546 | ATTRIBUTE_GROUPS(lm80); |
0501a381 | 547 | |
8c8bacc8 | 548 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 549 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info) |
1da177e4 | 550 | { |
8c8bacc8 | 551 | struct i2c_adapter *adapter = client->adapter; |
9908ad4c JD |
552 | int i, cur, man_id, dev_id; |
553 | const char *name = NULL; | |
1da177e4 LT |
554 | |
555 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
8c8bacc8 | 556 | return -ENODEV; |
1da177e4 | 557 | |
9908ad4c JD |
558 | /* First check for unused bits, common to both chip types */ |
559 | if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0) | |
560 | || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80)) | |
8c8bacc8 | 561 | return -ENODEV; |
9908ad4c JD |
562 | |
563 | /* | |
564 | * The LM96080 has manufacturer and stepping/die rev registers so we | |
565 | * can just check that. The LM80 does not have such registers so we | |
566 | * have to use a more expensive trick. | |
567 | */ | |
568 | man_id = lm80_read_value(client, LM96080_REG_MAN_ID); | |
569 | dev_id = lm80_read_value(client, LM96080_REG_DEV_ID); | |
570 | if (man_id == 0x01 && dev_id == 0x08) { | |
571 | /* Check more unused bits for confirmation */ | |
572 | if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe) | |
66b3b1f7 | 573 | return -ENODEV; |
9908ad4c JD |
574 | |
575 | name = "lm96080"; | |
576 | } else { | |
577 | /* Check 6-bit addressing */ | |
578 | for (i = 0x2a; i <= 0x3d; i++) { | |
579 | cur = i2c_smbus_read_byte_data(client, i); | |
580 | if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur) | |
581 | || (i2c_smbus_read_byte_data(client, i + 0x80) != cur) | |
582 | || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur)) | |
583 | return -ENODEV; | |
584 | } | |
585 | ||
586 | name = "lm80"; | |
1da177e4 LT |
587 | } |
588 | ||
9908ad4c | 589 | strlcpy(info->type, name, I2C_NAME_SIZE); |
1da177e4 | 590 | |
8c8bacc8 JD |
591 | return 0; |
592 | } | |
1da177e4 | 593 | |
67487038 | 594 | static int lm80_probe(struct i2c_client *client) |
8c8bacc8 | 595 | { |
118c9a61 GR |
596 | struct device *dev = &client->dev; |
597 | struct device *hwmon_dev; | |
8c8bacc8 | 598 | struct lm80_data *data; |
8c8bacc8 | 599 | |
118c9a61 | 600 | data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL); |
c38b2820 GR |
601 | if (!data) |
602 | return -ENOMEM; | |
8c8bacc8 | 603 | |
118c9a61 | 604 | data->client = client; |
8c8bacc8 | 605 | mutex_init(&data->update_lock); |
1da177e4 LT |
606 | |
607 | /* Initialize the LM80 chip */ | |
6cc37ee5 | 608 | lm80_init_client(client); |
1da177e4 LT |
609 | |
610 | /* A few vars need to be filled upon startup */ | |
99ae3417 GKH |
611 | data->fan[f_min][0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
612 | data->fan[f_min][1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); | |
1da177e4 | 613 | |
118c9a61 GR |
614 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
615 | data, lm80_groups); | |
943b0830 | 616 | |
118c9a61 | 617 | return PTR_ERR_OR_ZERO(hwmon_dev); |
1da177e4 LT |
618 | } |
619 | ||
c254ffde GR |
620 | /* |
621 | * Driver data (common to all clients) | |
622 | */ | |
1da177e4 | 623 | |
c254ffde GR |
624 | static const struct i2c_device_id lm80_id[] = { |
625 | { "lm80", 0 }, | |
626 | { "lm96080", 1 }, | |
627 | { } | |
628 | }; | |
629 | MODULE_DEVICE_TABLE(i2c, lm80_id); | |
1da177e4 | 630 | |
c254ffde GR |
631 | static struct i2c_driver lm80_driver = { |
632 | .class = I2C_CLASS_HWMON, | |
633 | .driver = { | |
634 | .name = "lm80", | |
635 | }, | |
67487038 | 636 | .probe_new = lm80_probe, |
c254ffde GR |
637 | .id_table = lm80_id, |
638 | .detect = lm80_detect, | |
639 | .address_list = normal_i2c, | |
640 | }; | |
1da177e4 | 641 | |
f0967eea | 642 | module_i2c_driver(lm80_driver); |
1da177e4 LT |
643 | |
644 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " | |
645 | "Philip Edelbrock <phil@netroedge.com>"); | |
646 | MODULE_DESCRIPTION("LM80 driver"); | |
647 | MODULE_LICENSE("GPL"); |