1 /*
2 * w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
5 * Philip Edelbrock <phil@netroedge.com>,
6 * and Mark Studebaker <mdsxyz123@yahoo.com>
7 * Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
8 * Copyright (c) 2007 - 1012 Jean Delvare <jdelvare@suse.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 /*
26 * Supports following chips:
27 *
28 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
29 * w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
30 * w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
31 * w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
32 * w83687thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
33 * w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
34 *
35 * For other winbond chips, and for i2c support in the above chips,
36 * use w83781d.c.
37 *
38 * Note: automatic ("cruise") fan control for 697, 637 & 627thf not
39 * supported yet.
40 */
41
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/jiffies.h>
48 #include <linux/platform_device.h>
49 #include <linux/hwmon.h>
50 #include <linux/hwmon-sysfs.h>
51 #include <linux/hwmon-vid.h>
52 #include <linux/err.h>
53 #include <linux/mutex.h>
54 #include <linux/ioport.h>
55 #include <linux/acpi.h>
56 #include <linux/io.h>
57 #include "lm75.h"
58
59 static struct platform_device *pdev;
60
61 #define DRVNAME "w83627hf"
62 enum chips { w83627hf, w83627thf, w83697hf, w83637hf, w83687thf };
63
64 struct w83627hf_sio_data {
65 enum chips type;
66 int sioaddr;
67 };
68
69 static u8 force_i2c = 0x1f;
70 module_param(force_i2c, byte, 0);
71 MODULE_PARM_DESC(force_i2c,
72 "Initialize the i2c address of the sensors");
73
74 static bool init = 1;
75 module_param(init, bool, 0);
76 MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
77
78 static unsigned short force_id;
79 module_param(force_id, ushort, 0);
80 MODULE_PARM_DESC(force_id, "Override the detected device ID");
81
82 /* modified from kernel/include/traps.c */
83 #define DEV 0x07 /* Register: Logical device select */
84
85 /* logical device numbers for superio_select (below) */
86 #define W83627HF_LD_FDC 0x00
87 #define W83627HF_LD_PRT 0x01
88 #define W83627HF_LD_UART1 0x02
89 #define W83627HF_LD_UART2 0x03
90 #define W83627HF_LD_KBC 0x05
91 #define W83627HF_LD_CIR 0x06 /* w83627hf only */
92 #define W83627HF_LD_GAME 0x07
93 #define W83627HF_LD_MIDI 0x07
94 #define W83627HF_LD_GPIO1 0x07
95 #define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
96 #define W83627HF_LD_GPIO2 0x08
97 #define W83627HF_LD_GPIO3 0x09
98 #define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
99 #define W83627HF_LD_ACPI 0x0a
100 #define W83627HF_LD_HWM 0x0b
101
102 #define DEVID 0x20 /* Register: Device ID */
103
104 #define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
105 #define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
106 #define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
107
108 #define W83687THF_VID_EN 0x29 /* w83687thf only */
109 #define W83687THF_VID_CFG 0xF0 /* w83687thf only */
110 #define W83687THF_VID_DATA 0xF1 /* w83687thf only */
111
112 static inline void
superio_outb(struct w83627hf_sio_data * sio,int reg,int val)113 superio_outb(struct w83627hf_sio_data *sio, int reg, int val)
114 {
115 outb(reg, sio->sioaddr);
116 outb(val, sio->sioaddr + 1);
117 }
118
119 static inline int
superio_inb(struct w83627hf_sio_data * sio,int reg)120 superio_inb(struct w83627hf_sio_data *sio, int reg)
121 {
122 outb(reg, sio->sioaddr);
123 return inb(sio->sioaddr + 1);
124 }
125
126 static inline void
superio_select(struct w83627hf_sio_data * sio,int ld)127 superio_select(struct w83627hf_sio_data *sio, int ld)
128 {
129 outb(DEV, sio->sioaddr);
130 outb(ld, sio->sioaddr + 1);
131 }
132
133 static inline int
superio_enter(struct w83627hf_sio_data * sio)134 superio_enter(struct w83627hf_sio_data *sio)
135 {
136 if (!request_muxed_region(sio->sioaddr, 2, DRVNAME))
137 return -EBUSY;
138
139 outb(0x87, sio->sioaddr);
140 outb(0x87, sio->sioaddr);
141
142 return 0;
143 }
144
145 static inline void
superio_exit(struct w83627hf_sio_data * sio)146 superio_exit(struct w83627hf_sio_data *sio)
147 {
148 outb(0xAA, sio->sioaddr);
149 release_region(sio->sioaddr, 2);
150 }
151
152 #define W627_DEVID 0x52
153 #define W627THF_DEVID 0x82
154 #define W697_DEVID 0x60
155 #define W637_DEVID 0x70
156 #define W687THF_DEVID 0x85
157 #define WINB_ACT_REG 0x30
158 #define WINB_BASE_REG 0x60
159 /* Constants specified below */
160
161 /* Alignment of the base address */
162 #define WINB_ALIGNMENT ~7
163
164 /* Offset & size of I/O region we are interested in */
165 #define WINB_REGION_OFFSET 5
166 #define WINB_REGION_SIZE 2
167
168 /* Where are the sensors address/data registers relative to the region offset */
169 #define W83781D_ADDR_REG_OFFSET 0
170 #define W83781D_DATA_REG_OFFSET 1
171
172 /* The W83781D registers */
173 /* The W83782D registers for nr=7,8 are in bank 5 */
174 #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
175 (0x554 + (((nr) - 7) * 2)))
176 #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
177 (0x555 + (((nr) - 7) * 2)))
178 #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
179 (0x550 + (nr) - 7))
180
181 /* nr:0-2 for fans:1-3 */
182 #define W83627HF_REG_FAN_MIN(nr) (0x3b + (nr))
183 #define W83627HF_REG_FAN(nr) (0x28 + (nr))
184
185 #define W83627HF_REG_TEMP2_CONFIG 0x152
186 #define W83627HF_REG_TEMP3_CONFIG 0x252
187 /* these are zero-based, unlike config constants above */
188 static const u16 w83627hf_reg_temp[] = { 0x27, 0x150, 0x250 };
189 static const u16 w83627hf_reg_temp_hyst[] = { 0x3A, 0x153, 0x253 };
190 static const u16 w83627hf_reg_temp_over[] = { 0x39, 0x155, 0x255 };
191
192 #define W83781D_REG_BANK 0x4E
193
194 #define W83781D_REG_CONFIG 0x40
195 #define W83781D_REG_ALARM1 0x459
196 #define W83781D_REG_ALARM2 0x45A
197 #define W83781D_REG_ALARM3 0x45B
198
199 #define W83781D_REG_BEEP_CONFIG 0x4D
200 #define W83781D_REG_BEEP_INTS1 0x56
201 #define W83781D_REG_BEEP_INTS2 0x57
202 #define W83781D_REG_BEEP_INTS3 0x453
203
204 #define W83781D_REG_VID_FANDIV 0x47
205
206 #define W83781D_REG_CHIPID 0x49
207 #define W83781D_REG_WCHIPID 0x58
208 #define W83781D_REG_CHIPMAN 0x4F
209 #define W83781D_REG_PIN 0x4B
210
211 #define W83781D_REG_VBAT 0x5D
212
213 #define W83627HF_REG_PWM1 0x5A
214 #define W83627HF_REG_PWM2 0x5B
215
216 static const u8 W83627THF_REG_PWM_ENABLE[] = {
217 0x04, /* FAN 1 mode */
218 0x04, /* FAN 2 mode */
219 0x12, /* FAN AUX mode */
220 };
221 static const u8 W83627THF_PWM_ENABLE_SHIFT[] = { 2, 4, 1 };
222
223 #define W83627THF_REG_PWM1 0x01 /* 697HF/637HF/687THF too */
224 #define W83627THF_REG_PWM2 0x03 /* 697HF/637HF/687THF too */
225 #define W83627THF_REG_PWM3 0x11 /* 637HF/687THF too */
226
227 #define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF/687THF too */
228
229 static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
230 static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
231 W83627THF_REG_PWM3 };
232 #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
233 regpwm_627hf[nr] : regpwm[nr])
234
235 #define W83627HF_REG_PWM_FREQ 0x5C /* Only for the 627HF */
236
237 #define W83637HF_REG_PWM_FREQ1 0x00 /* 697HF/687THF too */
238 #define W83637HF_REG_PWM_FREQ2 0x02 /* 697HF/687THF too */
239 #define W83637HF_REG_PWM_FREQ3 0x10 /* 687THF too */
240
241 static const u8 W83637HF_REG_PWM_FREQ[] = { W83637HF_REG_PWM_FREQ1,
242 W83637HF_REG_PWM_FREQ2,
243 W83637HF_REG_PWM_FREQ3 };
244
245 #define W83627HF_BASE_PWM_FREQ 46870
246
247 #define W83781D_REG_I2C_ADDR 0x48
248 #define W83781D_REG_I2C_SUBADDR 0x4A
249
250 /* Sensor selection */
251 #define W83781D_REG_SCFG1 0x5D
252 static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
253 #define W83781D_REG_SCFG2 0x59
254 static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
255 #define W83781D_DEFAULT_BETA 3435
256
257 /*
258 * Conversions. Limit checking is only done on the TO_REG
259 * variants. Note that you should be a bit careful with which arguments
260 * these macros are called: arguments may be evaluated more than once.
261 * Fixing this is just not worth it.
262 */
263 #define IN_TO_REG(val) (clamp_val((((val) + 8) / 16), 0, 255))
264 #define IN_FROM_REG(val) ((val) * 16)
265
FAN_TO_REG(long rpm,int div)266 static inline u8 FAN_TO_REG(long rpm, int div)
267 {
268 if (rpm == 0)
269 return 255;
270 rpm = clamp_val(rpm, 1, 1000000);
271 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
272 }
273
274 #define TEMP_MIN (-128000)
275 #define TEMP_MAX ( 127000)
276
277 /*
278 * TEMP: 0.001C/bit (-128C to +127C)
279 * REG: 1C/bit, two's complement
280 */
TEMP_TO_REG(long temp)281 static u8 TEMP_TO_REG(long temp)
282 {
283 int ntemp = clamp_val(temp, TEMP_MIN, TEMP_MAX);
284 ntemp += (ntemp < 0 ? -500 : 500);
285 return (u8)(ntemp / 1000);
286 }
287
TEMP_FROM_REG(u8 reg)288 static int TEMP_FROM_REG(u8 reg)
289 {
290 return (s8)reg * 1000;
291 }
292
293 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
294
295 #define PWM_TO_REG(val) (clamp_val((val), 0, 255))
296
pwm_freq_from_reg_627hf(u8 reg)297 static inline unsigned long pwm_freq_from_reg_627hf(u8 reg)
298 {
299 unsigned long freq;
300 freq = W83627HF_BASE_PWM_FREQ >> reg;
301 return freq;
302 }
pwm_freq_to_reg_627hf(unsigned long val)303 static inline u8 pwm_freq_to_reg_627hf(unsigned long val)
304 {
305 u8 i;
306 /*
307 * Only 5 dividers (1 2 4 8 16)
308 * Search for the nearest available frequency
309 */
310 for (i = 0; i < 4; i++) {
311 if (val > (((W83627HF_BASE_PWM_FREQ >> i) +
312 (W83627HF_BASE_PWM_FREQ >> (i+1))) / 2))
313 break;
314 }
315 return i;
316 }
317
pwm_freq_from_reg(u8 reg)318 static inline unsigned long pwm_freq_from_reg(u8 reg)
319 {
320 /* Clock bit 8 -> 180 kHz or 24 MHz */
321 unsigned long clock = (reg & 0x80) ? 180000UL : 24000000UL;
322
323 reg &= 0x7f;
324 /* This should not happen but anyway... */
325 if (reg == 0)
326 reg++;
327 return clock / (reg << 8);
328 }
pwm_freq_to_reg(unsigned long val)329 static inline u8 pwm_freq_to_reg(unsigned long val)
330 {
331 /* Minimum divider value is 0x01 and maximum is 0x7F */
332 if (val >= 93750) /* The highest we can do */
333 return 0x01;
334 if (val >= 720) /* Use 24 MHz clock */
335 return 24000000UL / (val << 8);
336 if (val < 6) /* The lowest we can do */
337 return 0xFF;
338 else /* Use 180 kHz clock */
339 return 0x80 | (180000UL / (val << 8));
340 }
341
342 #define BEEP_MASK_FROM_REG(val) ((val) & 0xff7fff)
343 #define BEEP_MASK_TO_REG(val) ((val) & 0xff7fff)
344
345 #define DIV_FROM_REG(val) (1 << (val))
346
DIV_TO_REG(long val)347 static inline u8 DIV_TO_REG(long val)
348 {
349 int i;
350 val = clamp_val(val, 1, 128) >> 1;
351 for (i = 0; i < 7; i++) {
352 if (val == 0)
353 break;
354 val >>= 1;
355 }
356 return (u8)i;
357 }
358
359 /*
360 * For each registered chip, we need to keep some data in memory.
361 * The structure is dynamically allocated.
362 */
363 struct w83627hf_data {
364 unsigned short addr;
365 const char *name;
366 struct device *hwmon_dev;
367 struct mutex lock;
368 enum chips type;
369
370 struct mutex update_lock;
371 char valid; /* !=0 if following fields are valid */
372 unsigned long last_updated; /* In jiffies */
373
374 u8 in[9]; /* Register value */
375 u8 in_max[9]; /* Register value */
376 u8 in_min[9]; /* Register value */
377 u8 fan[3]; /* Register value */
378 u8 fan_min[3]; /* Register value */
379 u16 temp[3]; /* Register value */
380 u16 temp_max[3]; /* Register value */
381 u16 temp_max_hyst[3]; /* Register value */
382 u8 fan_div[3]; /* Register encoding, shifted right */
383 u8 vid; /* Register encoding, combined */
384 u32 alarms; /* Register encoding, combined */
385 u32 beep_mask; /* Register encoding, combined */
386 u8 pwm[3]; /* Register value */
387 u8 pwm_enable[3]; /* 1 = manual
388 * 2 = thermal cruise (also called SmartFan I)
389 * 3 = fan speed cruise
390 */
391 u8 pwm_freq[3]; /* Register value */
392 u16 sens[3]; /* 1 = pentium diode; 2 = 3904 diode;
393 * 4 = thermistor
394 */
395 u8 vrm;
396 u8 vrm_ovt; /* Register value, 627THF/637HF/687THF only */
397
398 #ifdef CONFIG_PM
399 /* Remember extra register values over suspend/resume */
400 u8 scfg1;
401 u8 scfg2;
402 #endif
403 };
404
405
406 static int w83627hf_probe(struct platform_device *pdev);
407 static int w83627hf_remove(struct platform_device *pdev);
408
409 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg);
410 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value);
411 static void w83627hf_update_fan_div(struct w83627hf_data *data);
412 static struct w83627hf_data *w83627hf_update_device(struct device *dev);
413 static void w83627hf_init_device(struct platform_device *pdev);
414
415 #ifdef CONFIG_PM
w83627hf_suspend(struct device * dev)416 static int w83627hf_suspend(struct device *dev)
417 {
418 struct w83627hf_data *data = w83627hf_update_device(dev);
419
420 mutex_lock(&data->update_lock);
421 data->scfg1 = w83627hf_read_value(data, W83781D_REG_SCFG1);
422 data->scfg2 = w83627hf_read_value(data, W83781D_REG_SCFG2);
423 mutex_unlock(&data->update_lock);
424
425 return 0;
426 }
427
w83627hf_resume(struct device * dev)428 static int w83627hf_resume(struct device *dev)
429 {
430 struct w83627hf_data *data = dev_get_drvdata(dev);
431 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
432
433 /* Restore limits */
434 mutex_lock(&data->update_lock);
435 for (i = 0; i <= 8; i++) {
436 /* skip missing sensors */
437 if (((data->type == w83697hf) && (i == 1)) ||
438 ((data->type != w83627hf && data->type != w83697hf)
439 && (i == 5 || i == 6)))
440 continue;
441 w83627hf_write_value(data, W83781D_REG_IN_MAX(i),
442 data->in_max[i]);
443 w83627hf_write_value(data, W83781D_REG_IN_MIN(i),
444 data->in_min[i]);
445 }
446 for (i = 0; i <= 2; i++)
447 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(i),
448 data->fan_min[i]);
449 for (i = 0; i < num_temps; i++) {
450 w83627hf_write_value(data, w83627hf_reg_temp_over[i],
451 data->temp_max[i]);
452 w83627hf_write_value(data, w83627hf_reg_temp_hyst[i],
453 data->temp_max_hyst[i]);
454 }
455
456 /* Fixup BIOS bugs */
457 if (data->type == w83627thf || data->type == w83637hf ||
458 data->type == w83687thf)
459 w83627hf_write_value(data, W83627THF_REG_VRM_OVT_CFG,
460 data->vrm_ovt);
461 w83627hf_write_value(data, W83781D_REG_SCFG1, data->scfg1);
462 w83627hf_write_value(data, W83781D_REG_SCFG2, data->scfg2);
463
464 /* Force re-reading all values */
465 data->valid = 0;
466 mutex_unlock(&data->update_lock);
467
468 return 0;
469 }
470
471 static const struct dev_pm_ops w83627hf_dev_pm_ops = {
472 .suspend = w83627hf_suspend,
473 .resume = w83627hf_resume,
474 };
475
476 #define W83627HF_DEV_PM_OPS (&w83627hf_dev_pm_ops)
477 #else
478 #define W83627HF_DEV_PM_OPS NULL
479 #endif /* CONFIG_PM */
480
481 static struct platform_driver w83627hf_driver = {
482 .driver = {
483 .name = DRVNAME,
484 .pm = W83627HF_DEV_PM_OPS,
485 },
486 .probe = w83627hf_probe,
487 .remove = w83627hf_remove,
488 };
489
490 static ssize_t
show_in_input(struct device * dev,struct device_attribute * devattr,char * buf)491 show_in_input(struct device *dev, struct device_attribute *devattr, char *buf)
492 {
493 int nr = to_sensor_dev_attr(devattr)->index;
494 struct w83627hf_data *data = w83627hf_update_device(dev);
495 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in[nr]));
496 }
497 static ssize_t
show_in_min(struct device * dev,struct device_attribute * devattr,char * buf)498 show_in_min(struct device *dev, struct device_attribute *devattr, char *buf)
499 {
500 int nr = to_sensor_dev_attr(devattr)->index;
501 struct w83627hf_data *data = w83627hf_update_device(dev);
502 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_min[nr]));
503 }
504 static ssize_t
show_in_max(struct device * dev,struct device_attribute * devattr,char * buf)505 show_in_max(struct device *dev, struct device_attribute *devattr, char *buf)
506 {
507 int nr = to_sensor_dev_attr(devattr)->index;
508 struct w83627hf_data *data = w83627hf_update_device(dev);
509 return sprintf(buf, "%ld\n", (long)IN_FROM_REG(data->in_max[nr]));
510 }
511 static ssize_t
store_in_min(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)512 store_in_min(struct device *dev, struct device_attribute *devattr,
513 const char *buf, size_t count)
514 {
515 int nr = to_sensor_dev_attr(devattr)->index;
516 struct w83627hf_data *data = dev_get_drvdata(dev);
517 long val;
518 int err;
519
520 err = kstrtol(buf, 10, &val);
521 if (err)
522 return err;
523
524 mutex_lock(&data->update_lock);
525 data->in_min[nr] = IN_TO_REG(val);
526 w83627hf_write_value(data, W83781D_REG_IN_MIN(nr), data->in_min[nr]);
527 mutex_unlock(&data->update_lock);
528 return count;
529 }
530 static ssize_t
store_in_max(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)531 store_in_max(struct device *dev, struct device_attribute *devattr,
532 const char *buf, size_t count)
533 {
534 int nr = to_sensor_dev_attr(devattr)->index;
535 struct w83627hf_data *data = dev_get_drvdata(dev);
536 long val;
537 int err;
538
539 err = kstrtol(buf, 10, &val);
540 if (err)
541 return err;
542
543 mutex_lock(&data->update_lock);
544 data->in_max[nr] = IN_TO_REG(val);
545 w83627hf_write_value(data, W83781D_REG_IN_MAX(nr), data->in_max[nr]);
546 mutex_unlock(&data->update_lock);
547 return count;
548 }
549 #define sysfs_vin_decl(offset) \
550 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
551 show_in_input, NULL, offset); \
552 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO|S_IWUSR, \
553 show_in_min, store_in_min, offset); \
554 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO|S_IWUSR, \
555 show_in_max, store_in_max, offset);
556
557 sysfs_vin_decl(1);
558 sysfs_vin_decl(2);
559 sysfs_vin_decl(3);
560 sysfs_vin_decl(4);
561 sysfs_vin_decl(5);
562 sysfs_vin_decl(6);
563 sysfs_vin_decl(7);
564 sysfs_vin_decl(8);
565
566 /* use a different set of functions for in0 */
show_in_0(struct w83627hf_data * data,char * buf,u8 reg)567 static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
568 {
569 long in0;
570
571 if ((data->vrm_ovt & 0x01) &&
572 (w83627thf == data->type || w83637hf == data->type
573 || w83687thf == data->type))
574
575 /* use VRM9 calculation */
576 in0 = (long)((reg * 488 + 70000 + 50) / 100);
577 else
578 /* use VRM8 (standard) calculation */
579 in0 = (long)IN_FROM_REG(reg);
580
581 return sprintf(buf,"%ld\n", in0);
582 }
583
show_regs_in_0(struct device * dev,struct device_attribute * attr,char * buf)584 static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
585 {
586 struct w83627hf_data *data = w83627hf_update_device(dev);
587 return show_in_0(data, buf, data->in[0]);
588 }
589
show_regs_in_min0(struct device * dev,struct device_attribute * attr,char * buf)590 static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
591 {
592 struct w83627hf_data *data = w83627hf_update_device(dev);
593 return show_in_0(data, buf, data->in_min[0]);
594 }
595
show_regs_in_max0(struct device * dev,struct device_attribute * attr,char * buf)596 static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
597 {
598 struct w83627hf_data *data = w83627hf_update_device(dev);
599 return show_in_0(data, buf, data->in_max[0]);
600 }
601
store_regs_in_min0(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)602 static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
603 const char *buf, size_t count)
604 {
605 struct w83627hf_data *data = dev_get_drvdata(dev);
606 unsigned long val;
607 int err;
608
609 err = kstrtoul(buf, 10, &val);
610 if (err)
611 return err;
612
613 mutex_lock(&data->update_lock);
614
615 if ((data->vrm_ovt & 0x01) &&
616 (w83627thf == data->type || w83637hf == data->type
617 || w83687thf == data->type))
618
619 /* use VRM9 calculation */
620 data->in_min[0] =
621 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
622 else
623 /* use VRM8 (standard) calculation */
624 data->in_min[0] = IN_TO_REG(val);
625
626 w83627hf_write_value(data, W83781D_REG_IN_MIN(0), data->in_min[0]);
627 mutex_unlock(&data->update_lock);
628 return count;
629 }
630
store_regs_in_max0(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)631 static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
632 const char *buf, size_t count)
633 {
634 struct w83627hf_data *data = dev_get_drvdata(dev);
635 unsigned long val;
636 int err;
637
638 err = kstrtoul(buf, 10, &val);
639 if (err)
640 return err;
641
642 mutex_lock(&data->update_lock);
643
644 if ((data->vrm_ovt & 0x01) &&
645 (w83627thf == data->type || w83637hf == data->type
646 || w83687thf == data->type))
647
648 /* use VRM9 calculation */
649 data->in_max[0] =
650 clamp_val(((val * 100) - 70000 + 244) / 488, 0, 255);
651 else
652 /* use VRM8 (standard) calculation */
653 data->in_max[0] = IN_TO_REG(val);
654
655 w83627hf_write_value(data, W83781D_REG_IN_MAX(0), data->in_max[0]);
656 mutex_unlock(&data->update_lock);
657 return count;
658 }
659
660 static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
661 static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
662 show_regs_in_min0, store_regs_in_min0);
663 static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
664 show_regs_in_max0, store_regs_in_max0);
665
666 static ssize_t
show_fan_input(struct device * dev,struct device_attribute * devattr,char * buf)667 show_fan_input(struct device *dev, struct device_attribute *devattr, char *buf)
668 {
669 int nr = to_sensor_dev_attr(devattr)->index;
670 struct w83627hf_data *data = w83627hf_update_device(dev);
671 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan[nr],
672 (long)DIV_FROM_REG(data->fan_div[nr])));
673 }
674 static ssize_t
show_fan_min(struct device * dev,struct device_attribute * devattr,char * buf)675 show_fan_min(struct device *dev, struct device_attribute *devattr, char *buf)
676 {
677 int nr = to_sensor_dev_attr(devattr)->index;
678 struct w83627hf_data *data = w83627hf_update_device(dev);
679 return sprintf(buf, "%ld\n", FAN_FROM_REG(data->fan_min[nr],
680 (long)DIV_FROM_REG(data->fan_div[nr])));
681 }
682 static ssize_t
store_fan_min(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)683 store_fan_min(struct device *dev, struct device_attribute *devattr,
684 const char *buf, size_t count)
685 {
686 int nr = to_sensor_dev_attr(devattr)->index;
687 struct w83627hf_data *data = dev_get_drvdata(dev);
688 unsigned long val;
689 int err;
690
691 err = kstrtoul(buf, 10, &val);
692 if (err)
693 return err;
694
695 mutex_lock(&data->update_lock);
696 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
697 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr),
698 data->fan_min[nr]);
699
700 mutex_unlock(&data->update_lock);
701 return count;
702 }
703 #define sysfs_fan_decl(offset) \
704 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
705 show_fan_input, NULL, offset - 1); \
706 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
707 show_fan_min, store_fan_min, offset - 1);
708
709 sysfs_fan_decl(1);
710 sysfs_fan_decl(2);
711 sysfs_fan_decl(3);
712
713 static ssize_t
show_temp(struct device * dev,struct device_attribute * devattr,char * buf)714 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
715 {
716 int nr = to_sensor_dev_attr(devattr)->index;
717 struct w83627hf_data *data = w83627hf_update_device(dev);
718
719 u16 tmp = data->temp[nr];
720 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
721 : (long) TEMP_FROM_REG(tmp));
722 }
723
724 static ssize_t
show_temp_max(struct device * dev,struct device_attribute * devattr,char * buf)725 show_temp_max(struct device *dev, struct device_attribute *devattr,
726 char *buf)
727 {
728 int nr = to_sensor_dev_attr(devattr)->index;
729 struct w83627hf_data *data = w83627hf_update_device(dev);
730
731 u16 tmp = data->temp_max[nr];
732 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
733 : (long) TEMP_FROM_REG(tmp));
734 }
735
736 static ssize_t
show_temp_max_hyst(struct device * dev,struct device_attribute * devattr,char * buf)737 show_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
738 char *buf)
739 {
740 int nr = to_sensor_dev_attr(devattr)->index;
741 struct w83627hf_data *data = w83627hf_update_device(dev);
742
743 u16 tmp = data->temp_max_hyst[nr];
744 return sprintf(buf, "%ld\n", (nr) ? (long) LM75_TEMP_FROM_REG(tmp)
745 : (long) TEMP_FROM_REG(tmp));
746 }
747
748 static ssize_t
store_temp_max(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)749 store_temp_max(struct device *dev, struct device_attribute *devattr,
750 const char *buf, size_t count)
751 {
752 int nr = to_sensor_dev_attr(devattr)->index;
753 struct w83627hf_data *data = dev_get_drvdata(dev);
754 u16 tmp;
755 long val;
756 int err;
757
758 err = kstrtol(buf, 10, &val);
759 if (err)
760 return err;
761
762 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
763 mutex_lock(&data->update_lock);
764 data->temp_max[nr] = tmp;
765 w83627hf_write_value(data, w83627hf_reg_temp_over[nr], tmp);
766 mutex_unlock(&data->update_lock);
767 return count;
768 }
769
770 static ssize_t
store_temp_max_hyst(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)771 store_temp_max_hyst(struct device *dev, struct device_attribute *devattr,
772 const char *buf, size_t count)
773 {
774 int nr = to_sensor_dev_attr(devattr)->index;
775 struct w83627hf_data *data = dev_get_drvdata(dev);
776 u16 tmp;
777 long val;
778 int err;
779
780 err = kstrtol(buf, 10, &val);
781 if (err)
782 return err;
783
784 tmp = (nr) ? LM75_TEMP_TO_REG(val) : TEMP_TO_REG(val);
785 mutex_lock(&data->update_lock);
786 data->temp_max_hyst[nr] = tmp;
787 w83627hf_write_value(data, w83627hf_reg_temp_hyst[nr], tmp);
788 mutex_unlock(&data->update_lock);
789 return count;
790 }
791
792 #define sysfs_temp_decl(offset) \
793 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
794 show_temp, NULL, offset - 1); \
795 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO|S_IWUSR, \
796 show_temp_max, store_temp_max, offset - 1); \
797 static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO|S_IWUSR, \
798 show_temp_max_hyst, store_temp_max_hyst, offset - 1);
799
800 sysfs_temp_decl(1);
801 sysfs_temp_decl(2);
802 sysfs_temp_decl(3);
803
804 static ssize_t
show_vid_reg(struct device * dev,struct device_attribute * attr,char * buf)805 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
806 {
807 struct w83627hf_data *data = w83627hf_update_device(dev);
808 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
809 }
810 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
811
812 static ssize_t
show_vrm_reg(struct device * dev,struct device_attribute * attr,char * buf)813 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
814 {
815 struct w83627hf_data *data = dev_get_drvdata(dev);
816 return sprintf(buf, "%ld\n", (long) data->vrm);
817 }
818 static ssize_t
store_vrm_reg(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)819 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
820 {
821 struct w83627hf_data *data = dev_get_drvdata(dev);
822 unsigned long val;
823 int err;
824
825 err = kstrtoul(buf, 10, &val);
826 if (err)
827 return err;
828
829 if (val > 255)
830 return -EINVAL;
831 data->vrm = val;
832
833 return count;
834 }
835 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
836
837 static ssize_t
show_alarms_reg(struct device * dev,struct device_attribute * attr,char * buf)838 show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
839 {
840 struct w83627hf_data *data = w83627hf_update_device(dev);
841 return sprintf(buf, "%ld\n", (long) data->alarms);
842 }
843 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
844
845 static ssize_t
show_alarm(struct device * dev,struct device_attribute * attr,char * buf)846 show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
847 {
848 struct w83627hf_data *data = w83627hf_update_device(dev);
849 int bitnr = to_sensor_dev_attr(attr)->index;
850 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
851 }
852 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
853 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
854 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
855 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
856 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
857 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
858 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
859 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
860 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
861 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
862 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
863 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
864 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
865 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
866 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
867
868 static ssize_t
show_beep_mask(struct device * dev,struct device_attribute * attr,char * buf)869 show_beep_mask(struct device *dev, struct device_attribute *attr, char *buf)
870 {
871 struct w83627hf_data *data = w83627hf_update_device(dev);
872 return sprintf(buf, "%ld\n",
873 (long)BEEP_MASK_FROM_REG(data->beep_mask));
874 }
875
876 static ssize_t
store_beep_mask(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)877 store_beep_mask(struct device *dev, struct device_attribute *attr,
878 const char *buf, size_t count)
879 {
880 struct w83627hf_data *data = dev_get_drvdata(dev);
881 unsigned long val;
882 int err;
883
884 err = kstrtoul(buf, 10, &val);
885 if (err)
886 return err;
887
888 mutex_lock(&data->update_lock);
889
890 /* preserve beep enable */
891 data->beep_mask = (data->beep_mask & 0x8000)
892 | BEEP_MASK_TO_REG(val);
893 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1,
894 data->beep_mask & 0xff);
895 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3,
896 ((data->beep_mask) >> 16) & 0xff);
897 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2,
898 (data->beep_mask >> 8) & 0xff);
899
900 mutex_unlock(&data->update_lock);
901 return count;
902 }
903
904 static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
905 show_beep_mask, store_beep_mask);
906
907 static ssize_t
show_beep(struct device * dev,struct device_attribute * attr,char * buf)908 show_beep(struct device *dev, struct device_attribute *attr, char *buf)
909 {
910 struct w83627hf_data *data = w83627hf_update_device(dev);
911 int bitnr = to_sensor_dev_attr(attr)->index;
912 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
913 }
914
915 static ssize_t
store_beep(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)916 store_beep(struct device *dev, struct device_attribute *attr,
917 const char *buf, size_t count)
918 {
919 struct w83627hf_data *data = dev_get_drvdata(dev);
920 int bitnr = to_sensor_dev_attr(attr)->index;
921 u8 reg;
922 unsigned long bit;
923 int err;
924
925 err = kstrtoul(buf, 10, &bit);
926 if (err)
927 return err;
928
929 if (bit & ~1)
930 return -EINVAL;
931
932 mutex_lock(&data->update_lock);
933 if (bit)
934 data->beep_mask |= (1 << bitnr);
935 else
936 data->beep_mask &= ~(1 << bitnr);
937
938 if (bitnr < 8) {
939 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS1);
940 if (bit)
941 reg |= (1 << bitnr);
942 else
943 reg &= ~(1 << bitnr);
944 w83627hf_write_value(data, W83781D_REG_BEEP_INTS1, reg);
945 } else if (bitnr < 16) {
946 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
947 if (bit)
948 reg |= (1 << (bitnr - 8));
949 else
950 reg &= ~(1 << (bitnr - 8));
951 w83627hf_write_value(data, W83781D_REG_BEEP_INTS2, reg);
952 } else {
953 reg = w83627hf_read_value(data, W83781D_REG_BEEP_INTS3);
954 if (bit)
955 reg |= (1 << (bitnr - 16));
956 else
957 reg &= ~(1 << (bitnr - 16));
958 w83627hf_write_value(data, W83781D_REG_BEEP_INTS3, reg);
959 }
960 mutex_unlock(&data->update_lock);
961
962 return count;
963 }
964
965 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
966 show_beep, store_beep, 0);
967 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
968 show_beep, store_beep, 1);
969 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
970 show_beep, store_beep, 2);
971 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
972 show_beep, store_beep, 3);
973 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
974 show_beep, store_beep, 8);
975 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
976 show_beep, store_beep, 9);
977 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
978 show_beep, store_beep, 10);
979 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
980 show_beep, store_beep, 16);
981 static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
982 show_beep, store_beep, 17);
983 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
984 show_beep, store_beep, 6);
985 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
986 show_beep, store_beep, 7);
987 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
988 show_beep, store_beep, 11);
989 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
990 show_beep, store_beep, 4);
991 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
992 show_beep, store_beep, 5);
993 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO | S_IWUSR,
994 show_beep, store_beep, 13);
995 static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
996 show_beep, store_beep, 15);
997
998 static ssize_t
show_fan_div(struct device * dev,struct device_attribute * devattr,char * buf)999 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
1000 {
1001 int nr = to_sensor_dev_attr(devattr)->index;
1002 struct w83627hf_data *data = w83627hf_update_device(dev);
1003 return sprintf(buf, "%ld\n",
1004 (long) DIV_FROM_REG(data->fan_div[nr]));
1005 }
1006 /*
1007 * Note: we save and restore the fan minimum here, because its value is
1008 * determined in part by the fan divisor. This follows the principle of
1009 * least surprise; the user doesn't expect the fan minimum to change just
1010 * because the divisor changed.
1011 */
1012 static ssize_t
store_fan_div(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1013 store_fan_div(struct device *dev, struct device_attribute *devattr,
1014 const char *buf, size_t count)
1015 {
1016 int nr = to_sensor_dev_attr(devattr)->index;
1017 struct w83627hf_data *data = dev_get_drvdata(dev);
1018 unsigned long min;
1019 u8 reg;
1020 unsigned long val;
1021 int err;
1022
1023 err = kstrtoul(buf, 10, &val);
1024 if (err)
1025 return err;
1026
1027 mutex_lock(&data->update_lock);
1028
1029 /* Save fan_min */
1030 min = FAN_FROM_REG(data->fan_min[nr],
1031 DIV_FROM_REG(data->fan_div[nr]));
1032
1033 data->fan_div[nr] = DIV_TO_REG(val);
1034
1035 reg = (w83627hf_read_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
1036 & (nr==0 ? 0xcf : 0x3f))
1037 | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
1038 w83627hf_write_value(data, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
1039
1040 reg = (w83627hf_read_value(data, W83781D_REG_VBAT)
1041 & ~(1 << (5 + nr)))
1042 | ((data->fan_div[nr] & 0x04) << (3 + nr));
1043 w83627hf_write_value(data, W83781D_REG_VBAT, reg);
1044
1045 /* Restore fan_min */
1046 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1047 w83627hf_write_value(data, W83627HF_REG_FAN_MIN(nr), data->fan_min[nr]);
1048
1049 mutex_unlock(&data->update_lock);
1050 return count;
1051 }
1052
1053 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO|S_IWUSR,
1054 show_fan_div, store_fan_div, 0);
1055 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO|S_IWUSR,
1056 show_fan_div, store_fan_div, 1);
1057 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO|S_IWUSR,
1058 show_fan_div, store_fan_div, 2);
1059
1060 static ssize_t
show_pwm(struct device * dev,struct device_attribute * devattr,char * buf)1061 show_pwm(struct device *dev, struct device_attribute *devattr, char *buf)
1062 {
1063 int nr = to_sensor_dev_attr(devattr)->index;
1064 struct w83627hf_data *data = w83627hf_update_device(dev);
1065 return sprintf(buf, "%ld\n", (long) data->pwm[nr]);
1066 }
1067
1068 static ssize_t
store_pwm(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1069 store_pwm(struct device *dev, struct device_attribute *devattr,
1070 const char *buf, size_t count)
1071 {
1072 int nr = to_sensor_dev_attr(devattr)->index;
1073 struct w83627hf_data *data = dev_get_drvdata(dev);
1074 unsigned long val;
1075 int err;
1076
1077 err = kstrtoul(buf, 10, &val);
1078 if (err)
1079 return err;
1080
1081 mutex_lock(&data->update_lock);
1082
1083 if (data->type == w83627thf) {
1084 /* bits 0-3 are reserved in 627THF */
1085 data->pwm[nr] = PWM_TO_REG(val) & 0xf0;
1086 w83627hf_write_value(data,
1087 W836X7HF_REG_PWM(data->type, nr),
1088 data->pwm[nr] |
1089 (w83627hf_read_value(data,
1090 W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
1091 } else {
1092 data->pwm[nr] = PWM_TO_REG(val);
1093 w83627hf_write_value(data,
1094 W836X7HF_REG_PWM(data->type, nr),
1095 data->pwm[nr]);
1096 }
1097
1098 mutex_unlock(&data->update_lock);
1099 return count;
1100 }
1101
1102 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0);
1103 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 1);
1104 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 2);
1105
1106 static ssize_t
show_pwm_enable(struct device * dev,struct device_attribute * devattr,char * buf)1107 show_pwm_enable(struct device *dev, struct device_attribute *devattr, char *buf)
1108 {
1109 int nr = to_sensor_dev_attr(devattr)->index;
1110 struct w83627hf_data *data = w83627hf_update_device(dev);
1111 return sprintf(buf, "%d\n", data->pwm_enable[nr]);
1112 }
1113
1114 static ssize_t
store_pwm_enable(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1115 store_pwm_enable(struct device *dev, struct device_attribute *devattr,
1116 const char *buf, size_t count)
1117 {
1118 int nr = to_sensor_dev_attr(devattr)->index;
1119 struct w83627hf_data *data = dev_get_drvdata(dev);
1120 u8 reg;
1121 unsigned long val;
1122 int err;
1123
1124 err = kstrtoul(buf, 10, &val);
1125 if (err)
1126 return err;
1127
1128 if (!val || val > 3) /* modes 1, 2 and 3 are supported */
1129 return -EINVAL;
1130 mutex_lock(&data->update_lock);
1131 data->pwm_enable[nr] = val;
1132 reg = w83627hf_read_value(data, W83627THF_REG_PWM_ENABLE[nr]);
1133 reg &= ~(0x03 << W83627THF_PWM_ENABLE_SHIFT[nr]);
1134 reg |= (val - 1) << W83627THF_PWM_ENABLE_SHIFT[nr];
1135 w83627hf_write_value(data, W83627THF_REG_PWM_ENABLE[nr], reg);
1136 mutex_unlock(&data->update_lock);
1137 return count;
1138 }
1139
1140 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1141 store_pwm_enable, 0);
1142 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1143 store_pwm_enable, 1);
1144 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
1145 store_pwm_enable, 2);
1146
1147 static ssize_t
show_pwm_freq(struct device * dev,struct device_attribute * devattr,char * buf)1148 show_pwm_freq(struct device *dev, struct device_attribute *devattr, char *buf)
1149 {
1150 int nr = to_sensor_dev_attr(devattr)->index;
1151 struct w83627hf_data *data = w83627hf_update_device(dev);
1152 if (data->type == w83627hf)
1153 return sprintf(buf, "%ld\n",
1154 pwm_freq_from_reg_627hf(data->pwm_freq[nr]));
1155 else
1156 return sprintf(buf, "%ld\n",
1157 pwm_freq_from_reg(data->pwm_freq[nr]));
1158 }
1159
1160 static ssize_t
store_pwm_freq(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1161 store_pwm_freq(struct device *dev, struct device_attribute *devattr,
1162 const char *buf, size_t count)
1163 {
1164 int nr = to_sensor_dev_attr(devattr)->index;
1165 struct w83627hf_data *data = dev_get_drvdata(dev);
1166 static const u8 mask[]={0xF8, 0x8F};
1167 unsigned long val;
1168 int err;
1169
1170 err = kstrtoul(buf, 10, &val);
1171 if (err)
1172 return err;
1173
1174 mutex_lock(&data->update_lock);
1175
1176 if (data->type == w83627hf) {
1177 data->pwm_freq[nr] = pwm_freq_to_reg_627hf(val);
1178 w83627hf_write_value(data, W83627HF_REG_PWM_FREQ,
1179 (data->pwm_freq[nr] << (nr*4)) |
1180 (w83627hf_read_value(data,
1181 W83627HF_REG_PWM_FREQ) & mask[nr]));
1182 } else {
1183 data->pwm_freq[nr] = pwm_freq_to_reg(val);
1184 w83627hf_write_value(data, W83637HF_REG_PWM_FREQ[nr],
1185 data->pwm_freq[nr]);
1186 }
1187
1188 mutex_unlock(&data->update_lock);
1189 return count;
1190 }
1191
1192 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO|S_IWUSR,
1193 show_pwm_freq, store_pwm_freq, 0);
1194 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO|S_IWUSR,
1195 show_pwm_freq, store_pwm_freq, 1);
1196 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO|S_IWUSR,
1197 show_pwm_freq, store_pwm_freq, 2);
1198
1199 static ssize_t
show_temp_type(struct device * dev,struct device_attribute * devattr,char * buf)1200 show_temp_type(struct device *dev, struct device_attribute *devattr,
1201 char *buf)
1202 {
1203 int nr = to_sensor_dev_attr(devattr)->index;
1204 struct w83627hf_data *data = w83627hf_update_device(dev);
1205 return sprintf(buf, "%ld\n", (long) data->sens[nr]);
1206 }
1207
1208 static ssize_t
store_temp_type(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)1209 store_temp_type(struct device *dev, struct device_attribute *devattr,
1210 const char *buf, size_t count)
1211 {
1212 int nr = to_sensor_dev_attr(devattr)->index;
1213 struct w83627hf_data *data = dev_get_drvdata(dev);
1214 unsigned long val;
1215 u32 tmp;
1216 int err;
1217
1218 err = kstrtoul(buf, 10, &val);
1219 if (err)
1220 return err;
1221
1222 mutex_lock(&data->update_lock);
1223
1224 switch (val) {
1225 case 1: /* PII/Celeron diode */
1226 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1227 w83627hf_write_value(data, W83781D_REG_SCFG1,
1228 tmp | BIT_SCFG1[nr]);
1229 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1230 w83627hf_write_value(data, W83781D_REG_SCFG2,
1231 tmp | BIT_SCFG2[nr]);
1232 data->sens[nr] = val;
1233 break;
1234 case 2: /* 3904 */
1235 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1236 w83627hf_write_value(data, W83781D_REG_SCFG1,
1237 tmp | BIT_SCFG1[nr]);
1238 tmp = w83627hf_read_value(data, W83781D_REG_SCFG2);
1239 w83627hf_write_value(data, W83781D_REG_SCFG2,
1240 tmp & ~BIT_SCFG2[nr]);
1241 data->sens[nr] = val;
1242 break;
1243 case W83781D_DEFAULT_BETA:
1244 dev_warn(dev, "Sensor type %d is deprecated, please use 4 "
1245 "instead\n", W83781D_DEFAULT_BETA);
1246 /* fall through */
1247 case 4: /* thermistor */
1248 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1249 w83627hf_write_value(data, W83781D_REG_SCFG1,
1250 tmp & ~BIT_SCFG1[nr]);
1251 data->sens[nr] = val;
1252 break;
1253 default:
1254 dev_err(dev,
1255 "Invalid sensor type %ld; must be 1, 2, or 4\n",
1256 (long) val);
1257 break;
1258 }
1259
1260 mutex_unlock(&data->update_lock);
1261 return count;
1262 }
1263
1264 #define sysfs_temp_type(offset) \
1265 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
1266 show_temp_type, store_temp_type, offset - 1);
1267
1268 sysfs_temp_type(1);
1269 sysfs_temp_type(2);
1270 sysfs_temp_type(3);
1271
1272 static ssize_t
show_name(struct device * dev,struct device_attribute * devattr,char * buf)1273 show_name(struct device *dev, struct device_attribute *devattr, char *buf)
1274 {
1275 struct w83627hf_data *data = dev_get_drvdata(dev);
1276
1277 return sprintf(buf, "%s\n", data->name);
1278 }
1279 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1280
w83627hf_find(int sioaddr,unsigned short * addr,struct w83627hf_sio_data * sio_data)1281 static int __init w83627hf_find(int sioaddr, unsigned short *addr,
1282 struct w83627hf_sio_data *sio_data)
1283 {
1284 int err;
1285 u16 val;
1286
1287 static __initconst char *const names[] = {
1288 "W83627HF",
1289 "W83627THF",
1290 "W83697HF",
1291 "W83637HF",
1292 "W83687THF",
1293 };
1294
1295 sio_data->sioaddr = sioaddr;
1296 err = superio_enter(sio_data);
1297 if (err)
1298 return err;
1299
1300 err = -ENODEV;
1301 val = force_id ? force_id : superio_inb(sio_data, DEVID);
1302 switch (val) {
1303 case W627_DEVID:
1304 sio_data->type = w83627hf;
1305 break;
1306 case W627THF_DEVID:
1307 sio_data->type = w83627thf;
1308 break;
1309 case W697_DEVID:
1310 sio_data->type = w83697hf;
1311 break;
1312 case W637_DEVID:
1313 sio_data->type = w83637hf;
1314 break;
1315 case W687THF_DEVID:
1316 sio_data->type = w83687thf;
1317 break;
1318 case 0xff: /* No device at all */
1319 goto exit;
1320 default:
1321 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%02x)\n", val);
1322 goto exit;
1323 }
1324
1325 superio_select(sio_data, W83627HF_LD_HWM);
1326 val = (superio_inb(sio_data, WINB_BASE_REG) << 8) |
1327 superio_inb(sio_data, WINB_BASE_REG + 1);
1328 *addr = val & WINB_ALIGNMENT;
1329 if (*addr == 0) {
1330 pr_warn("Base address not set, skipping\n");
1331 goto exit;
1332 }
1333
1334 val = superio_inb(sio_data, WINB_ACT_REG);
1335 if (!(val & 0x01)) {
1336 pr_warn("Enabling HWM logical device\n");
1337 superio_outb(sio_data, WINB_ACT_REG, val | 0x01);
1338 }
1339
1340 err = 0;
1341 pr_info(DRVNAME ": Found %s chip at %#x\n",
1342 names[sio_data->type], *addr);
1343
1344 exit:
1345 superio_exit(sio_data);
1346 return err;
1347 }
1348
1349 #define VIN_UNIT_ATTRS(_X_) \
1350 &sensor_dev_attr_in##_X_##_input.dev_attr.attr, \
1351 &sensor_dev_attr_in##_X_##_min.dev_attr.attr, \
1352 &sensor_dev_attr_in##_X_##_max.dev_attr.attr, \
1353 &sensor_dev_attr_in##_X_##_alarm.dev_attr.attr, \
1354 &sensor_dev_attr_in##_X_##_beep.dev_attr.attr
1355
1356 #define FAN_UNIT_ATTRS(_X_) \
1357 &sensor_dev_attr_fan##_X_##_input.dev_attr.attr, \
1358 &sensor_dev_attr_fan##_X_##_min.dev_attr.attr, \
1359 &sensor_dev_attr_fan##_X_##_div.dev_attr.attr, \
1360 &sensor_dev_attr_fan##_X_##_alarm.dev_attr.attr, \
1361 &sensor_dev_attr_fan##_X_##_beep.dev_attr.attr
1362
1363 #define TEMP_UNIT_ATTRS(_X_) \
1364 &sensor_dev_attr_temp##_X_##_input.dev_attr.attr, \
1365 &sensor_dev_attr_temp##_X_##_max.dev_attr.attr, \
1366 &sensor_dev_attr_temp##_X_##_max_hyst.dev_attr.attr, \
1367 &sensor_dev_attr_temp##_X_##_type.dev_attr.attr, \
1368 &sensor_dev_attr_temp##_X_##_alarm.dev_attr.attr, \
1369 &sensor_dev_attr_temp##_X_##_beep.dev_attr.attr
1370
1371 static struct attribute *w83627hf_attributes[] = {
1372 &dev_attr_in0_input.attr,
1373 &dev_attr_in0_min.attr,
1374 &dev_attr_in0_max.attr,
1375 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1376 &sensor_dev_attr_in0_beep.dev_attr.attr,
1377 VIN_UNIT_ATTRS(2),
1378 VIN_UNIT_ATTRS(3),
1379 VIN_UNIT_ATTRS(4),
1380 VIN_UNIT_ATTRS(7),
1381 VIN_UNIT_ATTRS(8),
1382
1383 FAN_UNIT_ATTRS(1),
1384 FAN_UNIT_ATTRS(2),
1385
1386 TEMP_UNIT_ATTRS(1),
1387 TEMP_UNIT_ATTRS(2),
1388
1389 &dev_attr_alarms.attr,
1390 &sensor_dev_attr_beep_enable.dev_attr.attr,
1391 &dev_attr_beep_mask.attr,
1392
1393 &sensor_dev_attr_pwm1.dev_attr.attr,
1394 &sensor_dev_attr_pwm2.dev_attr.attr,
1395 &dev_attr_name.attr,
1396 NULL
1397 };
1398
1399 static const struct attribute_group w83627hf_group = {
1400 .attrs = w83627hf_attributes,
1401 };
1402
1403 static struct attribute *w83627hf_attributes_opt[] = {
1404 VIN_UNIT_ATTRS(1),
1405 VIN_UNIT_ATTRS(5),
1406 VIN_UNIT_ATTRS(6),
1407
1408 FAN_UNIT_ATTRS(3),
1409 TEMP_UNIT_ATTRS(3),
1410 &sensor_dev_attr_pwm3.dev_attr.attr,
1411
1412 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1413 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1414 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1415
1416 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1417 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1418 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1419
1420 NULL
1421 };
1422
1423 static const struct attribute_group w83627hf_group_opt = {
1424 .attrs = w83627hf_attributes_opt,
1425 };
1426
w83627hf_probe(struct platform_device * pdev)1427 static int w83627hf_probe(struct platform_device *pdev)
1428 {
1429 struct device *dev = &pdev->dev;
1430 struct w83627hf_sio_data *sio_data = dev_get_platdata(dev);
1431 struct w83627hf_data *data;
1432 struct resource *res;
1433 int err, i;
1434
1435 static const char *names[] = {
1436 "w83627hf",
1437 "w83627thf",
1438 "w83697hf",
1439 "w83637hf",
1440 "w83687thf",
1441 };
1442
1443 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1444 if (!devm_request_region(dev, res->start, WINB_REGION_SIZE, DRVNAME)) {
1445 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1446 (unsigned long)res->start,
1447 (unsigned long)(res->start + WINB_REGION_SIZE - 1));
1448 return -EBUSY;
1449 }
1450
1451 data = devm_kzalloc(dev, sizeof(struct w83627hf_data), GFP_KERNEL);
1452 if (!data)
1453 return -ENOMEM;
1454
1455 data->addr = res->start;
1456 data->type = sio_data->type;
1457 data->name = names[sio_data->type];
1458 mutex_init(&data->lock);
1459 mutex_init(&data->update_lock);
1460 platform_set_drvdata(pdev, data);
1461
1462 /* Initialize the chip */
1463 w83627hf_init_device(pdev);
1464
1465 /* A few vars need to be filled upon startup */
1466 for (i = 0; i <= 2; i++)
1467 data->fan_min[i] = w83627hf_read_value(
1468 data, W83627HF_REG_FAN_MIN(i));
1469 w83627hf_update_fan_div(data);
1470
1471 /* Register common device attributes */
1472 err = sysfs_create_group(&dev->kobj, &w83627hf_group);
1473 if (err)
1474 return err;
1475
1476 /* Register chip-specific device attributes */
1477 if (data->type == w83627hf || data->type == w83697hf)
1478 if ((err = device_create_file(dev,
1479 &sensor_dev_attr_in5_input.dev_attr))
1480 || (err = device_create_file(dev,
1481 &sensor_dev_attr_in5_min.dev_attr))
1482 || (err = device_create_file(dev,
1483 &sensor_dev_attr_in5_max.dev_attr))
1484 || (err = device_create_file(dev,
1485 &sensor_dev_attr_in5_alarm.dev_attr))
1486 || (err = device_create_file(dev,
1487 &sensor_dev_attr_in5_beep.dev_attr))
1488 || (err = device_create_file(dev,
1489 &sensor_dev_attr_in6_input.dev_attr))
1490 || (err = device_create_file(dev,
1491 &sensor_dev_attr_in6_min.dev_attr))
1492 || (err = device_create_file(dev,
1493 &sensor_dev_attr_in6_max.dev_attr))
1494 || (err = device_create_file(dev,
1495 &sensor_dev_attr_in6_alarm.dev_attr))
1496 || (err = device_create_file(dev,
1497 &sensor_dev_attr_in6_beep.dev_attr))
1498 || (err = device_create_file(dev,
1499 &sensor_dev_attr_pwm1_freq.dev_attr))
1500 || (err = device_create_file(dev,
1501 &sensor_dev_attr_pwm2_freq.dev_attr)))
1502 goto error;
1503
1504 if (data->type != w83697hf)
1505 if ((err = device_create_file(dev,
1506 &sensor_dev_attr_in1_input.dev_attr))
1507 || (err = device_create_file(dev,
1508 &sensor_dev_attr_in1_min.dev_attr))
1509 || (err = device_create_file(dev,
1510 &sensor_dev_attr_in1_max.dev_attr))
1511 || (err = device_create_file(dev,
1512 &sensor_dev_attr_in1_alarm.dev_attr))
1513 || (err = device_create_file(dev,
1514 &sensor_dev_attr_in1_beep.dev_attr))
1515 || (err = device_create_file(dev,
1516 &sensor_dev_attr_fan3_input.dev_attr))
1517 || (err = device_create_file(dev,
1518 &sensor_dev_attr_fan3_min.dev_attr))
1519 || (err = device_create_file(dev,
1520 &sensor_dev_attr_fan3_div.dev_attr))
1521 || (err = device_create_file(dev,
1522 &sensor_dev_attr_fan3_alarm.dev_attr))
1523 || (err = device_create_file(dev,
1524 &sensor_dev_attr_fan3_beep.dev_attr))
1525 || (err = device_create_file(dev,
1526 &sensor_dev_attr_temp3_input.dev_attr))
1527 || (err = device_create_file(dev,
1528 &sensor_dev_attr_temp3_max.dev_attr))
1529 || (err = device_create_file(dev,
1530 &sensor_dev_attr_temp3_max_hyst.dev_attr))
1531 || (err = device_create_file(dev,
1532 &sensor_dev_attr_temp3_alarm.dev_attr))
1533 || (err = device_create_file(dev,
1534 &sensor_dev_attr_temp3_beep.dev_attr))
1535 || (err = device_create_file(dev,
1536 &sensor_dev_attr_temp3_type.dev_attr)))
1537 goto error;
1538
1539 if (data->type != w83697hf && data->vid != 0xff) {
1540 /* Convert VID to voltage based on VRM */
1541 data->vrm = vid_which_vrm();
1542
1543 if ((err = device_create_file(dev, &dev_attr_cpu0_vid))
1544 || (err = device_create_file(dev, &dev_attr_vrm)))
1545 goto error;
1546 }
1547
1548 if (data->type == w83627thf || data->type == w83637hf
1549 || data->type == w83687thf) {
1550 err = device_create_file(dev, &sensor_dev_attr_pwm3.dev_attr);
1551 if (err)
1552 goto error;
1553 }
1554
1555 if (data->type == w83637hf || data->type == w83687thf)
1556 if ((err = device_create_file(dev,
1557 &sensor_dev_attr_pwm1_freq.dev_attr))
1558 || (err = device_create_file(dev,
1559 &sensor_dev_attr_pwm2_freq.dev_attr))
1560 || (err = device_create_file(dev,
1561 &sensor_dev_attr_pwm3_freq.dev_attr)))
1562 goto error;
1563
1564 if (data->type != w83627hf)
1565 if ((err = device_create_file(dev,
1566 &sensor_dev_attr_pwm1_enable.dev_attr))
1567 || (err = device_create_file(dev,
1568 &sensor_dev_attr_pwm2_enable.dev_attr)))
1569 goto error;
1570
1571 if (data->type == w83627thf || data->type == w83637hf
1572 || data->type == w83687thf) {
1573 err = device_create_file(dev,
1574 &sensor_dev_attr_pwm3_enable.dev_attr);
1575 if (err)
1576 goto error;
1577 }
1578
1579 data->hwmon_dev = hwmon_device_register(dev);
1580 if (IS_ERR(data->hwmon_dev)) {
1581 err = PTR_ERR(data->hwmon_dev);
1582 goto error;
1583 }
1584
1585 return 0;
1586
1587 error:
1588 sysfs_remove_group(&dev->kobj, &w83627hf_group);
1589 sysfs_remove_group(&dev->kobj, &w83627hf_group_opt);
1590 return err;
1591 }
1592
w83627hf_remove(struct platform_device * pdev)1593 static int w83627hf_remove(struct platform_device *pdev)
1594 {
1595 struct w83627hf_data *data = platform_get_drvdata(pdev);
1596
1597 hwmon_device_unregister(data->hwmon_dev);
1598
1599 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group);
1600 sysfs_remove_group(&pdev->dev.kobj, &w83627hf_group_opt);
1601
1602 return 0;
1603 }
1604
1605
1606 /* Registers 0x50-0x5f are banked */
w83627hf_set_bank(struct w83627hf_data * data,u16 reg)1607 static inline void w83627hf_set_bank(struct w83627hf_data *data, u16 reg)
1608 {
1609 if ((reg & 0x00f0) == 0x50) {
1610 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1611 outb_p(reg >> 8, data->addr + W83781D_DATA_REG_OFFSET);
1612 }
1613 }
1614
1615 /* Not strictly necessary, but play it safe for now */
w83627hf_reset_bank(struct w83627hf_data * data,u16 reg)1616 static inline void w83627hf_reset_bank(struct w83627hf_data *data, u16 reg)
1617 {
1618 if (reg & 0xff00) {
1619 outb_p(W83781D_REG_BANK, data->addr + W83781D_ADDR_REG_OFFSET);
1620 outb_p(0, data->addr + W83781D_DATA_REG_OFFSET);
1621 }
1622 }
1623
w83627hf_read_value(struct w83627hf_data * data,u16 reg)1624 static int w83627hf_read_value(struct w83627hf_data *data, u16 reg)
1625 {
1626 int res, word_sized;
1627
1628 mutex_lock(&data->lock);
1629 word_sized = (((reg & 0xff00) == 0x100)
1630 || ((reg & 0xff00) == 0x200))
1631 && (((reg & 0x00ff) == 0x50)
1632 || ((reg & 0x00ff) == 0x53)
1633 || ((reg & 0x00ff) == 0x55));
1634 w83627hf_set_bank(data, reg);
1635 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1636 res = inb_p(data->addr + W83781D_DATA_REG_OFFSET);
1637 if (word_sized) {
1638 outb_p((reg & 0xff) + 1,
1639 data->addr + W83781D_ADDR_REG_OFFSET);
1640 res =
1641 (res << 8) + inb_p(data->addr +
1642 W83781D_DATA_REG_OFFSET);
1643 }
1644 w83627hf_reset_bank(data, reg);
1645 mutex_unlock(&data->lock);
1646 return res;
1647 }
1648
w83627thf_read_gpio5(struct platform_device * pdev)1649 static int w83627thf_read_gpio5(struct platform_device *pdev)
1650 {
1651 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
1652 int res = 0xff, sel;
1653
1654 if (superio_enter(sio_data)) {
1655 /*
1656 * Some other driver reserved the address space for itself.
1657 * We don't want to fail driver instantiation because of that,
1658 * so display a warning and keep going.
1659 */
1660 dev_warn(&pdev->dev,
1661 "Can not read VID data: Failed to enable SuperIO access\n");
1662 return res;
1663 }
1664
1665 superio_select(sio_data, W83627HF_LD_GPIO5);
1666
1667 res = 0xff;
1668
1669 /* Make sure these GPIO pins are enabled */
1670 if (!(superio_inb(sio_data, W83627THF_GPIO5_EN) & (1<<3))) {
1671 dev_dbg(&pdev->dev, "GPIO5 disabled, no VID function\n");
1672 goto exit;
1673 }
1674
1675 /*
1676 * Make sure the pins are configured for input
1677 * There must be at least five (VRM 9), and possibly 6 (VRM 10)
1678 */
1679 sel = superio_inb(sio_data, W83627THF_GPIO5_IOSR) & 0x3f;
1680 if ((sel & 0x1f) != 0x1f) {
1681 dev_dbg(&pdev->dev, "GPIO5 not configured for VID "
1682 "function\n");
1683 goto exit;
1684 }
1685
1686 dev_info(&pdev->dev, "Reading VID from GPIO5\n");
1687 res = superio_inb(sio_data, W83627THF_GPIO5_DR) & sel;
1688
1689 exit:
1690 superio_exit(sio_data);
1691 return res;
1692 }
1693
w83687thf_read_vid(struct platform_device * pdev)1694 static int w83687thf_read_vid(struct platform_device *pdev)
1695 {
1696 struct w83627hf_sio_data *sio_data = dev_get_platdata(&pdev->dev);
1697 int res = 0xff;
1698
1699 if (superio_enter(sio_data)) {
1700 /*
1701 * Some other driver reserved the address space for itself.
1702 * We don't want to fail driver instantiation because of that,
1703 * so display a warning and keep going.
1704 */
1705 dev_warn(&pdev->dev,
1706 "Can not read VID data: Failed to enable SuperIO access\n");
1707 return res;
1708 }
1709
1710 superio_select(sio_data, W83627HF_LD_HWM);
1711
1712 /* Make sure these GPIO pins are enabled */
1713 if (!(superio_inb(sio_data, W83687THF_VID_EN) & (1 << 2))) {
1714 dev_dbg(&pdev->dev, "VID disabled, no VID function\n");
1715 goto exit;
1716 }
1717
1718 /* Make sure the pins are configured for input */
1719 if (!(superio_inb(sio_data, W83687THF_VID_CFG) & (1 << 4))) {
1720 dev_dbg(&pdev->dev, "VID configured as output, "
1721 "no VID function\n");
1722 goto exit;
1723 }
1724
1725 res = superio_inb(sio_data, W83687THF_VID_DATA) & 0x3f;
1726
1727 exit:
1728 superio_exit(sio_data);
1729 return res;
1730 }
1731
w83627hf_write_value(struct w83627hf_data * data,u16 reg,u16 value)1732 static int w83627hf_write_value(struct w83627hf_data *data, u16 reg, u16 value)
1733 {
1734 int word_sized;
1735
1736 mutex_lock(&data->lock);
1737 word_sized = (((reg & 0xff00) == 0x100)
1738 || ((reg & 0xff00) == 0x200))
1739 && (((reg & 0x00ff) == 0x53)
1740 || ((reg & 0x00ff) == 0x55));
1741 w83627hf_set_bank(data, reg);
1742 outb_p(reg & 0xff, data->addr + W83781D_ADDR_REG_OFFSET);
1743 if (word_sized) {
1744 outb_p(value >> 8,
1745 data->addr + W83781D_DATA_REG_OFFSET);
1746 outb_p((reg & 0xff) + 1,
1747 data->addr + W83781D_ADDR_REG_OFFSET);
1748 }
1749 outb_p(value & 0xff,
1750 data->addr + W83781D_DATA_REG_OFFSET);
1751 w83627hf_reset_bank(data, reg);
1752 mutex_unlock(&data->lock);
1753 return 0;
1754 }
1755
w83627hf_init_device(struct platform_device * pdev)1756 static void w83627hf_init_device(struct platform_device *pdev)
1757 {
1758 struct w83627hf_data *data = platform_get_drvdata(pdev);
1759 int i;
1760 enum chips type = data->type;
1761 u8 tmp;
1762
1763 /* Minimize conflicts with other winbond i2c-only clients... */
1764 /* disable i2c subclients... how to disable main i2c client?? */
1765 /* force i2c address to relatively uncommon address */
1766 if (type == w83627hf) {
1767 w83627hf_write_value(data, W83781D_REG_I2C_SUBADDR, 0x89);
1768 w83627hf_write_value(data, W83781D_REG_I2C_ADDR, force_i2c);
1769 }
1770
1771 /* Read VID only once */
1772 if (type == w83627hf || type == w83637hf) {
1773 int lo = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1774 int hi = w83627hf_read_value(data, W83781D_REG_CHIPID);
1775 data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
1776 } else if (type == w83627thf) {
1777 data->vid = w83627thf_read_gpio5(pdev);
1778 } else if (type == w83687thf) {
1779 data->vid = w83687thf_read_vid(pdev);
1780 }
1781
1782 /* Read VRM & OVT Config only once */
1783 if (type == w83627thf || type == w83637hf || type == w83687thf) {
1784 data->vrm_ovt =
1785 w83627hf_read_value(data, W83627THF_REG_VRM_OVT_CFG);
1786 }
1787
1788 tmp = w83627hf_read_value(data, W83781D_REG_SCFG1);
1789 for (i = 1; i <= 3; i++) {
1790 if (!(tmp & BIT_SCFG1[i - 1])) {
1791 data->sens[i - 1] = 4;
1792 } else {
1793 if (w83627hf_read_value
1794 (data,
1795 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1796 data->sens[i - 1] = 1;
1797 else
1798 data->sens[i - 1] = 2;
1799 }
1800 if ((type == w83697hf) && (i == 2))
1801 break;
1802 }
1803
1804 if(init) {
1805 /* Enable temp2 */
1806 tmp = w83627hf_read_value(data, W83627HF_REG_TEMP2_CONFIG);
1807 if (tmp & 0x01) {
1808 dev_warn(&pdev->dev, "Enabling temp2, readings "
1809 "might not make sense\n");
1810 w83627hf_write_value(data, W83627HF_REG_TEMP2_CONFIG,
1811 tmp & 0xfe);
1812 }
1813
1814 /* Enable temp3 */
1815 if (type != w83697hf) {
1816 tmp = w83627hf_read_value(data,
1817 W83627HF_REG_TEMP3_CONFIG);
1818 if (tmp & 0x01) {
1819 dev_warn(&pdev->dev, "Enabling temp3, "
1820 "readings might not make sense\n");
1821 w83627hf_write_value(data,
1822 W83627HF_REG_TEMP3_CONFIG, tmp & 0xfe);
1823 }
1824 }
1825 }
1826
1827 /* Start monitoring */
1828 w83627hf_write_value(data, W83781D_REG_CONFIG,
1829 (w83627hf_read_value(data,
1830 W83781D_REG_CONFIG) & 0xf7)
1831 | 0x01);
1832
1833 /* Enable VBAT monitoring if needed */
1834 tmp = w83627hf_read_value(data, W83781D_REG_VBAT);
1835 if (!(tmp & 0x01))
1836 w83627hf_write_value(data, W83781D_REG_VBAT, tmp | 0x01);
1837 }
1838
w83627hf_update_fan_div(struct w83627hf_data * data)1839 static void w83627hf_update_fan_div(struct w83627hf_data *data)
1840 {
1841 int reg;
1842
1843 reg = w83627hf_read_value(data, W83781D_REG_VID_FANDIV);
1844 data->fan_div[0] = (reg >> 4) & 0x03;
1845 data->fan_div[1] = (reg >> 6) & 0x03;
1846 if (data->type != w83697hf) {
1847 data->fan_div[2] = (w83627hf_read_value(data,
1848 W83781D_REG_PIN) >> 6) & 0x03;
1849 }
1850 reg = w83627hf_read_value(data, W83781D_REG_VBAT);
1851 data->fan_div[0] |= (reg >> 3) & 0x04;
1852 data->fan_div[1] |= (reg >> 4) & 0x04;
1853 if (data->type != w83697hf)
1854 data->fan_div[2] |= (reg >> 5) & 0x04;
1855 }
1856
w83627hf_update_device(struct device * dev)1857 static struct w83627hf_data *w83627hf_update_device(struct device *dev)
1858 {
1859 struct w83627hf_data *data = dev_get_drvdata(dev);
1860 int i, num_temps = (data->type == w83697hf) ? 2 : 3;
1861 int num_pwms = (data->type == w83697hf) ? 2 : 3;
1862
1863 mutex_lock(&data->update_lock);
1864
1865 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1866 || !data->valid) {
1867 for (i = 0; i <= 8; i++) {
1868 /* skip missing sensors */
1869 if (((data->type == w83697hf) && (i == 1)) ||
1870 ((data->type != w83627hf && data->type != w83697hf)
1871 && (i == 5 || i == 6)))
1872 continue;
1873 data->in[i] =
1874 w83627hf_read_value(data, W83781D_REG_IN(i));
1875 data->in_min[i] =
1876 w83627hf_read_value(data,
1877 W83781D_REG_IN_MIN(i));
1878 data->in_max[i] =
1879 w83627hf_read_value(data,
1880 W83781D_REG_IN_MAX(i));
1881 }
1882 for (i = 0; i <= 2; i++) {
1883 data->fan[i] =
1884 w83627hf_read_value(data, W83627HF_REG_FAN(i));
1885 data->fan_min[i] =
1886 w83627hf_read_value(data,
1887 W83627HF_REG_FAN_MIN(i));
1888 }
1889 for (i = 0; i <= 2; i++) {
1890 u8 tmp = w83627hf_read_value(data,
1891 W836X7HF_REG_PWM(data->type, i));
1892 /* bits 0-3 are reserved in 627THF */
1893 if (data->type == w83627thf)
1894 tmp &= 0xf0;
1895 data->pwm[i] = tmp;
1896 if (i == 1 &&
1897 (data->type == w83627hf || data->type == w83697hf))
1898 break;
1899 }
1900 if (data->type == w83627hf) {
1901 u8 tmp = w83627hf_read_value(data,
1902 W83627HF_REG_PWM_FREQ);
1903 data->pwm_freq[0] = tmp & 0x07;
1904 data->pwm_freq[1] = (tmp >> 4) & 0x07;
1905 } else if (data->type != w83627thf) {
1906 for (i = 1; i <= 3; i++) {
1907 data->pwm_freq[i - 1] =
1908 w83627hf_read_value(data,
1909 W83637HF_REG_PWM_FREQ[i - 1]);
1910 if (i == 2 && (data->type == w83697hf))
1911 break;
1912 }
1913 }
1914 if (data->type != w83627hf) {
1915 for (i = 0; i < num_pwms; i++) {
1916 u8 tmp = w83627hf_read_value(data,
1917 W83627THF_REG_PWM_ENABLE[i]);
1918 data->pwm_enable[i] =
1919 ((tmp >> W83627THF_PWM_ENABLE_SHIFT[i])
1920 & 0x03) + 1;
1921 }
1922 }
1923 for (i = 0; i < num_temps; i++) {
1924 data->temp[i] = w83627hf_read_value(
1925 data, w83627hf_reg_temp[i]);
1926 data->temp_max[i] = w83627hf_read_value(
1927 data, w83627hf_reg_temp_over[i]);
1928 data->temp_max_hyst[i] = w83627hf_read_value(
1929 data, w83627hf_reg_temp_hyst[i]);
1930 }
1931
1932 w83627hf_update_fan_div(data);
1933
1934 data->alarms =
1935 w83627hf_read_value(data, W83781D_REG_ALARM1) |
1936 (w83627hf_read_value(data, W83781D_REG_ALARM2) << 8) |
1937 (w83627hf_read_value(data, W83781D_REG_ALARM3) << 16);
1938 i = w83627hf_read_value(data, W83781D_REG_BEEP_INTS2);
1939 data->beep_mask = (i << 8) |
1940 w83627hf_read_value(data, W83781D_REG_BEEP_INTS1) |
1941 w83627hf_read_value(data, W83781D_REG_BEEP_INTS3) << 16;
1942 data->last_updated = jiffies;
1943 data->valid = 1;
1944 }
1945
1946 mutex_unlock(&data->update_lock);
1947
1948 return data;
1949 }
1950
w83627hf_device_add(unsigned short address,const struct w83627hf_sio_data * sio_data)1951 static int __init w83627hf_device_add(unsigned short address,
1952 const struct w83627hf_sio_data *sio_data)
1953 {
1954 struct resource res = {
1955 .start = address + WINB_REGION_OFFSET,
1956 .end = address + WINB_REGION_OFFSET + WINB_REGION_SIZE - 1,
1957 .name = DRVNAME,
1958 .flags = IORESOURCE_IO,
1959 };
1960 int err;
1961
1962 err = acpi_check_resource_conflict(&res);
1963 if (err)
1964 goto exit;
1965
1966 pdev = platform_device_alloc(DRVNAME, address);
1967 if (!pdev) {
1968 err = -ENOMEM;
1969 pr_err("Device allocation failed\n");
1970 goto exit;
1971 }
1972
1973 err = platform_device_add_resources(pdev, &res, 1);
1974 if (err) {
1975 pr_err("Device resource addition failed (%d)\n", err);
1976 goto exit_device_put;
1977 }
1978
1979 err = platform_device_add_data(pdev, sio_data,
1980 sizeof(struct w83627hf_sio_data));
1981 if (err) {
1982 pr_err("Platform data allocation failed\n");
1983 goto exit_device_put;
1984 }
1985
1986 err = platform_device_add(pdev);
1987 if (err) {
1988 pr_err("Device addition failed (%d)\n", err);
1989 goto exit_device_put;
1990 }
1991
1992 return 0;
1993
1994 exit_device_put:
1995 platform_device_put(pdev);
1996 exit:
1997 return err;
1998 }
1999
sensors_w83627hf_init(void)2000 static int __init sensors_w83627hf_init(void)
2001 {
2002 int err;
2003 unsigned short address;
2004 struct w83627hf_sio_data sio_data;
2005
2006 if (w83627hf_find(0x2e, &address, &sio_data)
2007 && w83627hf_find(0x4e, &address, &sio_data))
2008 return -ENODEV;
2009
2010 err = platform_driver_register(&w83627hf_driver);
2011 if (err)
2012 goto exit;
2013
2014 /* Sets global pdev as a side effect */
2015 err = w83627hf_device_add(address, &sio_data);
2016 if (err)
2017 goto exit_driver;
2018
2019 return 0;
2020
2021 exit_driver:
2022 platform_driver_unregister(&w83627hf_driver);
2023 exit:
2024 return err;
2025 }
2026
sensors_w83627hf_exit(void)2027 static void __exit sensors_w83627hf_exit(void)
2028 {
2029 platform_device_unregister(pdev);
2030 platform_driver_unregister(&w83627hf_driver);
2031 }
2032
2033 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2034 "Philip Edelbrock <phil@netroedge.com>, "
2035 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2036 MODULE_DESCRIPTION("W83627HF driver");
2037 MODULE_LICENSE("GPL");
2038
2039 module_init(sensors_w83627hf_init);
2040 module_exit(sensors_w83627hf_exit);
2041