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