1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
5 * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
7 * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
8 * and its port to kernel 2.6 by Lars Ekman.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/jiffies.h>
17 #include <linux/platform_device.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/hwmon-vid.h>
21 #include <linux/err.h>
22 #include <linux/mutex.h>
23 #include <linux/ioport.h>
24 #include <linux/acpi.h>
27 static int uch_config
= -1;
28 module_param(uch_config
, int, 0);
29 MODULE_PARM_DESC(uch_config
, "Initialize the universal channel configuration");
31 static int int_mode
= -1;
32 module_param(int_mode
, int, 0);
33 MODULE_PARM_DESC(int_mode
, "Force the temperature interrupt mode");
35 static unsigned short force_id
;
36 module_param(force_id
, ushort
, 0);
37 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
39 static struct platform_device
*pdev
;
41 #define DRVNAME "vt1211"
43 /* ---------------------------------------------------------------------
46 * The sensors are defined as follows.
48 * Sensor Voltage Mode Temp Mode Notes (from the datasheet)
49 * -------- ------------ --------- --------------------------
50 * Reading 1 temp1 Intel thermal diode
51 * Reading 3 temp2 Internal thermal diode
52 * UCH1/Reading2 in0 temp3 NTC type thermistor
53 * UCH2 in1 temp4 +2.5V
57 * 3.3V in5 Internal VDD (+3.3V)
59 * --------------------------------------------------------------------- */
61 /* Voltages (in) numbered 0-5 (ix) */
62 #define VT1211_REG_IN(ix) (0x21 + (ix))
63 #define VT1211_REG_IN_MIN(ix) ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
64 #define VT1211_REG_IN_MAX(ix) ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
66 /* Temperatures (temp) numbered 0-6 (ix) */
67 static u8 regtemp
[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
68 static u8 regtempmax
[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
69 static u8 regtemphyst
[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
71 /* Fans numbered 0-1 (ix) */
72 #define VT1211_REG_FAN(ix) (0x29 + (ix))
73 #define VT1211_REG_FAN_MIN(ix) (0x3b + (ix))
74 #define VT1211_REG_FAN_DIV 0x47
76 /* PWMs numbered 0-1 (ix) */
77 /* Auto points numbered 0-3 (ap) */
78 #define VT1211_REG_PWM(ix) (0x60 + (ix))
79 #define VT1211_REG_PWM_CLK 0x50
80 #define VT1211_REG_PWM_CTL 0x51
81 #define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap))
82 #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
84 /* Miscellaneous registers */
85 #define VT1211_REG_CONFIG 0x40
86 #define VT1211_REG_ALARM1 0x41
87 #define VT1211_REG_ALARM2 0x42
88 #define VT1211_REG_VID 0x45
89 #define VT1211_REG_UCH_CONFIG 0x4a
90 #define VT1211_REG_TEMP1_CONFIG 0x4b
91 #define VT1211_REG_TEMP2_CONFIG 0x4c
93 /* In, temp & fan alarm bits */
94 static const u8 bitalarmin
[] = {11, 0, 1, 3, 8, 2, 9};
95 static const u8 bitalarmtemp
[] = {4, 15, 11, 0, 1, 3, 8};
96 static const u8 bitalarmfan
[] = {6, 7};
98 /* ---------------------------------------------------------------------
99 * Data structures and manipulation thereof
100 * --------------------------------------------------------------------- */
105 struct device
*hwmon_dev
;
107 struct mutex update_lock
;
108 char valid
; /* !=0 if following fields are valid */
109 unsigned long last_updated
; /* In jiffies */
111 /* Register values */
126 u8 pwm_auto_pwm
[2][4];
127 u8 vid
; /* Read once at init time */
129 u8 uch_config
; /* Read once at init time */
134 #define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \
135 !(((uch_config) >> ((ix) + 2)) & 1))
138 #define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \
139 ((uch_config) >> (ix)) & 1)
142 * in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
143 * driver according to the VT1211 BIOS porting guide
145 #define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \
146 (((reg) - 3) * 15882 + 479) / 958 : \
147 (((reg) - 3) * 10000 + 479) / 958)
148 #define IN_TO_REG(ix, val) (clamp_val((ix) == 5 ? \
149 ((val) * 958 + 7941) / 15882 + 3 : \
150 ((val) * 958 + 5000) / 10000 + 3, 0, 255))
153 * temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
154 * temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
155 * according to some measurements that I took on an EPIA M10000.
156 * temp3-7 are thermistor based so the driver returns the voltage measured at
157 * the pin (range 0V - 2.2V).
159 #define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \
160 (ix) == 1 ? (reg) < 51 ? 0 : \
161 ((reg) - 51) * 1000 : \
162 ((253 - (reg)) * 2200 + 105) / 210)
163 #define TEMP_TO_REG(ix, val) clamp_val( \
164 ((ix) == 0 ? ((val) + 500) / 1000 : \
165 (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
166 253 - ((val) * 210 + 1100) / 2200), 0, 255)
168 #define DIV_FROM_REG(reg) (1 << (reg))
170 #define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \
171 1310720 / (reg) / DIV_FROM_REG(div))
172 #define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \
173 clamp_val((1310720 / (val) / \
174 DIV_FROM_REG(div)), 1, 254))
176 /* ---------------------------------------------------------------------
177 * Super-I/O constants and functions
178 * --------------------------------------------------------------------- */
181 * Configuration index port registers
182 * The vt1211 can live at 2 different addresses so we need to probe both
184 #define SIO_REG_CIP1 0x2e
185 #define SIO_REG_CIP2 0x4e
187 /* Configuration registers */
188 #define SIO_VT1211_LDN 0x07 /* logical device number */
189 #define SIO_VT1211_DEVID 0x20 /* device ID */
190 #define SIO_VT1211_DEVREV 0x21 /* device revision */
191 #define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
192 #define SIO_VT1211_BADDR 0x60 /* base I/O address */
193 #define SIO_VT1211_ID 0x3c /* VT1211 device ID */
195 /* VT1211 logical device numbers */
196 #define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
198 static inline void superio_outb(int sio_cip
, int reg
, int val
)
201 outb(val
, sio_cip
+ 1);
204 static inline int superio_inb(int sio_cip
, int reg
)
207 return inb(sio_cip
+ 1);
210 static inline void superio_select(int sio_cip
, int ldn
)
212 outb(SIO_VT1211_LDN
, sio_cip
);
213 outb(ldn
, sio_cip
+ 1);
216 static inline int superio_enter(int sio_cip
)
218 if (!request_muxed_region(sio_cip
, 2, DRVNAME
))
227 static inline void superio_exit(int sio_cip
)
230 release_region(sio_cip
, 2);
233 /* ---------------------------------------------------------------------
235 * --------------------------------------------------------------------- */
237 static inline u8
vt1211_read8(struct vt1211_data
*data
, u8 reg
)
239 return inb(data
->addr
+ reg
);
242 static inline void vt1211_write8(struct vt1211_data
*data
, u8 reg
, u8 val
)
244 outb(val
, data
->addr
+ reg
);
247 static struct vt1211_data
*vt1211_update_device(struct device
*dev
)
249 struct vt1211_data
*data
= dev_get_drvdata(dev
);
252 mutex_lock(&data
->update_lock
);
254 /* registers cache is refreshed after 1 second */
255 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
257 data
->vid
= vt1211_read8(data
, VT1211_REG_VID
) & 0x1f;
259 /* voltage (in) registers */
260 for (ix
= 0; ix
< ARRAY_SIZE(data
->in
); ix
++) {
261 if (ISVOLT(ix
, data
->uch_config
)) {
262 data
->in
[ix
] = vt1211_read8(data
,
264 data
->in_min
[ix
] = vt1211_read8(data
,
265 VT1211_REG_IN_MIN(ix
));
266 data
->in_max
[ix
] = vt1211_read8(data
,
267 VT1211_REG_IN_MAX(ix
));
272 for (ix
= 0; ix
< ARRAY_SIZE(data
->temp
); ix
++) {
273 if (ISTEMP(ix
, data
->uch_config
)) {
274 data
->temp
[ix
] = vt1211_read8(data
,
276 data
->temp_max
[ix
] = vt1211_read8(data
,
278 data
->temp_hyst
[ix
] = vt1211_read8(data
,
283 /* fan & pwm registers */
284 for (ix
= 0; ix
< ARRAY_SIZE(data
->fan
); ix
++) {
285 data
->fan
[ix
] = vt1211_read8(data
,
287 data
->fan_min
[ix
] = vt1211_read8(data
,
288 VT1211_REG_FAN_MIN(ix
));
289 data
->pwm
[ix
] = vt1211_read8(data
,
292 val
= vt1211_read8(data
, VT1211_REG_FAN_DIV
);
293 data
->fan_div
[0] = (val
>> 4) & 3;
294 data
->fan_div
[1] = (val
>> 6) & 3;
295 data
->fan_ctl
= val
& 0xf;
297 val
= vt1211_read8(data
, VT1211_REG_PWM_CTL
);
298 data
->pwm_ctl
[0] = val
& 0xf;
299 data
->pwm_ctl
[1] = (val
>> 4) & 0xf;
301 data
->pwm_clk
= vt1211_read8(data
, VT1211_REG_PWM_CLK
);
303 /* pwm & temp auto point registers */
304 data
->pwm_auto_pwm
[0][1] = vt1211_read8(data
,
305 VT1211_REG_PWM_AUTO_PWM(0, 1));
306 data
->pwm_auto_pwm
[0][2] = vt1211_read8(data
,
307 VT1211_REG_PWM_AUTO_PWM(0, 2));
308 data
->pwm_auto_pwm
[1][1] = vt1211_read8(data
,
309 VT1211_REG_PWM_AUTO_PWM(1, 1));
310 data
->pwm_auto_pwm
[1][2] = vt1211_read8(data
,
311 VT1211_REG_PWM_AUTO_PWM(1, 2));
312 for (ix
= 0; ix
< ARRAY_SIZE(data
->pwm_auto_temp
); ix
++) {
313 data
->pwm_auto_temp
[ix
] = vt1211_read8(data
,
314 VT1211_REG_PWM_AUTO_TEMP(ix
));
317 /* alarm registers */
318 data
->alarms
= (vt1211_read8(data
, VT1211_REG_ALARM2
) << 8) |
319 vt1211_read8(data
, VT1211_REG_ALARM1
);
321 data
->last_updated
= jiffies
;
325 mutex_unlock(&data
->update_lock
);
330 /* ---------------------------------------------------------------------
331 * Voltage sysfs interfaces
333 * --------------------------------------------------------------------- */
335 #define SHOW_IN_INPUT 0
336 #define SHOW_SET_IN_MIN 1
337 #define SHOW_SET_IN_MAX 2
338 #define SHOW_IN_ALARM 3
340 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
343 struct vt1211_data
*data
= vt1211_update_device(dev
);
344 struct sensor_device_attribute_2
*sensor_attr_2
=
345 to_sensor_dev_attr_2(attr
);
346 int ix
= sensor_attr_2
->index
;
347 int fn
= sensor_attr_2
->nr
;
352 res
= IN_FROM_REG(ix
, data
->in
[ix
]);
354 case SHOW_SET_IN_MIN
:
355 res
= IN_FROM_REG(ix
, data
->in_min
[ix
]);
357 case SHOW_SET_IN_MAX
:
358 res
= IN_FROM_REG(ix
, data
->in_max
[ix
]);
361 res
= (data
->alarms
>> bitalarmin
[ix
]) & 1;
365 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
368 return sprintf(buf
, "%d\n", res
);
371 static ssize_t
set_in(struct device
*dev
, struct device_attribute
*attr
,
372 const char *buf
, size_t count
)
374 struct vt1211_data
*data
= dev_get_drvdata(dev
);
375 struct sensor_device_attribute_2
*sensor_attr_2
=
376 to_sensor_dev_attr_2(attr
);
377 int ix
= sensor_attr_2
->index
;
378 int fn
= sensor_attr_2
->nr
;
382 err
= kstrtol(buf
, 10, &val
);
386 mutex_lock(&data
->update_lock
);
388 case SHOW_SET_IN_MIN
:
389 data
->in_min
[ix
] = IN_TO_REG(ix
, val
);
390 vt1211_write8(data
, VT1211_REG_IN_MIN(ix
), data
->in_min
[ix
]);
392 case SHOW_SET_IN_MAX
:
393 data
->in_max
[ix
] = IN_TO_REG(ix
, val
);
394 vt1211_write8(data
, VT1211_REG_IN_MAX(ix
), data
->in_max
[ix
]);
397 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
399 mutex_unlock(&data
->update_lock
);
404 /* ---------------------------------------------------------------------
405 * Temperature sysfs interfaces
407 * --------------------------------------------------------------------- */
409 #define SHOW_TEMP_INPUT 0
410 #define SHOW_SET_TEMP_MAX 1
411 #define SHOW_SET_TEMP_MAX_HYST 2
412 #define SHOW_TEMP_ALARM 3
414 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
417 struct vt1211_data
*data
= vt1211_update_device(dev
);
418 struct sensor_device_attribute_2
*sensor_attr_2
=
419 to_sensor_dev_attr_2(attr
);
420 int ix
= sensor_attr_2
->index
;
421 int fn
= sensor_attr_2
->nr
;
425 case SHOW_TEMP_INPUT
:
426 res
= TEMP_FROM_REG(ix
, data
->temp
[ix
]);
428 case SHOW_SET_TEMP_MAX
:
429 res
= TEMP_FROM_REG(ix
, data
->temp_max
[ix
]);
431 case SHOW_SET_TEMP_MAX_HYST
:
432 res
= TEMP_FROM_REG(ix
, data
->temp_hyst
[ix
]);
434 case SHOW_TEMP_ALARM
:
435 res
= (data
->alarms
>> bitalarmtemp
[ix
]) & 1;
439 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
442 return sprintf(buf
, "%d\n", res
);
445 static ssize_t
set_temp(struct device
*dev
, struct device_attribute
*attr
,
446 const char *buf
, size_t count
)
448 struct vt1211_data
*data
= dev_get_drvdata(dev
);
449 struct sensor_device_attribute_2
*sensor_attr_2
=
450 to_sensor_dev_attr_2(attr
);
451 int ix
= sensor_attr_2
->index
;
452 int fn
= sensor_attr_2
->nr
;
456 err
= kstrtol(buf
, 10, &val
);
460 mutex_lock(&data
->update_lock
);
462 case SHOW_SET_TEMP_MAX
:
463 data
->temp_max
[ix
] = TEMP_TO_REG(ix
, val
);
464 vt1211_write8(data
, regtempmax
[ix
],
467 case SHOW_SET_TEMP_MAX_HYST
:
468 data
->temp_hyst
[ix
] = TEMP_TO_REG(ix
, val
);
469 vt1211_write8(data
, regtemphyst
[ix
],
470 data
->temp_hyst
[ix
]);
473 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
475 mutex_unlock(&data
->update_lock
);
480 /* ---------------------------------------------------------------------
481 * Fan sysfs interfaces
483 * --------------------------------------------------------------------- */
485 #define SHOW_FAN_INPUT 0
486 #define SHOW_SET_FAN_MIN 1
487 #define SHOW_SET_FAN_DIV 2
488 #define SHOW_FAN_ALARM 3
490 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
493 struct vt1211_data
*data
= vt1211_update_device(dev
);
494 struct sensor_device_attribute_2
*sensor_attr_2
=
495 to_sensor_dev_attr_2(attr
);
496 int ix
= sensor_attr_2
->index
;
497 int fn
= sensor_attr_2
->nr
;
502 res
= RPM_FROM_REG(data
->fan
[ix
], data
->fan_div
[ix
]);
504 case SHOW_SET_FAN_MIN
:
505 res
= RPM_FROM_REG(data
->fan_min
[ix
], data
->fan_div
[ix
]);
507 case SHOW_SET_FAN_DIV
:
508 res
= DIV_FROM_REG(data
->fan_div
[ix
]);
511 res
= (data
->alarms
>> bitalarmfan
[ix
]) & 1;
515 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
518 return sprintf(buf
, "%d\n", res
);
521 static ssize_t
set_fan(struct device
*dev
, struct device_attribute
*attr
,
522 const char *buf
, size_t count
)
524 struct vt1211_data
*data
= dev_get_drvdata(dev
);
525 struct sensor_device_attribute_2
*sensor_attr_2
=
526 to_sensor_dev_attr_2(attr
);
527 int ix
= sensor_attr_2
->index
;
528 int fn
= sensor_attr_2
->nr
;
533 err
= kstrtoul(buf
, 10, &val
);
537 mutex_lock(&data
->update_lock
);
539 /* sync the data cache */
540 reg
= vt1211_read8(data
, VT1211_REG_FAN_DIV
);
541 data
->fan_div
[0] = (reg
>> 4) & 3;
542 data
->fan_div
[1] = (reg
>> 6) & 3;
543 data
->fan_ctl
= reg
& 0xf;
546 case SHOW_SET_FAN_MIN
:
547 data
->fan_min
[ix
] = RPM_TO_REG(val
, data
->fan_div
[ix
]);
548 vt1211_write8(data
, VT1211_REG_FAN_MIN(ix
),
551 case SHOW_SET_FAN_DIV
:
554 data
->fan_div
[ix
] = 0;
557 data
->fan_div
[ix
] = 1;
560 data
->fan_div
[ix
] = 2;
563 data
->fan_div
[ix
] = 3;
568 "fan div value %ld not supported. Choose one of 1, 2, 4, or 8.\n",
572 vt1211_write8(data
, VT1211_REG_FAN_DIV
,
573 ((data
->fan_div
[1] << 6) |
574 (data
->fan_div
[0] << 4) |
578 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
582 mutex_unlock(&data
->update_lock
);
586 /* ---------------------------------------------------------------------
587 * PWM sysfs interfaces
589 * --------------------------------------------------------------------- */
592 #define SHOW_SET_PWM_ENABLE 1
593 #define SHOW_SET_PWM_FREQ 2
594 #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
596 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
599 struct vt1211_data
*data
= vt1211_update_device(dev
);
600 struct sensor_device_attribute_2
*sensor_attr_2
=
601 to_sensor_dev_attr_2(attr
);
602 int ix
= sensor_attr_2
->index
;
603 int fn
= sensor_attr_2
->nr
;
610 case SHOW_SET_PWM_ENABLE
:
611 res
= ((data
->pwm_ctl
[ix
] >> 3) & 1) ? 2 : 0;
613 case SHOW_SET_PWM_FREQ
:
614 res
= 90000 >> (data
->pwm_clk
& 7);
616 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP
:
617 res
= (data
->pwm_ctl
[ix
] & 7) + 1;
621 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
624 return sprintf(buf
, "%d\n", res
);
627 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
628 const char *buf
, size_t count
)
630 struct vt1211_data
*data
= dev_get_drvdata(dev
);
631 struct sensor_device_attribute_2
*sensor_attr_2
=
632 to_sensor_dev_attr_2(attr
);
633 int ix
= sensor_attr_2
->index
;
634 int fn
= sensor_attr_2
->nr
;
639 err
= kstrtoul(buf
, 10, &val
);
643 mutex_lock(&data
->update_lock
);
646 case SHOW_SET_PWM_ENABLE
:
647 /* sync the data cache */
648 reg
= vt1211_read8(data
, VT1211_REG_FAN_DIV
);
649 data
->fan_div
[0] = (reg
>> 4) & 3;
650 data
->fan_div
[1] = (reg
>> 6) & 3;
651 data
->fan_ctl
= reg
& 0xf;
652 reg
= vt1211_read8(data
, VT1211_REG_PWM_CTL
);
653 data
->pwm_ctl
[0] = reg
& 0xf;
654 data
->pwm_ctl
[1] = (reg
>> 4) & 0xf;
657 data
->pwm_ctl
[ix
] &= 7;
659 * disable SmartGuardian if both PWM outputs are
662 if ((data
->pwm_ctl
[ix
^ 1] & 1) == 0)
663 data
->fan_ctl
&= 0xe;
666 data
->pwm_ctl
[ix
] |= 8;
672 "pwm mode %ld not supported. Choose one of 0 or 2.\n",
676 vt1211_write8(data
, VT1211_REG_PWM_CTL
,
677 ((data
->pwm_ctl
[1] << 4) |
679 vt1211_write8(data
, VT1211_REG_FAN_DIV
,
680 ((data
->fan_div
[1] << 6) |
681 (data
->fan_div
[0] << 4) |
684 case SHOW_SET_PWM_FREQ
:
685 val
= 135000 / clamp_val(val
, 135000 >> 7, 135000);
686 /* calculate tmp = log2(val) */
688 for (val
>>= 1; val
> 0; val
>>= 1)
690 /* sync the data cache */
691 reg
= vt1211_read8(data
, VT1211_REG_PWM_CLK
);
692 data
->pwm_clk
= (reg
& 0xf8) | tmp
;
693 vt1211_write8(data
, VT1211_REG_PWM_CLK
, data
->pwm_clk
);
695 case SHOW_SET_PWM_AUTO_CHANNELS_TEMP
:
696 if (val
< 1 || val
> 7) {
699 "temp channel %ld not supported. Choose a value between 1 and 7.\n",
703 if (!ISTEMP(val
- 1, data
->uch_config
)) {
705 dev_warn(dev
, "temp channel %ld is not available.\n",
709 /* sync the data cache */
710 reg
= vt1211_read8(data
, VT1211_REG_PWM_CTL
);
711 data
->pwm_ctl
[0] = reg
& 0xf;
712 data
->pwm_ctl
[1] = (reg
>> 4) & 0xf;
713 data
->pwm_ctl
[ix
] = (data
->pwm_ctl
[ix
] & 8) | (val
- 1);
714 vt1211_write8(data
, VT1211_REG_PWM_CTL
,
715 ((data
->pwm_ctl
[1] << 4) | data
->pwm_ctl
[0]));
718 dev_dbg(dev
, "Unknown attr fetch (%d)\n", fn
);
722 mutex_unlock(&data
->update_lock
);
726 /* ---------------------------------------------------------------------
727 * PWM auto point definitions
730 * --------------------------------------------------------------------- */
733 * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
734 * Note that there is only a single set of temp auto points that controls both
735 * PWM controllers. We still create 2 sets of sysfs files to make it look
736 * more consistent even though they map to the same registers.
738 * ix ap : description
739 * -------------------
740 * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
741 * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
742 * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
743 * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
744 * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
745 * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
746 * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
747 * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
750 static ssize_t
show_pwm_auto_point_temp(struct device
*dev
,
751 struct device_attribute
*attr
,
754 struct vt1211_data
*data
= vt1211_update_device(dev
);
755 struct sensor_device_attribute_2
*sensor_attr_2
=
756 to_sensor_dev_attr_2(attr
);
757 int ix
= sensor_attr_2
->index
;
758 int ap
= sensor_attr_2
->nr
;
760 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->pwm_ctl
[ix
] & 7,
761 data
->pwm_auto_temp
[ap
]));
764 static ssize_t
set_pwm_auto_point_temp(struct device
*dev
,
765 struct device_attribute
*attr
,
766 const char *buf
, size_t count
)
768 struct vt1211_data
*data
= dev_get_drvdata(dev
);
769 struct sensor_device_attribute_2
*sensor_attr_2
=
770 to_sensor_dev_attr_2(attr
);
771 int ix
= sensor_attr_2
->index
;
772 int ap
= sensor_attr_2
->nr
;
777 err
= kstrtol(buf
, 10, &val
);
782 mutex_lock(&data
->update_lock
);
784 /* sync the data cache */
785 reg
= vt1211_read8(data
, VT1211_REG_PWM_CTL
);
786 data
->pwm_ctl
[0] = reg
& 0xf;
787 data
->pwm_ctl
[1] = (reg
>> 4) & 0xf;
789 data
->pwm_auto_temp
[ap
] = TEMP_TO_REG(data
->pwm_ctl
[ix
] & 7, val
);
790 vt1211_write8(data
, VT1211_REG_PWM_AUTO_TEMP(ap
),
791 data
->pwm_auto_temp
[ap
]);
792 mutex_unlock(&data
->update_lock
);
798 * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
799 * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
802 * ix ap : description
803 * -------------------
804 * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
805 * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
806 * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
807 * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
808 * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
809 * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
810 * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
811 * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
814 static ssize_t
show_pwm_auto_point_pwm(struct device
*dev
,
815 struct device_attribute
*attr
,
818 struct vt1211_data
*data
= vt1211_update_device(dev
);
819 struct sensor_device_attribute_2
*sensor_attr_2
=
820 to_sensor_dev_attr_2(attr
);
821 int ix
= sensor_attr_2
->index
;
822 int ap
= sensor_attr_2
->nr
;
824 return sprintf(buf
, "%d\n", data
->pwm_auto_pwm
[ix
][ap
]);
827 static ssize_t
set_pwm_auto_point_pwm(struct device
*dev
,
828 struct device_attribute
*attr
,
829 const char *buf
, size_t count
)
831 struct vt1211_data
*data
= dev_get_drvdata(dev
);
832 struct sensor_device_attribute_2
*sensor_attr_2
=
833 to_sensor_dev_attr_2(attr
);
834 int ix
= sensor_attr_2
->index
;
835 int ap
= sensor_attr_2
->nr
;
839 err
= kstrtoul(buf
, 10, &val
);
843 mutex_lock(&data
->update_lock
);
844 data
->pwm_auto_pwm
[ix
][ap
] = clamp_val(val
, 0, 255);
845 vt1211_write8(data
, VT1211_REG_PWM_AUTO_PWM(ix
, ap
),
846 data
->pwm_auto_pwm
[ix
][ap
]);
847 mutex_unlock(&data
->update_lock
);
852 /* ---------------------------------------------------------------------
853 * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
854 * --------------------------------------------------------------------- */
856 static ssize_t
show_vrm(struct device
*dev
, struct device_attribute
*attr
,
859 struct vt1211_data
*data
= dev_get_drvdata(dev
);
861 return sprintf(buf
, "%d\n", data
->vrm
);
864 static ssize_t
set_vrm(struct device
*dev
, struct device_attribute
*attr
,
865 const char *buf
, size_t count
)
867 struct vt1211_data
*data
= dev_get_drvdata(dev
);
871 err
= kstrtoul(buf
, 10, &val
);
883 static ssize_t
show_vid(struct device
*dev
, struct device_attribute
*attr
,
886 struct vt1211_data
*data
= dev_get_drvdata(dev
);
888 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
, data
->vrm
));
891 static ssize_t
show_name(struct device
*dev
,
892 struct device_attribute
*attr
, char *buf
)
894 struct vt1211_data
*data
= dev_get_drvdata(dev
);
896 return sprintf(buf
, "%s\n", data
->name
);
899 static ssize_t
show_alarms(struct device
*dev
,
900 struct device_attribute
*attr
, char *buf
)
902 struct vt1211_data
*data
= vt1211_update_device(dev
);
904 return sprintf(buf
, "%d\n", data
->alarms
);
907 /* ---------------------------------------------------------------------
908 * Device attribute structs
909 * --------------------------------------------------------------------- */
911 #define SENSOR_ATTR_IN(ix) \
912 { SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
913 show_in, NULL, SHOW_IN_INPUT, ix), \
914 SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
915 show_in, set_in, SHOW_SET_IN_MIN, ix), \
916 SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
917 show_in, set_in, SHOW_SET_IN_MAX, ix), \
918 SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
919 show_in, NULL, SHOW_IN_ALARM, ix) \
922 static struct sensor_device_attribute_2 vt1211_sysfs_in
[][4] = {
931 #define IN_UNIT_ATTRS(X) \
932 { &vt1211_sysfs_in[X][0].dev_attr.attr, \
933 &vt1211_sysfs_in[X][1].dev_attr.attr, \
934 &vt1211_sysfs_in[X][2].dev_attr.attr, \
935 &vt1211_sysfs_in[X][3].dev_attr.attr, \
939 static struct attribute
*vt1211_in_attr
[][5] = {
948 static const struct attribute_group vt1211_in_attr_group
[] = {
949 { .attrs
= vt1211_in_attr
[0] },
950 { .attrs
= vt1211_in_attr
[1] },
951 { .attrs
= vt1211_in_attr
[2] },
952 { .attrs
= vt1211_in_attr
[3] },
953 { .attrs
= vt1211_in_attr
[4] },
954 { .attrs
= vt1211_in_attr
[5] }
957 #define SENSOR_ATTR_TEMP(ix) \
958 { SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
959 show_temp, NULL, SHOW_TEMP_INPUT, ix-1), \
960 SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
961 show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1), \
962 SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
963 show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1), \
964 SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
965 show_temp, NULL, SHOW_TEMP_ALARM, ix-1) \
968 static struct sensor_device_attribute_2 vt1211_sysfs_temp
[][4] = {
978 #define TEMP_UNIT_ATTRS(X) \
979 { &vt1211_sysfs_temp[X][0].dev_attr.attr, \
980 &vt1211_sysfs_temp[X][1].dev_attr.attr, \
981 &vt1211_sysfs_temp[X][2].dev_attr.attr, \
982 &vt1211_sysfs_temp[X][3].dev_attr.attr, \
986 static struct attribute
*vt1211_temp_attr
[][5] = {
996 static const struct attribute_group vt1211_temp_attr_group
[] = {
997 { .attrs
= vt1211_temp_attr
[0] },
998 { .attrs
= vt1211_temp_attr
[1] },
999 { .attrs
= vt1211_temp_attr
[2] },
1000 { .attrs
= vt1211_temp_attr
[3] },
1001 { .attrs
= vt1211_temp_attr
[4] },
1002 { .attrs
= vt1211_temp_attr
[5] },
1003 { .attrs
= vt1211_temp_attr
[6] }
1006 #define SENSOR_ATTR_FAN(ix) \
1007 SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
1008 show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
1009 SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1010 show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
1011 SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
1012 show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
1013 SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1014 show_fan, NULL, SHOW_FAN_ALARM, ix-1)
1016 #define SENSOR_ATTR_PWM(ix) \
1017 SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
1018 show_pwm, NULL, SHOW_PWM, ix-1), \
1019 SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
1020 show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
1021 SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
1022 show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
1024 #define SENSOR_ATTR_PWM_FREQ(ix) \
1025 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1026 show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
1028 #define SENSOR_ATTR_PWM_FREQ_RO(ix) \
1029 SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1030 show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
1032 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
1033 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
1034 show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
1037 #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
1038 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
1039 show_pwm_auto_point_temp, NULL, \
1042 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
1043 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
1044 show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
1047 #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
1048 SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
1049 show_pwm_auto_point_pwm, NULL, \
1052 static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm
[] = {
1057 SENSOR_ATTR_PWM_FREQ(1),
1058 SENSOR_ATTR_PWM_FREQ_RO(2),
1059 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
1060 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
1061 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
1062 SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
1063 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
1064 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
1065 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
1066 SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
1067 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
1068 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
1069 SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
1070 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
1071 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
1072 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
1073 SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
1074 SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
1077 static struct device_attribute vt1211_sysfs_misc
[] = {
1078 __ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm
, set_vrm
),
1079 __ATTR(cpu0_vid
, S_IRUGO
, show_vid
, NULL
),
1080 __ATTR(name
, S_IRUGO
, show_name
, NULL
),
1081 __ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
),
1084 /* ---------------------------------------------------------------------
1085 * Device registration and initialization
1086 * --------------------------------------------------------------------- */
1088 static void vt1211_init_device(struct vt1211_data
*data
)
1091 data
->vrm
= vid_which_vrm();
1093 /* Read (and initialize) UCH config */
1094 data
->uch_config
= vt1211_read8(data
, VT1211_REG_UCH_CONFIG
);
1095 if (uch_config
> -1) {
1096 data
->uch_config
= (data
->uch_config
& 0x83) |
1098 vt1211_write8(data
, VT1211_REG_UCH_CONFIG
, data
->uch_config
);
1102 * Initialize the interrupt mode (if request at module load time).
1103 * The VT1211 implements 3 different modes for clearing interrupts:
1104 * 0: Clear INT when status register is read. Regenerate INT as long
1105 * as temp stays above hysteresis limit.
1106 * 1: Clear INT when status register is read. DON'T regenerate INT
1107 * until temp falls below hysteresis limit and exceeds hot limit
1109 * 2: Clear INT when temp falls below max limit.
1111 * The driver only allows to force mode 0 since that's the only one
1112 * that makes sense for 'sensors'
1114 if (int_mode
== 0) {
1115 vt1211_write8(data
, VT1211_REG_TEMP1_CONFIG
, 0);
1116 vt1211_write8(data
, VT1211_REG_TEMP2_CONFIG
, 0);
1119 /* Fill in some hard wired values into our data struct */
1120 data
->pwm_auto_pwm
[0][3] = 255;
1121 data
->pwm_auto_pwm
[1][3] = 255;
1124 static void vt1211_remove_sysfs(struct platform_device
*pdev
)
1126 struct device
*dev
= &pdev
->dev
;
1129 for (i
= 0; i
< ARRAY_SIZE(vt1211_in_attr_group
); i
++)
1130 sysfs_remove_group(&dev
->kobj
, &vt1211_in_attr_group
[i
]);
1132 for (i
= 0; i
< ARRAY_SIZE(vt1211_temp_attr_group
); i
++)
1133 sysfs_remove_group(&dev
->kobj
, &vt1211_temp_attr_group
[i
]);
1135 for (i
= 0; i
< ARRAY_SIZE(vt1211_sysfs_fan_pwm
); i
++) {
1136 device_remove_file(dev
,
1137 &vt1211_sysfs_fan_pwm
[i
].dev_attr
);
1139 for (i
= 0; i
< ARRAY_SIZE(vt1211_sysfs_misc
); i
++)
1140 device_remove_file(dev
, &vt1211_sysfs_misc
[i
]);
1143 static int vt1211_probe(struct platform_device
*pdev
)
1145 struct device
*dev
= &pdev
->dev
;
1146 struct vt1211_data
*data
;
1147 struct resource
*res
;
1150 data
= devm_kzalloc(dev
, sizeof(struct vt1211_data
), GFP_KERNEL
);
1154 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
1155 if (!devm_request_region(dev
, res
->start
, resource_size(res
),
1157 dev_err(dev
, "Failed to request region 0x%lx-0x%lx\n",
1158 (unsigned long)res
->start
, (unsigned long)res
->end
);
1161 data
->addr
= res
->start
;
1162 data
->name
= DRVNAME
;
1163 mutex_init(&data
->update_lock
);
1165 platform_set_drvdata(pdev
, data
);
1167 /* Initialize the VT1211 chip */
1168 vt1211_init_device(data
);
1170 /* Create sysfs interface files */
1171 for (i
= 0; i
< ARRAY_SIZE(vt1211_in_attr_group
); i
++) {
1172 if (ISVOLT(i
, data
->uch_config
)) {
1173 err
= sysfs_create_group(&dev
->kobj
,
1174 &vt1211_in_attr_group
[i
]);
1176 goto EXIT_DEV_REMOVE
;
1179 for (i
= 0; i
< ARRAY_SIZE(vt1211_temp_attr_group
); i
++) {
1180 if (ISTEMP(i
, data
->uch_config
)) {
1181 err
= sysfs_create_group(&dev
->kobj
,
1182 &vt1211_temp_attr_group
[i
]);
1184 goto EXIT_DEV_REMOVE
;
1187 for (i
= 0; i
< ARRAY_SIZE(vt1211_sysfs_fan_pwm
); i
++) {
1188 err
= device_create_file(dev
,
1189 &vt1211_sysfs_fan_pwm
[i
].dev_attr
);
1191 goto EXIT_DEV_REMOVE
;
1193 for (i
= 0; i
< ARRAY_SIZE(vt1211_sysfs_misc
); i
++) {
1194 err
= device_create_file(dev
,
1195 &vt1211_sysfs_misc
[i
]);
1197 goto EXIT_DEV_REMOVE
;
1200 /* Register device */
1201 data
->hwmon_dev
= hwmon_device_register(dev
);
1202 if (IS_ERR(data
->hwmon_dev
)) {
1203 err
= PTR_ERR(data
->hwmon_dev
);
1204 dev_err(dev
, "Class registration failed (%d)\n", err
);
1205 goto EXIT_DEV_REMOVE_SILENT
;
1211 dev_err(dev
, "Sysfs interface creation failed (%d)\n", err
);
1212 EXIT_DEV_REMOVE_SILENT
:
1213 vt1211_remove_sysfs(pdev
);
1217 static int vt1211_remove(struct platform_device
*pdev
)
1219 struct vt1211_data
*data
= platform_get_drvdata(pdev
);
1221 hwmon_device_unregister(data
->hwmon_dev
);
1222 vt1211_remove_sysfs(pdev
);
1227 static struct platform_driver vt1211_driver
= {
1231 .probe
= vt1211_probe
,
1232 .remove
= vt1211_remove
,
1235 static int __init
vt1211_device_add(unsigned short address
)
1237 struct resource res
= {
1239 .end
= address
+ 0x7f,
1240 .flags
= IORESOURCE_IO
,
1244 pdev
= platform_device_alloc(DRVNAME
, address
);
1247 pr_err("Device allocation failed (%d)\n", err
);
1251 res
.name
= pdev
->name
;
1252 err
= acpi_check_resource_conflict(&res
);
1256 err
= platform_device_add_resources(pdev
, &res
, 1);
1258 pr_err("Device resource addition failed (%d)\n", err
);
1262 err
= platform_device_add(pdev
);
1264 pr_err("Device addition failed (%d)\n", err
);
1271 platform_device_put(pdev
);
1276 static int __init
vt1211_find(int sio_cip
, unsigned short *address
)
1281 err
= superio_enter(sio_cip
);
1286 devid
= force_id
? force_id
: superio_inb(sio_cip
, SIO_VT1211_DEVID
);
1287 if (devid
!= SIO_VT1211_ID
)
1290 superio_select(sio_cip
, SIO_VT1211_LDN_HWMON
);
1292 if ((superio_inb(sio_cip
, SIO_VT1211_ACTIVE
) & 1) == 0) {
1293 pr_warn("HW monitor is disabled, skipping\n");
1297 *address
= ((superio_inb(sio_cip
, SIO_VT1211_BADDR
) << 8) |
1298 (superio_inb(sio_cip
, SIO_VT1211_BADDR
+ 1))) & 0xff00;
1299 if (*address
== 0) {
1300 pr_warn("Base address is not set, skipping\n");
1305 pr_info("Found VT1211 chip at 0x%04x, revision %u\n",
1306 *address
, superio_inb(sio_cip
, SIO_VT1211_DEVREV
));
1309 superio_exit(sio_cip
);
1313 static int __init
vt1211_init(void)
1316 unsigned short address
= 0;
1318 err
= vt1211_find(SIO_REG_CIP1
, &address
);
1320 err
= vt1211_find(SIO_REG_CIP2
, &address
);
1325 if ((uch_config
< -1) || (uch_config
> 31)) {
1327 pr_warn("Invalid UCH configuration %d. Choose a value between 0 and 31.\n",
1332 if ((int_mode
< -1) || (int_mode
> 0)) {
1334 pr_warn("Invalid interrupt mode %d. Only mode 0 is supported.\n",
1339 err
= platform_driver_register(&vt1211_driver
);
1343 /* Sets global pdev as a side effect */
1344 err
= vt1211_device_add(address
);
1346 goto EXIT_DRV_UNREGISTER
;
1350 EXIT_DRV_UNREGISTER
:
1351 platform_driver_unregister(&vt1211_driver
);
1356 static void __exit
vt1211_exit(void)
1358 platform_device_unregister(pdev
);
1359 platform_driver_unregister(&vt1211_driver
);
1362 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
1363 MODULE_DESCRIPTION("VT1211 sensors");
1364 MODULE_LICENSE("GPL");
1366 module_init(vt1211_init
);
1367 module_exit(vt1211_exit
);