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