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 */
190 /* Register values */
217 /* Nominal voltage values */
218 static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
221 * Voltage inputs have 16 bits resolution, limit values have 8 bits
223 static inline int IN_FROM_REG(int reg, int ix, int res)
225 return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2));
228 static inline int IN_TO_REG(int val, int ix)
230 return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) /
231 IN_NOMINAL[ix], 0, 255);
235 * The register values represent temperatures in 2's complement notation from
236 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
237 * values have 8 bits resolution. */
238 static inline int TEMP_FROM_REG(int reg, int res)
240 return (reg * 1000) >> (res - 8);
243 static inline int TEMP_TO_REG(int val)
245 return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
249 /* Temperature range */
250 static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
251 10000, 13333, 16000, 20000, 26666, 32000,
252 40000, 53333, 80000};
254 static inline int TEMP_RANGE_FROM_REG(int reg)
256 return TEMP_RANGE[(reg >> 4) & 0x0f];
259 static int TEMP_RANGE_TO_REG(int val, int reg)
263 for (i = 15; i > 0; i--) {
264 if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
269 return (reg & 0x0f) | (i << 4);
272 /* Temperature hysteresis
274 * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
275 * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
276 static inline int TEMP_HYST_FROM_REG(int reg, int ix)
278 return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
281 static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
283 int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
285 return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
289 static inline int FAN_FROM_REG(int reg, int tpc)
294 return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
298 static inline int FAN_TO_REG(int val, int tpc)
301 return SENSORS_LIMIT(val / tpc, 0, 0xffff);
303 return (val <= 0) ? 0xffff :
304 SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
308 /* Fan TPC (tach pulse count)
309 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
310 * is configured in legacy (non-tpc) mode */
311 static inline int FAN_TPC_FROM_REG(int reg)
313 return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
317 * The type of a fan is expressed in number of pulses-per-revolution that it
319 static inline int FAN_TYPE_FROM_REG(int reg)
321 int edge = (reg >> 1) & 0x03;
323 return (edge > 0) ? 1 << (edge - 1) : 0;
326 static inline int FAN_TYPE_TO_REG(int val, int reg)
328 int edge = (val == 4) ? 3 : val;
330 return (reg & 0xf9) | (edge << 1);
334 static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
337 static int FAN_MAX_FROM_REG(int reg)
341 for (i = 10; i > 0; i--) {
342 if (reg == FAN_MAX[i]) {
347 return 1000 + i * 500;
350 static int FAN_MAX_TO_REG(int val)
354 for (i = 10; i > 0; i--) {
355 if (val > (1000 + (i - 1) * 500)) {
364 * Register to enable mapping:
365 * 000: 2 fan on zone 1 auto
366 * 001: 2 fan on zone 2 auto
367 * 010: 2 fan on zone 3 auto
369 * 100: -1 fan disabled
370 * 101: 2 fan on hottest of zones 2,3 auto
371 * 110: 2 fan on hottest of zones 1,2,3 auto
372 * 111: 1 fan in manual mode */
373 static inline int PWM_EN_FROM_REG(int reg)
375 static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
377 return en[(reg >> 5) & 0x07];
380 static inline int PWM_EN_TO_REG(int val, int reg)
382 int en = (val == 1) ? 7 : 3;
384 return (reg & 0x1f) | ((en & 0x07) << 5);
387 /* PWM auto channels zone
388 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
389 * corresponding to zone x+1):
390 * 000: 001 fan on zone 1 auto
391 * 001: 010 fan on zone 2 auto
392 * 010: 100 fan on zone 3 auto
393 * 011: 000 fan full on
394 * 100: 000 fan disabled
395 * 101: 110 fan on hottest of zones 2,3 auto
396 * 110: 111 fan on hottest of zones 1,2,3 auto
397 * 111: 000 fan in manual mode */
398 static inline int PWM_ACZ_FROM_REG(int reg)
400 static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
402 return acz[(reg >> 5) & 0x07];
405 static inline int PWM_ACZ_TO_REG(int val, int reg)
407 int acz = (val == 4) ? 2 : val - 1;
409 return (reg & 0x1f) | ((acz & 0x07) << 5);
413 static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
414 15000, 20000, 30000, 25000, 0, 0, 0, 0};
416 static inline int PWM_FREQ_FROM_REG(int reg)
418 return PWM_FREQ[reg & 0x0f];
421 static int PWM_FREQ_TO_REG(int val, int reg)
425 /* the first two cases are special - stupid chip design! */
428 } else if (val > 22500) {
431 for (i = 9; i > 0; i--) {
432 if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
438 return (reg & 0xf0) | i;
443 * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
444 * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
445 static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
447 static inline int PWM_RR_FROM_REG(int reg, int ix)
449 int rr = (ix == 1) ? reg >> 4 : reg;
451 return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
454 static int PWM_RR_TO_REG(int val, int ix, int reg)
458 for (i = 0; i < 7; i++) {
459 if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
464 return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
467 /* PWM ramp rate enable */
468 static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
470 return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
473 static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
475 int en = (ix == 1) ? 0x80 : 0x08;
477 return val ? reg | en : reg & ~en;
481 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
482 * the register layout). */
483 static inline int PWM_OFF_FROM_REG(int reg, int ix)
485 return (reg >> (ix + 5)) & 0x01;
488 static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
490 return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
493 /* ---------------------------------------------------------------------
496 * ISA access is performed through an index/data register pair and needs to
497 * be protected by a mutex during runtime (not required for initialization).
498 * We use data->update_lock for this and need to ensure that we acquire it
499 * before calling dme1737_read or dme1737_write.
500 * --------------------------------------------------------------------- */
502 static u8 dme1737_read(struct i2c_client *client, u8 reg)
506 if (client->driver) { /* I2C device */
507 val = i2c_smbus_read_byte_data(client, reg);
510 dev_warn(&client->dev, "Read from register "
511 "0x%02x failed! Please report to the driver "
512 "maintainer.\n", reg);
514 } else { /* ISA device */
515 outb(reg, client->addr);
516 val = inb(client->addr + 1);
522 static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
526 if (client->driver) { /* I2C device */
527 res = i2c_smbus_write_byte_data(client, reg, val);
530 dev_warn(&client->dev, "Write to register "
531 "0x%02x failed! Please report to the driver "
532 "maintainer.\n", reg);
534 } else { /* ISA device */
535 outb(reg, client->addr);
536 outb(val, client->addr + 1);
542 static struct dme1737_data *dme1737_update_device(struct device *dev)
544 struct dme1737_data *data = dev_get_drvdata(dev);
545 struct i2c_client *client = &data->client;
549 mutex_lock(&data->update_lock);
551 /* Enable a Vbat monitoring cycle every 10 mins */
552 if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
553 dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
554 DME1737_REG_CONFIG) | 0x10);
555 data->last_vbat = jiffies;
558 /* Sample register contents every 1 sec */
559 if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
560 data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
562 /* In (voltage) registers */
563 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
564 /* Voltage inputs are stored as 16 bit values even
565 * though they have only 12 bits resolution. This is
566 * to make it consistent with the temp inputs. */
567 data->in[ix] = dme1737_read(client,
568 DME1737_REG_IN(ix)) << 8;
569 data->in_min[ix] = dme1737_read(client,
570 DME1737_REG_IN_MIN(ix));
571 data->in_max[ix] = dme1737_read(client,
572 DME1737_REG_IN_MAX(ix));
576 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
577 /* Temp inputs are stored as 16 bit values even
578 * though they have only 12 bits resolution. This is
579 * to take advantage of implicit conversions between
580 * register values (2's complement) and temp values
581 * (signed decimal). */
582 data->temp[ix] = dme1737_read(client,
583 DME1737_REG_TEMP(ix)) << 8;
584 data->temp_min[ix] = dme1737_read(client,
585 DME1737_REG_TEMP_MIN(ix));
586 data->temp_max[ix] = dme1737_read(client,
587 DME1737_REG_TEMP_MAX(ix));
588 data->temp_offset[ix] = dme1737_read(client,
589 DME1737_REG_TEMP_OFFSET(ix));
592 /* In and temp LSB registers
593 * The LSBs are latched when the MSBs are read, so the order in
594 * which the registers are read (MSB first, then LSB) is
596 for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
597 lsb[ix] = dme1737_read(client,
598 DME1737_REG_IN_TEMP_LSB(ix));
600 for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
601 data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
602 DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
604 for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
605 data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
606 DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
610 for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
611 /* Skip reading registers if optional fans are not
613 if (!(data->has_fan & (1 << ix))) {
616 data->fan[ix] = dme1737_read(client,
617 DME1737_REG_FAN(ix));
618 data->fan[ix] |= dme1737_read(client,
619 DME1737_REG_FAN(ix) + 1) << 8;
620 data->fan_min[ix] = dme1737_read(client,
621 DME1737_REG_FAN_MIN(ix));
622 data->fan_min[ix] |= dme1737_read(client,
623 DME1737_REG_FAN_MIN(ix) + 1) << 8;
624 data->fan_opt[ix] = dme1737_read(client,
625 DME1737_REG_FAN_OPT(ix));
626 /* fan_max exists only for fan[5-6] */
628 data->fan_max[ix - 4] = dme1737_read(client,
629 DME1737_REG_FAN_MAX(ix));
634 for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
635 /* Skip reading registers if optional PWMs are not
637 if (!(data->has_pwm & (1 << ix))) {
640 data->pwm[ix] = dme1737_read(client,
641 DME1737_REG_PWM(ix));
642 data->pwm_freq[ix] = dme1737_read(client,
643 DME1737_REG_PWM_FREQ(ix));
644 /* pwm_config and pwm_min exist only for pwm[1-3] */
646 data->pwm_config[ix] = dme1737_read(client,
647 DME1737_REG_PWM_CONFIG(ix));
648 data->pwm_min[ix] = dme1737_read(client,
649 DME1737_REG_PWM_MIN(ix));
652 for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
653 data->pwm_rr[ix] = dme1737_read(client,
654 DME1737_REG_PWM_RR(ix));
657 /* Thermal zone registers */
658 for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
659 data->zone_low[ix] = dme1737_read(client,
660 DME1737_REG_ZONE_LOW(ix));
661 data->zone_abs[ix] = dme1737_read(client,
662 DME1737_REG_ZONE_ABS(ix));
664 for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
665 data->zone_hyst[ix] = dme1737_read(client,
666 DME1737_REG_ZONE_HYST(ix));
669 /* Alarm registers */
670 data->alarms = dme1737_read(client,
672 /* Bit 7 tells us if the other alarm registers are non-zero and
673 * therefore also need to be read */
674 if (data->alarms & 0x80) {
675 data->alarms |= dme1737_read(client,
676 DME1737_REG_ALARM2) << 8;
677 data->alarms |= dme1737_read(client,
678 DME1737_REG_ALARM3) << 16;
681 /* The ISA chips require explicit clearing of alarm bits.
682 * Don't worry, an alarm will come back if the condition
683 * that causes it still exists */
684 if (!client->driver) {
685 if (data->alarms & 0xff0000) {
686 dme1737_write(client, DME1737_REG_ALARM3,
689 if (data->alarms & 0xff00) {
690 dme1737_write(client, DME1737_REG_ALARM2,
693 if (data->alarms & 0xff) {
694 dme1737_write(client, DME1737_REG_ALARM1,
699 data->last_update = jiffies;
703 mutex_unlock(&data->update_lock);
708 /* ---------------------------------------------------------------------
709 * Voltage sysfs attributes
711 * --------------------------------------------------------------------- */
713 #define SYS_IN_INPUT 0
716 #define SYS_IN_ALARM 3
718 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
721 struct dme1737_data *data = dme1737_update_device(dev);
722 struct sensor_device_attribute_2
723 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
724 int ix = sensor_attr_2->index;
725 int fn = sensor_attr_2->nr;
730 res = IN_FROM_REG(data->in[ix], ix, 16);
733 res = IN_FROM_REG(data->in_min[ix], ix, 8);
736 res = IN_FROM_REG(data->in_max[ix], ix, 8);
739 res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
743 dev_dbg(dev, "Unknown function %d.\n", fn);
746 return sprintf(buf, "%d\n", res);
749 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
752 struct dme1737_data *data = dev_get_drvdata(dev);
753 struct i2c_client *client = &data->client;
754 struct sensor_device_attribute_2
755 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
756 int ix = sensor_attr_2->index;
757 int fn = sensor_attr_2->nr;
758 long val = simple_strtol(buf, NULL, 10);
760 mutex_lock(&data->update_lock);
763 data->in_min[ix] = IN_TO_REG(val, ix);
764 dme1737_write(client, DME1737_REG_IN_MIN(ix),
768 data->in_max[ix] = IN_TO_REG(val, ix);
769 dme1737_write(client, DME1737_REG_IN_MAX(ix),
773 dev_dbg(dev, "Unknown function %d.\n", fn);
775 mutex_unlock(&data->update_lock);
780 /* ---------------------------------------------------------------------
781 * Temperature sysfs attributes
783 * --------------------------------------------------------------------- */
785 #define SYS_TEMP_INPUT 0
786 #define SYS_TEMP_MIN 1
787 #define SYS_TEMP_MAX 2
788 #define SYS_TEMP_OFFSET 3
789 #define SYS_TEMP_ALARM 4
790 #define SYS_TEMP_FAULT 5
792 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
795 struct dme1737_data *data = dme1737_update_device(dev);
796 struct sensor_device_attribute_2
797 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
798 int ix = sensor_attr_2->index;
799 int fn = sensor_attr_2->nr;
804 res = TEMP_FROM_REG(data->temp[ix], 16);
807 res = TEMP_FROM_REG(data->temp_min[ix], 8);
810 res = TEMP_FROM_REG(data->temp_max[ix], 8);
812 case SYS_TEMP_OFFSET:
813 res = TEMP_FROM_REG(data->temp_offset[ix], 8);
816 res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
819 res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
823 dev_dbg(dev, "Unknown function %d.\n", fn);
826 return sprintf(buf, "%d\n", res);
829 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
830 const char *buf, size_t count)
832 struct dme1737_data *data = dev_get_drvdata(dev);
833 struct i2c_client *client = &data->client;
834 struct sensor_device_attribute_2
835 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
836 int ix = sensor_attr_2->index;
837 int fn = sensor_attr_2->nr;
838 long val = simple_strtol(buf, NULL, 10);
840 mutex_lock(&data->update_lock);
843 data->temp_min[ix] = TEMP_TO_REG(val);
844 dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
848 data->temp_max[ix] = TEMP_TO_REG(val);
849 dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
852 case SYS_TEMP_OFFSET:
853 data->temp_offset[ix] = TEMP_TO_REG(val);
854 dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
855 data->temp_offset[ix]);
858 dev_dbg(dev, "Unknown function %d.\n", fn);
860 mutex_unlock(&data->update_lock);
865 /* ---------------------------------------------------------------------
866 * Zone sysfs attributes
868 * --------------------------------------------------------------------- */
870 #define SYS_ZONE_AUTO_CHANNELS_TEMP 0
871 #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
872 #define SYS_ZONE_AUTO_POINT1_TEMP 2
873 #define SYS_ZONE_AUTO_POINT2_TEMP 3
874 #define SYS_ZONE_AUTO_POINT3_TEMP 4
876 static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
879 struct dme1737_data *data = dme1737_update_device(dev);
880 struct sensor_device_attribute_2
881 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
882 int ix = sensor_attr_2->index;
883 int fn = sensor_attr_2->nr;
887 case SYS_ZONE_AUTO_CHANNELS_TEMP:
888 /* check config2 for non-standard temp-to-zone mapping */
889 if ((ix == 1) && (data->config2 & 0x02)) {
895 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
896 res = TEMP_FROM_REG(data->zone_low[ix], 8) -
897 TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
899 case SYS_ZONE_AUTO_POINT1_TEMP:
900 res = TEMP_FROM_REG(data->zone_low[ix], 8);
902 case SYS_ZONE_AUTO_POINT2_TEMP:
903 /* pwm_freq holds the temp range bits in the upper nibble */
904 res = TEMP_FROM_REG(data->zone_low[ix], 8) +
905 TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
907 case SYS_ZONE_AUTO_POINT3_TEMP:
908 res = TEMP_FROM_REG(data->zone_abs[ix], 8);
912 dev_dbg(dev, "Unknown function %d.\n", fn);
915 return sprintf(buf, "%d\n", res);
918 static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
919 const char *buf, size_t count)
921 struct dme1737_data *data = dev_get_drvdata(dev);
922 struct i2c_client *client = &data->client;
923 struct sensor_device_attribute_2
924 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
925 int ix = sensor_attr_2->index;
926 int fn = sensor_attr_2->nr;
927 long val = simple_strtol(buf, NULL, 10);
929 mutex_lock(&data->update_lock);
931 case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
932 /* Refresh the cache */
933 data->zone_low[ix] = dme1737_read(client,
934 DME1737_REG_ZONE_LOW(ix));
935 /* Modify the temp hyst value */
936 data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
937 TEMP_FROM_REG(data->zone_low[ix], 8) -
938 val, ix, dme1737_read(client,
939 DME1737_REG_ZONE_HYST(ix == 2)));
940 dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
941 data->zone_hyst[ix == 2]);
943 case SYS_ZONE_AUTO_POINT1_TEMP:
944 data->zone_low[ix] = TEMP_TO_REG(val);
945 dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
948 case SYS_ZONE_AUTO_POINT2_TEMP:
949 /* Refresh the cache */
950 data->zone_low[ix] = dme1737_read(client,
951 DME1737_REG_ZONE_LOW(ix));
952 /* Modify the temp range value (which is stored in the upper
953 * nibble of the pwm_freq register) */
954 data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
955 TEMP_FROM_REG(data->zone_low[ix], 8),
957 DME1737_REG_PWM_FREQ(ix)));
958 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
961 case SYS_ZONE_AUTO_POINT3_TEMP:
962 data->zone_abs[ix] = TEMP_TO_REG(val);
963 dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
967 dev_dbg(dev, "Unknown function %d.\n", fn);
969 mutex_unlock(&data->update_lock);
974 /* ---------------------------------------------------------------------
975 * Fan sysfs attributes
977 * --------------------------------------------------------------------- */
979 #define SYS_FAN_INPUT 0
980 #define SYS_FAN_MIN 1
981 #define SYS_FAN_MAX 2
982 #define SYS_FAN_ALARM 3
983 #define SYS_FAN_TYPE 4
985 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
988 struct dme1737_data *data = dme1737_update_device(dev);
989 struct sensor_device_attribute_2
990 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
991 int ix = sensor_attr_2->index;
992 int fn = sensor_attr_2->nr;
997 res = FAN_FROM_REG(data->fan[ix],
999 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1002 res = FAN_FROM_REG(data->fan_min[ix],
1004 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1007 /* only valid for fan[5-6] */
1008 res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1011 res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1014 /* only valid for fan[1-4] */
1015 res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1019 dev_dbg(dev, "Unknown function %d.\n", fn);
1022 return sprintf(buf, "%d\n", res);
1025 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1026 const char *buf, size_t count)
1028 struct dme1737_data *data = dev_get_drvdata(dev);
1029 struct i2c_client *client = &data->client;
1030 struct sensor_device_attribute_2
1031 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1032 int ix = sensor_attr_2->index;
1033 int fn = sensor_attr_2->nr;
1034 long val = simple_strtol(buf, NULL, 10);
1036 mutex_lock(&data->update_lock);
1040 data->fan_min[ix] = FAN_TO_REG(val, 0);
1042 /* Refresh the cache */
1043 data->fan_opt[ix] = dme1737_read(client,
1044 DME1737_REG_FAN_OPT(ix));
1045 /* Modify the fan min value */
1046 data->fan_min[ix] = FAN_TO_REG(val,
1047 FAN_TPC_FROM_REG(data->fan_opt[ix]));
1049 dme1737_write(client, DME1737_REG_FAN_MIN(ix),
1050 data->fan_min[ix] & 0xff);
1051 dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
1052 data->fan_min[ix] >> 8);
1055 /* Only valid for fan[5-6] */
1056 data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1057 dme1737_write(client, DME1737_REG_FAN_MAX(ix),
1058 data->fan_max[ix - 4]);
1061 /* Only valid for fan[1-4] */
1062 if (!(val == 1 || val == 2 || val == 4)) {
1064 dev_warn(dev, "Fan type value %ld not "
1065 "supported. Choose one of 1, 2, or 4.\n",
1069 data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
1070 DME1737_REG_FAN_OPT(ix)));
1071 dme1737_write(client, DME1737_REG_FAN_OPT(ix),
1075 dev_dbg(dev, "Unknown function %d.\n", fn);
1078 mutex_unlock(&data->update_lock);
1083 /* ---------------------------------------------------------------------
1084 * PWM sysfs attributes
1086 * --------------------------------------------------------------------- */
1089 #define SYS_PWM_FREQ 1
1090 #define SYS_PWM_ENABLE 2
1091 #define SYS_PWM_RAMP_RATE 3
1092 #define SYS_PWM_AUTO_CHANNELS_ZONE 4
1093 #define SYS_PWM_AUTO_PWM_MIN 5
1094 #define SYS_PWM_AUTO_POINT1_PWM 6
1095 #define SYS_PWM_AUTO_POINT2_PWM 7
1097 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1100 struct dme1737_data *data = dme1737_update_device(dev);
1101 struct sensor_device_attribute_2
1102 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1103 int ix = sensor_attr_2->index;
1104 int fn = sensor_attr_2->nr;
1109 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1112 res = data->pwm[ix];
1116 res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1118 case SYS_PWM_ENABLE:
1120 res = 1; /* pwm[5-6] hard-wired to manual mode */
1122 res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1125 case SYS_PWM_RAMP_RATE:
1126 /* Only valid for pwm[1-3] */
1127 res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1129 case SYS_PWM_AUTO_CHANNELS_ZONE:
1130 /* Only valid for pwm[1-3] */
1131 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1132 res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1134 res = data->pwm_acz[ix];
1137 case SYS_PWM_AUTO_PWM_MIN:
1138 /* Only valid for pwm[1-3] */
1139 if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1140 res = data->pwm_min[ix];
1145 case SYS_PWM_AUTO_POINT1_PWM:
1146 /* Only valid for pwm[1-3] */
1147 res = data->pwm_min[ix];
1149 case SYS_PWM_AUTO_POINT2_PWM:
1150 /* Only valid for pwm[1-3] */
1151 res = 255; /* hard-wired */
1155 dev_dbg(dev, "Unknown function %d.\n", fn);
1158 return sprintf(buf, "%d\n", res);
1161 static struct attribute *dme1737_attr_pwm[];
1162 static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
1164 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1165 const char *buf, size_t count)
1167 struct dme1737_data *data = dev_get_drvdata(dev);
1168 struct i2c_client *client = &data->client;
1169 struct sensor_device_attribute_2
1170 *sensor_attr_2 = to_sensor_dev_attr_2(attr);
1171 int ix = sensor_attr_2->index;
1172 int fn = sensor_attr_2->nr;
1173 long val = simple_strtol(buf, NULL, 10);
1175 mutex_lock(&data->update_lock);
1178 data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1179 dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
1182 data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
1183 DME1737_REG_PWM_FREQ(ix)));
1184 dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
1185 data->pwm_freq[ix]);
1187 case SYS_PWM_ENABLE:
1188 /* Only valid for pwm[1-3] */
1189 if (val < 0 || val > 2) {
1191 dev_warn(dev, "PWM enable %ld not "
1192 "supported. Choose one of 0, 1, or 2.\n",
1196 /* Refresh the cache */
1197 data->pwm_config[ix] = dme1737_read(client,
1198 DME1737_REG_PWM_CONFIG(ix));
1199 if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1200 /* Bail out if no change */
1203 /* Do some housekeeping if we are currently in auto mode */
1204 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1205 /* Save the current zone channel assignment */
1206 data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1207 data->pwm_config[ix]);
1208 /* Save the current ramp rate state and disable it */
1209 data->pwm_rr[ix > 0] = dme1737_read(client,
1210 DME1737_REG_PWM_RR(ix > 0));
1211 data->pwm_rr_en &= ~(1 << ix);
1212 if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1213 data->pwm_rr_en |= (1 << ix);
1214 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1215 data->pwm_rr[ix > 0]);
1216 dme1737_write(client,
1217 DME1737_REG_PWM_RR(ix > 0),
1218 data->pwm_rr[ix > 0]);
1221 /* Set the new PWM mode */
1224 /* Change permissions of pwm[ix] to read-only */
1225 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1227 /* Turn fan fully on */
1228 data->pwm_config[ix] = PWM_EN_TO_REG(0,
1229 data->pwm_config[ix]);
1230 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1231 data->pwm_config[ix]);
1234 /* Turn on manual mode */
1235 data->pwm_config[ix] = PWM_EN_TO_REG(1,
1236 data->pwm_config[ix]);
1237 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1238 data->pwm_config[ix]);
1239 /* Change permissions of pwm[ix] to read-writeable */
1240 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1244 /* Change permissions of pwm[ix] to read-only */
1245 dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1247 /* Turn on auto mode using the saved zone channel
1249 data->pwm_config[ix] = PWM_ACZ_TO_REG(
1251 data->pwm_config[ix]);
1252 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1253 data->pwm_config[ix]);
1254 /* Enable PWM ramp rate if previously enabled */
1255 if (data->pwm_rr_en & (1 << ix)) {
1256 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1257 dme1737_read(client,
1258 DME1737_REG_PWM_RR(ix > 0)));
1259 dme1737_write(client,
1260 DME1737_REG_PWM_RR(ix > 0),
1261 data->pwm_rr[ix > 0]);
1266 case SYS_PWM_RAMP_RATE:
1267 /* Only valid for pwm[1-3] */
1268 /* Refresh the cache */
1269 data->pwm_config[ix] = dme1737_read(client,
1270 DME1737_REG_PWM_CONFIG(ix));
1271 data->pwm_rr[ix > 0] = dme1737_read(client,
1272 DME1737_REG_PWM_RR(ix > 0));
1273 /* Set the ramp rate value */
1275 data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1276 data->pwm_rr[ix > 0]);
1278 /* Enable/disable the feature only if the associated PWM
1279 * output is in automatic mode. */
1280 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1281 data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1282 data->pwm_rr[ix > 0]);
1284 dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
1285 data->pwm_rr[ix > 0]);
1287 case SYS_PWM_AUTO_CHANNELS_ZONE:
1288 /* Only valid for pwm[1-3] */
1289 if (!(val == 1 || val == 2 || val == 4 ||
1290 val == 6 || val == 7)) {
1292 dev_warn(dev, "PWM auto channels zone %ld "
1293 "not supported. Choose one of 1, 2, 4, 6, "
1297 /* Refresh the cache */
1298 data->pwm_config[ix] = dme1737_read(client,
1299 DME1737_REG_PWM_CONFIG(ix));
1300 if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1301 /* PWM is already in auto mode so update the temp
1302 * channel assignment */
1303 data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1304 data->pwm_config[ix]);
1305 dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1306 data->pwm_config[ix]);
1308 /* PWM is not in auto mode so we save the temp
1309 * channel assignment for later use */
1310 data->pwm_acz[ix] = val;
1313 case SYS_PWM_AUTO_PWM_MIN:
1314 /* Only valid for pwm[1-3] */
1315 /* Refresh the cache */
1316 data->pwm_min[ix] = dme1737_read(client,
1317 DME1737_REG_PWM_MIN(ix));
1318 /* There are only 2 values supported for the auto_pwm_min
1319 * value: 0 or auto_point1_pwm. So if the temperature drops
1320 * below the auto_point1_temp_hyst value, the fan either turns
1321 * off or runs at auto_point1_pwm duty-cycle. */
1322 if (val > ((data->pwm_min[ix] + 1) / 2)) {
1323 data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1324 dme1737_read(client,
1325 DME1737_REG_PWM_RR(0)));
1327 data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1328 dme1737_read(client,
1329 DME1737_REG_PWM_RR(0)));
1331 dme1737_write(client, DME1737_REG_PWM_RR(0),
1334 case SYS_PWM_AUTO_POINT1_PWM:
1335 /* Only valid for pwm[1-3] */
1336 data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1337 dme1737_write(client, DME1737_REG_PWM_MIN(ix),
1341 dev_dbg(dev, "Unknown function %d.\n", fn);
1344 mutex_unlock(&data->update_lock);
1349 /* ---------------------------------------------------------------------
1350 * Miscellaneous sysfs attributes
1351 * --------------------------------------------------------------------- */
1353 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1356 struct i2c_client *client = to_i2c_client(dev);
1357 struct dme1737_data *data = i2c_get_clientdata(client);
1359 return sprintf(buf, "%d\n", data->vrm);
1362 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1363 const char *buf, size_t count)
1365 struct dme1737_data *data = dev_get_drvdata(dev);
1366 long val = simple_strtol(buf, NULL, 10);
1372 static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1375 struct dme1737_data *data = dme1737_update_device(dev);
1377 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1380 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1383 struct dme1737_data *data = dev_get_drvdata(dev);
1385 return sprintf(buf, "%s\n", data->client.name);
1388 /* ---------------------------------------------------------------------
1389 * Sysfs device attribute defines and structs
1390 * --------------------------------------------------------------------- */
1394 #define SENSOR_DEVICE_ATTR_IN(ix) \
1395 static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
1396 show_in, NULL, SYS_IN_INPUT, ix); \
1397 static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
1398 show_in, set_in, SYS_IN_MIN, ix); \
1399 static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
1400 show_in, set_in, SYS_IN_MAX, ix); \
1401 static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
1402 show_in, NULL, SYS_IN_ALARM, ix)
1404 SENSOR_DEVICE_ATTR_IN(0);
1405 SENSOR_DEVICE_ATTR_IN(1);
1406 SENSOR_DEVICE_ATTR_IN(2);
1407 SENSOR_DEVICE_ATTR_IN(3);
1408 SENSOR_DEVICE_ATTR_IN(4);
1409 SENSOR_DEVICE_ATTR_IN(5);
1410 SENSOR_DEVICE_ATTR_IN(6);
1412 /* Temperatures 1-3 */
1414 #define SENSOR_DEVICE_ATTR_TEMP(ix) \
1415 static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
1416 show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
1417 static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
1418 show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
1419 static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
1420 show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
1421 static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
1422 show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
1423 static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
1424 show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
1425 static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
1426 show_temp, NULL, SYS_TEMP_FAULT, ix-1)
1428 SENSOR_DEVICE_ATTR_TEMP(1);
1429 SENSOR_DEVICE_ATTR_TEMP(2);
1430 SENSOR_DEVICE_ATTR_TEMP(3);
1434 #define SENSOR_DEVICE_ATTR_ZONE(ix) \
1435 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
1436 show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
1437 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
1438 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
1439 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
1440 show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
1441 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
1442 show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
1443 static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
1444 show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
1446 SENSOR_DEVICE_ATTR_ZONE(1);
1447 SENSOR_DEVICE_ATTR_ZONE(2);
1448 SENSOR_DEVICE_ATTR_ZONE(3);
1452 #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1453 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1454 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1455 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1456 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1457 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1458 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1459 static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
1460 show_fan, set_fan, SYS_FAN_TYPE, ix-1)
1462 SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1463 SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1464 SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1465 SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1469 #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1470 static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1471 show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1472 static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1473 show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1474 static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1475 show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1476 static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
1477 show_fan, set_fan, SYS_FAN_MAX, ix-1)
1479 SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1480 SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1484 #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1485 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1486 show_pwm, set_pwm, SYS_PWM, ix-1); \
1487 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1488 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1489 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1490 show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
1491 static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
1492 show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
1493 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
1494 show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
1495 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
1496 show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
1497 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
1498 show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
1499 static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
1500 show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
1502 SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1503 SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1504 SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1508 #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
1509 static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1510 show_pwm, set_pwm, SYS_PWM, ix-1); \
1511 static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1512 show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1513 static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1514 show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
1516 SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1517 SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1521 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1522 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1523 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */
1525 /* This struct holds all the attributes that are always present and need to be
1526 * created unconditionally. The attributes that need modification of their
1527 * permissions are created read-only and write permissions are added or removed
1528 * on the fly when required */
1529 static struct attribute *dme1737_attr[] ={
1531 &sensor_dev_attr_in0_input.dev_attr.attr,
1532 &sensor_dev_attr_in0_min.dev_attr.attr,
1533 &sensor_dev_attr_in0_max.dev_attr.attr,
1534 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1535 &sensor_dev_attr_in1_input.dev_attr.attr,
1536 &sensor_dev_attr_in1_min.dev_attr.attr,
1537 &sensor_dev_attr_in1_max.dev_attr.attr,
1538 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1539 &sensor_dev_attr_in2_input.dev_attr.attr,
1540 &sensor_dev_attr_in2_min.dev_attr.attr,
1541 &sensor_dev_attr_in2_max.dev_attr.attr,
1542 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1543 &sensor_dev_attr_in3_input.dev_attr.attr,
1544 &sensor_dev_attr_in3_min.dev_attr.attr,
1545 &sensor_dev_attr_in3_max.dev_attr.attr,
1546 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1547 &sensor_dev_attr_in4_input.dev_attr.attr,
1548 &sensor_dev_attr_in4_min.dev_attr.attr,
1549 &sensor_dev_attr_in4_max.dev_attr.attr,
1550 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1551 &sensor_dev_attr_in5_input.dev_attr.attr,
1552 &sensor_dev_attr_in5_min.dev_attr.attr,
1553 &sensor_dev_attr_in5_max.dev_attr.attr,
1554 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1555 &sensor_dev_attr_in6_input.dev_attr.attr,
1556 &sensor_dev_attr_in6_min.dev_attr.attr,
1557 &sensor_dev_attr_in6_max.dev_attr.attr,
1558 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1560 &sensor_dev_attr_temp1_input.dev_attr.attr,
1561 &sensor_dev_attr_temp1_min.dev_attr.attr,
1562 &sensor_dev_attr_temp1_max.dev_attr.attr,
1563 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1564 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1565 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1566 &sensor_dev_attr_temp2_input.dev_attr.attr,
1567 &sensor_dev_attr_temp2_min.dev_attr.attr,
1568 &sensor_dev_attr_temp2_max.dev_attr.attr,
1569 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1570 &sensor_dev_attr_temp2_fault.dev_attr.attr,
1571 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1572 &sensor_dev_attr_temp3_input.dev_attr.attr,
1573 &sensor_dev_attr_temp3_min.dev_attr.attr,
1574 &sensor_dev_attr_temp3_max.dev_attr.attr,
1575 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1576 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1577 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1579 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1580 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1581 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1582 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1583 &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
1584 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1585 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1586 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1587 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1588 &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
1589 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1590 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1591 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1592 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1593 &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
1596 &dev_attr_cpu0_vid.attr,
1600 static const struct attribute_group dme1737_group = {
1601 .attrs = dme1737_attr,
1604 /* The following structs hold the PWM attributes, some of which are optional.
1605 * Their creation depends on the chip configuration which is determined during
1607 static struct attribute *dme1737_attr_pwm1[] = {
1608 &sensor_dev_attr_pwm1.dev_attr.attr,
1609 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1610 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1611 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1612 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1613 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1614 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1615 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1618 static struct attribute *dme1737_attr_pwm2[] = {
1619 &sensor_dev_attr_pwm2.dev_attr.attr,
1620 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1621 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1622 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1623 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1624 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1625 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1626 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1629 static struct attribute *dme1737_attr_pwm3[] = {
1630 &sensor_dev_attr_pwm3.dev_attr.attr,
1631 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1632 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1633 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1634 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1635 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1636 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1637 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1640 static struct attribute *dme1737_attr_pwm5[] = {
1641 &sensor_dev_attr_pwm5.dev_attr.attr,
1642 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1643 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1646 static struct attribute *dme1737_attr_pwm6[] = {
1647 &sensor_dev_attr_pwm6.dev_attr.attr,
1648 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1649 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1653 static const struct attribute_group dme1737_pwm_group[] = {
1654 { .attrs = dme1737_attr_pwm1 },
1655 { .attrs = dme1737_attr_pwm2 },
1656 { .attrs = dme1737_attr_pwm3 },
1658 { .attrs = dme1737_attr_pwm5 },
1659 { .attrs = dme1737_attr_pwm6 },
1662 /* The following structs hold the fan attributes, some of which are optional.
1663 * Their creation depends on the chip configuration which is determined during
1665 static struct attribute *dme1737_attr_fan1[] = {
1666 &sensor_dev_attr_fan1_input.dev_attr.attr,
1667 &sensor_dev_attr_fan1_min.dev_attr.attr,
1668 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1669 &sensor_dev_attr_fan1_type.dev_attr.attr,
1672 static struct attribute *dme1737_attr_fan2[] = {
1673 &sensor_dev_attr_fan2_input.dev_attr.attr,
1674 &sensor_dev_attr_fan2_min.dev_attr.attr,
1675 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1676 &sensor_dev_attr_fan2_type.dev_attr.attr,
1679 static struct attribute *dme1737_attr_fan3[] = {
1680 &sensor_dev_attr_fan3_input.dev_attr.attr,
1681 &sensor_dev_attr_fan3_min.dev_attr.attr,
1682 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1683 &sensor_dev_attr_fan3_type.dev_attr.attr,
1686 static struct attribute *dme1737_attr_fan4[] = {
1687 &sensor_dev_attr_fan4_input.dev_attr.attr,
1688 &sensor_dev_attr_fan4_min.dev_attr.attr,
1689 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1690 &sensor_dev_attr_fan4_type.dev_attr.attr,
1693 static struct attribute *dme1737_attr_fan5[] = {
1694 &sensor_dev_attr_fan5_input.dev_attr.attr,
1695 &sensor_dev_attr_fan5_min.dev_attr.attr,
1696 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1697 &sensor_dev_attr_fan5_max.dev_attr.attr,
1700 static struct attribute *dme1737_attr_fan6[] = {
1701 &sensor_dev_attr_fan6_input.dev_attr.attr,
1702 &sensor_dev_attr_fan6_min.dev_attr.attr,
1703 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1704 &sensor_dev_attr_fan6_max.dev_attr.attr,
1708 static const struct attribute_group dme1737_fan_group[] = {
1709 { .attrs = dme1737_attr_fan1 },
1710 { .attrs = dme1737_attr_fan2 },
1711 { .attrs = dme1737_attr_fan3 },
1712 { .attrs = dme1737_attr_fan4 },
1713 { .attrs = dme1737_attr_fan5 },
1714 { .attrs = dme1737_attr_fan6 },
1717 /* The permissions of all of the following attributes are changed to read-
1718 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1719 static struct attribute *dme1737_attr_lock[] = {
1721 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1722 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1723 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1725 &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
1726 &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
1727 &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
1728 &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
1729 &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
1730 &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
1731 &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
1732 &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
1733 &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
1734 &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
1735 &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
1736 &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
1740 static const struct attribute_group dme1737_lock_group = {
1741 .attrs = dme1737_attr_lock,
1744 /* The permissions of the following PWM attributes are changed to read-
1745 * writeable if the chip is *not* locked and the respective PWM is available.
1746 * Otherwise they stay read-only. */
1747 static struct attribute *dme1737_attr_pwm1_lock[] = {
1748 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1749 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1750 &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
1751 &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
1752 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1753 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1756 static struct attribute *dme1737_attr_pwm2_lock[] = {
1757 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1758 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1759 &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
1760 &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
1761 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1762 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1765 static struct attribute *dme1737_attr_pwm3_lock[] = {
1766 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1767 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1768 &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
1769 &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
1770 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1771 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1774 static struct attribute *dme1737_attr_pwm5_lock[] = {
1775 &sensor_dev_attr_pwm5.dev_attr.attr,
1776 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1779 static struct attribute *dme1737_attr_pwm6_lock[] = {
1780 &sensor_dev_attr_pwm6.dev_attr.attr,
1781 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1785 static const struct attribute_group dme1737_pwm_lock_group[] = {
1786 { .attrs = dme1737_attr_pwm1_lock },
1787 { .attrs = dme1737_attr_pwm2_lock },
1788 { .attrs = dme1737_attr_pwm3_lock },
1790 { .attrs = dme1737_attr_pwm5_lock },
1791 { .attrs = dme1737_attr_pwm6_lock },
1794 /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1795 * chip is not locked. Otherwise they are read-only. */
1796 static struct attribute *dme1737_attr_pwm[] = {
1797 &sensor_dev_attr_pwm1.dev_attr.attr,
1798 &sensor_dev_attr_pwm2.dev_attr.attr,
1799 &sensor_dev_attr_pwm3.dev_attr.attr,
1802 /* ---------------------------------------------------------------------
1803 * Super-IO functions
1804 * --------------------------------------------------------------------- */
1806 static inline void dme1737_sio_enter(int sio_cip)
1808 outb(0x55, sio_cip);
1811 static inline void dme1737_sio_exit(int sio_cip)
1813 outb(0xaa, sio_cip);
1816 static inline int dme1737_sio_inb(int sio_cip, int reg)
1819 return inb(sio_cip + 1);
1822 static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1825 outb(val, sio_cip + 1);
1828 /* ---------------------------------------------------------------------
1829 * Device initialization
1830 * --------------------------------------------------------------------- */
1832 static int dme1737_i2c_get_features(int, struct dme1737_data*);
1834 static void dme1737_chmod_file(struct device *dev,
1835 struct attribute *attr, mode_t mode)
1837 if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1838 dev_warn(dev, "Failed to change permissions of %s.\n",
1843 static void dme1737_chmod_group(struct device *dev,
1844 const struct attribute_group *group,
1847 struct attribute **attr;
1849 for (attr = group->attrs; *attr; attr++) {
1850 dme1737_chmod_file(dev, *attr, mode);
1854 static void dme1737_remove_files(struct device *dev)
1856 struct dme1737_data *data = dev_get_drvdata(dev);
1859 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1860 if (data->has_fan & (1 << ix)) {
1861 sysfs_remove_group(&dev->kobj,
1862 &dme1737_fan_group[ix]);
1866 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1867 if (data->has_pwm & (1 << ix)) {
1868 sysfs_remove_group(&dev->kobj,
1869 &dme1737_pwm_group[ix]);
1873 sysfs_remove_group(&dev->kobj, &dme1737_group);
1875 if (!data->client.driver) {
1876 sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
1880 static int dme1737_create_files(struct device *dev)
1882 struct dme1737_data *data = dev_get_drvdata(dev);
1885 /* Create a name attribute for ISA devices */
1886 if (!data->client.driver &&
1887 (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
1891 /* Create standard sysfs attributes */
1892 if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
1896 /* Create fan sysfs attributes */
1897 for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1898 if (data->has_fan & (1 << ix)) {
1899 if ((err = sysfs_create_group(&dev->kobj,
1900 &dme1737_fan_group[ix]))) {
1906 /* Create PWM sysfs attributes */
1907 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1908 if (data->has_pwm & (1 << ix)) {
1909 if ((err = sysfs_create_group(&dev->kobj,
1910 &dme1737_pwm_group[ix]))) {
1916 /* Inform if the device is locked. Otherwise change the permissions of
1917 * selected attributes from read-only to read-writeable. */
1918 if (data->config & 0x02) {
1919 dev_info(dev, "Device is locked. Some attributes "
1920 "will be read-only.\n");
1922 /* Change permissions of standard attributes */
1923 dme1737_chmod_group(dev, &dme1737_lock_group,
1926 /* Change permissions of PWM attributes */
1927 for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
1928 if (data->has_pwm & (1 << ix)) {
1929 dme1737_chmod_group(dev,
1930 &dme1737_pwm_lock_group[ix],
1935 /* Change permissions of pwm[1-3] if in manual mode */
1936 for (ix = 0; ix < 3; ix++) {
1937 if ((data->has_pwm & (1 << ix)) &&
1938 (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
1939 dme1737_chmod_file(dev,
1940 dme1737_attr_pwm[ix],
1949 dme1737_remove_files(dev);
1954 static int dme1737_init_device(struct device *dev)
1956 struct dme1737_data *data = dev_get_drvdata(dev);
1957 struct i2c_client *client = &data->client;
1961 data->config = dme1737_read(client, DME1737_REG_CONFIG);
1962 /* Inform if part is not monitoring/started */
1963 if (!(data->config & 0x01)) {
1965 dev_err(dev, "Device is not monitoring. "
1966 "Use the force_start load parameter to "
1971 /* Force monitoring */
1972 data->config |= 0x01;
1973 dme1737_write(client, DME1737_REG_CONFIG, data->config);
1975 /* Inform if part is not ready */
1976 if (!(data->config & 0x04)) {
1977 dev_err(dev, "Device is not ready.\n");
1981 /* Determine which optional fan and pwm features are enabled/present */
1982 if (client->driver) { /* I2C chip */
1983 data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
1984 /* Check if optional fan3 input is enabled */
1985 if (data->config2 & 0x04) {
1986 data->has_fan |= (1 << 2);
1989 /* Fan4 and pwm3 are only available if the client's I2C address
1990 * is the default 0x2e. Otherwise the I/Os associated with
1991 * these functions are used for addr enable/select. */
1992 if (data->client.addr == 0x2e) {
1993 data->has_fan |= (1 << 3);
1994 data->has_pwm |= (1 << 2);
1997 /* Determine which of the optional fan[5-6] and pwm[5-6]
1998 * features are enabled. For this, we need to query the runtime
1999 * registers through the Super-IO LPC interface. Try both
2000 * config ports 0x2e and 0x4e. */
2001 if (dme1737_i2c_get_features(0x2e, data) &&
2002 dme1737_i2c_get_features(0x4e, data)) {
2003 dev_warn(dev, "Failed to query Super-IO for optional "
2006 } else { /* ISA chip */
2007 /* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6]
2008 * don't exist in the ISA chip. */
2009 data->has_fan |= (1 << 2);
2010 data->has_pwm |= (1 << 2);
2013 /* Fan1, fan2, pwm1, and pwm2 are always present */
2014 data->has_fan |= 0x03;
2015 data->has_pwm |= 0x03;
2017 dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
2018 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
2019 (data->has_pwm & (1 << 2)) ? "yes" : "no",
2020 (data->has_pwm & (1 << 4)) ? "yes" : "no",
2021 (data->has_pwm & (1 << 5)) ? "yes" : "no",
2022 (data->has_fan & (1 << 2)) ? "yes" : "no",
2023 (data->has_fan & (1 << 3)) ? "yes" : "no",
2024 (data->has_fan & (1 << 4)) ? "yes" : "no",
2025 (data->has_fan & (1 << 5)) ? "yes" : "no");
2027 reg = dme1737_read(client, DME1737_REG_TACH_PWM);
2028 /* Inform if fan-to-pwm mapping differs from the default */
2029 if (client->driver && reg != 0xa4) { /* I2C chip */
2030 dev_warn(dev, "Non-standard fan to pwm mapping: "
2031 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
2032 "fan4->pwm%d. Please report to the driver "
2034 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2035 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
2036 } else if (!client->driver && reg != 0x24) { /* ISA chip */
2037 dev_warn(dev, "Non-standard fan to pwm mapping: "
2038 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
2039 "Please report to the driver maintainer.\n",
2040 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
2041 ((reg >> 4) & 0x03) + 1);
2044 /* Switch pwm[1-3] to manual mode if they are currently disabled and
2045 * set the duty-cycles to 0% (which is identical to the PWMs being
2047 if (!(data->config & 0x02)) {
2048 for (ix = 0; ix < 3; ix++) {
2049 data->pwm_config[ix] = dme1737_read(client,
2050 DME1737_REG_PWM_CONFIG(ix));
2051 if ((data->has_pwm & (1 << ix)) &&
2052 (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
2053 dev_info(dev, "Switching pwm%d to "
2054 "manual mode.\n", ix + 1);
2055 data->pwm_config[ix] = PWM_EN_TO_REG(1,
2056 data->pwm_config[ix]);
2057 dme1737_write(client, DME1737_REG_PWM(ix), 0);
2058 dme1737_write(client,
2059 DME1737_REG_PWM_CONFIG(ix),
2060 data->pwm_config[ix]);
2065 /* Initialize the default PWM auto channels zone (acz) assignments */
2066 data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
2067 data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
2068 data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
2071 data->vrm = vid_which_vrm();
2076 /* ---------------------------------------------------------------------
2077 * I2C device detection and registration
2078 * --------------------------------------------------------------------- */
2080 static struct i2c_driver dme1737_i2c_driver;
2082 static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2087 dme1737_sio_enter(sio_cip);
2090 * The DME1737 can return either 0x78 or 0x77 as its device ID. */
2091 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2092 if (!(reg == 0x77 || reg == 0x78)) {
2097 /* Select logical device A (runtime registers) */
2098 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2100 /* Get the base address of the runtime registers */
2101 if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2102 dme1737_sio_inb(sio_cip, 0x61))) {
2107 /* Read the runtime registers to determine which optional features
2108 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2109 * to '10' if the respective feature is enabled. */
2110 if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
2111 data->has_fan |= (1 << 5);
2113 if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
2114 data->has_pwm |= (1 << 5);
2116 if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
2117 data->has_fan |= (1 << 4);
2119 if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
2120 data->has_pwm |= (1 << 4);
2124 dme1737_sio_exit(sio_cip);
2129 static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2132 u8 company, verstep = 0;
2133 struct i2c_client *client;
2134 struct dme1737_data *data;
2139 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
2143 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2148 client = &data->client;
2149 i2c_set_clientdata(client, data);
2150 client->addr = address;
2151 client->adapter = adapter;
2152 client->driver = &dme1737_i2c_driver;
2155 /* A negative kind means that the driver was loaded with no force
2156 * parameter (default), so we must identify the chip. */
2158 company = dme1737_read(client, DME1737_REG_COMPANY);
2159 verstep = dme1737_read(client, DME1737_REG_VERSTEP);
2161 if (!((company == DME1737_COMPANY_SMSC) &&
2162 ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
2171 /* Fill in the remaining client fields and put it into the global
2173 strlcpy(client->name, name, I2C_NAME_SIZE);
2174 mutex_init(&data->update_lock);
2176 /* Tell the I2C layer a new client has arrived */
2177 if ((err = i2c_attach_client(client))) {
2181 dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2182 client->addr, verstep);
2184 /* Initialize the DME1737 chip */
2185 if ((err = dme1737_init_device(dev))) {
2186 dev_err(dev, "Failed to initialize device.\n");
2190 /* Create sysfs files */
2191 if ((err = dme1737_create_files(dev))) {
2192 dev_err(dev, "Failed to create sysfs files.\n");
2196 /* Register device */
2197 data->hwmon_dev = hwmon_device_register(dev);
2198 if (IS_ERR(data->hwmon_dev)) {
2199 dev_err(dev, "Failed to register device.\n");
2200 err = PTR_ERR(data->hwmon_dev);
2207 dme1737_remove_files(dev);
2209 i2c_detach_client(client);
2216 static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
2218 if (!(adapter->class & I2C_CLASS_HWMON)) {
2222 return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
2225 static int dme1737_i2c_detach_client(struct i2c_client *client)
2227 struct dme1737_data *data = i2c_get_clientdata(client);
2230 hwmon_device_unregister(data->hwmon_dev);
2231 dme1737_remove_files(&client->dev);
2233 if ((err = i2c_detach_client(client))) {
2241 static struct i2c_driver dme1737_i2c_driver = {
2245 .attach_adapter = dme1737_i2c_attach_adapter,
2246 .detach_client = dme1737_i2c_detach_client,
2249 /* ---------------------------------------------------------------------
2250 * ISA device detection and registration
2251 * --------------------------------------------------------------------- */
2253 static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2256 unsigned short base_addr;
2258 dme1737_sio_enter(sio_cip);
2261 * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and
2262 * SCH3116 (0x7f). */
2263 reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2264 if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
2269 /* Select logical device A (runtime registers) */
2270 dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2272 /* Get the base address of the runtime registers */
2273 if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2274 dme1737_sio_inb(sio_cip, 0x61))) {
2275 printk(KERN_ERR "dme1737: Base address not set.\n");
2280 /* Access to the hwmon registers is through an index/data register
2281 * pair located at offset 0x70/0x71. */
2282 *addr = base_addr + 0x70;
2285 dme1737_sio_exit(sio_cip);
2289 static int __init dme1737_isa_device_add(unsigned short addr)
2291 struct resource res = {
2293 .end = addr + DME1737_EXTENT - 1,
2295 .flags = IORESOURCE_IO,
2299 if (!(pdev = platform_device_alloc("dme1737", addr))) {
2300 printk(KERN_ERR "dme1737: Failed to allocate device.\n");
2305 if ((err = platform_device_add_resources(pdev, &res, 1))) {
2306 printk(KERN_ERR "dme1737: Failed to add device resource "
2307 "(err = %d).\n", err);
2308 goto exit_device_put;
2311 if ((err = platform_device_add(pdev))) {
2312 printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n",
2314 goto exit_device_put;
2320 platform_device_put(pdev);
2326 static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2329 struct resource *res;
2330 struct i2c_client *client;
2331 struct dme1737_data *data;
2332 struct device *dev = &pdev->dev;
2335 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2336 if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
2337 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2338 (unsigned short)res->start,
2339 (unsigned short)res->start + DME1737_EXTENT - 1);
2344 if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2346 goto exit_release_region;
2349 client = &data->client;
2350 i2c_set_clientdata(client, data);
2351 client->addr = res->start;
2352 platform_set_drvdata(pdev, data);
2354 company = dme1737_read(client, DME1737_REG_COMPANY);
2355 device = dme1737_read(client, DME1737_REG_DEVICE);
2357 if (!((company == DME1737_COMPANY_SMSC) &&
2358 (device == SCH311X_DEVICE))) {
2363 /* Fill in the remaining client fields and initialize the mutex */
2364 strlcpy(client->name, "sch311x", I2C_NAME_SIZE);
2365 mutex_init(&data->update_lock);
2367 dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr);
2369 /* Initialize the chip */
2370 if ((err = dme1737_init_device(dev))) {
2371 dev_err(dev, "Failed to initialize device.\n");
2375 /* Create sysfs files */
2376 if ((err = dme1737_create_files(dev))) {
2377 dev_err(dev, "Failed to create sysfs files.\n");
2381 /* Register device */
2382 data->hwmon_dev = hwmon_device_register(dev);
2383 if (IS_ERR(data->hwmon_dev)) {
2384 dev_err(dev, "Failed to register device.\n");
2385 err = PTR_ERR(data->hwmon_dev);
2386 goto exit_remove_files;
2392 dme1737_remove_files(dev);
2394 platform_set_drvdata(pdev, NULL);
2396 exit_release_region:
2397 release_region(res->start, DME1737_EXTENT);
2402 static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2404 struct dme1737_data *data = platform_get_drvdata(pdev);
2406 hwmon_device_unregister(data->hwmon_dev);
2407 dme1737_remove_files(&pdev->dev);
2408 release_region(data->client.addr, DME1737_EXTENT);
2409 platform_set_drvdata(pdev, NULL);
2415 static struct platform_driver dme1737_isa_driver = {
2417 .owner = THIS_MODULE,
2420 .probe = dme1737_isa_probe,
2421 .remove = __devexit_p(dme1737_isa_remove),
2424 /* ---------------------------------------------------------------------
2425 * Module initialization and cleanup
2426 * --------------------------------------------------------------------- */
2428 static int __init dme1737_init(void)
2431 unsigned short addr;
2433 if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
2437 if (dme1737_isa_detect(0x2e, &addr) &&
2438 dme1737_isa_detect(0x4e, &addr) &&
2440 (dme1737_isa_detect(0x162e, &addr) &&
2441 dme1737_isa_detect(0x164e, &addr)))) {
2442 /* Return 0 if we didn't find an ISA device */
2446 if ((err = platform_driver_register(&dme1737_isa_driver))) {
2447 goto exit_del_i2c_driver;
2450 /* Sets global pdev as a side effect */
2451 if ((err = dme1737_isa_device_add(addr))) {
2452 goto exit_del_isa_driver;
2457 exit_del_isa_driver:
2458 platform_driver_unregister(&dme1737_isa_driver);
2459 exit_del_i2c_driver:
2460 i2c_del_driver(&dme1737_i2c_driver);
2465 static void __exit dme1737_exit(void)
2468 platform_device_unregister(pdev);
2469 platform_driver_unregister(&dme1737_isa_driver);
2472 i2c_del_driver(&dme1737_i2c_driver);
2475 MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2476 MODULE_DESCRIPTION("DME1737 sensors");
2477 MODULE_LICENSE("GPL");
2479 module_init(dme1737_init);
2480 module_exit(dme1737_exit);