blob: 0c61fa36c83cf0b4f2b16e6149e0aa73034d3bc0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020032#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020033#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Addresses to scan */
38static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020041I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* The LM85 registers */
44
45#define LM85_REG_IN(nr) (0x20 + (nr))
46#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
47#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
48
49#define LM85_REG_TEMP(nr) (0x25 + (nr))
50#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
51#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
52
53/* Fan speeds are LSB, MSB (2 bytes) */
54#define LM85_REG_FAN(nr) (0x28 + (nr) *2)
55#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2)
56
57#define LM85_REG_PWM(nr) (0x30 + (nr))
58
59#define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
60
61#define ADT7463_REG_TMIN_CTL1 0x36
62#define ADT7463_REG_TMIN_CTL2 0x37
63
64#define LM85_REG_DEVICE 0x3d
65#define LM85_REG_COMPANY 0x3e
66#define LM85_REG_VERSTEP 0x3f
67/* These are the recognized values for the above regs */
68#define LM85_DEVICE_ADX 0x27
69#define LM85_COMPANY_NATIONAL 0x01
70#define LM85_COMPANY_ANALOG_DEV 0x41
71#define LM85_COMPANY_SMSC 0x5c
72#define LM85_VERSTEP_VMASK 0xf0
73#define LM85_VERSTEP_GENERIC 0x60
74#define LM85_VERSTEP_LM85C 0x60
75#define LM85_VERSTEP_LM85B 0x62
76#define LM85_VERSTEP_ADM1027 0x60
77#define LM85_VERSTEP_ADT7463 0x62
78#define LM85_VERSTEP_ADT7463C 0x6A
79#define LM85_VERSTEP_EMC6D100_A0 0x60
80#define LM85_VERSTEP_EMC6D100_A1 0x61
81#define LM85_VERSTEP_EMC6D102 0x65
82
83#define LM85_REG_CONFIG 0x40
84
85#define LM85_REG_ALARM1 0x41
86#define LM85_REG_ALARM2 0x42
87
88#define LM85_REG_VID 0x43
89
90/* Automated FAN control */
91#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
92#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
93#define LM85_REG_AFAN_SPIKE1 0x62
94#define LM85_REG_AFAN_SPIKE2 0x63
95#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
96#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
97#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
98#define LM85_REG_AFAN_HYST1 0x6d
99#define LM85_REG_AFAN_HYST2 0x6e
100
101#define LM85_REG_TACH_MODE 0x74
102#define LM85_REG_SPINUP_CTL 0x75
103
104#define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
105#define ADM1027_REG_CONFIG2 0x73
106#define ADM1027_REG_INTMASK1 0x74
107#define ADM1027_REG_INTMASK2 0x75
108#define ADM1027_REG_EXTEND_ADC1 0x76
109#define ADM1027_REG_EXTEND_ADC2 0x77
110#define ADM1027_REG_CONFIG3 0x78
111#define ADM1027_REG_FAN_PPR 0x7b
112
113#define ADT7463_REG_THERM 0x79
114#define ADT7463_REG_THERM_LIMIT 0x7A
115
116#define EMC6D100_REG_ALARM3 0x7d
117/* IN5, IN6 and IN7 */
118#define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5))
119#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2)
120#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2)
121#define EMC6D102_REG_EXTEND_ADC1 0x85
122#define EMC6D102_REG_EXTEND_ADC2 0x86
123#define EMC6D102_REG_EXTEND_ADC3 0x87
124#define EMC6D102_REG_EXTEND_ADC4 0x88
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* Conversions. Rounding and limit checking is only done on the TO_REG
128 variants. Note that you should be a bit careful with which arguments
129 these macros are called: arguments may be evaluated more than once.
130 */
131
132/* IN are scaled acording to built-in resistors */
133static int lm85_scaling[] = { /* .001 Volts */
134 2500, 2250, 3300, 5000, 12000,
135 3300, 1500, 1800 /*EMC6D100*/
136 };
137#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
138
139#define INS_TO_REG(n,val) \
140 SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
141
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200142#define INSEXT_FROM_REG(n,val,ext) \
143 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200145#define INS_FROM_REG(n,val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* FAN speed is measured using 90kHz clock */
148#define FAN_TO_REG(val) (SENSORS_LIMIT( (val)<=0?0: 5400000/(val),0,65534))
149#define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
150
151/* Temperature is reported in .001 degC increments */
152#define TEMP_TO_REG(val) \
153 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200154#define TEMPEXT_FROM_REG(val,ext) \
155 SCALE(((val) << 4) + (ext), 16, 1000)
156#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
159#define PWM_FROM_REG(val) (val)
160
161
162/* ZONEs have the following parameters:
163 * Limit (low) temp, 1. degC
164 * Hysteresis (below limit), 1. degC (0-15)
165 * Range of speed control, .1 degC (2-80)
166 * Critical (high) temp, 1. degC
167 *
168 * FAN PWMs have the following parameters:
169 * Reference Zone, 1, 2, 3, etc.
170 * Spinup time, .05 sec
171 * PWM value at limit/low temp, 1 count
172 * PWM Frequency, 1. Hz
173 * PWM is Min or OFF below limit, flag
174 * Invert PWM output, flag
175 *
176 * Some chips filter the temp, others the fan.
177 * Filter constant (or disabled) .1 seconds
178 */
179
180/* These are the zone temperature range encodings in .001 degree C */
181static int lm85_range_map[] = {
182 2000, 2500, 3300, 4000, 5000, 6600,
183 8000, 10000, 13300, 16000, 20000, 26600,
184 32000, 40000, 53300, 80000
185 };
186static int RANGE_TO_REG( int range )
187{
188 int i;
189
190 if ( range < lm85_range_map[0] ) {
191 return 0 ;
192 } else if ( range > lm85_range_map[15] ) {
193 return 15 ;
194 } else { /* find closest match */
195 for ( i = 14 ; i >= 0 ; --i ) {
196 if ( range > lm85_range_map[i] ) { /* range bracketed */
197 if ((lm85_range_map[i+1] - range) <
198 (range - lm85_range_map[i])) {
199 i++;
200 break;
201 }
202 break;
203 }
204 }
205 }
206 return( i & 0x0f );
207}
208#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
209
210/* These are the Acoustic Enhancement, or Temperature smoothing encodings
211 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
212 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
213 * is ignored, or set to 0.
214 */
215/* These are the PWM frequency encodings */
216static int lm85_freq_map[] = { /* .1 Hz */
217 100, 150, 230, 300, 380, 470, 620, 940
218 };
219static int FREQ_TO_REG( int freq )
220{
221 int i;
222
223 if( freq >= lm85_freq_map[7] ) { return 7 ; }
224 for( i = 0 ; i < 7 ; ++i )
225 if( freq <= lm85_freq_map[i] )
226 break ;
227 return( i & 0x07 );
228}
229#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
230
231/* Since we can't use strings, I'm abusing these numbers
232 * to stand in for the following meanings:
233 * 1 -- PWM responds to Zone 1
234 * 2 -- PWM responds to Zone 2
235 * 3 -- PWM responds to Zone 3
236 * 23 -- PWM responds to the higher temp of Zone 2 or 3
237 * 123 -- PWM responds to highest of Zone 1, 2, or 3
238 * 0 -- PWM is always at 0% (ie, off)
239 * -1 -- PWM is always at 100%
240 * -2 -- PWM responds to manual control
241 */
242
243static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
244#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
245
246static int ZONE_TO_REG( int zone )
247{
248 int i;
249
250 for( i = 0 ; i <= 7 ; ++i )
251 if( zone == lm85_zone_map[i] )
252 break ;
253 if( i > 7 ) /* Not found. */
254 i = 3; /* Always 100% */
255 return( (i & 0x07)<<5 );
256}
257
258#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
259#define HYST_FROM_REG(val) ((val)*1000)
260
261#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
262#define OFFSET_FROM_REG(val) ((val)*25)
263
264#define PPR_MASK(fan) (0x03<<(fan *2))
265#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
266#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* Chip sampling rates
269 *
270 * Some sensors are not updated more frequently than once per second
271 * so it doesn't make sense to read them more often than that.
272 * We cache the results and return the saved data if the driver
273 * is called again before a second has elapsed.
274 *
275 * Also, there is significant configuration data for this chip
276 * given the automatic PWM fan control that is possible. There
277 * are about 47 bytes of config data to only 22 bytes of actual
278 * readings. So, we keep the config data up to date in the cache
279 * when it is written and only sample it once every 1 *minute*
280 */
281#define LM85_DATA_INTERVAL (HZ + HZ / 2)
282#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* LM85 can automatically adjust fan speeds based on temperature
285 * This structure encapsulates an entire Zone config. There are
286 * three zones (one for each temperature input) on the lm85
287 */
288struct lm85_zone {
289 s8 limit; /* Low temp limit */
290 u8 hyst; /* Low limit hysteresis. (0-15) */
291 u8 range; /* Temp range, encoded */
292 s8 critical; /* "All fans ON" temp limit */
293 u8 off_desired; /* Actual "off" temperature specified. Preserved
294 * to prevent "drift" as other autofan control
295 * values change.
296 */
297 u8 max_desired; /* Actual "max" temperature specified. Preserved
298 * to prevent "drift" as other autofan control
299 * values change.
300 */
301};
302
303struct lm85_autofan {
304 u8 config; /* Register value */
305 u8 freq; /* PWM frequency, encoded */
306 u8 min_pwm; /* Minimum PWM value, encoded */
307 u8 min_off; /* Min PWM or OFF below "limit", flag */
308};
309
Jean Delvareed6bafb2007-02-14 21:15:03 +0100310/* For each registered chip, we need to keep some data in memory.
311 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312struct lm85_data {
313 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700314 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 enum chips type;
316
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100317 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int valid; /* !=0 if following fields are valid */
319 unsigned long last_reading; /* In jiffies */
320 unsigned long last_config; /* In jiffies */
321
322 u8 in[8]; /* Register value */
323 u8 in_max[8]; /* Register value */
324 u8 in_min[8]; /* Register value */
325 s8 temp[3]; /* Register value */
326 s8 temp_min[3]; /* Register value */
327 s8 temp_max[3]; /* Register value */
328 s8 temp_offset[3]; /* Register value */
329 u16 fan[4]; /* Register value */
330 u16 fan_min[4]; /* Register value */
331 u8 pwm[3]; /* Register value */
332 u8 spinup_ctl; /* Register encoding, combined */
333 u8 tach_mode; /* Register encoding, combined */
334 u8 temp_ext[3]; /* Decoded values */
335 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 u8 fan_ppr; /* Register value */
337 u8 smooth[3]; /* Register encoding */
338 u8 vid; /* Register value */
339 u8 vrm; /* VRM version */
340 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
341 u8 oppoint[3]; /* Register value */
342 u16 tmin_ctl; /* Register value */
343 unsigned long therm_total; /* Cummulative therm count */
344 u8 therm_limit; /* Register value */
345 u32 alarms; /* Register encoding, combined */
346 struct lm85_autofan autofan[3];
347 struct lm85_zone zone[3];
348};
349
350static int lm85_attach_adapter(struct i2c_adapter *adapter);
351static int lm85_detect(struct i2c_adapter *adapter, int address,
352 int kind);
353static int lm85_detach_client(struct i2c_client *client);
354
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100355static int lm85_read_value(struct i2c_client *client, u8 reg);
356static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static struct lm85_data *lm85_update_device(struct device *dev);
358static void lm85_init_client(struct i2c_client *client);
359
360
361static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100362 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100363 .name = "lm85",
364 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .id = I2C_DRIVERID_LM85,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 .attach_adapter = lm85_attach_adapter,
367 .detach_client = lm85_detach_client,
368};
369
370
371/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200372static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
373 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jean Delvareb353a482007-07-05 20:36:12 +0200375 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct lm85_data *data = lm85_update_device(dev);
377 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
378}
Jean Delvareb353a482007-07-05 20:36:12 +0200379
380static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
381 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvareb353a482007-07-05 20:36:12 +0200383 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct lm85_data *data = lm85_update_device(dev);
385 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
386}
Jean Delvareb353a482007-07-05 20:36:12 +0200387
388static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Jean Delvareb353a482007-07-05 20:36:12 +0200391 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct i2c_client *client = to_i2c_client(dev);
393 struct lm85_data *data = i2c_get_clientdata(client);
394 long val = simple_strtol(buf, NULL, 10);
395
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 data->fan_min[nr] = FAN_TO_REG(val);
398 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return count;
401}
402
403#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200404static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
405 show_fan, NULL, offset - 1); \
406static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
407 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409show_fan_offset(1);
410show_fan_offset(2);
411show_fan_offset(3);
412show_fan_offset(4);
413
414/* vid, vrm, alarms */
415
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400416static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100419 int vid;
420
421 if (data->type == adt7463 && (data->vid & 0x80)) {
422 /* 6-pin VID (VRM 10) */
423 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
424 } else {
425 /* 5-pin VID (VRM 9) */
426 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
427 }
428
429 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
433
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400434static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct lm85_data *data = lm85_update_device(dev);
437 return sprintf(buf, "%ld\n", (long) data->vrm);
438}
439
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400440static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct i2c_client *client = to_i2c_client(dev);
443 struct lm85_data *data = i2c_get_clientdata(client);
444 u32 val;
445
446 val = simple_strtoul(buf, NULL, 10);
447 data->vrm = val;
448 return count;
449}
450
451static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
452
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400453static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200456 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
460
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200461static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
462 char *buf)
463{
464 int nr = to_sensor_dev_attr(attr)->index;
465 struct lm85_data *data = lm85_update_device(dev);
466 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
467}
468
469static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
470static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
471static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
472static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
473static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
474static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
475static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
476static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
477static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
478static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
479static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
480static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
481static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
482static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
483static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
484static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
485static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/* pwm */
488
Jean Delvareb353a482007-07-05 20:36:12 +0200489static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
490 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jean Delvareb353a482007-07-05 20:36:12 +0200492 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct lm85_data *data = lm85_update_device(dev);
494 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
495}
Jean Delvareb353a482007-07-05 20:36:12 +0200496
497static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jean Delvareb353a482007-07-05 20:36:12 +0200500 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct i2c_client *client = to_i2c_client(dev);
502 struct lm85_data *data = i2c_get_clientdata(client);
503 long val = simple_strtol(buf, NULL, 10);
504
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100505 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 data->pwm[nr] = PWM_TO_REG(val);
507 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return count;
510}
Jean Delvareb353a482007-07-05 20:36:12 +0200511
512static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
513 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jean Delvareb353a482007-07-05 20:36:12 +0200515 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct lm85_data *data = lm85_update_device(dev);
517 int pwm_zone;
518
519 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
520 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
521}
522
523#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200524static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
525 show_pwm, set_pwm, offset - 1); \
526static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
527 show_pwm_enable, NULL, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529show_pwm_reg(1);
530show_pwm_reg(2);
531show_pwm_reg(3);
532
533/* Voltages */
534
Jean Delvareb353a482007-07-05 20:36:12 +0200535static ssize_t show_in(struct device *dev, struct device_attribute *attr,
536 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Jean Delvareb353a482007-07-05 20:36:12 +0200538 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct lm85_data *data = lm85_update_device(dev);
540 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
541 data->in[nr],
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200542 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
Jean Delvareb353a482007-07-05 20:36:12 +0200544
545static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
546 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Jean Delvareb353a482007-07-05 20:36:12 +0200548 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct lm85_data *data = lm85_update_device(dev);
550 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
551}
Jean Delvareb353a482007-07-05 20:36:12 +0200552
553static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
554 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jean Delvareb353a482007-07-05 20:36:12 +0200556 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct i2c_client *client = to_i2c_client(dev);
558 struct lm85_data *data = i2c_get_clientdata(client);
559 long val = simple_strtol(buf, NULL, 10);
560
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100561 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 data->in_min[nr] = INS_TO_REG(nr, val);
563 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100564 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return count;
566}
Jean Delvareb353a482007-07-05 20:36:12 +0200567
568static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
569 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jean Delvareb353a482007-07-05 20:36:12 +0200571 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct lm85_data *data = lm85_update_device(dev);
573 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
574}
Jean Delvareb353a482007-07-05 20:36:12 +0200575
576static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
577 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jean Delvareb353a482007-07-05 20:36:12 +0200579 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct i2c_client *client = to_i2c_client(dev);
581 struct lm85_data *data = i2c_get_clientdata(client);
582 long val = simple_strtol(buf, NULL, 10);
583
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100584 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 data->in_max[nr] = INS_TO_REG(nr, val);
586 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return count;
589}
Jean Delvareb353a482007-07-05 20:36:12 +0200590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200592static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
593 show_in, NULL, offset); \
594static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
595 show_in_min, set_in_min, offset); \
596static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
597 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599show_in_reg(0);
600show_in_reg(1);
601show_in_reg(2);
602show_in_reg(3);
603show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200604show_in_reg(5);
605show_in_reg(6);
606show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608/* Temps */
609
Jean Delvareb353a482007-07-05 20:36:12 +0200610static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
611 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jean Delvareb353a482007-07-05 20:36:12 +0200613 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct lm85_data *data = lm85_update_device(dev);
615 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200616 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
Jean Delvareb353a482007-07-05 20:36:12 +0200618
619static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
620 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jean Delvareb353a482007-07-05 20:36:12 +0200622 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct lm85_data *data = lm85_update_device(dev);
624 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
625}
Jean Delvareb353a482007-07-05 20:36:12 +0200626
627static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
628 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jean Delvareb353a482007-07-05 20:36:12 +0200630 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct i2c_client *client = to_i2c_client(dev);
632 struct lm85_data *data = i2c_get_clientdata(client);
633 long val = simple_strtol(buf, NULL, 10);
634
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100635 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 data->temp_min[nr] = TEMP_TO_REG(val);
637 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100638 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return count;
640}
Jean Delvareb353a482007-07-05 20:36:12 +0200641
642static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
643 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jean Delvareb353a482007-07-05 20:36:12 +0200645 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct lm85_data *data = lm85_update_device(dev);
647 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
648}
Jean Delvareb353a482007-07-05 20:36:12 +0200649
650static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
651 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Jean Delvareb353a482007-07-05 20:36:12 +0200653 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 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
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100658 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 data->temp_max[nr] = TEMP_TO_REG(val);
660 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100661 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return count;
663}
Jean Delvareb353a482007-07-05 20:36:12 +0200664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200666static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
667 show_temp, NULL, offset - 1); \
668static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
669 show_temp_min, set_temp_min, offset - 1); \
670static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
671 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673show_temp_reg(1);
674show_temp_reg(2);
675show_temp_reg(3);
676
677
678/* Automatic PWM control */
679
Jean Delvareb353a482007-07-05 20:36:12 +0200680static ssize_t show_pwm_auto_channels(struct device *dev,
681 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Jean Delvareb353a482007-07-05 20:36:12 +0200683 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct lm85_data *data = lm85_update_device(dev);
685 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
686}
Jean Delvareb353a482007-07-05 20:36:12 +0200687
688static ssize_t set_pwm_auto_channels(struct device *dev,
689 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jean Delvareb353a482007-07-05 20:36:12 +0200691 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct i2c_client *client = to_i2c_client(dev);
693 struct lm85_data *data = i2c_get_clientdata(client);
694 long val = simple_strtol(buf, NULL, 10);
695
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100696 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
698 | ZONE_TO_REG(val) ;
699 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
700 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100701 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return count;
703}
Jean Delvareb353a482007-07-05 20:36:12 +0200704
705static ssize_t show_pwm_auto_pwm_min(struct device *dev,
706 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Jean Delvareb353a482007-07-05 20:36:12 +0200708 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct lm85_data *data = lm85_update_device(dev);
710 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
711}
Jean Delvareb353a482007-07-05 20:36:12 +0200712
713static ssize_t set_pwm_auto_pwm_min(struct device *dev,
714 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Jean Delvareb353a482007-07-05 20:36:12 +0200716 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct i2c_client *client = to_i2c_client(dev);
718 struct lm85_data *data = i2c_get_clientdata(client);
719 long val = simple_strtol(buf, NULL, 10);
720
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 data->autofan[nr].min_pwm = PWM_TO_REG(val);
723 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
724 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100725 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return count;
727}
Jean Delvareb353a482007-07-05 20:36:12 +0200728
729static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
730 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Jean Delvareb353a482007-07-05 20:36:12 +0200732 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct lm85_data *data = lm85_update_device(dev);
734 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
735}
Jean Delvareb353a482007-07-05 20:36:12 +0200736
737static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
738 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jean Delvareb353a482007-07-05 20:36:12 +0200740 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct i2c_client *client = to_i2c_client(dev);
742 struct lm85_data *data = i2c_get_clientdata(client);
743 long val = simple_strtol(buf, NULL, 10);
744
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100745 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 data->autofan[nr].min_off = val;
747 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
748 | data->syncpwm3
749 | (data->autofan[0].min_off ? 0x20 : 0)
750 | (data->autofan[1].min_off ? 0x40 : 0)
751 | (data->autofan[2].min_off ? 0x80 : 0)
752 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100753 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return count;
755}
Jean Delvareb353a482007-07-05 20:36:12 +0200756
757static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
758 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jean Delvareb353a482007-07-05 20:36:12 +0200760 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct lm85_data *data = lm85_update_device(dev);
762 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
763}
Jean Delvareb353a482007-07-05 20:36:12 +0200764
765static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
766 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Jean Delvareb353a482007-07-05 20:36:12 +0200768 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct i2c_client *client = to_i2c_client(dev);
770 struct lm85_data *data = i2c_get_clientdata(client);
771 long val = simple_strtol(buf, NULL, 10);
772
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100773 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 data->autofan[nr].freq = FREQ_TO_REG(val);
775 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
776 (data->zone[nr].range << 4)
777 | data->autofan[nr].freq
778 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100779 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return count;
781}
Jean Delvareb353a482007-07-05 20:36:12 +0200782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200784static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
785 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
786 set_pwm_auto_channels, offset - 1); \
787static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
788 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
789 set_pwm_auto_pwm_min, offset - 1); \
790static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
791 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
792 set_pwm_auto_pwm_minctl, offset - 1); \
793static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
794 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
795 set_pwm_auto_pwm_freq, offset - 1);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797pwm_auto(1);
798pwm_auto(2);
799pwm_auto(3);
800
801/* Temperature settings for automatic PWM control */
802
Jean Delvareb353a482007-07-05 20:36:12 +0200803static ssize_t show_temp_auto_temp_off(struct device *dev,
804 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Jean Delvareb353a482007-07-05 20:36:12 +0200806 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct lm85_data *data = lm85_update_device(dev);
808 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
809 HYST_FROM_REG(data->zone[nr].hyst));
810}
Jean Delvareb353a482007-07-05 20:36:12 +0200811
812static ssize_t set_temp_auto_temp_off(struct device *dev,
813 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jean Delvareb353a482007-07-05 20:36:12 +0200815 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct i2c_client *client = to_i2c_client(dev);
817 struct lm85_data *data = i2c_get_clientdata(client);
818 int min;
819 long val = simple_strtol(buf, NULL, 10);
820
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100821 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 min = TEMP_FROM_REG(data->zone[nr].limit);
823 data->zone[nr].off_desired = TEMP_TO_REG(val);
824 data->zone[nr].hyst = HYST_TO_REG(min - val);
825 if ( nr == 0 || nr == 1 ) {
826 lm85_write_value(client, LM85_REG_AFAN_HYST1,
827 (data->zone[0].hyst << 4)
828 | data->zone[1].hyst
829 );
830 } else {
831 lm85_write_value(client, LM85_REG_AFAN_HYST2,
832 (data->zone[2].hyst << 4)
833 );
834 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100835 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return count;
837}
Jean Delvareb353a482007-07-05 20:36:12 +0200838
839static ssize_t show_temp_auto_temp_min(struct device *dev,
840 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Jean Delvareb353a482007-07-05 20:36:12 +0200842 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct lm85_data *data = lm85_update_device(dev);
844 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
845}
Jean Delvareb353a482007-07-05 20:36:12 +0200846
847static ssize_t set_temp_auto_temp_min(struct device *dev,
848 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Jean Delvareb353a482007-07-05 20:36:12 +0200850 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct i2c_client *client = to_i2c_client(dev);
852 struct lm85_data *data = i2c_get_clientdata(client);
853 long val = simple_strtol(buf, NULL, 10);
854
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100855 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 data->zone[nr].limit = TEMP_TO_REG(val);
857 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
858 data->zone[nr].limit);
859
860/* Update temp_auto_max and temp_auto_range */
861 data->zone[nr].range = RANGE_TO_REG(
862 TEMP_FROM_REG(data->zone[nr].max_desired) -
863 TEMP_FROM_REG(data->zone[nr].limit));
864 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
865 ((data->zone[nr].range & 0x0f) << 4)
866 | (data->autofan[nr].freq & 0x07));
867
868/* Update temp_auto_hyst and temp_auto_off */
869 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
870 data->zone[nr].limit) - TEMP_FROM_REG(
871 data->zone[nr].off_desired));
872 if ( nr == 0 || nr == 1 ) {
873 lm85_write_value(client, LM85_REG_AFAN_HYST1,
874 (data->zone[0].hyst << 4)
875 | data->zone[1].hyst
876 );
877 } else {
878 lm85_write_value(client, LM85_REG_AFAN_HYST2,
879 (data->zone[2].hyst << 4)
880 );
881 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100882 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return count;
884}
Jean Delvareb353a482007-07-05 20:36:12 +0200885
886static ssize_t show_temp_auto_temp_max(struct device *dev,
887 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Jean Delvareb353a482007-07-05 20:36:12 +0200889 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct lm85_data *data = lm85_update_device(dev);
891 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
892 RANGE_FROM_REG(data->zone[nr].range));
893}
Jean Delvareb353a482007-07-05 20:36:12 +0200894
895static ssize_t set_temp_auto_temp_max(struct device *dev,
896 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jean Delvareb353a482007-07-05 20:36:12 +0200898 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct i2c_client *client = to_i2c_client(dev);
900 struct lm85_data *data = i2c_get_clientdata(client);
901 int min;
902 long val = simple_strtol(buf, NULL, 10);
903
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100904 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 min = TEMP_FROM_REG(data->zone[nr].limit);
906 data->zone[nr].max_desired = TEMP_TO_REG(val);
907 data->zone[nr].range = RANGE_TO_REG(
908 val - min);
909 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
910 ((data->zone[nr].range & 0x0f) << 4)
911 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100912 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return count;
914}
Jean Delvareb353a482007-07-05 20:36:12 +0200915
916static ssize_t show_temp_auto_temp_crit(struct device *dev,
917 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Jean Delvareb353a482007-07-05 20:36:12 +0200919 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct lm85_data *data = lm85_update_device(dev);
921 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
922}
Jean Delvareb353a482007-07-05 20:36:12 +0200923
924static ssize_t set_temp_auto_temp_crit(struct device *dev,
925 struct device_attribute *attr,const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Jean Delvareb353a482007-07-05 20:36:12 +0200927 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct i2c_client *client = to_i2c_client(dev);
929 struct lm85_data *data = i2c_get_clientdata(client);
930 long val = simple_strtol(buf, NULL, 10);
931
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100932 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 data->zone[nr].critical = TEMP_TO_REG(val);
934 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
935 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100936 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return count;
938}
Jean Delvareb353a482007-07-05 20:36:12 +0200939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200941static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
942 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
943 set_temp_auto_temp_off, offset - 1); \
944static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
945 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
946 set_temp_auto_temp_min, offset - 1); \
947static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
948 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
949 set_temp_auto_temp_max, offset - 1); \
950static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
951 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
952 set_temp_auto_temp_crit, offset - 1);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954temp_auto(1);
955temp_auto(2);
956temp_auto(3);
957
Ben Dooksd8d20612005-10-26 21:05:46 +0200958static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 if (!(adapter->class & I2C_CLASS_HWMON))
961 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200962 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200965static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200966 &sensor_dev_attr_fan1_input.dev_attr.attr,
967 &sensor_dev_attr_fan2_input.dev_attr.attr,
968 &sensor_dev_attr_fan3_input.dev_attr.attr,
969 &sensor_dev_attr_fan4_input.dev_attr.attr,
970 &sensor_dev_attr_fan1_min.dev_attr.attr,
971 &sensor_dev_attr_fan2_min.dev_attr.attr,
972 &sensor_dev_attr_fan3_min.dev_attr.attr,
973 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200974 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
975 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
976 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
977 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200978
979 &sensor_dev_attr_pwm1.dev_attr.attr,
980 &sensor_dev_attr_pwm2.dev_attr.attr,
981 &sensor_dev_attr_pwm3.dev_attr.attr,
982 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
983 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
984 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
985
986 &sensor_dev_attr_in0_input.dev_attr.attr,
987 &sensor_dev_attr_in1_input.dev_attr.attr,
988 &sensor_dev_attr_in2_input.dev_attr.attr,
989 &sensor_dev_attr_in3_input.dev_attr.attr,
990 &sensor_dev_attr_in0_min.dev_attr.attr,
991 &sensor_dev_attr_in1_min.dev_attr.attr,
992 &sensor_dev_attr_in2_min.dev_attr.attr,
993 &sensor_dev_attr_in3_min.dev_attr.attr,
994 &sensor_dev_attr_in0_max.dev_attr.attr,
995 &sensor_dev_attr_in1_max.dev_attr.attr,
996 &sensor_dev_attr_in2_max.dev_attr.attr,
997 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200998 &sensor_dev_attr_in0_alarm.dev_attr.attr,
999 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1000 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1001 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001002
1003 &sensor_dev_attr_temp1_input.dev_attr.attr,
1004 &sensor_dev_attr_temp2_input.dev_attr.attr,
1005 &sensor_dev_attr_temp3_input.dev_attr.attr,
1006 &sensor_dev_attr_temp1_min.dev_attr.attr,
1007 &sensor_dev_attr_temp2_min.dev_attr.attr,
1008 &sensor_dev_attr_temp3_min.dev_attr.attr,
1009 &sensor_dev_attr_temp1_max.dev_attr.attr,
1010 &sensor_dev_attr_temp2_max.dev_attr.attr,
1011 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001012 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1013 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1014 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1015 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1016 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001017
1018 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1019 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1020 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1021 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1022 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1023 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1024 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1025 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1026 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1027 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1028 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1029 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1030
1031 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1032 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1033 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1034 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1035 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1036 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1037 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1038 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1039 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1040 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1041 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1042 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1043
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001044 &dev_attr_vrm.attr,
1045 &dev_attr_cpu0_vid.attr,
1046 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001047 NULL
1048};
1049
1050static const struct attribute_group lm85_group = {
1051 .attrs = lm85_attributes,
1052};
1053
Jean Delvare6b9aad22007-07-05 20:37:21 +02001054static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001055 &sensor_dev_attr_in4_input.dev_attr.attr,
1056 &sensor_dev_attr_in4_min.dev_attr.attr,
1057 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001058 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001059 NULL
1060};
1061
Jean Delvare6b9aad22007-07-05 20:37:21 +02001062static const struct attribute_group lm85_group_in4 = {
1063 .attrs = lm85_attributes_in4,
1064};
1065
1066static struct attribute *lm85_attributes_in567[] = {
1067 &sensor_dev_attr_in5_input.dev_attr.attr,
1068 &sensor_dev_attr_in6_input.dev_attr.attr,
1069 &sensor_dev_attr_in7_input.dev_attr.attr,
1070 &sensor_dev_attr_in5_min.dev_attr.attr,
1071 &sensor_dev_attr_in6_min.dev_attr.attr,
1072 &sensor_dev_attr_in7_min.dev_attr.attr,
1073 &sensor_dev_attr_in5_max.dev_attr.attr,
1074 &sensor_dev_attr_in6_max.dev_attr.attr,
1075 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001076 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1077 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1078 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001079 NULL
1080};
1081
1082static const struct attribute_group lm85_group_in567 = {
1083 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001084};
1085
Ben Dooksd8d20612005-10-26 21:05:46 +02001086static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int kind)
1088{
1089 int company, verstep ;
1090 struct i2c_client *new_client = NULL;
1091 struct lm85_data *data;
1092 int err = 0;
1093 const char *type_name = "";
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!i2c_check_functionality(adapter,
1096 I2C_FUNC_SMBUS_BYTE_DATA)) {
1097 /* We need to be able to do byte I/O */
1098 goto ERROR0 ;
1099 };
1100
1101 /* OK. For now, we presume we have a valid client. We now create the
1102 client structure, even though we cannot fill it completely yet.
1103 But it allows us to access lm85_{read,write}_value. */
1104
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001105 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 err = -ENOMEM;
1107 goto ERROR0;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 new_client = &data->client;
1111 i2c_set_clientdata(new_client, data);
1112 new_client->addr = address;
1113 new_client->adapter = adapter;
1114 new_client->driver = &lm85_driver;
1115 new_client->flags = 0;
1116
1117 /* Now, we do the remaining detection. */
1118
1119 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1120 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1121
1122 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1123 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1124 i2c_adapter_id(new_client->adapter), new_client->addr,
1125 company, verstep);
1126
1127 /* If auto-detecting, Determine the chip type. */
1128 if (kind <= 0) {
1129 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1130 i2c_adapter_id(adapter), address );
1131 if( company == LM85_COMPANY_NATIONAL
1132 && verstep == LM85_VERSTEP_LM85C ) {
1133 kind = lm85c ;
1134 } else if( company == LM85_COMPANY_NATIONAL
1135 && verstep == LM85_VERSTEP_LM85B ) {
1136 kind = lm85b ;
1137 } else if( company == LM85_COMPANY_NATIONAL
1138 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1139 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1140 " Defaulting to LM85.\n", verstep);
1141 kind = any_chip ;
1142 } else if( company == LM85_COMPANY_ANALOG_DEV
1143 && verstep == LM85_VERSTEP_ADM1027 ) {
1144 kind = adm1027 ;
1145 } else if( company == LM85_COMPANY_ANALOG_DEV
1146 && (verstep == LM85_VERSTEP_ADT7463
1147 || verstep == LM85_VERSTEP_ADT7463C) ) {
1148 kind = adt7463 ;
1149 } else if( company == LM85_COMPANY_ANALOG_DEV
1150 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1151 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1152 " Defaulting to Generic LM85.\n", verstep );
1153 kind = any_chip ;
1154 } else if( company == LM85_COMPANY_SMSC
1155 && (verstep == LM85_VERSTEP_EMC6D100_A0
1156 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1157 /* Unfortunately, we can't tell a '100 from a '101
1158 * from the registers. Since a '101 is a '100
1159 * in a package with fewer pins and therefore no
1160 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1161 * inputs read 0, then it's a '101.
1162 */
1163 kind = emc6d100 ;
1164 } else if( company == LM85_COMPANY_SMSC
1165 && verstep == LM85_VERSTEP_EMC6D102) {
1166 kind = emc6d102 ;
1167 } else if( company == LM85_COMPANY_SMSC
1168 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1169 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1170 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1171 " Defaulting to Generic LM85.\n", verstep );
1172 kind = any_chip ;
1173 } else if( kind == any_chip
1174 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1175 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1176 /* Leave kind as "any_chip" */
1177 } else {
1178 dev_dbg(&adapter->dev, "Autodetection failed\n");
1179 /* Not an LM85 ... */
1180 if( kind == any_chip ) { /* User used force=x,y */
1181 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1182 " found at %d,0x%02x. Try force_lm85c.\n",
1183 i2c_adapter_id(adapter), address );
1184 }
1185 err = 0 ;
1186 goto ERROR1;
1187 }
1188 }
1189
1190 /* Fill in the chip specific driver values */
1191 if ( kind == any_chip ) {
1192 type_name = "lm85";
1193 } else if ( kind == lm85b ) {
1194 type_name = "lm85b";
1195 } else if ( kind == lm85c ) {
1196 type_name = "lm85c";
1197 } else if ( kind == adm1027 ) {
1198 type_name = "adm1027";
1199 } else if ( kind == adt7463 ) {
1200 type_name = "adt7463";
1201 } else if ( kind == emc6d100){
1202 type_name = "emc6d100";
1203 } else if ( kind == emc6d102 ) {
1204 type_name = "emc6d102";
1205 }
1206 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1207
1208 /* Fill in the remaining client fields */
1209 data->type = kind;
1210 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001211 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* Tell the I2C layer a new client has arrived */
1214 if ((err = i2c_attach_client(new_client)))
1215 goto ERROR1;
1216
1217 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001218 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* Initialize the LM85 chip */
1221 lm85_init_client(new_client);
1222
1223 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001224 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001225 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Jean Delvare9c516ef2005-11-26 20:07:54 +01001227 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1228 as a sixth digital VID input rather than an analog input. */
1229 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001230 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001231 if ((err = sysfs_create_group(&new_client->dev.kobj,
1232 &lm85_group_in4)))
1233 goto ERROR3;
1234
1235 /* The EMC6D100 has 3 additional voltage inputs */
1236 if (kind == emc6d100)
1237 if ((err = sysfs_create_group(&new_client->dev.kobj,
1238 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001239 goto ERROR3;
1240
Tony Jones1beeffe2007-08-20 13:46:20 -07001241 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1242 if (IS_ERR(data->hwmon_dev)) {
1243 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001244 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001245 }
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return 0;
1248
1249 /* Error out and cleanup code */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001250 ERROR3:
1251 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001252 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1253 if (kind == emc6d100)
1254 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001255 ERROR2:
1256 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ERROR1:
1258 kfree(data);
1259 ERROR0:
1260 return err;
1261}
1262
Ben Dooksd8d20612005-10-26 21:05:46 +02001263static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001265 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001266 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001267 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001268 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1269 if (data->type == emc6d100)
1270 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001272 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 0;
1274}
1275
1276
Ben Dooksd8d20612005-10-26 21:05:46 +02001277static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 int res;
1280
1281 /* What size location is it? */
1282 switch( reg ) {
1283 case LM85_REG_FAN(0) : /* Read WORD data */
1284 case LM85_REG_FAN(1) :
1285 case LM85_REG_FAN(2) :
1286 case LM85_REG_FAN(3) :
1287 case LM85_REG_FAN_MIN(0) :
1288 case LM85_REG_FAN_MIN(1) :
1289 case LM85_REG_FAN_MIN(2) :
1290 case LM85_REG_FAN_MIN(3) :
1291 case LM85_REG_ALARM1 : /* Read both bytes at once */
1292 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1293 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1294 break ;
1295 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1296 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1297 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1298 break ;
1299 default: /* Read BYTE data */
1300 res = i2c_smbus_read_byte_data(client, reg);
1301 break ;
1302 }
1303
1304 return res ;
1305}
1306
Ben Dooksd8d20612005-10-26 21:05:46 +02001307static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int res ;
1310
1311 switch( reg ) {
1312 case LM85_REG_FAN(0) : /* Write WORD data */
1313 case LM85_REG_FAN(1) :
1314 case LM85_REG_FAN(2) :
1315 case LM85_REG_FAN(3) :
1316 case LM85_REG_FAN_MIN(0) :
1317 case LM85_REG_FAN_MIN(1) :
1318 case LM85_REG_FAN_MIN(2) :
1319 case LM85_REG_FAN_MIN(3) :
1320 /* NOTE: ALARM is read only, so not included here */
1321 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1322 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1323 break ;
1324 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1325 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1326 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1327 break ;
1328 default: /* Write BYTE data */
1329 res = i2c_smbus_write_byte_data(client, reg, value);
1330 break ;
1331 }
1332
1333 return res ;
1334}
1335
Ben Dooksd8d20612005-10-26 21:05:46 +02001336static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 int value;
1339 struct lm85_data *data = i2c_get_clientdata(client);
1340
1341 dev_dbg(&client->dev, "Initializing device\n");
1342
1343 /* Warn if part was not "READY" */
1344 value = lm85_read_value(client, LM85_REG_CONFIG);
1345 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1346 if( value & 0x02 ) {
1347 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1348 i2c_adapter_id(client->adapter), client->addr );
1349 };
1350 if( ! (value & 0x04) ) {
1351 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1352 i2c_adapter_id(client->adapter), client->addr );
1353 };
1354 if( value & 0x10
1355 && ( data->type == adm1027
1356 || data->type == adt7463 ) ) {
1357 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1358 "Please report this to the lm85 maintainer.\n",
1359 i2c_adapter_id(client->adapter), client->addr );
1360 };
1361
1362 /* WE INTENTIONALLY make no changes to the limits,
1363 * offsets, pwms, fans and zones. If they were
1364 * configured, we don't want to mess with them.
1365 * If they weren't, the default is 100% PWM, no
1366 * control and will suffice until 'sensors -s'
1367 * can be run by the user.
1368 */
1369
1370 /* Start monitoring */
1371 value = lm85_read_value(client, LM85_REG_CONFIG);
1372 /* Try to clear LOCK, Set START, save everything else */
1373 value = (value & ~ 0x02) | 0x01 ;
1374 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1375 lm85_write_value(client, LM85_REG_CONFIG, value);
1376}
1377
1378static struct lm85_data *lm85_update_device(struct device *dev)
1379{
1380 struct i2c_client *client = to_i2c_client(dev);
1381 struct lm85_data *data = i2c_get_clientdata(client);
1382 int i;
1383
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001384 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if ( !data->valid ||
1387 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1388 /* Things that change quickly */
1389 dev_dbg(&client->dev, "Reading sensor values\n");
1390
1391 /* Have to read extended bits first to "freeze" the
1392 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001393 * There are 2 additional resolution bits per channel and we
1394 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
1396 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1397 int ext1 = lm85_read_value(client,
1398 ADM1027_REG_EXTEND_ADC1);
1399 int ext2 = lm85_read_value(client,
1400 ADM1027_REG_EXTEND_ADC2);
1401 int val = (ext1 << 8) + ext2;
1402
1403 for(i = 0; i <= 4; i++)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001404 data->in_ext[i] = ((val>>(i * 2))&0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 for(i = 0; i <= 2; i++)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001407 data->temp_ext[i] = (val>>((i + 4) * 2))&0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Jean Delvare9c516ef2005-11-26 20:07:54 +01001410 data->vid = lm85_read_value(client, LM85_REG_VID);
1411
1412 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 data->in[i] =
1414 lm85_read_value(client, LM85_REG_IN(i));
1415 }
1416
Jean Delvare9c516ef2005-11-26 20:07:54 +01001417 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1418 data->in[4] = lm85_read_value(client,
1419 LM85_REG_IN(4));
1420 }
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 for (i = 0; i <= 3; ++i) {
1423 data->fan[i] =
1424 lm85_read_value(client, LM85_REG_FAN(i));
1425 }
1426
1427 for (i = 0; i <= 2; ++i) {
1428 data->temp[i] =
1429 lm85_read_value(client, LM85_REG_TEMP(i));
1430 }
1431
1432 for (i = 0; i <= 2; ++i) {
1433 data->pwm[i] =
1434 lm85_read_value(client, LM85_REG_PWM(i));
1435 }
1436
1437 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1438
1439 if ( data->type == adt7463 ) {
1440 if( data->therm_total < ULONG_MAX - 256 ) {
1441 data->therm_total +=
1442 lm85_read_value(client, ADT7463_REG_THERM );
1443 }
1444 } else if ( data->type == emc6d100 ) {
1445 /* Three more voltage sensors */
1446 for (i = 5; i <= 7; ++i) {
1447 data->in[i] =
1448 lm85_read_value(client, EMC6D100_REG_IN(i));
1449 }
1450 /* More alarm bits */
1451 data->alarms |=
1452 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1453 } else if (data->type == emc6d102 ) {
1454 /* Have to read LSB bits after the MSB ones because
1455 the reading of the MSB bits has frozen the
1456 LSBs (backward from the ADM1027).
1457 */
1458 int ext1 = lm85_read_value(client,
1459 EMC6D102_REG_EXTEND_ADC1);
1460 int ext2 = lm85_read_value(client,
1461 EMC6D102_REG_EXTEND_ADC2);
1462 int ext3 = lm85_read_value(client,
1463 EMC6D102_REG_EXTEND_ADC3);
1464 int ext4 = lm85_read_value(client,
1465 EMC6D102_REG_EXTEND_ADC4);
1466 data->in_ext[0] = ext3 & 0x0f;
1467 data->in_ext[1] = ext4 & 0x0f;
1468 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1469 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1470 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1471
1472 data->temp_ext[0] = ext1 & 0x0f;
1473 data->temp_ext[1] = ext2 & 0x0f;
1474 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1475 }
1476
1477 data->last_reading = jiffies ;
1478 }; /* last_reading */
1479
1480 if ( !data->valid ||
1481 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1482 /* Things that don't change often */
1483 dev_dbg(&client->dev, "Reading config values\n");
1484
Jean Delvare9c516ef2005-11-26 20:07:54 +01001485 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 data->in_min[i] =
1487 lm85_read_value(client, LM85_REG_IN_MIN(i));
1488 data->in_max[i] =
1489 lm85_read_value(client, LM85_REG_IN_MAX(i));
1490 }
1491
Jean Delvare9c516ef2005-11-26 20:07:54 +01001492 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1493 data->in_min[4] = lm85_read_value(client,
1494 LM85_REG_IN_MIN(4));
1495 data->in_max[4] = lm85_read_value(client,
1496 LM85_REG_IN_MAX(4));
1497 }
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if ( data->type == emc6d100 ) {
1500 for (i = 5; i <= 7; ++i) {
1501 data->in_min[i] =
1502 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1503 data->in_max[i] =
1504 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1505 }
1506 }
1507
1508 for (i = 0; i <= 3; ++i) {
1509 data->fan_min[i] =
1510 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1511 }
1512
1513 for (i = 0; i <= 2; ++i) {
1514 data->temp_min[i] =
1515 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1516 data->temp_max[i] =
1517 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1518 }
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 for (i = 0; i <= 2; ++i) {
1521 int val ;
1522 data->autofan[i].config =
1523 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1524 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1525 data->autofan[i].freq = val & 0x07 ;
1526 data->zone[i].range = (val >> 4) & 0x0f ;
1527 data->autofan[i].min_pwm =
1528 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1529 data->zone[i].limit =
1530 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1531 data->zone[i].critical =
1532 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1533 }
1534
1535 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1536 data->smooth[0] = i & 0x0f ;
1537 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1538 data->autofan[0].min_off = (i & 0x20) != 0 ;
1539 data->autofan[1].min_off = (i & 0x40) != 0 ;
1540 data->autofan[2].min_off = (i & 0x80) != 0 ;
1541 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1542 data->smooth[1] = (i>>4) & 0x0f ;
1543 data->smooth[2] = i & 0x0f ;
1544
1545 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1546 data->zone[0].hyst = (i>>4) & 0x0f ;
1547 data->zone[1].hyst = i & 0x0f ;
1548
1549 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1550 data->zone[2].hyst = (i>>4) & 0x0f ;
1551
1552 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1553 data->tach_mode = lm85_read_value(client,
1554 LM85_REG_TACH_MODE );
1555 data->spinup_ctl = lm85_read_value(client,
1556 LM85_REG_SPINUP_CTL );
1557 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1558 if ( data->type == adt7463 ) {
1559 for (i = 0; i <= 2; ++i) {
1560 data->oppoint[i] = lm85_read_value(client,
1561 ADT7463_REG_OPPOINT(i) );
1562 }
1563 data->tmin_ctl = lm85_read_value(client,
1564 ADT7463_REG_TMIN_CTL1 );
1565 data->therm_limit = lm85_read_value(client,
1566 ADT7463_REG_THERM_LIMIT );
1567 }
1568 for (i = 0; i <= 2; ++i) {
1569 data->temp_offset[i] = lm85_read_value(client,
1570 ADM1027_REG_TEMP_OFFSET(i) );
1571 }
1572 data->tach_mode = lm85_read_value(client,
1573 ADM1027_REG_CONFIG3 );
1574 data->fan_ppr = lm85_read_value(client,
1575 ADM1027_REG_FAN_PPR );
1576 }
1577
1578 data->last_config = jiffies;
1579 }; /* last_config */
1580
1581 data->valid = 1;
1582
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001583 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 return data;
1586}
1587
1588
1589static int __init sm_lm85_init(void)
1590{
1591 return i2c_add_driver(&lm85_driver);
1592}
1593
1594static void __exit sm_lm85_exit(void)
1595{
1596 i2c_del_driver(&lm85_driver);
1597}
1598
1599/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1600 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1601 * post 2.7.0 CVS changes.
1602 */
1603MODULE_LICENSE("GPL");
1604MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1605MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1606
1607module_init(sm_lm85_init);
1608module_exit(sm_lm85_exit);