dme1737.c revision 345a22245451c0fd2c44b2afb5dfb75628b487fa
1/*
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>
5 *
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.
10 *
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.
15 *
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.
20 *
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.
24 */
25
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>
37#include <asm/io.h>
38
39/* ISA device, if found */
40static struct platform_device *pdev;
41
42/* Module load parameters */
43static int force_start;
44module_param(force_start, bool, 0);
45MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
46
47static unsigned short force_id;
48module_param(force_id, ushort, 0);
49MODULE_PARM_DESC(force_id, "Override the detected device ID");
50
51/* Addresses to scan */
52static unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
53
54/* Insmod parameters */
55I2C_CLIENT_INSMOD_1(dme1737);
56
57/* ---------------------------------------------------------------------
58 * Registers
59 *
60 * The sensors are defined as follows:
61 *
62 * Voltages                          Temperatures
63 * --------                          ------------
64 * in0   +5VTR (+5V stdby)           temp1   Remote diode 1
65 * in1   Vccp  (proc core)           temp2   Internal temp
66 * in2   VCC   (internal +3.3V)      temp3   Remote diode 2
67 * in3   +5V
68 * in4   +12V
69 * in5   VTR   (+3.3V stby)
70 * in6   Vbat
71 *
72 * --------------------------------------------------------------------- */
73
74/* Voltages (in) numbered 0-6 (ix) */
75#define	DME1737_REG_IN(ix)		((ix) < 5 ? 0x20 + (ix) \
76						  : 0x94 + (ix))
77#define	DME1737_REG_IN_MIN(ix)		((ix) < 5 ? 0x44 + (ix) * 2 \
78						  : 0x91 + (ix) * 2)
79#define	DME1737_REG_IN_MAX(ix)		((ix) < 5 ? 0x45 + (ix) * 2 \
80						  : 0x92 + (ix) * 2)
81
82/* Temperatures (temp) numbered 0-2 (ix) */
83#define DME1737_REG_TEMP(ix)		(0x25 + (ix))
84#define DME1737_REG_TEMP_MIN(ix)	(0x4e + (ix) * 2)
85#define DME1737_REG_TEMP_MAX(ix)	(0x4f + (ix) * 2)
86#define DME1737_REG_TEMP_OFFSET(ix)	((ix) == 0 ? 0x1f \
87						   : 0x1c + (ix))
88
89/* Voltage and temperature LSBs
90 * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
91 *    IN_TEMP_LSB(0) = [in5, in6]
92 *    IN_TEMP_LSB(1) = [temp3, temp1]
93 *    IN_TEMP_LSB(2) = [in4, temp2]
94 *    IN_TEMP_LSB(3) = [in3, in0]
95 *    IN_TEMP_LSB(4) = [in2, in1] */
96#define DME1737_REG_IN_TEMP_LSB(ix)	(0x84 + (ix))
97static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
98static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
99static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
100static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
101
102/* Fans numbered 0-5 (ix) */
103#define DME1737_REG_FAN(ix)		((ix) < 4 ? 0x28 + (ix) * 2 \
104						  : 0xa1 + (ix) * 2)
105#define DME1737_REG_FAN_MIN(ix)		((ix) < 4 ? 0x54 + (ix) * 2 \
106						  : 0xa5 + (ix) * 2)
107#define DME1737_REG_FAN_OPT(ix)		((ix) < 4 ? 0x90 + (ix) \
108						  : 0xb2 + (ix))
109#define DME1737_REG_FAN_MAX(ix)		(0xb4 + (ix)) /* only for fan[4-5] */
110
111/* PWMs numbered 0-2, 4-5 (ix) */
112#define DME1737_REG_PWM(ix)		((ix) < 3 ? 0x30 + (ix) \
113						  : 0xa1 + (ix))
114#define DME1737_REG_PWM_CONFIG(ix)	(0x5c + (ix)) /* only for pwm[0-2] */
115#define DME1737_REG_PWM_MIN(ix)		(0x64 + (ix)) /* only for pwm[0-2] */
116#define DME1737_REG_PWM_FREQ(ix)	((ix) < 3 ? 0x5f + (ix) \
117						  : 0xa3 + (ix))
118/* The layout of the ramp rate registers is different from the other pwm
119 * registers. The bits for the 3 PWMs are stored in 2 registers:
120 *    PWM_RR(0) = [OFF3, OFF2,  OFF1,  RES,   RR1E, RR1-2, RR1-1, RR1-0]
121 *    PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
122#define DME1737_REG_PWM_RR(ix)		(0x62 + (ix)) /* only for pwm[0-2] */
123
124/* Thermal zones 0-2 */
125#define DME1737_REG_ZONE_LOW(ix)	(0x67 + (ix))
126#define DME1737_REG_ZONE_ABS(ix)	(0x6a + (ix))
127/* The layout of the hysteresis registers is different from the other zone
128 * registers. The bits for the 3 zones are stored in 2 registers:
129 *    ZONE_HYST(0) = [H1-3,  H1-2,  H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
130 *    ZONE_HYST(1) = [H3-3,  H3-2,  H3-1, H3-0, RES,  RES,  RES,  RES] */
131#define DME1737_REG_ZONE_HYST(ix)	(0x6d + (ix))
132
133/* Alarm registers and bit mapping
134 * The 3 8-bit alarm registers will be concatenated to a single 32-bit
135 * alarm value [0, ALARM3, ALARM2, ALARM1]. */
136#define DME1737_REG_ALARM1		0x41
137#define DME1737_REG_ALARM2		0x42
138#define DME1737_REG_ALARM3		0x83
139static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
140static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
141static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
142
143/* Miscellaneous registers */
144#define DME1737_REG_DEVICE		0x3d
145#define DME1737_REG_COMPANY		0x3e
146#define DME1737_REG_VERSTEP		0x3f
147#define DME1737_REG_CONFIG		0x40
148#define DME1737_REG_CONFIG2		0x7f
149#define DME1737_REG_VID			0x43
150#define DME1737_REG_TACH_PWM		0x81
151
152/* ---------------------------------------------------------------------
153 * Misc defines
154 * --------------------------------------------------------------------- */
155
156/* Chip identification */
157#define DME1737_COMPANY_SMSC	0x5c
158#define DME1737_VERSTEP		0x88
159#define DME1737_VERSTEP_MASK	0xf8
160#define SCH311X_DEVICE		0x8c
161
162/* Length of ISA address segment */
163#define DME1737_EXTENT	2
164
165/* ---------------------------------------------------------------------
166 * Data structures and manipulation thereof
167 * --------------------------------------------------------------------- */
168
169/* For ISA chips, we abuse the i2c_client addr and name fields. We also use
170   the driver field to differentiate between I2C and ISA chips. */
171struct dme1737_data {
172	struct i2c_client client;
173	struct device *hwmon_dev;
174
175	struct mutex update_lock;
176	int valid;			/* !=0 if following fields are valid */
177	unsigned long last_update;	/* in jiffies */
178	unsigned long last_vbat;	/* in jiffies */
179
180	u8 vid;
181	u8 pwm_rr_en;
182	u8 has_pwm;
183	u8 has_fan;
184
185	/* Register values */
186	u16 in[7];
187	u8  in_min[7];
188	u8  in_max[7];
189	s16 temp[3];
190	s8  temp_min[3];
191	s8  temp_max[3];
192	s8  temp_offset[3];
193	u8  config;
194	u8  config2;
195	u8  vrm;
196	u16 fan[6];
197	u16 fan_min[6];
198	u8  fan_max[2];
199	u8  fan_opt[6];
200	u8  pwm[6];
201	u8  pwm_min[3];
202	u8  pwm_config[3];
203	u8  pwm_acz[3];
204	u8  pwm_freq[6];
205	u8  pwm_rr[2];
206	u8  zone_low[3];
207	u8  zone_abs[3];
208	u8  zone_hyst[2];
209	u32 alarms;
210};
211
212/* Nominal voltage values */
213static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
214
215/* Voltage input
216 * Voltage inputs have 16 bits resolution, limit values have 8 bits
217 * resolution. */
218static inline int IN_FROM_REG(int reg, int ix, int res)
219{
220	return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2));
221}
222
223static inline int IN_TO_REG(int val, int ix)
224{
225	return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) /
226			     IN_NOMINAL[ix], 0, 255);
227}
228
229/* Temperature input
230 * The register values represent temperatures in 2's complement notation from
231 * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
232 * values have 8 bits resolution. */
233static inline int TEMP_FROM_REG(int reg, int res)
234{
235	return (reg * 1000) >> (res - 8);
236}
237
238static inline int TEMP_TO_REG(int val)
239{
240	return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
241			     -128, 127);
242}
243
244/* Temperature range */
245static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
246				 10000, 13333, 16000, 20000, 26666, 32000,
247				 40000, 53333, 80000};
248
249static inline int TEMP_RANGE_FROM_REG(int reg)
250{
251	return TEMP_RANGE[(reg >> 4) & 0x0f];
252}
253
254static int TEMP_RANGE_TO_REG(int val, int reg)
255{
256	int i;
257
258	for (i = 15; i > 0; i--) {
259		if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
260			break;
261		}
262	}
263
264	return (reg & 0x0f) | (i << 4);
265}
266
267/* Temperature hysteresis
268 * Register layout:
269 *    reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
270 *    reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
271static inline int TEMP_HYST_FROM_REG(int reg, int ix)
272{
273	return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
274}
275
276static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
277{
278	int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
279
280	return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
281}
282
283/* Fan input RPM */
284static inline int FAN_FROM_REG(int reg, int tpc)
285{
286	if (tpc) {
287		return tpc * reg;
288	} else {
289		return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
290	}
291}
292
293static inline int FAN_TO_REG(int val, int tpc)
294{
295	if (tpc) {
296		return SENSORS_LIMIT(val / tpc, 0, 0xffff);
297	} else {
298		return (val <= 0) ? 0xffff :
299			SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
300	}
301}
302
303/* Fan TPC (tach pulse count)
304 * Converts a register value to a TPC multiplier or returns 0 if the tachometer
305 * is configured in legacy (non-tpc) mode */
306static inline int FAN_TPC_FROM_REG(int reg)
307{
308	return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
309}
310
311/* Fan type
312 * The type of a fan is expressed in number of pulses-per-revolution that it
313 * emits */
314static inline int FAN_TYPE_FROM_REG(int reg)
315{
316	int edge = (reg >> 1) & 0x03;
317
318	return (edge > 0) ? 1 << (edge - 1) : 0;
319}
320
321static inline int FAN_TYPE_TO_REG(int val, int reg)
322{
323	int edge = (val == 4) ? 3 : val;
324
325	return (reg & 0xf9) | (edge << 1);
326}
327
328/* Fan max RPM */
329static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
330			      0x11, 0x0f, 0x0e};
331
332static int FAN_MAX_FROM_REG(int reg)
333{
334	int i;
335
336	for (i = 10; i > 0; i--) {
337		if (reg == FAN_MAX[i]) {
338			break;
339		}
340	}
341
342	return 1000 + i * 500;
343}
344
345static int FAN_MAX_TO_REG(int val)
346{
347	int i;
348
349	for (i = 10; i > 0; i--) {
350		if (val > (1000 + (i - 1) * 500)) {
351			break;
352		}
353	}
354
355	return FAN_MAX[i];
356}
357
358/* PWM enable
359 * Register to enable mapping:
360 * 000:  2  fan on zone 1 auto
361 * 001:  2  fan on zone 2 auto
362 * 010:  2  fan on zone 3 auto
363 * 011:  0  fan full on
364 * 100: -1  fan disabled
365 * 101:  2  fan on hottest of zones 2,3 auto
366 * 110:  2  fan on hottest of zones 1,2,3 auto
367 * 111:  1  fan in manual mode */
368static inline int PWM_EN_FROM_REG(int reg)
369{
370	static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
371
372	return en[(reg >> 5) & 0x07];
373}
374
375static inline int PWM_EN_TO_REG(int val, int reg)
376{
377	int en = (val == 1) ? 7 : 3;
378
379	return (reg & 0x1f) | ((en & 0x07) << 5);
380}
381
382/* PWM auto channels zone
383 * Register to auto channels zone mapping (ACZ is a bitfield with bit x
384 * corresponding to zone x+1):
385 * 000: 001  fan on zone 1 auto
386 * 001: 010  fan on zone 2 auto
387 * 010: 100  fan on zone 3 auto
388 * 011: 000  fan full on
389 * 100: 000  fan disabled
390 * 101: 110  fan on hottest of zones 2,3 auto
391 * 110: 111  fan on hottest of zones 1,2,3 auto
392 * 111: 000  fan in manual mode */
393static inline int PWM_ACZ_FROM_REG(int reg)
394{
395	static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
396
397	return acz[(reg >> 5) & 0x07];
398}
399
400static inline int PWM_ACZ_TO_REG(int val, int reg)
401{
402	int acz = (val == 4) ? 2 : val - 1;
403
404	return (reg & 0x1f) | ((acz & 0x07) << 5);
405}
406
407/* PWM frequency */
408static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
409			       15000, 20000, 30000, 25000, 0, 0, 0, 0};
410
411static inline int PWM_FREQ_FROM_REG(int reg)
412{
413	return PWM_FREQ[reg & 0x0f];
414}
415
416static int PWM_FREQ_TO_REG(int val, int reg)
417{
418	int i;
419
420	/* the first two cases are special - stupid chip design! */
421	if (val > 27500) {
422		i = 10;
423	} else if (val > 22500) {
424		i = 11;
425	} else {
426		for (i = 9; i > 0; i--) {
427			if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
428				break;
429			}
430		}
431	}
432
433	return (reg & 0xf0) | i;
434}
435
436/* PWM ramp rate
437 * Register layout:
438 *    reg[0] = [OFF3,  OFF2,  OFF1,  RES,   RR1-E, RR1-2, RR1-1, RR1-0]
439 *    reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
440static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
441
442static inline int PWM_RR_FROM_REG(int reg, int ix)
443{
444	int rr = (ix == 1) ? reg >> 4 : reg;
445
446	return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
447}
448
449static int PWM_RR_TO_REG(int val, int ix, int reg)
450{
451	int i;
452
453	for (i = 0; i < 7; i++) {
454		if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
455			break;
456		}
457	}
458
459	return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
460}
461
462/* PWM ramp rate enable */
463static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
464{
465	return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
466}
467
468static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
469{
470	int en = (ix == 1) ? 0x80 : 0x08;
471
472	return val ? reg | en : reg & ~en;
473}
474
475/* PWM min/off
476 * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
477 * the register layout). */
478static inline int PWM_OFF_FROM_REG(int reg, int ix)
479{
480	return (reg >> (ix + 5)) & 0x01;
481}
482
483static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
484{
485	return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
486}
487
488/* ---------------------------------------------------------------------
489 * Device I/O access
490 *
491 * ISA access is performed through an index/data register pair and needs to
492 * be protected by a mutex during runtime (not required for initialization).
493 * We use data->update_lock for this and need to ensure that we acquire it
494 * before calling dme1737_read or dme1737_write.
495 * --------------------------------------------------------------------- */
496
497static u8 dme1737_read(struct i2c_client *client, u8 reg)
498{
499	s32 val;
500
501	if (client->driver) { /* I2C device */
502		val = i2c_smbus_read_byte_data(client, reg);
503
504		if (val < 0) {
505			dev_warn(&client->dev, "Read from register "
506				 "0x%02x failed! Please report to the driver "
507				 "maintainer.\n", reg);
508		}
509	} else { /* ISA device */
510		outb(reg, client->addr);
511		val = inb(client->addr + 1);
512	}
513
514	return val;
515}
516
517static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 val)
518{
519	s32 res = 0;
520
521	if (client->driver) { /* I2C device */
522		res = i2c_smbus_write_byte_data(client, reg, val);
523
524		if (res < 0) {
525			dev_warn(&client->dev, "Write to register "
526				 "0x%02x failed! Please report to the driver "
527				 "maintainer.\n", reg);
528		}
529	} else { /* ISA device */
530		outb(reg, client->addr);
531		outb(val, client->addr + 1);
532	}
533
534	return res;
535}
536
537static struct dme1737_data *dme1737_update_device(struct device *dev)
538{
539	struct dme1737_data *data = dev_get_drvdata(dev);
540	struct i2c_client *client = &data->client;
541	int ix;
542	u8 lsb[5];
543
544	mutex_lock(&data->update_lock);
545
546	/* Enable a Vbat monitoring cycle every 10 mins */
547	if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
548		dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
549						DME1737_REG_CONFIG) | 0x10);
550		data->last_vbat = jiffies;
551	}
552
553	/* Sample register contents every 1 sec */
554	if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
555		data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
556
557		/* In (voltage) registers */
558		for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
559			/* Voltage inputs are stored as 16 bit values even
560			 * though they have only 12 bits resolution. This is
561			 * to make it consistent with the temp inputs. */
562			data->in[ix] = dme1737_read(client,
563					DME1737_REG_IN(ix)) << 8;
564			data->in_min[ix] = dme1737_read(client,
565					DME1737_REG_IN_MIN(ix));
566			data->in_max[ix] = dme1737_read(client,
567					DME1737_REG_IN_MAX(ix));
568		}
569
570		/* Temp registers */
571		for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
572			/* Temp inputs are stored as 16 bit values even
573			 * though they have only 12 bits resolution. This is
574			 * to take advantage of implicit conversions between
575			 * register values (2's complement) and temp values
576			 * (signed decimal). */
577			data->temp[ix] = dme1737_read(client,
578					DME1737_REG_TEMP(ix)) << 8;
579			data->temp_min[ix] = dme1737_read(client,
580					DME1737_REG_TEMP_MIN(ix));
581			data->temp_max[ix] = dme1737_read(client,
582					DME1737_REG_TEMP_MAX(ix));
583			data->temp_offset[ix] = dme1737_read(client,
584					DME1737_REG_TEMP_OFFSET(ix));
585		}
586
587		/* In and temp LSB registers
588		 * The LSBs are latched when the MSBs are read, so the order in
589		 * which the registers are read (MSB first, then LSB) is
590		 * important! */
591		for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
592			lsb[ix] = dme1737_read(client,
593					DME1737_REG_IN_TEMP_LSB(ix));
594		}
595		for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
596			data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
597					DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
598		}
599		for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
600			data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
601					DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
602		}
603
604		/* Fan registers */
605		for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
606			/* Skip reading registers if optional fans are not
607			 * present */
608			if (!(data->has_fan & (1 << ix))) {
609				continue;
610			}
611			data->fan[ix] = dme1737_read(client,
612					DME1737_REG_FAN(ix));
613			data->fan[ix] |= dme1737_read(client,
614					DME1737_REG_FAN(ix) + 1) << 8;
615			data->fan_min[ix] = dme1737_read(client,
616					DME1737_REG_FAN_MIN(ix));
617			data->fan_min[ix] |= dme1737_read(client,
618					DME1737_REG_FAN_MIN(ix) + 1) << 8;
619			data->fan_opt[ix] = dme1737_read(client,
620					DME1737_REG_FAN_OPT(ix));
621			/* fan_max exists only for fan[5-6] */
622			if (ix > 3) {
623				data->fan_max[ix - 4] = dme1737_read(client,
624					DME1737_REG_FAN_MAX(ix));
625			}
626		}
627
628		/* PWM registers */
629		for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
630			/* Skip reading registers if optional PWMs are not
631			 * present */
632			if (!(data->has_pwm & (1 << ix))) {
633				continue;
634			}
635			data->pwm[ix] = dme1737_read(client,
636					DME1737_REG_PWM(ix));
637			data->pwm_freq[ix] = dme1737_read(client,
638					DME1737_REG_PWM_FREQ(ix));
639			/* pwm_config and pwm_min exist only for pwm[1-3] */
640			if (ix < 3) {
641				data->pwm_config[ix] = dme1737_read(client,
642						DME1737_REG_PWM_CONFIG(ix));
643				data->pwm_min[ix] = dme1737_read(client,
644						DME1737_REG_PWM_MIN(ix));
645			}
646		}
647		for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
648			data->pwm_rr[ix] = dme1737_read(client,
649						DME1737_REG_PWM_RR(ix));
650		}
651
652		/* Thermal zone registers */
653		for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
654			data->zone_low[ix] = dme1737_read(client,
655					DME1737_REG_ZONE_LOW(ix));
656			data->zone_abs[ix] = dme1737_read(client,
657					DME1737_REG_ZONE_ABS(ix));
658		}
659		for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
660			data->zone_hyst[ix] = dme1737_read(client,
661						DME1737_REG_ZONE_HYST(ix));
662		}
663
664		/* Alarm registers */
665		data->alarms = dme1737_read(client,
666						DME1737_REG_ALARM1);
667		/* Bit 7 tells us if the other alarm registers are non-zero and
668		 * therefore also need to be read */
669		if (data->alarms & 0x80) {
670			data->alarms |= dme1737_read(client,
671						DME1737_REG_ALARM2) << 8;
672			data->alarms |= dme1737_read(client,
673						DME1737_REG_ALARM3) << 16;
674		}
675
676		/* The ISA chips require explicit clearing of alarm bits.
677		 * Don't worry, an alarm will come back if the condition
678		 * that causes it still exists */
679		if (!client->driver) {
680			if (data->alarms & 0xff0000) {
681				dme1737_write(client, DME1737_REG_ALARM3,
682					      0xff);
683			}
684			if (data->alarms & 0xff00) {
685				dme1737_write(client, DME1737_REG_ALARM2,
686					      0xff);
687			}
688			if (data->alarms & 0xff) {
689				dme1737_write(client, DME1737_REG_ALARM1,
690					      0xff);
691			}
692		}
693
694		data->last_update = jiffies;
695		data->valid = 1;
696	}
697
698	mutex_unlock(&data->update_lock);
699
700	return data;
701}
702
703/* ---------------------------------------------------------------------
704 * Voltage sysfs attributes
705 * ix = [0-5]
706 * --------------------------------------------------------------------- */
707
708#define SYS_IN_INPUT	0
709#define SYS_IN_MIN	1
710#define SYS_IN_MAX	2
711#define SYS_IN_ALARM	3
712
713static ssize_t show_in(struct device *dev, struct device_attribute *attr,
714		       char *buf)
715{
716	struct dme1737_data *data = dme1737_update_device(dev);
717	struct sensor_device_attribute_2
718		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
719	int ix = sensor_attr_2->index;
720	int fn = sensor_attr_2->nr;
721	int res;
722
723	switch (fn) {
724	case SYS_IN_INPUT:
725		res = IN_FROM_REG(data->in[ix], ix, 16);
726		break;
727	case SYS_IN_MIN:
728		res = IN_FROM_REG(data->in_min[ix], ix, 8);
729		break;
730	case SYS_IN_MAX:
731		res = IN_FROM_REG(data->in_max[ix], ix, 8);
732		break;
733	case SYS_IN_ALARM:
734		res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
735		break;
736	default:
737		res = 0;
738		dev_dbg(dev, "Unknown function %d.\n", fn);
739	}
740
741	return sprintf(buf, "%d\n", res);
742}
743
744static ssize_t set_in(struct device *dev, struct device_attribute *attr,
745		      const char *buf, size_t count)
746{
747	struct dme1737_data *data = dev_get_drvdata(dev);
748	struct i2c_client *client = &data->client;
749	struct sensor_device_attribute_2
750		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
751	int ix = sensor_attr_2->index;
752	int fn = sensor_attr_2->nr;
753	long val = simple_strtol(buf, NULL, 10);
754
755	mutex_lock(&data->update_lock);
756	switch (fn) {
757	case SYS_IN_MIN:
758		data->in_min[ix] = IN_TO_REG(val, ix);
759		dme1737_write(client, DME1737_REG_IN_MIN(ix),
760			      data->in_min[ix]);
761		break;
762	case SYS_IN_MAX:
763		data->in_max[ix] = IN_TO_REG(val, ix);
764		dme1737_write(client, DME1737_REG_IN_MAX(ix),
765			      data->in_max[ix]);
766		break;
767	default:
768		dev_dbg(dev, "Unknown function %d.\n", fn);
769	}
770	mutex_unlock(&data->update_lock);
771
772	return count;
773}
774
775/* ---------------------------------------------------------------------
776 * Temperature sysfs attributes
777 * ix = [0-2]
778 * --------------------------------------------------------------------- */
779
780#define SYS_TEMP_INPUT			0
781#define SYS_TEMP_MIN			1
782#define SYS_TEMP_MAX			2
783#define SYS_TEMP_OFFSET			3
784#define SYS_TEMP_ALARM			4
785#define SYS_TEMP_FAULT			5
786
787static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
788			 char *buf)
789{
790	struct dme1737_data *data = dme1737_update_device(dev);
791	struct sensor_device_attribute_2
792		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
793	int ix = sensor_attr_2->index;
794	int fn = sensor_attr_2->nr;
795	int res;
796
797	switch (fn) {
798	case SYS_TEMP_INPUT:
799		res = TEMP_FROM_REG(data->temp[ix], 16);
800		break;
801	case SYS_TEMP_MIN:
802		res = TEMP_FROM_REG(data->temp_min[ix], 8);
803		break;
804	case SYS_TEMP_MAX:
805		res = TEMP_FROM_REG(data->temp_max[ix], 8);
806		break;
807	case SYS_TEMP_OFFSET:
808		res = TEMP_FROM_REG(data->temp_offset[ix], 8);
809		break;
810	case SYS_TEMP_ALARM:
811		res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
812		break;
813	case SYS_TEMP_FAULT:
814		res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
815		break;
816	default:
817		res = 0;
818		dev_dbg(dev, "Unknown function %d.\n", fn);
819	}
820
821	return sprintf(buf, "%d\n", res);
822}
823
824static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
825			const char *buf, size_t count)
826{
827	struct dme1737_data *data = dev_get_drvdata(dev);
828	struct i2c_client *client = &data->client;
829	struct sensor_device_attribute_2
830		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
831	int ix = sensor_attr_2->index;
832	int fn = sensor_attr_2->nr;
833	long val = simple_strtol(buf, NULL, 10);
834
835	mutex_lock(&data->update_lock);
836	switch (fn) {
837	case SYS_TEMP_MIN:
838		data->temp_min[ix] = TEMP_TO_REG(val);
839		dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
840			      data->temp_min[ix]);
841		break;
842	case SYS_TEMP_MAX:
843		data->temp_max[ix] = TEMP_TO_REG(val);
844		dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
845			      data->temp_max[ix]);
846		break;
847	case SYS_TEMP_OFFSET:
848		data->temp_offset[ix] = TEMP_TO_REG(val);
849		dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
850			      data->temp_offset[ix]);
851		break;
852	default:
853		dev_dbg(dev, "Unknown function %d.\n", fn);
854	}
855	mutex_unlock(&data->update_lock);
856
857	return count;
858}
859
860/* ---------------------------------------------------------------------
861 * Zone sysfs attributes
862 * ix = [0-2]
863 * --------------------------------------------------------------------- */
864
865#define SYS_ZONE_AUTO_CHANNELS_TEMP	0
866#define SYS_ZONE_AUTO_POINT1_TEMP_HYST	1
867#define SYS_ZONE_AUTO_POINT1_TEMP	2
868#define SYS_ZONE_AUTO_POINT2_TEMP	3
869#define SYS_ZONE_AUTO_POINT3_TEMP	4
870
871static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
872			 char *buf)
873{
874	struct dme1737_data *data = dme1737_update_device(dev);
875	struct sensor_device_attribute_2
876		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
877	int ix = sensor_attr_2->index;
878	int fn = sensor_attr_2->nr;
879	int res;
880
881	switch (fn) {
882	case SYS_ZONE_AUTO_CHANNELS_TEMP:
883		/* check config2 for non-standard temp-to-zone mapping */
884		if ((ix == 1) && (data->config2 & 0x02)) {
885			res = 4;
886		} else {
887			res = 1 << ix;
888		}
889		break;
890	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
891		res = TEMP_FROM_REG(data->zone_low[ix], 8) -
892		      TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
893		break;
894	case SYS_ZONE_AUTO_POINT1_TEMP:
895		res = TEMP_FROM_REG(data->zone_low[ix], 8);
896		break;
897	case SYS_ZONE_AUTO_POINT2_TEMP:
898		/* pwm_freq holds the temp range bits in the upper nibble */
899		res = TEMP_FROM_REG(data->zone_low[ix], 8) +
900		      TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
901		break;
902	case SYS_ZONE_AUTO_POINT3_TEMP:
903		res = TEMP_FROM_REG(data->zone_abs[ix], 8);
904		break;
905	default:
906		res = 0;
907		dev_dbg(dev, "Unknown function %d.\n", fn);
908	}
909
910	return sprintf(buf, "%d\n", res);
911}
912
913static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
914			const char *buf, size_t count)
915{
916	struct dme1737_data *data = dev_get_drvdata(dev);
917	struct i2c_client *client = &data->client;
918	struct sensor_device_attribute_2
919		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
920	int ix = sensor_attr_2->index;
921	int fn = sensor_attr_2->nr;
922	long val = simple_strtol(buf, NULL, 10);
923
924	mutex_lock(&data->update_lock);
925	switch (fn) {
926	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
927		/* Refresh the cache */
928		data->zone_low[ix] = dme1737_read(client,
929						  DME1737_REG_ZONE_LOW(ix));
930		/* Modify the temp hyst value */
931		data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
932					TEMP_FROM_REG(data->zone_low[ix], 8) -
933					val, ix, dme1737_read(client,
934					DME1737_REG_ZONE_HYST(ix == 2)));
935		dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
936			      data->zone_hyst[ix == 2]);
937		break;
938	case SYS_ZONE_AUTO_POINT1_TEMP:
939		data->zone_low[ix] = TEMP_TO_REG(val);
940		dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
941			      data->zone_low[ix]);
942		break;
943	case SYS_ZONE_AUTO_POINT2_TEMP:
944		/* Refresh the cache */
945		data->zone_low[ix] = dme1737_read(client,
946						  DME1737_REG_ZONE_LOW(ix));
947		/* Modify the temp range value (which is stored in the upper
948		 * nibble of the pwm_freq register) */
949		data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
950					TEMP_FROM_REG(data->zone_low[ix], 8),
951					dme1737_read(client,
952					DME1737_REG_PWM_FREQ(ix)));
953		dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
954			      data->pwm_freq[ix]);
955		break;
956	case SYS_ZONE_AUTO_POINT3_TEMP:
957		data->zone_abs[ix] = TEMP_TO_REG(val);
958		dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
959			      data->zone_abs[ix]);
960		break;
961	default:
962		dev_dbg(dev, "Unknown function %d.\n", fn);
963	}
964	mutex_unlock(&data->update_lock);
965
966	return count;
967}
968
969/* ---------------------------------------------------------------------
970 * Fan sysfs attributes
971 * ix = [0-5]
972 * --------------------------------------------------------------------- */
973
974#define SYS_FAN_INPUT	0
975#define SYS_FAN_MIN	1
976#define SYS_FAN_MAX	2
977#define SYS_FAN_ALARM	3
978#define SYS_FAN_TYPE	4
979
980static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
981			char *buf)
982{
983	struct dme1737_data *data = dme1737_update_device(dev);
984	struct sensor_device_attribute_2
985		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
986	int ix = sensor_attr_2->index;
987	int fn = sensor_attr_2->nr;
988	int res;
989
990	switch (fn) {
991	case SYS_FAN_INPUT:
992		res = FAN_FROM_REG(data->fan[ix],
993				   ix < 4 ? 0 :
994				   FAN_TPC_FROM_REG(data->fan_opt[ix]));
995		break;
996	case SYS_FAN_MIN:
997		res = FAN_FROM_REG(data->fan_min[ix],
998				   ix < 4 ? 0 :
999				   FAN_TPC_FROM_REG(data->fan_opt[ix]));
1000		break;
1001	case SYS_FAN_MAX:
1002		/* only valid for fan[5-6] */
1003		res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
1004		break;
1005	case SYS_FAN_ALARM:
1006		res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
1007		break;
1008	case SYS_FAN_TYPE:
1009		/* only valid for fan[1-4] */
1010		res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
1011		break;
1012	default:
1013		res = 0;
1014		dev_dbg(dev, "Unknown function %d.\n", fn);
1015	}
1016
1017	return sprintf(buf, "%d\n", res);
1018}
1019
1020static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1021		       const char *buf, size_t count)
1022{
1023	struct dme1737_data *data = dev_get_drvdata(dev);
1024	struct i2c_client *client = &data->client;
1025	struct sensor_device_attribute_2
1026		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
1027	int ix = sensor_attr_2->index;
1028	int fn = sensor_attr_2->nr;
1029	long val = simple_strtol(buf, NULL, 10);
1030
1031	mutex_lock(&data->update_lock);
1032	switch (fn) {
1033	case SYS_FAN_MIN:
1034		if (ix < 4) {
1035			data->fan_min[ix] = FAN_TO_REG(val, 0);
1036		} else {
1037			/* Refresh the cache */
1038			data->fan_opt[ix] = dme1737_read(client,
1039						DME1737_REG_FAN_OPT(ix));
1040			/* Modify the fan min value */
1041			data->fan_min[ix] = FAN_TO_REG(val,
1042					FAN_TPC_FROM_REG(data->fan_opt[ix]));
1043		}
1044		dme1737_write(client, DME1737_REG_FAN_MIN(ix),
1045			      data->fan_min[ix] & 0xff);
1046		dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
1047			      data->fan_min[ix] >> 8);
1048		break;
1049	case SYS_FAN_MAX:
1050		/* Only valid for fan[5-6] */
1051		data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
1052		dme1737_write(client, DME1737_REG_FAN_MAX(ix),
1053			      data->fan_max[ix - 4]);
1054		break;
1055	case SYS_FAN_TYPE:
1056		/* Only valid for fan[1-4] */
1057		if (!(val == 1 || val == 2 || val == 4)) {
1058			count = -EINVAL;
1059			dev_warn(dev, "Fan type value %ld not "
1060				 "supported. Choose one of 1, 2, or 4.\n",
1061				 val);
1062			goto exit;
1063		}
1064		data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
1065					DME1737_REG_FAN_OPT(ix)));
1066		dme1737_write(client, DME1737_REG_FAN_OPT(ix),
1067			      data->fan_opt[ix]);
1068		break;
1069	default:
1070		dev_dbg(dev, "Unknown function %d.\n", fn);
1071	}
1072exit:
1073	mutex_unlock(&data->update_lock);
1074
1075	return count;
1076}
1077
1078/* ---------------------------------------------------------------------
1079 * PWM sysfs attributes
1080 * ix = [0-4]
1081 * --------------------------------------------------------------------- */
1082
1083#define SYS_PWM				0
1084#define SYS_PWM_FREQ			1
1085#define SYS_PWM_ENABLE			2
1086#define SYS_PWM_RAMP_RATE		3
1087#define SYS_PWM_AUTO_CHANNELS_ZONE	4
1088#define SYS_PWM_AUTO_PWM_MIN		5
1089#define SYS_PWM_AUTO_POINT1_PWM		6
1090#define SYS_PWM_AUTO_POINT2_PWM		7
1091
1092static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1093			char *buf)
1094{
1095	struct dme1737_data *data = dme1737_update_device(dev);
1096	struct sensor_device_attribute_2
1097		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
1098	int ix = sensor_attr_2->index;
1099	int fn = sensor_attr_2->nr;
1100	int res;
1101
1102	switch (fn) {
1103	case SYS_PWM:
1104		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
1105			res = 255;
1106		} else {
1107			res = data->pwm[ix];
1108		}
1109		break;
1110	case SYS_PWM_FREQ:
1111		res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
1112		break;
1113	case SYS_PWM_ENABLE:
1114		if (ix > 3) {
1115			res = 1; /* pwm[5-6] hard-wired to manual mode */
1116		} else {
1117			res = PWM_EN_FROM_REG(data->pwm_config[ix]);
1118		}
1119		break;
1120	case SYS_PWM_RAMP_RATE:
1121		/* Only valid for pwm[1-3] */
1122		res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
1123		break;
1124	case SYS_PWM_AUTO_CHANNELS_ZONE:
1125		/* Only valid for pwm[1-3] */
1126		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1127			res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
1128		} else {
1129			res = data->pwm_acz[ix];
1130		}
1131		break;
1132	case SYS_PWM_AUTO_PWM_MIN:
1133		/* Only valid for pwm[1-3] */
1134		if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
1135			res = data->pwm_min[ix];
1136		} else {
1137			res = 0;
1138		}
1139		break;
1140	case SYS_PWM_AUTO_POINT1_PWM:
1141		/* Only valid for pwm[1-3] */
1142		res = data->pwm_min[ix];
1143		break;
1144	case SYS_PWM_AUTO_POINT2_PWM:
1145		/* Only valid for pwm[1-3] */
1146		res = 255; /* hard-wired */
1147		break;
1148	default:
1149		res = 0;
1150		dev_dbg(dev, "Unknown function %d.\n", fn);
1151	}
1152
1153	return sprintf(buf, "%d\n", res);
1154}
1155
1156static struct attribute *dme1737_attr_pwm[];
1157static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
1158
1159static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1160		       const char *buf, size_t count)
1161{
1162	struct dme1737_data *data = dev_get_drvdata(dev);
1163	struct i2c_client *client = &data->client;
1164	struct sensor_device_attribute_2
1165		*sensor_attr_2 = to_sensor_dev_attr_2(attr);
1166	int ix = sensor_attr_2->index;
1167	int fn = sensor_attr_2->nr;
1168	long val = simple_strtol(buf, NULL, 10);
1169
1170	mutex_lock(&data->update_lock);
1171	switch (fn) {
1172	case SYS_PWM:
1173		data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
1174		dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
1175		break;
1176	case SYS_PWM_FREQ:
1177		data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
1178						DME1737_REG_PWM_FREQ(ix)));
1179		dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
1180			      data->pwm_freq[ix]);
1181		break;
1182	case SYS_PWM_ENABLE:
1183		/* Only valid for pwm[1-3] */
1184		if (val < 0 || val > 2) {
1185			count = -EINVAL;
1186			dev_warn(dev, "PWM enable %ld not "
1187				 "supported. Choose one of 0, 1, or 2.\n",
1188				 val);
1189			goto exit;
1190		}
1191		/* Refresh the cache */
1192		data->pwm_config[ix] = dme1737_read(client,
1193						DME1737_REG_PWM_CONFIG(ix));
1194		if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
1195			/* Bail out if no change */
1196			goto exit;
1197		}
1198		/* Do some housekeeping if we are currently in auto mode */
1199		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1200			/* Save the current zone channel assignment */
1201			data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
1202							data->pwm_config[ix]);
1203			/* Save the current ramp rate state and disable it */
1204			data->pwm_rr[ix > 0] = dme1737_read(client,
1205						DME1737_REG_PWM_RR(ix > 0));
1206			data->pwm_rr_en &= ~(1 << ix);
1207			if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
1208				data->pwm_rr_en |= (1 << ix);
1209				data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
1210							data->pwm_rr[ix > 0]);
1211				dme1737_write(client,
1212					      DME1737_REG_PWM_RR(ix > 0),
1213					      data->pwm_rr[ix > 0]);
1214			}
1215		}
1216		/* Set the new PWM mode */
1217		switch (val) {
1218		case 0:
1219			/* Change permissions of pwm[ix] to read-only */
1220			dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1221					   S_IRUGO);
1222			/* Turn fan fully on */
1223			data->pwm_config[ix] = PWM_EN_TO_REG(0,
1224							data->pwm_config[ix]);
1225			dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1226				      data->pwm_config[ix]);
1227			break;
1228		case 1:
1229			/* Turn on manual mode */
1230			data->pwm_config[ix] = PWM_EN_TO_REG(1,
1231							data->pwm_config[ix]);
1232			dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1233				      data->pwm_config[ix]);
1234			/* Change permissions of pwm[ix] to read-writeable */
1235			dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1236					   S_IRUGO | S_IWUSR);
1237			break;
1238		case 2:
1239			/* Change permissions of pwm[ix] to read-only */
1240			dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
1241					   S_IRUGO);
1242			/* Turn on auto mode using the saved zone channel
1243			 * assignment */
1244			data->pwm_config[ix] = PWM_ACZ_TO_REG(
1245							data->pwm_acz[ix],
1246							data->pwm_config[ix]);
1247			dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1248				      data->pwm_config[ix]);
1249			/* Enable PWM ramp rate if previously enabled */
1250			if (data->pwm_rr_en & (1 << ix)) {
1251				data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
1252						dme1737_read(client,
1253						DME1737_REG_PWM_RR(ix > 0)));
1254				dme1737_write(client,
1255					      DME1737_REG_PWM_RR(ix > 0),
1256					      data->pwm_rr[ix > 0]);
1257			}
1258			break;
1259		}
1260		break;
1261	case SYS_PWM_RAMP_RATE:
1262		/* Only valid for pwm[1-3] */
1263		/* Refresh the cache */
1264		data->pwm_config[ix] = dme1737_read(client,
1265						DME1737_REG_PWM_CONFIG(ix));
1266		data->pwm_rr[ix > 0] = dme1737_read(client,
1267						DME1737_REG_PWM_RR(ix > 0));
1268		/* Set the ramp rate value */
1269		if (val > 0) {
1270			data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
1271							data->pwm_rr[ix > 0]);
1272		}
1273		/* Enable/disable the feature only if the associated PWM
1274		 * output is in automatic mode. */
1275		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1276			data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
1277							data->pwm_rr[ix > 0]);
1278		}
1279		dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
1280			      data->pwm_rr[ix > 0]);
1281		break;
1282	case SYS_PWM_AUTO_CHANNELS_ZONE:
1283		/* Only valid for pwm[1-3] */
1284		if (!(val == 1 || val == 2 || val == 4 ||
1285		      val == 6 || val == 7)) {
1286			count = -EINVAL;
1287			dev_warn(dev, "PWM auto channels zone %ld "
1288				 "not supported. Choose one of 1, 2, 4, 6, "
1289				 "or 7.\n", val);
1290			goto exit;
1291		}
1292		/* Refresh the cache */
1293		data->pwm_config[ix] = dme1737_read(client,
1294						DME1737_REG_PWM_CONFIG(ix));
1295		if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
1296			/* PWM is already in auto mode so update the temp
1297			 * channel assignment */
1298			data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
1299						data->pwm_config[ix]);
1300			dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
1301				      data->pwm_config[ix]);
1302		} else {
1303			/* PWM is not in auto mode so we save the temp
1304			 * channel assignment for later use */
1305			data->pwm_acz[ix] = val;
1306		}
1307		break;
1308	case SYS_PWM_AUTO_PWM_MIN:
1309		/* Only valid for pwm[1-3] */
1310		/* Refresh the cache */
1311		data->pwm_min[ix] = dme1737_read(client,
1312						DME1737_REG_PWM_MIN(ix));
1313		/* There are only 2 values supported for the auto_pwm_min
1314		 * value: 0 or auto_point1_pwm. So if the temperature drops
1315		 * below the auto_point1_temp_hyst value, the fan either turns
1316		 * off or runs at auto_point1_pwm duty-cycle. */
1317		if (val > ((data->pwm_min[ix] + 1) / 2)) {
1318			data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
1319						dme1737_read(client,
1320						DME1737_REG_PWM_RR(0)));
1321		} else {
1322			data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
1323						dme1737_read(client,
1324						DME1737_REG_PWM_RR(0)));
1325		}
1326		dme1737_write(client, DME1737_REG_PWM_RR(0),
1327			      data->pwm_rr[0]);
1328		break;
1329	case SYS_PWM_AUTO_POINT1_PWM:
1330		/* Only valid for pwm[1-3] */
1331		data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
1332		dme1737_write(client, DME1737_REG_PWM_MIN(ix),
1333			      data->pwm_min[ix]);
1334		break;
1335	default:
1336		dev_dbg(dev, "Unknown function %d.\n", fn);
1337	}
1338exit:
1339	mutex_unlock(&data->update_lock);
1340
1341	return count;
1342}
1343
1344/* ---------------------------------------------------------------------
1345 * Miscellaneous sysfs attributes
1346 * --------------------------------------------------------------------- */
1347
1348static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
1349			char *buf)
1350{
1351	struct i2c_client *client = to_i2c_client(dev);
1352	struct dme1737_data *data = i2c_get_clientdata(client);
1353
1354	return sprintf(buf, "%d\n", data->vrm);
1355}
1356
1357static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
1358		       const char *buf, size_t count)
1359{
1360	struct dme1737_data *data = dev_get_drvdata(dev);
1361	long val = simple_strtol(buf, NULL, 10);
1362
1363	data->vrm = val;
1364	return count;
1365}
1366
1367static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
1368			char *buf)
1369{
1370	struct dme1737_data *data = dme1737_update_device(dev);
1371
1372	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1373}
1374
1375static ssize_t show_name(struct device *dev, struct device_attribute *attr,
1376			 char *buf)
1377{
1378	struct dme1737_data *data = dev_get_drvdata(dev);
1379
1380	return sprintf(buf, "%s\n", data->client.name);
1381}
1382
1383/* ---------------------------------------------------------------------
1384 * Sysfs device attribute defines and structs
1385 * --------------------------------------------------------------------- */
1386
1387/* Voltages 0-6 */
1388
1389#define SENSOR_DEVICE_ATTR_IN(ix) \
1390static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
1391	show_in, NULL, SYS_IN_INPUT, ix); \
1392static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
1393	show_in, set_in, SYS_IN_MIN, ix); \
1394static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
1395	show_in, set_in, SYS_IN_MAX, ix); \
1396static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
1397	show_in, NULL, SYS_IN_ALARM, ix)
1398
1399SENSOR_DEVICE_ATTR_IN(0);
1400SENSOR_DEVICE_ATTR_IN(1);
1401SENSOR_DEVICE_ATTR_IN(2);
1402SENSOR_DEVICE_ATTR_IN(3);
1403SENSOR_DEVICE_ATTR_IN(4);
1404SENSOR_DEVICE_ATTR_IN(5);
1405SENSOR_DEVICE_ATTR_IN(6);
1406
1407/* Temperatures 1-3 */
1408
1409#define SENSOR_DEVICE_ATTR_TEMP(ix) \
1410static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
1411	show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
1412static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
1413	show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
1414static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
1415	show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
1416static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
1417	show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
1418static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
1419	show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
1420static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
1421	show_temp, NULL, SYS_TEMP_FAULT, ix-1)
1422
1423SENSOR_DEVICE_ATTR_TEMP(1);
1424SENSOR_DEVICE_ATTR_TEMP(2);
1425SENSOR_DEVICE_ATTR_TEMP(3);
1426
1427/* Zones 1-3 */
1428
1429#define SENSOR_DEVICE_ATTR_ZONE(ix) \
1430static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
1431	show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
1432static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
1433	show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
1434static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
1435	show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
1436static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
1437	show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
1438static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
1439	show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
1440
1441SENSOR_DEVICE_ATTR_ZONE(1);
1442SENSOR_DEVICE_ATTR_ZONE(2);
1443SENSOR_DEVICE_ATTR_ZONE(3);
1444
1445/* Fans 1-4 */
1446
1447#define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
1448static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1449	show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1450static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1451	show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1452static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1453	show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1454static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
1455	show_fan, set_fan, SYS_FAN_TYPE, ix-1)
1456
1457SENSOR_DEVICE_ATTR_FAN_1TO4(1);
1458SENSOR_DEVICE_ATTR_FAN_1TO4(2);
1459SENSOR_DEVICE_ATTR_FAN_1TO4(3);
1460SENSOR_DEVICE_ATTR_FAN_1TO4(4);
1461
1462/* Fans 5-6 */
1463
1464#define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
1465static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
1466	show_fan, NULL, SYS_FAN_INPUT, ix-1); \
1467static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
1468	show_fan, set_fan, SYS_FAN_MIN, ix-1); \
1469static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
1470	show_fan, NULL, SYS_FAN_ALARM, ix-1); \
1471static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
1472	show_fan, set_fan, SYS_FAN_MAX, ix-1)
1473
1474SENSOR_DEVICE_ATTR_FAN_5TO6(5);
1475SENSOR_DEVICE_ATTR_FAN_5TO6(6);
1476
1477/* PWMs 1-3 */
1478
1479#define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
1480static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
1481	show_pwm, set_pwm, SYS_PWM, ix-1); \
1482static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
1483	show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1484static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1485	show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
1486static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
1487	show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
1488static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
1489	show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
1490static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
1491	show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
1492static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
1493	show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
1494static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
1495	show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
1496
1497SENSOR_DEVICE_ATTR_PWM_1TO3(1);
1498SENSOR_DEVICE_ATTR_PWM_1TO3(2);
1499SENSOR_DEVICE_ATTR_PWM_1TO3(3);
1500
1501/* PWMs 5-6 */
1502
1503#define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
1504static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO | S_IWUSR, \
1505	show_pwm, set_pwm, SYS_PWM, ix-1); \
1506static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
1507	show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
1508static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
1509	show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
1510
1511SENSOR_DEVICE_ATTR_PWM_5TO6(5);
1512SENSOR_DEVICE_ATTR_PWM_5TO6(6);
1513
1514/* Misc */
1515
1516static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
1517static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1518static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);   /* for ISA devices */
1519
1520#define SENSOR_DEV_ATTR_IN(ix) \
1521&sensor_dev_attr_in##ix##_input.dev_attr.attr, \
1522&sensor_dev_attr_in##ix##_min.dev_attr.attr, \
1523&sensor_dev_attr_in##ix##_max.dev_attr.attr, \
1524&sensor_dev_attr_in##ix##_alarm.dev_attr.attr
1525
1526/* These attributes are read-writeable only if the chip is *not* locked */
1527#define SENSOR_DEV_ATTR_TEMP_LOCK(ix) \
1528&sensor_dev_attr_temp##ix##_offset.dev_attr.attr
1529
1530#define SENSOR_DEV_ATTR_TEMP(ix) \
1531SENSOR_DEV_ATTR_TEMP_LOCK(ix), \
1532&sensor_dev_attr_temp##ix##_input.dev_attr.attr, \
1533&sensor_dev_attr_temp##ix##_min.dev_attr.attr, \
1534&sensor_dev_attr_temp##ix##_max.dev_attr.attr, \
1535&sensor_dev_attr_temp##ix##_alarm.dev_attr.attr, \
1536&sensor_dev_attr_temp##ix##_fault.dev_attr.attr
1537
1538/* These attributes are read-writeable only if the chip is *not* locked */
1539#define SENSOR_DEV_ATTR_ZONE_LOCK(ix) \
1540&sensor_dev_attr_zone##ix##_auto_point1_temp_hyst.dev_attr.attr, \
1541&sensor_dev_attr_zone##ix##_auto_point1_temp.dev_attr.attr, \
1542&sensor_dev_attr_zone##ix##_auto_point2_temp.dev_attr.attr, \
1543&sensor_dev_attr_zone##ix##_auto_point3_temp.dev_attr.attr
1544
1545#define SENSOR_DEV_ATTR_ZONE(ix) \
1546SENSOR_DEV_ATTR_ZONE_LOCK(ix), \
1547&sensor_dev_attr_zone##ix##_auto_channels_temp.dev_attr.attr
1548
1549#define SENSOR_DEV_ATTR_FAN_1TO4(ix) \
1550&sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
1551&sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
1552&sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
1553&sensor_dev_attr_fan##ix##_type.dev_attr.attr
1554
1555#define SENSOR_DEV_ATTR_FAN_5TO6(ix) \
1556&sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
1557&sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
1558&sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
1559&sensor_dev_attr_fan##ix##_max.dev_attr.attr
1560
1561/* These attributes are read-writeable only if the chip is *not* locked */
1562#define SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix) \
1563&sensor_dev_attr_pwm##ix##_freq.dev_attr.attr, \
1564&sensor_dev_attr_pwm##ix##_enable.dev_attr.attr, \
1565&sensor_dev_attr_pwm##ix##_ramp_rate.dev_attr.attr, \
1566&sensor_dev_attr_pwm##ix##_auto_channels_zone.dev_attr.attr, \
1567&sensor_dev_attr_pwm##ix##_auto_pwm_min.dev_attr.attr, \
1568&sensor_dev_attr_pwm##ix##_auto_point1_pwm.dev_attr.attr
1569
1570#define SENSOR_DEV_ATTR_PWM_1TO3(ix) \
1571SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix), \
1572&sensor_dev_attr_pwm##ix.dev_attr.attr, \
1573&sensor_dev_attr_pwm##ix##_auto_point2_pwm.dev_attr.attr
1574
1575/* These attributes are read-writeable only if the chip is *not* locked */
1576#define SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix) \
1577&sensor_dev_attr_pwm##ix.dev_attr.attr, \
1578&sensor_dev_attr_pwm##ix##_freq.dev_attr.attr
1579
1580#define SENSOR_DEV_ATTR_PWM_5TO6(ix) \
1581SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix), \
1582&sensor_dev_attr_pwm##ix##_enable.dev_attr.attr
1583
1584/* This struct holds all the attributes that are always present and need to be
1585 * created unconditionally. The attributes that need modification of their
1586 * permissions are created read-only and write permissions are added or removed
1587 * on the fly when required */
1588static struct attribute *dme1737_attr[] ={
1589	/* Voltages */
1590	SENSOR_DEV_ATTR_IN(0),
1591	SENSOR_DEV_ATTR_IN(1),
1592	SENSOR_DEV_ATTR_IN(2),
1593	SENSOR_DEV_ATTR_IN(3),
1594	SENSOR_DEV_ATTR_IN(4),
1595	SENSOR_DEV_ATTR_IN(5),
1596	SENSOR_DEV_ATTR_IN(6),
1597	/* Temperatures */
1598	SENSOR_DEV_ATTR_TEMP(1),
1599	SENSOR_DEV_ATTR_TEMP(2),
1600	SENSOR_DEV_ATTR_TEMP(3),
1601	/* Zones */
1602	SENSOR_DEV_ATTR_ZONE(1),
1603	SENSOR_DEV_ATTR_ZONE(2),
1604	SENSOR_DEV_ATTR_ZONE(3),
1605	/* Misc */
1606	&dev_attr_vrm.attr,
1607	&dev_attr_cpu0_vid.attr,
1608	NULL
1609};
1610
1611static const struct attribute_group dme1737_group = {
1612	.attrs = dme1737_attr,
1613};
1614
1615/* The following structs hold the PWM attributes, some of which are optional.
1616 * Their creation depends on the chip configuration which is determined during
1617 * module load. */
1618static struct attribute *dme1737_attr_pwm1[] = {
1619	SENSOR_DEV_ATTR_PWM_1TO3(1),
1620	NULL
1621};
1622static struct attribute *dme1737_attr_pwm2[] = {
1623	SENSOR_DEV_ATTR_PWM_1TO3(2),
1624	NULL
1625};
1626static struct attribute *dme1737_attr_pwm3[] = {
1627	SENSOR_DEV_ATTR_PWM_1TO3(3),
1628	NULL
1629};
1630static struct attribute *dme1737_attr_pwm5[] = {
1631	SENSOR_DEV_ATTR_PWM_5TO6(5),
1632	NULL
1633};
1634static struct attribute *dme1737_attr_pwm6[] = {
1635	SENSOR_DEV_ATTR_PWM_5TO6(6),
1636	NULL
1637};
1638
1639static const struct attribute_group dme1737_pwm_group[] = {
1640	{ .attrs = dme1737_attr_pwm1 },
1641	{ .attrs = dme1737_attr_pwm2 },
1642	{ .attrs = dme1737_attr_pwm3 },
1643	{ .attrs = NULL },
1644	{ .attrs = dme1737_attr_pwm5 },
1645	{ .attrs = dme1737_attr_pwm6 },
1646};
1647
1648/* The following structs hold the fan attributes, some of which are optional.
1649 * Their creation depends on the chip configuration which is determined during
1650 * module load. */
1651static struct attribute *dme1737_attr_fan1[] = {
1652	SENSOR_DEV_ATTR_FAN_1TO4(1),
1653	NULL
1654};
1655static struct attribute *dme1737_attr_fan2[] = {
1656	SENSOR_DEV_ATTR_FAN_1TO4(2),
1657	NULL
1658};
1659static struct attribute *dme1737_attr_fan3[] = {
1660	SENSOR_DEV_ATTR_FAN_1TO4(3),
1661	NULL
1662};
1663static struct attribute *dme1737_attr_fan4[] = {
1664	SENSOR_DEV_ATTR_FAN_1TO4(4),
1665	NULL
1666};
1667static struct attribute *dme1737_attr_fan5[] = {
1668	SENSOR_DEV_ATTR_FAN_5TO6(5),
1669	NULL
1670};
1671static struct attribute *dme1737_attr_fan6[] = {
1672	SENSOR_DEV_ATTR_FAN_5TO6(6),
1673	NULL
1674};
1675
1676static const struct attribute_group dme1737_fan_group[] = {
1677	{ .attrs = dme1737_attr_fan1 },
1678	{ .attrs = dme1737_attr_fan2 },
1679	{ .attrs = dme1737_attr_fan3 },
1680	{ .attrs = dme1737_attr_fan4 },
1681	{ .attrs = dme1737_attr_fan5 },
1682	{ .attrs = dme1737_attr_fan6 },
1683};
1684
1685/* The permissions of all of the following attributes are changed to read-
1686 * writeable if the chip is *not* locked. Otherwise they stay read-only. */
1687static struct attribute *dme1737_attr_lock[] = {
1688	/* Temperatures */
1689	SENSOR_DEV_ATTR_TEMP_LOCK(1),
1690	SENSOR_DEV_ATTR_TEMP_LOCK(2),
1691	SENSOR_DEV_ATTR_TEMP_LOCK(3),
1692	/* Zones */
1693	SENSOR_DEV_ATTR_ZONE_LOCK(1),
1694	SENSOR_DEV_ATTR_ZONE_LOCK(2),
1695	SENSOR_DEV_ATTR_ZONE_LOCK(3),
1696	NULL
1697};
1698
1699static const struct attribute_group dme1737_lock_group = {
1700	.attrs = dme1737_attr_lock,
1701};
1702
1703/* The permissions of the following PWM attributes are changed to read-
1704 * writeable if the chip is *not* locked and the respective PWM is available.
1705 * Otherwise they stay read-only. */
1706static struct attribute *dme1737_attr_pwm1_lock[] = {
1707	SENSOR_DEV_ATTR_PWM_1TO3_LOCK(1),
1708	NULL
1709};
1710static struct attribute *dme1737_attr_pwm2_lock[] = {
1711	SENSOR_DEV_ATTR_PWM_1TO3_LOCK(2),
1712	NULL
1713};
1714static struct attribute *dme1737_attr_pwm3_lock[] = {
1715	SENSOR_DEV_ATTR_PWM_1TO3_LOCK(3),
1716	NULL
1717};
1718static struct attribute *dme1737_attr_pwm5_lock[] = {
1719	SENSOR_DEV_ATTR_PWM_5TO6_LOCK(5),
1720	NULL
1721};
1722static struct attribute *dme1737_attr_pwm6_lock[] = {
1723	SENSOR_DEV_ATTR_PWM_5TO6_LOCK(6),
1724	NULL
1725};
1726
1727static const struct attribute_group dme1737_pwm_lock_group[] = {
1728	{ .attrs = dme1737_attr_pwm1_lock },
1729	{ .attrs = dme1737_attr_pwm2_lock },
1730	{ .attrs = dme1737_attr_pwm3_lock },
1731	{ .attrs = NULL },
1732	{ .attrs = dme1737_attr_pwm5_lock },
1733	{ .attrs = dme1737_attr_pwm6_lock },
1734};
1735
1736/* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
1737 * chip is not locked. Otherwise they are read-only. */
1738static struct attribute *dme1737_attr_pwm[] = {
1739	&sensor_dev_attr_pwm1.dev_attr.attr,
1740	&sensor_dev_attr_pwm2.dev_attr.attr,
1741	&sensor_dev_attr_pwm3.dev_attr.attr,
1742};
1743
1744/* ---------------------------------------------------------------------
1745 * Super-IO functions
1746 * --------------------------------------------------------------------- */
1747
1748static inline void dme1737_sio_enter(int sio_cip)
1749{
1750	outb(0x55, sio_cip);
1751}
1752
1753static inline void dme1737_sio_exit(int sio_cip)
1754{
1755	outb(0xaa, sio_cip);
1756}
1757
1758static inline int dme1737_sio_inb(int sio_cip, int reg)
1759{
1760	outb(reg, sio_cip);
1761	return inb(sio_cip + 1);
1762}
1763
1764static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
1765{
1766	outb(reg, sio_cip);
1767	outb(val, sio_cip + 1);
1768}
1769
1770/* ---------------------------------------------------------------------
1771 * Device initialization
1772 * --------------------------------------------------------------------- */
1773
1774static int dme1737_i2c_get_features(int, struct dme1737_data*);
1775
1776static void dme1737_chmod_file(struct device *dev,
1777			       struct attribute *attr, mode_t mode)
1778{
1779	if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
1780		dev_warn(dev, "Failed to change permissions of %s.\n",
1781			 attr->name);
1782	}
1783}
1784
1785static void dme1737_chmod_group(struct device *dev,
1786				const struct attribute_group *group,
1787				mode_t mode)
1788{
1789	struct attribute **attr;
1790
1791	for (attr = group->attrs; *attr; attr++) {
1792		dme1737_chmod_file(dev, *attr, mode);
1793	}
1794}
1795
1796static void dme1737_remove_files(struct device *dev)
1797{
1798	struct dme1737_data *data = dev_get_drvdata(dev);
1799	int ix;
1800
1801	for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1802		if (data->has_fan & (1 << ix)) {
1803			sysfs_remove_group(&dev->kobj,
1804					   &dme1737_fan_group[ix]);
1805		}
1806	}
1807
1808	for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1809		if (data->has_pwm & (1 << ix)) {
1810			sysfs_remove_group(&dev->kobj,
1811					   &dme1737_pwm_group[ix]);
1812		}
1813	}
1814
1815	sysfs_remove_group(&dev->kobj, &dme1737_group);
1816
1817	if (!data->client.driver) {
1818		sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
1819	}
1820}
1821
1822static int dme1737_create_files(struct device *dev)
1823{
1824	struct dme1737_data *data = dev_get_drvdata(dev);
1825	int err, ix;
1826
1827	/* Create a name attribute for ISA devices */
1828	if (!data->client.driver &&
1829	    (err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr))) {
1830		goto exit;
1831	}
1832
1833	/* Create standard sysfs attributes */
1834	if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
1835		goto exit_remove;
1836	}
1837
1838	/* Create fan sysfs attributes */
1839	for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
1840		if (data->has_fan & (1 << ix)) {
1841			if ((err = sysfs_create_group(&dev->kobj,
1842						&dme1737_fan_group[ix]))) {
1843				goto exit_remove;
1844			}
1845		}
1846	}
1847
1848	/* Create PWM sysfs attributes */
1849	for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
1850		if (data->has_pwm & (1 << ix)) {
1851			if ((err = sysfs_create_group(&dev->kobj,
1852						&dme1737_pwm_group[ix]))) {
1853				goto exit_remove;
1854			}
1855		}
1856	}
1857
1858	/* Inform if the device is locked. Otherwise change the permissions of
1859	 * selected attributes from read-only to read-writeable. */
1860	if (data->config & 0x02) {
1861		dev_info(dev, "Device is locked. Some attributes "
1862			 "will be read-only.\n");
1863	} else {
1864		/* Change permissions of standard attributes */
1865		dme1737_chmod_group(dev, &dme1737_lock_group,
1866				    S_IRUGO | S_IWUSR);
1867
1868		/* Change permissions of PWM attributes */
1869		for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
1870			if (data->has_pwm & (1 << ix)) {
1871				dme1737_chmod_group(dev,
1872						&dme1737_pwm_lock_group[ix],
1873						S_IRUGO | S_IWUSR);
1874			}
1875		}
1876
1877		/* Change permissions of pwm[1-3] if in manual mode */
1878		for (ix = 0; ix < 3; ix++) {
1879			if ((data->has_pwm & (1 << ix)) &&
1880			    (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
1881				dme1737_chmod_file(dev,
1882						dme1737_attr_pwm[ix],
1883						S_IRUGO | S_IWUSR);
1884			}
1885		}
1886	}
1887
1888	return 0;
1889
1890exit_remove:
1891	dme1737_remove_files(dev);
1892exit:
1893	return err;
1894}
1895
1896static int dme1737_init_device(struct device *dev)
1897{
1898	struct dme1737_data *data = dev_get_drvdata(dev);
1899	struct i2c_client *client = &data->client;
1900	int ix;
1901	u8 reg;
1902
1903	data->config = dme1737_read(client, DME1737_REG_CONFIG);
1904	/* Inform if part is not monitoring/started */
1905	if (!(data->config & 0x01)) {
1906		if (!force_start) {
1907			dev_err(dev, "Device is not monitoring. "
1908				"Use the force_start load parameter to "
1909				"override.\n");
1910			return -EFAULT;
1911		}
1912
1913		/* Force monitoring */
1914		data->config |= 0x01;
1915		dme1737_write(client, DME1737_REG_CONFIG, data->config);
1916	}
1917	/* Inform if part is not ready */
1918	if (!(data->config & 0x04)) {
1919		dev_err(dev, "Device is not ready.\n");
1920		return -EFAULT;
1921	}
1922
1923	/* Determine which optional fan and pwm features are enabled/present */
1924	if (client->driver) {   /* I2C chip */
1925		data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
1926		/* Check if optional fan3 input is enabled */
1927		if (data->config2 & 0x04) {
1928			data->has_fan |= (1 << 2);
1929		}
1930
1931		/* Fan4 and pwm3 are only available if the client's I2C address
1932		 * is the default 0x2e. Otherwise the I/Os associated with
1933		 * these functions are used for addr enable/select. */
1934		if (data->client.addr == 0x2e) {
1935			data->has_fan |= (1 << 3);
1936			data->has_pwm |= (1 << 2);
1937		}
1938
1939		/* Determine which of the optional fan[5-6] and pwm[5-6]
1940		 * features are enabled. For this, we need to query the runtime
1941		 * registers through the Super-IO LPC interface. Try both
1942		 * config ports 0x2e and 0x4e. */
1943		if (dme1737_i2c_get_features(0x2e, data) &&
1944		    dme1737_i2c_get_features(0x4e, data)) {
1945			dev_warn(dev, "Failed to query Super-IO for optional "
1946				 "features.\n");
1947		}
1948	} else {   /* ISA chip */
1949		/* Fan3 and pwm3 are always available. Fan[4-5] and pwm[5-6]
1950		 * don't exist in the ISA chip. */
1951		data->has_fan |= (1 << 2);
1952		data->has_pwm |= (1 << 2);
1953	}
1954
1955	/* Fan1, fan2, pwm1, and pwm2 are always present */
1956	data->has_fan |= 0x03;
1957	data->has_pwm |= 0x03;
1958
1959	dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
1960		 "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
1961		 (data->has_pwm & (1 << 2)) ? "yes" : "no",
1962		 (data->has_pwm & (1 << 4)) ? "yes" : "no",
1963		 (data->has_pwm & (1 << 5)) ? "yes" : "no",
1964		 (data->has_fan & (1 << 2)) ? "yes" : "no",
1965		 (data->has_fan & (1 << 3)) ? "yes" : "no",
1966		 (data->has_fan & (1 << 4)) ? "yes" : "no",
1967		 (data->has_fan & (1 << 5)) ? "yes" : "no");
1968
1969	reg = dme1737_read(client, DME1737_REG_TACH_PWM);
1970	/* Inform if fan-to-pwm mapping differs from the default */
1971	if (client->driver && reg != 0xa4) {   /* I2C chip */
1972		dev_warn(dev, "Non-standard fan to pwm mapping: "
1973			 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
1974			 "fan4->pwm%d. Please report to the driver "
1975			 "maintainer.\n",
1976			 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
1977			 ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
1978	} else if (!client->driver && reg != 0x24) {   /* ISA chip */
1979		dev_warn(dev, "Non-standard fan to pwm mapping: "
1980			 "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
1981			 "Please report to the driver maintainer.\n",
1982			 (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
1983			 ((reg >> 4) & 0x03) + 1);
1984	}
1985
1986	/* Switch pwm[1-3] to manual mode if they are currently disabled and
1987	 * set the duty-cycles to 0% (which is identical to the PWMs being
1988	 * disabled). */
1989	if (!(data->config & 0x02)) {
1990		for (ix = 0; ix < 3; ix++) {
1991			data->pwm_config[ix] = dme1737_read(client,
1992						DME1737_REG_PWM_CONFIG(ix));
1993			if ((data->has_pwm & (1 << ix)) &&
1994			    (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
1995				dev_info(dev, "Switching pwm%d to "
1996					 "manual mode.\n", ix + 1);
1997				data->pwm_config[ix] = PWM_EN_TO_REG(1,
1998							data->pwm_config[ix]);
1999				dme1737_write(client, DME1737_REG_PWM(ix), 0);
2000				dme1737_write(client,
2001					      DME1737_REG_PWM_CONFIG(ix),
2002					      data->pwm_config[ix]);
2003			}
2004		}
2005	}
2006
2007	/* Initialize the default PWM auto channels zone (acz) assignments */
2008	data->pwm_acz[0] = 1;	/* pwm1 -> zone1 */
2009	data->pwm_acz[1] = 2;	/* pwm2 -> zone2 */
2010	data->pwm_acz[2] = 4;	/* pwm3 -> zone3 */
2011
2012	/* Set VRM */
2013	data->vrm = vid_which_vrm();
2014
2015	return 0;
2016}
2017
2018/* ---------------------------------------------------------------------
2019 * I2C device detection and registration
2020 * --------------------------------------------------------------------- */
2021
2022static struct i2c_driver dme1737_i2c_driver;
2023
2024static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
2025{
2026	int err = 0, reg;
2027	u16 addr;
2028
2029	dme1737_sio_enter(sio_cip);
2030
2031	/* Check device ID
2032	 * The DME1737 can return either 0x78 or 0x77 as its device ID. */
2033	reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2034	if (!(reg == 0x77 || reg == 0x78)) {
2035		err = -ENODEV;
2036		goto exit;
2037	}
2038
2039	/* Select logical device A (runtime registers) */
2040	dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2041
2042	/* Get the base address of the runtime registers */
2043	if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2044		      dme1737_sio_inb(sio_cip, 0x61))) {
2045		err = -ENODEV;
2046		goto exit;
2047	}
2048
2049	/* Read the runtime registers to determine which optional features
2050	 * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
2051	 * to '10' if the respective feature is enabled. */
2052	if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
2053		data->has_fan |= (1 << 5);
2054	}
2055	if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
2056		data->has_pwm |= (1 << 5);
2057	}
2058	if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
2059		data->has_fan |= (1 << 4);
2060	}
2061	if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
2062		data->has_pwm |= (1 << 4);
2063	}
2064
2065exit:
2066	dme1737_sio_exit(sio_cip);
2067
2068	return err;
2069}
2070
2071static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
2072			      int kind)
2073{
2074	u8 company, verstep = 0;
2075	struct i2c_client *client;
2076	struct dme1737_data *data;
2077	struct device *dev;
2078	int err = 0;
2079	const char *name;
2080
2081	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
2082		goto exit;
2083	}
2084
2085	if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2086		err = -ENOMEM;
2087		goto exit;
2088	}
2089
2090	client = &data->client;
2091	i2c_set_clientdata(client, data);
2092	client->addr = address;
2093	client->adapter = adapter;
2094	client->driver = &dme1737_i2c_driver;
2095	dev = &client->dev;
2096
2097	/* A negative kind means that the driver was loaded with no force
2098	 * parameter (default), so we must identify the chip. */
2099	if (kind < 0) {
2100		company = dme1737_read(client, DME1737_REG_COMPANY);
2101		verstep = dme1737_read(client, DME1737_REG_VERSTEP);
2102
2103		if (!((company == DME1737_COMPANY_SMSC) &&
2104		      ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
2105			err = -ENODEV;
2106			goto exit_kfree;
2107		}
2108	}
2109
2110	kind = dme1737;
2111	name = "dme1737";
2112
2113	/* Fill in the remaining client fields and put it into the global
2114	 * list */
2115	strlcpy(client->name, name, I2C_NAME_SIZE);
2116	mutex_init(&data->update_lock);
2117
2118	/* Tell the I2C layer a new client has arrived */
2119	if ((err = i2c_attach_client(client))) {
2120		goto exit_kfree;
2121	}
2122
2123	dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
2124		 client->addr, verstep);
2125
2126	/* Initialize the DME1737 chip */
2127	if ((err = dme1737_init_device(dev))) {
2128		dev_err(dev, "Failed to initialize device.\n");
2129		goto exit_detach;
2130	}
2131
2132	/* Create sysfs files */
2133	if ((err = dme1737_create_files(dev))) {
2134		dev_err(dev, "Failed to create sysfs files.\n");
2135		goto exit_detach;
2136	}
2137
2138	/* Register device */
2139	data->hwmon_dev = hwmon_device_register(dev);
2140	if (IS_ERR(data->hwmon_dev)) {
2141		dev_err(dev, "Failed to register device.\n");
2142		err = PTR_ERR(data->hwmon_dev);
2143		goto exit_remove;
2144	}
2145
2146	return 0;
2147
2148exit_remove:
2149	dme1737_remove_files(dev);
2150exit_detach:
2151	i2c_detach_client(client);
2152exit_kfree:
2153	kfree(data);
2154exit:
2155	return err;
2156}
2157
2158static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
2159{
2160	if (!(adapter->class & I2C_CLASS_HWMON)) {
2161		return 0;
2162	}
2163
2164	return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
2165}
2166
2167static int dme1737_i2c_detach_client(struct i2c_client *client)
2168{
2169	struct dme1737_data *data = i2c_get_clientdata(client);
2170	int err;
2171
2172	hwmon_device_unregister(data->hwmon_dev);
2173	dme1737_remove_files(&client->dev);
2174
2175	if ((err = i2c_detach_client(client))) {
2176		return err;
2177	}
2178
2179	kfree(data);
2180	return 0;
2181}
2182
2183static struct i2c_driver dme1737_i2c_driver = {
2184	.driver = {
2185		.name = "dme1737",
2186	},
2187	.attach_adapter	= dme1737_i2c_attach_adapter,
2188	.detach_client = dme1737_i2c_detach_client,
2189};
2190
2191/* ---------------------------------------------------------------------
2192 * ISA device detection and registration
2193 * --------------------------------------------------------------------- */
2194
2195static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
2196{
2197	int err = 0, reg;
2198	unsigned short base_addr;
2199
2200	dme1737_sio_enter(sio_cip);
2201
2202	/* Check device ID
2203	 * We currently know about SCH3112 (0x7c), SCH3114 (0x7d), and
2204	 * SCH3116 (0x7f). */
2205	reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
2206	if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
2207		err = -ENODEV;
2208		goto exit;
2209	}
2210
2211	/* Select logical device A (runtime registers) */
2212	dme1737_sio_outb(sio_cip, 0x07, 0x0a);
2213
2214	/* Get the base address of the runtime registers */
2215	if (!(base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
2216			   dme1737_sio_inb(sio_cip, 0x61))) {
2217		printk(KERN_ERR "dme1737: Base address not set.\n");
2218		err = -ENODEV;
2219		goto exit;
2220	}
2221
2222	/* Access to the hwmon registers is through an index/data register
2223	 * pair located at offset 0x70/0x71. */
2224	*addr = base_addr + 0x70;
2225
2226exit:
2227	dme1737_sio_exit(sio_cip);
2228	return err;
2229}
2230
2231static int __init dme1737_isa_device_add(unsigned short addr)
2232{
2233	struct resource res = {
2234		.start	= addr,
2235		.end	= addr + DME1737_EXTENT - 1,
2236		.name	= "dme1737",
2237		.flags	= IORESOURCE_IO,
2238	};
2239	int err;
2240
2241	if (!(pdev = platform_device_alloc("dme1737", addr))) {
2242		printk(KERN_ERR "dme1737: Failed to allocate device.\n");
2243		err = -ENOMEM;
2244		goto exit;
2245	}
2246
2247	if ((err = platform_device_add_resources(pdev, &res, 1))) {
2248		printk(KERN_ERR "dme1737: Failed to add device resource "
2249		       "(err = %d).\n", err);
2250		goto exit_device_put;
2251	}
2252
2253	if ((err = platform_device_add(pdev))) {
2254		printk(KERN_ERR "dme1737: Failed to add device (err = %d).\n",
2255		       err);
2256		goto exit_device_put;
2257	}
2258
2259	return 0;
2260
2261exit_device_put:
2262	platform_device_put(pdev);
2263	pdev = NULL;
2264exit:
2265	return err;
2266}
2267
2268static int __devinit dme1737_isa_probe(struct platform_device *pdev)
2269{
2270	u8 company, device;
2271	struct resource *res;
2272	struct i2c_client *client;
2273	struct dme1737_data *data;
2274	struct device *dev = &pdev->dev;
2275	int err;
2276
2277	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2278	if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
2279		dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
2280			(unsigned short)res->start,
2281			(unsigned short)res->start + DME1737_EXTENT - 1);
2282                err = -EBUSY;
2283                goto exit;
2284        }
2285
2286	if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
2287		err = -ENOMEM;
2288		goto exit_release_region;
2289	}
2290
2291	client = &data->client;
2292	i2c_set_clientdata(client, data);
2293	client->addr = res->start;
2294	platform_set_drvdata(pdev, data);
2295
2296	company = dme1737_read(client, DME1737_REG_COMPANY);
2297	device = dme1737_read(client, DME1737_REG_DEVICE);
2298
2299	if (!((company == DME1737_COMPANY_SMSC) &&
2300	      (device == SCH311X_DEVICE))) {
2301		err = -ENODEV;
2302		goto exit_kfree;
2303	}
2304
2305	/* Fill in the remaining client fields and initialize the mutex */
2306	strlcpy(client->name, "sch311x", I2C_NAME_SIZE);
2307	mutex_init(&data->update_lock);
2308
2309	dev_info(dev, "Found a SCH311x chip at 0x%04x\n", client->addr);
2310
2311	/* Initialize the chip */
2312	if ((err = dme1737_init_device(dev))) {
2313		dev_err(dev, "Failed to initialize device.\n");
2314		goto exit_kfree;
2315	}
2316
2317	/* Create sysfs files */
2318	if ((err = dme1737_create_files(dev))) {
2319		dev_err(dev, "Failed to create sysfs files.\n");
2320		goto exit_kfree;
2321	}
2322
2323	/* Register device */
2324	data->hwmon_dev = hwmon_device_register(dev);
2325	if (IS_ERR(data->hwmon_dev)) {
2326		dev_err(dev, "Failed to register device.\n");
2327		err = PTR_ERR(data->hwmon_dev);
2328		goto exit_remove_files;
2329	}
2330
2331	return 0;
2332
2333exit_remove_files:
2334	dme1737_remove_files(dev);
2335exit_kfree:
2336	platform_set_drvdata(pdev, NULL);
2337	kfree(data);
2338exit_release_region:
2339	release_region(res->start, DME1737_EXTENT);
2340exit:
2341	return err;
2342}
2343
2344static int __devexit dme1737_isa_remove(struct platform_device *pdev)
2345{
2346	struct dme1737_data *data = platform_get_drvdata(pdev);
2347
2348	hwmon_device_unregister(data->hwmon_dev);
2349	dme1737_remove_files(&pdev->dev);
2350	release_region(data->client.addr, DME1737_EXTENT);
2351	platform_set_drvdata(pdev, NULL);
2352	kfree(data);
2353
2354	return 0;
2355}
2356
2357static struct platform_driver dme1737_isa_driver = {
2358	.driver = {
2359		.owner = THIS_MODULE,
2360		.name = "dme1737",
2361	},
2362	.probe = dme1737_isa_probe,
2363	.remove = __devexit_p(dme1737_isa_remove),
2364};
2365
2366/* ---------------------------------------------------------------------
2367 * Module initialization and cleanup
2368 * --------------------------------------------------------------------- */
2369
2370static int __init dme1737_init(void)
2371{
2372	int err;
2373	unsigned short addr;
2374
2375	if ((err = i2c_add_driver(&dme1737_i2c_driver))) {
2376		goto exit;
2377	}
2378
2379	if (dme1737_isa_detect(0x2e, &addr) &&
2380	    dme1737_isa_detect(0x4e, &addr)) {
2381		/* Return 0 if we didn't find an ISA device */
2382		return 0;
2383	}
2384
2385	if ((err = platform_driver_register(&dme1737_isa_driver))) {
2386		goto exit_del_i2c_driver;
2387	}
2388
2389	/* Sets global pdev as a side effect */
2390	if ((err = dme1737_isa_device_add(addr))) {
2391		goto exit_del_isa_driver;
2392	}
2393
2394	return 0;
2395
2396exit_del_isa_driver:
2397	platform_driver_unregister(&dme1737_isa_driver);
2398exit_del_i2c_driver:
2399	i2c_del_driver(&dme1737_i2c_driver);
2400exit:
2401	return err;
2402}
2403
2404static void __exit dme1737_exit(void)
2405{
2406	if (pdev) {
2407		platform_device_unregister(pdev);
2408		platform_driver_unregister(&dme1737_isa_driver);
2409	}
2410
2411	i2c_del_driver(&dme1737_i2c_driver);
2412}
2413
2414MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
2415MODULE_DESCRIPTION("DME1737 sensors");
2416MODULE_LICENSE("GPL");
2417
2418module_init(dme1737_init);
2419module_exit(dme1737_exit);
2420