| 1 | /* |
|---|
| 2 | w83627ehf - Driver for the hardware monitoring functionality of |
|---|
| 3 | the Winbond W83627EHF Super-I/O chip |
|---|
| 4 | Copyright (C) 2005, 2007 Jean Delvare <khali@linux-fr.org> |
|---|
| 5 | Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz> |
|---|
| 6 | Backported to kernel 2.4 by Yuan Mu (Winbond). |
|---|
| 7 | |
|---|
| 8 | Shamelessly ripped from the w83627hf driver |
|---|
| 9 | Copyright (C) 2003 Mark Studebaker |
|---|
| 10 | |
|---|
| 11 | This driver also supports the W83627EHG, which is the lead-free |
|---|
| 12 | version of the W83627EHF. |
|---|
| 13 | |
|---|
| 14 | This program is free software; you can redistribute it and/or modify |
|---|
| 15 | it under the terms of the GNU General Public License as published by |
|---|
| 16 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 17 | (at your option) any later version. |
|---|
| 18 | |
|---|
| 19 | This program is distributed in the hope that it will be useful, |
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | GNU General Public License for more details. |
|---|
| 23 | |
|---|
| 24 | You should have received a copy of the GNU General Public License |
|---|
| 25 | along with this program; if not, write to the Free Software |
|---|
| 26 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 27 | |
|---|
| 28 | Supports the following chips: |
|---|
| 29 | |
|---|
| 30 | Chip #vin #fan #pwm #temp |
|---|
| 31 | w83627ehf 10 5 4 3 |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include <linux/module.h> |
|---|
| 35 | #include <linux/slab.h> |
|---|
| 36 | #include <linux/ioport.h> |
|---|
| 37 | #include <linux/i2c.h> |
|---|
| 38 | #include <linux/i2c-proc.h> |
|---|
| 39 | #include <linux/init.h> |
|---|
| 40 | #include <asm/io.h> |
|---|
| 41 | #include "version.h" |
|---|
| 42 | #include "lm75.h" |
|---|
| 43 | |
|---|
| 44 | /* The actual ISA address is read from Super-I/O configuration space */ |
|---|
| 45 | static unsigned short normal_i2c[] = { SENSORS_I2C_END }; |
|---|
| 46 | static unsigned short normal_i2c_range[] = { SENSORS_I2C_END }; |
|---|
| 47 | static unsigned int normal_isa[] = { 0, SENSORS_ISA_END }; |
|---|
| 48 | static unsigned int normal_isa_range[] = { SENSORS_ISA_END }; |
|---|
| 49 | |
|---|
| 50 | /* Insmod parameters */ |
|---|
| 51 | SENSORS_INSMOD_1(w83627ehf); |
|---|
| 52 | |
|---|
| 53 | /* |
|---|
| 54 | * Super-I/O constants and functions |
|---|
| 55 | */ |
|---|
| 56 | |
|---|
| 57 | static int REG; /* The register to read/write */ |
|---|
| 58 | static int VAL; /* The value to read/write */ |
|---|
| 59 | |
|---|
| 60 | #define W83627EHF_LD_HWM 0x0b |
|---|
| 61 | |
|---|
| 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) */ |
|---|
| 66 | |
|---|
| 67 | #define SIO_W83627EHF_ID 0x8850 |
|---|
| 68 | #define SIO_W83627EHG_ID 0x8860 |
|---|
| 69 | #define SIO_ID_MASK 0xFFF0 |
|---|
| 70 | |
|---|
| 71 | static inline void superio_outb(int reg, int val) |
|---|
| 72 | { |
|---|
| 73 | outb(reg, REG); |
|---|
| 74 | outb(val, VAL); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | static inline int superio_inb(int reg) |
|---|
| 78 | { |
|---|
| 79 | outb(reg, REG); |
|---|
| 80 | return inb(VAL); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | static inline void superio_select(int ld) |
|---|
| 84 | { |
|---|
| 85 | outb(SIO_REG_LDSEL, REG); |
|---|
| 86 | outb(ld, VAL); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | static inline void superio_enter(void) |
|---|
| 90 | { |
|---|
| 91 | outb(0x87, REG); |
|---|
| 92 | outb(0x87, REG); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | static inline void superio_exit(void) |
|---|
| 96 | { |
|---|
| 97 | outb(0x02, REG); |
|---|
| 98 | outb(0x02, VAL); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /* |
|---|
| 102 | * ISA constants |
|---|
| 103 | */ |
|---|
| 104 | |
|---|
| 105 | #define REGION_ALIGNMENT ~7 |
|---|
| 106 | #define REGION_OFFSET 5 |
|---|
| 107 | #define REGION_LENGTH 2 |
|---|
| 108 | #define ADDR_REG_OFFSET 5 |
|---|
| 109 | #define DATA_REG_OFFSET 6 |
|---|
| 110 | |
|---|
| 111 | #define W83627EHF_REG_BANK 0x4E |
|---|
| 112 | #define W83627EHF_REG_CONFIG 0x40 |
|---|
| 113 | |
|---|
| 114 | static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 }; |
|---|
| 115 | static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c }; |
|---|
| 116 | |
|---|
| 117 | /* The W83627EHF registers for nr=7,8,9 are in bank 5 */ |
|---|
| 118 | #define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \ |
|---|
| 119 | (0x554 + (((nr) - 7) * 2))) |
|---|
| 120 | #define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \ |
|---|
| 121 | (0x555 + (((nr) - 7) * 2))) |
|---|
| 122 | #define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \ |
|---|
| 123 | (0x550 + (nr) - 7)) |
|---|
| 124 | |
|---|
| 125 | static const u16 W83627EHF_REG_TEMP[] = { 0x27, 0x150, 0x250 }; |
|---|
| 126 | static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x3a, 0x153, 0x253 }; |
|---|
| 127 | static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x39, 0x155, 0x255 }; |
|---|
| 128 | static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 }; |
|---|
| 129 | |
|---|
| 130 | /* Fan clock dividers are spread over the following five registers */ |
|---|
| 131 | #define W83627EHF_REG_FANDIV1 0x47 |
|---|
| 132 | #define W83627EHF_REG_FANDIV2 0x4B |
|---|
| 133 | #define W83627EHF_REG_VBAT 0x5D |
|---|
| 134 | #define W83627EHF_REG_DIODE 0x59 |
|---|
| 135 | #define W83627EHF_REG_SMI_OVT 0x4C |
|---|
| 136 | |
|---|
| 137 | #define W83627EHF_REG_ALARM1 0x459 |
|---|
| 138 | #define W83627EHF_REG_ALARM2 0x45A |
|---|
| 139 | #define W83627EHF_REG_ALARM3 0x45B |
|---|
| 140 | |
|---|
| 141 | /* SmartFan registers */ |
|---|
| 142 | /* DC or PWM output fan configuration */ |
|---|
| 143 | static const u8 W83627EHF_REG_PWM_ENABLE[] = { |
|---|
| 144 | 0x04, /* SYS FAN0 */ |
|---|
| 145 | 0x04, /* CPU FAN0 */ |
|---|
| 146 | 0x12, /* AUX FAN */ |
|---|
| 147 | 0x62, /* CPU FAN1 */ |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | #if 0 |
|---|
| 151 | static const u8 W83627EHF_PWM_MODE_SHIFT[4] = { 0, 1, 0, 6 }; |
|---|
| 152 | #endif |
|---|
| 153 | static const u8 W83627EHF_PWM_ENABLE_SHIFT[4] = { 2, 4, 1, 4 }; |
|---|
| 154 | |
|---|
| 155 | /* FAN Duty Cycle, be used to control */ |
|---|
| 156 | static const u8 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 }; |
|---|
| 157 | |
|---|
| 158 | /* |
|---|
| 159 | * Conversions |
|---|
| 160 | */ |
|---|
| 161 | |
|---|
| 162 | static inline unsigned int fan_from_reg(u8 reg, unsigned int div) |
|---|
| 163 | { |
|---|
| 164 | if (reg == 0 || reg == 255) |
|---|
| 165 | return 0; |
|---|
| 166 | return 1350000U / (reg * div); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | static inline unsigned int div_from_reg(u8 reg) |
|---|
| 170 | { |
|---|
| 171 | return 1 << reg; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */ |
|---|
| 175 | static const u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 }; |
|---|
| 176 | |
|---|
| 177 | static inline long in_from_reg(u8 reg, u8 nr) |
|---|
| 178 | { |
|---|
| 179 | return reg * scale_in[nr]; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | static inline u8 in_to_reg(long val, u8 nr) |
|---|
| 183 | { |
|---|
| 184 | return SENSORS_LIMIT((val + scale_in[nr] / 2) / scale_in[nr], 0, 255); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /* |
|---|
| 188 | * Data structures and manipulation thereof |
|---|
| 189 | */ |
|---|
| 190 | |
|---|
| 191 | struct w83627ehf_data { |
|---|
| 192 | struct i2c_client client; |
|---|
| 193 | struct class_device *class_dev; |
|---|
| 194 | int sysctl_id; |
|---|
| 195 | struct semaphore lock; |
|---|
| 196 | struct semaphore update_lock; |
|---|
| 197 | char valid; /* !=0 if following fields are valid */ |
|---|
| 198 | unsigned long last_updated; /* In jiffies */ |
|---|
| 199 | |
|---|
| 200 | /* Register values */ |
|---|
| 201 | u8 in[10]; /* Register value */ |
|---|
| 202 | u8 in_max[10]; /* Register value */ |
|---|
| 203 | u8 in_min[10]; /* Register value */ |
|---|
| 204 | u8 fan[5]; |
|---|
| 205 | u8 fan_min[5]; |
|---|
| 206 | u8 fan_div[5]; |
|---|
| 207 | u8 has_fan; /* some fan inputs can be disabled */ |
|---|
| 208 | u16 temp[3]; |
|---|
| 209 | u16 temp_max[3]; |
|---|
| 210 | u16 temp_hyst[3]; |
|---|
| 211 | u32 alarms; |
|---|
| 212 | |
|---|
| 213 | u8 pwm_enable[4]; /* 1 for manual, 2+ for auto */ |
|---|
| 214 | u8 pwm[4]; |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | /* The /proc/sys entries */ |
|---|
| 218 | /* -- SENSORS SYSCTL START -- */ |
|---|
| 219 | |
|---|
| 220 | #define W83627EHF_SYSCTL_IN0 1000 /* Volts * 1000 */ |
|---|
| 221 | #define W83627EHF_SYSCTL_IN1 1001 |
|---|
| 222 | #define W83627EHF_SYSCTL_IN2 1002 |
|---|
| 223 | #define W83627EHF_SYSCTL_IN3 1003 |
|---|
| 224 | #define W83627EHF_SYSCTL_IN4 1004 |
|---|
| 225 | #define W83627EHF_SYSCTL_IN5 1005 |
|---|
| 226 | #define W83627EHF_SYSCTL_IN6 1006 |
|---|
| 227 | #define W83627EHF_SYSCTL_IN7 1007 |
|---|
| 228 | #define W83627EHF_SYSCTL_IN8 1008 |
|---|
| 229 | #define W83627EHF_SYSCTL_IN9 1009 |
|---|
| 230 | #define W83627EHF_SYSCTL_FAN1 1101 /* Rotations/min */ |
|---|
| 231 | #define W83627EHF_SYSCTL_FAN2 1102 |
|---|
| 232 | #define W83627EHF_SYSCTL_FAN3 1103 |
|---|
| 233 | #define W83627EHF_SYSCTL_FAN4 1104 |
|---|
| 234 | #define W83627EHF_SYSCTL_FAN5 1105 |
|---|
| 235 | #define W83627EHF_SYSCTL_TEMP1 1201 /* Degrees Celsius * 10 */ |
|---|
| 236 | #define W83627EHF_SYSCTL_TEMP2 1202 |
|---|
| 237 | #define W83627EHF_SYSCTL_TEMP3 1203 |
|---|
| 238 | #define W83627EHF_SYSCTL_PWM1 1401 |
|---|
| 239 | #define W83627EHF_SYSCTL_PWM2 1402 |
|---|
| 240 | #define W83627EHF_SYSCTL_PWM3 1403 |
|---|
| 241 | #define W83627EHF_SYSCTL_PWM4 1404 |
|---|
| 242 | #define W83627EHF_SYSCTL_FAN_DIV 1506 |
|---|
| 243 | #define W83627EHF_SYSCTL_ALARMS 1507 /* bitvector */ |
|---|
| 244 | |
|---|
| 245 | #define W83627EHF_ALARM_IN0 (1 << 0) |
|---|
| 246 | #define W83627EHF_ALARM_IN1 (1 << 1) |
|---|
| 247 | #define W83627EHF_ALARM_IN2 (1 << 2) |
|---|
| 248 | #define W83627EHF_ALARM_IN3 (1 << 3) |
|---|
| 249 | #define W83627EHF_ALARM_IN4 (1 << 8) |
|---|
| 250 | #define W83627EHF_ALARM_IN5 (1 << 21) |
|---|
| 251 | #define W83627EHF_ALARM_IN6 (1 << 20) |
|---|
| 252 | #define W83627EHF_ALARM_IN7 (1 << 16) |
|---|
| 253 | #define W83627EHF_ALARM_IN8 (1 << 17) |
|---|
| 254 | #define W83627EHF_ALARM_IN9 (1 << 19) |
|---|
| 255 | #define W83627EHF_ALARM_TEMP1 (1 << 4) |
|---|
| 256 | #define W83627EHF_ALARM_TEMP2 (1 << 5) |
|---|
| 257 | #define W83627EHF_ALARM_TEMP3 (1 << 13) |
|---|
| 258 | #define W83627EHF_ALARM_FAN1 (1 << 6) |
|---|
| 259 | #define W83627EHF_ALARM_FAN2 (1 << 7) |
|---|
| 260 | #define W83627EHF_ALARM_FAN3 (1 << 11) |
|---|
| 261 | #define W83627EHF_ALARM_FAN4 (1 << 10) |
|---|
| 262 | #define W83627EHF_ALARM_FAN5 (1 << 23) |
|---|
| 263 | |
|---|
| 264 | /* -- SENSORS SYSCTL END -- */ |
|---|
| 265 | |
|---|
| 266 | static inline int is_word_sized(u16 reg) |
|---|
| 267 | { |
|---|
| 268 | return (((reg & 0xff00) == 0x100 || (reg & 0xff00) == 0x200) |
|---|
| 269 | && ((reg & 0x00ff) == 0x50 || (reg & 0x00ff) == 0x53 || |
|---|
| 270 | (reg & 0x00ff) == 0x55)); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /* We assume that the default bank is 0, thus the following two functions do |
|---|
| 274 | nothing for registers which live in bank 0. For others, they respectively |
|---|
| 275 | set the bank register to the correct value (before the register is |
|---|
| 276 | accessed), and back to 0 (afterwards). */ |
|---|
| 277 | static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg) |
|---|
| 278 | { |
|---|
| 279 | if (reg & 0xff00) { |
|---|
| 280 | outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET); |
|---|
| 281 | outb_p(reg >> 8, client->addr + DATA_REG_OFFSET); |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg) |
|---|
| 286 | { |
|---|
| 287 | if (reg & 0xff00) { |
|---|
| 288 | outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET); |
|---|
| 289 | outb_p(0, client->addr + DATA_REG_OFFSET); |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg) |
|---|
| 294 | { |
|---|
| 295 | struct w83627ehf_data *data = client->data; |
|---|
| 296 | int res, word_sized = is_word_sized(reg); |
|---|
| 297 | |
|---|
| 298 | down(&data->lock); |
|---|
| 299 | |
|---|
| 300 | w83627ehf_set_bank(client, reg); |
|---|
| 301 | outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET); |
|---|
| 302 | res = inb_p(client->addr + DATA_REG_OFFSET); |
|---|
| 303 | if (word_sized) { |
|---|
| 304 | outb_p((reg & 0xff) + 1, client->addr + ADDR_REG_OFFSET); |
|---|
| 305 | res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET); |
|---|
| 306 | } |
|---|
| 307 | w83627ehf_reset_bank(client, reg); |
|---|
| 308 | |
|---|
| 309 | up(&data->lock); |
|---|
| 310 | return res; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value) |
|---|
| 314 | { |
|---|
| 315 | struct w83627ehf_data *data = client->data; |
|---|
| 316 | int word_sized = is_word_sized(reg); |
|---|
| 317 | |
|---|
| 318 | down(&data->lock); |
|---|
| 319 | |
|---|
| 320 | w83627ehf_set_bank(client, reg); |
|---|
| 321 | outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET); |
|---|
| 322 | if (word_sized) { |
|---|
| 323 | outb_p(value >> 8, client->addr + DATA_REG_OFFSET); |
|---|
| 324 | outb_p((reg & 0xff) + 1, client->addr + ADDR_REG_OFFSET); |
|---|
| 325 | } |
|---|
| 326 | outb_p(value & 0xff, client->addr + DATA_REG_OFFSET); |
|---|
| 327 | w83627ehf_reset_bank(client, reg); |
|---|
| 328 | |
|---|
| 329 | up(&data->lock); |
|---|
| 330 | return 0; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | /* This function assumes that the caller holds data->update_lock */ |
|---|
| 334 | static void w83627ehf_write_fan_div(struct i2c_client *client, int nr) |
|---|
| 335 | { |
|---|
| 336 | struct w83627ehf_data *data = client->data; |
|---|
| 337 | u8 reg; |
|---|
| 338 | |
|---|
| 339 | switch (nr) { |
|---|
| 340 | case 0: |
|---|
| 341 | reg = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) |
|---|
| 342 | & 0xcf; |
|---|
| 343 | reg |= (data->fan_div[0] & 0x03) << 4; |
|---|
| 344 | /* fan5 input control bit is write only, compute the value */ |
|---|
| 345 | reg |= (data->has_fan & (1 << 4)) ? 1 : 0; |
|---|
| 346 | w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg); |
|---|
| 347 | reg = w83627ehf_read_value(client, W83627EHF_REG_VBAT) |
|---|
| 348 | & 0xdf; |
|---|
| 349 | reg |= (data->fan_div[0] & 0x04) << 3; |
|---|
| 350 | w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg); |
|---|
| 351 | break; |
|---|
| 352 | case 1: |
|---|
| 353 | reg = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) |
|---|
| 354 | & 0x3f; |
|---|
| 355 | reg |= (data->fan_div[1] & 0x03) << 6; |
|---|
| 356 | /* fan5 input control bit is write only, compute the value */ |
|---|
| 357 | reg |= (data->has_fan & (1 << 4)) ? 1 : 0; |
|---|
| 358 | w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg); |
|---|
| 359 | reg = w83627ehf_read_value(client, W83627EHF_REG_VBAT) |
|---|
| 360 | & 0xbf; |
|---|
| 361 | reg |= (data->fan_div[1] & 0x04) << 4; |
|---|
| 362 | w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg); |
|---|
| 363 | break; |
|---|
| 364 | case 2: |
|---|
| 365 | reg = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) |
|---|
| 366 | & 0x3f; |
|---|
| 367 | reg |= (data->fan_div[2] & 0x03) << 6; |
|---|
| 368 | w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg); |
|---|
| 369 | reg = w83627ehf_read_value(client, W83627EHF_REG_VBAT) |
|---|
| 370 | & 0x7f; |
|---|
| 371 | reg |= (data->fan_div[2] & 0x04) << 5; |
|---|
| 372 | w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg); |
|---|
| 373 | break; |
|---|
| 374 | case 3: |
|---|
| 375 | reg = w83627ehf_read_value(client, W83627EHF_REG_DIODE) |
|---|
| 376 | & 0xfc; |
|---|
| 377 | reg |= data->fan_div[3] & 0x03; |
|---|
| 378 | w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg); |
|---|
| 379 | reg = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) |
|---|
| 380 | & 0x7f; |
|---|
| 381 | reg |= (data->fan_div[3] & 0x04) << 5; |
|---|
| 382 | w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg); |
|---|
| 383 | break; |
|---|
| 384 | case 4: |
|---|
| 385 | reg = w83627ehf_read_value(client, W83627EHF_REG_DIODE) |
|---|
| 386 | & 0x73; |
|---|
| 387 | reg |= (data->fan_div[4] & 0x03) << 2; |
|---|
| 388 | reg |= (data->fan_div[4] & 0x04) << 5; |
|---|
| 389 | w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg); |
|---|
| 390 | break; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | static void w83627ehf_update_client(struct i2c_client *client) |
|---|
| 395 | { |
|---|
| 396 | struct w83627ehf_data *data = client->data; |
|---|
| 397 | int pwmcfg = 0; /* shut up the compiler */ |
|---|
| 398 | int i; |
|---|
| 399 | |
|---|
| 400 | down(&data->update_lock); |
|---|
| 401 | if (time_after(jiffies, data->last_updated + HZ) |
|---|
| 402 | || !data->valid) { |
|---|
| 403 | /* Fan clock dividers */ |
|---|
| 404 | i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1); |
|---|
| 405 | data->fan_div[0] = (i >> 4) & 0x03; |
|---|
| 406 | data->fan_div[1] = (i >> 6) & 0x03; |
|---|
| 407 | i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2); |
|---|
| 408 | data->fan_div[2] = (i >> 6) & 0x03; |
|---|
| 409 | i = w83627ehf_read_value(client, W83627EHF_REG_VBAT); |
|---|
| 410 | data->fan_div[0] |= (i >> 3) & 0x04; |
|---|
| 411 | data->fan_div[1] |= (i >> 4) & 0x04; |
|---|
| 412 | data->fan_div[2] |= (i >> 5) & 0x04; |
|---|
| 413 | if (data->has_fan & ((1 << 3) | (1 << 4))) { |
|---|
| 414 | i = w83627ehf_read_value(client, W83627EHF_REG_DIODE); |
|---|
| 415 | data->fan_div[3] = i & 0x03; |
|---|
| 416 | data->fan_div[4] = ((i >> 2) & 0x03) |
|---|
| 417 | | ((i >> 5) & 0x04); |
|---|
| 418 | } |
|---|
| 419 | if (data->has_fan & (1 << 3)) { |
|---|
| 420 | i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT); |
|---|
| 421 | data->fan_div[3] |= (i >> 5) & 0x04; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | /* Measured voltages and limits */ |
|---|
| 425 | for (i = 0; i < 10; i++) { |
|---|
| 426 | data->in[i] = w83627ehf_read_value(client, |
|---|
| 427 | W83627EHF_REG_IN(i)); |
|---|
| 428 | data->in_min[i] = w83627ehf_read_value(client, |
|---|
| 429 | W83627EHF_REG_IN_MIN(i)); |
|---|
| 430 | data->in_max[i] = w83627ehf_read_value(client, |
|---|
| 431 | W83627EHF_REG_IN_MAX(i)); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | /* Measured fan speeds and limits */ |
|---|
| 435 | for (i = 0; i < 5; i++) { |
|---|
| 436 | if (!(data->has_fan & (1 << i))) |
|---|
| 437 | continue; |
|---|
| 438 | |
|---|
| 439 | data->fan[i] = w83627ehf_read_value(client, |
|---|
| 440 | W83627EHF_REG_FAN[i]); |
|---|
| 441 | data->fan_min[i] = w83627ehf_read_value(client, |
|---|
| 442 | W83627EHF_REG_FAN_MIN[i]); |
|---|
| 443 | |
|---|
| 444 | /* If we failed to measure the fan speed and clock |
|---|
| 445 | divider can be increased, let's try that for next |
|---|
| 446 | time */ |
|---|
| 447 | if (data->fan[i] == 0xff |
|---|
| 448 | && data->fan_div[i] < 0x07) { |
|---|
| 449 | #ifdef DEBUG |
|---|
| 450 | printk(KERN_DEBUG "w83627ehf: Increasing " |
|---|
| 451 | "fan%d clock divider from %u to %u\n", |
|---|
| 452 | i + 1, div_from_reg(data->fan_div[i]), |
|---|
| 453 | div_from_reg(data->fan_div[i] + 1)); |
|---|
| 454 | #endif |
|---|
| 455 | data->fan_div[i]++; |
|---|
| 456 | w83627ehf_write_fan_div(client, i); |
|---|
| 457 | /* Preserve min limit if possible */ |
|---|
| 458 | if (data->fan_min[i] >= 2 |
|---|
| 459 | && data->fan_min[i] != 255) { |
|---|
| 460 | w83627ehf_write_value(client, |
|---|
| 461 | W83627EHF_REG_FAN_MIN[i], |
|---|
| 462 | (data->fan_min[i] /= 2)); |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | for (i = 0; i < 4; i++) { |
|---|
| 468 | /* pwmcfg mapped for i=0, i=1 to same reg */ |
|---|
| 469 | if (i != 1) |
|---|
| 470 | pwmcfg = w83627ehf_read_value(client, |
|---|
| 471 | W83627EHF_REG_PWM_ENABLE[i]); |
|---|
| 472 | |
|---|
| 473 | data->pwm_enable[i] = 1 + |
|---|
| 474 | ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i]) & 3); |
|---|
| 475 | data->pwm[i] = w83627ehf_read_value(client, |
|---|
| 476 | W83627EHF_REG_PWM[i]); |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | /* Measured temperatures and limits */ |
|---|
| 480 | for (i = 0; i < 3; i++) { |
|---|
| 481 | data->temp[i] = w83627ehf_read_value(client, |
|---|
| 482 | W83627EHF_REG_TEMP[i]); |
|---|
| 483 | data->temp_max[i] = w83627ehf_read_value(client, |
|---|
| 484 | W83627EHF_REG_TEMP_OVER[i]); |
|---|
| 485 | data->temp_hyst[i] = w83627ehf_read_value(client, |
|---|
| 486 | W83627EHF_REG_TEMP_HYST[i]); |
|---|
| 487 | } |
|---|
| 488 | /* temp1 is 8-bit, align it */ |
|---|
| 489 | data->temp[0] <<= 8; |
|---|
| 490 | data->temp_max[0] <<= 8; |
|---|
| 491 | data->temp_hyst[0] <<= 8; |
|---|
| 492 | |
|---|
| 493 | data->alarms = w83627ehf_read_value(client, |
|---|
| 494 | W83627EHF_REG_ALARM1) | |
|---|
| 495 | (w83627ehf_read_value(client, |
|---|
| 496 | W83627EHF_REG_ALARM2) << 8) | |
|---|
| 497 | (w83627ehf_read_value(client, W83627EHF_REG_ALARM3) << 16); |
|---|
| 498 | |
|---|
| 499 | data->last_updated = jiffies; |
|---|
| 500 | data->valid = 1; |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | up(&data->update_lock); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | static void w83627ehf_in(struct i2c_client *client, int operation, int ctl_name, |
|---|
| 507 | int *nrels_mag, long *results) |
|---|
| 508 | { |
|---|
| 509 | struct w83627ehf_data *data = client->data; |
|---|
| 510 | int nr = ctl_name - W83627EHF_SYSCTL_IN0; |
|---|
| 511 | |
|---|
| 512 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 513 | *nrels_mag = 3; |
|---|
| 514 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 515 | w83627ehf_update_client(client); |
|---|
| 516 | results[0] = in_from_reg(data->in_min[nr], nr); |
|---|
| 517 | results[1] = in_from_reg(data->in_max[nr], nr); |
|---|
| 518 | results[2] = in_from_reg(data->in[nr], nr); |
|---|
| 519 | *nrels_mag = 3; |
|---|
| 520 | } else if (operation == SENSORS_PROC_REAL_WRITE) { |
|---|
| 521 | down(&data->update_lock); |
|---|
| 522 | if (*nrels_mag >= 1) { |
|---|
| 523 | /* Write low limit into register */ |
|---|
| 524 | data->in_min[nr] = in_to_reg(results[0], nr); |
|---|
| 525 | w83627ehf_write_value(client, W83627EHF_REG_IN_MIN(nr), |
|---|
| 526 | data->in_min[nr]); |
|---|
| 527 | } |
|---|
| 528 | if (*nrels_mag >= 2) { |
|---|
| 529 | /* Write high limit into register */ |
|---|
| 530 | data->in_max[nr] = in_to_reg(results[1], nr); |
|---|
| 531 | w83627ehf_write_value(client, W83627EHF_REG_IN_MAX(nr), |
|---|
| 532 | data->in_max[nr]); |
|---|
| 533 | } |
|---|
| 534 | up(&data->update_lock); |
|---|
| 535 | } |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | static void store_fan_min(struct i2c_client *client, long val, int nr) |
|---|
| 539 | { |
|---|
| 540 | struct w83627ehf_data *data = client->data; |
|---|
| 541 | unsigned int reg; |
|---|
| 542 | u8 new_div; |
|---|
| 543 | |
|---|
| 544 | down(&data->update_lock); |
|---|
| 545 | if (val <= 0) { |
|---|
| 546 | /* No min limit, alarm disabled */ |
|---|
| 547 | data->fan_min[nr] = 255; |
|---|
| 548 | new_div = data->fan_div[nr]; /* No change */ |
|---|
| 549 | printk(KERN_INFO "w83627ehf: fan%u low limit and alarm " |
|---|
| 550 | "disabled\n", nr + 1); |
|---|
| 551 | } else if ((reg = 1350000U / val) >= 128 * 255) { |
|---|
| 552 | /* Speed below this value cannot possibly be represented, |
|---|
| 553 | even with the highest divider (128) */ |
|---|
| 554 | data->fan_min[nr] = 254; |
|---|
| 555 | new_div = 7; /* 128 == (1 << 7) */ |
|---|
| 556 | printk(KERN_WARNING "w83627ehf: fan%u low limit %lu below " |
|---|
| 557 | "minimum %u, set to minimum\n", nr + 1, val, |
|---|
| 558 | fan_from_reg(254, 128)); |
|---|
| 559 | } else if (!reg) { |
|---|
| 560 | /* Speed above this value cannot possibly be represented, |
|---|
| 561 | even with the lowest divider (1) */ |
|---|
| 562 | data->fan_min[nr] = 1; |
|---|
| 563 | new_div = 0; /* 1 == (1 << 0) */ |
|---|
| 564 | printk(KERN_WARNING "w83627ehf: fan%u low limit %lu above " |
|---|
| 565 | "maximum %u, set to maximum\n", nr + 1, val, |
|---|
| 566 | fan_from_reg(1, 1)); |
|---|
| 567 | } else { |
|---|
| 568 | /* Automatically pick the best divider, i.e. the one such |
|---|
| 569 | that the min limit will correspond to a register value |
|---|
| 570 | in the 96..192 range */ |
|---|
| 571 | new_div = 0; |
|---|
| 572 | while (reg > 192 && new_div < 7) { |
|---|
| 573 | reg >>= 1; |
|---|
| 574 | new_div++; |
|---|
| 575 | } |
|---|
| 576 | data->fan_min[nr] = reg; |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | /* Write both the fan clock divider (if it changed) and the new |
|---|
| 580 | fan min (unconditionally) */ |
|---|
| 581 | if (new_div != data->fan_div[nr]) { |
|---|
| 582 | /* Preserve the fan speed reading */ |
|---|
| 583 | if (new_div > data->fan_div[nr]) |
|---|
| 584 | data->fan[nr] >>= new_div - data->fan_div[nr]; |
|---|
| 585 | else |
|---|
| 586 | data->fan[nr] <<= data->fan_div[nr] - new_div; |
|---|
| 587 | |
|---|
| 588 | #ifdef DEBUG |
|---|
| 589 | printk(KERN_DEBUG "w83627ehf: fan%u clock divider changed " |
|---|
| 590 | "from %u to %u\n", nr + 1, |
|---|
| 591 | div_from_reg(data->fan_div[nr]), |
|---|
| 592 | div_from_reg(new_div)); |
|---|
| 593 | #endif |
|---|
| 594 | data->fan_div[nr] = new_div; |
|---|
| 595 | w83627ehf_write_fan_div(client, nr); |
|---|
| 596 | } |
|---|
| 597 | w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr], |
|---|
| 598 | data->fan_min[nr]); |
|---|
| 599 | up(&data->update_lock); |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | static void w83627ehf_fan(struct i2c_client *client, int operation, |
|---|
| 603 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 604 | { |
|---|
| 605 | struct w83627ehf_data *data = client->data; |
|---|
| 606 | int nr = ctl_name - W83627EHF_SYSCTL_FAN1; |
|---|
| 607 | |
|---|
| 608 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 609 | *nrels_mag = 0; |
|---|
| 610 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 611 | w83627ehf_update_client(client); |
|---|
| 612 | if (data->has_fan & (1 << nr)) { |
|---|
| 613 | results[0] = fan_from_reg(data->fan_min[nr], |
|---|
| 614 | div_from_reg(data->fan_div[nr])); |
|---|
| 615 | results[1] = fan_from_reg(data->fan[nr], |
|---|
| 616 | div_from_reg(data->fan_div[nr])); |
|---|
| 617 | } else { |
|---|
| 618 | results[0] = 0; |
|---|
| 619 | results[1] = 0; |
|---|
| 620 | } |
|---|
| 621 | *nrels_mag = 2; |
|---|
| 622 | } else if (operation == SENSORS_PROC_REAL_WRITE) { |
|---|
| 623 | if ((data->has_fan & (1 << nr)) |
|---|
| 624 | && (*nrels_mag >= 1)) { |
|---|
| 625 | store_fan_min(client, results[0], nr); |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | static void w83627ehf_temp(struct i2c_client *client, int operation, |
|---|
| 631 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 632 | { |
|---|
| 633 | struct w83627ehf_data *data = client->data; |
|---|
| 634 | int nr = ctl_name - W83627EHF_SYSCTL_TEMP1; |
|---|
| 635 | |
|---|
| 636 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 637 | *nrels_mag = 1; |
|---|
| 638 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 639 | w83627ehf_update_client(client); |
|---|
| 640 | results[0] = LM75_TEMP_FROM_REG(data->temp_max[nr]); |
|---|
| 641 | results[1] = LM75_TEMP_FROM_REG(data->temp_hyst[nr]); |
|---|
| 642 | results[2] = LM75_TEMP_FROM_REG(data->temp[nr]); |
|---|
| 643 | *nrels_mag = 3; |
|---|
| 644 | } else if (operation == SENSORS_PROC_REAL_WRITE) { |
|---|
| 645 | down(&data->update_lock); |
|---|
| 646 | if (*nrels_mag >= 1) { |
|---|
| 647 | data->temp_max[nr] = LM75_TEMP_TO_REG(results[0]); |
|---|
| 648 | w83627ehf_write_value(client, |
|---|
| 649 | W83627EHF_REG_TEMP_OVER[nr], |
|---|
| 650 | nr ? data->temp_max[nr] : |
|---|
| 651 | data->temp_max[nr] >> 8); |
|---|
| 652 | } |
|---|
| 653 | if (*nrels_mag >= 2) { |
|---|
| 654 | data->temp_hyst[nr] = LM75_TEMP_TO_REG(results[1]); |
|---|
| 655 | w83627ehf_write_value(client, |
|---|
| 656 | W83627EHF_REG_TEMP_HYST[nr], |
|---|
| 657 | nr ? data->temp_hyst[nr] : |
|---|
| 658 | data->temp_hyst[nr] >> 8); |
|---|
| 659 | } |
|---|
| 660 | up(&data->update_lock); |
|---|
| 661 | } |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | static void w83627ehf_alarms(struct i2c_client *client, int operation, |
|---|
| 665 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 666 | { |
|---|
| 667 | struct w83627ehf_data *data = client->data; |
|---|
| 668 | |
|---|
| 669 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 670 | *nrels_mag = 0; |
|---|
| 671 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 672 | w83627ehf_update_client(client); |
|---|
| 673 | results[0] = data->alarms; |
|---|
| 674 | *nrels_mag = 1; |
|---|
| 675 | } |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | static void w83627ehf_fan_div(struct i2c_client *client, int operation, |
|---|
| 679 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 680 | { |
|---|
| 681 | struct w83627ehf_data *data = client->data; |
|---|
| 682 | int i; |
|---|
| 683 | |
|---|
| 684 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 685 | *nrels_mag = 0; |
|---|
| 686 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 687 | w83627ehf_update_client(client); |
|---|
| 688 | for (i = 0; i < 5; i++) |
|---|
| 689 | results[i] = div_from_reg(data->fan_div[i]); |
|---|
| 690 | *nrels_mag = 5; |
|---|
| 691 | } |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | /* This function assumes that the caller holds data->update_lock */ |
|---|
| 695 | static void store_pwm_enable(struct i2c_client *client, long val, int nr) |
|---|
| 696 | { |
|---|
| 697 | struct w83627ehf_data *data = client->data; |
|---|
| 698 | u16 reg; |
|---|
| 699 | long modemax; |
|---|
| 700 | |
|---|
| 701 | /* Only pwm2 and pwm4 support Smart Fan III */ |
|---|
| 702 | modemax = (nr == 1 || nr == 3) ? 4 : 3; |
|---|
| 703 | if (val < 1 || val > modemax) |
|---|
| 704 | return; |
|---|
| 705 | |
|---|
| 706 | data->pwm_enable[nr] = val; |
|---|
| 707 | reg = w83627ehf_read_value(client, W83627EHF_REG_PWM_ENABLE[nr]); |
|---|
| 708 | reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]); |
|---|
| 709 | reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr]; |
|---|
| 710 | w83627ehf_write_value(client, W83627EHF_REG_PWM_ENABLE[nr], reg); |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | static void w83627ehf_pwm(struct i2c_client *client, int operation, |
|---|
| 714 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 715 | { |
|---|
| 716 | struct w83627ehf_data *data = client->data; |
|---|
| 717 | int nr = ctl_name - W83627EHF_SYSCTL_PWM1; |
|---|
| 718 | |
|---|
| 719 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 720 | *nrels_mag = 0; |
|---|
| 721 | else if (operation == SENSORS_PROC_REAL_READ) { |
|---|
| 722 | w83627ehf_update_client(client); |
|---|
| 723 | results[0] = data->pwm[nr]; |
|---|
| 724 | results[1] = data->pwm_enable[nr]; |
|---|
| 725 | *nrels_mag = 2; |
|---|
| 726 | } else if (operation == SENSORS_PROC_REAL_WRITE) { |
|---|
| 727 | down(&data->update_lock); |
|---|
| 728 | if (*nrels_mag >= 1) { |
|---|
| 729 | data->pwm[nr] = SENSORS_LIMIT(results[0], 0, 255); |
|---|
| 730 | w83627ehf_write_value(client, W83627EHF_REG_PWM[nr], |
|---|
| 731 | data->pwm[nr]); |
|---|
| 732 | } |
|---|
| 733 | if (*nrels_mag >= 2) { |
|---|
| 734 | store_pwm_enable(client, results[1], nr); |
|---|
| 735 | } |
|---|
| 736 | up(&data->update_lock); |
|---|
| 737 | } |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | static ctl_table w83627ehf_dir_table_template[] = { |
|---|
| 741 | {W83627EHF_SYSCTL_IN0, "in0", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 742 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 743 | {W83627EHF_SYSCTL_IN1, "in1", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 744 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 745 | {W83627EHF_SYSCTL_IN2, "in2", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 746 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 747 | {W83627EHF_SYSCTL_IN3, "in3", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 748 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 749 | {W83627EHF_SYSCTL_IN4, "in4", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 750 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 751 | {W83627EHF_SYSCTL_IN5, "in5", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 752 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 753 | {W83627EHF_SYSCTL_IN6, "in6", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 754 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 755 | {W83627EHF_SYSCTL_IN7, "in7", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 756 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 757 | {W83627EHF_SYSCTL_IN8, "in8", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 758 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 759 | {W83627EHF_SYSCTL_IN9, "in9", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 760 | &i2c_sysctl_real, NULL, &w83627ehf_in}, |
|---|
| 761 | |
|---|
| 762 | {W83627EHF_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 763 | &i2c_sysctl_real, NULL, &w83627ehf_fan}, |
|---|
| 764 | {W83627EHF_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 765 | &i2c_sysctl_real, NULL, &w83627ehf_fan}, |
|---|
| 766 | {W83627EHF_SYSCTL_FAN3, "fan3", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 767 | &i2c_sysctl_real, NULL, &w83627ehf_fan}, |
|---|
| 768 | {W83627EHF_SYSCTL_FAN4, "fan4", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 769 | &i2c_sysctl_real, NULL, &w83627ehf_fan}, |
|---|
| 770 | {W83627EHF_SYSCTL_FAN5, "fan5", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 771 | &i2c_sysctl_real, NULL, &w83627ehf_fan}, |
|---|
| 772 | |
|---|
| 773 | {W83627EHF_SYSCTL_TEMP1, "temp1", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 774 | &i2c_sysctl_real, NULL, &w83627ehf_temp}, |
|---|
| 775 | {W83627EHF_SYSCTL_TEMP2, "temp2", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 776 | &i2c_sysctl_real, NULL, &w83627ehf_temp}, |
|---|
| 777 | {W83627EHF_SYSCTL_TEMP3, "temp3", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 778 | &i2c_sysctl_real, NULL, &w83627ehf_temp}, |
|---|
| 779 | |
|---|
| 780 | /* manual adjust fan divisor not allowed */ |
|---|
| 781 | {W83627EHF_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0444, NULL, |
|---|
| 782 | &i2c_proc_real, &i2c_sysctl_real, NULL, &w83627ehf_fan_div}, |
|---|
| 783 | {W83627EHF_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real, |
|---|
| 784 | &i2c_sysctl_real, NULL, &w83627ehf_alarms}, |
|---|
| 785 | |
|---|
| 786 | {W83627EHF_SYSCTL_PWM1, "pwm1", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 787 | &i2c_sysctl_real, NULL, &w83627ehf_pwm}, |
|---|
| 788 | {W83627EHF_SYSCTL_PWM2, "pwm2", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 789 | &i2c_sysctl_real, NULL, &w83627ehf_pwm}, |
|---|
| 790 | {W83627EHF_SYSCTL_PWM3, "pwm3", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 791 | &i2c_sysctl_real, NULL, &w83627ehf_pwm}, |
|---|
| 792 | {W83627EHF_SYSCTL_PWM4, "pwm4", NULL, 0, 0644, NULL, &i2c_proc_real, |
|---|
| 793 | &i2c_sysctl_real, NULL, &w83627ehf_pwm}, |
|---|
| 794 | {0} |
|---|
| 795 | }; |
|---|
| 796 | |
|---|
| 797 | /* |
|---|
| 798 | * Driver and client management |
|---|
| 799 | */ |
|---|
| 800 | |
|---|
| 801 | static struct i2c_driver w83627ehf_driver; |
|---|
| 802 | |
|---|
| 803 | static void w83627ehf_init_client(struct i2c_client *client) |
|---|
| 804 | { |
|---|
| 805 | int i; |
|---|
| 806 | u8 tmp; |
|---|
| 807 | |
|---|
| 808 | /* Start monitoring if needed */ |
|---|
| 809 | tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG); |
|---|
| 810 | if (!(tmp & 0x01)) |
|---|
| 811 | w83627ehf_write_value(client, W83627EHF_REG_CONFIG, |
|---|
| 812 | tmp | 0x01); |
|---|
| 813 | |
|---|
| 814 | /* Enable temp2 and temp3 if needed */ |
|---|
| 815 | for (i = 0; i < 2; i++) { |
|---|
| 816 | tmp = w83627ehf_read_value(client, |
|---|
| 817 | W83627EHF_REG_TEMP_CONFIG[i]); |
|---|
| 818 | if (tmp & 0x01) |
|---|
| 819 | w83627ehf_write_value(client, |
|---|
| 820 | W83627EHF_REG_TEMP_CONFIG[i], |
|---|
| 821 | tmp & 0xfe); |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | } |
|---|
| 825 | |
|---|
| 826 | static int w83627ehf_detect(struct i2c_adapter *adapter, int address, |
|---|
| 827 | unsigned short flags, int kind) |
|---|
| 828 | { |
|---|
| 829 | struct i2c_client *client; |
|---|
| 830 | struct w83627ehf_data *data; |
|---|
| 831 | u8 fan4pin, fan5pin; |
|---|
| 832 | int i, err; |
|---|
| 833 | |
|---|
| 834 | if (!i2c_is_isa_adapter(adapter)) |
|---|
| 835 | return -ENODEV; |
|---|
| 836 | |
|---|
| 837 | if (!request_region(address + REGION_OFFSET, REGION_LENGTH, |
|---|
| 838 | "w83627ehf")) { |
|---|
| 839 | printk(KERN_ERR "w83627ehf: Region 0x%x-0x%x already in " |
|---|
| 840 | "use!\n", address + REGION_OFFSET, |
|---|
| 841 | address + REGION_OFFSET + REGION_LENGTH - 1); |
|---|
| 842 | err = -EBUSY; |
|---|
| 843 | goto exit; |
|---|
| 844 | } |
|---|
| 845 | |
|---|
| 846 | if (!(data = kmalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) { |
|---|
| 847 | err = -ENOMEM; |
|---|
| 848 | goto exit_release; |
|---|
| 849 | } |
|---|
| 850 | memset(data, 0, sizeof(struct w83627ehf_data)); |
|---|
| 851 | |
|---|
| 852 | client = &data->client; |
|---|
| 853 | client->addr = address; |
|---|
| 854 | client->data = data; |
|---|
| 855 | client->adapter = adapter; |
|---|
| 856 | client->driver = &w83627ehf_driver; |
|---|
| 857 | strcpy(client->name, "W83627EHF chip"); |
|---|
| 858 | init_MUTEX(&data->lock); |
|---|
| 859 | init_MUTEX(&data->update_lock); |
|---|
| 860 | |
|---|
| 861 | /* Tell the i2c layer a new client has arrived */ |
|---|
| 862 | if ((err = i2c_attach_client(client))) |
|---|
| 863 | goto exit_free; |
|---|
| 864 | |
|---|
| 865 | w83627ehf_init_client(client); |
|---|
| 866 | |
|---|
| 867 | /* A few vars need to be filled upon startup */ |
|---|
| 868 | for (i = 0; i < 5; i++) |
|---|
| 869 | data->fan_min[i] = w83627ehf_read_value(client, |
|---|
| 870 | W83627EHF_REG_FAN_MIN[i]); |
|---|
| 871 | |
|---|
| 872 | /* fan4 and fan5 share some pins with the GPIO and serial flash */ |
|---|
| 873 | superio_enter(); |
|---|
| 874 | fan5pin = superio_inb(0x24) & 0x2; |
|---|
| 875 | fan4pin = superio_inb(0x29) & 0x6; |
|---|
| 876 | superio_exit(); |
|---|
| 877 | |
|---|
| 878 | /* It looks like fan4 and fan5 pins can be alternatively used |
|---|
| 879 | as fan on/off switches, but fan5 control is write only :/ |
|---|
| 880 | We assume that if the serial interface is disabled, designers |
|---|
| 881 | connected fan5 as input unless they are emitting log 1, which |
|---|
| 882 | is not the default. */ |
|---|
| 883 | |
|---|
| 884 | data->has_fan = 0x07; /* fan1, fan2 and fan3 */ |
|---|
| 885 | i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1); |
|---|
| 886 | if ((i & (1 << 2)) && (!fan4pin)) |
|---|
| 887 | data->has_fan |= (1 << 3); |
|---|
| 888 | if (!(i & (1 << 1)) && (!fan5pin)) |
|---|
| 889 | data->has_fan |= (1 << 4); |
|---|
| 890 | |
|---|
| 891 | /* Register a new directory entry with module sensors */ |
|---|
| 892 | if ((i = i2c_register_entry(client, |
|---|
| 893 | "w83627ehf", |
|---|
| 894 | w83627ehf_dir_table_template, |
|---|
| 895 | THIS_MODULE)) < 0) { |
|---|
| 896 | err = i; |
|---|
| 897 | goto exit_detach; |
|---|
| 898 | } |
|---|
| 899 | data->sysctl_id = i; |
|---|
| 900 | |
|---|
| 901 | return 0; |
|---|
| 902 | |
|---|
| 903 | exit_detach: |
|---|
| 904 | i2c_detach_client(client); |
|---|
| 905 | exit_free: |
|---|
| 906 | kfree(data); |
|---|
| 907 | exit_release: |
|---|
| 908 | release_region(address + REGION_OFFSET, REGION_LENGTH); |
|---|
| 909 | exit: |
|---|
| 910 | return err; |
|---|
| 911 | |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | static int w83627ehf_attach_adapter(struct i2c_adapter *adapter) |
|---|
| 915 | { |
|---|
| 916 | return i2c_detect(adapter, &addr_data, w83627ehf_detect); |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | static int w83627ehf_detach_client(struct i2c_client *client) |
|---|
| 920 | { |
|---|
| 921 | struct w83627ehf_data *data = client->data; |
|---|
| 922 | int err; |
|---|
| 923 | |
|---|
| 924 | i2c_deregister_entry(data->sysctl_id); |
|---|
| 925 | |
|---|
| 926 | if ((err = i2c_detach_client(client))) { |
|---|
| 927 | printk(KERN_ERR "w83627ehf: Client deregistration failed\n"); |
|---|
| 928 | return err; |
|---|
| 929 | } |
|---|
| 930 | |
|---|
| 931 | release_region(client->addr + REGION_OFFSET, REGION_LENGTH); |
|---|
| 932 | kfree(client->data); |
|---|
| 933 | |
|---|
| 934 | return 0; |
|---|
| 935 | } |
|---|
| 936 | |
|---|
| 937 | static struct i2c_driver w83627ehf_driver = { |
|---|
| 938 | .name = "w83627ehf", |
|---|
| 939 | .flags = I2C_DF_NOTIFY, |
|---|
| 940 | .attach_adapter = w83627ehf_attach_adapter, |
|---|
| 941 | .detach_client = w83627ehf_detach_client, |
|---|
| 942 | }; |
|---|
| 943 | |
|---|
| 944 | static int __init w83627ehf_find(int sioaddr, unsigned int *addr) |
|---|
| 945 | { |
|---|
| 946 | u16 val; |
|---|
| 947 | const char *devname; |
|---|
| 948 | |
|---|
| 949 | REG = sioaddr; |
|---|
| 950 | VAL = sioaddr + 1; |
|---|
| 951 | superio_enter(); |
|---|
| 952 | |
|---|
| 953 | val = (superio_inb(SIO_REG_DEVID) << 8) |
|---|
| 954 | | superio_inb(SIO_REG_DEVID + 1); |
|---|
| 955 | switch (val & SIO_ID_MASK) { |
|---|
| 956 | case SIO_W83627EHF_ID: |
|---|
| 957 | devname = "W83627EHF"; |
|---|
| 958 | break; |
|---|
| 959 | case SIO_W83627EHG_ID: |
|---|
| 960 | devname = "W83627EHG"; |
|---|
| 961 | break; |
|---|
| 962 | default: |
|---|
| 963 | superio_exit(); |
|---|
| 964 | return -ENODEV; |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | superio_select(W83627EHF_LD_HWM); |
|---|
| 968 | val = (superio_inb(SIO_REG_ADDR) << 8) |
|---|
| 969 | | superio_inb(SIO_REG_ADDR + 1); |
|---|
| 970 | *addr = val & REGION_ALIGNMENT; |
|---|
| 971 | if (*addr == 0) { |
|---|
| 972 | printk(KERN_ERR "w83627ehf: HWM I/O area not set\n"); |
|---|
| 973 | superio_exit(); |
|---|
| 974 | return -ENODEV; |
|---|
| 975 | } |
|---|
| 976 | |
|---|
| 977 | printk(KERN_INFO "w83627ehf: Found %s device at 0x%x\n", |
|---|
| 978 | devname, *addr); |
|---|
| 979 | |
|---|
| 980 | /* Activate logical device if needed */ |
|---|
| 981 | val = superio_inb(SIO_REG_ENABLE); |
|---|
| 982 | if (!(val & 0x01)) { |
|---|
| 983 | printk(KERN_WARNING "w83627ehf: HWM disabled, enabling\n"); |
|---|
| 984 | superio_outb(SIO_REG_ENABLE, val | 0x01); |
|---|
| 985 | } |
|---|
| 986 | |
|---|
| 987 | superio_exit(); |
|---|
| 988 | return 0; |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | static int __init sensors_w83627ehf_init(void) |
|---|
| 992 | { |
|---|
| 993 | printk(KERN_INFO "w83627ehf: Driver version %s (%s)\n", |
|---|
| 994 | LM_VERSION, LM_DATE); |
|---|
| 995 | if (w83627ehf_find(0x2e, &normal_isa[0]) |
|---|
| 996 | && w83627ehf_find(0x4e, &normal_isa[0])) |
|---|
| 997 | return -ENODEV; |
|---|
| 998 | |
|---|
| 999 | return i2c_add_driver(&w83627ehf_driver); |
|---|
| 1000 | } |
|---|
| 1001 | |
|---|
| 1002 | static void __exit sensors_w83627ehf_exit(void) |
|---|
| 1003 | { |
|---|
| 1004 | i2c_del_driver(&w83627ehf_driver); |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| 1007 | MODULE_AUTHOR("Yuan Mu (Winbond), Jean Delvare <khali@linux-fr.org>"); |
|---|
| 1008 | MODULE_DESCRIPTION("W83627EHF harware monitoring driver"); |
|---|
| 1009 | MODULE_LICENSE("GPL"); |
|---|
| 1010 | |
|---|
| 1011 | module_init(sensors_w83627ehf_init); |
|---|
| 1012 | module_exit(sensors_w83627ehf_exit); |
|---|