Version:  2.0.40 2.2.26 2.4.37 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

Linux/drivers/hwmon/nct6775.c

  1 /*
  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 <jdelvare@suse.de>
  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  * nct6106d     9      3       3       6+3    0xc450 0xc1    0x5ca3
 37  * nct6775f     9      4       3       6+3    0xb470 0xc1    0x5ca3
 38  * nct6776f     9      5       3       6+3    0xc330 0xc1    0x5ca3
 39  * nct6779d    15      5       5       2+6    0xc560 0xc1    0x5ca3
 40  * nct6791d    15      6       6       2+6    0xc800 0xc1    0x5ca3
 41  * nct6792d    15      6       6       2+6    0xc910 0xc1    0x5ca3
 42  * nct6793d    15      6       6       2+6    0xd120 0xc1    0x5ca3
 43  *
 44  * #temp lists the number of monitored temperature sources (first value) plus
 45  * the number of directly connectable temperature sensors (second value).
 46  */
 47 
 48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 49 
 50 #include <linux/module.h>
 51 #include <linux/init.h>
 52 #include <linux/slab.h>
 53 #include <linux/jiffies.h>
 54 #include <linux/platform_device.h>
 55 #include <linux/hwmon.h>
 56 #include <linux/hwmon-sysfs.h>
 57 #include <linux/hwmon-vid.h>
 58 #include <linux/err.h>
 59 #include <linux/mutex.h>
 60 #include <linux/acpi.h>
 61 #include <linux/dmi.h>
 62 #include <linux/io.h>
 63 #include "lm75.h"
 64 
 65 #define USE_ALTERNATE
 66 
 67 enum kinds { nct6106, nct6775, nct6776, nct6779, nct6791, nct6792, nct6793 };
 68 
 69 /* used to set data->name = nct6775_device_names[data->sio_kind] */
 70 static const char * const nct6775_device_names[] = {
 71         "nct6106",
 72         "nct6775",
 73         "nct6776",
 74         "nct6779",
 75         "nct6791",
 76         "nct6792",
 77         "nct6793",
 78 };
 79 
 80 static const char * const nct6775_sio_names[] __initconst = {
 81         "NCT6106D",
 82         "NCT6775F",
 83         "NCT6776D/F",
 84         "NCT6779D",
 85         "NCT6791D",
 86         "NCT6792D",
 87         "NCT6793D",
 88 };
 89 
 90 static unsigned short force_id;
 91 module_param(force_id, ushort, 0);
 92 MODULE_PARM_DESC(force_id, "Override the detected device ID");
 93 
 94 static unsigned short fan_debounce;
 95 module_param(fan_debounce, ushort, 0);
 96 MODULE_PARM_DESC(fan_debounce, "Enable debouncing for fan RPM signal");
 97 
 98 #define DRVNAME "nct6775"
 99 
100 /*
101  * Super-I/O constants and functions
102  */
103 
104 #define NCT6775_LD_ACPI         0x0a
105 #define NCT6775_LD_HWM          0x0b
106 #define NCT6775_LD_VID          0x0d
107 
108 #define SIO_REG_LDSEL           0x07    /* Logical device select */
109 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
110 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
111 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
112 
113 #define SIO_NCT6106_ID          0xc450
114 #define SIO_NCT6775_ID          0xb470
115 #define SIO_NCT6776_ID          0xc330
116 #define SIO_NCT6779_ID          0xc560
117 #define SIO_NCT6791_ID          0xc800
118 #define SIO_NCT6792_ID          0xc910
119 #define SIO_NCT6793_ID          0xd120
120 #define SIO_ID_MASK             0xFFF0
121 
122 enum pwm_enable { off, manual, thermal_cruise, speed_cruise, sf3, sf4 };
123 
124 static inline void
125 superio_outb(int ioreg, int reg, int val)
126 {
127         outb(reg, ioreg);
128         outb(val, ioreg + 1);
129 }
130 
131 static inline int
132 superio_inb(int ioreg, int reg)
133 {
134         outb(reg, ioreg);
135         return inb(ioreg + 1);
136 }
137 
138 static inline void
139 superio_select(int ioreg, int ld)
140 {
141         outb(SIO_REG_LDSEL, ioreg);
142         outb(ld, ioreg + 1);
143 }
144 
145 static inline int
146 superio_enter(int ioreg)
147 {
148         /*
149          * Try to reserve <ioreg> and <ioreg + 1> for exclusive access.
150          */
151         if (!request_muxed_region(ioreg, 2, DRVNAME))
152                 return -EBUSY;
153 
154         outb(0x87, ioreg);
155         outb(0x87, ioreg);
156 
157         return 0;
158 }
159 
160 static inline void
161 superio_exit(int ioreg)
162 {
163         outb(0xaa, ioreg);
164         outb(0x02, ioreg);
165         outb(0x02, ioreg + 1);
166         release_region(ioreg, 2);
167 }
168 
169 /*
170  * ISA constants
171  */
172 
173 #define IOREGION_ALIGNMENT      (~7)
174 #define IOREGION_OFFSET         5
175 #define IOREGION_LENGTH         2
176 #define ADDR_REG_OFFSET         0
177 #define DATA_REG_OFFSET         1
178 
179 #define NCT6775_REG_BANK        0x4E
180 #define NCT6775_REG_CONFIG      0x40
181 
182 /*
183  * Not currently used:
184  * REG_MAN_ID has the value 0x5ca3 for all supported chips.
185  * REG_CHIP_ID == 0x88/0xa1/0xc1 depending on chip model.
186  * REG_MAN_ID is at port 0x4f
187  * REG_CHIP_ID is at port 0x58
188  */
189 
190 #define NUM_TEMP        10      /* Max number of temp attribute sets w/ limits*/
191 #define NUM_TEMP_FIXED  6       /* Max number of fixed temp attribute sets */
192 
193 #define NUM_REG_ALARM   7       /* Max number of alarm registers */
194 #define NUM_REG_BEEP    5       /* Max number of beep registers */
195 
196 #define NUM_FAN         6
197 
198 #define TEMP_SOURCE_VIRTUAL     0x1f
199 
200 /* Common and NCT6775 specific data */
201 
202 /* Voltage min/max registers for nr=7..14 are in bank 5 */
203 
204 static const u16 NCT6775_REG_IN_MAX[] = {
205         0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
206         0x55c, 0x55e, 0x560, 0x562 };
207 static const u16 NCT6775_REG_IN_MIN[] = {
208         0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
209         0x55d, 0x55f, 0x561, 0x563 };
210 static const u16 NCT6775_REG_IN[] = {
211         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
212 };
213 
214 #define NCT6775_REG_VBAT                0x5D
215 #define NCT6775_REG_DIODE               0x5E
216 #define NCT6775_DIODE_MASK              0x02
217 
218 #define NCT6775_REG_FANDIV1             0x506
219 #define NCT6775_REG_FANDIV2             0x507
220 
221 #define NCT6775_REG_CR_FAN_DEBOUNCE     0xf0
222 
223 static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
224 
225 /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
226 
227 static const s8 NCT6775_ALARM_BITS[] = {
228         0, 1, 2, 3, 8, 21, 20, 16,      /* in0.. in7 */
229         17, -1, -1, -1, -1, -1, -1,     /* in8..in14 */
230         -1,                             /* unused */
231         6, 7, 11, -1, -1,               /* fan1..fan5 */
232         -1, -1, -1,                     /* unused */
233         4, 5, 13, -1, -1, -1,           /* temp1..temp6 */
234         12, -1 };                       /* intrusion0, intrusion1 */
235 
236 #define FAN_ALARM_BASE          16
237 #define TEMP_ALARM_BASE         24
238 #define INTRUSION_ALARM_BASE    30
239 
240 static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
241 
242 /*
243  * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
244  * 30..31 intrusion
245  */
246 static const s8 NCT6775_BEEP_BITS[] = {
247         0, 1, 2, 3, 8, 9, 10, 16,       /* in0.. in7 */
248         17, -1, -1, -1, -1, -1, -1,     /* in8..in14 */
249         21,                             /* global beep enable */
250         6, 7, 11, 28, -1,               /* fan1..fan5 */
251         -1, -1, -1,                     /* unused */
252         4, 5, 13, -1, -1, -1,           /* temp1..temp6 */
253         12, -1 };                       /* intrusion0, intrusion1 */
254 
255 #define BEEP_ENABLE_BASE                15
256 
257 static const u8 NCT6775_REG_CR_CASEOPEN_CLR[] = { 0xe6,