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