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