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