2 * dme1737.c - Driver for the SMSC DME1737, Asus A8000, and SMSC SCH311x
3 * Super-I/O chips integrated hardware monitoring features.
4 * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com>
6 * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
7 * the chip registers if a DME1737 (or A8000) is found and the ISA bus if a
8 * SCH311x chip is found. Both types of chips have very similar hardware
9 * monitoring capabilities but differ in the way they can be accessed.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/platform_device.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/hwmon-vid.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
39 /* ISA device, if found */
40 static struct platform_device
*pdev
;
42 /* Module load parameters */
43 static int force_start
;
44 module_param(force_start
, bool, 0);
45 MODULE_PARM_DESC(force_start
, "Force the chip to start monitoring inputs");
47 static unsigned short force_id
;
48 module_param(force_id
, ushort
, 0);
49 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
51 /* Addresses to scan */
52 static unsigned short normal_i2c
[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
54 /* Insmod parameters */
55 I2C_CLIENT_INSMOD_1(dme1737
);
57 /* ---------------------------------------------------------------------
60 * The sensors are defined as follows:
62 * Voltages Temperatures
63 * -------- ------------
64 * in0 +5VTR (+5V stdby) temp1 Remote diode 1
65 * in1 Vccp (proc core) temp2 Internal temp
66 * in2 VCC (internal +3.3V) temp3 Remote diode 2
69 * in5 VTR (+3.3V stby)
72 * --------------------------------------------------------------------- */
74 /* Voltages (in) numbered 0-6 (ix) */
75 #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \
77 #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
79 #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
82 /* Temperatures (temp) numbered 0-2 (ix) */
83 #define DME1737_REG_TEMP(ix) (0x25 + (ix))
84 #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
85 #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
86 #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
89 /* Voltage and temperature LSBs
90 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
91 * IN_TEMP_LSB(0) = [in5, in6]
92 * IN_TEMP_LSB(1) = [temp3, temp1]
93 * IN_TEMP_LSB(2) = [in4, temp2]
94 * IN_TEMP_LSB(3) = [in3, in0]
95 * IN_TEMP_LSB(4) = [in2, in1] */
96 #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
97 static const u8 DME1737_REG_IN_LSB
[] = {3, 4, 4, 3, 2, 0, 0};
98 static const u8 DME1737_REG_IN_LSB_SHL
[] = {4, 4, 0, 0, 0, 0, 4};
99 static const u8 DME1737_REG_TEMP_LSB
[] = {1, 2, 1};
100 static const u8 DME1737_REG_TEMP_LSB_SHL
[] = {4, 4, 0};
102 /* Fans numbered 0-5 (ix) */
103 #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
105 #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
107 #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
109 #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
111 /* PWMs numbered 0-2, 4-5 (ix) */
112 #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
114 #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
115 #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
116 #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
118 /* The layout of the ramp rate registers is different from the other pwm
119 * registers. The bits for the 3 PWMs are stored in 2 registers:
120 * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
121 * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
122 #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
124 /* Thermal zones 0-2 */
125 #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
126 #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
127 /* The layout of the hysteresis registers is different from the other zone
128 * registers. The bits for the 3 zones are stored in 2 registers:
129 * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
130 * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
131 #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
133 /* Alarm registers and bit mapping
134 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
135 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
136 #define DME1737_REG_ALARM1 0x41
137 #define DME1737_REG_ALARM2 0x42
138 #define DME1737_REG_ALARM3 0x83
139 static const u8 DME1737_BIT_ALARM_IN
[] = {0, 1, 2, 3, 8, 16, 17};
140 static const u8 DME1737_BIT_ALARM_TEMP
[] = {4, 5, 6};
141 static const u8 DME1737_BIT_ALARM_FAN
[] = {10, 11, 12, 13, 22, 23};
143 /* Miscellaneous registers */
144 #define DME1737_REG_DEVICE 0x3d
145 #define DME1737_REG_COMPANY 0x3e
146 #define DME1737_REG_VERSTEP 0x3f
147 #define DME1737_REG_CONFIG 0x40
148 #define DME1737_REG_CONFIG2 0x7f
149 #define DME1737_REG_VID 0x43
150 #define DME1737_REG_TACH_PWM 0x81
152 /* ---------------------------------------------------------------------
154 * --------------------------------------------------------------------- */
156 /* Chip identification */
157 #define DME1737_COMPANY_SMSC 0x5c
158 #define DME1737_VERSTEP 0x88
159 #define DME1737_VERSTEP_MASK 0xf8
160 #define SCH311X_DEVICE 0x8c
162 /* Length of ISA address segment */
163 #define DME1737_EXTENT 2
165 /* ---------------------------------------------------------------------
166 * Data structures and manipulation thereof
167 * --------------------------------------------------------------------- */
169 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
170 the driver field to differentiate between I2C and ISA chips. */
171 struct dme1737_data
{
172 struct i2c_client client
;
173 struct device
*hwmon_dev
;
175 struct mutex update_lock
;
176 int valid
; /* !=0 if following fields are valid */
177 unsigned long last_update
; /* in jiffies */
178 unsigned long last_vbat
; /* in jiffies */
185 /* Register values */
212 /* Nominal voltage values */
213 static const int IN_NOMINAL
[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
216 * Voltage inputs have 16 bits resolution, limit values have 8 bits
218 static inline int IN_FROM_REG(int reg
, int ix
, int res
)
220 return (reg
* IN_NOMINAL
[ix
] + (3 << (res
- 3))) / (3 << (res
- 2));
223 static inline int IN_TO_REG(int val
, int ix
)
225 return SENSORS_LIMIT((val
* 192 + IN_NOMINAL
[ix
] / 2) /
226 IN_NOMINAL
[ix
], 0, 255);
230 * The register values represent temperatures in 2's complement notation from
231 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
232 * values have 8 bits resolution. */
233 static inline int TEMP_FROM_REG(int reg
, int res
)
235 return (reg
* 1000) >> (res
- 8);
238 static inline int TEMP_TO_REG(int val
)
240 return SENSORS_LIMIT((val
< 0 ? val
- 500 : val
+ 500) / 1000,
244 /* Temperature range */
245 static const int TEMP_RANGE
[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
246 10000, 13333, 16000, 20000, 26666, 32000,
247 40000, 53333, 80000};
249 static inline int TEMP_RANGE_FROM_REG(int reg
)
251 return TEMP_RANGE
[(reg
>> 4) & 0x0f];
254 static int TEMP_RANGE_TO_REG(int val
, int reg
)
258 for (i
= 15; i
> 0; i
--) {
259 if (val
> (TEMP_RANGE
[i
] + TEMP_RANGE
[i
- 1] + 1) / 2) {
264 return (reg
& 0x0f) | (i
<< 4);
267 /* Temperature hysteresis
269 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
270 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
271 static inline int TEMP_HYST_FROM_REG(int reg
, int ix
)
273 return (((ix
== 1) ? reg
: reg
>> 4) & 0x0f) * 1000;
276 static inline int TEMP_HYST_TO_REG(int val
, int ix
, int reg
)
278 int hyst
= SENSORS_LIMIT((val
+ 500) / 1000, 0, 15);
280 return (ix
== 1) ? (reg
& 0xf0) | hyst
: (reg
& 0x0f) | (hyst
<< 4);
284 static inline int FAN_FROM_REG(int reg
, int tpc
)
286 return (reg
== 0 || reg
== 0xffff) ? 0 :
287 (tpc
== 0) ? 90000 * 60 / reg
: tpc
* reg
;
290 static inline int FAN_TO_REG(int val
, int tpc
)
292 return SENSORS_LIMIT((tpc
== 0) ? 90000 * 60 / val
: val
/ tpc
,
296 /* Fan TPC (tach pulse count)
297 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
298 * is configured in legacy (non-tpc) mode */
299 static inline int FAN_TPC_FROM_REG(int reg
)
301 return (reg
& 0x20) ? 0 : 60 >> (reg
& 0x03);
305 * The type of a fan is expressed in number of pulses-per-revolution that it
307 static inline int FAN_TYPE_FROM_REG(int reg
)
309 int edge
= (reg
>> 1) & 0x03;
311 return (edge
> 0) ? 1 << (edge
- 1) : 0;
314 static inline int FAN_TYPE_TO_REG(int val
, int reg
)
316 int edge
= (val
== 4) ? 3 : val
;
318 return (reg
& 0xf9) | (edge
<< 1);
322 static const int FAN_MAX
[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
325 static int FAN_MAX_FROM_REG(int reg
)
329 for (i
= 10; i
> 0; i
--) {
330 if (reg
== FAN_MAX
[i
]) {
335 return 1000 + i
* 500;
338 static int FAN_MAX_TO_REG(int val
)
342 for (i
= 10; i
> 0; i
--) {
343 if (val
> (1000 + (i
- 1) * 500)) {
352 * Register to enable mapping:
353 * 000: 2 fan on zone 1 auto
354 * 001: 2 fan on zone 2 auto
355 * 010: 2 fan on zone 3 auto
357 * 100: -1 fan disabled
358 * 101: 2 fan on hottest of zones 2,3 auto
359 * 110: 2 fan on hottest of zones 1,2,3 auto
360 * 111: 1 fan in manual mode */
361 static inline int PWM_EN_FROM_REG(int reg
)
363 static const int en
[] = {2, 2, 2, 0, -1, 2, 2, 1};
365 return en
[(reg
>> 5) & 0x07];
368 static inline int PWM_EN_TO_REG(int val
, int reg
)
370 int en
= (val
== 1) ? 7 : 3;
372 return (reg
& 0x1f) | ((en
& 0x07) << 5);
375 /* PWM auto channels zone
376 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
377 * corresponding to zone x+1):
378 * 000: 001 fan on zone 1 auto
379 * 001: 010 fan on zone 2 auto
380 * 010: 100 fan on zone 3 auto
381 * 011: 000 fan full on
382 * 100: 000 fan disabled
383 * 101: 110 fan on hottest of zones 2,3 auto
384 * 110: 111 fan on hottest of zones 1,2,3 auto
385 * 111: 000 fan in manual mode */
386 static inline int PWM_ACZ_FROM_REG(int reg
)
388 static const int acz
[] = {1, 2, 4, 0, 0, 6, 7, 0};
390 return acz
[(reg
>> 5) & 0x07];
393 static inline int PWM_ACZ_TO_REG(int val
, int reg
)
395 int acz
= (val
== 4) ? 2 : val
- 1;
397 return (reg
& 0x1f) | ((acz
& 0x07) << 5);
401 static const int PWM_FREQ
[] = {11, 15, 22, 29, 35, 44, 59, 88,
402 15000, 20000, 30000, 25000, 0, 0, 0, 0};
404 static inline int PWM_FREQ_FROM_REG(int reg
)
406 return PWM_FREQ
[reg
& 0x0f];
409 static int PWM_FREQ_TO_REG(int val
, int reg
)
413 /* the first two cases are special - stupid chip design! */
416 } else if (val
> 22500) {
419 for (i
= 9; i
> 0; i
--) {
420 if (val
> (PWM_FREQ
[i
] + PWM_FREQ
[i
- 1] + 1) / 2) {
426 return (reg
& 0xf0) | i
;
431 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
432 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
433 static const u8 PWM_RR
[] = {206, 104, 69, 41, 26, 18, 10, 5};
435 static inline int PWM_RR_FROM_REG(int reg
, int ix
)
437 int rr
= (ix
== 1) ? reg
>> 4 : reg
;
439 return (rr
& 0x08) ? PWM_RR
[rr
& 0x07] : 0;
442 static int PWM_RR_TO_REG(int val
, int ix
, int reg
)
446 for (i
= 0; i
< 7; i
++) {
447 if (val
> (PWM_RR
[i
] + PWM_RR
[i
+ 1] + 1) / 2) {
452 return (ix
== 1) ? (reg
& 0x8f) | (i
<< 4) : (reg
& 0xf8) | i
;
455 /* PWM ramp rate enable */
456 static inline int PWM_RR_EN_FROM_REG(int reg
, int ix
)
458 return PWM_RR_FROM_REG(reg
, ix
) ? 1 : 0;
461 static inline int PWM_RR_EN_TO_REG(int val
, int ix
, int reg
)
463 int en
= (ix
== 1) ? 0x80 : 0x08;
465 return val
? reg
| en
: reg
& ~en
;
469 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
470 * the register layout). */
471 static inline int PWM_OFF_FROM_REG(int reg
, int ix
)
473 return (reg
>> (ix
+ 5)) & 0x01;
476 static inline int PWM_OFF_TO_REG(int val
, int ix
, int reg
)
478 return (reg
& ~(1 << (ix
+ 5))) | ((val
& 0x01) << (ix
+ 5));
481 /* ---------------------------------------------------------------------
484 * ISA access is performed through an index/data register pair and needs to
485 * be protected by a mutex during runtime (not required for initialization).
486 * We use data->update_lock for this and need to ensure that we acquire it
487 * before calling dme1737_read or dme1737_write.
488 * --------------------------------------------------------------------- */
490 static u8
dme1737_read(struct i2c_client
*client
, u8 reg
)
494 if (client
->driver
) { /* I2C device */
495 val
= i2c_smbus_read_byte_data(client
, reg
);
498 dev_warn(&client
->dev
, "Read from register "
499 "0x%02x failed! Please report to the driver "
500 "maintainer.\n", reg
);
502 } else { /* ISA device */
503 outb(reg
, client
->addr
);
504 val
= inb(client
->addr
+ 1);
510 static s32
dme1737_write(struct i2c_client
*client
, u8 reg
, u8 val
)
514 if (client
->driver
) { /* I2C device */
515 res
= i2c_smbus_write_byte_data(client
, reg
, val
);
518 dev_warn(&client
->dev
, "Write to register "
519 "0x%02x failed! Please report to the driver "
520 "maintainer.\n", reg
);
522 } else { /* ISA device */
523 outb(reg
, client
->addr
);
524 outb(val
, client
->addr
+ 1);
530 static struct dme1737_data
*dme1737_update_device(struct device
*dev
)
532 struct dme1737_data
*data
= dev_get_drvdata(dev
);
533 struct i2c_client
*client
= &data
->client
;
537 mutex_lock(&data
->update_lock
);
539 /* Enable a Vbat monitoring cycle every 10 mins */
540 if (time_after(jiffies
, data
->last_vbat
+ 600 * HZ
) || !data
->valid
) {
541 dme1737_write(client
, DME1737_REG_CONFIG
, dme1737_read(client
,
542 DME1737_REG_CONFIG
) | 0x10);
543 data
->last_vbat
= jiffies
;
546 /* Sample register contents every 1 sec */
547 if (time_after(jiffies
, data
->last_update
+ HZ
) || !data
->valid
) {
548 data
->vid
= dme1737_read(client
, DME1737_REG_VID
) & 0x3f;
550 /* In (voltage) registers */
551 for (ix
= 0; ix
< ARRAY_SIZE(data
->in
); ix
++) {
552 /* Voltage inputs are stored as 16 bit values even
553 * though they have only 12 bits resolution. This is
554 * to make it consistent with the temp inputs. */
555 data
->in
[ix
] = dme1737_read(client
,
556 DME1737_REG_IN(ix
)) << 8;
557 data
->in_min
[ix
] = dme1737_read(client
,
558 DME1737_REG_IN_MIN(ix
));
559 data
->in_max
[ix
] = dme1737_read(client
,
560 DME1737_REG_IN_MAX(ix
));
564 for (ix
= 0; ix
< ARRAY_SIZE(data
->temp
); ix
++) {
565 /* Temp inputs are stored as 16 bit values even
566 * though they have only 12 bits resolution. This is
567 * to take advantage of implicit conversions between
568 * register values (2's complement) and temp values
569 * (signed decimal). */
570 data
->temp
[ix
] = dme1737_read(client
,
571 DME1737_REG_TEMP(ix
)) << 8;
572 data
->temp_min
[ix
] = dme1737_read(client
,
573 DME1737_REG_TEMP_MIN(ix
));
574 data
->temp_max
[ix
] = dme1737_read(client
,
575 DME1737_REG_TEMP_MAX(ix
));
576 data
->temp_offset
[ix
] = dme1737_read(client
,
577 DME1737_REG_TEMP_OFFSET(ix
));
580 /* In and temp LSB registers
581 * The LSBs are latched when the MSBs are read, so the order in
582 * which the registers are read (MSB first, then LSB) is
584 for (ix
= 0; ix
< ARRAY_SIZE(lsb
); ix
++) {
585 lsb
[ix
] = dme1737_read(client
,
586 DME1737_REG_IN_TEMP_LSB(ix
));
588 for (ix
= 0; ix
< ARRAY_SIZE(data
->in
); ix
++) {
589 data
->in
[ix
] |= (lsb
[DME1737_REG_IN_LSB
[ix
]] <<
590 DME1737_REG_IN_LSB_SHL
[ix
]) & 0xf0;
592 for (ix
= 0; ix
< ARRAY_SIZE(data
->temp
); ix
++) {
593 data
->temp
[ix
] |= (lsb
[DME1737_REG_TEMP_LSB
[ix
]] <<
594 DME1737_REG_TEMP_LSB_SHL
[ix
]) & 0xf0;
598 for (ix
= 0; ix
< ARRAY_SIZE(data
->fan
); ix
++) {
599 /* Skip reading registers if optional fans are not
601 if (!(data
->has_fan
& (1 << ix
))) {
604 data
->fan
[ix
] = dme1737_read(client
,
605 DME1737_REG_FAN(ix
));
606 data
->fan
[ix
] |= dme1737_read(client
,
607 DME1737_REG_FAN(ix
) + 1) << 8;
608 data
->fan_min
[ix
] = dme1737_read(client
,
609 DME1737_REG_FAN_MIN(ix
));
610 data
->fan_min
[ix
] |= dme1737_read(client
,
611 DME1737_REG_FAN_MIN(ix
) + 1) << 8;
612 data
->fan_opt
[ix
] = dme1737_read(client
,
613 DME1737_REG_FAN_OPT(ix
));
614 /* fan_max exists only for fan[5-6] */
616 data
->fan_max
[ix
- 4] = dme1737_read(client
,
617 DME1737_REG_FAN_MAX(ix
));
622 for (ix
= 0; ix
< ARRAY_SIZE(data
->pwm
); ix
++) {
623 /* Skip reading registers if optional PWMs are not
625 if (!(data
->has_pwm
& (1 << ix
))) {
628 data
->pwm
[ix
] = dme1737_read(client
,
629 DME1737_REG_PWM(ix
));
630 data
->pwm_freq
[ix
] = dme1737_read(client
,
631 DME1737_REG_PWM_FREQ(ix
));
632 /* pwm_config and pwm_min exist only for pwm[1-3] */
634 data
->pwm_config
[ix
] = dme1737_read(client
,
635 DME1737_REG_PWM_CONFIG(ix
));
636 data
->pwm_min
[ix
] = dme1737_read(client
,
637 DME1737_REG_PWM_MIN(ix
));
640 for (ix
= 0; ix
< ARRAY_SIZE(data
->pwm_rr
); ix
++) {
641 data
->pwm_rr
[ix
] = dme1737_read(client
,
642 DME1737_REG_PWM_RR(ix
));
645 /* Thermal zone registers */
646 for (ix
= 0; ix
< ARRAY_SIZE(data
->zone_low
); ix
++) {
647 data
->zone_low
[ix
] = dme1737_read(client
,
648 DME1737_REG_ZONE_LOW(ix
));
649 data
->zone_abs
[ix
] = dme1737_read(client
,
650 DME1737_REG_ZONE_ABS(ix
));
652 for (ix
= 0; ix
< ARRAY_SIZE(data
->zone_hyst
); ix
++) {
653 data
->zone_hyst
[ix
] = dme1737_read(client
,
654 DME1737_REG_ZONE_HYST(ix
));
657 /* Alarm registers */
658 data
->alarms
= dme1737_read(client
,
660 /* Bit 7 tells us if the other alarm registers are non-zero and
661 * therefore also need to be read */
662 if (data
->alarms
& 0x80) {
663 data
->alarms
|= dme1737_read(client
,
664 DME1737_REG_ALARM2
) << 8;
665 data
->alarms
|= dme1737_read(client
,
666 DME1737_REG_ALARM3
) << 16;
669 /* The ISA chips require explicit clearing of alarm bits.
670 * Don't worry, an alarm will come back if the condition
671 * that causes it still exists */
672 if (!client
->driver
) {
673 if (data
->alarms
& 0xff0000) {
674 dme1737_write(client
, DME1737_REG_ALARM3
,
677 if (data
->alarms
& 0xff00) {
678 dme1737_write(client
, DME1737_REG_ALARM2
,
681 if (data
->alarms
& 0xff) {
682 dme1737_write(client
, DME1737_REG_ALARM1
,
687 data
->last_update
= jiffies
;
691 mutex_unlock(&data
->update_lock
);
696 /* ---------------------------------------------------------------------
697 * Voltage sysfs attributes
699 * --------------------------------------------------------------------- */
701 #define SYS_IN_INPUT 0
704 #define SYS_IN_ALARM 3
706 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
709 struct dme1737_data
*data
= dme1737_update_device(dev
);
710 struct sensor_device_attribute_2
711 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
712 int ix
= sensor_attr_2
->index
;
713 int fn
= sensor_attr_2
->nr
;
718 res
= IN_FROM_REG(data
->in
[ix
], ix
, 16);
721 res
= IN_FROM_REG(data
->in_min
[ix
], ix
, 8);
724 res
= IN_FROM_REG(data
->in_max
[ix
], ix
, 8);
727 res
= (data
->alarms
>> DME1737_BIT_ALARM_IN
[ix
]) & 0x01;
731 dev_dbg(dev
, "Unknown function %d.\n", fn
);
734 return sprintf(buf
, "%d\n", res
);
737 static ssize_t
set_in(struct device
*dev
, struct device_attribute
*attr
,
738 const char *buf
, size_t count
)
740 struct dme1737_data
*data
= dev_get_drvdata(dev
);
741 struct i2c_client
*client
= &data
->client
;
742 struct sensor_device_attribute_2
743 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
744 int ix
= sensor_attr_2
->index
;
745 int fn
= sensor_attr_2
->nr
;
746 long val
= simple_strtol(buf
, NULL
, 10);
748 mutex_lock(&data
->update_lock
);
751 data
->in_min
[ix
] = IN_TO_REG(val
, ix
);
752 dme1737_write(client
, DME1737_REG_IN_MIN(ix
),
756 data
->in_max
[ix
] = IN_TO_REG(val
, ix
);
757 dme1737_write(client
, DME1737_REG_IN_MAX(ix
),
761 dev_dbg(dev
, "Unknown function %d.\n", fn
);
763 mutex_unlock(&data
->update_lock
);
768 /* ---------------------------------------------------------------------
769 * Temperature sysfs attributes
771 * --------------------------------------------------------------------- */
773 #define SYS_TEMP_INPUT 0
774 #define SYS_TEMP_MIN 1
775 #define SYS_TEMP_MAX 2
776 #define SYS_TEMP_OFFSET 3
777 #define SYS_TEMP_ALARM 4
778 #define SYS_TEMP_FAULT 5
780 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
783 struct dme1737_data
*data
= dme1737_update_device(dev
);
784 struct sensor_device_attribute_2
785 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
786 int ix
= sensor_attr_2
->index
;
787 int fn
= sensor_attr_2
->nr
;
792 res
= TEMP_FROM_REG(data
->temp
[ix
], 16);
795 res
= TEMP_FROM_REG(data
->temp_min
[ix
], 8);
798 res
= TEMP_FROM_REG(data
->temp_max
[ix
], 8);
800 case SYS_TEMP_OFFSET
:
801 res
= TEMP_FROM_REG(data
->temp_offset
[ix
], 8);
804 res
= (data
->alarms
>> DME1737_BIT_ALARM_TEMP
[ix
]) & 0x01;
807 res
= (((u16
)data
->temp
[ix
] & 0xff00) == 0x8000);
811 dev_dbg(dev
, "Unknown function %d.\n", fn
);
814 return sprintf(buf
, "%d\n", res
);
817 static ssize_t
set_temp(struct device
*dev
, struct device_attribute
*attr
,
818 const char *buf
, size_t count
)
820 struct dme1737_data
*data
= dev_get_drvdata(dev
);
821 struct i2c_client
*client
= &data
->client
;
822 struct sensor_device_attribute_2
823 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
824 int ix
= sensor_attr_2
->index
;
825 int fn
= sensor_attr_2
->nr
;
826 long val
= simple_strtol(buf
, NULL
, 10);
828 mutex_lock(&data
->update_lock
);
831 data
->temp_min
[ix
] = TEMP_TO_REG(val
);
832 dme1737_write(client
, DME1737_REG_TEMP_MIN(ix
),
836 data
->temp_max
[ix
] = TEMP_TO_REG(val
);
837 dme1737_write(client
, DME1737_REG_TEMP_MAX(ix
),
840 case SYS_TEMP_OFFSET
:
841 data
->temp_offset
[ix
] = TEMP_TO_REG(val
);
842 dme1737_write(client
, DME1737_REG_TEMP_OFFSET(ix
),
843 data
->temp_offset
[ix
]);
846 dev_dbg(dev
, "Unknown function %d.\n", fn
);
848 mutex_unlock(&data
->update_lock
);
853 /* ---------------------------------------------------------------------
854 * Zone sysfs attributes
856 * --------------------------------------------------------------------- */
858 #define SYS_ZONE_AUTO_CHANNELS_TEMP 0
859 #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
860 #define SYS_ZONE_AUTO_POINT1_TEMP 2
861 #define SYS_ZONE_AUTO_POINT2_TEMP 3
862 #define SYS_ZONE_AUTO_POINT3_TEMP 4
864 static ssize_t
show_zone(struct device
*dev
, struct device_attribute
*attr
,
867 struct dme1737_data
*data
= dme1737_update_device(dev
);
868 struct sensor_device_attribute_2
869 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
870 int ix
= sensor_attr_2
->index
;
871 int fn
= sensor_attr_2
->nr
;
875 case SYS_ZONE_AUTO_CHANNELS_TEMP
:
876 /* check config2 for non-standard temp-to-zone mapping */
877 if ((ix
== 1) && (data
->config2
& 0x02)) {
883 case SYS_ZONE_AUTO_POINT1_TEMP_HYST
:
884 res
= TEMP_FROM_REG(data
->zone_low
[ix
], 8) -
885 TEMP_HYST_FROM_REG(data
->zone_hyst
[ix
== 2], ix
);
887 case SYS_ZONE_AUTO_POINT1_TEMP
:
888 res
= TEMP_FROM_REG(data
->zone_low
[ix
], 8);
890 case SYS_ZONE_AUTO_POINT2_TEMP
:
891 /* pwm_freq holds the temp range bits in the upper nibble */
892 res
= TEMP_FROM_REG(data
->zone_low
[ix
], 8) +
893 TEMP_RANGE_FROM_REG(data
->pwm_freq
[ix
]);
895 case SYS_ZONE_AUTO_POINT3_TEMP
:
896 res
= TEMP_FROM_REG(data
->zone_abs
[ix
], 8);
900 dev_dbg(dev
, "Unknown function %d.\n", fn
);
903 return sprintf(buf
, "%d\n", res
);
906 static ssize_t
set_zone(struct device
*dev
, struct device_attribute
*attr
,
907 const char *buf
, size_t count
)
909 struct dme1737_data
*data
= dev_get_drvdata(dev
);
910 struct i2c_client
*client
= &data
->client
;
911 struct sensor_device_attribute_2
912 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
913 int ix
= sensor_attr_2
->index
;
914 int fn
= sensor_attr_2
->nr
;
915 long val
= simple_strtol(buf
, NULL
, 10);
917 mutex_lock(&data
->update_lock
);
919 case SYS_ZONE_AUTO_POINT1_TEMP_HYST
:
920 /* Refresh the cache */
921 data
->zone_low
[ix
] = dme1737_read(client
,
922 DME1737_REG_ZONE_LOW(ix
));
923 /* Modify the temp hyst value */
924 data
->zone_hyst
[ix
== 2] = TEMP_HYST_TO_REG(
925 TEMP_FROM_REG(data
->zone_low
[ix
], 8) -
926 val
, ix
, dme1737_read(client
,
927 DME1737_REG_ZONE_HYST(ix
== 2)));
928 dme1737_write(client
, DME1737_REG_ZONE_HYST(ix
== 2),
929 data
->zone_hyst
[ix
== 2]);
931 case SYS_ZONE_AUTO_POINT1_TEMP
:
932 data
->zone_low
[ix
] = TEMP_TO_REG(val
);
933 dme1737_write(client
, DME1737_REG_ZONE_LOW(ix
),
936 case SYS_ZONE_AUTO_POINT2_TEMP
:
937 /* Refresh the cache */
938 data
->zone_low
[ix
] = dme1737_read(client
,
939 DME1737_REG_ZONE_LOW(ix
));
940 /* Modify the temp range value (which is stored in the upper
941 * nibble of the pwm_freq register) */
942 data
->pwm_freq
[ix
] = TEMP_RANGE_TO_REG(val
-
943 TEMP_FROM_REG(data
->zone_low
[ix
], 8),
945 DME1737_REG_PWM_FREQ(ix
)));
946 dme1737_write(client
, DME1737_REG_PWM_FREQ(ix
),
949 case SYS_ZONE_AUTO_POINT3_TEMP
:
950 data
->zone_abs
[ix
] = TEMP_TO_REG(val
);
951 dme1737_write(client
, DME1737_REG_ZONE_ABS(ix
),
955 dev_dbg(dev
, "Unknown function %d.\n", fn
);
957 mutex_unlock(&data
->update_lock
);
962 /* ---------------------------------------------------------------------
963 * Fan sysfs attributes
965 * --------------------------------------------------------------------- */
967 #define SYS_FAN_INPUT 0
968 #define SYS_FAN_MIN 1
969 #define SYS_FAN_MAX 2
970 #define SYS_FAN_ALARM 3
971 #define SYS_FAN_TYPE 4
973 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
976 struct dme1737_data
*data
= dme1737_update_device(dev
);
977 struct sensor_device_attribute_2
978 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
979 int ix
= sensor_attr_2
->index
;
980 int fn
= sensor_attr_2
->nr
;
985 res
= FAN_FROM_REG(data
->fan
[ix
],
987 FAN_TPC_FROM_REG(data
->fan_opt
[ix
]));
990 res
= FAN_FROM_REG(data
->fan_min
[ix
],
992 FAN_TPC_FROM_REG(data
->fan_opt
[ix
]));
995 /* only valid for fan[5-6] */
996 res
= FAN_MAX_FROM_REG(data
->fan_max
[ix
- 4]);
999 res
= (data
->alarms
>> DME1737_BIT_ALARM_FAN
[ix
]) & 0x01;
1002 /* only valid for fan[1-4] */
1003 res
= FAN_TYPE_FROM_REG(data
->fan_opt
[ix
]);
1007 dev_dbg(dev
, "Unknown function %d.\n", fn
);
1010 return sprintf(buf
, "%d\n", res
);
1013 static ssize_t
set_fan(struct device
*dev
, struct device_attribute
*attr
,
1014 const char *buf
, size_t count
)
1016 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1017 struct i2c_client
*client
= &data
->client
;
1018 struct sensor_device_attribute_2
1019 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
1020 int ix
= sensor_attr_2
->index
;
1021 int fn
= sensor_attr_2
->nr
;
1022 long val
= simple_strtol(buf
, NULL
, 10);
1024 mutex_lock(&data
->update_lock
);
1028 data
->fan_min
[ix
] = FAN_TO_REG(val
, 0);
1030 /* Refresh the cache */
1031 data
->fan_opt
[ix
] = dme1737_read(client
,
1032 DME1737_REG_FAN_OPT(ix
));
1033 /* Modify the fan min value */
1034 data
->fan_min
[ix
] = FAN_TO_REG(val
,
1035 FAN_TPC_FROM_REG(data
->fan_opt
[ix
]));
1037 dme1737_write(client
, DME1737_REG_FAN_MIN(ix
),
1038 data
->fan_min
[ix
] & 0xff);
1039 dme1737_write(client
, DME1737_REG_FAN_MIN(ix
) + 1,
1040 data
->fan_min
[ix
] >> 8);
1043 /* Only valid for fan[5-6] */
1044 data
->fan_max
[ix
- 4] = FAN_MAX_TO_REG(val
);
1045 dme1737_write(client
, DME1737_REG_FAN_MAX(ix
),
1046 data
->fan_max
[ix
- 4]);
1049 /* Only valid for fan[1-4] */
1050 if (!(val
== 1 || val
== 2 || val
== 4)) {
1052 dev_warn(dev
, "Fan type value %ld not "
1053 "supported. Choose one of 1, 2, or 4.\n",
1057 data
->fan_opt
[ix
] = FAN_TYPE_TO_REG(val
, dme1737_read(client
,
1058 DME1737_REG_FAN_OPT(ix
)));
1059 dme1737_write(client
, DME1737_REG_FAN_OPT(ix
),
1063 dev_dbg(dev
, "Unknown function %d.\n", fn
);
1066 mutex_unlock(&data
->update_lock
);
1071 /* ---------------------------------------------------------------------
1072 * PWM sysfs attributes
1074 * --------------------------------------------------------------------- */
1077 #define SYS_PWM_FREQ 1
1078 #define SYS_PWM_ENABLE 2
1079 #define SYS_PWM_RAMP_RATE 3
1080 #define SYS_PWM_AUTO_CHANNELS_ZONE 4
1081 #define SYS_PWM_AUTO_PWM_MIN 5
1082 #define SYS_PWM_AUTO_POINT1_PWM 6
1083 #define SYS_PWM_AUTO_POINT2_PWM 7
1085 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
1088 struct dme1737_data
*data
= dme1737_update_device(dev
);
1089 struct sensor_device_attribute_2
1090 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
1091 int ix
= sensor_attr_2
->index
;
1092 int fn
= sensor_attr_2
->nr
;
1097 if (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == 0) {
1100 res
= data
->pwm
[ix
];
1104 res
= PWM_FREQ_FROM_REG(data
->pwm_freq
[ix
]);
1106 case SYS_PWM_ENABLE
:
1108 res
= 1; /* pwm[5-6] hard-wired to manual mode */
1110 res
= PWM_EN_FROM_REG(data
->pwm_config
[ix
]);
1113 case SYS_PWM_RAMP_RATE
:
1114 /* Only valid for pwm[1-3] */
1115 res
= PWM_RR_FROM_REG(data
->pwm_rr
[ix
> 0], ix
);
1117 case SYS_PWM_AUTO_CHANNELS_ZONE
:
1118 /* Only valid for pwm[1-3] */
1119 if (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == 2) {
1120 res
= PWM_ACZ_FROM_REG(data
->pwm_config
[ix
]);
1122 res
= data
->pwm_acz
[ix
];
1125 case SYS_PWM_AUTO_PWM_MIN
:
1126 /* Only valid for pwm[1-3] */
1127 if (PWM_OFF_FROM_REG(data
->pwm_rr
[0], ix
)) {
1128 res
= data
->pwm_min
[ix
];
1133 case SYS_PWM_AUTO_POINT1_PWM
:
1134 /* Only valid for pwm[1-3] */
1135 res
= data
->pwm_min
[ix
];
1137 case SYS_PWM_AUTO_POINT2_PWM
:
1138 /* Only valid for pwm[1-3] */
1139 res
= 255; /* hard-wired */
1143 dev_dbg(dev
, "Unknown function %d.\n", fn
);
1146 return sprintf(buf
, "%d\n", res
);
1149 static struct attribute
*dme1737_attr_pwm
[];
1150 static void dme1737_chmod_file(struct device
*, struct attribute
*, mode_t
);
1152 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
1153 const char *buf
, size_t count
)
1155 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1156 struct i2c_client
*client
= &data
->client
;
1157 struct sensor_device_attribute_2
1158 *sensor_attr_2
= to_sensor_dev_attr_2(attr
);
1159 int ix
= sensor_attr_2
->index
;
1160 int fn
= sensor_attr_2
->nr
;
1161 long val
= simple_strtol(buf
, NULL
, 10);
1163 mutex_lock(&data
->update_lock
);
1166 data
->pwm
[ix
] = SENSORS_LIMIT(val
, 0, 255);
1167 dme1737_write(client
, DME1737_REG_PWM(ix
), data
->pwm
[ix
]);
1170 data
->pwm_freq
[ix
] = PWM_FREQ_TO_REG(val
, dme1737_read(client
,
1171 DME1737_REG_PWM_FREQ(ix
)));
1172 dme1737_write(client
, DME1737_REG_PWM_FREQ(ix
),
1173 data
->pwm_freq
[ix
]);
1175 case SYS_PWM_ENABLE
:
1176 /* Only valid for pwm[1-3] */
1177 if (val
< 0 || val
> 2) {
1179 dev_warn(dev
, "PWM enable %ld not "
1180 "supported. Choose one of 0, 1, or 2.\n",
1184 /* Refresh the cache */
1185 data
->pwm_config
[ix
] = dme1737_read(client
,
1186 DME1737_REG_PWM_CONFIG(ix
));
1187 if (val
== PWM_EN_FROM_REG(data
->pwm_config
[ix
])) {
1188 /* Bail out if no change */
1191 /* Do some housekeeping if we are currently in auto mode */
1192 if (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == 2) {
1193 /* Save the current zone channel assignment */
1194 data
->pwm_acz
[ix
] = PWM_ACZ_FROM_REG(
1195 data
->pwm_config
[ix
]);
1196 /* Save the current ramp rate state and disable it */
1197 data
->pwm_rr
[ix
> 0] = dme1737_read(client
,
1198 DME1737_REG_PWM_RR(ix
> 0));
1199 data
->pwm_rr_en
&= ~(1 << ix
);
1200 if (PWM_RR_EN_FROM_REG(data
->pwm_rr
[ix
> 0], ix
)) {
1201 data
->pwm_rr_en
|= (1 << ix
);
1202 data
->pwm_rr
[ix
> 0] = PWM_RR_EN_TO_REG(0, ix
,
1203 data
->pwm_rr
[ix
> 0]);
1204 dme1737_write(client
,
1205 DME1737_REG_PWM_RR(ix
> 0),
1206 data
->pwm_rr
[ix
> 0]);
1209 /* Set the new PWM mode */
1212 /* Change permissions of pwm[ix] to read-only */
1213 dme1737_chmod_file(dev
, dme1737_attr_pwm
[ix
],
1215 /* Turn fan fully on */
1216 data
->pwm_config
[ix
] = PWM_EN_TO_REG(0,
1217 data
->pwm_config
[ix
]);
1218 dme1737_write(client
, DME1737_REG_PWM_CONFIG(ix
),
1219 data
->pwm_config
[ix
]);
1222 /* Turn on manual mode */
1223 data
->pwm_config
[ix
] = PWM_EN_TO_REG(1,
1224 data
->pwm_config
[ix
]);
1225 dme1737_write(client
, DME1737_REG_PWM_CONFIG(ix
),
1226 data
->pwm_config
[ix
]);
1227 /* Change permissions of pwm[ix] to read-writeable */
1228 dme1737_chmod_file(dev
, dme1737_attr_pwm
[ix
],
1232 /* Change permissions of pwm[ix] to read-only */
1233 dme1737_chmod_file(dev
, dme1737_attr_pwm
[ix
],
1235 /* Turn on auto mode using the saved zone channel
1237 data
->pwm_config
[ix
] = PWM_ACZ_TO_REG(
1239 data
->pwm_config
[ix
]);
1240 dme1737_write(client
, DME1737_REG_PWM_CONFIG(ix
),
1241 data
->pwm_config
[ix
]);
1242 /* Enable PWM ramp rate if previously enabled */
1243 if (data
->pwm_rr_en
& (1 << ix
)) {
1244 data
->pwm_rr
[ix
> 0] = PWM_RR_EN_TO_REG(1, ix
,
1245 dme1737_read(client
,
1246 DME1737_REG_PWM_RR(ix
> 0)));
1247 dme1737_write(client
,
1248 DME1737_REG_PWM_RR(ix
> 0),
1249 data
->pwm_rr
[ix
> 0]);
1254 case SYS_PWM_RAMP_RATE
:
1255 /* Only valid for pwm[1-3] */
1256 /* Refresh the cache */
1257 data
->pwm_config
[ix
] = dme1737_read(client
,
1258 DME1737_REG_PWM_CONFIG(ix
));
1259 data
->pwm_rr
[ix
> 0] = dme1737_read(client
,
1260 DME1737_REG_PWM_RR(ix
> 0));
1261 /* Set the ramp rate value */
1263 data
->pwm_rr
[ix
> 0] = PWM_RR_TO_REG(val
, ix
,
1264 data
->pwm_rr
[ix
> 0]);
1266 /* Enable/disable the feature only if the associated PWM
1267 * output is in automatic mode. */
1268 if (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == 2) {
1269 data
->pwm_rr
[ix
> 0] = PWM_RR_EN_TO_REG(val
> 0, ix
,
1270 data
->pwm_rr
[ix
> 0]);
1272 dme1737_write(client
, DME1737_REG_PWM_RR(ix
> 0),
1273 data
->pwm_rr
[ix
> 0]);
1275 case SYS_PWM_AUTO_CHANNELS_ZONE
:
1276 /* Only valid for pwm[1-3] */
1277 if (!(val
== 1 || val
== 2 || val
== 4 ||
1278 val
== 6 || val
== 7)) {
1280 dev_warn(dev
, "PWM auto channels zone %ld "
1281 "not supported. Choose one of 1, 2, 4, 6, "
1285 /* Refresh the cache */
1286 data
->pwm_config
[ix
] = dme1737_read(client
,
1287 DME1737_REG_PWM_CONFIG(ix
));
1288 if (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == 2) {
1289 /* PWM is already in auto mode so update the temp
1290 * channel assignment */
1291 data
->pwm_config
[ix
] = PWM_ACZ_TO_REG(val
,
1292 data
->pwm_config
[ix
]);
1293 dme1737_write(client
, DME1737_REG_PWM_CONFIG(ix
),
1294 data
->pwm_config
[ix
]);
1296 /* PWM is not in auto mode so we save the temp
1297 * channel assignment for later use */
1298 data
->pwm_acz
[ix
] = val
;
1301 case SYS_PWM_AUTO_PWM_MIN
:
1302 /* Only valid for pwm[1-3] */
1303 /* Refresh the cache */
1304 data
->pwm_min
[ix
] = dme1737_read(client
,
1305 DME1737_REG_PWM_MIN(ix
));
1306 /* There are only 2 values supported for the auto_pwm_min
1307 * value: 0 or auto_point1_pwm. So if the temperature drops
1308 * below the auto_point1_temp_hyst value, the fan either turns
1309 * off or runs at auto_point1_pwm duty-cycle. */
1310 if (val
> ((data
->pwm_min
[ix
] + 1) / 2)) {
1311 data
->pwm_rr
[0] = PWM_OFF_TO_REG(1, ix
,
1312 dme1737_read(client
,
1313 DME1737_REG_PWM_RR(0)));
1315 data
->pwm_rr
[0] = PWM_OFF_TO_REG(0, ix
,
1316 dme1737_read(client
,
1317 DME1737_REG_PWM_RR(0)));
1319 dme1737_write(client
, DME1737_REG_PWM_RR(0),
1322 case SYS_PWM_AUTO_POINT1_PWM
:
1323 /* Only valid for pwm[1-3] */
1324 data
->pwm_min
[ix
] = SENSORS_LIMIT(val
, 0, 255);
1325 dme1737_write(client
, DME1737_REG_PWM_MIN(ix
),
1329 dev_dbg(dev
, "Unknown function %d.\n", fn
);
1332 mutex_unlock(&data
->update_lock
);
1337 /* ---------------------------------------------------------------------
1338 * Miscellaneous sysfs attributes
1339 * --------------------------------------------------------------------- */
1341 static ssize_t
show_vrm(struct device
*dev
, struct device_attribute
*attr
,
1344 struct i2c_client
*client
= to_i2c_client(dev
);
1345 struct dme1737_data
*data
= i2c_get_clientdata(client
);
1347 return sprintf(buf
, "%d\n", data
->vrm
);
1350 static ssize_t
set_vrm(struct device
*dev
, struct device_attribute
*attr
,
1351 const char *buf
, size_t count
)
1353 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1354 long val
= simple_strtol(buf
, NULL
, 10);
1360 static ssize_t
show_vid(struct device
*dev
, struct device_attribute
*attr
,
1363 struct dme1737_data
*data
= dme1737_update_device(dev
);
1365 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
, data
->vrm
));
1368 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*attr
,
1371 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1373 return sprintf(buf
, "%s\n", data
->client
.name
);
1376 /* ---------------------------------------------------------------------
1377 * Sysfs device attribute defines and structs
1378 * --------------------------------------------------------------------- */
1382 #define SENSOR_DEVICE_ATTR_IN(ix) \
1383 static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
1384 show_in, NULL, SYS_IN_INPUT, ix); \
1385 static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
1386 show_in, set_in, SYS_IN_MIN, ix); \
1387 static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
1388 show_in, set_in, SYS_IN_MAX, ix); \
1389 static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
1390 show_in, NULL, SYS_IN_ALARM, ix)
1392 SENSOR_DEVICE_ATTR_IN(0);
1393 SENSOR_DEVICE_ATTR_IN(1);
1394 SENSOR_DEVICE_ATTR_IN(2);
1395 SENSOR_DEVICE_ATTR_IN(3);
1396 SENSOR_DEVICE_ATTR_IN(4);
1397 SENSOR_DEVICE_ATTR_IN(5);
1398 SENSOR_DEVICE_ATTR_IN(6);
1400 /* Temperatures 1-3 */
1402 #define SENSOR_DEVICE_ATTR_TEMP(ix) \
1403 static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
1404 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
1405 static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
1406 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
1407 static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
1408 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
1409 static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
1410 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
1411 static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
1412 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
1413 static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
1414 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
1416 SENSOR_DEVICE_ATTR_TEMP(1);
1417 SENSOR_DEVICE_ATTR_TEMP(2);
1418 SENSOR_DEVICE_ATTR_TEMP(3);
1422 #define SENSOR_DEVICE_ATTR_ZONE(ix) \
1423 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
1424 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
1425 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
1426 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
1427 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
1428 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
1429 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
1430 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
1431 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
1432 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
1434 SENSOR_DEVICE_ATTR_ZONE(1);
1435 SENSOR_DEVICE_ATTR_ZONE(2);
1436 SENSOR_DEVICE_ATTR_ZONE(3);
1440 #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1441 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1442 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1443 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1444 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1445 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1446 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1447 static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
1448 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
1450 SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1451 SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1452 SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1453 SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1457 #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1458 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1459 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1460 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1461 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1462 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1463 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1464 static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
1465 show_fan, set_fan, SYS_FAN_MAX, ix-1)
1467 SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1468 SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1472 #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1473 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1474 show_pwm, set_pwm, SYS_PWM, ix-1); \
1475 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1476 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1477 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1478 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
1479 static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
1480 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
1481 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
1482 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
1483 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
1484 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
1485 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
1486 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
1487 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
1488 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
1490 SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1491 SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1492 SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1496 #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
1497 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO | S_IWUSR, \
1498 show_pwm, set_pwm, SYS_PWM, ix-1); \
1499 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1500 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1501 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1502 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
1504 SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1505 SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1509 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm
, set_vrm
);
1510 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid
, NULL
);
1511 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
); /* for ISA devices */
1513 #define SENSOR_DEV_ATTR_IN(ix) \
1514 &sensor_dev_attr_in##ix##_input.dev_attr.attr, \
1515 &sensor_dev_attr_in##ix##_min.dev_attr.attr, \
1516 &sensor_dev_attr_in##ix##_max.dev_attr.attr, \
1517 &sensor_dev_attr_in##ix##_alarm.dev_attr.attr
1519 /* These attributes are read-writeable only if the chip is *not* locked */
1520 #define SENSOR_DEV_ATTR_TEMP_LOCK(ix) \
1521 &sensor_dev_attr_temp##ix##_offset.dev_attr.attr
1523 #define SENSOR_DEV_ATTR_TEMP(ix) \
1524 SENSOR_DEV_ATTR_TEMP_LOCK(ix), \
1525 &sensor_dev_attr_temp##ix##_input.dev_attr.attr, \
1526 &sensor_dev_attr_temp##ix##_min.dev_attr.attr, \
1527 &sensor_dev_attr_temp##ix##_max.dev_attr.attr, \
1528 &sensor_dev_attr_temp##ix##_alarm.dev_attr.attr, \
1529 &sensor_dev_attr_temp##ix##_fault.dev_attr.attr
1531 /* These attributes are read-writeable only if the chip is *not* locked */
1532 #define SENSOR_DEV_ATTR_ZONE_LOCK(ix) \
1533 &sensor_dev_attr_zone##ix##_auto_point1_temp_hyst.dev_attr.attr, \
1534 &sensor_dev_attr_zone##ix##_auto_point1_temp.dev_attr.attr, \
1535 &sensor_dev_attr_zone##ix##_auto_point2_temp.dev_attr.attr, \
1536 &sensor_dev_attr_zone##ix##_auto_point3_temp.dev_attr.attr
1538 #define SENSOR_DEV_ATTR_ZONE(ix) \
1539 SENSOR_DEV_ATTR_ZONE_LOCK(ix), \
1540 &sensor_dev_attr_zone##ix##_auto_channels_temp.dev_attr.attr
1542 #define SENSOR_DEV_ATTR_FAN_1TO4(ix) \
1543 &sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
1544 &sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
1545 &sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
1546 &sensor_dev_attr_fan##ix##_type.dev_attr.attr
1548 #define SENSOR_DEV_ATTR_FAN_5TO6(ix) \
1549 &sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
1550 &sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
1551 &sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
1552 &sensor_dev_attr_fan##ix##_max.dev_attr.attr
1554 /* These attributes are read-writeable only if the chip is *not* locked */
1555 #define SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix) \
1556 &sensor_dev_attr_pwm##ix##_freq.dev_attr.attr, \
1557 &sensor_dev_attr_pwm##ix##_enable.dev_attr.attr, \
1558 &sensor_dev_attr_pwm##ix##_ramp_rate.dev_attr.attr, \
1559 &sensor_dev_attr_pwm##ix##_auto_channels_zone.dev_attr.attr, \
1560 &sensor_dev_attr_pwm##ix##_auto_pwm_min.dev_attr.attr, \
1561 &sensor_dev_attr_pwm##ix##_auto_point1_pwm.dev_attr.attr
1563 #define SENSOR_DEV_ATTR_PWM_1TO3(ix) \
1564 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix), \
1565 &sensor_dev_attr_pwm##ix.dev_attr.attr, \
1566 &sensor_dev_attr_pwm##ix##_auto_point2_pwm.dev_attr.attr
1568 /* These attributes are read-writeable only if the chip is *not* locked */
1569 #define SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix) \
1570 &sensor_dev_attr_pwm##ix.dev_attr.attr, \
1571 &sensor_dev_attr_pwm##ix##_freq.dev_attr.attr
1573 #define SENSOR_DEV_ATTR_PWM_5TO6(ix) \
1574 SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix), \
1575 &sensor_dev_attr_pwm##ix##_enable.dev_attr.attr
1577 /* This struct holds all the attributes that are always present and need to be
1578 * created unconditionally. The attributes that need modification of their
1579 * permissions are created read-only and write permissions are added or removed
1580 * on the fly when required */
1581 static struct attribute
*dme1737_attr
[] ={
1583 SENSOR_DEV_ATTR_IN(0),
1584 SENSOR_DEV_ATTR_IN(1),
1585 SENSOR_DEV_ATTR_IN(2),
1586 SENSOR_DEV_ATTR_IN(3),
1587 SENSOR_DEV_ATTR_IN(4),
1588 SENSOR_DEV_ATTR_IN(5),
1589 SENSOR_DEV_ATTR_IN(6),
1591 SENSOR_DEV_ATTR_TEMP(1),
1592 SENSOR_DEV_ATTR_TEMP(2),
1593 SENSOR_DEV_ATTR_TEMP(3),
1595 SENSOR_DEV_ATTR_ZONE(1),
1596 SENSOR_DEV_ATTR_ZONE(2),
1597 SENSOR_DEV_ATTR_ZONE(3),
1600 &dev_attr_cpu0_vid
.attr
,
1604 static const struct attribute_group dme1737_group
= {
1605 .attrs
= dme1737_attr
,
1608 /* The following structs hold the PWM attributes, some of which are optional.
1609 * Their creation depends on the chip configuration which is determined during
1611 static struct attribute
*dme1737_attr_pwm1
[] = {
1612 SENSOR_DEV_ATTR_PWM_1TO3(1),
1615 static struct attribute
*dme1737_attr_pwm2
[] = {
1616 SENSOR_DEV_ATTR_PWM_1TO3(2),
1619 static struct attribute
*dme1737_attr_pwm3
[] = {
1620 SENSOR_DEV_ATTR_PWM_1TO3(3),
1623 static struct attribute
*dme1737_attr_pwm5
[] = {
1624 SENSOR_DEV_ATTR_PWM_5TO6(5),
1627 static struct attribute
*dme1737_attr_pwm6
[] = {
1628 SENSOR_DEV_ATTR_PWM_5TO6(6),
1632 static const struct attribute_group dme1737_pwm_group
[] = {
1633 { .attrs
= dme1737_attr_pwm1
},
1634 { .attrs
= dme1737_attr_pwm2
},
1635 { .attrs
= dme1737_attr_pwm3
},
1637 { .attrs
= dme1737_attr_pwm5
},
1638 { .attrs
= dme1737_attr_pwm6
},
1641 /* The following structs hold the fan attributes, some of which are optional.
1642 * Their creation depends on the chip configuration which is determined during
1644 static struct attribute
*dme1737_attr_fan1
[] = {
1645 SENSOR_DEV_ATTR_FAN_1TO4(1),
1648 static struct attribute
*dme1737_attr_fan2
[] = {
1649 SENSOR_DEV_ATTR_FAN_1TO4(2),
1652 static struct attribute
*dme1737_attr_fan3
[] = {
1653 SENSOR_DEV_ATTR_FAN_1TO4(3),
1656 static struct attribute
*dme1737_attr_fan4
[] = {
1657 SENSOR_DEV_ATTR_FAN_1TO4(4),
1660 static struct attribute
*dme1737_attr_fan5
[] = {
1661 SENSOR_DEV_ATTR_FAN_5TO6(5),
1664 static struct attribute
*dme1737_attr_fan6
[] = {
1665 SENSOR_DEV_ATTR_FAN_5TO6(6),
1669 static const struct attribute_group dme1737_fan_group
[] = {
1670 { .attrs
= dme1737_attr_fan1
},
1671 { .attrs
= dme1737_attr_fan2
},
1672 { .attrs
= dme1737_attr_fan3
},
1673 { .attrs
= dme1737_attr_fan4
},
1674 { .attrs
= dme1737_attr_fan5
},
1675 { .attrs
= dme1737_attr_fan6
},
1678 /* The permissions of all of the following attributes are changed to read-
1679 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1680 static struct attribute
*dme1737_attr_lock
[] = {
1682 SENSOR_DEV_ATTR_TEMP_LOCK(1),
1683 SENSOR_DEV_ATTR_TEMP_LOCK(2),
1684 SENSOR_DEV_ATTR_TEMP_LOCK(3),
1686 SENSOR_DEV_ATTR_ZONE_LOCK(1),
1687 SENSOR_DEV_ATTR_ZONE_LOCK(2),
1688 SENSOR_DEV_ATTR_ZONE_LOCK(3),
1692 static const struct attribute_group dme1737_lock_group
= {
1693 .attrs
= dme1737_attr_lock
,
1696 /* The permissions of the following PWM attributes are changed to read-
1697 * writeable if the chip is *not* locked and the respective PWM is available.
1698 * Otherwise they stay read-only. */
1699 static struct attribute
*dme1737_attr_pwm1_lock
[] = {
1700 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(1),
1703 static struct attribute
*dme1737_attr_pwm2_lock
[] = {
1704 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(2),
1707 static struct attribute
*dme1737_attr_pwm3_lock
[] = {
1708 SENSOR_DEV_ATTR_PWM_1TO3_LOCK(3),
1711 static struct attribute
*dme1737_attr_pwm5_lock
[] = {
1712 SENSOR_DEV_ATTR_PWM_5TO6_LOCK(5),
1715 static struct attribute
*dme1737_attr_pwm6_lock
[] = {
1716 SENSOR_DEV_ATTR_PWM_5TO6_LOCK(6),
1720 static const struct attribute_group dme1737_pwm_lock_group
[] = {
1721 { .attrs
= dme1737_attr_pwm1_lock
},
1722 { .attrs
= dme1737_attr_pwm2_lock
},
1723 { .attrs
= dme1737_attr_pwm3_lock
},
1725 { .attrs
= dme1737_attr_pwm5_lock
},
1726 { .attrs
= dme1737_attr_pwm6_lock
},
1729 /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1730 * chip is not locked. Otherwise they are read-only. */
1731 static struct attribute
*dme1737_attr_pwm
[] = {
1732 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1733 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1734 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1737 /* ---------------------------------------------------------------------
1738 * Super-IO functions
1739 * --------------------------------------------------------------------- */
1741 static inline void dme1737_sio_enter(int sio_cip
)
1743 outb(0x55, sio_cip
);
1746 static inline void dme1737_sio_exit(int sio_cip
)
1748 outb(0xaa, sio_cip
);
1751 static inline int dme1737_sio_inb(int sio_cip
, int reg
)
1754 return inb(sio_cip
+ 1);
1757 static inline void dme1737_sio_outb(int sio_cip
, int reg
, int val
)
1760 outb(val
, sio_cip
+ 1);
1763 /* ---------------------------------------------------------------------
1764 * Device initialization
1765 * --------------------------------------------------------------------- */
1767 static int dme1737_i2c_get_features(int, struct dme1737_data
*);
1769 static void dme1737_chmod_file(struct device
*dev
,
1770 struct attribute
*attr
, mode_t mode
)
1772 if (sysfs_chmod_file(&dev
->kobj
, attr
, mode
)) {
1773 dev_warn(dev
, "Failed to change permissions of %s.\n",
1778 static void dme1737_chmod_group(struct device
*dev
,
1779 const struct attribute_group
*group
,
1782 struct attribute
**attr
;
1784 for (attr
= group
->attrs
; *attr
; attr
++) {
1785 dme1737_chmod_file(dev
, *attr
, mode
);
1789 static void dme1737_remove_files(struct device
*dev
)
1791 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1794 for (ix
= 0; ix
< ARRAY_SIZE(dme1737_fan_group
); ix
++) {
1795 if (data
->has_fan
& (1 << ix
)) {
1796 sysfs_remove_group(&dev
->kobj
,
1797 &dme1737_fan_group
[ix
]);
1801 for (ix
= 0; ix
< ARRAY_SIZE(dme1737_pwm_group
); ix
++) {
1802 if (data
->has_pwm
& (1 << ix
)) {
1803 sysfs_remove_group(&dev
->kobj
,
1804 &dme1737_pwm_group
[ix
]);
1808 sysfs_remove_group(&dev
->kobj
, &dme1737_group
);
1810 if (!data
->client
.driver
) {
1811 sysfs_remove_file(&dev
->kobj
, &dev_attr_name
.attr
);
1815 static int dme1737_create_files(struct device
*dev
)
1817 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1820 /* Create a name attribute for ISA devices */
1821 if (!data
->client
.driver
&&
1822 (err
= sysfs_create_file(&dev
->kobj
, &dev_attr_name
.attr
))) {
1826 /* Create standard sysfs attributes */
1827 if ((err
= sysfs_create_group(&dev
->kobj
, &dme1737_group
))) {
1831 /* Create fan sysfs attributes */
1832 for (ix
= 0; ix
< ARRAY_SIZE(dme1737_fan_group
); ix
++) {
1833 if (data
->has_fan
& (1 << ix
)) {
1834 if ((err
= sysfs_create_group(&dev
->kobj
,
1835 &dme1737_fan_group
[ix
]))) {
1841 /* Create PWM sysfs attributes */
1842 for (ix
= 0; ix
< ARRAY_SIZE(dme1737_pwm_group
); ix
++) {
1843 if (data
->has_pwm
& (1 << ix
)) {
1844 if ((err
= sysfs_create_group(&dev
->kobj
,
1845 &dme1737_pwm_group
[ix
]))) {
1851 /* Inform if the device is locked. Otherwise change the permissions of
1852 * selected attributes from read-only to read-writeable. */
1853 if (data
->config
& 0x02) {
1854 dev_info(dev
, "Device is locked. Some attributes "
1855 "will be read-only.\n");
1857 /* Change permissions of standard attributes */
1858 dme1737_chmod_group(dev
, &dme1737_lock_group
,
1861 /* Change permissions of PWM attributes */
1862 for (ix
= 0; ix
< ARRAY_SIZE(dme1737_pwm_lock_group
); ix
++) {
1863 if (data
->has_pwm
& (1 << ix
)) {
1864 dme1737_chmod_group(dev
,
1865 &dme1737_pwm_lock_group
[ix
],
1870 /* Change permissions of pwm[1-3] if in manual mode */
1871 for (ix
= 0; ix
< 3; ix
++) {
1872 if ((data
->has_pwm
& (1 << ix
)) &&
1873 (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == 1)) {
1874 dme1737_chmod_file(dev
,
1875 dme1737_attr_pwm
[ix
],
1884 dme1737_remove_files(dev
);
1889 static int dme1737_init_device(struct device
*dev
)
1891 struct dme1737_data
*data
= dev_get_drvdata(dev
);
1892 struct i2c_client
*client
= &data
->client
;
1896 data
->config
= dme1737_read(client
, DME1737_REG_CONFIG
);
1897 /* Inform if part is not monitoring/started */
1898 if (!(data
->config
& 0x01)) {
1900 dev_err(dev
, "Device is not monitoring. "
1901 "Use the force_start load parameter to "
1906 /* Force monitoring */
1907 data
->config
|= 0x01;
1908 dme1737_write(client
, DME1737_REG_CONFIG
, data
->config
);
1910 /* Inform if part is not ready */
1911 if (!(data
->config
& 0x04)) {
1912 dev_err(dev
, "Device is not ready.\n");
1916 /* Determine which optional fan and pwm features are enabled/present */
1917 if (client
->driver
) { /* I2C chip */
1918 data
->config2
= dme1737_read(client
, DME1737_REG_CONFIG2
);
1919 /* Check if optional fan3 input is enabled */
1920 if (data
->config2
& 0x04) {
1921 data
->has_fan
|= (1 << 2);
1924 /* Fan4 and pwm3 are only available if the client's I2C address
1925 * is the default 0x2e. Otherwise the I/Os associated with
1926 * these functions are used for addr enable/select. */
1927 if (data
->client
.addr
== 0x2e) {
1928 data
->has_fan
|= (1 << 3);
1929 data
->has_pwm
|= (1 << 2);
1932 /* Determine which of the optional fan[5-6] and pwm[5-6]
1933 * features are enabled. For this, we need to query the runtime
1934 * registers through the Super-IO LPC interface. Try both
1935 * config ports 0x2e and 0x4e. */
1936 if (dme1737_i2c_get_features(0x2e, data
) &&
1937 dme1737_i2c_get_features(0x4e, data
)) {
1938 dev_warn(dev
, "Failed to query Super-IO for optional "
1941 } else { /* ISA chip */
1942 /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6]
1943 * don't exist in the ISA chip. */
1944 data
->has_fan
|= (1 << 2);
1945 data
->has_pwm
|= (1 << 2);
1948 /* Fan1, fan2, pwm1, and pwm2 are always present */
1949 data
->has_fan
|= 0x03;
1950 data
->has_pwm
|= 0x03;
1952 dev_info(dev
, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
1953 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
1954 (data
->has_pwm
& (1 << 2)) ? "yes" : "no",
1955 (data
->has_pwm
& (1 << 4)) ? "yes" : "no",
1956 (data
->has_pwm
& (1 << 5)) ? "yes" : "no",
1957 (data
->has_fan
& (1 << 2)) ? "yes" : "no",
1958 (data
->has_fan
& (1 << 3)) ? "yes" : "no",
1959 (data
->has_fan
& (1 << 4)) ? "yes" : "no",
1960 (data
->has_fan
& (1 << 5)) ? "yes" : "no");
1962 reg
= dme1737_read(client
, DME1737_REG_TACH_PWM
);
1963 /* Inform if fan-to-pwm mapping differs from the default */
1964 if (client
->driver
&& reg
!= 0xa4) { /* I2C chip */
1965 dev_warn(dev
, "Non-standard fan to pwm mapping: "
1966 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
1967 "fan4->pwm%d. Please report to the driver "
1969 (reg
& 0x03) + 1, ((reg
>> 2) & 0x03) + 1,
1970 ((reg
>> 4) & 0x03) + 1, ((reg
>> 6) & 0x03) + 1);
1971 } else if (!client
->driver
&& reg
!= 0x24) { /* ISA chip */
1972 dev_warn(dev
, "Non-standard fan to pwm mapping: "
1973 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
1974 "Please report to the driver maintainer.\n",
1975 (reg
& 0x03) + 1, ((reg
>> 2) & 0x03) + 1,
1976 ((reg
>> 4) & 0x03) + 1);
1979 /* Switch pwm[1-3] to manual mode if they are currently disabled and
1980 * set the duty-cycles to 0% (which is identical to the PWMs being
1982 if (!(data
->config
& 0x02)) {
1983 for (ix
= 0; ix
< 3; ix
++) {
1984 data
->pwm_config
[ix
] = dme1737_read(client
,
1985 DME1737_REG_PWM_CONFIG(ix
));
1986 if ((data
->has_pwm
& (1 << ix
)) &&
1987 (PWM_EN_FROM_REG(data
->pwm_config
[ix
]) == -1)) {
1988 dev_info(dev
, "Switching pwm%d to "
1989 "manual mode.\n", ix
+ 1);
1990 data
->pwm_config
[ix
] = PWM_EN_TO_REG(1,
1991 data
->pwm_config
[ix
]);
1992 dme1737_write(client
, DME1737_REG_PWM(ix
), 0);
1993 dme1737_write(client
,
1994 DME1737_REG_PWM_CONFIG(ix
),
1995 data
->pwm_config
[ix
]);
2000 /* Initialize the default PWM auto channels zone (acz) assignments */
2001 data
->pwm_acz
[0] = 1; /* pwm1 -> zone1 */
2002 data
->pwm_acz
[1] = 2; /* pwm2 -> zone2 */
2003 data
->pwm_acz
[2] = 4; /* pwm3 -> zone3 */
2006 data
->vrm
= vid_which_vrm();
2011 /* ---------------------------------------------------------------------
2012 * I2C device detection and registration
2013 * --------------------------------------------------------------------- */
2015 static struct i2c_driver dme1737_i2c_driver
;
2017 static int dme1737_i2c_get_features(int sio_cip
, struct dme1737_data
*data
)
2022 dme1737_sio_enter(sio_cip
);
2025 * The DME1737 can return either 0x78 or 0x77 as its device ID. */
2026 reg
= dme1737_sio_inb(sio_cip
, 0x20);
2027 if (!(reg
== 0x77 || reg
== 0x78)) {
2032 /* Select logical device A (runtime registers) */
2033 dme1737_sio_outb(sio_cip
, 0x07, 0x0a);
2035 /* Get the base address of the runtime registers */
2036 if (!(addr
= (dme1737_sio_inb(sio_cip
, 0x60) << 8) |
2037 dme1737_sio_inb(sio_cip
, 0x61))) {
2042 /* Read the runtime registers to determine which optional features
2043 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2044 * to '10' if the respective feature is enabled. */
2045 if ((inb(addr
+ 0x43) & 0x0c) == 0x08) { /* fan6 */
2046 data
->has_fan
|= (1 << 5);
2048 if ((inb(addr
+ 0x44) & 0x0c) == 0x08) { /* pwm6 */
2049 data
->has_pwm
|= (1 << 5);
2051 if ((inb(addr
+ 0x45) & 0x0c) == 0x08) { /* fan5 */
2052 data
->has_fan
|= (1 << 4);
2054 if ((inb(addr
+ 0x46) & 0x0c) == 0x08) { /* pwm5 */
2055 data
->has_pwm
|= (1 << 4);
2059 dme1737_sio_exit(sio_cip
);
2064 static int dme1737_i2c_detect(struct i2c_adapter
*adapter
, int address
,
2067 u8 company
, verstep
= 0;
2068 struct i2c_client
*client
;
2069 struct dme1737_data
*data
;
2074 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
2078 if (!(data
= kzalloc(sizeof(struct dme1737_data
), GFP_KERNEL
))) {
2083 client
= &data
->client
;
2084 i2c_set_clientdata(client
, data
);
2085 client
->addr
= address
;
2086 client
->adapter
= adapter
;
2087 client
->driver
= &dme1737_i2c_driver
;
2090 /* A negative kind means that the driver was loaded with no force
2091 * parameter (default), so we must identify the chip. */
2093 company
= dme1737_read(client
, DME1737_REG_COMPANY
);
2094 verstep
= dme1737_read(client
, DME1737_REG_VERSTEP
);
2096 if (!((company
== DME1737_COMPANY_SMSC
) &&
2097 ((verstep
& DME1737_VERSTEP_MASK
) == DME1737_VERSTEP
))) {
2106 /* Fill in the remaining client fields and put it into the global
2108 strlcpy(client
->name
, name
, I2C_NAME_SIZE
);
2109 mutex_init(&data
->update_lock
);
2111 /* Tell the I2C layer a new client has arrived */
2112 if ((err
= i2c_attach_client(client
))) {
2116 dev_info(dev
, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2117 client
->addr
, verstep
);
2119 /* Initialize the DME1737 chip */
2120 if ((err
= dme1737_init_device(dev
))) {
2121 dev_err(dev
, "Failed to initialize device.\n");
2125 /* Create sysfs files */
2126 if ((err
= dme1737_create_files(dev
))) {
2127 dev_err(dev
, "Failed to create sysfs files.\n");
2131 /* Register device */
2132 data
->hwmon_dev
= hwmon_device_register(dev
);
2133 if (IS_ERR(data
->hwmon_dev
)) {
2134 dev_err(dev
, "Failed to register device.\n");
2135 err
= PTR_ERR(data
->hwmon_dev
);
2142 dme1737_remove_files(dev
);
2144 i2c_detach_client(client
);
2151 static int dme1737_i2c_attach_adapter(struct i2c_adapter
*adapter
)
2153 if (!(adapter
->class & I2C_CLASS_HWMON
)) {
2157 return i2c_probe(adapter
, &addr_data
, dme1737_i2c_detect
);
2160 static int dme1737_i2c_detach_client(struct i2c_client
*client
)
2162 struct dme1737_data
*data
= i2c_get_clientdata(client
);
2165 hwmon_device_unregister(data
->hwmon_dev
);
2166 dme1737_remove_files(&client
->dev
);
2168 if ((err
= i2c_detach_client(client
))) {
2176 static struct i2c_driver dme1737_i2c_driver
= {
2180 .attach_adapter
= dme1737_i2c_attach_adapter
,
2181 .detach_client
= dme1737_i2c_detach_client
,
2184 /* ---------------------------------------------------------------------
2185 * ISA device detection and registration
2186 * --------------------------------------------------------------------- */
2188 static int __init
dme1737_isa_detect(int sio_cip
, unsigned short *addr
)
2191 unsigned short base_addr
;
2193 dme1737_sio_enter(sio_cip
);
2196 * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and
2197 * SCH3116 (0x7f). */
2198 reg
= force_id
? force_id
: dme1737_sio_inb(sio_cip
, 0x20);
2199 if (!(reg
== 0x7c || reg
== 0x7d || reg
== 0x7f)) {
2204 /* Select logical device A (runtime registers) */
2205 dme1737_sio_outb(sio_cip
, 0x07, 0x0a);
2207 /* Get the base address of the runtime registers */
2208 if (!(base_addr
= (dme1737_sio_inb(sio_cip
, 0x60) << 8) |
2209 dme1737_sio_inb(sio_cip
, 0x61))) {
2210 printk(KERN_ERR
"dme1737: Base address not set.\n");
2215 /* Access to the hwmon registers is through an index/data register
2216 * pair located at offset 0x70/0x71. */
2217 *addr
= base_addr
+ 0x70;
2220 dme1737_sio_exit(sio_cip
);
2224 static int __init
dme1737_isa_device_add(unsigned short addr
)
2226 struct resource res
= {
2228 .end
= addr
+ DME1737_EXTENT
- 1,
2230 .flags
= IORESOURCE_IO
,
2234 if (!(pdev
= platform_device_alloc("dme1737", addr
))) {
2235 printk(KERN_ERR
"dme1737: Failed to allocate device.\n");
2240 if ((err
= platform_device_add_resources(pdev
, &res
, 1))) {
2241 printk(KERN_ERR
"dme1737: Failed to add device resource "
2242 "(err = %d).\n", err
);
2243 goto exit_device_put
;
2246 if ((err
= platform_device_add(pdev
))) {
2247 printk(KERN_ERR
"dme1737: Failed to add device (err = %d).\n",
2249 goto exit_device_put
;
2255 platform_device_put(pdev
);
2261 static int __devinit
dme1737_isa_probe(struct platform_device
*pdev
)
2264 struct resource
*res
;
2265 struct i2c_client
*client
;
2266 struct dme1737_data
*data
;
2267 struct device
*dev
= &pdev
->dev
;
2270 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
2271 if (!request_region(res
->start
, DME1737_EXTENT
, "dme1737")) {
2272 dev_err(dev
, "Failed to request region 0x%04x-0x%04x.\n",
2273 (unsigned short)res
->start
,
2274 (unsigned short)res
->start
+ DME1737_EXTENT
- 1);
2279 if (!(data
= kzalloc(sizeof(struct dme1737_data
), GFP_KERNEL
))) {
2281 goto exit_release_region
;
2284 client
= &data
->client
;
2285 i2c_set_clientdata(client
, data
);
2286 client
->addr
= res
->start
;
2287 platform_set_drvdata(pdev
, data
);
2289 company
= dme1737_read(client
, DME1737_REG_COMPANY
);
2290 device
= dme1737_read(client
, DME1737_REG_DEVICE
);
2292 if (!((company
== DME1737_COMPANY_SMSC
) &&
2293 (device
== SCH311X_DEVICE
))) {
2298 /* Fill in the remaining client fields and initialize the mutex */
2299 strlcpy(client
->name
, "sch311x", I2C_NAME_SIZE
);
2300 mutex_init(&data
->update_lock
);
2302 dev_info(dev
, "Found a SCH311x chip at 0x%04x\n", client
->addr
);
2304 /* Initialize the chip */
2305 if ((err
= dme1737_init_device(dev
))) {
2306 dev_err(dev
, "Failed to initialize device.\n");
2310 /* Create sysfs files */
2311 if ((err
= dme1737_create_files(dev
))) {
2312 dev_err(dev
, "Failed to create sysfs files.\n");
2316 /* Register device */
2317 data
->hwmon_dev
= hwmon_device_register(dev
);
2318 if (IS_ERR(data
->hwmon_dev
)) {
2319 dev_err(dev
, "Failed to register device.\n");
2320 err
= PTR_ERR(data
->hwmon_dev
);
2321 goto exit_remove_files
;
2327 dme1737_remove_files(dev
);
2329 platform_set_drvdata(pdev
, NULL
);
2331 exit_release_region
:
2332 release_region(res
->start
, DME1737_EXTENT
);
2337 static int __devexit
dme1737_isa_remove(struct platform_device
*pdev
)
2339 struct dme1737_data
*data
= platform_get_drvdata(pdev
);
2341 hwmon_device_unregister(data
->hwmon_dev
);
2342 dme1737_remove_files(&pdev
->dev
);
2343 release_region(data
->client
.addr
, DME1737_EXTENT
);
2344 platform_set_drvdata(pdev
, NULL
);
2350 static struct platform_driver dme1737_isa_driver
= {
2352 .owner
= THIS_MODULE
,
2355 .probe
= dme1737_isa_probe
,
2356 .remove
= __devexit_p(dme1737_isa_remove
),
2359 /* ---------------------------------------------------------------------
2360 * Module initialization and cleanup
2361 * --------------------------------------------------------------------- */
2363 static int __init
dme1737_init(void)
2366 unsigned short addr
;
2368 if ((err
= i2c_add_driver(&dme1737_i2c_driver
))) {
2372 if (dme1737_isa_detect(0x2e, &addr
) &&
2373 dme1737_isa_detect(0x4e, &addr
)) {
2374 /* Return 0 if we didn't find an ISA device */
2378 if ((err
= platform_driver_register(&dme1737_isa_driver
))) {
2379 goto exit_del_i2c_driver
;
2382 /* Sets global pdev as a side effect */
2383 if ((err
= dme1737_isa_device_add(addr
))) {
2384 goto exit_del_isa_driver
;
2389 exit_del_isa_driver
:
2390 platform_driver_unregister(&dme1737_isa_driver
);
2391 exit_del_i2c_driver
:
2392 i2c_del_driver(&dme1737_i2c_driver
);
2397 static void __exit
dme1737_exit(void)
2400 platform_device_unregister(pdev
);
2401 platform_driver_unregister(&dme1737_isa_driver
);
2404 i2c_del_driver(&dme1737_i2c_driver
);
2407 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2408 MODULE_DESCRIPTION("DME1737 sensors");
2409 MODULE_LICENSE("GPL");
2411 module_init(dme1737_init
);
2412 module_exit(dme1737_exit
);