pc87360: Check for error on sysfs files creation
[deliverable/linux.git] / drivers / hwmon / w83627ehf.c
CommitLineData
08e7e278
JD
1/*
2 w83627ehf - Driver for the hardware monitoring functionality of
3 the Winbond W83627EHF Super-I/O chip
4 Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
08c79950
RM
5 Copyright (C) 2006 Yuan Mu <Ymu@Winbond.com.tw>,
6 Rudolf Marek <r.marek@sh.cvut.cz>
08e7e278
JD
7
8 Shamelessly ripped from the w83627hf driver
9 Copyright (C) 2003 Mark Studebaker
10
11 Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
12 in testing and debugging this driver.
13
8dd2d2ca
JD
14 This driver also supports the W83627EHG, which is the lead-free
15 version of the W83627EHF.
16
08e7e278
JD
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30
31
32 Supports the following chips:
33
08c79950
RM
34 Chip #vin #fan #pwm #temp chip_id man_id
35 w83627ehf 10 5 4 3 0x88,0xa1 0x5ca3
08e7e278
JD
36*/
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/i2c.h>
fde09509 42#include <linux/i2c-isa.h>
943b0830 43#include <linux/hwmon.h>
412fec82 44#include <linux/hwmon-sysfs.h>
943b0830 45#include <linux/err.h>
9a61bf63 46#include <linux/mutex.h>
08e7e278
JD
47#include <asm/io.h>
48#include "lm75.h"
49
2d8672c5
JD
50/* The actual ISA address is read from Super-I/O configuration space */
51static unsigned short address;
08e7e278
JD
52
53/*
54 * Super-I/O constants and functions
55 */
56
57static int REG; /* The register to read/write */
58static 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 0x8840
68#define SIO_ID_MASK 0xFFC0
69
70static inline void
71superio_outb(int reg, int val)
72{
73 outb(reg, REG);
74 outb(val, VAL);
75}
76
77static inline int
78superio_inb(int reg)
79{
80 outb(reg, REG);
81 return inb(VAL);
82}
83
84static inline void
85superio_select(int ld)
86{
87 outb(SIO_REG_LDSEL, REG);
88 outb(ld, VAL);
89}
90
91static inline void
92superio_enter(void)
93{
94 outb(0x87, REG);
95 outb(0x87, REG);
96}
97
98static inline void
99superio_exit(void)
100{
101 outb(0x02, REG);
102 outb(0x02, VAL);
103}
104
105/*
106 * ISA constants
107 */
108
ada0c2f8
PV
109#define REGION_ALIGNMENT ~7
110#define REGION_OFFSET 5
111#define REGION_LENGTH 2
08e7e278
JD
112#define ADDR_REG_OFFSET 5
113#define DATA_REG_OFFSET 6
114
115#define W83627EHF_REG_BANK 0x4E
116#define W83627EHF_REG_CONFIG 0x40
117#define W83627EHF_REG_CHIP_ID 0x49
118#define W83627EHF_REG_MAN_ID 0x4F
119
120static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
121static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
122
cf0676fe
RM
123/* The W83627EHF registers for nr=7,8,9 are in bank 5 */
124#define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
125 (0x554 + (((nr) - 7) * 2)))
126#define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
127 (0x555 + (((nr) - 7) * 2)))
128#define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
129 (0x550 + (nr) - 7))
130
08e7e278
JD
131#define W83627EHF_REG_TEMP1 0x27
132#define W83627EHF_REG_TEMP1_HYST 0x3a
133#define W83627EHF_REG_TEMP1_OVER 0x39
134static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
135static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
136static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
137static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
138
139/* Fan clock dividers are spread over the following five registers */
140#define W83627EHF_REG_FANDIV1 0x47
141#define W83627EHF_REG_FANDIV2 0x4B
142#define W83627EHF_REG_VBAT 0x5D
143#define W83627EHF_REG_DIODE 0x59
144#define W83627EHF_REG_SMI_OVT 0x4C
145
a4589dbb
JD
146#define W83627EHF_REG_ALARM1 0x459
147#define W83627EHF_REG_ALARM2 0x45A
148#define W83627EHF_REG_ALARM3 0x45B
149
08c79950
RM
150/* SmartFan registers */
151/* DC or PWM output fan configuration */
152static const u8 W83627EHF_REG_PWM_ENABLE[] = {
153 0x04, /* SYS FAN0 output mode and PWM mode */
154 0x04, /* CPU FAN0 output mode and PWM mode */
155 0x12, /* AUX FAN mode */
156 0x62, /* CPU fan1 mode */
157};
158
159static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
160static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
161
162/* FAN Duty Cycle, be used to control */
163static const u8 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
164static const u8 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
165static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
166
167
168/* Advanced Fan control, some values are common for all fans */
169static const u8 W83627EHF_REG_FAN_MIN_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
170static const u8 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0C, 0x0D, 0x17, 0x66 };
171
08e7e278
JD
172/*
173 * Conversions
174 */
175
08c79950
RM
176/* 1 is PWM mode, output in ms */
177static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
178{
179 return mode ? 100 * reg : 400 * reg;
180}
181
182static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
183{
184 return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
185 (msec + 200) / 400), 1, 255);
186}
187
08e7e278
JD
188static inline unsigned int
189fan_from_reg(u8 reg, unsigned int div)
190{
191 if (reg == 0 || reg == 255)
192 return 0;
193 return 1350000U / (reg * div);
194}
195
196static inline unsigned int
197div_from_reg(u8 reg)
198{
199 return 1 << reg;
200}
201
202static inline int
203temp1_from_reg(s8 reg)
204{
205 return reg * 1000;
206}
207
208static inline s8
08c79950 209temp1_to_reg(int temp, int min, int max)
08e7e278 210{
08c79950
RM
211 if (temp <= min)
212 return min / 1000;
213 if (temp >= max)
214 return max / 1000;
08e7e278
JD
215 if (temp < 0)
216 return (temp - 500) / 1000;
217 return (temp + 500) / 1000;
218}
219
cf0676fe
RM
220/* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
221
222static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
223
224static inline long in_from_reg(u8 reg, u8 nr)
225{
226 return reg * scale_in[nr];
227}
228
229static inline u8 in_to_reg(u32 val, u8 nr)
230{
231 return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0, 255);
232}
233
08e7e278
JD
234/*
235 * Data structures and manipulation thereof
236 */
237
238struct w83627ehf_data {
239 struct i2c_client client;
943b0830 240 struct class_device *class_dev;
9a61bf63 241 struct mutex lock;
08e7e278 242
9a61bf63 243 struct mutex update_lock;
08e7e278
JD
244 char valid; /* !=0 if following fields are valid */
245 unsigned long last_updated; /* In jiffies */
246
247 /* Register values */
cf0676fe
RM
248 u8 in[10]; /* Register value */
249 u8 in_max[10]; /* Register value */
250 u8 in_min[10]; /* Register value */
08e7e278
JD
251 u8 fan[5];
252 u8 fan_min[5];
253 u8 fan_div[5];
254 u8 has_fan; /* some fan inputs can be disabled */
255 s8 temp1;
256 s8 temp1_max;
257 s8 temp1_max_hyst;
258 s16 temp[2];
259 s16 temp_max[2];
260 s16 temp_max_hyst[2];
a4589dbb 261 u32 alarms;
08c79950
RM
262
263 u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
264 u8 pwm_enable[4]; /* 1->manual
265 2->thermal cruise (also called SmartFan I) */
266 u8 pwm[4];
267 u8 target_temp[4];
268 u8 tolerance[4];
269
270 u8 fan_min_output[4]; /* minimum fan speed */
271 u8 fan_stop_time[4];
08e7e278
JD
272};
273
274static inline int is_word_sized(u16 reg)
275{
276 return (((reg & 0xff00) == 0x100
277 || (reg & 0xff00) == 0x200)
278 && ((reg & 0x00ff) == 0x50
279 || (reg & 0x00ff) == 0x53
280 || (reg & 0x00ff) == 0x55));
281}
282
283/* We assume that the default bank is 0, thus the following two functions do
284 nothing for registers which live in bank 0. For others, they respectively
285 set the bank register to the correct value (before the register is
286 accessed), and back to 0 (afterwards). */
287static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg)
288{
289 if (reg & 0xff00) {
290 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
291 outb_p(reg >> 8, client->addr + DATA_REG_OFFSET);
292 }
293}
294
295static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
296{
297 if (reg & 0xff00) {
298 outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
299 outb_p(0, client->addr + DATA_REG_OFFSET);
300 }
301}
302
303static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
304{
305 struct w83627ehf_data *data = i2c_get_clientdata(client);
306 int res, word_sized = is_word_sized(reg);
307
9a61bf63 308 mutex_lock(&data->lock);
08e7e278
JD
309
310 w83627ehf_set_bank(client, reg);
311 outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
312 res = inb_p(client->addr + DATA_REG_OFFSET);
313 if (word_sized) {
314 outb_p((reg & 0xff) + 1,
315 client->addr + ADDR_REG_OFFSET);
316 res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET);
317 }
318 w83627ehf_reset_bank(client, reg);
319
9a61bf63 320 mutex_unlock(&data->lock);
08e7e278
JD
321
322 return res;
323}
324
325static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
326{
327 struct w83627ehf_data *data = i2c_get_clientdata(client);
328 int word_sized = is_word_sized(reg);
329
9a61bf63 330 mutex_lock(&data->lock);
08e7e278
JD
331
332 w83627ehf_set_bank(client, reg);
333 outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
334 if (word_sized) {
335 outb_p(value >> 8, client->addr + DATA_REG_OFFSET);
336 outb_p((reg & 0xff) + 1,
337 client->addr + ADDR_REG_OFFSET);
338 }
339 outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
340 w83627ehf_reset_bank(client, reg);
341
9a61bf63 342 mutex_unlock(&data->lock);
08e7e278
JD
343 return 0;
344}
345
346/* This function assumes that the caller holds data->update_lock */
347static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
348{
349 struct w83627ehf_data *data = i2c_get_clientdata(client);
350 u8 reg;
351
352 switch (nr) {
353 case 0:
354 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0xcf)
355 | ((data->fan_div[0] & 0x03) << 4);
356 w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
357 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xdf)
358 | ((data->fan_div[0] & 0x04) << 3);
359 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
360 break;
361 case 1:
362 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0x3f)
363 | ((data->fan_div[1] & 0x03) << 6);
364 w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
365 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xbf)
366 | ((data->fan_div[1] & 0x04) << 4);
367 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
368 break;
369 case 2:
370 reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) & 0x3f)
371 | ((data->fan_div[2] & 0x03) << 6);
372 w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg);
373 reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0x7f)
374 | ((data->fan_div[2] & 0x04) << 5);
375 w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
376 break;
377 case 3:
378 reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0xfc)
379 | (data->fan_div[3] & 0x03);
380 w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
381 reg = (w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) & 0x7f)
382 | ((data->fan_div[3] & 0x04) << 5);
383 w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg);
384 break;
385 case 4:
386 reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0x73)
387 | ((data->fan_div[4] & 0x03) << 3)
388 | ((data->fan_div[4] & 0x04) << 5);
389 w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
390 break;
391 }
392}
393
394static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
395{
396 struct i2c_client *client = to_i2c_client(dev);
397 struct w83627ehf_data *data = i2c_get_clientdata(client);
08c79950 398 int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
08e7e278
JD
399 int i;
400
9a61bf63 401 mutex_lock(&data->update_lock);
08e7e278
JD
402
403 if (time_after(jiffies, data->last_updated + HZ)
404 || !data->valid) {
405 /* Fan clock dividers */
406 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
407 data->fan_div[0] = (i >> 4) & 0x03;
408 data->fan_div[1] = (i >> 6) & 0x03;
409 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2);
410 data->fan_div[2] = (i >> 6) & 0x03;
411 i = w83627ehf_read_value(client, W83627EHF_REG_VBAT);
412 data->fan_div[0] |= (i >> 3) & 0x04;
413 data->fan_div[1] |= (i >> 4) & 0x04;
414 data->fan_div[2] |= (i >> 5) & 0x04;
415 if (data->has_fan & ((1 << 3) | (1 << 4))) {
416 i = w83627ehf_read_value(client, W83627EHF_REG_DIODE);
417 data->fan_div[3] = i & 0x03;
418 data->fan_div[4] = ((i >> 2) & 0x03)
419 | ((i >> 5) & 0x04);
420 }
421 if (data->has_fan & (1 << 3)) {
422 i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT);
423 data->fan_div[3] |= (i >> 5) & 0x04;
424 }
425
cf0676fe
RM
426 /* Measured voltages and limits */
427 for (i = 0; i < 10; i++) {
428 data->in[i] = w83627ehf_read_value(client,
429 W83627EHF_REG_IN(i));
430 data->in_min[i] = w83627ehf_read_value(client,
431 W83627EHF_REG_IN_MIN(i));
432 data->in_max[i] = w83627ehf_read_value(client,
433 W83627EHF_REG_IN_MAX(i));
434 }
435
08e7e278
JD
436 /* Measured fan speeds and limits */
437 for (i = 0; i < 5; i++) {
438 if (!(data->has_fan & (1 << i)))
439 continue;
440
441 data->fan[i] = w83627ehf_read_value(client,
442 W83627EHF_REG_FAN[i]);
443 data->fan_min[i] = w83627ehf_read_value(client,
444 W83627EHF_REG_FAN_MIN[i]);
445
446 /* If we failed to measure the fan speed and clock
447 divider can be increased, let's try that for next
448 time */
449 if (data->fan[i] == 0xff
450 && data->fan_div[i] < 0x07) {
451 dev_dbg(&client->dev, "Increasing fan %d "
452 "clock divider from %u to %u\n",
453 i, div_from_reg(data->fan_div[i]),
454 div_from_reg(data->fan_div[i] + 1));
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
08c79950
RM
466 for (i = 0; i < 4; i++) {
467 /* pwmcfg, tolarance mapped for i=0, i=1 to same reg */
468 if (i != 1) {
469 pwmcfg = w83627ehf_read_value(client,
470 W83627EHF_REG_PWM_ENABLE[i]);
471 tolerance = w83627ehf_read_value(client,
472 W83627EHF_REG_TOLERANCE[i]);
473 }
474 data->pwm_mode[i] =
475 ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1)
476 ? 0 : 1;
477 data->pwm_enable[i] =
478 ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
479 & 3) + 1;
480 data->pwm[i] = w83627ehf_read_value(client,
481 W83627EHF_REG_PWM[i]);
482 data->fan_min_output[i] = w83627ehf_read_value(client,
483 W83627EHF_REG_FAN_MIN_OUTPUT[i]);
484 data->fan_stop_time[i] = w83627ehf_read_value(client,
485 W83627EHF_REG_FAN_STOP_TIME[i]);
486 data->target_temp[i] =
487 w83627ehf_read_value(client,
488 W83627EHF_REG_TARGET[i]) &
489 (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
490 data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0))
491 & 0x0f;
492 }
493
08e7e278
JD
494 /* Measured temperatures and limits */
495 data->temp1 = w83627ehf_read_value(client,
496 W83627EHF_REG_TEMP1);
497 data->temp1_max = w83627ehf_read_value(client,
498 W83627EHF_REG_TEMP1_OVER);
499 data->temp1_max_hyst = w83627ehf_read_value(client,
500 W83627EHF_REG_TEMP1_HYST);
501 for (i = 0; i < 2; i++) {
502 data->temp[i] = w83627ehf_read_value(client,
503 W83627EHF_REG_TEMP[i]);
504 data->temp_max[i] = w83627ehf_read_value(client,
505 W83627EHF_REG_TEMP_OVER[i]);
506 data->temp_max_hyst[i] = w83627ehf_read_value(client,
507 W83627EHF_REG_TEMP_HYST[i]);
508 }
509
a4589dbb
JD
510 data->alarms = w83627ehf_read_value(client,
511 W83627EHF_REG_ALARM1) |
512 (w83627ehf_read_value(client,
513 W83627EHF_REG_ALARM2) << 8) |
514 (w83627ehf_read_value(client,
515 W83627EHF_REG_ALARM3) << 16);
516
08e7e278
JD
517 data->last_updated = jiffies;
518 data->valid = 1;
519 }
520
9a61bf63 521 mutex_unlock(&data->update_lock);
08e7e278
JD
522 return data;
523}
524
525/*
526 * Sysfs callback functions
527 */
cf0676fe
RM
528#define show_in_reg(reg) \
529static ssize_t \
530show_##reg(struct device *dev, struct device_attribute *attr, \
531 char *buf) \
532{ \
533 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
534 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
535 int nr = sensor_attr->index; \
536 return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
537}
538show_in_reg(in)
539show_in_reg(in_min)
540show_in_reg(in_max)
541
542#define store_in_reg(REG, reg) \
543static ssize_t \
544store_in_##reg (struct device *dev, struct device_attribute *attr, \
545 const char *buf, size_t count) \
546{ \
547 struct i2c_client *client = to_i2c_client(dev); \
548 struct w83627ehf_data *data = i2c_get_clientdata(client); \
549 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
550 int nr = sensor_attr->index; \
551 u32 val = simple_strtoul(buf, NULL, 10); \
552 \
553 mutex_lock(&data->update_lock); \
554 data->in_##reg[nr] = in_to_reg(val, nr); \
555 w83627ehf_write_value(client, W83627EHF_REG_IN_##REG(nr), \
556 data->in_##reg[nr]); \
557 mutex_unlock(&data->update_lock); \
558 return count; \
559}
560
561store_in_reg(MIN, min)
562store_in_reg(MAX, max)
563
a4589dbb
JD
564static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
565{
566 struct w83627ehf_data *data = w83627ehf_update_device(dev);
567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
569 return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
570}
571
cf0676fe
RM
572static struct sensor_device_attribute sda_in_input[] = {
573 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
574 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
575 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
576 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
577 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
578 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
579 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
580 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
581 SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
582 SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
583};
584
a4589dbb
JD
585static struct sensor_device_attribute sda_in_alarm[] = {
586 SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
587 SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
588 SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
589 SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
590 SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
591 SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
592 SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
593 SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
594 SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
595 SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
596};
597
cf0676fe
RM
598static struct sensor_device_attribute sda_in_min[] = {
599 SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
600 SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
601 SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
602 SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
603 SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
604 SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
605 SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
606 SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
607 SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
608 SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
609};
610
611static struct sensor_device_attribute sda_in_max[] = {
612 SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
613 SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
614 SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
615 SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
616 SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
617 SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
618 SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
619 SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
620 SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
621 SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
622};
623
624static void device_create_file_in(struct device *dev, int i)
625{
626 device_create_file(dev, &sda_in_input[i].dev_attr);
a4589dbb 627 device_create_file(dev, &sda_in_alarm[i].dev_attr);
cf0676fe
RM
628 device_create_file(dev, &sda_in_min[i].dev_attr);
629 device_create_file(dev, &sda_in_max[i].dev_attr);
630}
08e7e278
JD
631
632#define show_fan_reg(reg) \
633static ssize_t \
412fec82
YM
634show_##reg(struct device *dev, struct device_attribute *attr, \
635 char *buf) \
08e7e278
JD
636{ \
637 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
412fec82
YM
638 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
639 int nr = sensor_attr->index; \
08e7e278
JD
640 return sprintf(buf, "%d\n", \
641 fan_from_reg(data->reg[nr], \
642 div_from_reg(data->fan_div[nr]))); \
643}
644show_fan_reg(fan);
645show_fan_reg(fan_min);
646
647static ssize_t
412fec82
YM
648show_fan_div(struct device *dev, struct device_attribute *attr,
649 char *buf)
08e7e278
JD
650{
651 struct w83627ehf_data *data = w83627ehf_update_device(dev);
412fec82
YM
652 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
653 int nr = sensor_attr->index;
654 return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
08e7e278
JD
655}
656
657static ssize_t
412fec82
YM
658store_fan_min(struct device *dev, struct device_attribute *attr,
659 const char *buf, size_t count)
08e7e278
JD
660{
661 struct i2c_client *client = to_i2c_client(dev);
662 struct w83627ehf_data *data = i2c_get_clientdata(client);
412fec82
YM
663 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
664 int nr = sensor_attr->index;
08e7e278
JD
665 unsigned int val = simple_strtoul(buf, NULL, 10);
666 unsigned int reg;
667 u8 new_div;
668
9a61bf63 669 mutex_lock(&data->update_lock);
08e7e278
JD
670 if (!val) {
671 /* No min limit, alarm disabled */
672 data->fan_min[nr] = 255;
673 new_div = data->fan_div[nr]; /* No change */
674 dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
675 } else if ((reg = 1350000U / val) >= 128 * 255) {
676 /* Speed below this value cannot possibly be represented,
677 even with the highest divider (128) */
678 data->fan_min[nr] = 254;
679 new_div = 7; /* 128 == (1 << 7) */
680 dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
681 "minimum\n", nr + 1, val, fan_from_reg(254, 128));
682 } else if (!reg) {
683 /* Speed above this value cannot possibly be represented,
684 even with the lowest divider (1) */
685 data->fan_min[nr] = 1;
686 new_div = 0; /* 1 == (1 << 0) */
687 dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
b9110b1c 688 "maximum\n", nr + 1, val, fan_from_reg(1, 1));
08e7e278
JD
689 } else {
690 /* Automatically pick the best divider, i.e. the one such
691 that the min limit will correspond to a register value
692 in the 96..192 range */
693 new_div = 0;
694 while (reg > 192 && new_div < 7) {
695 reg >>= 1;
696 new_div++;
697 }
698 data->fan_min[nr] = reg;
699 }
700
701 /* Write both the fan clock divider (if it changed) and the new
702 fan min (unconditionally) */
703 if (new_div != data->fan_div[nr]) {
704 if (new_div > data->fan_div[nr])
705 data->fan[nr] >>= (data->fan_div[nr] - new_div);
706 else
707 data->fan[nr] <<= (new_div - data->fan_div[nr]);
708
709 dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
710 nr + 1, div_from_reg(data->fan_div[nr]),
711 div_from_reg(new_div));
712 data->fan_div[nr] = new_div;
713 w83627ehf_write_fan_div(client, nr);
714 }
715 w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
716 data->fan_min[nr]);
9a61bf63 717 mutex_unlock(&data->update_lock);
08e7e278
JD
718
719 return count;
720}
721
412fec82
YM
722static struct sensor_device_attribute sda_fan_input[] = {
723 SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
724 SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
725 SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
726 SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
727 SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
728};
08e7e278 729
a4589dbb
JD
730static struct sensor_device_attribute sda_fan_alarm[] = {
731 SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
732 SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
733 SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
734 SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
735 SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
736};
737
412fec82
YM
738static struct sensor_device_attribute sda_fan_min[] = {
739 SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
740 store_fan_min, 0),
741 SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
742 store_fan_min, 1),
743 SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
744 store_fan_min, 2),
745 SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
746 store_fan_min, 3),
747 SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
748 store_fan_min, 4),
749};
08e7e278 750
412fec82
YM
751static struct sensor_device_attribute sda_fan_div[] = {
752 SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
753 SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
754 SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
755 SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
756 SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
757};
758
759static void device_create_file_fan(struct device *dev, int i)
760{
761 device_create_file(dev, &sda_fan_input[i].dev_attr);
a4589dbb 762 device_create_file(dev, &sda_fan_alarm[i].dev_attr);
412fec82
YM
763 device_create_file(dev, &sda_fan_div[i].dev_attr);
764 device_create_file(dev, &sda_fan_min[i].dev_attr);
765}
08e7e278
JD
766
767#define show_temp1_reg(reg) \
768static ssize_t \
6f637a64
GKH
769show_##reg(struct device *dev, struct device_attribute *attr, \
770 char *buf) \
08e7e278
JD
771{ \
772 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
773 return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
774}
775show_temp1_reg(temp1);
776show_temp1_reg(temp1_max);
777show_temp1_reg(temp1_max_hyst);
778
779#define store_temp1_reg(REG, reg) \
780static ssize_t \
6f637a64
GKH
781store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
782 const char *buf, size_t count) \
08e7e278
JD
783{ \
784 struct i2c_client *client = to_i2c_client(dev); \
785 struct w83627ehf_data *data = i2c_get_clientdata(client); \
786 u32 val = simple_strtoul(buf, NULL, 10); \
787 \
9a61bf63 788 mutex_lock(&data->update_lock); \
08c79950 789 data->temp1_##reg = temp1_to_reg(val, -128000, 127000); \
08e7e278
JD
790 w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
791 data->temp1_##reg); \
9a61bf63 792 mutex_unlock(&data->update_lock); \
08e7e278
JD
793 return count; \
794}
795store_temp1_reg(OVER, max);
796store_temp1_reg(HYST, max_hyst);
797
08e7e278
JD
798#define show_temp_reg(reg) \
799static ssize_t \
412fec82
YM
800show_##reg(struct device *dev, struct device_attribute *attr, \
801 char *buf) \
08e7e278
JD
802{ \
803 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
412fec82
YM
804 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
805 int nr = sensor_attr->index; \
08e7e278
JD
806 return sprintf(buf, "%d\n", \
807 LM75_TEMP_FROM_REG(data->reg[nr])); \
808}
809show_temp_reg(temp);
810show_temp_reg(temp_max);
811show_temp_reg(temp_max_hyst);
812
813#define store_temp_reg(REG, reg) \
814static ssize_t \
412fec82
YM
815store_##reg(struct device *dev, struct device_attribute *attr, \
816 const char *buf, size_t count) \
08e7e278
JD
817{ \
818 struct i2c_client *client = to_i2c_client(dev); \
819 struct w83627ehf_data *data = i2c_get_clientdata(client); \
412fec82
YM
820 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
821 int nr = sensor_attr->index; \
08e7e278
JD
822 u32 val = simple_strtoul(buf, NULL, 10); \
823 \
9a61bf63 824 mutex_lock(&data->update_lock); \
08e7e278
JD
825 data->reg[nr] = LM75_TEMP_TO_REG(val); \
826 w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
827 data->reg[nr]); \
9a61bf63 828 mutex_unlock(&data->update_lock); \
08e7e278
JD
829 return count; \
830}
831store_temp_reg(OVER, temp_max);
832store_temp_reg(HYST, temp_max_hyst);
833
412fec82
YM
834static struct sensor_device_attribute sda_temp[] = {
835 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp1, NULL, 0),
836 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0),
837 SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 1),
838 SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp1_max,
839 store_temp1_max, 0),
840 SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
841 store_temp_max, 0),
842 SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
843 store_temp_max, 1),
844 SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp1_max_hyst,
845 store_temp1_max_hyst, 0),
846 SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
847 store_temp_max_hyst, 0),
848 SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
849 store_temp_max_hyst, 1),
a4589dbb
JD
850 SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
851 SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
852 SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
412fec82 853};
08e7e278 854
08c79950
RM
855#define show_pwm_reg(reg) \
856static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \
857 char *buf) \
858{ \
859 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
860 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
861 int nr = sensor_attr->index; \
862 return sprintf(buf, "%d\n", data->reg[nr]); \
863}
864
865show_pwm_reg(pwm_mode)
866show_pwm_reg(pwm_enable)
867show_pwm_reg(pwm)
868
869static ssize_t
870store_pwm_mode(struct device *dev, struct device_attribute *attr,
871 const char *buf, size_t count)
872{
873 struct i2c_client *client = to_i2c_client(dev);
874 struct w83627ehf_data *data = i2c_get_clientdata(client);
875 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
876 int nr = sensor_attr->index;
877 u32 val = simple_strtoul(buf, NULL, 10);
878 u16 reg;
879
880 if (val > 1)
881 return -EINVAL;
882 mutex_lock(&data->update_lock);
883 reg = w83627ehf_read_value(client, W83627EHF_REG_PWM_ENABLE[nr]);
884 data->pwm_mode[nr] = val;
885 reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
886 if (!val)
887 reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
888 w83627ehf_write_value(client, W83627EHF_REG_PWM_ENABLE[nr], reg);
889 mutex_unlock(&data->update_lock);
890 return count;
891}
892
893static ssize_t
894store_pwm(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
896{
897 struct i2c_client *client = to_i2c_client(dev);
898 struct w83627ehf_data *data = i2c_get_clientdata(client);
899 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
900 int nr = sensor_attr->index;
901 u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 255);
902
903 mutex_lock(&data->update_lock);
904 data->pwm[nr] = val;
905 w83627ehf_write_value(client, W83627EHF_REG_PWM[nr], val);
906 mutex_unlock(&data->update_lock);
907 return count;
908}
909
910static ssize_t
911store_pwm_enable(struct device *dev, struct device_attribute *attr,
912 const char *buf, size_t count)
913{
914 struct i2c_client *client = to_i2c_client(dev);
915 struct w83627ehf_data *data = i2c_get_clientdata(client);
916 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
917 int nr = sensor_attr->index;
918 u32 val = simple_strtoul(buf, NULL, 10);
919 u16 reg;
920
921 if (!val || (val > 2)) /* only modes 1 and 2 are supported */
922 return -EINVAL;
923 mutex_lock(&data->update_lock);
924 reg = w83627ehf_read_value(client, W83627EHF_REG_PWM_ENABLE[nr]);
925 data->pwm_enable[nr] = val;
926 reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
927 reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
928 w83627ehf_write_value(client, W83627EHF_REG_PWM_ENABLE[nr], reg);
929 mutex_unlock(&data->update_lock);
930 return count;
931}
932
933
934#define show_tol_temp(reg) \
935static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
936 char *buf) \
937{ \
938 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
939 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
940 int nr = sensor_attr->index; \
941 return sprintf(buf, "%d\n", temp1_from_reg(data->reg[nr])); \
942}
943
944show_tol_temp(tolerance)
945show_tol_temp(target_temp)
946
947static ssize_t
948store_target_temp(struct device *dev, struct device_attribute *attr,
949 const char *buf, size_t count)
950{
951 struct i2c_client *client = to_i2c_client(dev);
952 struct w83627ehf_data *data = i2c_get_clientdata(client);
953 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
954 int nr = sensor_attr->index;
955 u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 127000);
956
957 mutex_lock(&data->update_lock);
958 data->target_temp[nr] = val;
959 w83627ehf_write_value(client, W83627EHF_REG_TARGET[nr], val);
960 mutex_unlock(&data->update_lock);
961 return count;
962}
963
964static ssize_t
965store_tolerance(struct device *dev, struct device_attribute *attr,
966 const char *buf, size_t count)
967{
968 struct i2c_client *client = to_i2c_client(dev);
969 struct w83627ehf_data *data = i2c_get_clientdata(client);
970 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
971 int nr = sensor_attr->index;
972 u16 reg;
973 /* Limit the temp to 0C - 15C */
974 u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 15000);
975
976 mutex_lock(&data->update_lock);
977 reg = w83627ehf_read_value(client, W83627EHF_REG_TOLERANCE[nr]);
978 data->tolerance[nr] = val;
979 if (nr == 1)
980 reg = (reg & 0x0f) | (val << 4);
981 else
982 reg = (reg & 0xf0) | val;
983 w83627ehf_write_value(client, W83627EHF_REG_TOLERANCE[nr], reg);
984 mutex_unlock(&data->update_lock);
985 return count;
986}
987
988static struct sensor_device_attribute sda_pwm[] = {
989 SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
990 SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
991 SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
992 SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
993};
994
995static struct sensor_device_attribute sda_pwm_mode[] = {
996 SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
997 store_pwm_mode, 0),
998 SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
999 store_pwm_mode, 1),
1000 SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1001 store_pwm_mode, 2),
1002 SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
1003 store_pwm_mode, 3),
1004};
1005
1006static struct sensor_device_attribute sda_pwm_enable[] = {
1007 SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1008 store_pwm_enable, 0),
1009 SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1010 store_pwm_enable, 1),
1011 SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1012 store_pwm_enable, 2),
1013 SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1014 store_pwm_enable, 3),
1015};
1016
1017static struct sensor_device_attribute sda_target_temp[] = {
1018 SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
1019 store_target_temp, 0),
1020 SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
1021 store_target_temp, 1),
1022 SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
1023 store_target_temp, 2),
1024 SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
1025 store_target_temp, 3),
1026};
1027
1028static struct sensor_device_attribute sda_tolerance[] = {
1029 SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1030 store_tolerance, 0),
1031 SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1032 store_tolerance, 1),
1033 SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1034 store_tolerance, 2),
1035 SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
1036 store_tolerance, 3),
1037};
1038
1039static void device_create_file_pwm(struct device *dev, int i)
1040{
1041 device_create_file(dev, &sda_pwm[i].dev_attr);
1042 device_create_file(dev, &sda_pwm_mode[i].dev_attr);
1043 device_create_file(dev, &sda_pwm_enable[i].dev_attr);
1044 device_create_file(dev, &sda_target_temp[i].dev_attr);
1045 device_create_file(dev, &sda_tolerance[i].dev_attr);
1046}
1047
1048/* Smart Fan registers */
1049
1050#define fan_functions(reg, REG) \
1051static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1052 char *buf) \
1053{ \
1054 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1055 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1056 int nr = sensor_attr->index; \
1057 return sprintf(buf, "%d\n", data->reg[nr]); \
1058}\
1059static ssize_t \
1060store_##reg(struct device *dev, struct device_attribute *attr, \
1061 const char *buf, size_t count) \
1062{\
1063 struct i2c_client *client = to_i2c_client(dev); \
1064 struct w83627ehf_data *data = i2c_get_clientdata(client); \
1065 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1066 int nr = sensor_attr->index; \
1067 u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 1, 255); \
1068 mutex_lock(&data->update_lock); \
1069 data->reg[nr] = val; \
1070 w83627ehf_write_value(client, W83627EHF_REG_##REG[nr], val); \
1071 mutex_unlock(&data->update_lock); \
1072 return count; \
1073}
1074
1075fan_functions(fan_min_output, FAN_MIN_OUTPUT)
1076
1077#define fan_time_functions(reg, REG) \
1078static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
1079 char *buf) \
1080{ \
1081 struct w83627ehf_data *data = w83627ehf_update_device(dev); \
1082 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1083 int nr = sensor_attr->index; \
1084 return sprintf(buf, "%d\n", \
1085 step_time_from_reg(data->reg[nr], data->pwm_mode[nr])); \
1086} \
1087\
1088static ssize_t \
1089store_##reg(struct device *dev, struct device_attribute *attr, \
1090 const char *buf, size_t count) \
1091{ \
1092 struct i2c_client *client = to_i2c_client(dev); \
1093 struct w83627ehf_data *data = i2c_get_clientdata(client); \
1094 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
1095 int nr = sensor_attr->index; \
1096 u8 val = step_time_to_reg(simple_strtoul(buf, NULL, 10), \
1097 data->pwm_mode[nr]); \
1098 mutex_lock(&data->update_lock); \
1099 data->reg[nr] = val; \
1100 w83627ehf_write_value(client, W83627EHF_REG_##REG[nr], val); \
1101 mutex_unlock(&data->update_lock); \
1102 return count; \
1103} \
1104
1105fan_time_functions(fan_stop_time, FAN_STOP_TIME)
1106
1107
1108static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
1109 SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1110 store_fan_stop_time, 3),
1111 SENSOR_ATTR(pwm4_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1112 store_fan_min_output, 3),
1113};
1114
1115static struct sensor_device_attribute sda_sf3_arrays[] = {
1116 SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1117 store_fan_stop_time, 0),
1118 SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1119 store_fan_stop_time, 1),
1120 SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
1121 store_fan_stop_time, 2),
1122 SENSOR_ATTR(pwm1_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1123 store_fan_min_output, 0),
1124 SENSOR_ATTR(pwm2_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1125 store_fan_min_output, 1),
1126 SENSOR_ATTR(pwm3_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
1127 store_fan_min_output, 2),
1128};
1129
08e7e278
JD
1130/*
1131 * Driver and client management
1132 */
1133
1134static struct i2c_driver w83627ehf_driver;
1135
1136static void w83627ehf_init_client(struct i2c_client *client)
1137{
1138 int i;
1139 u8 tmp;
1140
1141 /* Start monitoring is needed */
1142 tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG);
1143 if (!(tmp & 0x01))
1144 w83627ehf_write_value(client, W83627EHF_REG_CONFIG,
1145 tmp | 0x01);
1146
1147 /* Enable temp2 and temp3 if needed */
1148 for (i = 0; i < 2; i++) {
1149 tmp = w83627ehf_read_value(client,
1150 W83627EHF_REG_TEMP_CONFIG[i]);
1151 if (tmp & 0x01)
1152 w83627ehf_write_value(client,
1153 W83627EHF_REG_TEMP_CONFIG[i],
1154 tmp & 0xfe);
1155 }
1156}
1157
2d8672c5 1158static int w83627ehf_detect(struct i2c_adapter *adapter)
08e7e278
JD
1159{
1160 struct i2c_client *client;
1161 struct w83627ehf_data *data;
412fec82 1162 struct device *dev;
08c79950 1163 u8 fan4pin, fan5pin;
08e7e278
JD
1164 int i, err = 0;
1165
ada0c2f8 1166 if (!request_region(address + REGION_OFFSET, REGION_LENGTH,
cdaf7934 1167 w83627ehf_driver.driver.name)) {
08e7e278
JD
1168 err = -EBUSY;
1169 goto exit;
1170 }
1171
ba9c2e8d 1172 if (!(data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
08e7e278
JD
1173 err = -ENOMEM;
1174 goto exit_release;
1175 }
08e7e278
JD
1176
1177 client = &data->client;
1178 i2c_set_clientdata(client, data);
1179 client->addr = address;
9a61bf63 1180 mutex_init(&data->lock);
08e7e278
JD
1181 client->adapter = adapter;
1182 client->driver = &w83627ehf_driver;
1183 client->flags = 0;
412fec82 1184 dev = &client->dev;
08e7e278
JD
1185
1186 strlcpy(client->name, "w83627ehf", I2C_NAME_SIZE);
1187 data->valid = 0;
9a61bf63 1188 mutex_init(&data->update_lock);
08e7e278
JD
1189
1190 /* Tell the i2c layer a new client has arrived */
1191 if ((err = i2c_attach_client(client)))
1192 goto exit_free;
1193
1194 /* Initialize the chip */
1195 w83627ehf_init_client(client);
1196
1197 /* A few vars need to be filled upon startup */
1198 for (i = 0; i < 5; i++)
1199 data->fan_min[i] = w83627ehf_read_value(client,
1200 W83627EHF_REG_FAN_MIN[i]);
1201
08c79950
RM
1202 /* fan4 and fan5 share some pins with the GPIO and serial flash */
1203
1204 superio_enter();
1205 fan5pin = superio_inb(0x24) & 0x2;
1206 fan4pin = superio_inb(0x29) & 0x6;
1207 superio_exit();
1208
08e7e278
JD
1209 /* It looks like fan4 and fan5 pins can be alternatively used
1210 as fan on/off switches */
08c79950 1211
08e7e278
JD
1212 data->has_fan = 0x07; /* fan1, fan2 and fan3 */
1213 i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
08c79950 1214 if ((i & (1 << 2)) && (!fan4pin))
08e7e278 1215 data->has_fan |= (1 << 3);
08c79950 1216 if ((i & (1 << 0)) && (!fan5pin))
08e7e278
JD
1217 data->has_fan |= (1 << 4);
1218
1219 /* Register sysfs hooks */
412fec82 1220 data->class_dev = hwmon_device_register(dev);
943b0830
MH
1221 if (IS_ERR(data->class_dev)) {
1222 err = PTR_ERR(data->class_dev);
1223 goto exit_detach;
1224 }
1225
08c79950
RM
1226 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
1227 device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
1228
1229 /* if fan4 is enabled create the sf3 files for it */
1230 if (data->has_fan & (1 << 3))
1231 for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
1232 device_create_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
1233
cf0676fe
RM
1234 for (i = 0; i < 10; i++)
1235 device_create_file_in(dev, i);
1236
412fec82 1237 for (i = 0; i < 5; i++) {
08c79950 1238 if (data->has_fan & (1 << i)) {
412fec82 1239 device_create_file_fan(dev, i);
08c79950
RM
1240 if (i != 4) /* we have only 4 pwm */
1241 device_create_file_pwm(dev, i);
1242 }
08e7e278 1243 }
08c79950 1244
412fec82
YM
1245 for (i = 0; i < ARRAY_SIZE(sda_temp); i++)
1246 device_create_file(dev, &sda_temp[i].dev_attr);
08e7e278
JD
1247
1248 return 0;
1249
943b0830
MH
1250exit_detach:
1251 i2c_detach_client(client);
08e7e278
JD
1252exit_free:
1253 kfree(data);
1254exit_release:
ada0c2f8 1255 release_region(address + REGION_OFFSET, REGION_LENGTH);
08e7e278
JD
1256exit:
1257 return err;
1258}
1259
08e7e278
JD
1260static int w83627ehf_detach_client(struct i2c_client *client)
1261{
943b0830 1262 struct w83627ehf_data *data = i2c_get_clientdata(client);
08e7e278
JD
1263 int err;
1264
943b0830
MH
1265 hwmon_device_unregister(data->class_dev);
1266
7bef5594 1267 if ((err = i2c_detach_client(client)))
08e7e278 1268 return err;
ada0c2f8 1269 release_region(client->addr + REGION_OFFSET, REGION_LENGTH);
943b0830 1270 kfree(data);
08e7e278
JD
1271
1272 return 0;
1273}
1274
1275static struct i2c_driver w83627ehf_driver = {
cdaf7934 1276 .driver = {
87218842 1277 .owner = THIS_MODULE,
cdaf7934
LR
1278 .name = "w83627ehf",
1279 },
2d8672c5 1280 .attach_adapter = w83627ehf_detect,
08e7e278
JD
1281 .detach_client = w83627ehf_detach_client,
1282};
1283
2d8672c5 1284static int __init w83627ehf_find(int sioaddr, unsigned short *addr)
08e7e278
JD
1285{
1286 u16 val;
1287
1288 REG = sioaddr;
1289 VAL = sioaddr + 1;
1290 superio_enter();
1291
1292 val = (superio_inb(SIO_REG_DEVID) << 8)
1293 | superio_inb(SIO_REG_DEVID + 1);
1294 if ((val & SIO_ID_MASK) != SIO_W83627EHF_ID) {
1295 superio_exit();
1296 return -ENODEV;
1297 }
1298
1299 superio_select(W83627EHF_LD_HWM);
1300 val = (superio_inb(SIO_REG_ADDR) << 8)
1301 | superio_inb(SIO_REG_ADDR + 1);
ada0c2f8 1302 *addr = val & REGION_ALIGNMENT;
2d8672c5 1303 if (*addr == 0) {
08e7e278
JD
1304 superio_exit();
1305 return -ENODEV;
1306 }
1307
1308 /* Activate logical device if needed */
1309 val = superio_inb(SIO_REG_ENABLE);
1310 if (!(val & 0x01))
1311 superio_outb(SIO_REG_ENABLE, val | 0x01);
1312
1313 superio_exit();
1314 return 0;
1315}
1316
1317static int __init sensors_w83627ehf_init(void)
1318{
2d8672c5
JD
1319 if (w83627ehf_find(0x2e, &address)
1320 && w83627ehf_find(0x4e, &address))
08e7e278
JD
1321 return -ENODEV;
1322
fde09509 1323 return i2c_isa_add_driver(&w83627ehf_driver);
08e7e278
JD
1324}
1325
1326static void __exit sensors_w83627ehf_exit(void)
1327{
fde09509 1328 i2c_isa_del_driver(&w83627ehf_driver);
08e7e278
JD
1329}
1330
1331MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1332MODULE_DESCRIPTION("W83627EHF driver");
1333MODULE_LICENSE("GPL");
1334
1335module_init(sensors_w83627ehf_init);
1336module_exit(sensors_w83627ehf_exit);
This page took 0.217376 seconds and 5 git commands to generate.