blob: 435691febe9b578f7bdcd04d0b8ef2a3389b9459 [file] [log] [blame]
Guenter Roeck9de2e2e2012-05-20 19:29:48 -07001/*
2 * nct6775 - Driver for the hardware monitoring functionality of
3 * Nuvoton NCT677x Super-I/O chips
4 *
5 * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
6 *
7 * Derived from w83627ehf driver
8 * Copyright (C) 2005-2012 Jean Delvare <khali@linux-fr.org>
9 * Copyright (C) 2006 Yuan Mu (Winbond),
10 * Rudolf Marek <r.marek@assembler.cz>
11 * David Hubbard <david.c.hubbard@gmail.com>
12 * Daniel J Blueman <daniel.blueman@gmail.com>
13 * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
14 *
15 * Shamelessly ripped from the w83627hf driver
16 * Copyright (C) 2003 Mark Studebaker
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 *
32 *
33 * Supports the following chips:
34 *
35 * Chip #vin #fan #pwm #temp chip IDs man ID
36 * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
37 * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
38 * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
39 *
40 * #temp lists the number of monitored temperature sources (first value) plus
41 * the number of directly connectable temperature sensors (second value).
42 */
43
44#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
46#include <linux/module.h>
47#include <linux/init.h>
48#include <linux/slab.h>
49#include <linux/jiffies.h>
50#include <linux/platform_device.h>
51#include <linux/hwmon.h>
52#include <linux/hwmon-sysfs.h>
53#include <linux/hwmon-vid.h>
54#include <linux/err.h>
55#include <linux/mutex.h>
56#include <linux/acpi.h>
57#include <linux/io.h>
58#include "lm75.h"
59
60enum kinds { nct6775, nct6776, nct6779 };
61
62/* used to set data->name = nct6775_device_names[data->sio_kind] */
63static const char * const nct6775_device_names[] = {
64 "nct6775",
65 "nct6776",
66 "nct6779",
67};
68
69static unsigned short force_id;
70module_param(force_id, ushort, 0);
71MODULE_PARM_DESC(force_id, "Override the detected device ID");
72
73#define DRVNAME "nct6775"
74
75/*
76 * Super-I/O constants and functions
77 */
78
Guenter Roecka6bd5872012-12-04 03:13:34 -080079#define NCT6775_LD_ACPI 0x0a
Guenter Roeck9de2e2e2012-05-20 19:29:48 -070080#define NCT6775_LD_HWM 0x0b
81#define NCT6775_LD_VID 0x0d
82
83#define SIO_REG_LDSEL 0x07 /* Logical device select */
84#define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
85#define SIO_REG_ENABLE 0x30 /* Logical device enable */
86#define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
87
88#define SIO_NCT6775_ID 0xb470
89#define SIO_NCT6776_ID 0xc330
90#define SIO_NCT6779_ID 0xc560
91#define SIO_ID_MASK 0xFFF0
92
93static inline void
94superio_outb(int ioreg, int reg, int val)
95{
96 outb(reg, ioreg);
97 outb(val, ioreg + 1);
98}
99
100static inline int
101superio_inb(int ioreg, int reg)
102{
103 outb(reg, ioreg);
104 return inb(ioreg + 1);
105}
106
107static inline void
108superio_select(int ioreg, int ld)
109{
110 outb(SIO_REG_LDSEL, ioreg);
111 outb(ld, ioreg + 1);
112}
113
114static inline int
115superio_enter(int ioreg)
116{
117 /*
118 * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
119 */
120 if (!request_muxed_region(ioreg, 2, DRVNAME))
121 return -EBUSY;
122
123 outb(0x87, ioreg);
124 outb(0x87, ioreg);
125
126 return 0;
127}
128
129static inline void
130superio_exit(int ioreg)
131{
132 outb(0xaa, ioreg);
133 outb(0x02, ioreg);
134 outb(0x02, ioreg + 1);
135 release_region(ioreg, 2);
136}
137
138/*
139 * ISA constants
140 */
141
142#define IOREGION_ALIGNMENT (~7)
143#define IOREGION_OFFSET 5
144#define IOREGION_LENGTH 2
145#define ADDR_REG_OFFSET 0
146#define DATA_REG_OFFSET 1
147
148#define NCT6775_REG_BANK 0x4E
149#define NCT6775_REG_CONFIG 0x40
150
151/*
152 * Not currently used:
153 * REG_MAN_ID has the value 0x5ca3 for all supported chips.
154 * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
155 * REG_MAN_ID is at port 0x4f
156 * REG_CHIP_ID is at port 0x58
157 */
158
159#define NUM_REG_ALARM 4 /* Max number of alarm registers */
160
161/* Common and NCT6775 specific data */
162
163/* Voltage min/max registers for nr=7..14 are in bank 5 */
164
165static const u16 NCT6775_REG_IN_MAX[] = {
166 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
167 0x55c, 0x55e, 0x560, 0x562 };
168static const u16 NCT6775_REG_IN_MIN[] = {
169 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
170 0x55d, 0x55f, 0x561, 0x563 };
171static const u16 NCT6775_REG_IN[] = {
172 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
173};
174
175#define NCT6775_REG_VBAT 0x5D
176
177static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
178
179/* 0..15 voltages, 16..23 fans, 24..31 temperatures */
180
181static const s8 NCT6775_ALARM_BITS[] = {
182 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
183 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
184 -1, /* unused */
185 6, 7, 11, 10, 23, /* fan1..fan5 */
186 -1, -1, -1, /* unused */
187 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
188 12, -1 }; /* intrusion0, intrusion1 */
189
Guenter Roecka6bd5872012-12-04 03:13:34 -0800190#define INTRUSION_ALARM_BASE 30
191
192static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6, 0xee };
193static const u8 NCT6775_CR_CASEOPEN_CLR_MASK[] = { 0x20, 0x01 };
194
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700195/* NCT6776 specific data */
196
197static const s8 NCT6776_ALARM_BITS[] = {
198 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
199 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
200 -1, /* unused */
201 6, 7, 11, 10, 23, /* fan1..fan5 */
202 -1, -1, -1, /* unused */
203 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
204 12, 9 }; /* intrusion0, intrusion1 */
205
206/* NCT6779 specific data */
207
208static const u16 NCT6779_REG_IN[] = {
209 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
210 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
211
212static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
213 0x459, 0x45A, 0x45B, 0x568 };
214
215static const s8 NCT6779_ALARM_BITS[] = {
216 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
217 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
218 -1, /* unused */
219 6, 7, 11, 10, 23, /* fan1..fan5 */
220 -1, -1, -1, /* unused */
221 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
222 12, 9 }; /* intrusion0, intrusion1 */
223
224/*
225 * Conversions
226 */
227
228/*
229 * Some of the voltage inputs have internal scaling, the tables below
230 * contain 8 (the ADC LSB in mV) * scaling factor * 100
231 */
232static const u16 scale_in[15] = {
233 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
234 800, 800
235};
236
237static inline long in_from_reg(u8 reg, u8 nr)
238{
239 return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
240}
241
242static inline u8 in_to_reg(u32 val, u8 nr)
243{
244 return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
245}
246
247/*
248 * Data structures and manipulation thereof
249 */
250
251struct nct6775_data {
252 int addr; /* IO base of hw monitor block */
253 enum kinds kind;
254 const char *name;
255
256 struct device *hwmon_dev;
257 struct mutex lock;
258
259 u16 REG_CONFIG;
260 u16 REG_VBAT;
261
262 const s8 *ALARM_BITS;
263
264 const u16 *REG_VIN;
265 const u16 *REG_IN_MINMAX[2];
266
267 const u16 *REG_ALARM;
268
269 struct mutex update_lock;
270 bool valid; /* true if following fields are valid */
271 unsigned long last_updated; /* In jiffies */
272
273 /* Register values */
274 u8 bank; /* current register bank */
275 u8 in_num; /* number of in inputs we have */
276 u8 in[15][3]; /* [0]=in, [1]=in_max, [2]=in_min */
277
278 u64 alarms;
279
280 u8 vid;
281 u8 vrm;
282
283 u16 have_in;
284};
285
286struct nct6775_sio_data {
287 int sioreg;
288 enum kinds kind;
289};
290
291static bool is_word_sized(struct nct6775_data *data, u16 reg)
292{
293 switch (data->kind) {
294 case nct6775:
295 return (((reg & 0xff00) == 0x100 ||
296 (reg & 0xff00) == 0x200) &&
297 ((reg & 0x00ff) == 0x50 ||
298 (reg & 0x00ff) == 0x53 ||
299 (reg & 0x00ff) == 0x55)) ||
300 (reg & 0xfff0) == 0x630 ||
301 reg == 0x640 || reg == 0x642 ||
302 reg == 0x662 ||
303 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
304 reg == 0x73 || reg == 0x75 || reg == 0x77;
305 case nct6776:
306 return (((reg & 0xff00) == 0x100 ||
307 (reg & 0xff00) == 0x200) &&
308 ((reg & 0x00ff) == 0x50 ||
309 (reg & 0x00ff) == 0x53 ||
310 (reg & 0x00ff) == 0x55)) ||
311 (reg & 0xfff0) == 0x630 ||
312 reg == 0x402 ||
313 reg == 0x640 || reg == 0x642 ||
314 ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
315 reg == 0x73 || reg == 0x75 || reg == 0x77;
316 case nct6779:
317 return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
318 ((reg & 0xfff0) == 0x4b0 && (reg & 0x000f) < 0x09) ||
319 reg == 0x402 ||
320 reg == 0x63a || reg == 0x63c || reg == 0x63e ||
321 reg == 0x640 || reg == 0x642 ||
322 reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
323 reg == 0x7b;
324 }
325 return false;
326}
327
328/*
329 * On older chips, only registers 0x50-0x5f are banked.
330 * On more recent chips, all registers are banked.
331 * Assume that is the case and set the bank number for each access.
332 * Cache the bank number so it only needs to be set if it changes.
333 */
334static inline void nct6775_set_bank(struct nct6775_data *data, u16 reg)
335{
336 u8 bank = reg >> 8;
337 if (data->bank != bank) {
338 outb_p(NCT6775_REG_BANK, data->addr + ADDR_REG_OFFSET);
339 outb_p(bank, data->addr + DATA_REG_OFFSET);
340 data->bank = bank;
341 }
342}
343
344static u16 nct6775_read_value(struct nct6775_data *data, u16 reg)
345{
346 int res, word_sized = is_word_sized(data, reg);
347
348 mutex_lock(&data->lock);
349
350 nct6775_set_bank(data, reg);
351 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
352 res = inb_p(data->addr + DATA_REG_OFFSET);
353 if (word_sized) {
354 outb_p((reg & 0xff) + 1,
355 data->addr + ADDR_REG_OFFSET);
356 res = (res << 8) + inb_p(data->addr + DATA_REG_OFFSET);
357 }
358
359 mutex_unlock(&data->lock);
360 return res;
361}
362
363static int nct6775_write_value(struct nct6775_data *data, u16 reg, u16 value)
364{
365 int word_sized = is_word_sized(data, reg);
366
367 mutex_lock(&data->lock);
368
369 nct6775_set_bank(data, reg);
370 outb_p(reg & 0xff, data->addr + ADDR_REG_OFFSET);
371 if (word_sized) {
372 outb_p(value >> 8, data->addr + DATA_REG_OFFSET);
373 outb_p((reg & 0xff) + 1,
374 data->addr + ADDR_REG_OFFSET);
375 }
376 outb_p(value & 0xff, data->addr + DATA_REG_OFFSET);
377
378 mutex_unlock(&data->lock);
379 return 0;
380}
381
382static struct nct6775_data *nct6775_update_device(struct device *dev)
383{
384 struct nct6775_data *data = dev_get_drvdata(dev);
385 int i;
386
387 mutex_lock(&data->update_lock);
388
389 if (time_after(jiffies, data->last_updated + HZ + HZ/2)
390 || !data->valid) {
391 /* Measured voltages and limits */
392 for (i = 0; i < data->in_num; i++) {
393 if (!(data->have_in & (1 << i)))
394 continue;
395
396 data->in[i][0] = nct6775_read_value(data,
397 data->REG_VIN[i]);
398 data->in[i][1] = nct6775_read_value(data,
399 data->REG_IN_MINMAX[0][i]);
400 data->in[i][2] = nct6775_read_value(data,
401 data->REG_IN_MINMAX[1][i]);
402 }
403
404 data->alarms = 0;
405 for (i = 0; i < NUM_REG_ALARM; i++) {
406 u8 alarm;
407 if (!data->REG_ALARM[i])
408 continue;
409 alarm = nct6775_read_value(data, data->REG_ALARM[i]);
410 data->alarms |= ((u64)alarm) << (i << 3);
411 }
412
413 data->last_updated = jiffies;
414 data->valid = true;
415 }
416
417 mutex_unlock(&data->update_lock);
418 return data;
419}
420
421/*
422 * Sysfs callback functions
423 */
424static ssize_t
425show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
426{
427 struct nct6775_data *data = nct6775_update_device(dev);
428 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
429 int nr = sattr->nr;
430 int index = sattr->index;
431 return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
432}
433
434static ssize_t
435store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
436 size_t count)
437{
438 struct nct6775_data *data = dev_get_drvdata(dev);
439 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
440 int nr = sattr->nr;
441 int index = sattr->index;
442 unsigned long val;
443 int err = kstrtoul(buf, 10, &val);
444 if (err < 0)
445 return err;
446 mutex_lock(&data->update_lock);
447 data->in[nr][index] = in_to_reg(val, nr);
448 nct6775_write_value(data, data->REG_IN_MINMAX[index-1][nr],
449 data->in[nr][index]);
450 mutex_unlock(&data->update_lock);
451 return count;
452}
453
454static ssize_t
455show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
456{
457 struct nct6775_data *data = nct6775_update_device(dev);
458 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
459 int nr = data->ALARM_BITS[sattr->index];
460 return sprintf(buf, "%u\n",
461 (unsigned int)((data->alarms >> nr) & 0x01));
462}
463
464static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in_reg, NULL, 0, 0);
465static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in_reg, NULL, 1, 0);
466static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in_reg, NULL, 2, 0);
467static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in_reg, NULL, 3, 0);
468static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in_reg, NULL, 4, 0);
469static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in_reg, NULL, 5, 0);
470static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in_reg, NULL, 6, 0);
471static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in_reg, NULL, 7, 0);
472static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in_reg, NULL, 8, 0);
473static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in_reg, NULL, 9, 0);
474static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in_reg, NULL, 10, 0);
475static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in_reg, NULL, 11, 0);
476static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in_reg, NULL, 12, 0);
477static SENSOR_DEVICE_ATTR_2(in13_input, S_IRUGO, show_in_reg, NULL, 13, 0);
478static SENSOR_DEVICE_ATTR_2(in14_input, S_IRUGO, show_in_reg, NULL, 14, 0);
479
480static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
481static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
482static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
483static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
484static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
485static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
486static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
487static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
488static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
489static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
490static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
491static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 11);
492static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 12);
493static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 13);
494static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 14);
495
496static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, show_in_reg,
497 store_in_reg, 0, 1);
498static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, show_in_reg,
499 store_in_reg, 1, 1);
500static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, show_in_reg,
501 store_in_reg, 2, 1);
502static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, show_in_reg,
503 store_in_reg, 3, 1);
504static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, show_in_reg,
505 store_in_reg, 4, 1);
506static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, show_in_reg,
507 store_in_reg, 5, 1);
508static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, show_in_reg,
509 store_in_reg, 6, 1);
510static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, show_in_reg,
511 store_in_reg, 7, 1);
512static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, show_in_reg,
513 store_in_reg, 8, 1);
514static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, show_in_reg,
515 store_in_reg, 9, 1);
516static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, show_in_reg,
517 store_in_reg, 10, 1);
518static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, show_in_reg,
519 store_in_reg, 11, 1);
520static SENSOR_DEVICE_ATTR_2(in12_min, S_IWUSR | S_IRUGO, show_in_reg,
521 store_in_reg, 12, 1);
522static SENSOR_DEVICE_ATTR_2(in13_min, S_IWUSR | S_IRUGO, show_in_reg,
523 store_in_reg, 13, 1);
524static SENSOR_DEVICE_ATTR_2(in14_min, S_IWUSR | S_IRUGO, show_in_reg,
525 store_in_reg, 14, 1);
526
527static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, show_in_reg,
528 store_in_reg, 0, 2);
529static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, show_in_reg,
530 store_in_reg, 1, 2);
531static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, show_in_reg,
532 store_in_reg, 2, 2);
533static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, show_in_reg,
534 store_in_reg, 3, 2);
535static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, show_in_reg,
536 store_in_reg, 4, 2);
537static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, show_in_reg,
538 store_in_reg, 5, 2);
539static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, show_in_reg,
540 store_in_reg, 6, 2);
541static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, show_in_reg,
542 store_in_reg, 7, 2);
543static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, show_in_reg,
544 store_in_reg, 8, 2);
545static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, show_in_reg,
546 store_in_reg, 9, 2);
547static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, show_in_reg,
548 store_in_reg, 10, 2);
549static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, show_in_reg,
550 store_in_reg, 11, 2);
551static SENSOR_DEVICE_ATTR_2(in12_max, S_IWUSR | S_IRUGO, show_in_reg,
552 store_in_reg, 12, 2);
553static SENSOR_DEVICE_ATTR_2(in13_max, S_IWUSR | S_IRUGO, show_in_reg,
554 store_in_reg, 13, 2);
555static SENSOR_DEVICE_ATTR_2(in14_max, S_IWUSR | S_IRUGO, show_in_reg,
556 store_in_reg, 14, 2);
557
558static struct attribute *nct6775_attributes_in[15][5] = {
559 {
560 &sensor_dev_attr_in0_input.dev_attr.attr,
561 &sensor_dev_attr_in0_min.dev_attr.attr,
562 &sensor_dev_attr_in0_max.dev_attr.attr,
563 &sensor_dev_attr_in0_alarm.dev_attr.attr,
564 NULL
565 },
566 {
567 &sensor_dev_attr_in1_input.dev_attr.attr,
568 &sensor_dev_attr_in1_min.dev_attr.attr,
569 &sensor_dev_attr_in1_max.dev_attr.attr,
570 &sensor_dev_attr_in1_alarm.dev_attr.attr,
571 NULL
572 },
573 {
574 &sensor_dev_attr_in2_input.dev_attr.attr,
575 &sensor_dev_attr_in2_min.dev_attr.attr,
576 &sensor_dev_attr_in2_max.dev_attr.attr,
577 &sensor_dev_attr_in2_alarm.dev_attr.attr,
578 NULL
579 },
580 {
581 &sensor_dev_attr_in3_input.dev_attr.attr,
582 &sensor_dev_attr_in3_min.dev_attr.attr,
583 &sensor_dev_attr_in3_max.dev_attr.attr,
584 &sensor_dev_attr_in3_alarm.dev_attr.attr,
585 NULL
586 },
587 {
588 &sensor_dev_attr_in4_input.dev_attr.attr,
589 &sensor_dev_attr_in4_min.dev_attr.attr,
590 &sensor_dev_attr_in4_max.dev_attr.attr,
591 &sensor_dev_attr_in4_alarm.dev_attr.attr,
592 NULL
593 },
594 {
595 &sensor_dev_attr_in5_input.dev_attr.attr,
596 &sensor_dev_attr_in5_min.dev_attr.attr,
597 &sensor_dev_attr_in5_max.dev_attr.attr,
598 &sensor_dev_attr_in5_alarm.dev_attr.attr,
599 NULL
600 },
601 {
602 &sensor_dev_attr_in6_input.dev_attr.attr,
603 &sensor_dev_attr_in6_min.dev_attr.attr,
604 &sensor_dev_attr_in6_max.dev_attr.attr,
605 &sensor_dev_attr_in6_alarm.dev_attr.attr,
606 NULL
607 },
608 {
609 &sensor_dev_attr_in7_input.dev_attr.attr,
610 &sensor_dev_attr_in7_min.dev_attr.attr,
611 &sensor_dev_attr_in7_max.dev_attr.attr,
612 &sensor_dev_attr_in7_alarm.dev_attr.attr,
613 NULL
614 },
615 {
616 &sensor_dev_attr_in8_input.dev_attr.attr,
617 &sensor_dev_attr_in8_min.dev_attr.attr,
618 &sensor_dev_attr_in8_max.dev_attr.attr,
619 &sensor_dev_attr_in8_alarm.dev_attr.attr,
620 NULL
621 },
622 {
623 &sensor_dev_attr_in9_input.dev_attr.attr,
624 &sensor_dev_attr_in9_min.dev_attr.attr,
625 &sensor_dev_attr_in9_max.dev_attr.attr,
626 &sensor_dev_attr_in9_alarm.dev_attr.attr,
627 NULL
628 },
629 {
630 &sensor_dev_attr_in10_input.dev_attr.attr,
631 &sensor_dev_attr_in10_min.dev_attr.attr,
632 &sensor_dev_attr_in10_max.dev_attr.attr,
633 &sensor_dev_attr_in10_alarm.dev_attr.attr,
634 NULL
635 },
636 {
637 &sensor_dev_attr_in11_input.dev_attr.attr,
638 &sensor_dev_attr_in11_min.dev_attr.attr,
639 &sensor_dev_attr_in11_max.dev_attr.attr,
640 &sensor_dev_attr_in11_alarm.dev_attr.attr,
641 NULL
642 },
643 {
644 &sensor_dev_attr_in12_input.dev_attr.attr,
645 &sensor_dev_attr_in12_min.dev_attr.attr,
646 &sensor_dev_attr_in12_max.dev_attr.attr,
647 &sensor_dev_attr_in12_alarm.dev_attr.attr,
648 NULL
649 },
650 {
651 &sensor_dev_attr_in13_input.dev_attr.attr,
652 &sensor_dev_attr_in13_min.dev_attr.attr,
653 &sensor_dev_attr_in13_max.dev_attr.attr,
654 &sensor_dev_attr_in13_alarm.dev_attr.attr,
655 NULL
656 },
657 {
658 &sensor_dev_attr_in14_input.dev_attr.attr,
659 &sensor_dev_attr_in14_min.dev_attr.attr,
660 &sensor_dev_attr_in14_max.dev_attr.attr,
661 &sensor_dev_attr_in14_alarm.dev_attr.attr,
662 NULL
663 },
664};
665
666static const struct attribute_group nct6775_group_in[15] = {
667 { .attrs = nct6775_attributes_in[0] },
668 { .attrs = nct6775_attributes_in[1] },
669 { .attrs = nct6775_attributes_in[2] },
670 { .attrs = nct6775_attributes_in[3] },
671 { .attrs = nct6775_attributes_in[4] },
672 { .attrs = nct6775_attributes_in[5] },
673 { .attrs = nct6775_attributes_in[6] },
674 { .attrs = nct6775_attributes_in[7] },
675 { .attrs = nct6775_attributes_in[8] },
676 { .attrs = nct6775_attributes_in[9] },
677 { .attrs = nct6775_attributes_in[10] },
678 { .attrs = nct6775_attributes_in[11] },
679 { .attrs = nct6775_attributes_in[12] },
680 { .attrs = nct6775_attributes_in[13] },
681 { .attrs = nct6775_attributes_in[14] },
682};
683
684static ssize_t
685show_name(struct device *dev, struct device_attribute *attr, char *buf)
686{
687 struct nct6775_data *data = dev_get_drvdata(dev);
688
689 return sprintf(buf, "%s\n", data->name);
690}
691
692static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
693
694static ssize_t
695show_vid(struct device *dev, struct device_attribute *attr, char *buf)
696{
697 struct nct6775_data *data = dev_get_drvdata(dev);
698 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
699}
700
701static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
702
Guenter Roecka6bd5872012-12-04 03:13:34 -0800703/* Case open detection */
704
705static ssize_t
706clear_caseopen(struct device *dev, struct device_attribute *attr,
707 const char *buf, size_t count)
708{
709 struct nct6775_data *data = dev_get_drvdata(dev);
710 struct nct6775_sio_data *sio_data = dev->platform_data;
711 int nr = to_sensor_dev_attr(attr)->index - INTRUSION_ALARM_BASE;
712 unsigned long val;
713 u8 reg;
714 int ret;
715
716 if (kstrtoul(buf, 10, &val) || val != 0)
717 return -EINVAL;
718
719 mutex_lock(&data->update_lock);
720
721 /*
722 * Use CR registers to clear caseopen status.
723 * The CR registers are the same for all chips, and not all chips
724 * support clearing the caseopen status through "regular" registers.
725 */
726 ret = superio_enter(sio_data->sioreg);
727 if (ret) {
728 count = ret;
729 goto error;
730 }
731
732 superio_select(sio_data->sioreg, NCT6775_LD_ACPI);
733 reg = superio_inb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr]);
734 reg |= NCT6775_CR_CASEOPEN_CLR_MASK[nr];
735 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
736 reg &= ~NCT6775_CR_CASEOPEN_CLR_MASK[nr];
737 superio_outb(sio_data->sioreg, NCT6775_REG_CR_CASEOPEN_CLR[nr], reg);
738 superio_exit(sio_data->sioreg);
739
740 data->valid = false; /* Force cache refresh */
741error:
742 mutex_unlock(&data->update_lock);
743 return count;
744}
745
746static struct sensor_device_attribute sda_caseopen[] = {
747 SENSOR_ATTR(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm,
748 clear_caseopen, INTRUSION_ALARM_BASE),
749 SENSOR_ATTR(intrusion1_alarm, S_IWUSR | S_IRUGO, show_alarm,
750 clear_caseopen, INTRUSION_ALARM_BASE + 1),
751};
752
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700753/*
754 * Driver and device management
755 */
756
757static void nct6775_device_remove_files(struct device *dev)
758{
759 /*
760 * some entries in the following arrays may not have been used in
761 * device_create_file(), but device_remove_file() will ignore them
762 */
763 int i;
764 struct nct6775_data *data = dev_get_drvdata(dev);
765
766 for (i = 0; i < data->in_num; i++)
767 sysfs_remove_group(&dev->kobj, &nct6775_group_in[i]);
768
Guenter Roecka6bd5872012-12-04 03:13:34 -0800769 device_remove_file(dev, &sda_caseopen[0].dev_attr);
770 device_remove_file(dev, &sda_caseopen[1].dev_attr);
771
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700772 device_remove_file(dev, &dev_attr_name);
773 device_remove_file(dev, &dev_attr_cpu0_vid);
774}
775
776/* Get the monitoring functions started */
777static inline void nct6775_init_device(struct nct6775_data *data)
778{
779 u8 tmp;
780
781 /* Start monitoring if needed */
782 if (data->REG_CONFIG) {
783 tmp = nct6775_read_value(data, data->REG_CONFIG);
784 if (!(tmp & 0x01))
785 nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
786 }
787
788 /* Enable VBAT monitoring if needed */
789 tmp = nct6775_read_value(data, data->REG_VBAT);
790 if (!(tmp & 0x01))
791 nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
792}
793
794static int nct6775_probe(struct platform_device *pdev)
795{
796 struct device *dev = &pdev->dev;
797 struct nct6775_sio_data *sio_data = dev->platform_data;
798 struct nct6775_data *data;
799 struct resource *res;
800 int i, err = 0;
801
802 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
803 if (!devm_request_region(&pdev->dev, res->start, IOREGION_LENGTH,
804 DRVNAME))
805 return -EBUSY;
806
807 data = devm_kzalloc(&pdev->dev, sizeof(struct nct6775_data),
808 GFP_KERNEL);
809 if (!data)
810 return -ENOMEM;
811
812 data->kind = sio_data->kind;
813 data->addr = res->start;
814 mutex_init(&data->lock);
815 mutex_init(&data->update_lock);
816 data->name = nct6775_device_names[data->kind];
817 data->bank = 0xff; /* Force initial bank selection */
818 platform_set_drvdata(pdev, data);
819
820 switch (data->kind) {
821 case nct6775:
822 data->in_num = 9;
823
824 data->ALARM_BITS = NCT6775_ALARM_BITS;
825
826 data->REG_CONFIG = NCT6775_REG_CONFIG;
827 data->REG_VBAT = NCT6775_REG_VBAT;
828 data->REG_VIN = NCT6775_REG_IN;
829 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
830 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
831 data->REG_ALARM = NCT6775_REG_ALARM;
832 break;
833 case nct6776:
834 data->in_num = 9;
835
836 data->ALARM_BITS = NCT6776_ALARM_BITS;
837
838 data->REG_CONFIG = NCT6775_REG_CONFIG;
839 data->REG_VBAT = NCT6775_REG_VBAT;
840 data->REG_VIN = NCT6775_REG_IN;
841 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
842 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
843 data->REG_ALARM = NCT6775_REG_ALARM;
844 break;
845 case nct6779:
846 data->in_num = 15;
847
848 data->ALARM_BITS = NCT6779_ALARM_BITS;
849
850 data->REG_CONFIG = NCT6775_REG_CONFIG;
851 data->REG_VBAT = NCT6775_REG_VBAT;
852 data->REG_VIN = NCT6779_REG_IN;
853 data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
854 data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
855 data->REG_ALARM = NCT6779_REG_ALARM;
856 break;
857 default:
858 return -ENODEV;
859 }
860 data->have_in = (1 << data->in_num) - 1;
861
862 /* Initialize the chip */
863 nct6775_init_device(data);
864
865 data->vrm = vid_which_vrm();
866 err = superio_enter(sio_data->sioreg);
867 if (err)
868 return err;
869
870 /*
871 * Read VID value
872 * We can get the VID input values directly at logical device D 0xe3.
873 */
874 superio_select(sio_data->sioreg, NCT6775_LD_VID);
875 data->vid = superio_inb(sio_data->sioreg, 0xe3);
876 superio_exit(sio_data->sioreg);
877
878 err = device_create_file(dev, &dev_attr_cpu0_vid);
879 if (err)
880 return err;
881
882 for (i = 0; i < data->in_num; i++) {
883 if (!(data->have_in & (1 << i)))
884 continue;
885 err = sysfs_create_group(&dev->kobj, &nct6775_group_in[i]);
886 if (err)
887 goto exit_remove;
888 }
889
Guenter Roecka6bd5872012-12-04 03:13:34 -0800890 for (i = 0; i < ARRAY_SIZE(sda_caseopen); i++) {
891 if (data->ALARM_BITS[INTRUSION_ALARM_BASE + i] < 0)
892 continue;
893 err = device_create_file(dev, &sda_caseopen[i].dev_attr);
894 if (err)
895 goto exit_remove;
896 }
897
Guenter Roeck9de2e2e2012-05-20 19:29:48 -0700898 err = device_create_file(dev, &dev_attr_name);
899 if (err)
900 goto exit_remove;
901
902 data->hwmon_dev = hwmon_device_register(dev);
903 if (IS_ERR(data->hwmon_dev)) {
904 err = PTR_ERR(data->hwmon_dev);
905 goto exit_remove;
906 }
907
908 return 0;
909
910exit_remove:
911 nct6775_device_remove_files(dev);
912 return err;
913}
914
915static int nct6775_remove(struct platform_device *pdev)
916{
917 struct nct6775_data *data = platform_get_drvdata(pdev);
918
919 hwmon_device_unregister(data->hwmon_dev);
920 nct6775_device_remove_files(&pdev->dev);
921
922 return 0;
923}
924
925static struct platform_driver nct6775_driver = {
926 .driver = {
927 .owner = THIS_MODULE,
928 .name = DRVNAME,
929 },
930 .probe = nct6775_probe,
931 .remove = nct6775_remove,
932};
933
934/* nct6775_find() looks for a '627 in the Super-I/O config space */
935static int __init nct6775_find(int sioaddr, unsigned short *addr,
936 struct nct6775_sio_data *sio_data)
937{
938 static const char sio_name_NCT6775[] __initconst = "NCT6775F";
939 static const char sio_name_NCT6776[] __initconst = "NCT6776F";
940 static const char sio_name_NCT6779[] __initconst = "NCT6779D";
941
942 u16 val;
943 const char *sio_name;
944 int err;
945
946 err = superio_enter(sioaddr);
947 if (err)
948 return err;
949
950 if (force_id)
951 val = force_id;
952 else
953 val = (superio_inb(sioaddr, SIO_REG_DEVID) << 8)
954 | superio_inb(sioaddr, SIO_REG_DEVID + 1);
955 switch (val & SIO_ID_MASK) {
956 case SIO_NCT6775_ID:
957 sio_data->kind = nct6775;
958 sio_name = sio_name_NCT6775;
959 break;
960 case SIO_NCT6776_ID:
961 sio_data->kind = nct6776;
962 sio_name = sio_name_NCT6776;
963 break;
964 case SIO_NCT6779_ID:
965 sio_data->kind = nct6779;
966 sio_name = sio_name_NCT6779;
967 break;
968 default:
969 if (val != 0xffff)
970 pr_debug("unsupported chip ID: 0x%04x\n", val);
971 superio_exit(sioaddr);
972 return -ENODEV;
973 }
974
975 /* We have a known chip, find the HWM I/O address */
976 superio_select(sioaddr, NCT6775_LD_HWM);
977 val = (superio_inb(sioaddr, SIO_REG_ADDR) << 8)
978 | superio_inb(sioaddr, SIO_REG_ADDR + 1);
979 *addr = val & IOREGION_ALIGNMENT;
980 if (*addr == 0) {
981 pr_err("Refusing to enable a Super-I/O device with a base I/O port 0\n");
982 superio_exit(sioaddr);
983 return -ENODEV;
984 }
985
986 /* Activate logical device if needed */
987 val = superio_inb(sioaddr, SIO_REG_ENABLE);
988 if (!(val & 0x01)) {
989 pr_warn("Forcibly enabling Super-I/O. Sensor is probably unusable.\n");
990 superio_outb(sioaddr, SIO_REG_ENABLE, val | 0x01);
991 }
992
993 superio_exit(sioaddr);
994 pr_info("Found %s chip at %#x\n", sio_name, *addr);
995 sio_data->sioreg = sioaddr;
996
997 return 0;
998}
999
1000/*
1001 * when Super-I/O functions move to a separate file, the Super-I/O
1002 * bus will manage the lifetime of the device and this module will only keep
1003 * track of the nct6775 driver. But since we platform_device_alloc(), we
1004 * must keep track of the device
1005 */
1006static struct platform_device *pdev;
1007
1008static int __init sensors_nct6775_init(void)
1009{
1010 int err;
1011 unsigned short address;
1012 struct resource res;
1013 struct nct6775_sio_data sio_data;
1014
1015 /*
1016 * initialize sio_data->kind and sio_data->sioreg.
1017 *
1018 * when Super-I/O functions move to a separate file, the Super-I/O
1019 * driver will probe 0x2e and 0x4e and auto-detect the presence of a
1020 * nct6775 hardware monitor, and call probe()
1021 */
1022 if (nct6775_find(0x2e, &address, &sio_data) &&
1023 nct6775_find(0x4e, &address, &sio_data))
1024 return -ENODEV;
1025
1026 err = platform_driver_register(&nct6775_driver);
1027 if (err)
1028 goto exit;
1029
1030 pdev = platform_device_alloc(DRVNAME, address);
1031 if (!pdev) {
1032 err = -ENOMEM;
1033 pr_err("Device allocation failed\n");
1034 goto exit_unregister;
1035 }
1036
1037 err = platform_device_add_data(pdev, &sio_data,
1038 sizeof(struct nct6775_sio_data));
1039 if (err) {
1040 pr_err("Platform data allocation failed\n");
1041 goto exit_device_put;
1042 }
1043
1044 memset(&res, 0, sizeof(res));
1045 res.name = DRVNAME;
1046 res.start = address + IOREGION_OFFSET;
1047 res.end = address + IOREGION_OFFSET + IOREGION_LENGTH - 1;
1048 res.flags = IORESOURCE_IO;
1049
1050 err = acpi_check_resource_conflict(&res);
1051 if (err)
1052 goto exit_device_put;
1053
1054 err = platform_device_add_resources(pdev, &res, 1);
1055 if (err) {
1056 pr_err("Device resource addition failed (%d)\n", err);
1057 goto exit_device_put;
1058 }
1059
1060 /* platform_device_add calls probe() */
1061 err = platform_device_add(pdev);
1062 if (err) {
1063 pr_err("Device addition failed (%d)\n", err);
1064 goto exit_device_put;
1065 }
1066
1067 return 0;
1068
1069exit_device_put:
1070 platform_device_put(pdev);
1071exit_unregister:
1072 platform_driver_unregister(&nct6775_driver);
1073exit:
1074 return err;
1075}
1076
1077static void __exit sensors_nct6775_exit(void)
1078{
1079 platform_device_unregister(pdev);
1080 platform_driver_unregister(&nct6775_driver);
1081}
1082
1083MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
1084MODULE_DESCRIPTION("NCT6775F/NCT6776F/NCT6779D driver");
1085MODULE_LICENSE("GPL");
1086
1087module_init(sensors_nct6775_init);
1088module_exit(sensors_nct6775_exit);