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 static int probe_all_addr;
52 module_param(probe_all_addr, bool, 0);
53 MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
56 /* Addresses to scan */
57 static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
59 /* Insmod parameters */
60 I2C_CLIENT_INSMOD_1(dme1737);
62 /* ---------------------------------------------------------------------
65 * The sensors are defined as follows:
67 * Voltages Temperatures
68 * -------- ------------
69 * in0 +5VTR (+5V stdby) temp1 Remote diode 1
70 * in1 Vccp (proc core) temp2 Internal temp
71 * in2 VCC (internal +3.3V) temp3 Remote diode 2
74 * in5 VTR (+3.3V stby)
77 * --------------------------------------------------------------------- */
79 /* Voltages (in) numbered 0-6 (ix) */
80 #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \
82 #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
84 #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
87 /* Temperatures (temp) numbered 0-2 (ix) */
88 #define DME1737_REG_TEMP(ix) (0x25 + (ix))
89 #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
90 #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
91 #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
94 /* Voltage and temperature LSBs
95 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
96 * IN_TEMP_LSB(0) = [in5, in6]
97 * IN_TEMP_LSB(1) = [temp3, temp1]
98 * IN_TEMP_LSB(2) = [in4, temp2]
99 * IN_TEMP_LSB(3) = [in3, in0]
100 * IN_TEMP_LSB(4) = [in2, in1] */
101 #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
102 static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
103 static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
104 static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
105 static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
107 /* Fans numbered 0-5 (ix) */
108 #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
110 #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
112 #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
114 #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
116 /* PWMs numbered 0-2, 4-5 (ix) */
117 #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
119 #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
120 #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
121 #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
123 /* The layout of the ramp rate registers is different from the other pwm
124 * registers. The bits for the 3 PWMs are stored in 2 registers:
125 * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
126 * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
127 #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
129 /* Thermal zones 0-2 */
130 #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
131 #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
132 /* The layout of the hysteresis registers is different from the other zone
133 * registers. The bits for the 3 zones are stored in 2 registers:
134 * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
135 * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
136 #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
138 /* Alarm registers and bit mapping
139 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
140 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
141 #define DME1737_REG_ALARM1 0x41
142 #define DME1737_REG_ALARM2 0x42
143 #define DME1737_REG_ALARM3 0x83
144 static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
145 static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
146 static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
148 /* Miscellaneous registers */
149 #define DME1737_REG_DEVICE 0x3d
150 #define DME1737_REG_COMPANY 0x3e
151 #define DME1737_REG_VERSTEP 0x3f
152 #define DME1737_REG_CONFIG 0x40
153 #define DME1737_REG_CONFIG2 0x7f
154 #define DME1737_REG_VID 0x43
155 #define DME1737_REG_TACH_PWM 0x81
157 /* ---------------------------------------------------------------------
159 * --------------------------------------------------------------------- */
161 /* Chip identification */
162 #define DME1737_COMPANY_SMSC 0x5c
163 #define DME1737_VERSTEP 0x88
164 #define DME1737_VERSTEP_MASK 0xf8
165 #define SCH311X_DEVICE 0x8c
167 /* Length of ISA address segment */
168 #define DME1737_EXTENT 2
170 /* ---------------------------------------------------------------------
171 * Data structures and manipulation thereof
172 * --------------------------------------------------------------------- */
174 /* For ISA chips, we abuse the i2c_client addr and name fields. We also use
175 the driver field to differentiate between I2C and ISA chips. */
176 struct dme1737_data {
177 struct i2c_client client;
178 struct device *hwmon_dev;
180 struct mutex update_lock;
181 int valid; /* !=0 if following fields are valid */
182 unsigned long last_update; /* in jiffies */
183 unsigned long last_vbat; /* in jiffies */
191 /* Register values */
218 /* Nominal voltage values */
219 static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
221 static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
223 #define IN_NOMINAL(ix, type) (((type) == dme1737) ? \
224 IN_NOMINAL_DME1737[(ix)] : \
225 IN_NOMINAL_SCH311x[(ix)])
228 * Voltage inputs have 16 bits resolution, limit values have 8 bits
230 static inline int IN_FROM_REG(int reg, int ix, int res, int type)
232 return (reg * IN_NOMINAL(ix, type) + (3 << (res - 3))) /
236 static inline int IN_TO_REG(int val, int ix, int type)
238 return SENSORS_LIMIT((val * 192 + IN_NOMINAL(ix, type) / 2) /
239 IN_NOMINAL(ix, type), 0, 255);
243 * The register values represent temperatures in 2's complement notation from
244 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
245 * values have 8 bits resolution. */
246 static inline int TEMP_FROM_REG(int reg, int res)
248 return (reg * 1000) >> (res - 8);
251 static inline int TEMP_TO_REG(int val)
253 return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
257 /* Temperature range */
258 static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
259 10000, 13333, 16000, 20000, 26666, 32000,
260 40000, 53333, 80000};
262 static inline int TEMP_RANGE_FROM_REG(int reg)
264 return TEMP_RANGE[(reg >> 4) & 0x0f];
267 static int TEMP_RANGE_TO_REG(int val, int reg)
271 for (i = 15; i > 0; i--) {
272 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
277 return (reg & 0x0f) | (i << 4);
280 /* Temperature hysteresis
282 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
283 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
284 static inline int TEMP_HYST_FROM_REG(int reg, int ix)
286 return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
289 static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
291 int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
293 return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
297 static inline int FAN_FROM_REG(int reg, int tpc)
302 return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
306 static inline int FAN_TO_REG(int val, int tpc)
309 return SENSORS_LIMIT(val / tpc, 0, 0xffff);
311 return (val <= 0) ? 0xffff :
312 SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
316 /* Fan TPC (tach pulse count)
317 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
318 * is configured in legacy (non-tpc) mode */
319 static inline int FAN_TPC_FROM_REG(int reg)
321 return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
325 * The type of a fan is expressed in number of pulses-per-revolution that it
327 static inline int FAN_TYPE_FROM_REG(int reg)
329 int edge = (reg >> 1) & 0x03;
331 return (edge > 0) ? 1 << (edge - 1) : 0;
334 static inline int FAN_TYPE_TO_REG(int val, int reg)
336 int edge = (val == 4) ? 3 : val;
338 return (reg & 0xf9) | (edge << 1);
342 static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
345 static int FAN_MAX_FROM_REG(int reg)
349 for (i = 10; i > 0; i--) {
350 if (reg == FAN_MAX[i]) {
355 return 1000 + i * 500;
358 static int FAN_MAX_TO_REG(int val)
362 for (i = 10; i > 0; i--) {
363 if (val > (1000 + (i - 1) * 500)) {
372 * Register to enable mapping:
373 * 000: 2 fan on zone 1 auto
374 * 001: 2 fan on zone 2 auto
375 * 010: 2 fan on zone 3 auto
377 * 100: -1 fan disabled
378 * 101: 2 fan on hottest of zones 2,3 auto
379 * 110: 2 fan on hottest of zones 1,2,3 auto
380 * 111: 1 fan in manual mode */
381 static inline int PWM_EN_FROM_REG(int reg)
383 static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
385 return en[(reg >> 5) & 0x07];
388 static inline int PWM_EN_TO_REG(int val, int reg)
390 int en = (val == 1) ? 7 : 3;
392 return (reg & 0x1f) | ((en & 0x07) << 5);
395 /* PWM auto channels zone
396 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
397 * corresponding to zone x+1):
398 * 000: 001 fan on zone 1 auto
399 * 001: 010 fan on zone 2 auto
400 * 010: 100 fan on zone 3 auto
401 * 011: 000 fan full on
402 * 100: 000 fan disabled
403 * 101: 110 fan on hottest of zones 2,3 auto
404 * 110: 111 fan on hottest of zones 1,2,3 auto
405 * 111: 000 fan in manual mode */
406 static inline int PWM_ACZ_FROM_REG(int reg)
408 static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
410 return acz[(reg >> 5) & 0x07];
413 static inline int PWM_ACZ_TO_REG(int val, int reg)
415 int acz = (val == 4) ? 2 : val - 1;
417 return (reg & 0x1f) | ((acz & 0x07) << 5);
421 static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
422 15000, 20000, 30000, 25000, 0, 0, 0, 0};
424 static inline int PWM_FREQ_FROM_REG(int reg)
426 return PWM_FREQ[reg & 0x0f];
429 static int PWM_FREQ_TO_REG(int val, int reg)
433 /* the first two cases are special - stupid chip design! */
436 } else if (val > 22500) {
439 for (i = 9; i > 0; i--) {
440 if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
446 return (reg & 0xf0) | i;
451 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
452 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
453 static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
455 static inline int PWM_RR_FROM_REG(int reg, int ix)
457 int rr = (ix == 1) ? reg >> 4 : reg;
459 return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
462 static int PWM_RR_TO_REG(int val, int ix, int reg)
466 for (i = 0; i < 7; i++) {
467 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
472 return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
475 /* PWM ramp rate enable */
476 static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
478 return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
481 static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
483 int en = (ix == 1) ? 0x80 : 0x08;
485 return val ? reg | en : reg & ~en;
489 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
490 * the register layout). */
491 static inline int PWM_OFF_FROM_REG(int reg, int ix)
493 return (reg >> (ix + 5)) & 0x01;
496 static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
498 return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
501 /* ---------------------------------------------------------------------
504 * ISA access is performed through an index/data register pair and needs to
505 * be protected by a mutex during runtime (not required for initialization).
506 * We use data->update_lock for this and need to ensure that we acquire it
507 * before calling dme1737_read or dme1737_write.
508 * --------------------------------------------------------------------- */
510 static u8 dme1737_read(struct i2c_client *client, u8 reg)
514 if (client->driver) { /* I2C device */
515 val = i2c_smbus_read_byte_data(client, reg);
518 dev_warn(&client->dev, "Read from register "
519 "0x%02x failed! Please report to the driver "
520 "maintainer.\n", reg);
522 } else { /* ISA device */
523 outb(reg, client->addr);
524 val = inb(client->addr + 1);
530 static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
534 if (client->driver) { /* I2C device */
535 res = i2c_smbus_write_byte_data(client, reg, val);
538 dev_warn(&client->dev, "Write to register "
539 "0x%02x failed! Please report to the driver "
540 "maintainer.\n", reg);
542 } else { /* ISA device */
543 outb(reg, client->addr);
544 outb(val, client->addr + 1);
550 static struct dme1737_data *dme1737_update_device(struct device *dev)
552 struct dme1737_data *data = dev_get_drvdata(dev);
553 struct i2c_client *client = &data->client;
557 mutex_lock(&data->update_lock);
559 /* Enable a Vbat monitoring cycle every 10 mins */
560 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
561 dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
562 DME1737_REG_CONFIG) | 0x10);
563 data->last_vbat = jiffies;
566 /* Sample register contents every 1 sec */
567 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
568 data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
570 /* In (voltage) registers */
571 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
572 /* Voltage inputs are stored as 16 bit values even
573 * though they have only 12 bits resolution. This is
574 * to make it consistent with the temp inputs. */
575 data->in[ix] = dme1737_read(client,
576 DME1737_REG_IN(ix)) << 8;
577 data->in_min[ix] = dme1737_read(client,
578 DME1737_REG_IN_MIN(ix));
579 data->in_max[ix] = dme1737_read(client,
580 DME1737_REG_IN_MAX(ix));
584 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
585 /* Temp inputs are stored as 16 bit values even
586 * though they have only 12 bits resolution. This is
587 * to take advantage of implicit conversions between
588 * register values (2's complement) and temp values
589 * (signed decimal). */
590 data->temp[ix] = dme1737_read(client,
591 DME1737_REG_TEMP(ix)) << 8;
592 data->temp_min[ix] = dme1737_read(client,
593 DME1737_REG_TEMP_MIN(ix));
594 data->temp_max[ix] = dme1737_read(client,
595 DME1737_REG_TEMP_MAX(ix));
596 data->temp_offset[ix] = dme1737_read(client,
597 DME1737_REG_TEMP_OFFSET(ix));
600 /* In and temp LSB registers
601 * The LSBs are latched when the MSBs are read, so the order in
602 * which the registers are read (MSB first, then LSB) is
604 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
605 lsb[ix] = dme1737_read(client,
606 DME1737_REG_IN_TEMP_LSB(ix));
608 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
609 data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
610 DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
612 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
613 data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
614 DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
618 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
619 /* Skip reading registers if optional fans are not
621 if (!(data->has_fan & (1 << ix))) {
624 data->fan[ix] = dme1737_read(client,
625 DME1737_REG_FAN(ix));
626 data->fan[ix] |= dme1737_read(client,
627 DME1737_REG_FAN(ix) + 1) << 8;
628 data->fan_min[ix] = dme1737_read(client,
629 DME1737_REG_FAN_MIN(ix));
630 data->fan_min[ix] |= dme1737_read(client,
631 DME1737_REG_FAN_MIN(ix) + 1) << 8;
632 data->fan_opt[ix] = dme1737_read(client,
633 DME1737_REG_FAN_OPT(ix));
634 /* fan_max exists only for fan[5-6] */
636 data->fan_max[ix - 4] = dme1737_read(client,
637 DME1737_REG_FAN_MAX(ix));
642 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
643 /* Skip reading registers if optional PWMs are not
645 if (!(data->has_pwm & (1 << ix))) {
648 data->pwm[ix] = dme1737_read(client,
649 DME1737_REG_PWM(ix));
650 data->pwm_freq[ix] = dme1737_read(client,
651 DME1737_REG_PWM_FREQ(ix));
652 /* pwm_config and pwm_min exist only for pwm[1-3] */
654 data->pwm_config[ix] = dme1737_read(client,
655 DME1737_REG_PWM_CONFIG(ix));
656 data->pwm_min[ix] = dme1737_read(client,
657 DME1737_REG_PWM_MIN(ix));
660 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
661 data->pwm_rr[ix] = dme1737_read(client,
662 DME1737_REG_PWM_RR(ix));
665 /* Thermal zone registers */
666 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
667 data->zone_low[ix] = dme1737_read(client,
668 DME1737_REG_ZONE_LOW(ix));
669 data->zone_abs[ix] = dme1737_read(client,
670 DME1737_REG_ZONE_ABS(ix));
672 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
673 data->zone_hyst[ix] = dme1737_read(client,
674 DME1737_REG_ZONE_HYST(ix));
677 /* Alarm registers */
678 data->alarms = dme1737_read(client,
680 /* Bit 7 tells us if the other alarm registers are non-zero and
681 * therefore also need to be read */
682 if (data->alarms & 0x80) {
683 data->alarms |= dme1737_read(client,
684 DME1737_REG_ALARM2) << 8;
685 data->alarms |= dme1737_read(client,
686 DME1737_REG_ALARM3) << 16;
689 /* The ISA chips require explicit clearing of alarm bits.
690 * Don't worry, an alarm will come back if the condition
691 * that causes it still exists */
692 if (!client->driver) {
693 if (data->alarms & 0xff0000) {
694 dme1737_write(client, DME1737_REG_ALARM3,
697 if (data->alarms & 0xff00) {
698 dme1737_write(client, DME1737_REG_ALARM2,
701 if (data->alarms & 0xff) {
702 dme1737_write(client, DME1737_REG_ALARM1,
707 data->last_update = jiffies;
711 mutex_unlock(&data->update_lock);
716 /* ---------------------------------------------------------------------
717 * Voltage sysfs attributes
719 * --------------------------------------------------------------------- */
721 #define SYS_IN_INPUT 0
724 #define SYS_IN_ALARM 3
726 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
729 struct dme1737_data *data = dme1737_update_device(dev);
730 struct sensor_device_attribute_2
731 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
732 int ix = sensor_attr_2->index;
733 int fn = sensor_attr_2->nr;
738 res = IN_FROM_REG(data->in[ix], ix, 16, data->type);
741 res = IN_FROM_REG(data->in_min[ix], ix, 8, data->type);
744 res = IN_FROM_REG(data->in_max[ix], ix, 8, data->type);
747 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
751 dev_dbg(dev, "Unknown function %d.\n", fn);
754 return sprintf(buf, "%d\n", res);
757 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
758 const char *buf, size_t count)
760 struct dme1737_data *data = dev_get_drvdata(dev);
761 struct i2c_client *client = &data->client;
762 struct sensor_device_attribute_2
763 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
764 int ix = sensor_attr_2->index;
765 int fn = sensor_attr_2->nr;
766 long val = simple_strtol(buf, NULL, 10);
768 mutex_lock(&data->update_lock);
771 data->in_min[ix] = IN_TO_REG(val, ix, data->type);
772 dme1737_write(client, DME1737_REG_IN_MIN(ix),
776 data->in_max[ix] = IN_TO_REG(val, ix, data->type);
777 dme1737_write(client, DME1737_REG_IN_MAX(ix),
781 dev_dbg(dev, "Unknown function %d.\n", fn);
783 mutex_unlock(&data->update_lock);
788 /* ---------------------------------------------------------------------
789 * Temperature sysfs attributes
791 * --------------------------------------------------------------------- */
793 #define SYS_TEMP_INPUT 0
794 #define SYS_TEMP_MIN 1
795 #define SYS_TEMP_MAX 2
796 #define SYS_TEMP_OFFSET 3
797 #define SYS_TEMP_ALARM 4
798 #define SYS_TEMP_FAULT 5
800 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
803 struct dme1737_data *data = dme1737_update_device(dev);
804 struct sensor_device_attribute_2
805 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
806 int ix = sensor_attr_2->index;
807 int fn = sensor_attr_2->nr;
812 res = TEMP_FROM_REG(data->temp[ix], 16);
815 res = TEMP_FROM_REG(data->temp_min[ix], 8);
818 res = TEMP_FROM_REG(data->temp_max[ix], 8);
820 case SYS_TEMP_OFFSET:
821 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
824 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
827 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
831 dev_dbg(dev, "Unknown function %d.\n", fn);
834 return sprintf(buf, "%d\n", res);
837 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
838 const char *buf, size_t count)
840 struct dme1737_data *data = dev_get_drvdata(dev);
841 struct i2c_client *client = &data->client;
842 struct sensor_device_attribute_2
843 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
844 int ix = sensor_attr_2->index;
845 int fn = sensor_attr_2->nr;
846 long val = simple_strtol(buf, NULL, 10);
848 mutex_lock(&data->update_lock);
851 data->temp_min[ix] = TEMP_TO_REG(val);
852 dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
856 data->temp_max[ix] = TEMP_TO_REG(val);
857 dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
860 case SYS_TEMP_OFFSET:
861 data->temp_offset[ix] = TEMP_TO_REG(val);
862 dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
863 data->temp_offset[ix]);
866 dev_dbg(dev, "Unknown function %d.\n", fn);
868 mutex_unlock(&data->update_lock);
873 /* ---------------------------------------------------------------------
874 * Zone sysfs attributes
876 * --------------------------------------------------------------------- */
878 #define SYS_ZONE_AUTO_CHANNELS_TEMP 0
879 #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
880 #define SYS_ZONE_AUTO_POINT1_TEMP 2
881 #define SYS_ZONE_AUTO_POINT2_TEMP 3
882 #define SYS_ZONE_AUTO_POINT3_TEMP 4
884 static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
887 struct dme1737_data *data = dme1737_update_device(dev);
888 struct sensor_device_attribute_2
889 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
890 int ix = sensor_attr_2->index;
891 int fn = sensor_attr_2->nr;
895 case SYS_ZONE_AUTO_CHANNELS_TEMP:
896 /* check config2 for non-standard temp-to-zone mapping */
897 if ((ix == 1) && (data->config2 & 0x02)) {
903 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
904 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
905 TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
907 case SYS_ZONE_AUTO_POINT1_TEMP:
908 res = TEMP_FROM_REG(data->zone_low[ix], 8);
910 case SYS_ZONE_AUTO_POINT2_TEMP:
911 /* pwm_freq holds the temp range bits in the upper nibble */
912 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
913 TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
915 case SYS_ZONE_AUTO_POINT3_TEMP:
916 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
920 dev_dbg(dev, "Unknown function %d.\n", fn);
923 return sprintf(buf, "%d\n", res);
926 static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
927 const char *buf, size_t count)
929 struct dme1737_data *data = dev_get_drvdata(dev);
930 struct i2c_client *client = &data->client;
931 struct sensor_device_attribute_2
932 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
933 int ix = sensor_attr_2->index;
934 int fn = sensor_attr_2->nr;
935 long val = simple_strtol(buf, NULL, 10);
937 mutex_lock(&data->update_lock);
939 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
940 /* Refresh the cache */
941 data->zone_low[ix] = dme1737_read(client,
942 DME1737_REG_ZONE_LOW(ix));
943 /* Modify the temp hyst value */
944 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
945 TEMP_FROM_REG(data->zone_low[ix], 8) -
946 val, ix, dme1737_read(client,
947 DME1737_REG_ZONE_HYST(ix == 2)));
948 dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
949 data->zone_hyst[ix == 2]);
951 case SYS_ZONE_AUTO_POINT1_TEMP:
952 data->zone_low[ix] = TEMP_TO_REG(val);
953 dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
956 case SYS_ZONE_AUTO_POINT2_TEMP:
957 /* Refresh the cache */
958 data->zone_low[ix] = dme1737_read(client,
959 DME1737_REG_ZONE_LOW(ix));
960 /* Modify the temp range value (which is stored in the upper
961 * nibble of the pwm_freq register) */
962 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
963 TEMP_FROM_REG(data->zone_low[ix], 8),
965 DME1737_REG_PWM_FREQ(ix)));
966 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
969 case SYS_ZONE_AUTO_POINT3_TEMP:
970 data->zone_abs[ix] = TEMP_TO_REG(val);
971 dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
975 dev_dbg(dev, "Unknown function %d.\n", fn);
977 mutex_unlock(&data->update_lock);
982 /* ---------------------------------------------------------------------
983 * Fan sysfs attributes
985 * --------------------------------------------------------------------- */
987 #define SYS_FAN_INPUT 0
988 #define SYS_FAN_MIN 1
989 #define SYS_FAN_MAX 2
990 #define SYS_FAN_ALARM 3
991 #define SYS_FAN_TYPE 4
993 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
996 struct dme1737_data *data = dme1737_update_device(dev);
997 struct sensor_device_attribute_2
998 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
999 int ix = sensor_attr_2->index;
1000 int fn = sensor_attr_2->nr;
1005 res = FAN_FROM_REG(data->fan[ix],
1007 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1010 res = FAN_FROM_REG(data->fan_min[ix],
1012 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1015 /* only valid for fan[5-6] */
1016 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1019 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1022 /* only valid for fan[1-4] */
1023 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1027 dev_dbg(dev, "Unknown function %d.\n", fn);
1030 return sprintf(buf, "%d\n", res);
1033 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1034 const char *buf, size_t count)
1036 struct dme1737_data *data = dev_get_drvdata(dev);
1037 struct i2c_client *client = &data->client;
1038 struct sensor_device_attribute_2
1039 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1040 int ix = sensor_attr_2->index;
1041 int fn = sensor_attr_2->nr;
1042 long val = simple_strtol(buf, NULL, 10);
1044 mutex_lock(&data->update_lock);
1048 data->fan_min[ix] = FAN_TO_REG(val, 0);
1050 /* Refresh the cache */
1051 data->fan_opt[ix] = dme1737_read(client,
1052 DME1737_REG_FAN_OPT(ix));
1053 /* Modify the fan min value */
1054 data->fan_min[ix] = FAN_TO_REG(val,
1055 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1057 dme1737_write(client, DME1737_REG_FAN_MIN(ix),
1058 data->fan_min[ix] & 0xff);
1059 dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
1060 data->fan_min[ix] >> 8);
1063 /* Only valid for fan[5-6] */
1064 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1065 dme1737_write(client, DME1737_REG_FAN_MAX(ix),
1066 data->fan_max[ix - 4]);
1069 /* Only valid for fan[1-4] */
1070 if (!(val == 1 || val == 2 || val == 4)) {
1072 dev_warn(dev, "Fan type value %ld not "
1073 "supported. Choose one of 1, 2, or 4.\n",
1077 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
1078 DME1737_REG_FAN_OPT(ix)));
1079 dme1737_write(client, DME1737_REG_FAN_OPT(ix),
1083 dev_dbg(dev, "Unknown function %d.\n", fn);
1086 mutex_unlock(&data->update_lock);
1091 /* ---------------------------------------------------------------------
1092 * PWM sysfs attributes
1094 * --------------------------------------------------------------------- */
1097 #define SYS_PWM_FREQ 1
1098 #define SYS_PWM_ENABLE 2
1099 #define SYS_PWM_RAMP_RATE 3
1100 #define SYS_PWM_AUTO_CHANNELS_ZONE 4
1101 #define SYS_PWM_AUTO_PWM_MIN 5
1102 #define SYS_PWM_AUTO_POINT1_PWM 6
1103 #define SYS_PWM_AUTO_POINT2_PWM 7
1105 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1108 struct dme1737_data *data = dme1737_update_device(dev);
1109 struct sensor_device_attribute_2
1110 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1111 int ix = sensor_attr_2->index;
1112 int fn = sensor_attr_2->nr;
1117 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1120 res = data->pwm[ix];
1124 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1126 case SYS_PWM_ENABLE:
1128 res = 1; /* pwm[5-6] hard-wired to manual mode */
1130 res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1133 case SYS_PWM_RAMP_RATE:
1134 /* Only valid for pwm[1-3] */
1135 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1137 case SYS_PWM_AUTO_CHANNELS_ZONE:
1138 /* Only valid for pwm[1-3] */
1139 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1140 res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1142 res = data->pwm_acz[ix];
1145 case SYS_PWM_AUTO_PWM_MIN:
1146 /* Only valid for pwm[1-3] */
1147 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1148 res = data->pwm_min[ix];
1153 case SYS_PWM_AUTO_POINT1_PWM:
1154 /* Only valid for pwm[1-3] */
1155 res = data->pwm_min[ix];
1157 case SYS_PWM_AUTO_POINT2_PWM:
1158 /* Only valid for pwm[1-3] */
1159 res = 255; /* hard-wired */
1163 dev_dbg(dev, "Unknown function %d.\n", fn);
1166 return sprintf(buf, "%d\n", res);
1169 static struct attribute *dme1737_attr_pwm[];
1170 static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
1172 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1173 const char *buf, size_t count)
1175 struct dme1737_data *data = dev_get_drvdata(dev);
1176 struct i2c_client *client = &data->client;
1177 struct sensor_device_attribute_2
1178 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1179 int ix = sensor_attr_2->index;
1180 int fn = sensor_attr_2->nr;
1181 long val = simple_strtol(buf, NULL, 10);
1183 mutex_lock(&data->update_lock);
1186 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1187 dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
1190 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
1191 DME1737_REG_PWM_FREQ(ix)));
1192 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
1193 data->pwm_freq[ix]);
1195 case SYS_PWM_ENABLE:
1196 /* Only valid for pwm[1-3] */
1197 if (val < 0 || val > 2) {
1199 dev_warn(dev, "PWM enable %ld not "
1200 "supported. Choose one of 0, 1, or 2.\n",
1204 /* Refresh the cache */
1205 data->pwm_config[ix] = dme1737_read(client,
1206 DME1737_REG_PWM_CONFIG(ix));
1207 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1208 /* Bail out if no change */
1211 /* Do some housekeeping if we are currently in auto mode */
1212 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1213 /* Save the current zone channel assignment */
1214 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1215 data->pwm_config[ix]);
1216 /* Save the current ramp rate state and disable it */
1217 data->pwm_rr[ix > 0] = dme1737_read(client,
1218 DME1737_REG_PWM_RR(ix > 0));
1219 data->pwm_rr_en &= ~(1 << ix);
1220 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1221 data->pwm_rr_en |= (1 << ix);
1222 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1223 data->pwm_rr[ix > 0]);
1224 dme1737_write(client,
1225 DME1737_REG_PWM_RR(ix > 0),
1226 data->pwm_rr[ix > 0]);
1229 /* Set the new PWM mode */
1232 /* Change permissions of pwm[ix] to read-only */
1233 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1235 /* Turn fan fully on */
1236 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1237 data->pwm_config[ix]);
1238 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1239 data->pwm_config[ix]);
1242 /* Turn on manual mode */
1243 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1244 data->pwm_config[ix]);
1245 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1246 data->pwm_config[ix]);
1247 /* Change permissions of pwm[ix] to read-writeable */
1248 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1252 /* Change permissions of pwm[ix] to read-only */
1253 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1255 /* Turn on auto mode using the saved zone channel
1257 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1259 data->pwm_config[ix]);
1260 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1261 data->pwm_config[ix]);
1262 /* Enable PWM ramp rate if previously enabled */
1263 if (data->pwm_rr_en & (1 << ix)) {
1264 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1265 dme1737_read(client,
1266 DME1737_REG_PWM_RR(ix > 0)));
1267 dme1737_write(client,
1268 DME1737_REG_PWM_RR(ix > 0),
1269 data->pwm_rr[ix > 0]);
1274 case SYS_PWM_RAMP_RATE:
1275 /* Only valid for pwm[1-3] */
1276 /* Refresh the cache */
1277 data->pwm_config[ix] = dme1737_read(client,
1278 DME1737_REG_PWM_CONFIG(ix));
1279 data->pwm_rr[ix > 0] = dme1737_read(client,
1280 DME1737_REG_PWM_RR(ix > 0));
1281 /* Set the ramp rate value */
1283 data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1284 data->pwm_rr[ix > 0]);
1286 /* Enable/disable the feature only if the associated PWM
1287 * output is in automatic mode. */
1288 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1289 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1290 data->pwm_rr[ix > 0]);
1292 dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
1293 data->pwm_rr[ix > 0]);
1295 case SYS_PWM_AUTO_CHANNELS_ZONE:
1296 /* Only valid for pwm[1-3] */
1297 if (!(val == 1 || val == 2 || val == 4 ||
1298 val == 6 || val == 7)) {
1300 dev_warn(dev, "PWM auto channels zone %ld "
1301 "not supported. Choose one of 1, 2, 4, 6, "
1305 /* Refresh the cache */
1306 data->pwm_config[ix] = dme1737_read(client,
1307 DME1737_REG_PWM_CONFIG(ix));
1308 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1309 /* PWM is already in auto mode so update the temp
1310 * channel assignment */
1311 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1312 data->pwm_config[ix]);
1313 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1314 data->pwm_config[ix]);
1316 /* PWM is not in auto mode so we save the temp
1317 * channel assignment for later use */
1318 data->pwm_acz[ix] = val;
1321 case SYS_PWM_AUTO_PWM_MIN:
1322 /* Only valid for pwm[1-3] */
1323 /* Refresh the cache */
1324 data->pwm_min[ix] = dme1737_read(client,
1325 DME1737_REG_PWM_MIN(ix));
1326 /* There are only 2 values supported for the auto_pwm_min
1327 * value: 0 or auto_point1_pwm. So if the temperature drops
1328 * below the auto_point1_temp_hyst value, the fan either turns
1329 * off or runs at auto_point1_pwm duty-cycle. */
1330 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1331 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1332 dme1737_read(client,
1333 DME1737_REG_PWM_RR(0)));
1335 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1336 dme1737_read(client,
1337 DME1737_REG_PWM_RR(0)));
1339 dme1737_write(client, DME1737_REG_PWM_RR(0),
1342 case SYS_PWM_AUTO_POINT1_PWM:
1343 /* Only valid for pwm[1-3] */
1344 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1345 dme1737_write(client, DME1737_REG_PWM_MIN(ix),
1349 dev_dbg(dev, "Unknown function %d.\n", fn);
1352 mutex_unlock(&data->update_lock);
1357 /* ---------------------------------------------------------------------
1358 * Miscellaneous sysfs attributes
1359 * --------------------------------------------------------------------- */
1361 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1364 struct i2c_client *client = to_i2c_client(dev);
1365 struct dme1737_data *data = i2c_get_clientdata(client);
1367 return sprintf(buf, "%d\n", data->vrm);
1370 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1371 const char *buf, size_t count)
1373 struct dme1737_data *data = dev_get_drvdata(dev);
1374 long val = simple_strtol(buf, NULL, 10);
1380 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1383 struct dme1737_data *data = dme1737_update_device(dev);
1385 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1388 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1391 struct dme1737_data *data = dev_get_drvdata(dev);
1393 return sprintf(buf, "%s\n", data->client.name);
1396 /* ---------------------------------------------------------------------
1397 * Sysfs device attribute defines and structs
1398 * --------------------------------------------------------------------- */
1402 #define SENSOR_DEVICE_ATTR_IN(ix) \
1403 static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
1404 show_in, NULL, SYS_IN_INPUT, ix); \
1405 static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
1406 show_in, set_in, SYS_IN_MIN, ix); \
1407 static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
1408 show_in, set_in, SYS_IN_MAX, ix); \
1409 static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
1410 show_in, NULL, SYS_IN_ALARM, ix)
1412 SENSOR_DEVICE_ATTR_IN(0);
1413 SENSOR_DEVICE_ATTR_IN(1);
1414 SENSOR_DEVICE_ATTR_IN(2);
1415 SENSOR_DEVICE_ATTR_IN(3);
1416 SENSOR_DEVICE_ATTR_IN(4);
1417 SENSOR_DEVICE_ATTR_IN(5);
1418 SENSOR_DEVICE_ATTR_IN(6);
1420 /* Temperatures 1-3 */
1422 #define SENSOR_DEVICE_ATTR_TEMP(ix) \
1423 static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
1424 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
1425 static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
1426 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
1427 static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
1428 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
1429 static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
1430 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
1431 static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
1432 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
1433 static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
1434 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
1436 SENSOR_DEVICE_ATTR_TEMP(1);
1437 SENSOR_DEVICE_ATTR_TEMP(2);
1438 SENSOR_DEVICE_ATTR_TEMP(3);
1442 #define SENSOR_DEVICE_ATTR_ZONE(ix) \
1443 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
1444 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
1445 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
1446 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
1447 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
1448 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
1449 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
1450 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
1451 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
1452 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
1454 SENSOR_DEVICE_ATTR_ZONE(1);
1455 SENSOR_DEVICE_ATTR_ZONE(2);
1456 SENSOR_DEVICE_ATTR_ZONE(3);
1460 #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1461 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1462 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1463 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1464 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1465 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1466 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1467 static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
1468 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
1470 SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1471 SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1472 SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1473 SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1477 #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1478 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1479 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1480 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1481 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1482 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1483 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1484 static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
1485 show_fan, set_fan, SYS_FAN_MAX, ix-1)
1487 SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1488 SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1492 #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1493 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1494 show_pwm, set_pwm, SYS_PWM, ix-1); \
1495 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1496 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1497 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1498 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
1499 static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
1500 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
1501 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
1502 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
1503 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
1504 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
1505 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
1506 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
1507 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
1508 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
1510 SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1511 SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1512 SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1516 #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
1517 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1518 show_pwm, set_pwm, SYS_PWM, ix-1); \
1519 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1520 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1521 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1522 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
1524 SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1525 SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1529 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1530 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1531 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */
1533 /* This struct holds all the attributes that are always present and need to be
1534 * created unconditionally. The attributes that need modification of their
1535 * permissions are created read-only and write permissions are added or removed
1536 * on the fly when required */
1537 static struct attribute *dme1737_attr[] ={
1539 &sensor_dev_attr_in0_input.dev_attr.attr,
1540 &sensor_dev_attr_in0_min.dev_attr.attr,
1541 &sensor_dev_attr_in0_max.dev_attr.attr,
1542 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1543 &sensor_dev_attr_in1_input.dev_attr.attr,
1544 &sensor_dev_attr_in1_min.dev_attr.attr,
1545 &sensor_dev_attr_in1_max.dev_attr.attr,
1546 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1547 &sensor_dev_attr_in2_input.dev_attr.attr,
1548 &sensor_dev_attr_in2_min.dev_attr.attr,
1549 &sensor_dev_attr_in2_max.dev_attr.attr,
1550 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1551 &sensor_dev_attr_in3_input.dev_attr.attr,
1552 &sensor_dev_attr_in3_min.dev_attr.attr,
1553 &sensor_dev_attr_in3_max.dev_attr.attr,
1554 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1555 &sensor_dev_attr_in4_input.dev_attr.attr,
1556 &sensor_dev_attr_in4_min.dev_attr.attr,
1557 &sensor_dev_attr_in4_max.dev_attr.attr,
1558 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1559 &sensor_dev_attr_in5_input.dev_attr.attr,
1560 &sensor_dev_attr_in5_min.dev_attr.attr,
1561 &sensor_dev_attr_in5_max.dev_attr.attr,
1562 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1563 &sensor_dev_attr_in6_input.dev_attr.attr,
1564 &sensor_dev_attr_in6_min.dev_attr.attr,
1565 &sensor_dev_attr_in6_max.dev_attr.attr,
1566 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1568 &sensor_dev_attr_temp1_input.dev_attr.attr,
1569 &sensor_dev_attr_temp1_min.dev_attr.attr,
1570 &sensor_dev_attr_temp1_max.dev_attr.attr,
1571 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1572 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1573 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1574 &sensor_dev_attr_temp2_input.dev_attr.attr,
1575 &sensor_dev_attr_temp2_min.dev_attr.attr,
1576 &sensor_dev_attr_temp2_max.dev_attr.attr,
1577 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1578 &sensor_dev_attr_temp2_fault.dev_attr.attr,
1579 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1580 &sensor_dev_attr_temp3_input.dev_attr.attr,
1581 &sensor_dev_attr_temp3_min.dev_attr.attr,
1582 &sensor_dev_attr_temp3_max.dev_attr.attr,
1583 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1584 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1585 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1587 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1588 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1589 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1590 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1591 &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
1592 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1593 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1594 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1595 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1596 &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
1597 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1598 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1599 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1600 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1601 &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
1604 &dev_attr_cpu0_vid.attr,
1608 static const struct attribute_group dme1737_group = {
1609 .attrs = dme1737_attr,
1612 /* The following structs hold the PWM attributes, some of which are optional.
1613 * Their creation depends on the chip configuration which is determined during
1615 static struct attribute *dme1737_attr_pwm1[] = {
1616 &sensor_dev_attr_pwm1.dev_attr.attr,
1617 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1618 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1619 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1620 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1621 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1622 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1623 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1626 static struct attribute *dme1737_attr_pwm2[] = {
1627 &sensor_dev_attr_pwm2.dev_attr.attr,
1628 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1629 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1630 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1631 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1632 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1633 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1634 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1637 static struct attribute *dme1737_attr_pwm3[] = {
1638 &sensor_dev_attr_pwm3.dev_attr.attr,
1639 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1640 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1641 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1642 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1643 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1644 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1645 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1648 static struct attribute *dme1737_attr_pwm5[] = {
1649 &sensor_dev_attr_pwm5.dev_attr.attr,
1650 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1651 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1654 static struct attribute *dme1737_attr_pwm6[] = {
1655 &sensor_dev_attr_pwm6.dev_attr.attr,
1656 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1657 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1661 static const struct attribute_group dme1737_pwm_group[] = {
1662 { .attrs = dme1737_attr_pwm1 },
1663 { .attrs = dme1737_attr_pwm2 },
1664 { .attrs = dme1737_attr_pwm3 },
1666 { .attrs = dme1737_attr_pwm5 },
1667 { .attrs = dme1737_attr_pwm6 },
1670 /* The following structs hold the fan attributes, some of which are optional.
1671 * Their creation depends on the chip configuration which is determined during
1673 static struct attribute *dme1737_attr_fan1[] = {
1674 &sensor_dev_attr_fan1_input.dev_attr.attr,
1675 &sensor_dev_attr_fan1_min.dev_attr.attr,
1676 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1677 &sensor_dev_attr_fan1_type.dev_attr.attr,
1680 static struct attribute *dme1737_attr_fan2[] = {
1681 &sensor_dev_attr_fan2_input.dev_attr.attr,
1682 &sensor_dev_attr_fan2_min.dev_attr.attr,
1683 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1684 &sensor_dev_attr_fan2_type.dev_attr.attr,
1687 static struct attribute *dme1737_attr_fan3[] = {
1688 &sensor_dev_attr_fan3_input.dev_attr.attr,
1689 &sensor_dev_attr_fan3_min.dev_attr.attr,
1690 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1691 &sensor_dev_attr_fan3_type.dev_attr.attr,
1694 static struct attribute *dme1737_attr_fan4[] = {
1695 &sensor_dev_attr_fan4_input.dev_attr.attr,
1696 &sensor_dev_attr_fan4_min.dev_attr.attr,
1697 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1698 &sensor_dev_attr_fan4_type.dev_attr.attr,
1701 static struct attribute *dme1737_attr_fan5[] = {
1702 &sensor_dev_attr_fan5_input.dev_attr.attr,
1703 &sensor_dev_attr_fan5_min.dev_attr.attr,
1704 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1705 &sensor_dev_attr_fan5_max.dev_attr.attr,
1708 static struct attribute *dme1737_attr_fan6[] = {
1709 &sensor_dev_attr_fan6_input.dev_attr.attr,
1710 &sensor_dev_attr_fan6_min.dev_attr.attr,
1711 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1712 &sensor_dev_attr_fan6_max.dev_attr.attr,
1716 static const struct attribute_group dme1737_fan_group[] = {
1717 { .attrs = dme1737_attr_fan1 },
1718 { .attrs = dme1737_attr_fan2 },
1719 { .attrs = dme1737_attr_fan3 },
1720 { .attrs = dme1737_attr_fan4 },
1721 { .attrs = dme1737_attr_fan5 },
1722 { .attrs = dme1737_attr_fan6 },
1725 /* The permissions of all of the following attributes are changed to read-
1726 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1727 static struct attribute *dme1737_attr_lock[] = {
1729 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1730 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1731 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1733 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1734 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1735 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1736 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1737 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1738 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1739 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1740 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1741 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1742 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1743 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1744 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1748 static const struct attribute_group dme1737_lock_group = {
1749 .attrs = dme1737_attr_lock,
1752 /* The permissions of the following PWM attributes are changed to read-
1753 * writeable if the chip is *not* locked and the respective PWM is available.
1754 * Otherwise they stay read-only. */
1755 static struct attribute *dme1737_attr_pwm1_lock[] = {
1756 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1757 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1758 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1759 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1760 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1761 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1764 static struct attribute *dme1737_attr_pwm2_lock[] = {
1765 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1766 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1767 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1768 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1769 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1770 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1773 static struct attribute *dme1737_attr_pwm3_lock[] = {
1774 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1775 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1776 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1777 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1778 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1779 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1782 static struct attribute *dme1737_attr_pwm5_lock[] = {
1783 &sensor_dev_attr_pwm5.dev_attr.attr,
1784 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1787 static struct attribute *dme1737_attr_pwm6_lock[] = {
1788 &sensor_dev_attr_pwm6.dev_attr.attr,
1789 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1793 static const struct attribute_group dme1737_pwm_lock_group[] = {
1794 { .attrs = dme1737_attr_pwm1_lock },
1795 { .attrs = dme1737_attr_pwm2_lock },
1796 { .attrs = dme1737_attr_pwm3_lock },
1798 { .attrs = dme1737_attr_pwm5_lock },
1799 { .attrs = dme1737_attr_pwm6_lock },
1802 /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1803 * chip is not locked. Otherwise they are read-only. */
1804 static struct attribute *dme1737_attr_pwm[] = {
1805 &sensor_dev_attr_pwm1.dev_attr.attr,
1806 &sensor_dev_attr_pwm2.dev_attr.attr,
1807 &sensor_dev_attr_pwm3.dev_attr.attr,
1810 /* ---------------------------------------------------------------------
1811 * Super-IO functions
1812 * --------------------------------------------------------------------- */
1814 static inline void dme1737_sio_enter(int sio_cip)
1816 outb(0x55, sio_cip);
1819 static inline void dme1737_sio_exit(int sio_cip)
1821 outb(0xaa, sio_cip);
1824 static inline int dme1737_sio_inb(int sio_cip, int reg)
1827 return inb(sio_cip + 1);
1830 static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1833 outb(val, sio_cip + 1);
1836 /* ---------------------------------------------------------------------
1837 * Device initialization
1838 * --------------------------------------------------------------------- */
1840 static int dme1737_i2c_get_features(int, struct dme1737_data*);
1842 static void dme1737_chmod_file(struct device *dev,
1843 struct attribute *attr, mode_t mode)
1845 if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1846 dev_warn(dev, "Failed to change permissions of %s.\n",
1851 static void dme1737_chmod_group(struct device *dev,
1852 const struct attribute_group *group,
1855 struct attribute **attr;
1857 for (attr = group->attrs; *attr; attr++) {
1858 dme1737_chmod_file(dev, *attr, mode);
1862 static void dme1737_remove_files(struct device *dev)
1864 struct dme1737_data *data = dev_get_drvdata(dev);
1867 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1868 if (data->has_fan & (1 << ix)) {
1869 sysfs_remove_group(&dev->kobj,
1870 &dme1737_fan_group[ix]);
1874 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1875 if (data->has_pwm & (1 << ix)) {
1876 sysfs_remove_group(&dev->kobj,
1877 &dme1737_pwm_group[ix]);
1881 sysfs_remove_group(&dev->kobj, &dme1737_group);
1883 if (!data->client.driver) {
1884 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
1888 static int dme1737_create_files(struct device *dev)
1890 struct dme1737_data *data = dev_get_drvdata(dev);
1893 /* Create a name attribute for ISA devices */
1894 if (!data->client.driver &&
1895 (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
1899 /* Create standard sysfs attributes */
1900 if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
1904 /* Create fan sysfs attributes */
1905 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1906 if (data->has_fan & (1 << ix)) {
1907 if ((err = sysfs_create_group(&dev->kobj,
1908 &dme1737_fan_group[ix]))) {
1914 /* Create PWM sysfs attributes */
1915 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1916 if (data->has_pwm & (1 << ix)) {
1917 if ((err = sysfs_create_group(&dev->kobj,
1918 &dme1737_pwm_group[ix]))) {
1924 /* Inform if the device is locked. Otherwise change the permissions of
1925 * selected attributes from read-only to read-writeable. */
1926 if (data->config & 0x02) {
1927 dev_info(dev, "Device is locked. Some attributes "
1928 "will be read-only.\n");
1930 /* Change permissions of standard attributes */
1931 dme1737_chmod_group(dev, &dme1737_lock_group,
1934 /* Change permissions of PWM attributes */
1935 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
1936 if (data->has_pwm & (1 << ix)) {
1937 dme1737_chmod_group(dev,
1938 &dme1737_pwm_lock_group[ix],
1943 /* Change permissions of pwm[1-3] if in manual mode */
1944 for (ix = 0; ix < 3; ix++) {
1945 if ((data->has_pwm & (1 << ix)) &&
1946 (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
1947 dme1737_chmod_file(dev,
1948 dme1737_attr_pwm[ix],
1957 dme1737_remove_files(dev);
1962 static int dme1737_init_device(struct device *dev)
1964 struct dme1737_data *data = dev_get_drvdata(dev);
1965 struct i2c_client *client = &data->client;
1969 data->config = dme1737_read(client, DME1737_REG_CONFIG);
1970 /* Inform if part is not monitoring/started */
1971 if (!(data->config & 0x01)) {
1973 dev_err(dev, "Device is not monitoring. "
1974 "Use the force_start load parameter to "
1979 /* Force monitoring */
1980 data->config |= 0x01;
1981 dme1737_write(client, DME1737_REG_CONFIG, data->config);
1983 /* Inform if part is not ready */
1984 if (!(data->config & 0x04)) {
1985 dev_err(dev, "Device is not ready.\n");
1989 /* Determine which optional fan and pwm features are enabled/present */
1990 if (client->driver) { /* I2C chip */
1991 data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
1992 /* Check if optional fan3 input is enabled */
1993 if (data->config2 & 0x04) {
1994 data->has_fan |= (1 << 2);
1997 /* Fan4 and pwm3 are only available if the client's I2C address
1998 * is the default 0x2e. Otherwise the I/Os associated with
1999 * these functions are used for addr enable/select. */
2000 if (data->client.addr == 0x2e) {
2001 data->has_fan |= (1 << 3);
2002 data->has_pwm |= (1 << 2);
2005 /* Determine which of the optional fan[5-6] and pwm[5-6]
2006 * features are enabled. For this, we need to query the runtime
2007 * registers through the Super-IO LPC interface. Try both
2008 * config ports 0x2e and 0x4e. */
2009 if (dme1737_i2c_get_features(0x2e, data) &&
2010 dme1737_i2c_get_features(0x4e, data)) {
2011 dev_warn(dev, "Failed to query Super-IO for optional "
2014 } else { /* ISA chip */
2015 /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6]
2016 * don't exist in the ISA chip. */
2017 data->has_fan |= (1 << 2);
2018 data->has_pwm |= (1 << 2);
2021 /* Fan1, fan2, pwm1, and pwm2 are always present */
2022 data->has_fan |= 0x03;
2023 data->has_pwm |= 0x03;
2025 dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
2026 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
2027 (data->has_pwm & (1 << 2)) ? "yes" : "no",
2028 (data->has_pwm & (1 << 4)) ? "yes" : "no",
2029 (data->has_pwm & (1 << 5)) ? "yes" : "no",
2030 (data->has_fan & (1 << 2)) ? "yes" : "no",
2031 (data->has_fan & (1 << 3)) ? "yes" : "no",
2032 (data->has_fan & (1 << 4)) ? "yes" : "no",
2033 (data->has_fan & (1 << 5)) ? "yes" : "no");
2035 reg = dme1737_read(client, DME1737_REG_TACH_PWM);
2036 /* Inform if fan-to-pwm mapping differs from the default */
2037 if (client->driver && reg != 0xa4) { /* I2C chip */
2038 dev_warn(dev, "Non-standard fan to pwm mapping: "
2039 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
2040 "fan4->pwm%d. Please report to the driver "
2042 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2043 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
2044 } else if (!client->driver && reg != 0x24) { /* ISA chip */
2045 dev_warn(dev, "Non-standard fan to pwm mapping: "
2046 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2047 "Please report to the driver maintainer.\n",
2048 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2049 ((reg >> 4) & 0x03) + 1);
2052 /* Switch pwm[1-3] to manual mode if they are currently disabled and
2053 * set the duty-cycles to 0% (which is identical to the PWMs being
2055 if (!(data->config & 0x02)) {
2056 for (ix = 0; ix < 3; ix++) {
2057 data->pwm_config[ix] = dme1737_read(client,
2058 DME1737_REG_PWM_CONFIG(ix));
2059 if ((data->has_pwm & (1 << ix)) &&
2060 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
2061 dev_info(dev, "Switching pwm%d to "
2062 "manual mode.\n", ix + 1);
2063 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2064 data->pwm_config[ix]);
2065 dme1737_write(client, DME1737_REG_PWM(ix), 0);
2066 dme1737_write(client,
2067 DME1737_REG_PWM_CONFIG(ix),
2068 data->pwm_config[ix]);
2073 /* Initialize the default PWM auto channels zone (acz) assignments */
2074 data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
2075 data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
2076 data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
2079 data->vrm = vid_which_vrm();
2084 /* ---------------------------------------------------------------------
2085 * I2C device detection and registration
2086 * --------------------------------------------------------------------- */
2088 static struct i2c_driver dme1737_i2c_driver;
2090 static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2095 dme1737_sio_enter(sio_cip);
2098 * The DME1737 can return either 0x78 or 0x77 as its device ID. */
2099 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2100 if (!(reg == 0x77 || reg == 0x78)) {
2105 /* Select logical device A (runtime registers) */
2106 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2108 /* Get the base address of the runtime registers */
2109 if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2110 dme1737_sio_inb(sio_cip, 0x61))) {
2115 /* Read the runtime registers to determine which optional features
2116 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2117 * to '10' if the respective feature is enabled. */
2118 if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
2119 data->has_fan |= (1 << 5);
2121 if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
2122 data->has_pwm |= (1 << 5);
2124 if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
2125 data->has_fan |= (1 << 4);
2127 if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
2128 data->has_pwm |= (1 << 4);
2132 dme1737_sio_exit(sio_cip);
2137 static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2140 u8 company, verstep = 0;
2141 struct i2c_client *client;
2142 struct dme1737_data *data;
2147 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
2151 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2156 client = &data->client;
2157 i2c_set_clientdata(client, data);
2158 client->addr = address;
2159 client->adapter = adapter;
2160 client->driver = &dme1737_i2c_driver;
2163 /* A negative kind means that the driver was loaded with no force
2164 * parameter (default), so we must identify the chip. */
2166 company = dme1737_read(client, DME1737_REG_COMPANY);
2167 verstep = dme1737_read(client, DME1737_REG_VERSTEP);
2169 if (!((company == DME1737_COMPANY_SMSC) &&
2170 ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
2180 /* Fill in the remaining client fields and put it into the global
2182 strlcpy(client->name, name, I2C_NAME_SIZE);
2183 mutex_init(&data->update_lock);
2185 /* Tell the I2C layer a new client has arrived */
2186 if ((err = i2c_attach_client(client))) {
2190 dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2191 client->addr, verstep);
2193 /* Initialize the DME1737 chip */
2194 if ((err = dme1737_init_device(dev))) {
2195 dev_err(dev, "Failed to initialize device.\n");
2199 /* Create sysfs files */
2200 if ((err = dme1737_create_files(dev))) {
2201 dev_err(dev, "Failed to create sysfs files.\n");
2205 /* Register device */
2206 data->hwmon_dev = hwmon_device_register(dev);
2207 if (IS_ERR(data->hwmon_dev)) {
2208 dev_err(dev, "Failed to register device.\n");
2209 err = PTR_ERR(data->hwmon_dev);
2216 dme1737_remove_files(dev);
2218 i2c_detach_client(client);
2225 static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
2227 if (!(adapter->class & I2C_CLASS_HWMON)) {
2231 return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
2234 static int dme1737_i2c_detach_client(struct i2c_client *client)
2236 struct dme1737_data *data = i2c_get_clientdata(client);
2239 hwmon_device_unregister(data->hwmon_dev);
2240 dme1737_remove_files(&client->dev);
2242 if ((err = i2c_detach_client(client))) {
2250 static struct i2c_driver dme1737_i2c_driver = {
2254 .attach_adapter = dme1737_i2c_attach_adapter,
2255 .detach_client = dme1737_i2c_detach_client,
2258 /* ---------------------------------------------------------------------
2259 * ISA device detection and registration
2260 * --------------------------------------------------------------------- */
2262 static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2265 unsigned short base_addr;
2267 dme1737_sio_enter(sio_cip);
2270 * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and
2271 * SCH3116 (0x7f). */
2272 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2273 if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
2278 /* Select logical device A (runtime registers) */
2279 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2281 /* Get the base address of the runtime registers */
2282 if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2283 dme1737_sio_inb(sio_cip, 0x61))) {
2284 printk(KERN_ERR "dme1737: Base address not set.\n");
2289 /* Access to the hwmon registers is through an index/data register
2290 * pair located at offset 0x70/0x71. */
2291 *addr = base_addr + 0x70;
2294 dme1737_sio_exit(sio_cip);
2298 static int __init dme1737_isa_device_add(unsigned short addr)
2300 struct resource res = {
2302 .end = addr + DME1737_EXTENT - 1,
2304 .flags = IORESOURCE_IO,
2308 if (!(pdev = platform_device_alloc("dme1737", addr))) {
2309 printk(KERN_ERR "dme1737: Failed to allocate device.\n");
2314 if ((err = platform_device_add_resources(pdev, &res, 1))) {
2315 printk(KERN_ERR "dme1737: Failed to add device resource "
2316 "(err = %d).\n", err);
2317 goto exit_device_put;
2320 if ((err = platform_device_add(pdev))) {
2321 printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n",
2323 goto exit_device_put;
2329 platform_device_put(pdev);
2335 static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2338 struct resource *res;
2339 struct i2c_client *client;
2340 struct dme1737_data *data;
2341 struct device *dev = &pdev->dev;
2344 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2345 if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
2346 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2347 (unsigned short)res->start,
2348 (unsigned short)res->start + DME1737_EXTENT - 1);
2353 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2355 goto exit_release_region;
2358 client = &data->client;
2359 i2c_set_clientdata(client, data);
2360 client->addr = res->start;
2361 platform_set_drvdata(pdev, data);
2363 company = dme1737_read(client, DME1737_REG_COMPANY);
2364 device = dme1737_read(client, DME1737_REG_DEVICE);
2366 if (!((company == DME1737_COMPANY_SMSC) &&
2367 (device == SCH311X_DEVICE))) {
2373 /* Fill in the remaining client fields and initialize the mutex */
2374 strlcpy(client->name, "sch311x", I2C_NAME_SIZE);
2375 mutex_init(&data->update_lock);
2377 dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr);
2379 /* Initialize the chip */
2380 if ((err = dme1737_init_device(dev))) {
2381 dev_err(dev, "Failed to initialize device.\n");
2385 /* Create sysfs files */
2386 if ((err = dme1737_create_files(dev))) {
2387 dev_err(dev, "Failed to create sysfs files.\n");
2391 /* Register device */
2392 data->hwmon_dev = hwmon_device_register(dev);
2393 if (IS_ERR(data->hwmon_dev)) {
2394 dev_err(dev, "Failed to register device.\n");
2395 err = PTR_ERR(data->hwmon_dev);
2396 goto exit_remove_files;
2402 dme1737_remove_files(dev);
2404 platform_set_drvdata(pdev, NULL);
2406 exit_release_region:
2407 release_region(res->start, DME1737_EXTENT);
2412 static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2414 struct dme1737_data *data = platform_get_drvdata(pdev);
2416 hwmon_device_unregister(data->hwmon_dev);
2417 dme1737_remove_files(&pdev->dev);
2418 release_region(data->client.addr, DME1737_EXTENT);
2419 platform_set_drvdata(pdev, NULL);
2425 static struct platform_driver dme1737_isa_driver = {
2427 .owner = THIS_MODULE,
2430 .probe = dme1737_isa_probe,
2431 .remove = __devexit_p(dme1737_isa_remove),
2434 /* ---------------------------------------------------------------------
2435 * Module initialization and cleanup
2436 * --------------------------------------------------------------------- */
2438 static int __init dme1737_init(void)
2441 unsigned short addr;
2443 if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
2447 if (dme1737_isa_detect(0x2e, &addr) &&
2448 dme1737_isa_detect(0x4e, &addr) &&
2450 (dme1737_isa_detect(0x162e, &addr) &&
2451 dme1737_isa_detect(0x164e, &addr)))) {
2452 /* Return 0 if we didn't find an ISA device */
2456 if ((err = platform_driver_register(&dme1737_isa_driver))) {
2457 goto exit_del_i2c_driver;
2460 /* Sets global pdev as a side effect */
2461 if ((err = dme1737_isa_device_add(addr))) {
2462 goto exit_del_isa_driver;
2467 exit_del_isa_driver:
2468 platform_driver_unregister(&dme1737_isa_driver);
2469 exit_del_i2c_driver:
2470 i2c_del_driver(&dme1737_i2c_driver);
2475 static void __exit dme1737_exit(void)
2478 platform_device_unregister(pdev);
2479 platform_driver_unregister(&dme1737_isa_driver);
2482 i2c_del_driver(&dme1737_i2c_driver);
2485 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2486 MODULE_DESCRIPTION("DME1737 sensors");
2487 MODULE_LICENSE("GPL");
2489 module_init(dme1737_init);
2490 module_exit(dme1737_exit);