Commit | Line | Data |
---|---|---|
3434f378 GR |
1 | /* |
2 | * nct7802 - Driver for Nuvoton NCT7802Y | |
3 | * | |
4 | * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | */ | |
16 | ||
17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
18 | ||
19 | #include <linux/err.h> | |
20 | #include <linux/i2c.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/hwmon.h> | |
23 | #include <linux/hwmon-sysfs.h> | |
24 | #include <linux/jiffies.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/mutex.h> | |
27 | #include <linux/regmap.h> | |
28 | #include <linux/slab.h> | |
29 | ||
30 | #define DRVNAME "nct7802" | |
31 | ||
32 | static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e }; | |
33 | ||
34 | static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = { | |
35 | { 0x40, 0x00, 0x42, 0x44, 0x46 }, | |
36 | { 0x3f, 0x00, 0x41, 0x43, 0x45 }, | |
37 | }; | |
38 | ||
39 | static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 }; | |
40 | ||
41 | static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = { | |
42 | { 0, 0, 4, 0, 4 }, | |
43 | { 2, 0, 6, 2, 6 }, | |
44 | }; | |
45 | ||
46 | #define REG_BANK 0x00 | |
47 | #define REG_TEMP_LSB 0x05 | |
48 | #define REG_TEMP_PECI_LSB 0x08 | |
49 | #define REG_VOLTAGE_LOW 0x0f | |
50 | #define REG_FANCOUNT_LOW 0x13 | |
51 | #define REG_START 0x21 | |
fcdc5739 | 52 | #define REG_MODE 0x22 /* 7.2.32 Mode Selection Register */ |
3434f378 GR |
53 | #define REG_PECI_ENABLE 0x23 |
54 | #define REG_FAN_ENABLE 0x24 | |
55 | #define REG_VMON_ENABLE 0x25 | |
56 | #define REG_VENDOR_ID 0xfd | |
57 | #define REG_CHIP_ID 0xfe | |
58 | #define REG_VERSION_ID 0xff | |
59 | ||
60 | /* | |
61 | * Data structures and manipulation thereof | |
62 | */ | |
63 | ||
64 | struct nct7802_data { | |
65 | struct regmap *regmap; | |
66 | struct mutex access_lock; /* for multi-byte read and write operations */ | |
67 | }; | |
68 | ||
fcdc5739 CS |
69 | static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr, |
70 | char *buf) | |
71 | { | |
72 | struct nct7802_data *data = dev_get_drvdata(dev); | |
73 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); | |
74 | unsigned int mode; | |
75 | int ret; | |
76 | ||
77 | ret = regmap_read(data->regmap, REG_MODE, &mode); | |
78 | if (ret < 0) | |
79 | return ret; | |
80 | ||
81 | return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2); | |
82 | } | |
83 | ||
84 | static ssize_t store_temp_type(struct device *dev, | |
85 | struct device_attribute *attr, | |
86 | const char *buf, size_t count) | |
87 | { | |
88 | struct nct7802_data *data = dev_get_drvdata(dev); | |
89 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); | |
90 | unsigned int type; | |
91 | int err; | |
92 | ||
93 | err = kstrtouint(buf, 0, &type); | |
94 | if (err < 0) | |
95 | return err; | |
96 | if (sattr->index == 2 && type != 4) /* RD3 */ | |
97 | return -EINVAL; | |
98 | if (type < 3 || type > 4) | |
99 | return -EINVAL; | |
100 | err = regmap_update_bits(data->regmap, REG_MODE, | |
101 | 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index); | |
102 | return err ? : count; | |
103 | } | |
104 | ||
105 | ||
3434f378 GR |
106 | static int nct7802_read_temp(struct nct7802_data *data, |
107 | u8 reg_temp, u8 reg_temp_low, int *temp) | |
108 | { | |
109 | unsigned int t1, t2 = 0; | |
110 | int err; | |
111 | ||
112 | *temp = 0; | |
113 | ||
114 | mutex_lock(&data->access_lock); | |
115 | err = regmap_read(data->regmap, reg_temp, &t1); | |
116 | if (err < 0) | |
117 | goto abort; | |
118 | t1 <<= 8; | |
119 | if (reg_temp_low) { /* 11 bit data */ | |
120 | err = regmap_read(data->regmap, reg_temp_low, &t2); | |
121 | if (err < 0) | |
122 | goto abort; | |
123 | } | |
124 | t1 |= t2 & 0xe0; | |
125 | *temp = (s16)t1 / 32 * 125; | |
126 | abort: | |
127 | mutex_unlock(&data->access_lock); | |
128 | return err; | |
129 | } | |
130 | ||
131 | static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan) | |
132 | { | |
133 | unsigned int f1, f2; | |
134 | int ret; | |
135 | ||
136 | mutex_lock(&data->access_lock); | |
137 | ret = regmap_read(data->regmap, reg_fan, &f1); | |
138 | if (ret < 0) | |
139 | goto abort; | |
140 | ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2); | |
141 | if (ret < 0) | |
142 | goto abort; | |
143 | ret = (f1 << 5) | (f2 >> 3); | |
144 | /* convert fan count to rpm */ | |
145 | if (ret == 0x1fff) /* maximum value, assume fan is stopped */ | |
146 | ret = 0; | |
147 | else if (ret) | |
148 | ret = DIV_ROUND_CLOSEST(1350000U, ret); | |
149 | abort: | |
150 | mutex_unlock(&data->access_lock); | |
151 | return ret; | |
152 | } | |
153 | ||
154 | static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low, | |
155 | u8 reg_fan_high) | |
156 | { | |
157 | unsigned int f1, f2; | |
158 | int ret; | |
159 | ||
160 | mutex_lock(&data->access_lock); | |
161 | ret = regmap_read(data->regmap, reg_fan_low, &f1); | |
162 | if (ret < 0) | |
163 | goto abort; | |
164 | ret = regmap_read(data->regmap, reg_fan_high, &f2); | |
165 | if (ret < 0) | |
166 | goto abort; | |
167 | ret = f1 | ((f2 & 0xf8) << 5); | |
168 | /* convert fan count to rpm */ | |
169 | if (ret == 0x1fff) /* maximum value, assume no limit */ | |
170 | ret = 0; | |
171 | else if (ret) | |
172 | ret = DIV_ROUND_CLOSEST(1350000U, ret); | |
173 | abort: | |
174 | mutex_unlock(&data->access_lock); | |
175 | return ret; | |
176 | } | |
177 | ||
178 | static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low, | |
179 | u8 reg_fan_high, unsigned int limit) | |
180 | { | |
181 | int err; | |
182 | ||
183 | if (limit) | |
184 | limit = DIV_ROUND_CLOSEST(1350000U, limit); | |
185 | else | |
186 | limit = 0x1fff; | |
187 | limit = clamp_val(limit, 0, 0x1fff); | |
188 | ||
189 | mutex_lock(&data->access_lock); | |
190 | err = regmap_write(data->regmap, reg_fan_low, limit & 0xff); | |
191 | if (err < 0) | |
192 | goto abort; | |
193 | ||
194 | err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5); | |
195 | abort: | |
196 | mutex_unlock(&data->access_lock); | |
197 | return err; | |
198 | } | |
199 | ||
200 | static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 }; | |
201 | ||
202 | static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index) | |
203 | { | |
204 | unsigned int v1, v2; | |
205 | int ret; | |
206 | ||
207 | mutex_lock(&data->access_lock); | |
208 | if (index == 0) { /* voltage */ | |
209 | ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1); | |
210 | if (ret < 0) | |
211 | goto abort; | |
212 | ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2); | |
213 | if (ret < 0) | |
214 | goto abort; | |
215 | ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr]; | |
216 | } else { /* limit */ | |
217 | int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; | |
218 | ||
219 | ret = regmap_read(data->regmap, | |
220 | REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1); | |
221 | if (ret < 0) | |
222 | goto abort; | |
223 | ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], | |
224 | &v2); | |
225 | if (ret < 0) | |
226 | goto abort; | |
227 | ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr]; | |
228 | } | |
229 | abort: | |
230 | mutex_unlock(&data->access_lock); | |
231 | return ret; | |
232 | } | |
233 | ||
234 | static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index, | |
9200bc4c | 235 | unsigned long voltage) |
3434f378 GR |
236 | { |
237 | int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; | |
238 | int err; | |
239 | ||
240 | voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]); | |
241 | voltage = clamp_val(voltage, 0, 0x3ff); | |
242 | ||
243 | mutex_lock(&data->access_lock); | |
244 | err = regmap_write(data->regmap, | |
245 | REG_VOLTAGE_LIMIT_LSB[index - 1][nr], | |
246 | voltage & 0xff); | |
247 | if (err < 0) | |
248 | goto abort; | |
249 | ||
250 | err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], | |
251 | 0x0300 >> shift, (voltage & 0x0300) >> shift); | |
252 | abort: | |
253 | mutex_unlock(&data->access_lock); | |
254 | return err; | |
255 | } | |
256 | ||
257 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, | |
258 | char *buf) | |
259 | { | |
260 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
261 | struct nct7802_data *data = dev_get_drvdata(dev); | |
262 | int voltage; | |
263 | ||
264 | voltage = nct7802_read_voltage(data, sattr->nr, sattr->index); | |
265 | if (voltage < 0) | |
266 | return voltage; | |
267 | ||
268 | return sprintf(buf, "%d\n", voltage); | |
269 | } | |
270 | ||
271 | static ssize_t store_in(struct device *dev, struct device_attribute *attr, | |
272 | const char *buf, size_t count) | |
273 | { | |
274 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
275 | struct nct7802_data *data = dev_get_drvdata(dev); | |
276 | int index = sattr->index; | |
277 | int nr = sattr->nr; | |
278 | unsigned long val; | |
279 | int err; | |
280 | ||
281 | err = kstrtoul(buf, 10, &val); | |
282 | if (err < 0) | |
283 | return err; | |
284 | ||
285 | err = nct7802_write_voltage(data, nr, index, val); | |
286 | return err ? : count; | |
287 | } | |
288 | ||
289 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, | |
290 | char *buf) | |
291 | { | |
292 | struct nct7802_data *data = dev_get_drvdata(dev); | |
293 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
294 | int err, temp; | |
295 | ||
296 | err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp); | |
297 | if (err < 0) | |
298 | return err; | |
299 | ||
300 | return sprintf(buf, "%d\n", temp); | |
301 | } | |
302 | ||
303 | static ssize_t store_temp(struct device *dev, struct device_attribute *attr, | |
304 | const char *buf, size_t count) | |
305 | { | |
306 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
307 | struct nct7802_data *data = dev_get_drvdata(dev); | |
308 | int nr = sattr->nr; | |
309 | long val; | |
310 | int err; | |
311 | ||
312 | err = kstrtol(buf, 10, &val); | |
313 | if (err < 0) | |
314 | return err; | |
315 | ||
316 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); | |
317 | ||
318 | err = regmap_write(data->regmap, nr, val & 0xff); | |
319 | return err ? : count; | |
320 | } | |
321 | ||
322 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, | |
323 | char *buf) | |
324 | { | |
325 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); | |
326 | struct nct7802_data *data = dev_get_drvdata(dev); | |
327 | int speed; | |
328 | ||
329 | speed = nct7802_read_fan(data, sattr->index); | |
330 | if (speed < 0) | |
331 | return speed; | |
332 | ||
333 | return sprintf(buf, "%d\n", speed); | |
334 | } | |
335 | ||
336 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, | |
337 | char *buf) | |
338 | { | |
339 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
340 | struct nct7802_data *data = dev_get_drvdata(dev); | |
341 | int speed; | |
342 | ||
343 | speed = nct7802_read_fan_min(data, sattr->nr, sattr->index); | |
344 | if (speed < 0) | |
345 | return speed; | |
346 | ||
347 | return sprintf(buf, "%d\n", speed); | |
348 | } | |
349 | ||
350 | static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr, | |
351 | const char *buf, size_t count) | |
352 | { | |
353 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
354 | struct nct7802_data *data = dev_get_drvdata(dev); | |
355 | unsigned long val; | |
356 | int err; | |
357 | ||
358 | err = kstrtoul(buf, 10, &val); | |
359 | if (err < 0) | |
360 | return err; | |
361 | ||
362 | err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val); | |
363 | return err ? : count; | |
364 | } | |
365 | ||
366 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, | |
367 | char *buf) | |
368 | { | |
369 | struct nct7802_data *data = dev_get_drvdata(dev); | |
370 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
371 | int bit = sattr->index; | |
372 | unsigned int val; | |
373 | int ret; | |
374 | ||
375 | ret = regmap_read(data->regmap, sattr->nr, &val); | |
376 | if (ret < 0) | |
377 | return ret; | |
378 | ||
379 | return sprintf(buf, "%u\n", !!(val & (1 << bit))); | |
380 | } | |
381 | ||
382 | static ssize_t | |
383 | show_beep(struct device *dev, struct device_attribute *attr, char *buf) | |
384 | { | |
385 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
386 | struct nct7802_data *data = dev_get_drvdata(dev); | |
387 | unsigned int regval; | |
388 | int err; | |
389 | ||
390 | err = regmap_read(data->regmap, sattr->nr, ®val); | |
391 | if (err) | |
392 | return err; | |
393 | ||
394 | return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index))); | |
395 | } | |
396 | ||
397 | static ssize_t | |
398 | store_beep(struct device *dev, struct device_attribute *attr, const char *buf, | |
399 | size_t count) | |
400 | { | |
401 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); | |
402 | struct nct7802_data *data = dev_get_drvdata(dev); | |
403 | unsigned long val; | |
404 | int err; | |
405 | ||
406 | err = kstrtoul(buf, 10, &val); | |
407 | if (err < 0) | |
408 | return err; | |
409 | if (val > 1) | |
410 | return -EINVAL; | |
411 | ||
412 | err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index, | |
413 | val ? 1 << sattr->index : 0); | |
414 | return err ? : count; | |
415 | } | |
416 | ||
fcdc5739 CS |
417 | static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, |
418 | show_temp_type, store_temp_type, 0); | |
3434f378 GR |
419 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0x01, |
420 | REG_TEMP_LSB); | |
421 | static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, | |
422 | store_temp, 0x31, 0); | |
423 | static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, | |
424 | store_temp, 0x30, 0); | |
425 | static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, | |
426 | store_temp, 0x3a, 0); | |
427 | ||
fcdc5739 CS |
428 | static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, |
429 | show_temp_type, store_temp_type, 1); | |
3434f378 GR |
430 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0x02, |
431 | REG_TEMP_LSB); | |
432 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, | |
433 | store_temp, 0x33, 0); | |
434 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, | |
435 | store_temp, 0x32, 0); | |
436 | static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, | |
437 | store_temp, 0x3b, 0); | |
438 | ||
fcdc5739 CS |
439 | static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, |
440 | show_temp_type, store_temp_type, 2); | |
3434f378 GR |
441 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0x03, |
442 | REG_TEMP_LSB); | |
443 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, | |
444 | store_temp, 0x35, 0); | |
445 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, | |
446 | store_temp, 0x34, 0); | |
447 | static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, | |
448 | store_temp, 0x3c, 0); | |
449 | ||
450 | static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 0x04, 0); | |
451 | static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp, | |
452 | store_temp, 0x37, 0); | |
453 | static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, | |
454 | store_temp, 0x36, 0); | |
455 | static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, | |
456 | store_temp, 0x3d, 0); | |
457 | ||
458 | static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 0x06, | |
459 | REG_TEMP_PECI_LSB); | |
460 | static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp, | |
461 | store_temp, 0x39, 0); | |
462 | static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, | |
463 | store_temp, 0x38, 0); | |
464 | static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, | |
465 | store_temp, 0x3e, 0); | |
466 | ||
467 | static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 0x07, | |
468 | REG_TEMP_PECI_LSB); | |
469 | ||
470 | static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_alarm, NULL, | |
471 | 0x18, 0); | |
472 | static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_alarm, NULL, | |
473 | 0x18, 1); | |
474 | static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_alarm, NULL, | |
475 | 0x18, 2); | |
476 | static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, show_alarm, NULL, | |
477 | 0x18, 3); | |
478 | static SENSOR_DEVICE_ATTR_2(temp5_min_alarm, S_IRUGO, show_alarm, NULL, | |
479 | 0x18, 4); | |
480 | ||
481 | static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_alarm, NULL, | |
482 | 0x19, 0); | |
483 | static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_alarm, NULL, | |
484 | 0x19, 1); | |
485 | static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_alarm, NULL, | |
486 | 0x19, 2); | |
487 | static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, show_alarm, NULL, | |
488 | 0x19, 3); | |
489 | static SENSOR_DEVICE_ATTR_2(temp5_max_alarm, S_IRUGO, show_alarm, NULL, | |
490 | 0x19, 4); | |
491 | ||
492 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, | |
493 | 0x1b, 0); | |
494 | static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, | |
495 | 0x1b, 1); | |
496 | static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, | |
497 | 0x1b, 2); | |
498 | static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, | |
499 | 0x1b, 3); | |
500 | static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm, S_IRUGO, show_alarm, NULL, | |
501 | 0x1b, 4); | |
502 | ||
503 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_alarm, NULL, 0x17, 0); | |
504 | static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_alarm, NULL, 0x17, 1); | |
505 | static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_alarm, NULL, 0x17, 2); | |
506 | ||
507 | static SENSOR_DEVICE_ATTR_2(temp1_beep, S_IRUGO | S_IWUSR, show_beep, | |
508 | store_beep, 0x5c, 0); | |
509 | static SENSOR_DEVICE_ATTR_2(temp2_beep, S_IRUGO | S_IWUSR, show_beep, | |
510 | store_beep, 0x5c, 1); | |
511 | static SENSOR_DEVICE_ATTR_2(temp3_beep, S_IRUGO | S_IWUSR, show_beep, | |
512 | store_beep, 0x5c, 2); | |
513 | static SENSOR_DEVICE_ATTR_2(temp4_beep, S_IRUGO | S_IWUSR, show_beep, | |
514 | store_beep, 0x5c, 3); | |
515 | static SENSOR_DEVICE_ATTR_2(temp5_beep, S_IRUGO | S_IWUSR, show_beep, | |
516 | store_beep, 0x5c, 4); | |
517 | static SENSOR_DEVICE_ATTR_2(temp6_beep, S_IRUGO | S_IWUSR, show_beep, | |
518 | store_beep, 0x5c, 5); | |
519 | ||
520 | static struct attribute *nct7802_temp_attrs[] = { | |
fcdc5739 | 521 | &sensor_dev_attr_temp1_type.dev_attr.attr, |
3434f378 GR |
522 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
523 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
524 | &sensor_dev_attr_temp1_max.dev_attr.attr, | |
525 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | |
526 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
527 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
528 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | |
529 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | |
530 | &sensor_dev_attr_temp1_beep.dev_attr.attr, | |
531 | ||
fcdc5739 CS |
532 | &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */ |
533 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
3434f378 GR |
534 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
535 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
536 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | |
537 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
538 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
539 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | |
540 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | |
541 | &sensor_dev_attr_temp2_beep.dev_attr.attr, | |
542 | ||
fcdc5739 CS |
543 | &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */ |
544 | &sensor_dev_attr_temp3_input.dev_attr.attr, | |
3434f378 GR |
545 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
546 | &sensor_dev_attr_temp3_max.dev_attr.attr, | |
547 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | |
548 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | |
549 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | |
550 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | |
551 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | |
552 | &sensor_dev_attr_temp3_beep.dev_attr.attr, | |
553 | ||
fcdc5739 | 554 | &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */ |
3434f378 GR |
555 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
556 | &sensor_dev_attr_temp4_max.dev_attr.attr, | |
557 | &sensor_dev_attr_temp4_crit.dev_attr.attr, | |
558 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, | |
559 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, | |
560 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, | |
561 | &sensor_dev_attr_temp4_beep.dev_attr.attr, | |
562 | ||
fcdc5739 | 563 | &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */ |
3434f378 GR |
564 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
565 | &sensor_dev_attr_temp5_max.dev_attr.attr, | |
566 | &sensor_dev_attr_temp5_crit.dev_attr.attr, | |
567 | &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, | |
568 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, | |
569 | &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, | |
570 | &sensor_dev_attr_temp5_beep.dev_attr.attr, | |
571 | ||
fcdc5739 | 572 | &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */ |
3434f378 GR |
573 | &sensor_dev_attr_temp6_beep.dev_attr.attr, |
574 | ||
575 | NULL | |
576 | }; | |
577 | ||
578 | static umode_t nct7802_temp_is_visible(struct kobject *kobj, | |
579 | struct attribute *attr, int index) | |
580 | { | |
581 | struct device *dev = container_of(kobj, struct device, kobj); | |
582 | struct nct7802_data *data = dev_get_drvdata(dev); | |
583 | unsigned int reg; | |
584 | int err; | |
585 | ||
586 | err = regmap_read(data->regmap, REG_MODE, ®); | |
587 | if (err < 0) | |
588 | return 0; | |
589 | ||
fcdc5739 | 590 | if (index < 10 && |
3434f378 GR |
591 | (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */ |
592 | return 0; | |
fcdc5739 CS |
593 | |
594 | if (index >= 10 && index < 20 && | |
3434f378 GR |
595 | (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */ |
596 | return 0; | |
fcdc5739 | 597 | if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */ |
3434f378 | 598 | return 0; |
fcdc5739 CS |
599 | |
600 | if (index >= 30 && index < 38) /* local */ | |
3434f378 GR |
601 | return attr->mode; |
602 | ||
603 | err = regmap_read(data->regmap, REG_PECI_ENABLE, ®); | |
604 | if (err < 0) | |
605 | return 0; | |
606 | ||
fcdc5739 | 607 | if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */ |
3434f378 GR |
608 | return 0; |
609 | ||
fcdc5739 | 610 | if (index >= 0x46 && (!(reg & 0x02))) /* PECI 1 */ |
3434f378 GR |
611 | return 0; |
612 | ||
613 | return attr->mode; | |
614 | } | |
615 | ||
616 | static struct attribute_group nct7802_temp_group = { | |
617 | .attrs = nct7802_temp_attrs, | |
618 | .is_visible = nct7802_temp_is_visible, | |
619 | }; | |
620 | ||
621 | static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0); | |
622 | static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
623 | 0, 1); | |
624 | static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
625 | 0, 2); | |
626 | static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 3); | |
627 | static SENSOR_DEVICE_ATTR_2(in0_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
628 | 0x5a, 3); | |
629 | ||
630 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0); | |
631 | ||
632 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0); | |
633 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
634 | 2, 1); | |
635 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
636 | 2, 2); | |
637 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 0); | |
638 | static SENSOR_DEVICE_ATTR_2(in2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
639 | 0x5a, 0); | |
640 | ||
641 | static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0); | |
642 | static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
643 | 3, 1); | |
644 | static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
645 | 3, 2); | |
646 | static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 1); | |
647 | static SENSOR_DEVICE_ATTR_2(in3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
648 | 0x5a, 1); | |
649 | ||
650 | static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0); | |
651 | static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, store_in, | |
652 | 4, 1); | |
653 | static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, store_in, | |
654 | 4, 2); | |
655 | static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 2); | |
656 | static SENSOR_DEVICE_ATTR_2(in4_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
657 | 0x5a, 2); | |
658 | ||
659 | static struct attribute *nct7802_in_attrs[] = { | |
660 | &sensor_dev_attr_in0_input.dev_attr.attr, | |
661 | &sensor_dev_attr_in0_min.dev_attr.attr, | |
662 | &sensor_dev_attr_in0_max.dev_attr.attr, | |
663 | &sensor_dev_attr_in0_alarm.dev_attr.attr, | |
664 | &sensor_dev_attr_in0_beep.dev_attr.attr, | |
665 | ||
666 | &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */ | |
667 | ||
668 | &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */ | |
669 | &sensor_dev_attr_in2_min.dev_attr.attr, | |
670 | &sensor_dev_attr_in2_max.dev_attr.attr, | |
671 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | |
672 | &sensor_dev_attr_in2_beep.dev_attr.attr, | |
673 | ||
674 | &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */ | |
675 | &sensor_dev_attr_in3_min.dev_attr.attr, | |
676 | &sensor_dev_attr_in3_max.dev_attr.attr, | |
677 | &sensor_dev_attr_in3_alarm.dev_attr.attr, | |
678 | &sensor_dev_attr_in3_beep.dev_attr.attr, | |
679 | ||
680 | &sensor_dev_attr_in4_input.dev_attr.attr, /* 17 */ | |
681 | &sensor_dev_attr_in4_min.dev_attr.attr, | |
682 | &sensor_dev_attr_in4_max.dev_attr.attr, | |
683 | &sensor_dev_attr_in4_alarm.dev_attr.attr, | |
684 | &sensor_dev_attr_in4_beep.dev_attr.attr, | |
685 | ||
686 | NULL, | |
687 | }; | |
688 | ||
689 | static umode_t nct7802_in_is_visible(struct kobject *kobj, | |
690 | struct attribute *attr, int index) | |
691 | { | |
692 | struct device *dev = container_of(kobj, struct device, kobj); | |
693 | struct nct7802_data *data = dev_get_drvdata(dev); | |
694 | unsigned int reg; | |
695 | int err; | |
696 | ||
697 | if (index < 6) /* VCC, VCORE */ | |
698 | return attr->mode; | |
699 | ||
700 | err = regmap_read(data->regmap, REG_MODE, ®); | |
701 | if (err < 0) | |
702 | return 0; | |
703 | ||
704 | if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */ | |
705 | return 0; | |
706 | if (index >= 11 && index < 17 && (reg & 0x0c) != 0x0c) /* VSEN2 */ | |
707 | return 0; | |
708 | if (index >= 17 && (reg & 0x30) != 0x30) /* VSEN3 */ | |
709 | return 0; | |
710 | ||
711 | return attr->mode; | |
712 | } | |
713 | ||
714 | static struct attribute_group nct7802_in_group = { | |
715 | .attrs = nct7802_in_attrs, | |
716 | .is_visible = nct7802_in_is_visible, | |
717 | }; | |
718 | ||
719 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0x10); | |
720 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan_min, | |
721 | store_fan_min, 0x49, 0x4c); | |
722 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 0); | |
723 | static SENSOR_DEVICE_ATTR_2(fan1_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
724 | 0x5b, 0); | |
725 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 0x11); | |
726 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan_min, | |
727 | store_fan_min, 0x4a, 0x4d); | |
728 | static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 1); | |
729 | static SENSOR_DEVICE_ATTR_2(fan2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
730 | 0x5b, 1); | |
731 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 0x12); | |
732 | static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan_min, | |
733 | store_fan_min, 0x4b, 0x4e); | |
734 | static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 2); | |
735 | static SENSOR_DEVICE_ATTR_2(fan3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep, | |
736 | 0x5b, 2); | |
737 | ||
738 | static struct attribute *nct7802_fan_attrs[] = { | |
739 | &sensor_dev_attr_fan1_input.dev_attr.attr, | |
740 | &sensor_dev_attr_fan1_min.dev_attr.attr, | |
741 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | |
742 | &sensor_dev_attr_fan1_beep.dev_attr.attr, | |
743 | &sensor_dev_attr_fan2_input.dev_attr.attr, | |
744 | &sensor_dev_attr_fan2_min.dev_attr.attr, | |
745 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, | |
746 | &sensor_dev_attr_fan2_beep.dev_attr.attr, | |
747 | &sensor_dev_attr_fan3_input.dev_attr.attr, | |
748 | &sensor_dev_attr_fan3_min.dev_attr.attr, | |
749 | &sensor_dev_attr_fan3_alarm.dev_attr.attr, | |
750 | &sensor_dev_attr_fan3_beep.dev_attr.attr, | |
751 | ||
752 | NULL | |
753 | }; | |
754 | ||
755 | static umode_t nct7802_fan_is_visible(struct kobject *kobj, | |
756 | struct attribute *attr, int index) | |
757 | { | |
758 | struct device *dev = container_of(kobj, struct device, kobj); | |
759 | struct nct7802_data *data = dev_get_drvdata(dev); | |
760 | int fan = index / 4; /* 4 attributes per fan */ | |
761 | unsigned int reg; | |
762 | int err; | |
763 | ||
764 | err = regmap_read(data->regmap, REG_FAN_ENABLE, ®); | |
765 | if (err < 0 || !(reg & (1 << fan))) | |
766 | return 0; | |
767 | ||
768 | return attr->mode; | |
769 | } | |
770 | ||
771 | static struct attribute_group nct7802_fan_group = { | |
772 | .attrs = nct7802_fan_attrs, | |
773 | .is_visible = nct7802_fan_is_visible, | |
774 | }; | |
775 | ||
776 | static const struct attribute_group *nct7802_groups[] = { | |
777 | &nct7802_temp_group, | |
778 | &nct7802_in_group, | |
779 | &nct7802_fan_group, | |
780 | NULL | |
781 | }; | |
782 | ||
783 | static int nct7802_detect(struct i2c_client *client, | |
784 | struct i2c_board_info *info) | |
785 | { | |
786 | int reg; | |
787 | ||
788 | /* | |
789 | * Chip identification registers are only available in bank 0, | |
790 | * so only attempt chip detection if bank 0 is selected | |
791 | */ | |
792 | reg = i2c_smbus_read_byte_data(client, REG_BANK); | |
793 | if (reg != 0x00) | |
794 | return -ENODEV; | |
795 | ||
796 | reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID); | |
797 | if (reg != 0x50) | |
798 | return -ENODEV; | |
799 | ||
800 | reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID); | |
801 | if (reg != 0xc3) | |
802 | return -ENODEV; | |
803 | ||
804 | reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID); | |
805 | if (reg < 0 || (reg & 0xf0) != 0x20) | |
806 | return -ENODEV; | |
807 | ||
808 | /* Also validate lower bits of voltage and temperature registers */ | |
809 | reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB); | |
810 | if (reg < 0 || (reg & 0x1f)) | |
811 | return -ENODEV; | |
812 | ||
813 | reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB); | |
814 | if (reg < 0 || (reg & 0x3f)) | |
815 | return -ENODEV; | |
816 | ||
817 | reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW); | |
818 | if (reg < 0 || (reg & 0x3f)) | |
819 | return -ENODEV; | |
820 | ||
821 | strlcpy(info->type, "nct7802", I2C_NAME_SIZE); | |
822 | return 0; | |
823 | } | |
824 | ||
825 | static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg) | |
826 | { | |
827 | return reg != REG_BANK && reg <= 0x20; | |
828 | } | |
829 | ||
3c535bca | 830 | static const struct regmap_config nct7802_regmap_config = { |
3434f378 GR |
831 | .reg_bits = 8, |
832 | .val_bits = 8, | |
833 | .cache_type = REGCACHE_RBTREE, | |
834 | .volatile_reg = nct7802_regmap_is_volatile, | |
835 | }; | |
836 | ||
837 | static int nct7802_init_chip(struct nct7802_data *data) | |
838 | { | |
839 | int err; | |
840 | ||
841 | /* Enable ADC */ | |
842 | err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01); | |
843 | if (err) | |
844 | return err; | |
845 | ||
846 | /* Enable local temperature sensor */ | |
847 | err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40); | |
848 | if (err) | |
849 | return err; | |
850 | ||
851 | /* Enable Vcore and VCC voltage monitoring */ | |
852 | return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03); | |
853 | } | |
854 | ||
855 | static int nct7802_probe(struct i2c_client *client, | |
856 | const struct i2c_device_id *id) | |
857 | { | |
858 | struct device *dev = &client->dev; | |
859 | struct nct7802_data *data; | |
860 | struct device *hwmon_dev; | |
861 | int ret; | |
862 | ||
863 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); | |
864 | if (data == NULL) | |
865 | return -ENOMEM; | |
866 | ||
867 | data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config); | |
868 | if (IS_ERR(data->regmap)) | |
869 | return PTR_ERR(data->regmap); | |
870 | ||
871 | mutex_init(&data->access_lock); | |
872 | ||
873 | ret = nct7802_init_chip(data); | |
874 | if (ret < 0) | |
875 | return ret; | |
876 | ||
877 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, | |
878 | data, | |
879 | nct7802_groups); | |
880 | return PTR_ERR_OR_ZERO(hwmon_dev); | |
881 | } | |
882 | ||
883 | static const unsigned short nct7802_address_list[] = { | |
884 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END | |
885 | }; | |
886 | ||
887 | static const struct i2c_device_id nct7802_idtable[] = { | |
888 | { "nct7802", 0 }, | |
889 | { } | |
890 | }; | |
891 | MODULE_DEVICE_TABLE(i2c, nct7802_idtable); | |
892 | ||
893 | static struct i2c_driver nct7802_driver = { | |
894 | .class = I2C_CLASS_HWMON, | |
895 | .driver = { | |
896 | .name = DRVNAME, | |
897 | }, | |
898 | .detect = nct7802_detect, | |
899 | .probe = nct7802_probe, | |
900 | .id_table = nct7802_idtable, | |
901 | .address_list = nct7802_address_list, | |
902 | }; | |
903 | ||
904 | module_i2c_driver(nct7802_driver); | |
905 | ||
906 | MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); | |
907 | MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver"); | |
908 | MODULE_LICENSE("GPL v2"); |