Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / hwmon / nct6683.c
blob7f7e30f0de7b885c1b349e53794f41682c880d61
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * nct6683 - Driver for the hardware monitoring functionality of
4 * Nuvoton NCT6683D/NCT6687D eSIO
6 * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
8 * Derived from nct6775 driver
9 * Copyright (C) 2012, 2013 Guenter Roeck <linux@roeck-us.net>
11 * Supports the following chips:
13 * Chip #vin #fan #pwm #temp chip ID
14 * nct6683d 21(1) 16 8 32(1) 0xc730
15 * nct6687d 21(1) 16 8 32(1) 0xd590
17 * Notes:
18 * (1) Total number of vin and temp inputs is 32.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/acpi.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/io.h>
28 #include <linux/jiffies.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/module.h>
32 #include <linux/mutex.h>
33 #include <linux/platform_device.h>
34 #include <linux/slab.h>
36 enum kinds { nct6683, nct6687 };
38 static bool force;
39 module_param(force, bool, 0);
40 MODULE_PARM_DESC(force, "Set to one to enable support for unknown vendors");
42 static const char * const nct6683_device_names[] = {
43 "nct6683",
44 "nct6687",
47 static const char * const nct6683_chip_names[] = {
48 "NCT6683D",
49 "NCT6687D",
52 #define DRVNAME "nct6683"
55 * Super-I/O constants and functions
58 #define NCT6683_LD_ACPI 0x0a
59 #define NCT6683_LD_HWM 0x0b
60 #define NCT6683_LD_VID 0x0d
62 #define SIO_REG_LDSEL 0x07 /* Logical device select */
63 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
64 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
65 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
67 #define SIO_NCT6681_ID 0xb270 /* for later */
68 #define SIO_NCT6683_ID 0xc730
69 #define SIO_NCT6687_ID 0xd590
70 #define SIO_ID_MASK 0xFFF0
72 static inline void
73 superio_outb(int ioreg, int reg, int val)
75 outb(reg, ioreg);
76 outb(val, ioreg + 1);
79 static inline int
80 superio_inb(int ioreg, int reg)
82 outb(reg, ioreg);
83 return inb(ioreg + 1);
86 static inline void
87 superio_select(int ioreg, int ld)
89 outb(SIO_REG_LDSEL, ioreg);
90 outb(ld, ioreg + 1);
93 static inline int
94 superio_enter(int ioreg)
97 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
99 if (!request_muxed_region(ioreg, 2, DRVNAME))
100 return -EBUSY;
102 outb(0x87, ioreg);
103 outb(0x87, ioreg);
105 return 0;
108 static inline void
109 superio_exit(int ioreg)
111 outb(0xaa, ioreg);
112 outb(0x02, ioreg);
113 outb(0x02, ioreg + 1);
114 release_region(ioreg, 2);
118 * ISA constants
121 #define IOREGION_ALIGNMENT (~7)
122 #define IOREGION_OFFSET 4 /* Use EC port 1 */
123 #define IOREGION_LENGTH 4
125 #define EC_PAGE_REG 0
126 #define EC_INDEX_REG 1
127 #define EC_DATA_REG 2
128 #define EC_EVENT_REG 3
130 /* Common and NCT6683 specific data */
132 #define NCT6683_NUM_REG_MON 32
133 #define NCT6683_NUM_REG_FAN 16
134 #define NCT6683_NUM_REG_PWM 8
136 #define NCT6683_REG_MON(x) (0x100 + (x) * 2)
137 #define NCT6683_REG_FAN_RPM(x) (0x140 + (x) * 2)
138 #define NCT6683_REG_PWM(x) (0x160 + (x))
139 #define NCT6683_REG_PWM_WRITE(x) (0xa28 + (x))
141 #define NCT6683_REG_MON_STS(x) (0x174 + (x))
142 #define NCT6683_REG_IDLE(x) (0x178 + (x))
144 #define NCT6683_REG_FAN_STS(x) (0x17c + (x))
145 #define NCT6683_REG_FAN_ERRSTS 0x17e
146 #define NCT6683_REG_FAN_INITSTS 0x17f
148 #define NCT6683_HWM_CFG 0x180
150 #define NCT6683_REG_MON_CFG(x) (0x1a0 + (x))
151 #define NCT6683_REG_FANIN_CFG(x) (0x1c0 + (x))
152 #define NCT6683_REG_FANOUT_CFG(x) (0x1d0 + (x))
154 #define NCT6683_REG_INTEL_TEMP_MAX(x) (0x901 + (x) * 16)
155 #define NCT6683_REG_INTEL_TEMP_CRIT(x) (0x90d + (x) * 16)
157 #define NCT6683_REG_TEMP_HYST(x) (0x330 + (x)) /* 8 bit */
158 #define NCT6683_REG_TEMP_MAX(x) (0x350 + (x)) /* 8 bit */
159 #define NCT6683_REG_MON_HIGH(x) (0x370 + (x) * 2) /* 8 bit */
160 #define NCT6683_REG_MON_LOW(x) (0x371 + (x) * 2) /* 8 bit */
162 #define NCT6683_REG_FAN_MIN(x) (0x3b8 + (x) * 2) /* 16 bit */
164 #define NCT6683_REG_FAN_CFG_CTRL 0xa01
165 #define NCT6683_FAN_CFG_REQ 0x80
166 #define NCT6683_FAN_CFG_DONE 0x40
168 #define NCT6683_REG_CUSTOMER_ID 0x602
169 #define NCT6683_CUSTOMER_ID_INTEL 0x805
170 #define NCT6683_CUSTOMER_ID_MITAC 0xa0e
171 #define NCT6683_CUSTOMER_ID_MSI 0x201
173 #define NCT6683_REG_BUILD_YEAR 0x604
174 #define NCT6683_REG_BUILD_MONTH 0x605
175 #define NCT6683_REG_BUILD_DAY 0x606
176 #define NCT6683_REG_SERIAL 0x607
177 #define NCT6683_REG_VERSION_HI 0x608
178 #define NCT6683_REG_VERSION_LO 0x609
180 #define NCT6683_REG_CR_CASEOPEN 0xe8
181 #define NCT6683_CR_CASEOPEN_MASK (1 << 7)
183 #define NCT6683_REG_CR_BEEP 0xe0
184 #define NCT6683_CR_BEEP_MASK (1 << 6)
186 static const char *const nct6683_mon_label[] = {
187 NULL, /* disabled */
188 "Local",
189 "Diode 0 (curr)",
190 "Diode 1 (curr)",
191 "Diode 2 (curr)",
192 "Diode 0 (volt)",
193 "Diode 1 (volt)",
194 "Diode 2 (volt)",
195 "Thermistor 14",
196 "Thermistor 15",
197 "Thermistor 16",
198 "Thermistor 0",
199 "Thermistor 1",
200 "Thermistor 2",
201 "Thermistor 3",
202 "Thermistor 4",
203 "Thermistor 5", /* 0x10 */
204 "Thermistor 6",
205 "Thermistor 7",
206 "Thermistor 8",
207 "Thermistor 9",
208 "Thermistor 10",
209 "Thermistor 11",
210 "Thermistor 12",
211 "Thermistor 13",
212 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
213 "PECI 0.0", /* 0x20 */
214 "PECI 1.0",
215 "PECI 2.0",
216 "PECI 3.0",
217 "PECI 0.1",
218 "PECI 1.1",
219 "PECI 2.1",
220 "PECI 3.1",
221 "PECI DIMM 0",
222 "PECI DIMM 1",
223 "PECI DIMM 2",
224 "PECI DIMM 3",
225 NULL, NULL, NULL, NULL,
226 "PCH CPU", /* 0x30 */
227 "PCH CHIP",
228 "PCH CHIP CPU MAX",
229 "PCH MCH",
230 "PCH DIMM 0",
231 "PCH DIMM 1",
232 "PCH DIMM 2",
233 "PCH DIMM 3",
234 "SMBus 0",
235 "SMBus 1",
236 "SMBus 2",
237 "SMBus 3",
238 "SMBus 4",
239 "SMBus 5",
240 "DIMM 0",
241 "DIMM 1",
242 "DIMM 2", /* 0x40 */
243 "DIMM 3",
244 "AMD TSI Addr 90h",
245 "AMD TSI Addr 92h",
246 "AMD TSI Addr 94h",
247 "AMD TSI Addr 96h",
248 "AMD TSI Addr 98h",
249 "AMD TSI Addr 9ah",
250 "AMD TSI Addr 9ch",
251 "AMD TSI Addr 9dh",
252 NULL, NULL, NULL, NULL, NULL, NULL,
253 "Virtual 0", /* 0x50 */
254 "Virtual 1",
255 "Virtual 2",
256 "Virtual 3",
257 "Virtual 4",
258 "Virtual 5",
259 "Virtual 6",
260 "Virtual 7",
261 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
262 "VCC", /* 0x60 voltage sensors */
263 "VSB",
264 "AVSB",
265 "VTT",
266 "VBAT",
267 "VREF",
268 "VIN0",
269 "VIN1",
270 "VIN2",
271 "VIN3",
272 "VIN4",
273 "VIN5",
274 "VIN6",
275 "VIN7",
276 "VIN8",
277 "VIN9",
278 "VIN10",
279 "VIN11",
280 "VIN12",
281 "VIN13",
282 "VIN14",
283 "VIN15",
284 "VIN16",
287 #define NUM_MON_LABELS ARRAY_SIZE(nct6683_mon_label)
288 #define MON_VOLTAGE_START 0x60
290 /* ------------------------------------------------------- */
292 struct nct6683_data {
293 int addr; /* IO base of EC space */
294 int sioreg; /* SIO register */
295 enum kinds kind;
296 u16 customer_id;
298 struct device *hwmon_dev;
299 const struct attribute_group *groups[6];
301 int temp_num; /* number of temperature attributes */
302 u8 temp_index[NCT6683_NUM_REG_MON];
303 u8 temp_src[NCT6683_NUM_REG_MON];
305 u8 in_num; /* number of voltage attributes */
306 u8 in_index[NCT6683_NUM_REG_MON];
307 u8 in_src[NCT6683_NUM_REG_MON];
309 struct mutex update_lock; /* used to protect sensor updates */
310 bool valid; /* true if following fields are valid */
311 unsigned long last_updated; /* In jiffies */
313 /* Voltage attribute values */
314 u8 in[3][NCT6683_NUM_REG_MON]; /* [0]=in, [1]=in_max, [2]=in_min */
316 /* Temperature attribute values */
317 s16 temp_in[NCT6683_NUM_REG_MON];
318 s8 temp[4][NCT6683_NUM_REG_MON];/* [0]=min, [1]=max, [2]=hyst,
319 * [3]=crit
322 /* Fan attribute values */
323 unsigned int rpm[NCT6683_NUM_REG_FAN];
324 u16 fan_min[NCT6683_NUM_REG_FAN];
325 u8 fanin_cfg[NCT6683_NUM_REG_FAN];
326 u8 fanout_cfg[NCT6683_NUM_REG_FAN];
327 u16 have_fan; /* some fan inputs can be disabled */
329 u8 have_pwm;
330 u8 pwm[NCT6683_NUM_REG_PWM];
332 #ifdef CONFIG_PM
333 /* Remember extra register values over suspend/resume */
334 u8 hwm_cfg;
335 #endif
338 struct nct6683_sio_data {
339 int sioreg;
340 enum kinds kind;
343 struct sensor_device_template {
344 struct device_attribute dev_attr;
345 union {
346 struct {
347 u8 nr;
348 u8 index;
349 } s;
350 int index;
351 } u;
352 bool s2; /* true if both index and nr are used */
355 struct sensor_device_attr_u {
356 union {
357 struct sensor_device_attribute a1;
358 struct sensor_device_attribute_2 a2;
359 } u;
360 char name[32];
363 #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
364 .attr = {.name = _template, .mode = _mode }, \
365 .show = _show, \
366 .store = _store, \
369 #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
370 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
371 .u.index = _index, \
372 .s2 = false }
374 #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
375 _nr, _index) \
376 { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
377 .u.s.index = _index, \
378 .u.s.nr = _nr, \
379 .s2 = true }
381 #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
382 static struct sensor_device_template sensor_dev_template_##_name \
383 = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
384 _index)
386 #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
387 _nr, _index) \
388 static struct sensor_device_template sensor_dev_template_##_name \
389 = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
390 _nr, _index)
392 struct sensor_template_group {
393 struct sensor_device_template **templates;
394 umode_t (*is_visible)(struct kobject *, struct attribute *, int);
395 int base;
398 static struct attribute_group *
399 nct6683_create_attr_group(struct device *dev,
400 const struct sensor_template_group *tg,
401 int repeat)
403 struct sensor_device_attribute_2 *a2;
404 struct sensor_device_attribute *a;
405 struct sensor_device_template **t;
406 struct sensor_device_attr_u *su;
407 struct attribute_group *group;
408 struct attribute **attrs;
409 int i, j, count;
411 if (repeat <= 0)
412 return ERR_PTR(-EINVAL);
414 t = tg->templates;
415 for (count = 0; *t; t++, count++)
418 if (count == 0)
419 return ERR_PTR(-EINVAL);
421 group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
422 if (group == NULL)
423 return ERR_PTR(-ENOMEM);
425 attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
426 GFP_KERNEL);
427 if (attrs == NULL)
428 return ERR_PTR(-ENOMEM);
430 su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
431 GFP_KERNEL);
432 if (su == NULL)
433 return ERR_PTR(-ENOMEM);
435 group->attrs = attrs;
436 group->is_visible = tg->is_visible;
438 for (i = 0; i < repeat; i++) {
439 t = tg->templates;
440 for (j = 0; *t != NULL; j++) {
441 snprintf(su->name, sizeof(su->name),
442 (*t)->dev_attr.attr.name, tg->base + i);
443 if ((*t)->s2) {
444 a2 = &su->u.a2;
445 sysfs_attr_init(&a2->dev_attr.attr);
446 a2->dev_attr.attr.name = su->name;
447 a2->nr = (*t)->u.s.nr + i;
448 a2->index = (*t)->u.s.index;
449 a2->dev_attr.attr.mode =
450 (*t)->dev_attr.attr.mode;
451 a2->dev_attr.show = (*t)->dev_attr.show;
452 a2->dev_attr.store = (*t)->dev_attr.store;
453 *attrs = &a2->dev_attr.attr;
454 } else {
455 a = &su->u.a1;
456 sysfs_attr_init(&a->dev_attr.attr);
457 a->dev_attr.attr.name = su->name;
458 a->index = (*t)->u.index + i;
459 a->dev_attr.attr.mode =
460 (*t)->dev_attr.attr.mode;
461 a->dev_attr.show = (*t)->dev_attr.show;
462 a->dev_attr.store = (*t)->dev_attr.store;
463 *attrs = &a->dev_attr.attr;
465 attrs++;
466 su++;
467 t++;
471 return group;
474 /* LSB is 16 mV, except for the following sources, where it is 32 mV */
475 #define MON_SRC_VCC 0x60
476 #define MON_SRC_VSB 0x61
477 #define MON_SRC_AVSB 0x62
478 #define MON_SRC_VBAT 0x64
480 static inline long in_from_reg(u16 reg, u8 src)
482 int scale = 16;
484 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
485 src == MON_SRC_VBAT)
486 scale <<= 1;
487 return reg * scale;
490 static inline u16 in_to_reg(u32 val, u8 src)
492 int scale = 16;
494 if (src == MON_SRC_VCC || src == MON_SRC_VSB || src == MON_SRC_AVSB ||
495 src == MON_SRC_VBAT)
496 scale <<= 1;
498 return clamp_val(DIV_ROUND_CLOSEST(val, scale), 0, 127);
501 static u16 nct6683_read(struct nct6683_data *data, u16 reg)
503 int res;
505 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
506 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
507 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
508 res = inb_p(data->addr + EC_DATA_REG);
509 return res;
512 static u16 nct6683_read16(struct nct6683_data *data, u16 reg)
514 return (nct6683_read(data, reg) << 8) | nct6683_read(data, reg + 1);
517 static void nct6683_write(struct nct6683_data *data, u16 reg, u16 value)
519 outb_p(0xff, data->addr + EC_PAGE_REG); /* unlock */
520 outb_p(reg >> 8, data->addr + EC_PAGE_REG);
521 outb_p(reg & 0xff, data->addr + EC_INDEX_REG);
522 outb_p(value & 0xff, data->addr + EC_DATA_REG);
525 static int get_in_reg(struct nct6683_data *data, int nr, int index)
527 int ch = data->in_index[index];
528 int reg = -EINVAL;
530 switch (nr) {
531 case 0:
532 reg = NCT6683_REG_MON(ch);
533 break;
534 case 1:
535 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
536 reg = NCT6683_REG_MON_LOW(ch);
537 break;
538 case 2:
539 if (data->customer_id != NCT6683_CUSTOMER_ID_INTEL)
540 reg = NCT6683_REG_MON_HIGH(ch);
541 break;
542 default:
543 break;
545 return reg;
548 static int get_temp_reg(struct nct6683_data *data, int nr, int index)
550 int ch = data->temp_index[index];
551 int reg = -EINVAL;
553 switch (data->customer_id) {
554 case NCT6683_CUSTOMER_ID_INTEL:
555 switch (nr) {
556 default:
557 case 1: /* max */
558 reg = NCT6683_REG_INTEL_TEMP_MAX(ch);
559 break;
560 case 3: /* crit */
561 reg = NCT6683_REG_INTEL_TEMP_CRIT(ch);
562 break;
564 break;
565 case NCT6683_CUSTOMER_ID_MITAC:
566 default:
567 switch (nr) {
568 default:
569 case 0: /* min */
570 reg = NCT6683_REG_MON_LOW(ch);
571 break;
572 case 1: /* max */
573 reg = NCT6683_REG_TEMP_MAX(ch);
574 break;
575 case 2: /* hyst */
576 reg = NCT6683_REG_TEMP_HYST(ch);
577 break;
578 case 3: /* crit */
579 reg = NCT6683_REG_MON_HIGH(ch);
580 break;
582 break;
584 return reg;
587 static void nct6683_update_pwm(struct device *dev)
589 struct nct6683_data *data = dev_get_drvdata(dev);
590 int i;
592 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
593 if (!(data->have_pwm & (1 << i)))
594 continue;
595 data->pwm[i] = nct6683_read(data, NCT6683_REG_PWM(i));
599 static struct nct6683_data *nct6683_update_device(struct device *dev)
601 struct nct6683_data *data = dev_get_drvdata(dev);
602 int i, j;
604 mutex_lock(&data->update_lock);
606 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
607 /* Measured voltages and limits */
608 for (i = 0; i < data->in_num; i++) {
609 for (j = 0; j < 3; j++) {
610 int reg = get_in_reg(data, j, i);
612 if (reg >= 0)
613 data->in[j][i] =
614 nct6683_read(data, reg);
618 /* Measured temperatures and limits */
619 for (i = 0; i < data->temp_num; i++) {
620 u8 ch = data->temp_index[i];
622 data->temp_in[i] = nct6683_read16(data,
623 NCT6683_REG_MON(ch));
624 for (j = 0; j < 4; j++) {
625 int reg = get_temp_reg(data, j, i);
627 if (reg >= 0)
628 data->temp[j][i] =
629 nct6683_read(data, reg);
633 /* Measured fan speeds and limits */
634 for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
635 if (!(data->have_fan & (1 << i)))
636 continue;
638 data->rpm[i] = nct6683_read16(data,
639 NCT6683_REG_FAN_RPM(i));
640 data->fan_min[i] = nct6683_read16(data,
641 NCT6683_REG_FAN_MIN(i));
644 nct6683_update_pwm(dev);
646 data->last_updated = jiffies;
647 data->valid = true;
650 mutex_unlock(&data->update_lock);
651 return data;
655 * Sysfs callback functions
657 static ssize_t
658 show_in_label(struct device *dev, struct device_attribute *attr, char *buf)
660 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
661 struct nct6683_data *data = nct6683_update_device(dev);
662 int nr = sattr->index;
664 return sprintf(buf, "%s\n", nct6683_mon_label[data->in_src[nr]]);
667 static ssize_t
668 show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
670 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
671 struct nct6683_data *data = nct6683_update_device(dev);
672 int index = sattr->index;
673 int nr = sattr->nr;
675 return sprintf(buf, "%ld\n",
676 in_from_reg(data->in[index][nr], data->in_index[index]));
679 static umode_t nct6683_in_is_visible(struct kobject *kobj,
680 struct attribute *attr, int index)
682 struct device *dev = kobj_to_dev(kobj);
683 struct nct6683_data *data = dev_get_drvdata(dev);
684 int nr = index % 4; /* attribute */
687 * Voltage limits exist for Intel boards,
688 * but register location and encoding is unknown
690 if ((nr == 2 || nr == 3) &&
691 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
692 return 0;
694 return attr->mode;
697 SENSOR_TEMPLATE(in_label, "in%d_label", S_IRUGO, show_in_label, NULL, 0);
698 SENSOR_TEMPLATE_2(in_input, "in%d_input", S_IRUGO, show_in_reg, NULL, 0, 0);
699 SENSOR_TEMPLATE_2(in_min, "in%d_min", S_IRUGO, show_in_reg, NULL, 0, 1);
700 SENSOR_TEMPLATE_2(in_max, "in%d_max", S_IRUGO, show_in_reg, NULL, 0, 2);
702 static struct sensor_device_template *nct6683_attributes_in_template[] = {
703 &sensor_dev_template_in_label,
704 &sensor_dev_template_in_input,
705 &sensor_dev_template_in_min,
706 &sensor_dev_template_in_max,
707 NULL
710 static const struct sensor_template_group nct6683_in_template_group = {
711 .templates = nct6683_attributes_in_template,
712 .is_visible = nct6683_in_is_visible,
715 static ssize_t
716 show_fan(struct device *dev, struct device_attribute *attr, char *buf)
718 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
719 struct nct6683_data *data = nct6683_update_device(dev);
721 return sprintf(buf, "%d\n", data->rpm[sattr->index]);
724 static ssize_t
725 show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
727 struct nct6683_data *data = nct6683_update_device(dev);
728 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
729 int nr = sattr->index;
731 return sprintf(buf, "%d\n", data->fan_min[nr]);
734 static ssize_t
735 show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
737 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
738 struct nct6683_data *data = nct6683_update_device(dev);
740 return sprintf(buf, "%d\n",
741 ((data->fanin_cfg[sattr->index] >> 5) & 0x03) + 1);
744 static umode_t nct6683_fan_is_visible(struct kobject *kobj,
745 struct attribute *attr, int index)
747 struct device *dev = kobj_to_dev(kobj);
748 struct nct6683_data *data = dev_get_drvdata(dev);
749 int fan = index / 3; /* fan index */
750 int nr = index % 3; /* attribute index */
752 if (!(data->have_fan & (1 << fan)))
753 return 0;
756 * Intel may have minimum fan speed limits,
757 * but register location and encoding are unknown.
759 if (nr == 2 && data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
760 return 0;
762 return attr->mode;
765 SENSOR_TEMPLATE(fan_input, "fan%d_input", S_IRUGO, show_fan, NULL, 0);
766 SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", S_IRUGO, show_fan_pulses, NULL, 0);
767 SENSOR_TEMPLATE(fan_min, "fan%d_min", S_IRUGO, show_fan_min, NULL, 0);
770 * nct6683_fan_is_visible uses the index into the following array
771 * to determine if attributes should be created or not.
772 * Any change in order or content must be matched.
774 static struct sensor_device_template *nct6683_attributes_fan_template[] = {
775 &sensor_dev_template_fan_input,
776 &sensor_dev_template_fan_pulses,
777 &sensor_dev_template_fan_min,
778 NULL
781 static const struct sensor_template_group nct6683_fan_template_group = {
782 .templates = nct6683_attributes_fan_template,
783 .is_visible = nct6683_fan_is_visible,
784 .base = 1,
787 static ssize_t
788 show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
790 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
791 struct nct6683_data *data = nct6683_update_device(dev);
792 int nr = sattr->index;
794 return sprintf(buf, "%s\n", nct6683_mon_label[data->temp_src[nr]]);
797 static ssize_t
798 show_temp8(struct device *dev, struct device_attribute *attr, char *buf)
800 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
801 struct nct6683_data *data = nct6683_update_device(dev);
802 int index = sattr->index;
803 int nr = sattr->nr;
805 return sprintf(buf, "%d\n", data->temp[index][nr] * 1000);
808 static ssize_t
809 show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
811 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
812 struct nct6683_data *data = nct6683_update_device(dev);
813 int nr = sattr->index;
814 int temp = data->temp[1][nr] - data->temp[2][nr];
816 return sprintf(buf, "%d\n", temp * 1000);
819 static ssize_t
820 show_temp16(struct device *dev, struct device_attribute *attr, char *buf)
822 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
823 struct nct6683_data *data = nct6683_update_device(dev);
824 int index = sattr->index;
826 return sprintf(buf, "%d\n", (data->temp_in[index] / 128) * 500);
830 * Temperature sensor type is determined by temperature source
831 * and can not be modified.
832 * 0x02..0x07: Thermal diode
833 * 0x08..0x18: Thermistor
834 * 0x20..0x2b: Intel PECI
835 * 0x42..0x49: AMD TSI
836 * Others are unspecified (not visible)
839 static int get_temp_type(u8 src)
841 if (src >= 0x02 && src <= 0x07)
842 return 3; /* thermal diode */
843 else if (src >= 0x08 && src <= 0x18)
844 return 4; /* thermistor */
845 else if (src >= 0x20 && src <= 0x2b)
846 return 6; /* PECI */
847 else if (src >= 0x42 && src <= 0x49)
848 return 5;
850 return 0;
853 static ssize_t
854 show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
856 struct nct6683_data *data = nct6683_update_device(dev);
857 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
858 int nr = sattr->index;
859 return sprintf(buf, "%d\n", get_temp_type(data->temp_src[nr]));
862 static umode_t nct6683_temp_is_visible(struct kobject *kobj,
863 struct attribute *attr, int index)
865 struct device *dev = kobj_to_dev(kobj);
866 struct nct6683_data *data = dev_get_drvdata(dev);
867 int temp = index / 7; /* temp index */
868 int nr = index % 7; /* attribute index */
871 * Intel does not have low temperature limits or temperature hysteresis
872 * registers, or at least register location and encoding is unknown.
874 if ((nr == 2 || nr == 4) &&
875 data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
876 return 0;
878 if (nr == 6 && get_temp_type(data->temp_src[temp]) == 0)
879 return 0; /* type */
881 return attr->mode;
884 SENSOR_TEMPLATE(temp_input, "temp%d_input", S_IRUGO, show_temp16, NULL, 0);
885 SENSOR_TEMPLATE(temp_label, "temp%d_label", S_IRUGO, show_temp_label, NULL, 0);
886 SENSOR_TEMPLATE_2(temp_min, "temp%d_min", S_IRUGO, show_temp8, NULL, 0, 0);
887 SENSOR_TEMPLATE_2(temp_max, "temp%d_max", S_IRUGO, show_temp8, NULL, 0, 1);
888 SENSOR_TEMPLATE(temp_max_hyst, "temp%d_max_hyst", S_IRUGO, show_temp_hyst, NULL,
890 SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", S_IRUGO, show_temp8, NULL, 0, 3);
891 SENSOR_TEMPLATE(temp_type, "temp%d_type", S_IRUGO, show_temp_type, NULL, 0);
894 * nct6683_temp_is_visible uses the index into the following array
895 * to determine if attributes should be created or not.
896 * Any change in order or content must be matched.
898 static struct sensor_device_template *nct6683_attributes_temp_template[] = {
899 &sensor_dev_template_temp_input,
900 &sensor_dev_template_temp_label,
901 &sensor_dev_template_temp_min, /* 2 */
902 &sensor_dev_template_temp_max, /* 3 */
903 &sensor_dev_template_temp_max_hyst, /* 4 */
904 &sensor_dev_template_temp_crit, /* 5 */
905 &sensor_dev_template_temp_type, /* 6 */
906 NULL
909 static const struct sensor_template_group nct6683_temp_template_group = {
910 .templates = nct6683_attributes_temp_template,
911 .is_visible = nct6683_temp_is_visible,
912 .base = 1,
915 static ssize_t
916 show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
918 struct nct6683_data *data = nct6683_update_device(dev);
919 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
920 int index = sattr->index;
922 return sprintf(buf, "%d\n", data->pwm[index]);
925 static ssize_t
926 store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
927 size_t count)
929 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
930 struct nct6683_data *data = dev_get_drvdata(dev);
931 int index = sattr->index;
932 unsigned long val;
934 if (kstrtoul(buf, 10, &val) || val > 255)
935 return -EINVAL;
937 mutex_lock(&data->update_lock);
938 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_REQ);
939 usleep_range(1000, 2000);
940 nct6683_write(data, NCT6683_REG_PWM_WRITE(index), val);
941 nct6683_write(data, NCT6683_REG_FAN_CFG_CTRL, NCT6683_FAN_CFG_DONE);
942 mutex_unlock(&data->update_lock);
944 return count;
947 SENSOR_TEMPLATE(pwm, "pwm%d", S_IRUGO, show_pwm, store_pwm, 0);
949 static umode_t nct6683_pwm_is_visible(struct kobject *kobj,
950 struct attribute *attr, int index)
952 struct device *dev = kobj_to_dev(kobj);
953 struct nct6683_data *data = dev_get_drvdata(dev);
954 int pwm = index; /* pwm index */
956 if (!(data->have_pwm & (1 << pwm)))
957 return 0;
959 /* Only update pwm values for Mitac boards */
960 if (data->customer_id == NCT6683_CUSTOMER_ID_MITAC)
961 return attr->mode | S_IWUSR;
963 return attr->mode;
966 static struct sensor_device_template *nct6683_attributes_pwm_template[] = {
967 &sensor_dev_template_pwm,
968 NULL
971 static const struct sensor_template_group nct6683_pwm_template_group = {
972 .templates = nct6683_attributes_pwm_template,
973 .is_visible = nct6683_pwm_is_visible,
974 .base = 1,
977 static ssize_t
978 beep_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
980 struct nct6683_data *data = dev_get_drvdata(dev);
981 int ret;
982 u8 reg;
984 mutex_lock(&data->update_lock);
986 ret = superio_enter(data->sioreg);
987 if (ret)
988 goto error;
989 superio_select(data->sioreg, NCT6683_LD_HWM);
990 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
991 superio_exit(data->sioreg);
993 mutex_unlock(&data->update_lock);
995 return sprintf(buf, "%u\n", !!(reg & NCT6683_CR_BEEP_MASK));
997 error:
998 mutex_unlock(&data->update_lock);
999 return ret;
1002 static ssize_t
1003 beep_enable_store(struct device *dev, struct device_attribute *attr,
1004 const char *buf, size_t count)
1006 struct nct6683_data *data = dev_get_drvdata(dev);
1007 unsigned long val;
1008 u8 reg;
1009 int ret;
1011 if (kstrtoul(buf, 10, &val) || (val != 0 && val != 1))
1012 return -EINVAL;
1014 mutex_lock(&data->update_lock);
1016 ret = superio_enter(data->sioreg);
1017 if (ret) {
1018 count = ret;
1019 goto error;
1022 superio_select(data->sioreg, NCT6683_LD_HWM);
1023 reg = superio_inb(data->sioreg, NCT6683_REG_CR_BEEP);
1024 if (val)
1025 reg |= NCT6683_CR_BEEP_MASK;
1026 else
1027 reg &= ~NCT6683_CR_BEEP_MASK;
1028 superio_outb(data->sioreg, NCT6683_REG_CR_BEEP, reg);
1029 superio_exit(data->sioreg);
1030 error:
1031 mutex_unlock(&data->update_lock);
1032 return count;
1035 /* Case open detection */
1037 static ssize_t
1038 intrusion0_alarm_show(struct device *dev, struct device_attribute *attr,
1039 char *buf)
1041 struct nct6683_data *data = dev_get_drvdata(dev);
1042 int ret;
1043 u8 reg;
1045 mutex_lock(&data->update_lock);
1047 ret = superio_enter(data->sioreg);
1048 if (ret)
1049 goto error;
1050 superio_select(data->sioreg, NCT6683_LD_ACPI);
1051 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1052 superio_exit(data->sioreg);
1054 mutex_unlock(&data->update_lock);
1056 return sprintf(buf, "%u\n", !(reg & NCT6683_CR_CASEOPEN_MASK));
1058 error:
1059 mutex_unlock(&data->update_lock);
1060 return ret;
1063 static ssize_t
1064 intrusion0_alarm_store(struct device *dev, struct device_attribute *attr,
1065 const char *buf, size_t count)
1067 struct nct6683_data *data = dev_get_drvdata(dev);
1068 unsigned long val;
1069 u8 reg;
1070 int ret;
1072 if (kstrtoul(buf, 10, &val) || val != 0)
1073 return -EINVAL;
1075 mutex_lock(&data->update_lock);
1078 * Use CR registers to clear caseopen status.
1079 * Caseopen is activ low, clear by writing 1 into the register.
1082 ret = superio_enter(data->sioreg);
1083 if (ret) {
1084 count = ret;
1085 goto error;
1088 superio_select(data->sioreg, NCT6683_LD_ACPI);
1089 reg = superio_inb(data->sioreg, NCT6683_REG_CR_CASEOPEN);
1090 reg |= NCT6683_CR_CASEOPEN_MASK;
1091 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1092 reg &= ~NCT6683_CR_CASEOPEN_MASK;
1093 superio_outb(data->sioreg, NCT6683_REG_CR_CASEOPEN, reg);
1094 superio_exit(data->sioreg);
1096 data->valid = false; /* Force cache refresh */
1097 error:
1098 mutex_unlock(&data->update_lock);
1099 return count;
1102 static DEVICE_ATTR_RW(intrusion0_alarm);
1103 static DEVICE_ATTR_RW(beep_enable);
1105 static struct attribute *nct6683_attributes_other[] = {
1106 &dev_attr_intrusion0_alarm.attr,
1107 &dev_attr_beep_enable.attr,
1108 NULL
1111 static const struct attribute_group nct6683_group_other = {
1112 .attrs = nct6683_attributes_other,
1115 /* Get the monitoring functions started */
1116 static inline void nct6683_init_device(struct nct6683_data *data)
1118 u8 tmp;
1120 /* Start hardware monitoring if needed */
1121 tmp = nct6683_read(data, NCT6683_HWM_CFG);
1122 if (!(tmp & 0x80))
1123 nct6683_write(data, NCT6683_HWM_CFG, tmp | 0x80);
1127 * There are a total of 24 fan inputs. Each can be configured as input
1128 * or as output. A maximum of 16 inputs and 8 outputs is configurable.
1130 static void
1131 nct6683_setup_fans(struct nct6683_data *data)
1133 int i;
1134 u8 reg;
1136 for (i = 0; i < NCT6683_NUM_REG_FAN; i++) {
1137 reg = nct6683_read(data, NCT6683_REG_FANIN_CFG(i));
1138 if (reg & 0x80)
1139 data->have_fan |= 1 << i;
1140 data->fanin_cfg[i] = reg;
1142 for (i = 0; i < NCT6683_NUM_REG_PWM; i++) {
1143 reg = nct6683_read(data, NCT6683_REG_FANOUT_CFG(i));
1144 if (reg & 0x80)
1145 data->have_pwm |= 1 << i;
1146 data->fanout_cfg[i] = reg;
1151 * Translation from monitoring register to temperature and voltage attributes
1152 * ==========================================================================
1154 * There are a total of 32 monitoring registers. Each can be assigned to either
1155 * a temperature or voltage monitoring source.
1156 * NCT6683_REG_MON_CFG(x) defines assignment for each monitoring source.
1158 * Temperature and voltage attribute mapping is determined by walking through
1159 * the NCT6683_REG_MON_CFG registers. If the assigned source is
1160 * a temperature, temp_index[n] is set to the monitor register index, and
1161 * temp_src[n] is set to the temperature source. If the assigned source is
1162 * a voltage, the respective values are stored in in_index[] and in_src[],
1163 * respectively.
1166 static void nct6683_setup_sensors(struct nct6683_data *data)
1168 u8 reg;
1169 int i;
1171 data->temp_num = 0;
1172 data->in_num = 0;
1173 for (i = 0; i < NCT6683_NUM_REG_MON; i++) {
1174 reg = nct6683_read(data, NCT6683_REG_MON_CFG(i)) & 0x7f;
1175 /* Ignore invalid assignments */
1176 if (reg >= NUM_MON_LABELS)
1177 continue;
1178 /* Skip if disabled or reserved */
1179 if (nct6683_mon_label[reg] == NULL)
1180 continue;
1181 if (reg < MON_VOLTAGE_START) {
1182 data->temp_index[data->temp_num] = i;
1183 data->temp_src[data->temp_num] = reg;
1184 data->temp_num++;
1185 } else {
1186 data->in_index[data->in_num] = i;
1187 data->in_src[data->in_num] = reg;
1188 data->in_num++;
1193 static int nct6683_probe(struct platform_device *pdev)
1195 struct device *dev = &pdev->dev;
1196 struct nct6683_sio_data *sio_data = dev->platform_data;
1197 struct attribute_group *group;
1198 struct nct6683_data *data;
1199 struct device *hwmon_dev;
1200 struct resource *res;
1201 int groups = 0;
1202 char build[16];
1204 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1205 if (!devm_request_region(dev, res->start, IOREGION_LENGTH, DRVNAME))
1206 return -EBUSY;
1208 data = devm_kzalloc(dev, sizeof(struct nct6683_data), GFP_KERNEL);
1209 if (!data)
1210 return -ENOMEM;
1212 data->kind = sio_data->kind;
1213 data->sioreg = sio_data->sioreg;
1214 data->addr = res->start;
1215 mutex_init(&data->update_lock);
1216 platform_set_drvdata(pdev, data);
1218 data->customer_id = nct6683_read16(data, NCT6683_REG_CUSTOMER_ID);
1220 /* By default only instantiate driver if the customer ID is known */
1221 switch (data->customer_id) {
1222 case NCT6683_CUSTOMER_ID_INTEL:
1223 break;
1224 case NCT6683_CUSTOMER_ID_MITAC:
1225 break;
1226 case NCT6683_CUSTOMER_ID_MSI:
1227 break;
1228 default:
1229 if (!force)
1230 return -ENODEV;
1233 nct6683_init_device(data);
1234 nct6683_setup_fans(data);
1235 nct6683_setup_sensors(data);
1237 /* Register sysfs hooks */
1239 if (data->have_pwm) {
1240 group = nct6683_create_attr_group(dev,
1241 &nct6683_pwm_template_group,
1242 fls(data->have_pwm));
1243 if (IS_ERR(group))
1244 return PTR_ERR(group);
1245 data->groups[groups++] = group;
1248 if (data->in_num) {
1249 group = nct6683_create_attr_group(dev,
1250 &nct6683_in_template_group,
1251 data->in_num);
1252 if (IS_ERR(group))
1253 return PTR_ERR(group);
1254 data->groups[groups++] = group;
1257 if (data->have_fan) {
1258 group = nct6683_create_attr_group(dev,
1259 &nct6683_fan_template_group,
1260 fls(data->have_fan));
1261 if (IS_ERR(group))
1262 return PTR_ERR(group);
1263 data->groups[groups++] = group;
1266 if (data->temp_num) {
1267 group = nct6683_create_attr_group(dev,
1268 &nct6683_temp_template_group,
1269 data->temp_num);
1270 if (IS_ERR(group))
1271 return PTR_ERR(group);
1272 data->groups[groups++] = group;
1274 data->groups[groups++] = &nct6683_group_other;
1276 if (data->customer_id == NCT6683_CUSTOMER_ID_INTEL)
1277 scnprintf(build, sizeof(build), "%02x/%02x/%02x",
1278 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1279 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1280 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1281 else
1282 scnprintf(build, sizeof(build), "%02d/%02d/%02d",
1283 nct6683_read(data, NCT6683_REG_BUILD_MONTH),
1284 nct6683_read(data, NCT6683_REG_BUILD_DAY),
1285 nct6683_read(data, NCT6683_REG_BUILD_YEAR));
1287 dev_info(dev, "%s EC firmware version %d.%d build %s\n",
1288 nct6683_chip_names[data->kind],
1289 nct6683_read(data, NCT6683_REG_VERSION_HI),
1290 nct6683_read(data, NCT6683_REG_VERSION_LO),
1291 build);
1293 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
1294 nct6683_device_names[data->kind], data, data->groups);
1295 return PTR_ERR_OR_ZERO(hwmon_dev);
1298 #ifdef CONFIG_PM
1299 static int nct6683_suspend(struct device *dev)
1301 struct nct6683_data *data = nct6683_update_device(dev);
1303 mutex_lock(&data->update_lock);
1304 data->hwm_cfg = nct6683_read(data, NCT6683_HWM_CFG);
1305 mutex_unlock(&data->update_lock);
1307 return 0;
1310 static int nct6683_resume(struct device *dev)
1312 struct nct6683_data *data = dev_get_drvdata(dev);
1314 mutex_lock(&data->update_lock);
1316 nct6683_write(data, NCT6683_HWM_CFG, data->hwm_cfg);
1318 /* Force re-reading all values */
1319 data->valid = false;
1320 mutex_unlock(&data->update_lock);
1322 return 0;
1325 static const struct dev_pm_ops nct6683_dev_pm_ops = {
1326 .suspend = nct6683_suspend,
1327 .resume = nct6683_resume,
1328 .freeze = nct6683_suspend,
1329 .restore = nct6683_resume,
1332 #define NCT6683_DEV_PM_OPS (&nct6683_dev_pm_ops)
1333 #else
1334 #define NCT6683_DEV_PM_OPS NULL
1335 #endif /* CONFIG_PM */
1337 static struct platform_driver nct6683_driver = {
1338 .driver = {
1339 .name = DRVNAME,
1340 .pm = NCT6683_DEV_PM_OPS,
1342 .probe = nct6683_probe,
1345 static int __init nct6683_find(int sioaddr, struct nct6683_sio_data *sio_data)
1347 int addr;
1348 u16 val;
1349 int err;
1351 err = superio_enter(sioaddr);
1352 if (err)
1353 return err;
1355 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
1356 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
1358 switch (val & SIO_ID_MASK) {
1359 case SIO_NCT6683_ID:
1360 sio_data->kind = nct6683;
1361 break;
1362 case SIO_NCT6687_ID:
1363 sio_data->kind = nct6687;
1364 break;
1365 default:
1366 if (val != 0xffff)
1367 pr_debug("unsupported chip ID: 0x%04x\n", val);
1368 goto fail;
1371 /* We have a known chip, find the HWM I/O address */
1372 superio_select(sioaddr, NCT6683_LD_HWM);
1373 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
1374 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
1375 addr = val & IOREGION_ALIGNMENT;
1376 if (addr == 0) {
1377 pr_err("EC base I/O port unconfigured\n");
1378 goto fail;
1381 /* Activate logical device if needed */
1382 val = superio_inb(sioaddr, SIO_REG_ENABLE);
1383 if (!(val & 0x01)) {
1384 pr_warn("Forcibly enabling EC access. Data may be unusable.\n");
1385 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
1388 superio_exit(sioaddr);
1389 pr_info("Found %s or compatible chip at %#x:%#x\n",
1390 nct6683_chip_names[sio_data->kind], sioaddr, addr);
1391 sio_data->sioreg = sioaddr;
1393 return addr;
1395 fail:
1396 superio_exit(sioaddr);
1397 return -ENODEV;
1401 * when Super-I/O functions move to a separate file, the Super-I/O
1402 * bus will manage the lifetime of the device and this module will only keep
1403 * track of the nct6683 driver. But since we use platform_device_alloc(), we
1404 * must keep track of the device
1406 static struct platform_device *pdev[2];
1408 static int __init sensors_nct6683_init(void)
1410 struct nct6683_sio_data sio_data;
1411 int sioaddr[2] = { 0x2e, 0x4e };
1412 struct resource res;
1413 bool found = false;
1414 int address;
1415 int i, err;
1417 err = platform_driver_register(&nct6683_driver);
1418 if (err)
1419 return err;
1422 * initialize sio_data->kind and sio_data->sioreg.
1424 * when Super-I/O functions move to a separate file, the Super-I/O
1425 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1426 * nct6683 hardware monitor, and call probe()
1428 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1429 address = nct6683_find(sioaddr[i], &sio_data);
1430 if (address <= 0)
1431 continue;
1433 found = true;
1435 pdev[i] = platform_device_alloc(DRVNAME, address);
1436 if (!pdev[i]) {
1437 err = -ENOMEM;
1438 goto exit_device_unregister;
1441 err = platform_device_add_data(pdev[i], &sio_data,
1442 sizeof(struct nct6683_sio_data));
1443 if (err)
1444 goto exit_device_put;
1446 memset(&res, 0, sizeof(res));
1447 res.name = DRVNAME;
1448 res.start = address + IOREGION_OFFSET;
1449 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1450 res.flags = IORESOURCE_IO;
1452 err = acpi_check_resource_conflict(&res);
1453 if (err) {
1454 platform_device_put(pdev[i]);
1455 pdev[i] = NULL;
1456 continue;
1459 err = platform_device_add_resources(pdev[i], &res, 1);
1460 if (err)
1461 goto exit_device_put;
1463 /* platform_device_add calls probe() */
1464 err = platform_device_add(pdev[i]);
1465 if (err)
1466 goto exit_device_put;
1468 if (!found) {
1469 err = -ENODEV;
1470 goto exit_unregister;
1473 return 0;
1475 exit_device_put:
1476 platform_device_put(pdev[i]);
1477 exit_device_unregister:
1478 while (--i >= 0) {
1479 if (pdev[i])
1480 platform_device_unregister(pdev[i]);
1482 exit_unregister:
1483 platform_driver_unregister(&nct6683_driver);
1484 return err;
1487 static void __exit sensors_nct6683_exit(void)
1489 int i;
1491 for (i = 0; i < ARRAY_SIZE(pdev); i++) {
1492 if (pdev[i])
1493 platform_device_unregister(pdev[i]);
1495 platform_driver_unregister(&nct6683_driver);
1498 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1499 MODULE_DESCRIPTION("NCT6683D driver");
1500 MODULE_LICENSE("GPL");
1502 module_init(sensors_nct6683_init);
1503 module_exit(sensors_nct6683_exit);