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