1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#include <linux/acpi.h>
25#include <linux/delay.h>
26#include <linux/err.h>
27#include <linux/init.h>
28#include <linux/io.h>
29#include <linux/jiffies.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/module.h>
33#include <linux/mutex.h>
34#include <linux/platform_device.h>
35#include <linux/slab.h>
36
37enum kinds { nct6683, nct6686, nct6687 };
38
39static bool force;
40module_param(force, bool, 0);
41MODULE_PARM_DESC(force, "Set to one to enable support for unknown vendors");
42
43static const char * const nct6683_device_names[] = {
44 "nct6683",
45 "nct6686",
46 "nct6687",
47};
48
49static const char * const nct6683_chip_names[] = {
50 "NCT6683D",
51 "NCT6686D",
52 "NCT6687D",
53};
54
55#define DRVNAME "nct6683"
56
57
58
59
60
61#define NCT6683_LD_ACPI 0x0a
62#define NCT6683_LD_HWM 0x0b
63#define NCT6683_LD_VID 0x0d
64
65#define SIO_REG_LDSEL 0x07
66#define SIO_REG_DEVID 0x20
67#define SIO_REG_ENABLE 0x30
68#define SIO_REG_ADDR 0x60
69
70#define SIO_NCT6681_ID 0xb270
71#define SIO_NCT6683_ID 0xc730
72#define SIO_NCT6686_ID 0xd440
73#define SIO_NCT6687_ID 0xd590
74#define SIO_ID_MASK 0xFFF0
75
76static inline void
77superio_outb(int ioreg, int reg, int val)
78{
79 outb(reg, ioreg);
80 outb(val, ioreg + 1);
81}
82
83static inline int
84superio_inb(int ioreg, int reg)
85{
86 outb(reg, ioreg);
87 return inb(ioreg + 1);
88}
89
90static inline void
91superio_select(int ioreg, int ld)
92{
93 outb(SIO_REG_LDSEL, ioreg);
94 outb(ld, ioreg + 1);
95}
96
97static inline int
98superio_enter(int ioreg)
99{
100
101
102
103 if (!request_muxed_region(ioreg, 2, DRVNAME))
104 return -EBUSY;
105
106 outb(0x87, ioreg);
107 outb(0x87, ioreg);
108
109 return 0;
110}
111
112static inline void
113superio_exit(int ioreg)
114{
115 outb(0xaa, ioreg);
116 outb(0x02, ioreg);
117 outb(0x02, ioreg + 1);
118 release_region(ioreg, 2);
119}
120
121
122
123
124
125#define IOREGION_ALIGNMENT (~7)
126#define IOREGION_OFFSET 4
127#define IOREGION_LENGTH 4
128
129#define EC_PAGE_REG 0
130#define EC_INDEX_REG 1
131#define EC_DATA_REG 2
132#define EC_EVENT_REG 3
133
134
135
136#define NCT6683_NUM_REG_MON 32
137#define NCT6683_NUM_REG_FAN 16
138#define NCT6683_NUM_REG_PWM 8
139
140#define NCT6683_REG_MON(x) (0x100 + (x) * 2)
141#define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
142#define NCT6683_REG_PWM(x) (0x160 + (x))
143#define NCT6683_REG_PWM_WRITE(x) (0xa28 + (x))
144
145#define NCT6683_REG_MON_STS(x) (0x174 + (x))
146#define NCT6683_REG_IDLE(x) (0x178 + (x))
147
148#define NCT6683_REG_FAN_STS(x) (0x17c + (x))
149#define NCT6683_REG_FAN_ERRSTS 0x17e
150#define NCT6683_REG_FAN_INITSTS 0x17f
151
152#define NCT6683_HWM_CFG 0x180
153
154#define NCT6683_REG_MON_CFG(x) (0x1a0 + (x))
155#define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x))
156#define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x))
157
158#define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16)
159#define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16)
160
161#define NCT6683_REG_TEMP_HYST(x) (0x330 + (x))
162#define NCT6683_REG_TEMP_MAX(x) (0x350 + (x))
163#define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2)
164#define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2)
165
166#define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2)
167
168#define NCT6683_REG_FAN_CFG_CTRL 0xa01
169#define NCT6683_FAN_CFG_REQ 0x80
170#define NCT6683_FAN_CFG_DONE 0x40
171
172#define NCT6683_REG_CUSTOMER_ID 0x602
173#define NCT6683_CUSTOMER_ID_INTEL 0x805
174#define NCT6683_CUSTOMER_ID_MITAC 0xa0e
175#define NCT6683_CUSTOMER_ID_MSI 0x201
176#define NCT6683_CUSTOMER_ID_ASROCK 0xe2c
177
178#define NCT6683_REG_BUILD_YEAR 0x604
179#define NCT6683_REG_BUILD_MONTH 0x605
180#define NCT6683_REG_BUILD_DAY 0x606
181#define NCT6683_REG_SERIAL 0x607
182#define NCT6683_REG_VERSION_HI 0x608
183#define NCT6683_REG_VERSION_LO 0x609
184
185#define NCT6683_REG_CR_CASEOPEN 0xe8
186#define NCT6683_CR_CASEOPEN_MASK (1 << 7)
187
188#define NCT6683_REG_CR_BEEP 0xe0
189#define NCT6683_CR_BEEP_MASK (1 << 6)
190
191static const char *const nct6683_mon_label[] = {
192 NULL,
193 "Local",
194 "Diode 0 (curr)",
195 "Diode 1 (curr)",
196 "Diode 2 (curr)",
197 "Diode 0 (volt)",
198 "Diode 1 (volt)",
199 "Diode 2 (volt)",
200 "Thermistor 14",
201 "Thermistor 15",
202 "Thermistor 16",
203 "Thermistor 0",
204 "Thermistor 1",
205 "Thermistor 2",
206 "Thermistor 3",
207 "Thermistor 4",
208 "Thermistor 5",
209 "Thermistor 6",
210 "Thermistor 7",
211 "Thermistor 8",
212 "Thermistor 9",
213 "Thermistor 10",
214 "Thermistor 11",
215 "Thermistor 12",
216 "Thermistor 13",
217 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
218 "PECI 0.0",
219 "PECI 1.0",
220 "PECI 2.0",
221 "PECI 3.0",
222 "PECI 0.1",
223 "PECI 1.1",
224 "PECI 2.1",
225 "PECI 3.1",
226 "PECI DIMM 0",
227 "PECI DIMM 1",
228 "PECI DIMM 2",
229 "PECI DIMM 3",
230 NULL, NULL, NULL, NULL,
231 "PCH CPU",
232 "PCH CHIP",
233 "PCH CHIP CPU MAX",
234 "PCH MCH",
235 "PCH DIMM 0",
236 "PCH DIMM 1",
237 "PCH DIMM 2",
238 "PCH DIMM 3",
239 "SMBus 0",
240 "SMBus 1",
241 "SMBus 2",
242 "SMBus 3",
243 "SMBus 4",
244 "SMBus 5",
245 "DIMM 0",
246 "DIMM 1",
247 "DIMM 2",
248 "DIMM 3",
249 "AMD TSI Addr 90h",
250 "AMD TSI Addr 92h",
251 "AMD TSI Addr 94h",
252 "AMD TSI Addr 96h",
253 "AMD TSI Addr 98h",
254 "AMD TSI Addr 9ah",
255 "AMD TSI Addr 9ch",
256 "AMD TSI Addr 9dh",
257 NULL, NULL, NULL, NULL, NULL, NULL,
258 "Virtual 0",
259 "Virtual 1",
260 "Virtual 2",
261 "Virtual 3",
262 "Virtual 4",
263 "Virtual 5",
264 "Virtual 6",
265 "Virtual 7",
266 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
267 "VCC",
268 "VSB",
269 "AVSB",
270 "VTT",
271 "VBAT",
272 "VREF",
273 "VIN0",
274 "VIN1",
275 "VIN2",
276 "VIN3",
277 "VIN4",
278 "VIN5",
279 "VIN6",
280 "VIN7",
281 "VIN8",
282 "VIN9",
283 "VIN10",
284 "VIN11",
285 "VIN12",
286 "VIN13",
287 "VIN14",
288 "VIN15",
289 "VIN16",
290};
291
292#define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
293#define MON_VOLTAGE_START 0x60
294
295
296
297struct nct6683_data {
298 int addr;
299 int sioreg;
300 enum kinds kind;
301 u16 customer_id;
302
303 struct device *hwmon_dev;
304 const struct attribute_group *groups[6];
305
306 int temp_num;
307 u8 temp_index[NCT6683_NUM_REG_MON];
308 u8 temp_src[NCT6683_NUM_REG_MON];
309
310 u8 in_num;
311 u8 in_index[NCT6683_NUM_REG_MON];
312 u8 in_src[NCT6683_NUM_REG_MON];
313
314 struct mutex update_lock;
315 bool valid;
316 unsigned long last_updated;
317
318
319 u8 in[3][NCT6683_NUM_REG_MON];
320
321
322 s16 temp_in[NCT6683_NUM_REG_MON];
323 s8 temp[4][NCT6683_NUM_REG_MON];
324
325
326
327
328 unsigned int rpm[NCT6683_NUM_REG_FAN];
329 u16 fan_min[NCT6683_NUM_REG_FAN];
330 u8 fanin_cfg[NCT6683_NUM_REG_FAN];
331 u8 fanout_cfg[NCT6683_NUM_REG_FAN];
332 u16 have_fan;
333
334 u8 have_pwm;
335 u8 pwm[NCT6683_NUM_REG_PWM];
336
337#ifdef CONFIG_PM
338
339 u8 hwm_cfg;
340#endif
341};
342
343struct nct6683_sio_data {
344 int sioreg;
345 enum kinds kind;
346};
347
348struct sensor_device_template {
349 struct device_attribute dev_attr;
350 union {
351 struct {
352 u8 nr;
353 u8 index;
354 } s;
355 int index;
356 } u;
357 bool s2;
358};
359
360struct sensor_device_attr_u {
361 union {
362 struct sensor_device_attribute a1;
363 struct sensor_device_attribute_2 a2;
364 } u;
365 char name[32];
366};
367
368#define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
369 .attr = {.name = _template, .mode = _mode }, \
370 .show = _show, \
371 .store = _store, \
372}
373
374#define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
375 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
376 .u.index = _index, \
377 .s2 = false }
378
379#define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
380 _nr, _index) \
381 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
382 .u.s.index = _index, \
383 .u.s.nr = _nr, \
384 .s2 = true }
385
386#define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
387static struct sensor_device_template sensor_dev_template_##_name \
388 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
389 _index)
390
391#define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
392 _nr, _index) \
393static struct sensor_device_template sensor_dev_template_##_name \
394 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
395 _nr, _index)
396
397struct sensor_template_group {
398 struct sensor_device_template **templates;
399 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
400 int base;
401};
402
403static struct attribute_group *
404nct6683_create_attr_group(struct device *dev,
405 const struct sensor_template_group *tg,
406 int repeat)
407{
408 struct sensor_device_attribute_2 *a2;
409 struct sensor_device_attribute *a;
410 struct sensor_device_template **t;
411 struct sensor_device_attr_u *su;
412 struct attribute_group *group;
413 struct attribute **attrs;
414 int i, j, count;
415
416 if (repeat <= 0)
417 return ERR_PTR(-EINVAL);
418
419 t = tg->templates;
420 for (count = 0; *t; t++, count++)
421 ;
422
423 if (count == 0)
424 return ERR_PTR(-EINVAL);
425
426 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
427 if (group == NULL)
428 return ERR_PTR(-ENOMEM);
429
430 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
431 GFP_KERNEL);
432 if (attrs == NULL)
433 return ERR_PTR(-ENOMEM);
434
435 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
436 GFP_KERNEL);
437 if (su == NULL)
438 return ERR_PTR(-ENOMEM);
439
440 group->attrs = attrs;
441 group->is_visible = tg->is_visible;
442
443 for (i = 0; i < repeat; i++) {
444 t = tg->templates;
445 for (j = 0; *t != NULL; j++) {
446 snprintf(su->name, sizeof(su->name),
447 (*t)->dev_attr.attr.name, tg->base + i);
448 if ((*t)->s2) {
449 a2 = &su->u.a2;
450 sysfs_attr_init(&a2->dev_attr.attr);
451 a2->dev_attr.attr.name = su->name;
452 a2->nr = (*t)->u.s.nr + i;
453 a2->index = (*t)->u.s.index;
454 a2->dev_attr.attr.mode =
455 (*t)->dev_attr.attr.mode;
456 a2->dev_attr.show = (*t)->dev_attr.show;
457 a2->dev_attr.store = (*t)->dev_attr.store;
458 *attrs = &a2->dev_attr.attr;
459 } else {
460 a = &su->u.a1;
461 sysfs_attr_init(&a->dev_attr.attr);
462 a->dev_attr.attr.name = su->name;
463 a->index = (*t)->u.index + i;
464 a->dev_attr.attr.mode =
465 (*t)->dev_attr.attr.mode;
466 a->dev_attr.show = (*t)->dev_attr.show;
467 a->dev_attr.store = (*t)->dev_attr.store;
468 *attrs = &a->dev_attr.attr;
469 }
470 attrs++;
471 su++;
472 t++;
473 }
474 }
475
476 return group;
477}
478
479
480#define MON_SRC_VCC 0x60
481#define MON_SRC_VSB 0x61
482#define MON_SRC_AVSB 0x62
483#define MON_SRC_VBAT 0x64
484
485static inline long in_from_reg(u16 reg, u8 src)
486{
487 int scale = 16;
488
489 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
490 src == MON_SRC_VBAT)
491 scale <<= 1;
492 return reg * scale;
493}
494
495static u16 nct6683_read(struct nct6683_data *data, u16 reg)
496{
497 int res;
498
499 outb_p(0xff, data->addr + EC_PAGE_REG);
500 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
501 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
502 res = inb_p(data->addr + EC_DATA_REG);
503 return res;
504}
505
506static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
507{
508 return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
509}
510
511static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
512{
513 outb_p(0xff, data->addr + EC_PAGE_REG);
514 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
515 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
516 outb_p(value & 0xff, data->addr + EC_DATA_REG);
517}
518
519static int get_in_reg(struct nct6683_data *data, int nr, int index)
520{
521 int ch = data->in_index[index];
522 int reg = -EINVAL;
523
524 switch (nr) {
525 case 0:
526 reg = NCT6683_REG_MON(ch);
527 break;
528 case 1:
529 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
530 reg = NCT6683_REG_MON_LOW(ch);
531 break;
532 case 2:
533 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
534 reg = NCT6683_REG_MON_HIGH(ch);
535 break;
536 default:
537 break;
538 }
539 return reg;
540}
541
542static int get_temp_reg(struct nct6683_data *data, int nr, int index)
543{
544 int ch = data->temp_index[index];
545 int reg = -EINVAL;
546
547 switch (data->customer_id) {
548 case NCT6683_CUSTOMER_ID_INTEL:
549 switch (nr) {
550 default:
551 case 1:
552 reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
553 break;
554 case 3:
555 reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
556 break;
557 }
558 break;
559 case NCT6683_CUSTOMER_ID_MITAC:
560 default:
561 switch (nr) {
562 default:
563 case 0:
564 reg = NCT6683_REG_MON_LOW(ch);
565 break;
566 case 1:
567 reg = NCT6683_REG_TEMP_MAX(ch);
568 break;
569 case 2:
570 reg = NCT6683_REG_TEMP_HYST(ch);
571 break;
572 case 3:
573 reg = NCT6683_REG_MON_HIGH(ch);
574 break;
575 }
576 break;
577 }
578 return reg;
579}
580
581static void nct6683_update_pwm(struct device *dev)
582{
583 struct nct6683_data *data = dev_get_drvdata(dev);
584 int i;
585
586 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
587 if (!(data->have_pwm & (1 << i)))
588 continue;
589 data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
590 }
591}
592
593static struct nct6683_data *nct6683_update_device(struct device *dev)
594{
595 struct nct6683_data *data = dev_get_drvdata(dev);
596 int i, j;
597
598 mutex_lock(&data->update_lock);
599
600 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
601
602 for (i = 0; i < data->in_num; i++) {
603 for (j = 0; j < 3; j++) {
604 int reg = get_in_reg(data, j, i);
605
606 if (reg >= 0)
607 data->in[j][i] =
608 nct6683_read(data, reg);
609 }
610 }
611
612
613 for (i = 0; i < data->temp_num; i++) {
614 u8 ch = data->temp_index[i];
615
616 data->temp_in[i] = nct6683_read16(data,
617 NCT6683_REG_MON(ch));
618 for (j = 0; j < 4; j++) {
619 int reg = get_temp_reg(data, j, i);
620
621 if (reg >= 0)
622 data->temp[j][i] =
623 nct6683_read(data, reg);
624 }
625 }
626
627
628 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
629 if (!(data->have_fan & (1 << i)))
630 continue;
631
632 data->rpm[i] = nct6683_read16(data,
633 NCT6683_REG_FAN_RPM(i));
634 data->fan_min[i] = nct6683_read16(data,
635 NCT6683_REG_FAN_MIN(i));
636 }
637
638 nct6683_update_pwm(dev);
639
640 data->last_updated = jiffies;
641 data->valid = true;
642 }
643
644 mutex_unlock(&data->update_lock);
645 return data;
646}
647
648
649
650
651static ssize_t
652show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
653{
654 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
655 struct nct6683_data *data = nct6683_update_device(dev);
656 int nr = sattr->index;
657
658 return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
659}
660
661static ssize_t
662show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
663{
664 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
665 struct nct6683_data *data = nct6683_update_device(dev);
666 int index = sattr->index;
667 int nr = sattr->nr;
668
669 return sprintf(buf, "%ld\n",
670 in_from_reg(data->in[index][nr], data->in_index[index]));
671}
672
673static umode_t nct6683_in_is_visible(struct kobject *kobj,
674 struct attribute *attr, int index)
675{
676 struct device *dev = kobj_to_dev(kobj);
677 struct nct6683_data *data = dev_get_drvdata(dev);
678 int nr = index % 4;
679
680
681
682
683
684 if ((nr == 2 || nr == 3) &&
685 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
686 return 0;
687
688 return attr->mode;
689}
690
691SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
692SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
693SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
694SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
695
696static struct sensor_device_template *nct6683_attributes_in_template[] = {
697 &sensor_dev_template_in_label,
698 &sensor_dev_template_in_input,
699 &sensor_dev_template_in_min,
700 &sensor_dev_template_in_max,
701 NULL
702};
703
704static const struct sensor_template_group nct6683_in_template_group = {
705 .templates = nct6683_attributes_in_template,
706 .is_visible = nct6683_in_is_visible,
707};
708
709static ssize_t
710show_fan(struct device *dev, struct device_attribute *attr, char *buf)
711{
712 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
713 struct nct6683_data *data = nct6683_update_device(dev);
714
715 return sprintf(buf, "%d\n", data->rpm[sattr->index]);
716}
717
718static ssize_t
719show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
720{
721 struct nct6683_data *data = nct6683_update_device(dev);
722 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
723 int nr = sattr->index;
724
725 return sprintf(buf, "%d\n", data->fan_min[nr]);
726}
727
728static ssize_t
729show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
730{
731 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
732 struct nct6683_data *data = nct6683_update_device(dev);
733
734 return sprintf(buf, "%d\n",
735 ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
736}
737
738static umode_t nct6683_fan_is_visible(struct kobject *kobj,
739 struct attribute *attr, int index)
740{
741 struct device *dev = kobj_to_dev(kobj);
742 struct nct6683_data *data = dev_get_drvdata(dev);
743 int fan = index / 3;
744 int nr = index % 3;
745
746 if (!(data->have_fan & (1 << fan)))
747 return 0;
748
749
750
751
752
753 if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
754 return 0;
755
756 return attr->mode;
757}
758
759SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
760SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
761SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
762
763
764
765
766
767
768static struct sensor_device_template *nct6683_attributes_fan_template[] = {
769 &sensor_dev_template_fan_input,
770 &sensor_dev_template_fan_pulses,
771 &sensor_dev_template_fan_min,
772 NULL
773};
774
775static const struct sensor_template_group nct6683_fan_template_group = {
776 .templates = nct6683_attributes_fan_template,
777 .is_visible = nct6683_fan_is_visible,
778 .base = 1,
779};
780
781static ssize_t
782show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
783{
784 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
785 struct nct6683_data *data = nct6683_update_device(dev);
786 int nr = sattr->index;
787
788 return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
789}
790
791static ssize_t
792show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
793{
794 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
795 struct nct6683_data *data = nct6683_update_device(dev);
796 int index = sattr->index;
797 int nr = sattr->nr;
798
799 return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
800}
801
802static ssize_t
803show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
804{
805 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
806 struct nct6683_data *data = nct6683_update_device(dev);
807 int nr = sattr->index;
808 int temp = data->temp[1][nr] - data->temp[2][nr];
809
810 return sprintf(buf, "%d\n", temp * 1000);
811}
812
813static ssize_t
814show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
815{
816 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
817 struct nct6683_data *data = nct6683_update_device(dev);
818 int index = sattr->index;
819
820 return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
821}
822
823
824
825
826
827
828
829
830
831
832
833static int get_temp_type(u8 src)
834{
835 if (src >= 0x02 && src <= 0x07)
836 return 3;
837 else if (src >= 0x08 && src <= 0x18)
838 return 4;
839 else if (src >= 0x20 && src <= 0x2b)
840 return 6;
841 else if (src >= 0x42 && src <= 0x49)
842 return 5;
843
844 return 0;
845}
846
847static ssize_t
848show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
849{
850 struct nct6683_data *data = nct6683_update_device(dev);
851 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
852 int nr = sattr->index;
853 return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
854}
855
856static umode_t nct6683_temp_is_visible(struct kobject *kobj,
857 struct attribute *attr, int index)
858{
859 struct device *dev = kobj_to_dev(kobj);
860 struct nct6683_data *data = dev_get_drvdata(dev);
861 int temp = index / 7;
862 int nr = index % 7;
863
864
865
866
867
868 if ((nr == 2 || nr == 4) &&
869 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
870 return 0;
871
872 if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
873 return 0;
874
875 return attr->mode;
876}
877
878SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
879SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
880SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
881SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
882SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
883 0);
884SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
885SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
886
887
888
889
890
891
892static struct sensor_device_template *nct6683_attributes_temp_template[] = {
893 &sensor_dev_template_temp_input,
894 &sensor_dev_template_temp_label,
895 &sensor_dev_template_temp_min,
896 &sensor_dev_template_temp_max,
897 &sensor_dev_template_temp_max_hyst,
898 &sensor_dev_template_temp_crit,
899 &sensor_dev_template_temp_type,
900 NULL
901};
902
903static const struct sensor_template_group nct6683_temp_template_group = {
904 .templates = nct6683_attributes_temp_template,
905 .is_visible = nct6683_temp_is_visible,
906 .base = 1,
907};
908
909static ssize_t
910show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
911{
912 struct nct6683_data *data = nct6683_update_device(dev);
913 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
914 int index = sattr->index;
915
916 return sprintf(buf, "%d\n", data->pwm[index]);
917}
918
919static ssize_t
920store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
921 size_t count)
922{
923 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
924 struct nct6683_data *data = dev_get_drvdata(dev);
925 int index = sattr->index;
926 unsigned long val;
927
928 if (kstrtoul(buf, 10, &val) || val > 255)
929 return -EINVAL;
930
931 mutex_lock(&data->update_lock);
932 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_REQ);
933 usleep_range(1000, 2000);
934 nct6683_write(data, NCT6683_REG_PWM_WRITE(index), val);
935 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_DONE);
936 mutex_unlock(&data->update_lock);
937
938 return count;
939}
940
941SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0);
942
943static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
944 struct attribute *attr, int index)
945{
946 struct device *dev = kobj_to_dev(kobj);
947 struct nct6683_data *data = dev_get_drvdata(dev);
948 int pwm = index;
949
950 if (!(data->have_pwm & (1 << pwm)))
951 return 0;
952
953
954 if (data->customer_id == NCT6683_CUSTOMER_ID_MITAC)
955 return attr->mode | S_IWUSR;
956
957 return attr->mode;
958}
959
960static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
961 &sensor_dev_template_pwm,
962 NULL
963};
964
965static const struct sensor_template_group nct6683_pwm_template_group = {
966 .templates = nct6683_attributes_pwm_template,
967 .is_visible = nct6683_pwm_is_visible,
968 .base = 1,
969};
970
971static ssize_t
972beep_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
973{
974 struct nct6683_data *data = dev_get_drvdata(dev);
975 int ret;
976 u8 reg;
977
978 mutex_lock(&data->update_lock);
979
980 ret = superio_enter(data->sioreg);
981 if (ret)
982 goto error;
983 superio_select(data->sioreg, NCT6683_LD_HWM);
984 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
985 superio_exit(data->sioreg);
986
987 mutex_unlock(&data->update_lock);
988
989 return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
990
991error:
992 mutex_unlock(&data->update_lock);
993 return ret;
994}
995
996static ssize_t
997beep_enable_store(struct device *dev, struct device_attribute *attr,
998 const char *buf, size_t count)
999{
1000 struct nct6683_data *data = dev_get_drvdata(dev);
1001 unsigned long val;
1002 u8 reg;
1003 int ret;
1004
1005 if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
1006 return -EINVAL;
1007
1008 mutex_lock(&data->update_lock);
1009
1010 ret = superio_enter(data->sioreg);
1011 if (ret) {
1012 count = ret;
1013 goto error;
1014 }
1015
1016 superio_select(data->sioreg, NCT6683_LD_HWM);
1017 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
1018 if (val)
1019 reg |= NCT6683_CR_BEEP_MASK;
1020 else
1021 reg &= ~NCT6683_CR_BEEP_MASK;
1022 superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
1023 superio_exit(data->sioreg);
1024error:
1025 mutex_unlock(&data->update_lock);
1026 return count;
1027}
1028
1029
1030
1031static ssize_t
1032intrusion0_alarm_show(struct device *dev, struct device_attribute *attr,
1033 char *buf)
1034{
1035 struct nct6683_data *data = dev_get_drvdata(dev);
1036 int ret;
1037 u8 reg;
1038
1039 mutex_lock(&data->update_lock);
1040
1041 ret = superio_enter(data->sioreg);
1042 if (ret)
1043 goto error;
1044 superio_select(data->sioreg, NCT6683_LD_ACPI);
1045 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1046 superio_exit(data->sioreg);
1047
1048 mutex_unlock(&data->update_lock);
1049
1050 return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
1051
1052error:
1053 mutex_unlock(&data->update_lock);
1054 return ret;
1055}
1056
1057static ssize_t
1058intrusion0_alarm_store(struct device *dev, struct device_attribute *attr,
1059 const char *buf, size_t count)
1060{
1061 struct nct6683_data *data = dev_get_drvdata(dev);
1062 unsigned long val;
1063 u8 reg;
1064 int ret;
1065
1066 if (kstrtoul(buf, 10, &val) || val != 0)
1067 return -EINVAL;
1068
1069 mutex_lock(&data->update_lock);
1070
1071
1072
1073
1074
1075
1076 ret = superio_enter(data->sioreg);
1077 if (ret) {
1078 count = ret;
1079 goto error;
1080 }
1081
1082 superio_select(data->sioreg, NCT6683_LD_ACPI);
1083 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1084 reg |= NCT6683_CR_CASEOPEN_MASK;
1085 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1086 reg &= ~NCT6683_CR_CASEOPEN_MASK;
1087 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1088 superio_exit(data->sioreg);
1089
1090 data->valid = false;
1091error:
1092 mutex_unlock(&data->update_lock);
1093 return count;
1094}
1095
1096static DEVICE_ATTR_RW(intrusion0_alarm);
1097static DEVICE_ATTR_RW(beep_enable);
1098
1099static struct attribute *nct6683_attributes_other[] = {
1100 &dev_attr_intrusion0_alarm.attr,
1101 &dev_attr_beep_enable.attr,
1102 NULL
1103};
1104
1105static const struct attribute_group nct6683_group_other = {
1106 .attrs = nct6683_attributes_other,
1107};
1108
1109
1110static inline void nct6683_init_device(struct nct6683_data *data)
1111{
1112 u8 tmp;
1113
1114
1115 tmp = nct6683_read(data, NCT6683_HWM_CFG);
1116 if (!(tmp & 0x80))
1117 nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
1118}
1119
1120
1121
1122
1123
1124static void
1125nct6683_setup_fans(struct nct6683_data *data)
1126{
1127 int i;
1128 u8 reg;
1129
1130 for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
1131 reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
1132 if (reg & 0x80)
1133 data->have_fan |= 1 << i;
1134 data->fanin_cfg[i] = reg;
1135 }
1136 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
1137 reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
1138 if (reg & 0x80)
1139 data->have_pwm |= 1 << i;
1140 data->fanout_cfg[i] = reg;
1141 }
1142}
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160static void nct6683_setup_sensors(struct nct6683_data *data)
1161{
1162 u8 reg;
1163 int i;
1164
1165 data->temp_num = 0;
1166 data->in_num = 0;
1167 for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
1168 reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
1169
1170 if (reg >= NUM_MON_LABELS)
1171 continue;
1172
1173 if (nct6683_mon_label[reg] == NULL)
1174 continue;
1175 if (reg < MON_VOLTAGE_START) {
1176 data->temp_index[data->temp_num] = i;
1177 data->temp_src[data->temp_num] = reg;
1178 data->temp_num++;
1179 } else {
1180 data->in_index[data->in_num] = i;
1181 data->in_src[data->in_num] = reg;
1182 data->in_num++;
1183 }
1184 }
1185}
1186
1187static int nct6683_probe(struct platform_device *pdev)
1188{
1189 struct device *dev = &pdev->dev;
1190 struct nct6683_sio_data *sio_data = dev->platform_data;
1191 struct attribute_group *group;
1192 struct nct6683_data *data;
1193 struct device *hwmon_dev;
1194 struct resource *res;
1195 int groups = 0;
1196 char build[16];
1197
1198 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1199 if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
1200 return -EBUSY;
1201
1202 data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
1203 if (!data)
1204 return -ENOMEM;
1205
1206 data->kind = sio_data->kind;
1207 data->sioreg = sio_data->sioreg;
1208 data->addr = res->start;
1209 mutex_init(&data->update_lock);
1210 platform_set_drvdata(pdev, data);
1211
1212 data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
1213
1214
1215 switch (data->customer_id) {
1216 case NCT6683_CUSTOMER_ID_INTEL:
1217 break;
1218 case NCT6683_CUSTOMER_ID_MITAC:
1219 break;
1220 case NCT6683_CUSTOMER_ID_MSI:
1221 break;
1222 case NCT6683_CUSTOMER_ID_ASROCK:
1223 break;
1224 default:
1225 if (!force)
1226 return -ENODEV;
1227 }
1228
1229 nct6683_init_device(data);
1230 nct6683_setup_fans(data);
1231 nct6683_setup_sensors(data);
1232
1233
1234
1235 if (data->have_pwm) {
1236 group = nct6683_create_attr_group(dev,
1237 &nct6683_pwm_template_group,
1238 fls(data->have_pwm));
1239 if (IS_ERR(group))
1240 return PTR_ERR(group);
1241 data->groups[groups++] = group;
1242 }
1243
1244 if (data->in_num) {
1245 group = nct6683_create_attr_group(dev,
1246 &nct6683_in_template_group,
1247 data->in_num);
1248 if (IS_ERR(group))
1249 return PTR_ERR(group);
1250 data->groups[groups++] = group;
1251 }
1252
1253 if (data->have_fan) {
1254 group = nct6683_create_attr_group(dev,
1255 &nct6683_fan_template_group,
1256 fls(data->have_fan));
1257 if (IS_ERR(group))
1258 return PTR_ERR(group);
1259 data->groups[groups++] = group;
1260 }
1261
1262 if (data->temp_num) {
1263 group = nct6683_create_attr_group(dev,
1264 &nct6683_temp_template_group,
1265 data->temp_num);
1266 if (IS_ERR(group))
1267 return PTR_ERR(group);
1268 data->groups[groups++] = group;
1269 }
1270 data->groups[groups++] = &nct6683_group_other;
1271
1272 if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
1273 scnprintf(build, sizeof(build), "%02x/%02x/%02x",
1274 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1275 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1276 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1277 else
1278 scnprintf(build, sizeof(build), "%02d/%02d/%02d",
1279 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1280 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1281 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1282
1283 dev_info(dev, "%s EC firmware version %d.%d build %s\n",
1284 nct6683_chip_names[data->kind],
1285 nct6683_read(data, NCT6683_REG_VERSION_HI),
1286 nct6683_read(data, NCT6683_REG_VERSION_LO),
1287 build);
1288
1289 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1290 nct6683_device_names[data->kind], data, data->groups);
1291 return PTR_ERR_OR_ZERO(hwmon_dev);
1292}
1293
1294#ifdef CONFIG_PM
1295static int nct6683_suspend(struct device *dev)
1296{
1297 struct nct6683_data *data = nct6683_update_device(dev);
1298
1299 mutex_lock(&data->update_lock);
1300 data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1301 mutex_unlock(&data->update_lock);
1302
1303 return 0;
1304}
1305
1306static int nct6683_resume(struct device *dev)
1307{
1308 struct nct6683_data *data = dev_get_drvdata(dev);
1309
1310 mutex_lock(&data->update_lock);
1311
1312 nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1313
1314
1315 data->valid = false;
1316 mutex_unlock(&data->update_lock);
1317
1318 return 0;
1319}
1320
1321static const struct dev_pm_ops nct6683_dev_pm_ops = {
1322 .suspend = nct6683_suspend,
1323 .resume = nct6683_resume,
1324 .freeze = nct6683_suspend,
1325 .restore = nct6683_resume,
1326};
1327
1328#define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1329#else
1330#define NCT6683_DEV_PM_OPS NULL
1331#endif
1332
1333static struct platform_driver nct6683_driver = {
1334 .driver = {
1335 .name = DRVNAME,
1336 .pm = NCT6683_DEV_PM_OPS,
1337 },
1338 .probe = nct6683_probe,
1339};
1340
1341static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1342{
1343 int addr;
1344 u16 val;
1345 int err;
1346
1347 err = superio_enter(sioaddr);
1348 if (err)
1349 return err;
1350
1351 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1352 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1353
1354 switch (val & SIO_ID_MASK) {
1355 case SIO_NCT6683_ID:
1356 sio_data->kind = nct6683;
1357 break;
1358 case SIO_NCT6686_ID:
1359 sio_data->kind = nct6686;
1360 break;
1361 case SIO_NCT6687_ID:
1362 sio_data->kind = nct6687;
1363 break;
1364 default:
1365 if (val != 0xffff)
1366 pr_debug("unsupported chip ID: 0x%04x\n", val);
1367 goto fail;
1368 }
1369
1370
1371 superio_select(sioaddr, NCT6683_LD_HWM);
1372 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1373 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1374 addr = val & IOREGION_ALIGNMENT;
1375 if (addr == 0) {
1376 pr_err("EC base I/O port unconfigured\n");
1377 goto fail;
1378 }
1379
1380
1381 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1382 if (!(val & 0x01)) {
1383 pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1384 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1385 }
1386
1387 superio_exit(sioaddr);
1388 pr_info("Found %s or compatible chip at %#x:%#x\n",
1389 nct6683_chip_names[sio_data->kind], sioaddr, addr);
1390 sio_data->sioreg = sioaddr;
1391
1392 return addr;
1393
1394fail:
1395 superio_exit(sioaddr);
1396 return -ENODEV;
1397}
1398
1399
1400
1401
1402
1403
1404
1405static struct platform_device *pdev[2];
1406
1407static int __init sensors_nct6683_init(void)
1408{
1409 struct nct6683_sio_data sio_data;
1410 int sioaddr[2] = { 0x2e, 0x4e };
1411 struct resource res;
1412 bool found = false;
1413 int address;
1414 int i, err;
1415
1416 err = platform_driver_register(&nct6683_driver);
1417 if (err)
1418 return err;
1419
1420
1421
1422
1423
1424
1425
1426
1427 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1428 address = nct6683_find(sioaddr[i], &sio_data);
1429 if (address <= 0)
1430 continue;
1431
1432 found = true;
1433
1434 pdev[i] = platform_device_alloc(DRVNAME, address);
1435 if (!pdev[i]) {
1436 err = -ENOMEM;
1437 goto exit_device_unregister;
1438 }
1439
1440 err = platform_device_add_data(pdev[i], &sio_data,
1441 sizeof(struct nct6683_sio_data));
1442 if (err)
1443 goto exit_device_put;
1444
1445 memset(&res, 0, sizeof(res));
1446 res.name = DRVNAME;
1447 res.start = address + IOREGION_OFFSET;
1448 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1449 res.flags = IORESOURCE_IO;
1450
1451 err = acpi_check_resource_conflict(&res);
1452 if (err) {
1453 platform_device_put(pdev[i]);
1454 pdev[i] = NULL;
1455 continue;
1456 }
1457
1458 err = platform_device_add_resources(pdev[i], &res, 1);
1459 if (err)
1460 goto exit_device_put;
1461
1462
1463 err = platform_device_add(pdev[i]);
1464 if (err)
1465 goto exit_device_put;
1466 }
1467 if (!found) {
1468 err = -ENODEV;
1469 goto exit_unregister;
1470 }
1471
1472 return 0;
1473
1474exit_device_put:
1475 platform_device_put(pdev[i]);
1476exit_device_unregister:
1477 while (--i >= 0) {
1478 if (pdev[i])
1479 platform_device_unregister(pdev[i]);
1480 }
1481exit_unregister:
1482 platform_driver_unregister(&nct6683_driver);
1483 return err;
1484}
1485
1486static void __exit sensors_nct6683_exit(void)
1487{
1488 int i;
1489
1490 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1491 if (pdev[i])
1492 platform_device_unregister(pdev[i]);
1493 }
1494 platform_driver_unregister(&nct6683_driver);
1495}
1496
1497MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1498MODULE_DESCRIPTION("NCT6683D driver");
1499MODULE_LICENSE("GPL");
1500
1501module_init(sensors_nct6683_init);
1502module_exit(sensors_nct6683_exit);
1503