1/*
2    lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3             monitoring
4    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5    Copyright (c) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
6    Copyright (c) 2003        Margit Schubert-While <margitsw@t-online.de>
7    Copyright (c) 2004        Justin Thiessen <jthiessen@penguincomputing.com>
8
9    Chip details at	      <http://www.national.com/ds/LM/LM85.pdf>
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/hwmon.h>
32#include <linux/hwmon-vid.h>
33#include <linux/err.h>
34#include <linux/mutex.h>
35
36/* Addresses to scan */
37static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
38
39/* Insmod parameters */
40I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
41
42/* The LM85 registers */
43
44#define	LM85_REG_IN(nr)			(0x20 + (nr))
45#define	LM85_REG_IN_MIN(nr)		(0x44 + (nr) * 2)
46#define	LM85_REG_IN_MAX(nr)		(0x45 + (nr) * 2)
47
48#define	LM85_REG_TEMP(nr)		(0x25 + (nr))
49#define	LM85_REG_TEMP_MIN(nr)		(0x4e + (nr) * 2)
50#define	LM85_REG_TEMP_MAX(nr)		(0x4f + (nr) * 2)
51
52/* Fan speeds are LSB, MSB (2 bytes) */
53#define	LM85_REG_FAN(nr)		(0x28 + (nr) *2)
54#define	LM85_REG_FAN_MIN(nr)		(0x54 + (nr) *2)
55
56#define	LM85_REG_PWM(nr)		(0x30 + (nr))
57
58#define	ADT7463_REG_OPPOINT(nr)		(0x33 + (nr))
59
60#define	ADT7463_REG_TMIN_CTL1		0x36
61#define	ADT7463_REG_TMIN_CTL2		0x37
62
63#define	LM85_REG_DEVICE			0x3d
64#define	LM85_REG_COMPANY		0x3e
65#define	LM85_REG_VERSTEP		0x3f
66/* These are the recognized values for the above regs */
67#define	LM85_DEVICE_ADX			0x27
68#define	LM85_COMPANY_NATIONAL		0x01
69#define	LM85_COMPANY_ANALOG_DEV		0x41
70#define	LM85_COMPANY_SMSC      		0x5c
71#define	LM85_VERSTEP_VMASK              0xf0
72#define	LM85_VERSTEP_GENERIC		0x60
73#define	LM85_VERSTEP_LM85C		0x60
74#define	LM85_VERSTEP_LM85B		0x62
75#define	LM85_VERSTEP_ADM1027		0x60
76#define	LM85_VERSTEP_ADT7463		0x62
77#define	LM85_VERSTEP_ADT7463C		0x6A
78#define	LM85_VERSTEP_EMC6D100_A0        0x60
79#define	LM85_VERSTEP_EMC6D100_A1        0x61
80#define	LM85_VERSTEP_EMC6D102		0x65
81
82#define	LM85_REG_CONFIG			0x40
83
84#define	LM85_REG_ALARM1			0x41
85#define	LM85_REG_ALARM2			0x42
86
87#define	LM85_REG_VID			0x43
88
89/* Automated FAN control */
90#define	LM85_REG_AFAN_CONFIG(nr)	(0x5c + (nr))
91#define	LM85_REG_AFAN_RANGE(nr)		(0x5f + (nr))
92#define	LM85_REG_AFAN_SPIKE1		0x62
93#define	LM85_REG_AFAN_SPIKE2		0x63
94#define	LM85_REG_AFAN_MINPWM(nr)	(0x64 + (nr))
95#define	LM85_REG_AFAN_LIMIT(nr)		(0x67 + (nr))
96#define	LM85_REG_AFAN_CRITICAL(nr)	(0x6a + (nr))
97#define	LM85_REG_AFAN_HYST1		0x6d
98#define	LM85_REG_AFAN_HYST2		0x6e
99
100#define	LM85_REG_TACH_MODE		0x74
101#define	LM85_REG_SPINUP_CTL		0x75
102
103#define	ADM1027_REG_TEMP_OFFSET(nr)	(0x70 + (nr))
104#define	ADM1027_REG_CONFIG2		0x73
105#define	ADM1027_REG_INTMASK1		0x74
106#define	ADM1027_REG_INTMASK2		0x75
107#define	ADM1027_REG_EXTEND_ADC1		0x76
108#define	ADM1027_REG_EXTEND_ADC2		0x77
109#define	ADM1027_REG_CONFIG3		0x78
110#define	ADM1027_REG_FAN_PPR		0x7b
111
112#define	ADT7463_REG_THERM		0x79
113#define	ADT7463_REG_THERM_LIMIT		0x7A
114
115#define EMC6D100_REG_ALARM3             0x7d
116/* IN5, IN6 and IN7 */
117#define	EMC6D100_REG_IN(nr)             (0x70 + ((nr)-5))
118#define	EMC6D100_REG_IN_MIN(nr)         (0x73 + ((nr)-5) * 2)
119#define	EMC6D100_REG_IN_MAX(nr)         (0x74 + ((nr)-5) * 2)
120#define	EMC6D102_REG_EXTEND_ADC1	0x85
121#define	EMC6D102_REG_EXTEND_ADC2	0x86
122#define	EMC6D102_REG_EXTEND_ADC3	0x87
123#define	EMC6D102_REG_EXTEND_ADC4	0x88
124
125#define	LM85_ALARM_IN0			0x0001
126#define	LM85_ALARM_IN1			0x0002
127#define	LM85_ALARM_IN2			0x0004
128#define	LM85_ALARM_IN3			0x0008
129#define	LM85_ALARM_TEMP1		0x0010
130#define	LM85_ALARM_TEMP2		0x0020
131#define	LM85_ALARM_TEMP3		0x0040
132#define	LM85_ALARM_ALARM2		0x0080
133#define	LM85_ALARM_IN4			0x0100
134#define	LM85_ALARM_RESERVED		0x0200
135#define	LM85_ALARM_FAN1			0x0400
136#define	LM85_ALARM_FAN2			0x0800
137#define	LM85_ALARM_FAN3			0x1000
138#define	LM85_ALARM_FAN4			0x2000
139#define	LM85_ALARM_TEMP1_FAULT		0x4000
140#define	LM85_ALARM_TEMP3_FAULT		0x8000
141
142
143/* Conversions. Rounding and limit checking is only done on the TO_REG
144   variants. Note that you should be a bit careful with which arguments
145   these macros are called: arguments may be evaluated more than once.
146 */
147
148/* IN are scaled acording to built-in resistors */
149static int lm85_scaling[] = {  /* .001 Volts */
150		2500, 2250, 3300, 5000, 12000,
151		3300, 1500, 1800 /*EMC6D100*/
152	};
153#define SCALE(val,from,to)		(((val)*(to) + ((from)/2))/(from))
154
155#define INS_TO_REG(n,val)	\
156		SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
157
158#define INSEXT_FROM_REG(n,val,ext,scale)	\
159		SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
160
161#define INS_FROM_REG(n,val)   INSEXT_FROM_REG(n,val,0,1)
162
163/* FAN speed is measured using 90kHz clock */
164#define FAN_TO_REG(val)		(SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
165#define FAN_FROM_REG(val)	((val)==0?-1:(val)==0xffff?0:5400000/(val))
166
167/* Temperature is reported in .001 degC increments */
168#define TEMP_TO_REG(val)	\
169		SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
170#define TEMPEXT_FROM_REG(val,ext,scale)	\
171		SCALE((val)*scale + (ext),scale,1000)
172#define TEMP_FROM_REG(val)	\
173		TEMPEXT_FROM_REG(val,0,1)
174
175#define PWM_TO_REG(val)			(SENSORS_LIMIT(val,0,255))
176#define PWM_FROM_REG(val)		(val)
177
178
179/* ZONEs have the following parameters:
180 *    Limit (low) temp,           1. degC
181 *    Hysteresis (below limit),   1. degC (0-15)
182 *    Range of speed control,     .1 degC (2-80)
183 *    Critical (high) temp,       1. degC
184 *
185 * FAN PWMs have the following parameters:
186 *    Reference Zone,                 1, 2, 3, etc.
187 *    Spinup time,                    .05 sec
188 *    PWM value at limit/low temp,    1 count
189 *    PWM Frequency,                  1. Hz
190 *    PWM is Min or OFF below limit,  flag
191 *    Invert PWM output,              flag
192 *
193 * Some chips filter the temp, others the fan.
194 *    Filter constant (or disabled)   .1 seconds
195 */
196
197/* These are the zone temperature range encodings in .001 degree C */
198static int lm85_range_map[] = {
199		2000,  2500,  3300,  4000,  5000,  6600,
200		8000, 10000, 13300, 16000, 20000, 26600,
201		32000, 40000, 53300, 80000
202	};
203static int RANGE_TO_REG( int range )
204{
205	int i;
206
207	if ( range < lm85_range_map[0] ) {
208		return 0 ;
209	} else if ( range > lm85_range_map[15] ) {
210		return 15 ;
211	} else {  /* find closest match */
212		for ( i = 14 ; i >= 0 ; --i ) {
213			if ( range > lm85_range_map[i] ) { /* range bracketed */
214				if ((lm85_range_map[i+1] - range) <
215					(range - lm85_range_map[i])) {
216					i++;
217					break;
218				}
219				break;
220			}
221		}
222	}
223	return( i & 0x0f );
224}
225#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
226
227/* These are the Acoustic Enhancement, or Temperature smoothing encodings
228 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
229 *       MSB (bit 3, value 8).  If the enable bit is 0, the encoded value
230 *       is ignored, or set to 0.
231 */
232/* These are the PWM frequency encodings */
233static int lm85_freq_map[] = { /* .1 Hz */
234		100, 150, 230, 300, 380, 470, 620, 940
235	};
236static int FREQ_TO_REG( int freq )
237{
238	int i;
239
240	if( freq >= lm85_freq_map[7] ) { return 7 ; }
241	for( i = 0 ; i < 7 ; ++i )
242		if( freq <= lm85_freq_map[i] )
243			break ;
244	return( i & 0x07 );
245}
246#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
247
248/* Since we can't use strings, I'm abusing these numbers
249 *   to stand in for the following meanings:
250 *      1 -- PWM responds to Zone 1
251 *      2 -- PWM responds to Zone 2
252 *      3 -- PWM responds to Zone 3
253 *     23 -- PWM responds to the higher temp of Zone 2 or 3
254 *    123 -- PWM responds to highest of Zone 1, 2, or 3
255 *      0 -- PWM is always at 0% (ie, off)
256 *     -1 -- PWM is always at 100%
257 *     -2 -- PWM responds to manual control
258 */
259
260static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
261#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
262
263static int ZONE_TO_REG( int zone )
264{
265	int i;
266
267	for( i = 0 ; i <= 7 ; ++i )
268		if( zone == lm85_zone_map[i] )
269			break ;
270	if( i > 7 )   /* Not found. */
271		i = 3;  /* Always 100% */
272	return( (i & 0x07)<<5 );
273}
274
275#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
276#define HYST_FROM_REG(val) ((val)*1000)
277
278#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
279#define OFFSET_FROM_REG(val) ((val)*25)
280
281#define PPR_MASK(fan) (0x03<<(fan *2))
282#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
283#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
284
285/* Chip sampling rates
286 *
287 * Some sensors are not updated more frequently than once per second
288 *    so it doesn't make sense to read them more often than that.
289 *    We cache the results and return the saved data if the driver
290 *    is called again before a second has elapsed.
291 *
292 * Also, there is significant configuration data for this chip
293 *    given the automatic PWM fan control that is possible.  There
294 *    are about 47 bytes of config data to only 22 bytes of actual
295 *    readings.  So, we keep the config data up to date in the cache
296 *    when it is written and only sample it once every 1 *minute*
297 */
298#define LM85_DATA_INTERVAL  (HZ + HZ / 2)
299#define LM85_CONFIG_INTERVAL  (1 * 60 * HZ)
300
301/* LM85 can automatically adjust fan speeds based on temperature
302 * This structure encapsulates an entire Zone config.  There are
303 * three zones (one for each temperature input) on the lm85
304 */
305struct lm85_zone {
306	s8 limit;	/* Low temp limit */
307	u8 hyst;	/* Low limit hysteresis. (0-15) */
308	u8 range;	/* Temp range, encoded */
309	s8 critical;	/* "All fans ON" temp limit */
310	u8 off_desired; /* Actual "off" temperature specified.  Preserved
311			 * to prevent "drift" as other autofan control
312			 * values change.
313			 */
314	u8 max_desired; /* Actual "max" temperature specified.  Preserved
315			 * to prevent "drift" as other autofan control
316			 * values change.
317			 */
318};
319
320struct lm85_autofan {
321	u8 config;	/* Register value */
322	u8 freq;	/* PWM frequency, encoded */
323	u8 min_pwm;	/* Minimum PWM value, encoded */
324	u8 min_off;	/* Min PWM or OFF below "limit", flag */
325};
326
327/* For each registered chip, we need to keep some data in memory.
328   The structure is dynamically allocated. */
329struct lm85_data {
330	struct i2c_client client;
331	struct class_device *class_dev;
332	enum chips type;
333
334	struct mutex update_lock;
335	int valid;		/* !=0 if following fields are valid */
336	unsigned long last_reading;	/* In jiffies */
337	unsigned long last_config;	/* In jiffies */
338
339	u8 in[8];		/* Register value */
340	u8 in_max[8];		/* Register value */
341	u8 in_min[8];		/* Register value */
342	s8 temp[3];		/* Register value */
343	s8 temp_min[3];		/* Register value */
344	s8 temp_max[3];		/* Register value */
345	s8 temp_offset[3];	/* Register value */
346	u16 fan[4];		/* Register value */
347	u16 fan_min[4];		/* Register value */
348	u8 pwm[3];		/* Register value */
349	u8 spinup_ctl;		/* Register encoding, combined */
350	u8 tach_mode;		/* Register encoding, combined */
351	u8 temp_ext[3];		/* Decoded values */
352	u8 in_ext[8];		/* Decoded values */
353	u8 adc_scale;		/* ADC Extended bits scaling factor */
354	u8 fan_ppr;		/* Register value */
355	u8 smooth[3];		/* Register encoding */
356	u8 vid;			/* Register value */
357	u8 vrm;			/* VRM version */
358	u8 syncpwm3;		/* Saved PWM3 for TACH 2,3,4 config */
359	u8 oppoint[3];		/* Register value */
360	u16 tmin_ctl;		/* Register value */
361	unsigned long therm_total; /* Cummulative therm count */
362	u8 therm_limit;		/* Register value */
363	u32 alarms;		/* Register encoding, combined */
364	struct lm85_autofan autofan[3];
365	struct lm85_zone zone[3];
366};
367
368static int lm85_attach_adapter(struct i2c_adapter *adapter);
369static int lm85_detect(struct i2c_adapter *adapter, int address,
370			int kind);
371static int lm85_detach_client(struct i2c_client *client);
372
373static int lm85_read_value(struct i2c_client *client, u8 reg);
374static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
375static struct lm85_data *lm85_update_device(struct device *dev);
376static void lm85_init_client(struct i2c_client *client);
377
378
379static struct i2c_driver lm85_driver = {
380	.driver = {
381		.name   = "lm85",
382	},
383	.id             = I2C_DRIVERID_LM85,
384	.attach_adapter = lm85_attach_adapter,
385	.detach_client  = lm85_detach_client,
386};
387
388
389/* 4 Fans */
390static ssize_t show_fan(struct device *dev, char *buf, int nr)
391{
392	struct lm85_data *data = lm85_update_device(dev);
393	return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
394}
395static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
396{
397	struct lm85_data *data = lm85_update_device(dev);
398	return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
399}
400static ssize_t set_fan_min(struct device *dev, const char *buf,
401		size_t count, int nr)
402{
403	struct i2c_client *client = to_i2c_client(dev);
404	struct lm85_data *data = i2c_get_clientdata(client);
405	long val = simple_strtol(buf, NULL, 10);
406
407	mutex_lock(&data->update_lock);
408	data->fan_min[nr] = FAN_TO_REG(val);
409	lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
410	mutex_unlock(&data->update_lock);
411	return count;
412}
413
414#define show_fan_offset(offset)						\
415static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
416{									\
417	return show_fan(dev, buf, offset - 1);				\
418}									\
419static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
420{									\
421	return show_fan_min(dev, buf, offset - 1);			\
422}									\
423static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, 		\
424	const char *buf, size_t count) 					\
425{									\
426	return set_fan_min(dev, buf, count, offset - 1);		\
427}									\
428static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset,	\
429		NULL);							\
430static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, 		\
431		show_fan_##offset##_min, set_fan_##offset##_min);
432
433show_fan_offset(1);
434show_fan_offset(2);
435show_fan_offset(3);
436show_fan_offset(4);
437
438/* vid, vrm, alarms */
439
440static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
441{
442	struct lm85_data *data = lm85_update_device(dev);
443	int vid;
444
445	if (data->type == adt7463 && (data->vid & 0x80)) {
446		/* 6-pin VID (VRM 10) */
447		vid = vid_from_reg(data->vid & 0x3f, data->vrm);
448	} else {
449		/* 5-pin VID (VRM 9) */
450		vid = vid_from_reg(data->vid & 0x1f, data->vrm);
451	}
452
453	return sprintf(buf, "%d\n", vid);
454}
455
456static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
457
458static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
459{
460	struct lm85_data *data = lm85_update_device(dev);
461	return sprintf(buf, "%ld\n", (long) data->vrm);
462}
463
464static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
465{
466	struct i2c_client *client = to_i2c_client(dev);
467	struct lm85_data *data = i2c_get_clientdata(client);
468	u32 val;
469
470	val = simple_strtoul(buf, NULL, 10);
471	data->vrm = val;
472	return count;
473}
474
475static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
476
477static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
478{
479	struct lm85_data *data = lm85_update_device(dev);
480	return sprintf(buf, "%u\n", data->alarms);
481}
482
483static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
484
485/* pwm */
486
487static ssize_t show_pwm(struct device *dev, char *buf, int nr)
488{
489	struct lm85_data *data = lm85_update_device(dev);
490	return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
491}
492static ssize_t set_pwm(struct device *dev, const char *buf,
493		size_t count, int nr)
494{
495	struct i2c_client *client = to_i2c_client(dev);
496	struct lm85_data *data = i2c_get_clientdata(client);
497	long val = simple_strtol(buf, NULL, 10);
498
499	mutex_lock(&data->update_lock);
500	data->pwm[nr] = PWM_TO_REG(val);
501	lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
502	mutex_unlock(&data->update_lock);
503	return count;
504}
505static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
506{
507	struct lm85_data *data = lm85_update_device(dev);
508	int	pwm_zone;
509
510	pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
511	return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
512}
513
514#define show_pwm_reg(offset)						\
515static ssize_t show_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
516{									\
517	return show_pwm(dev, buf, offset - 1);				\
518}									\
519static ssize_t set_pwm_##offset (struct device *dev, struct device_attribute *attr,			\
520				 const char *buf, size_t count)		\
521{									\
522	return set_pwm(dev, buf, count, offset - 1);			\
523}									\
524static ssize_t show_pwm_enable##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
525{									\
526	return show_pwm_enable(dev, buf, offset - 1);			\
527}									\
528static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, 			\
529		show_pwm_##offset, set_pwm_##offset);			\
530static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, 			\
531		show_pwm_enable##offset, NULL);
532
533show_pwm_reg(1);
534show_pwm_reg(2);
535show_pwm_reg(3);
536
537/* Voltages */
538
539static ssize_t show_in(struct device *dev, char *buf, int nr)
540{
541	struct lm85_data *data = lm85_update_device(dev);
542	return sprintf(	buf, "%d\n", INSEXT_FROM_REG(nr,
543						     data->in[nr],
544						     data->in_ext[nr],
545						     data->adc_scale) );
546}
547static ssize_t show_in_min(struct device *dev, char *buf, int nr)
548{
549	struct lm85_data *data = lm85_update_device(dev);
550	return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
551}
552static ssize_t set_in_min(struct device *dev, const char *buf,
553		size_t count, int nr)
554{
555	struct i2c_client *client = to_i2c_client(dev);
556	struct lm85_data *data = i2c_get_clientdata(client);
557	long val = simple_strtol(buf, NULL, 10);
558
559	mutex_lock(&data->update_lock);
560	data->in_min[nr] = INS_TO_REG(nr, val);
561	lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
562	mutex_unlock(&data->update_lock);
563	return count;
564}
565static ssize_t show_in_max(struct device *dev, char *buf, int nr)
566{
567	struct lm85_data *data = lm85_update_device(dev);
568	return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
569}
570static ssize_t set_in_max(struct device *dev, const char *buf,
571		size_t count, int nr)
572{
573	struct i2c_client *client = to_i2c_client(dev);
574	struct lm85_data *data = i2c_get_clientdata(client);
575	long val = simple_strtol(buf, NULL, 10);
576
577	mutex_lock(&data->update_lock);
578	data->in_max[nr] = INS_TO_REG(nr, val);
579	lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
580	mutex_unlock(&data->update_lock);
581	return count;
582}
583#define show_in_reg(offset)						\
584static ssize_t show_in_##offset (struct device *dev, struct device_attribute *attr, char *buf)		\
585{									\
586	return show_in(dev, buf, offset);				\
587}									\
588static ssize_t show_in_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
589{									\
590	return show_in_min(dev, buf, offset);				\
591}									\
592static ssize_t show_in_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)	\
593{									\
594	return show_in_max(dev, buf, offset);				\
595}									\
596static ssize_t set_in_##offset##_min (struct device *dev, struct device_attribute *attr, 		\
597	const char *buf, size_t count) 					\
598{									\
599	return set_in_min(dev, buf, count, offset);			\
600}									\
601static ssize_t set_in_##offset##_max (struct device *dev, struct device_attribute *attr, 		\
602	const char *buf, size_t count) 					\
603{									\
604	return set_in_max(dev, buf, count, offset);			\
605}									\
606static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in_##offset, 	\
607		NULL);							\
608static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, 		\
609		show_in_##offset##_min, set_in_##offset##_min);		\
610static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, 		\
611		show_in_##offset##_max, set_in_##offset##_max);
612
613show_in_reg(0);
614show_in_reg(1);
615show_in_reg(2);
616show_in_reg(3);
617show_in_reg(4);
618
619/* Temps */
620
621static ssize_t show_temp(struct device *dev, char *buf, int nr)
622{
623	struct lm85_data *data = lm85_update_device(dev);
624	return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
625						    data->temp_ext[nr],
626						    data->adc_scale) );
627}
628static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
629{
630	struct lm85_data *data = lm85_update_device(dev);
631	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
632}
633static ssize_t set_temp_min(struct device *dev, const char *buf,
634		size_t count, int nr)
635{
636	struct i2c_client *client = to_i2c_client(dev);
637	struct lm85_data *data = i2c_get_clientdata(client);
638	long val = simple_strtol(buf, NULL, 10);
639
640	mutex_lock(&data->update_lock);
641	data->temp_min[nr] = TEMP_TO_REG(val);
642	lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
643	mutex_unlock(&data->update_lock);
644	return count;
645}
646static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
647{
648	struct lm85_data *data = lm85_update_device(dev);
649	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
650}
651static ssize_t set_temp_max(struct device *dev, const char *buf,
652		size_t count, int nr)
653{
654	struct i2c_client *client = to_i2c_client(dev);
655	struct lm85_data *data = i2c_get_clientdata(client);
656	long val = simple_strtol(buf, NULL, 10);
657
658	mutex_lock(&data->update_lock);
659	data->temp_max[nr] = TEMP_TO_REG(val);
660	lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
661	mutex_unlock(&data->update_lock);
662	return count;
663}
664#define show_temp_reg(offset)						\
665static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf)	\
666{									\
667	return show_temp(dev, buf, offset - 1);				\
668}									\
669static ssize_t show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)	\
670{									\
671	return show_temp_min(dev, buf, offset - 1);			\
672}									\
673static ssize_t show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)	\
674{									\
675	return show_temp_max(dev, buf, offset - 1);			\
676}									\
677static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, 		\
678	const char *buf, size_t count) 					\
679{									\
680	return set_temp_min(dev, buf, count, offset - 1);		\
681}									\
682static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, 		\
683	const char *buf, size_t count) 					\
684{									\
685	return set_temp_max(dev, buf, count, offset - 1);		\
686}									\
687static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset,	\
688		NULL);							\
689static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, 		\
690		show_temp_##offset##_min, set_temp_##offset##_min);	\
691static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, 		\
692		show_temp_##offset##_max, set_temp_##offset##_max);
693
694show_temp_reg(1);
695show_temp_reg(2);
696show_temp_reg(3);
697
698
699/* Automatic PWM control */
700
701static ssize_t show_pwm_auto_channels(struct device *dev, char *buf, int nr)
702{
703	struct lm85_data *data = lm85_update_device(dev);
704	return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
705}
706static ssize_t set_pwm_auto_channels(struct device *dev, const char *buf,
707	size_t count, int nr)
708{
709	struct i2c_client *client = to_i2c_client(dev);
710	struct lm85_data *data = i2c_get_clientdata(client);
711	long val = simple_strtol(buf, NULL, 10);
712
713	mutex_lock(&data->update_lock);
714	data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
715		| ZONE_TO_REG(val) ;
716	lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
717		data->autofan[nr].config);
718	mutex_unlock(&data->update_lock);
719	return count;
720}
721static ssize_t show_pwm_auto_pwm_min(struct device *dev, char *buf, int nr)
722{
723	struct lm85_data *data = lm85_update_device(dev);
724	return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
725}
726static ssize_t set_pwm_auto_pwm_min(struct device *dev, const char *buf,
727	size_t count, int nr)
728{
729	struct i2c_client *client = to_i2c_client(dev);
730	struct lm85_data *data = i2c_get_clientdata(client);
731	long val = simple_strtol(buf, NULL, 10);
732
733	mutex_lock(&data->update_lock);
734	data->autofan[nr].min_pwm = PWM_TO_REG(val);
735	lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
736		data->autofan[nr].min_pwm);
737	mutex_unlock(&data->update_lock);
738	return count;
739}
740static ssize_t show_pwm_auto_pwm_minctl(struct device *dev, char *buf, int nr)
741{
742	struct lm85_data *data = lm85_update_device(dev);
743	return sprintf(buf,"%d\n", data->autofan[nr].min_off);
744}
745static ssize_t set_pwm_auto_pwm_minctl(struct device *dev, const char *buf,
746	size_t count, int nr)
747{
748	struct i2c_client *client = to_i2c_client(dev);
749	struct lm85_data *data = i2c_get_clientdata(client);
750	long val = simple_strtol(buf, NULL, 10);
751
752	mutex_lock(&data->update_lock);
753	data->autofan[nr].min_off = val;
754	lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
755		| data->syncpwm3
756		| (data->autofan[0].min_off ? 0x20 : 0)
757		| (data->autofan[1].min_off ? 0x40 : 0)
758		| (data->autofan[2].min_off ? 0x80 : 0)
759	);
760	mutex_unlock(&data->update_lock);
761	return count;
762}
763static ssize_t show_pwm_auto_pwm_freq(struct device *dev, char *buf, int nr)
764{
765	struct lm85_data *data = lm85_update_device(dev);
766	return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
767}
768static ssize_t set_pwm_auto_pwm_freq(struct device *dev, const char *buf,
769		size_t count, int nr)
770{
771	struct i2c_client *client = to_i2c_client(dev);
772	struct lm85_data *data = i2c_get_clientdata(client);
773	long val = simple_strtol(buf, NULL, 10);
774
775	mutex_lock(&data->update_lock);
776	data->autofan[nr].freq = FREQ_TO_REG(val);
777	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
778		(data->zone[nr].range << 4)
779		| data->autofan[nr].freq
780	);
781	mutex_unlock(&data->update_lock);
782	return count;
783}
784#define pwm_auto(offset)						\
785static ssize_t show_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr,	\
786	char *buf)							\
787{									\
788	return show_pwm_auto_channels(dev, buf, offset - 1);		\
789}									\
790static ssize_t set_pwm##offset##_auto_channels (struct device *dev, struct device_attribute *attr,	\
791	const char *buf, size_t count)					\
792{									\
793	return set_pwm_auto_channels(dev, buf, count, offset - 1);	\
794}									\
795static ssize_t show_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr,	\
796	char *buf)							\
797{									\
798	return show_pwm_auto_pwm_min(dev, buf, offset - 1);		\
799}									\
800static ssize_t set_pwm##offset##_auto_pwm_min (struct device *dev, struct device_attribute *attr,	\
801	const char *buf, size_t count)					\
802{									\
803	return set_pwm_auto_pwm_min(dev, buf, count, offset - 1);	\
804}									\
805static ssize_t show_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr,	\
806	char *buf)							\
807{									\
808	return show_pwm_auto_pwm_minctl(dev, buf, offset - 1);		\
809}									\
810static ssize_t set_pwm##offset##_auto_pwm_minctl (struct device *dev, struct device_attribute *attr,	\
811	const char *buf, size_t count)					\
812{									\
813	return set_pwm_auto_pwm_minctl(dev, buf, count, offset - 1);	\
814}									\
815static ssize_t show_pwm##offset##_auto_pwm_freq (struct device *dev, struct device_attribute *attr,	\
816	char *buf)							\
817{									\
818	return show_pwm_auto_pwm_freq(dev, buf, offset - 1);		\
819}									\
820static ssize_t set_pwm##offset##_auto_pwm_freq(struct device *dev, struct device_attribute *attr,	\
821	const char *buf, size_t count)					\
822{									\
823	return set_pwm_auto_pwm_freq(dev, buf, count, offset - 1);	\
824}									\
825static DEVICE_ATTR(pwm##offset##_auto_channels, S_IRUGO | S_IWUSR,	\
826		show_pwm##offset##_auto_channels,			\
827		set_pwm##offset##_auto_channels);			\
828static DEVICE_ATTR(pwm##offset##_auto_pwm_min, S_IRUGO | S_IWUSR,	\
829		show_pwm##offset##_auto_pwm_min,			\
830		set_pwm##offset##_auto_pwm_min);			\
831static DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, S_IRUGO | S_IWUSR,	\
832		show_pwm##offset##_auto_pwm_minctl,			\
833		set_pwm##offset##_auto_pwm_minctl);			\
834static DEVICE_ATTR(pwm##offset##_auto_pwm_freq, S_IRUGO | S_IWUSR,	\
835		show_pwm##offset##_auto_pwm_freq,			\
836		set_pwm##offset##_auto_pwm_freq);
837pwm_auto(1);
838pwm_auto(2);
839pwm_auto(3);
840
841/* Temperature settings for automatic PWM control */
842
843static ssize_t show_temp_auto_temp_off(struct device *dev, char *buf, int nr)
844{
845	struct lm85_data *data = lm85_update_device(dev);
846	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
847		HYST_FROM_REG(data->zone[nr].hyst));
848}
849static ssize_t set_temp_auto_temp_off(struct device *dev, const char *buf,
850	size_t count, int nr)
851{
852	struct i2c_client *client = to_i2c_client(dev);
853	struct lm85_data *data = i2c_get_clientdata(client);
854	int min;
855	long val = simple_strtol(buf, NULL, 10);
856
857	mutex_lock(&data->update_lock);
858	min = TEMP_FROM_REG(data->zone[nr].limit);
859	data->zone[nr].off_desired = TEMP_TO_REG(val);
860	data->zone[nr].hyst = HYST_TO_REG(min - val);
861	if ( nr == 0 || nr == 1 ) {
862		lm85_write_value(client, LM85_REG_AFAN_HYST1,
863			(data->zone[0].hyst << 4)
864			| data->zone[1].hyst
865			);
866	} else {
867		lm85_write_value(client, LM85_REG_AFAN_HYST2,
868			(data->zone[2].hyst << 4)
869		);
870	}
871	mutex_unlock(&data->update_lock);
872	return count;
873}
874static ssize_t show_temp_auto_temp_min(struct device *dev, char *buf, int nr)
875{
876	struct lm85_data *data = lm85_update_device(dev);
877	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
878}
879static ssize_t set_temp_auto_temp_min(struct device *dev, const char *buf,
880	size_t count, int nr)
881{
882	struct i2c_client *client = to_i2c_client(dev);
883	struct lm85_data *data = i2c_get_clientdata(client);
884	long val = simple_strtol(buf, NULL, 10);
885
886	mutex_lock(&data->update_lock);
887	data->zone[nr].limit = TEMP_TO_REG(val);
888	lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
889		data->zone[nr].limit);
890
891/* Update temp_auto_max and temp_auto_range */
892	data->zone[nr].range = RANGE_TO_REG(
893		TEMP_FROM_REG(data->zone[nr].max_desired) -
894		TEMP_FROM_REG(data->zone[nr].limit));
895	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
896		((data->zone[nr].range & 0x0f) << 4)
897		| (data->autofan[nr].freq & 0x07));
898
899/* Update temp_auto_hyst and temp_auto_off */
900	data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
901		data->zone[nr].limit) - TEMP_FROM_REG(
902		data->zone[nr].off_desired));
903	if ( nr == 0 || nr == 1 ) {
904		lm85_write_value(client, LM85_REG_AFAN_HYST1,
905			(data->zone[0].hyst << 4)
906			| data->zone[1].hyst
907			);
908	} else {
909		lm85_write_value(client, LM85_REG_AFAN_HYST2,
910			(data->zone[2].hyst << 4)
911		);
912	}
913	mutex_unlock(&data->update_lock);
914	return count;
915}
916static ssize_t show_temp_auto_temp_max(struct device *dev, char *buf, int nr)
917{
918	struct lm85_data *data = lm85_update_device(dev);
919	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
920		RANGE_FROM_REG(data->zone[nr].range));
921}
922static ssize_t set_temp_auto_temp_max(struct device *dev, const char *buf,
923	size_t count, int nr)
924{
925	struct i2c_client *client = to_i2c_client(dev);
926	struct lm85_data *data = i2c_get_clientdata(client);
927	int min;
928	long val = simple_strtol(buf, NULL, 10);
929
930	mutex_lock(&data->update_lock);
931	min = TEMP_FROM_REG(data->zone[nr].limit);
932	data->zone[nr].max_desired = TEMP_TO_REG(val);
933	data->zone[nr].range = RANGE_TO_REG(
934		val - min);
935	lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
936		((data->zone[nr].range & 0x0f) << 4)
937		| (data->autofan[nr].freq & 0x07));
938	mutex_unlock(&data->update_lock);
939	return count;
940}
941static ssize_t show_temp_auto_temp_crit(struct device *dev, char *buf, int nr)
942{
943	struct lm85_data *data = lm85_update_device(dev);
944	return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
945}
946static ssize_t set_temp_auto_temp_crit(struct device *dev, const char *buf,
947		size_t count, int nr)
948{
949	struct i2c_client *client = to_i2c_client(dev);
950	struct lm85_data *data = i2c_get_clientdata(client);
951	long val = simple_strtol(buf, NULL, 10);
952
953	mutex_lock(&data->update_lock);
954	data->zone[nr].critical = TEMP_TO_REG(val);
955	lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
956		data->zone[nr].critical);
957	mutex_unlock(&data->update_lock);
958	return count;
959}
960#define temp_auto(offset)						\
961static ssize_t show_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr,	\
962	char *buf)							\
963{									\
964	return show_temp_auto_temp_off(dev, buf, offset - 1);		\
965}									\
966static ssize_t set_temp##offset##_auto_temp_off (struct device *dev, struct device_attribute *attr,	\
967	const char *buf, size_t count)					\
968{									\
969	return set_temp_auto_temp_off(dev, buf, count, offset - 1);	\
970}									\
971static ssize_t show_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr,	\
972	char *buf)							\
973{									\
974	return show_temp_auto_temp_min(dev, buf, offset - 1);		\
975}									\
976static ssize_t set_temp##offset##_auto_temp_min (struct device *dev, struct device_attribute *attr,	\
977	const char *buf, size_t count)					\
978{									\
979	return set_temp_auto_temp_min(dev, buf, count, offset - 1);	\
980}									\
981static ssize_t show_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr,	\
982	char *buf)							\
983{									\
984	return show_temp_auto_temp_max(dev, buf, offset - 1);		\
985}									\
986static ssize_t set_temp##offset##_auto_temp_max (struct device *dev, struct device_attribute *attr,	\
987	const char *buf, size_t count)					\
988{									\
989	return set_temp_auto_temp_max(dev, buf, count, offset - 1);	\
990}									\
991static ssize_t show_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr,	\
992	char *buf)							\
993{									\
994	return show_temp_auto_temp_crit(dev, buf, offset - 1);		\
995}									\
996static ssize_t set_temp##offset##_auto_temp_crit (struct device *dev, struct device_attribute *attr,	\
997	const char *buf, size_t count)					\
998{									\
999	return set_temp_auto_temp_crit(dev, buf, count, offset - 1);	\
1000}									\
1001static DEVICE_ATTR(temp##offset##_auto_temp_off, S_IRUGO | S_IWUSR,	\
1002		show_temp##offset##_auto_temp_off,			\
1003		set_temp##offset##_auto_temp_off);			\
1004static DEVICE_ATTR(temp##offset##_auto_temp_min, S_IRUGO | S_IWUSR,	\
1005		show_temp##offset##_auto_temp_min,			\
1006		set_temp##offset##_auto_temp_min);			\
1007static DEVICE_ATTR(temp##offset##_auto_temp_max, S_IRUGO | S_IWUSR,	\
1008		show_temp##offset##_auto_temp_max,			\
1009		set_temp##offset##_auto_temp_max);			\
1010static DEVICE_ATTR(temp##offset##_auto_temp_crit, S_IRUGO | S_IWUSR,	\
1011		show_temp##offset##_auto_temp_crit,			\
1012		set_temp##offset##_auto_temp_crit);
1013temp_auto(1);
1014temp_auto(2);
1015temp_auto(3);
1016
1017static int lm85_attach_adapter(struct i2c_adapter *adapter)
1018{
1019	if (!(adapter->class & I2C_CLASS_HWMON))
1020		return 0;
1021	return i2c_probe(adapter, &addr_data, lm85_detect);
1022}
1023
1024static struct attribute *lm85_attributes[] = {
1025	&dev_attr_fan1_input.attr,
1026	&dev_attr_fan2_input.attr,
1027	&dev_attr_fan3_input.attr,
1028	&dev_attr_fan4_input.attr,
1029	&dev_attr_fan1_min.attr,
1030	&dev_attr_fan2_min.attr,
1031	&dev_attr_fan3_min.attr,
1032	&dev_attr_fan4_min.attr,
1033	&dev_attr_pwm1.attr,
1034	&dev_attr_pwm2.attr,
1035	&dev_attr_pwm3.attr,
1036	&dev_attr_pwm1_enable.attr,
1037	&dev_attr_pwm2_enable.attr,
1038	&dev_attr_pwm3_enable.attr,
1039	&dev_attr_in0_input.attr,
1040	&dev_attr_in1_input.attr,
1041	&dev_attr_in2_input.attr,
1042	&dev_attr_in3_input.attr,
1043	&dev_attr_in0_min.attr,
1044	&dev_attr_in1_min.attr,
1045	&dev_attr_in2_min.attr,
1046	&dev_attr_in3_min.attr,
1047	&dev_attr_in0_max.attr,
1048	&dev_attr_in1_max.attr,
1049	&dev_attr_in2_max.attr,
1050	&dev_attr_in3_max.attr,
1051	&dev_attr_temp1_input.attr,
1052	&dev_attr_temp2_input.attr,
1053	&dev_attr_temp3_input.attr,
1054	&dev_attr_temp1_min.attr,
1055	&dev_attr_temp2_min.attr,
1056	&dev_attr_temp3_min.attr,
1057	&dev_attr_temp1_max.attr,
1058	&dev_attr_temp2_max.attr,
1059	&dev_attr_temp3_max.attr,
1060	&dev_attr_vrm.attr,
1061	&dev_attr_cpu0_vid.attr,
1062	&dev_attr_alarms.attr,
1063	&dev_attr_pwm1_auto_channels.attr,
1064	&dev_attr_pwm2_auto_channels.attr,
1065	&dev_attr_pwm3_auto_channels.attr,
1066	&dev_attr_pwm1_auto_pwm_min.attr,
1067	&dev_attr_pwm2_auto_pwm_min.attr,
1068	&dev_attr_pwm3_auto_pwm_min.attr,
1069	&dev_attr_pwm1_auto_pwm_minctl.attr,
1070	&dev_attr_pwm2_auto_pwm_minctl.attr,
1071	&dev_attr_pwm3_auto_pwm_minctl.attr,
1072	&dev_attr_pwm1_auto_pwm_freq.attr,
1073	&dev_attr_pwm2_auto_pwm_freq.attr,
1074	&dev_attr_pwm3_auto_pwm_freq.attr,
1075	&dev_attr_temp1_auto_temp_off.attr,
1076	&dev_attr_temp2_auto_temp_off.attr,
1077	&dev_attr_temp3_auto_temp_off.attr,
1078	&dev_attr_temp1_auto_temp_min.attr,
1079	&dev_attr_temp2_auto_temp_min.attr,
1080	&dev_attr_temp3_auto_temp_min.attr,
1081	&dev_attr_temp1_auto_temp_max.attr,
1082	&dev_attr_temp2_auto_temp_max.attr,
1083	&dev_attr_temp3_auto_temp_max.attr,
1084	&dev_attr_temp1_auto_temp_crit.attr,
1085	&dev_attr_temp2_auto_temp_crit.attr,
1086	&dev_attr_temp3_auto_temp_crit.attr,
1087
1088	NULL
1089};
1090
1091static const struct attribute_group lm85_group = {
1092	.attrs = lm85_attributes,
1093};
1094
1095static struct attribute *lm85_attributes_opt[] = {
1096	&dev_attr_in4_input.attr,
1097	&dev_attr_in4_min.attr,
1098	&dev_attr_in4_max.attr,
1099
1100	NULL
1101};
1102
1103static const struct attribute_group lm85_group_opt = {
1104	.attrs = lm85_attributes_opt,
1105};
1106
1107static int lm85_detect(struct i2c_adapter *adapter, int address,
1108		int kind)
1109{
1110	int company, verstep ;
1111	struct i2c_client *new_client = NULL;
1112	struct lm85_data *data;
1113	int err = 0;
1114	const char *type_name = "";
1115
1116	if (!i2c_check_functionality(adapter,
1117					I2C_FUNC_SMBUS_BYTE_DATA)) {
1118		/* We need to be able to do byte I/O */
1119		goto ERROR0 ;
1120	};
1121
1122	/* OK. For now, we presume we have a valid client. We now create the
1123	   client structure, even though we cannot fill it completely yet.
1124	   But it allows us to access lm85_{read,write}_value. */
1125
1126	if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
1127		err = -ENOMEM;
1128		goto ERROR0;
1129	}
1130
1131	new_client = &data->client;
1132	i2c_set_clientdata(new_client, data);
1133	new_client->addr = address;
1134	new_client->adapter = adapter;
1135	new_client->driver = &lm85_driver;
1136	new_client->flags = 0;
1137
1138	/* Now, we do the remaining detection. */
1139
1140	company = lm85_read_value(new_client, LM85_REG_COMPANY);
1141	verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1142
1143	dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1144		" COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1145		i2c_adapter_id(new_client->adapter), new_client->addr,
1146		company, verstep);
1147
1148	/* If auto-detecting, Determine the chip type. */
1149	if (kind <= 0) {
1150		dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1151			i2c_adapter_id(adapter), address );
1152		if( company == LM85_COMPANY_NATIONAL
1153		    && verstep == LM85_VERSTEP_LM85C ) {
1154			kind = lm85c ;
1155		} else if( company == LM85_COMPANY_NATIONAL
1156		    && verstep == LM85_VERSTEP_LM85B ) {
1157			kind = lm85b ;
1158		} else if( company == LM85_COMPANY_NATIONAL
1159		    && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1160			dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1161				" Defaulting to LM85.\n", verstep);
1162			kind = any_chip ;
1163		} else if( company == LM85_COMPANY_ANALOG_DEV
1164		    && verstep == LM85_VERSTEP_ADM1027 ) {
1165			kind = adm1027 ;
1166		} else if( company == LM85_COMPANY_ANALOG_DEV
1167		    && (verstep == LM85_VERSTEP_ADT7463
1168			 || verstep == LM85_VERSTEP_ADT7463C) ) {
1169			kind = adt7463 ;
1170		} else if( company == LM85_COMPANY_ANALOG_DEV
1171		    && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1172			dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1173				" Defaulting to Generic LM85.\n", verstep );
1174			kind = any_chip ;
1175		} else if( company == LM85_COMPANY_SMSC
1176		    && (verstep == LM85_VERSTEP_EMC6D100_A0
1177			 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1178			/* Unfortunately, we can't tell a '100 from a '101
1179			 * from the registers.  Since a '101 is a '100
1180			 * in a package with fewer pins and therefore no
1181			 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1182			 * inputs read 0, then it's a '101.
1183			 */
1184			kind = emc6d100 ;
1185		} else if( company == LM85_COMPANY_SMSC
1186		    && verstep == LM85_VERSTEP_EMC6D102) {
1187			kind = emc6d102 ;
1188		} else if( company == LM85_COMPANY_SMSC
1189		    && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1190			dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1191			dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1192			    " Defaulting to Generic LM85.\n", verstep );
1193			kind = any_chip ;
1194		} else if( kind == any_chip
1195		    && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1196			dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1197			/* Leave kind as "any_chip" */
1198		} else {
1199			dev_dbg(&adapter->dev, "Autodetection failed\n");
1200			/* Not an LM85 ... */
1201			if( kind == any_chip ) {  /* User used force=x,y */
1202				dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1203					" found at %d,0x%02x. Try force_lm85c.\n",
1204					i2c_adapter_id(adapter), address );
1205			}
1206			err = 0 ;
1207			goto ERROR1;
1208		}
1209	}
1210
1211	/* Fill in the chip specific driver values */
1212	if ( kind == any_chip ) {
1213		type_name = "lm85";
1214	} else if ( kind == lm85b ) {
1215		type_name = "lm85b";
1216	} else if ( kind == lm85c ) {
1217		type_name = "lm85c";
1218	} else if ( kind == adm1027 ) {
1219		type_name = "adm1027";
1220	} else if ( kind == adt7463 ) {
1221		type_name = "adt7463";
1222	} else if ( kind == emc6d100){
1223		type_name = "emc6d100";
1224	} else if ( kind == emc6d102 ) {
1225		type_name = "emc6d102";
1226	}
1227	strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1228
1229	/* Fill in the remaining client fields */
1230	data->type = kind;
1231	data->valid = 0;
1232	mutex_init(&data->update_lock);
1233
1234	/* Tell the I2C layer a new client has arrived */
1235	if ((err = i2c_attach_client(new_client)))
1236		goto ERROR1;
1237
1238	/* Set the VRM version */
1239	data->vrm = vid_which_vrm();
1240
1241	/* Initialize the LM85 chip */
1242	lm85_init_client(new_client);
1243
1244	/* Register sysfs hooks */
1245	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
1246		goto ERROR2;
1247
1248	/* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1249	   as a sixth digital VID input rather than an analog input. */
1250	data->vid = lm85_read_value(new_client, LM85_REG_VID);
1251	if (!(kind == adt7463 && (data->vid & 0x80)))
1252		if ((err = device_create_file(&new_client->dev,
1253					&dev_attr_in4_input))
1254		 || (err = device_create_file(&new_client->dev,
1255					&dev_attr_in4_min))
1256		 || (err = device_create_file(&new_client->dev,
1257					&dev_attr_in4_max)))
1258			goto ERROR3;
1259
1260	data->class_dev = hwmon_device_register(&new_client->dev);
1261	if (IS_ERR(data->class_dev)) {
1262		err = PTR_ERR(data->class_dev);
1263		goto ERROR3;
1264	}
1265
1266	return 0;
1267
1268	/* Error out and cleanup code */
1269    ERROR3:
1270	sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
1271	sysfs_remove_group(&new_client->dev.kobj, &lm85_group_opt);
1272    ERROR2:
1273	i2c_detach_client(new_client);
1274    ERROR1:
1275	kfree(data);
1276    ERROR0:
1277	return err;
1278}
1279
1280static int lm85_detach_client(struct i2c_client *client)
1281{
1282	struct lm85_data *data = i2c_get_clientdata(client);
1283	hwmon_device_unregister(data->class_dev);
1284	sysfs_remove_group(&client->dev.kobj, &lm85_group);
1285	sysfs_remove_group(&client->dev.kobj, &lm85_group_opt);
1286	i2c_detach_client(client);
1287	kfree(data);
1288	return 0;
1289}
1290
1291
1292static int lm85_read_value(struct i2c_client *client, u8 reg)
1293{
1294	int res;
1295
1296	/* What size location is it? */
1297	switch( reg ) {
1298	case LM85_REG_FAN(0) :  /* Read WORD data */
1299	case LM85_REG_FAN(1) :
1300	case LM85_REG_FAN(2) :
1301	case LM85_REG_FAN(3) :
1302	case LM85_REG_FAN_MIN(0) :
1303	case LM85_REG_FAN_MIN(1) :
1304	case LM85_REG_FAN_MIN(2) :
1305	case LM85_REG_FAN_MIN(3) :
1306	case LM85_REG_ALARM1 :	/* Read both bytes at once */
1307		res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1308		res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1309		break ;
1310	case ADT7463_REG_TMIN_CTL1 :  /* Read WORD MSB, LSB */
1311		res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1312		res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1313		break ;
1314	default:	/* Read BYTE data */
1315		res = i2c_smbus_read_byte_data(client, reg);
1316		break ;
1317	}
1318
1319	return res ;
1320}
1321
1322static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
1323{
1324	int res ;
1325
1326	switch( reg ) {
1327	case LM85_REG_FAN(0) :  /* Write WORD data */
1328	case LM85_REG_FAN(1) :
1329	case LM85_REG_FAN(2) :
1330	case LM85_REG_FAN(3) :
1331	case LM85_REG_FAN_MIN(0) :
1332	case LM85_REG_FAN_MIN(1) :
1333	case LM85_REG_FAN_MIN(2) :
1334	case LM85_REG_FAN_MIN(3) :
1335	/* NOTE: ALARM is read only, so not included here */
1336		res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1337		res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1338		break ;
1339	case ADT7463_REG_TMIN_CTL1 :  /* Write WORD MSB, LSB */
1340		res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1341		res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1342		break ;
1343	default:	/* Write BYTE data */
1344		res = i2c_smbus_write_byte_data(client, reg, value);
1345		break ;
1346	}
1347
1348	return res ;
1349}
1350
1351static void lm85_init_client(struct i2c_client *client)
1352{
1353	int value;
1354	struct lm85_data *data = i2c_get_clientdata(client);
1355
1356	dev_dbg(&client->dev, "Initializing device\n");
1357
1358	/* Warn if part was not "READY" */
1359	value = lm85_read_value(client, LM85_REG_CONFIG);
1360	dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1361	if( value & 0x02 ) {
1362		dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1363			    i2c_adapter_id(client->adapter), client->addr );
1364	};
1365	if( ! (value & 0x04) ) {
1366		dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1367			    i2c_adapter_id(client->adapter), client->addr );
1368	};
1369	if( value & 0x10
1370	    && ( data->type == adm1027
1371		|| data->type == adt7463 ) ) {
1372		dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set.  "
1373			"Please report this to the lm85 maintainer.\n",
1374			    i2c_adapter_id(client->adapter), client->addr );
1375	};
1376
1377	/* WE INTENTIONALLY make no changes to the limits,
1378	 *   offsets, pwms, fans and zones.  If they were
1379	 *   configured, we don't want to mess with them.
1380	 *   If they weren't, the default is 100% PWM, no
1381	 *   control and will suffice until 'sensors -s'
1382	 *   can be run by the user.
1383	 */
1384
1385	/* Start monitoring */
1386	value = lm85_read_value(client, LM85_REG_CONFIG);
1387	/* Try to clear LOCK, Set START, save everything else */
1388	value = (value & ~ 0x02) | 0x01 ;
1389	dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1390	lm85_write_value(client, LM85_REG_CONFIG, value);
1391}
1392
1393static struct lm85_data *lm85_update_device(struct device *dev)
1394{
1395	struct i2c_client *client = to_i2c_client(dev);
1396	struct lm85_data *data = i2c_get_clientdata(client);
1397	int i;
1398
1399	mutex_lock(&data->update_lock);
1400
1401	if ( !data->valid ||
1402	     time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1403		/* Things that change quickly */
1404		dev_dbg(&client->dev, "Reading sensor values\n");
1405
1406		/* Have to read extended bits first to "freeze" the
1407		 * more significant bits that are read later.
1408		 */
1409		if ( (data->type == adm1027) || (data->type == adt7463) ) {
1410			int ext1 = lm85_read_value(client,
1411						   ADM1027_REG_EXTEND_ADC1);
1412			int ext2 =  lm85_read_value(client,
1413						    ADM1027_REG_EXTEND_ADC2);
1414			int val = (ext1 << 8) + ext2;
1415
1416			for(i = 0; i <= 4; i++)
1417				data->in_ext[i] = (val>>(i * 2))&0x03;
1418
1419			for(i = 0; i <= 2; i++)
1420				data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1421		}
1422
1423		/* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1424		   the emc6d102 and 2 in the adt7463 and adm1027. In all
1425		   other chips ext is always 0 and the value of scale is
1426		   irrelevant. So it is left in 4*/
1427		data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1428
1429		data->vid = lm85_read_value(client, LM85_REG_VID);
1430
1431		for (i = 0; i <= 3; ++i) {
1432			data->in[i] =
1433			    lm85_read_value(client, LM85_REG_IN(i));
1434		}
1435
1436		if (!(data->type == adt7463 && (data->vid & 0x80))) {
1437			data->in[4] = lm85_read_value(client,
1438				      LM85_REG_IN(4));
1439		}
1440
1441		for (i = 0; i <= 3; ++i) {
1442			data->fan[i] =
1443			    lm85_read_value(client, LM85_REG_FAN(i));
1444		}
1445
1446		for (i = 0; i <= 2; ++i) {
1447			data->temp[i] =
1448			    lm85_read_value(client, LM85_REG_TEMP(i));
1449		}
1450
1451		for (i = 0; i <= 2; ++i) {
1452			data->pwm[i] =
1453			    lm85_read_value(client, LM85_REG_PWM(i));
1454		}
1455
1456		data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1457
1458		if ( data->type == adt7463 ) {
1459			if( data->therm_total < ULONG_MAX - 256 ) {
1460			    data->therm_total +=
1461				lm85_read_value(client, ADT7463_REG_THERM );
1462			}
1463		} else if ( data->type == emc6d100 ) {
1464			/* Three more voltage sensors */
1465			for (i = 5; i <= 7; ++i) {
1466				data->in[i] =
1467					lm85_read_value(client, EMC6D100_REG_IN(i));
1468			}
1469			/* More alarm bits */
1470			data->alarms |=
1471				lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1472		} else if (data->type == emc6d102 ) {
1473			/* Have to read LSB bits after the MSB ones because
1474			   the reading of the MSB bits has frozen the
1475			   LSBs (backward from the ADM1027).
1476			 */
1477			int ext1 = lm85_read_value(client,
1478						   EMC6D102_REG_EXTEND_ADC1);
1479			int ext2 = lm85_read_value(client,
1480						   EMC6D102_REG_EXTEND_ADC2);
1481			int ext3 = lm85_read_value(client,
1482						   EMC6D102_REG_EXTEND_ADC3);
1483			int ext4 = lm85_read_value(client,
1484						   EMC6D102_REG_EXTEND_ADC4);
1485			data->in_ext[0] = ext3 & 0x0f;
1486			data->in_ext[1] = ext4 & 0x0f;
1487			data->in_ext[2] = (ext4 >> 4) & 0x0f;
1488			data->in_ext[3] = (ext3 >> 4) & 0x0f;
1489			data->in_ext[4] = (ext2 >> 4) & 0x0f;
1490
1491			data->temp_ext[0] = ext1 & 0x0f;
1492			data->temp_ext[1] = ext2 & 0x0f;
1493			data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1494		}
1495
1496		data->last_reading = jiffies ;
1497	};  /* last_reading */
1498
1499	if ( !data->valid ||
1500	     time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1501		/* Things that don't change often */
1502		dev_dbg(&client->dev, "Reading config values\n");
1503
1504		for (i = 0; i <= 3; ++i) {
1505			data->in_min[i] =
1506			    lm85_read_value(client, LM85_REG_IN_MIN(i));
1507			data->in_max[i] =
1508			    lm85_read_value(client, LM85_REG_IN_MAX(i));
1509		}
1510
1511		if (!(data->type == adt7463 && (data->vid & 0x80))) {
1512			data->in_min[4] = lm85_read_value(client,
1513					  LM85_REG_IN_MIN(4));
1514			data->in_max[4] = lm85_read_value(client,
1515					  LM85_REG_IN_MAX(4));
1516		}
1517
1518		if ( data->type == emc6d100 ) {
1519			for (i = 5; i <= 7; ++i) {
1520				data->in_min[i] =
1521					lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1522				data->in_max[i] =
1523					lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1524			}
1525		}
1526
1527		for (i = 0; i <= 3; ++i) {
1528			data->fan_min[i] =
1529			    lm85_read_value(client, LM85_REG_FAN_MIN(i));
1530		}
1531
1532		for (i = 0; i <= 2; ++i) {
1533			data->temp_min[i] =
1534			    lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1535			data->temp_max[i] =
1536			    lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1537		}
1538
1539		for (i = 0; i <= 2; ++i) {
1540			int val ;
1541			data->autofan[i].config =
1542			    lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1543			val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1544			data->autofan[i].freq = val & 0x07 ;
1545			data->zone[i].range = (val >> 4) & 0x0f ;
1546			data->autofan[i].min_pwm =
1547			    lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1548			data->zone[i].limit =
1549			    lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1550			data->zone[i].critical =
1551			    lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1552		}
1553
1554		i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1555		data->smooth[0] = i & 0x0f ;
1556		data->syncpwm3 = i & 0x10 ;  /* Save PWM3 config */
1557		data->autofan[0].min_off = (i & 0x20) != 0 ;
1558		data->autofan[1].min_off = (i & 0x40) != 0 ;
1559		data->autofan[2].min_off = (i & 0x80) != 0 ;
1560		i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1561		data->smooth[1] = (i>>4) & 0x0f ;
1562		data->smooth[2] = i & 0x0f ;
1563
1564		i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1565		data->zone[0].hyst = (i>>4) & 0x0f ;
1566		data->zone[1].hyst = i & 0x0f ;
1567
1568		i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1569		data->zone[2].hyst = (i>>4) & 0x0f ;
1570
1571		if ( (data->type == lm85b) || (data->type == lm85c) ) {
1572			data->tach_mode = lm85_read_value(client,
1573				LM85_REG_TACH_MODE );
1574			data->spinup_ctl = lm85_read_value(client,
1575				LM85_REG_SPINUP_CTL );
1576		} else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1577			if ( data->type == adt7463 ) {
1578				for (i = 0; i <= 2; ++i) {
1579				    data->oppoint[i] = lm85_read_value(client,
1580					ADT7463_REG_OPPOINT(i) );
1581				}
1582				data->tmin_ctl = lm85_read_value(client,
1583					ADT7463_REG_TMIN_CTL1 );
1584				data->therm_limit = lm85_read_value(client,
1585					ADT7463_REG_THERM_LIMIT );
1586			}
1587			for (i = 0; i <= 2; ++i) {
1588			    data->temp_offset[i] = lm85_read_value(client,
1589				ADM1027_REG_TEMP_OFFSET(i) );
1590			}
1591			data->tach_mode = lm85_read_value(client,
1592				ADM1027_REG_CONFIG3 );
1593			data->fan_ppr = lm85_read_value(client,
1594				ADM1027_REG_FAN_PPR );
1595		}
1596
1597		data->last_config = jiffies;
1598	};  /* last_config */
1599
1600	data->valid = 1;
1601
1602	mutex_unlock(&data->update_lock);
1603
1604	return data;
1605}
1606
1607
1608static int __init sm_lm85_init(void)
1609{
1610	return i2c_add_driver(&lm85_driver);
1611}
1612
1613static void  __exit sm_lm85_exit(void)
1614{
1615	i2c_del_driver(&lm85_driver);
1616}
1617
1618/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1619 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1620 *     post 2.7.0 CVS changes.
1621 */
1622MODULE_LICENSE("GPL");
1623MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1624MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1625
1626module_init(sm_lm85_init);
1627module_exit(sm_lm85_exit);
1628