blob: 3b70815ab982d1d39688fb55cdd9adb51a635e17 [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
142#define INSEXT_FROM_REG(n,val,ext,scale) \
143 SCALE((val)*(scale) + (ext),192*(scale),lm85_scaling[n])
144
145#define INS_FROM_REG(n,val) INSEXT_FROM_REG(n,val,0,1)
146
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)
154#define TEMPEXT_FROM_REG(val,ext,scale) \
155 SCALE((val)*scale + (ext),scale,1000)
156#define TEMP_FROM_REG(val) \
157 TEMPEXT_FROM_REG(val,0,1)
158
159#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
160#define PWM_FROM_REG(val) (val)
161
162
163/* ZONEs have the following parameters:
164 * Limit (low) temp, 1. degC
165 * Hysteresis (below limit), 1. degC (0-15)
166 * Range of speed control, .1 degC (2-80)
167 * Critical (high) temp, 1. degC
168 *
169 * FAN PWMs have the following parameters:
170 * Reference Zone, 1, 2, 3, etc.
171 * Spinup time, .05 sec
172 * PWM value at limit/low temp, 1 count
173 * PWM Frequency, 1. Hz
174 * PWM is Min or OFF below limit, flag
175 * Invert PWM output, flag
176 *
177 * Some chips filter the temp, others the fan.
178 * Filter constant (or disabled) .1 seconds
179 */
180
181/* These are the zone temperature range encodings in .001 degree C */
182static int lm85_range_map[] = {
183 2000, 2500, 3300, 4000, 5000, 6600,
184 8000, 10000, 13300, 16000, 20000, 26600,
185 32000, 40000, 53300, 80000
186 };
187static int RANGE_TO_REG( int range )
188{
189 int i;
190
191 if ( range < lm85_range_map[0] ) {
192 return 0 ;
193 } else if ( range > lm85_range_map[15] ) {
194 return 15 ;
195 } else { /* find closest match */
196 for ( i = 14 ; i >= 0 ; --i ) {
197 if ( range > lm85_range_map[i] ) { /* range bracketed */
198 if ((lm85_range_map[i+1] - range) <
199 (range - lm85_range_map[i])) {
200 i++;
201 break;
202 }
203 break;
204 }
205 }
206 }
207 return( i & 0x0f );
208}
209#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
210
211/* These are the Acoustic Enhancement, or Temperature smoothing encodings
212 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
213 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
214 * is ignored, or set to 0.
215 */
216/* These are the PWM frequency encodings */
217static int lm85_freq_map[] = { /* .1 Hz */
218 100, 150, 230, 300, 380, 470, 620, 940
219 };
220static int FREQ_TO_REG( int freq )
221{
222 int i;
223
224 if( freq >= lm85_freq_map[7] ) { return 7 ; }
225 for( i = 0 ; i < 7 ; ++i )
226 if( freq <= lm85_freq_map[i] )
227 break ;
228 return( i & 0x07 );
229}
230#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
231
232/* Since we can't use strings, I'm abusing these numbers
233 * to stand in for the following meanings:
234 * 1 -- PWM responds to Zone 1
235 * 2 -- PWM responds to Zone 2
236 * 3 -- PWM responds to Zone 3
237 * 23 -- PWM responds to the higher temp of Zone 2 or 3
238 * 123 -- PWM responds to highest of Zone 1, 2, or 3
239 * 0 -- PWM is always at 0% (ie, off)
240 * -1 -- PWM is always at 100%
241 * -2 -- PWM responds to manual control
242 */
243
244static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
245#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
246
247static int ZONE_TO_REG( int zone )
248{
249 int i;
250
251 for( i = 0 ; i <= 7 ; ++i )
252 if( zone == lm85_zone_map[i] )
253 break ;
254 if( i > 7 ) /* Not found. */
255 i = 3; /* Always 100% */
256 return( (i & 0x07)<<5 );
257}
258
259#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
260#define HYST_FROM_REG(val) ((val)*1000)
261
262#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
263#define OFFSET_FROM_REG(val) ((val)*25)
264
265#define PPR_MASK(fan) (0x03<<(fan *2))
266#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
267#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* Chip sampling rates
270 *
271 * Some sensors are not updated more frequently than once per second
272 * so it doesn't make sense to read them more often than that.
273 * We cache the results and return the saved data if the driver
274 * is called again before a second has elapsed.
275 *
276 * Also, there is significant configuration data for this chip
277 * given the automatic PWM fan control that is possible. There
278 * are about 47 bytes of config data to only 22 bytes of actual
279 * readings. So, we keep the config data up to date in the cache
280 * when it is written and only sample it once every 1 *minute*
281 */
282#define LM85_DATA_INTERVAL (HZ + HZ / 2)
283#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* LM85 can automatically adjust fan speeds based on temperature
286 * This structure encapsulates an entire Zone config. There are
287 * three zones (one for each temperature input) on the lm85
288 */
289struct lm85_zone {
290 s8 limit; /* Low temp limit */
291 u8 hyst; /* Low limit hysteresis. (0-15) */
292 u8 range; /* Temp range, encoded */
293 s8 critical; /* "All fans ON" temp limit */
294 u8 off_desired; /* Actual "off" temperature specified. Preserved
295 * to prevent "drift" as other autofan control
296 * values change.
297 */
298 u8 max_desired; /* Actual "max" temperature specified. Preserved
299 * to prevent "drift" as other autofan control
300 * values change.
301 */
302};
303
304struct lm85_autofan {
305 u8 config; /* Register value */
306 u8 freq; /* PWM frequency, encoded */
307 u8 min_pwm; /* Minimum PWM value, encoded */
308 u8 min_off; /* Min PWM or OFF below "limit", flag */
309};
310
Jean Delvareed6bafb2007-02-14 21:15:03 +0100311/* For each registered chip, we need to keep some data in memory.
312 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313struct lm85_data {
314 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700315 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 enum chips type;
317
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100318 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int valid; /* !=0 if following fields are valid */
320 unsigned long last_reading; /* In jiffies */
321 unsigned long last_config; /* In jiffies */
322
323 u8 in[8]; /* Register value */
324 u8 in_max[8]; /* Register value */
325 u8 in_min[8]; /* Register value */
326 s8 temp[3]; /* Register value */
327 s8 temp_min[3]; /* Register value */
328 s8 temp_max[3]; /* Register value */
329 s8 temp_offset[3]; /* Register value */
330 u16 fan[4]; /* Register value */
331 u16 fan_min[4]; /* Register value */
332 u8 pwm[3]; /* Register value */
333 u8 spinup_ctl; /* Register encoding, combined */
334 u8 tach_mode; /* Register encoding, combined */
335 u8 temp_ext[3]; /* Decoded values */
336 u8 in_ext[8]; /* Decoded values */
337 u8 adc_scale; /* ADC Extended bits scaling factor */
338 u8 fan_ppr; /* Register value */
339 u8 smooth[3]; /* Register encoding */
340 u8 vid; /* Register value */
341 u8 vrm; /* VRM version */
342 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
343 u8 oppoint[3]; /* Register value */
344 u16 tmin_ctl; /* Register value */
345 unsigned long therm_total; /* Cummulative therm count */
346 u8 therm_limit; /* Register value */
347 u32 alarms; /* Register encoding, combined */
348 struct lm85_autofan autofan[3];
349 struct lm85_zone zone[3];
350};
351
352static int lm85_attach_adapter(struct i2c_adapter *adapter);
353static int lm85_detect(struct i2c_adapter *adapter, int address,
354 int kind);
355static int lm85_detach_client(struct i2c_client *client);
356
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100357static int lm85_read_value(struct i2c_client *client, u8 reg);
358static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static struct lm85_data *lm85_update_device(struct device *dev);
360static void lm85_init_client(struct i2c_client *client);
361
362
363static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100364 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100365 .name = "lm85",
366 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 .id = I2C_DRIVERID_LM85,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 .attach_adapter = lm85_attach_adapter,
369 .detach_client = lm85_detach_client,
370};
371
372
373/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200374static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
375 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Jean Delvareb353a482007-07-05 20:36:12 +0200377 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct lm85_data *data = lm85_update_device(dev);
379 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
380}
Jean Delvareb353a482007-07-05 20:36:12 +0200381
382static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
383 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jean Delvareb353a482007-07-05 20:36:12 +0200385 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct lm85_data *data = lm85_update_device(dev);
387 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
388}
Jean Delvareb353a482007-07-05 20:36:12 +0200389
390static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
391 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jean Delvareb353a482007-07-05 20:36:12 +0200393 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct i2c_client *client = to_i2c_client(dev);
395 struct lm85_data *data = i2c_get_clientdata(client);
396 long val = simple_strtol(buf, NULL, 10);
397
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100398 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 data->fan_min[nr] = FAN_TO_REG(val);
400 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return count;
403}
404
405#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200406static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
407 show_fan, NULL, offset - 1); \
408static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
409 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411show_fan_offset(1);
412show_fan_offset(2);
413show_fan_offset(3);
414show_fan_offset(4);
415
416/* vid, vrm, alarms */
417
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400418static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100421 int vid;
422
423 if (data->type == adt7463 && (data->vid & 0x80)) {
424 /* 6-pin VID (VRM 10) */
425 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
426 } else {
427 /* 5-pin VID (VRM 9) */
428 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
429 }
430
431 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
435
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400436static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct lm85_data *data = lm85_update_device(dev);
439 return sprintf(buf, "%ld\n", (long) data->vrm);
440}
441
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400442static 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 -0700443{
444 struct i2c_client *client = to_i2c_client(dev);
445 struct lm85_data *data = i2c_get_clientdata(client);
446 u32 val;
447
448 val = simple_strtoul(buf, NULL, 10);
449 data->vrm = val;
450 return count;
451}
452
453static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
454
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400455static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200458 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
462
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200463static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
464 char *buf)
465{
466 int nr = to_sensor_dev_attr(attr)->index;
467 struct lm85_data *data = lm85_update_device(dev);
468 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
469}
470
471static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
472static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
473static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
474static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
475static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
476static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
477static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
478static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
479static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
480static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
481static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
482static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
483static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
484static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
485static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
486static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
487static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/* pwm */
490
Jean Delvareb353a482007-07-05 20:36:12 +0200491static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
492 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Jean Delvareb353a482007-07-05 20:36:12 +0200494 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct lm85_data *data = lm85_update_device(dev);
496 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
497}
Jean Delvareb353a482007-07-05 20:36:12 +0200498
499static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
500 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Jean Delvareb353a482007-07-05 20:36:12 +0200502 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct i2c_client *client = to_i2c_client(dev);
504 struct lm85_data *data = i2c_get_clientdata(client);
505 long val = simple_strtol(buf, NULL, 10);
506
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100507 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 data->pwm[nr] = PWM_TO_REG(val);
509 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return count;
512}
Jean Delvareb353a482007-07-05 20:36:12 +0200513
514static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
515 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jean Delvareb353a482007-07-05 20:36:12 +0200517 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct lm85_data *data = lm85_update_device(dev);
519 int pwm_zone;
520
521 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
522 return sprintf(buf,"%d\n", (pwm_zone != 0 && pwm_zone != -1) );
523}
524
525#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200526static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
527 show_pwm, set_pwm, offset - 1); \
528static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO, \
529 show_pwm_enable, NULL, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531show_pwm_reg(1);
532show_pwm_reg(2);
533show_pwm_reg(3);
534
535/* Voltages */
536
Jean Delvareb353a482007-07-05 20:36:12 +0200537static ssize_t show_in(struct device *dev, struct device_attribute *attr,
538 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Jean Delvareb353a482007-07-05 20:36:12 +0200540 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 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}
Jean Delvareb353a482007-07-05 20:36:12 +0200547
548static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
549 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jean Delvareb353a482007-07-05 20:36:12 +0200551 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct lm85_data *data = lm85_update_device(dev);
553 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
554}
Jean Delvareb353a482007-07-05 20:36:12 +0200555
556static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
557 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jean Delvareb353a482007-07-05 20:36:12 +0200559 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct i2c_client *client = to_i2c_client(dev);
561 struct lm85_data *data = i2c_get_clientdata(client);
562 long val = simple_strtol(buf, NULL, 10);
563
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100564 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 data->in_min[nr] = INS_TO_REG(nr, val);
566 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100567 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return count;
569}
Jean Delvareb353a482007-07-05 20:36:12 +0200570
571static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
572 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Jean Delvareb353a482007-07-05 20:36:12 +0200574 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct lm85_data *data = lm85_update_device(dev);
576 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
577}
Jean Delvareb353a482007-07-05 20:36:12 +0200578
579static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Jean Delvareb353a482007-07-05 20:36:12 +0200582 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct i2c_client *client = to_i2c_client(dev);
584 struct lm85_data *data = i2c_get_clientdata(client);
585 long val = simple_strtol(buf, NULL, 10);
586
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 data->in_max[nr] = INS_TO_REG(nr, val);
589 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100590 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return count;
592}
Jean Delvareb353a482007-07-05 20:36:12 +0200593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200595static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
596 show_in, NULL, offset); \
597static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
598 show_in_min, set_in_min, offset); \
599static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
600 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602show_in_reg(0);
603show_in_reg(1);
604show_in_reg(2);
605show_in_reg(3);
606show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200607show_in_reg(5);
608show_in_reg(6);
609show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/* Temps */
612
Jean Delvareb353a482007-07-05 20:36:12 +0200613static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
614 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Jean Delvareb353a482007-07-05 20:36:12 +0200616 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct lm85_data *data = lm85_update_device(dev);
618 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
619 data->temp_ext[nr],
620 data->adc_scale) );
621}
Jean Delvareb353a482007-07-05 20:36:12 +0200622
623static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
624 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Jean Delvareb353a482007-07-05 20:36:12 +0200626 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct lm85_data *data = lm85_update_device(dev);
628 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
629}
Jean Delvareb353a482007-07-05 20:36:12 +0200630
631static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
632 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jean Delvareb353a482007-07-05 20:36:12 +0200634 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct i2c_client *client = to_i2c_client(dev);
636 struct lm85_data *data = i2c_get_clientdata(client);
637 long val = simple_strtol(buf, NULL, 10);
638
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100639 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 data->temp_min[nr] = TEMP_TO_REG(val);
641 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100642 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return count;
644}
Jean Delvareb353a482007-07-05 20:36:12 +0200645
646static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
647 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Jean Delvareb353a482007-07-05 20:36:12 +0200649 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct lm85_data *data = lm85_update_device(dev);
651 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
652}
Jean Delvareb353a482007-07-05 20:36:12 +0200653
654static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
655 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Jean Delvareb353a482007-07-05 20:36:12 +0200657 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct i2c_client *client = to_i2c_client(dev);
659 struct lm85_data *data = i2c_get_clientdata(client);
660 long val = simple_strtol(buf, NULL, 10);
661
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100662 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 data->temp_max[nr] = TEMP_TO_REG(val);
664 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100665 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return count;
667}
Jean Delvareb353a482007-07-05 20:36:12 +0200668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200670static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
671 show_temp, NULL, offset - 1); \
672static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
673 show_temp_min, set_temp_min, offset - 1); \
674static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
675 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677show_temp_reg(1);
678show_temp_reg(2);
679show_temp_reg(3);
680
681
682/* Automatic PWM control */
683
Jean Delvareb353a482007-07-05 20:36:12 +0200684static ssize_t show_pwm_auto_channels(struct device *dev,
685 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jean Delvareb353a482007-07-05 20:36:12 +0200687 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct lm85_data *data = lm85_update_device(dev);
689 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
690}
Jean Delvareb353a482007-07-05 20:36:12 +0200691
692static ssize_t set_pwm_auto_channels(struct device *dev,
693 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Jean Delvareb353a482007-07-05 20:36:12 +0200695 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct i2c_client *client = to_i2c_client(dev);
697 struct lm85_data *data = i2c_get_clientdata(client);
698 long val = simple_strtol(buf, NULL, 10);
699
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100700 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
702 | ZONE_TO_REG(val) ;
703 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
704 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100705 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return count;
707}
Jean Delvareb353a482007-07-05 20:36:12 +0200708
709static ssize_t show_pwm_auto_pwm_min(struct device *dev,
710 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jean Delvareb353a482007-07-05 20:36:12 +0200712 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct lm85_data *data = lm85_update_device(dev);
714 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
715}
Jean Delvareb353a482007-07-05 20:36:12 +0200716
717static ssize_t set_pwm_auto_pwm_min(struct device *dev,
718 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jean Delvareb353a482007-07-05 20:36:12 +0200720 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct i2c_client *client = to_i2c_client(dev);
722 struct lm85_data *data = i2c_get_clientdata(client);
723 long val = simple_strtol(buf, NULL, 10);
724
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100725 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 data->autofan[nr].min_pwm = PWM_TO_REG(val);
727 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
728 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return count;
731}
Jean Delvareb353a482007-07-05 20:36:12 +0200732
733static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
734 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Jean Delvareb353a482007-07-05 20:36:12 +0200736 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct lm85_data *data = lm85_update_device(dev);
738 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
739}
Jean Delvareb353a482007-07-05 20:36:12 +0200740
741static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
742 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Jean Delvareb353a482007-07-05 20:36:12 +0200744 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct i2c_client *client = to_i2c_client(dev);
746 struct lm85_data *data = i2c_get_clientdata(client);
747 long val = simple_strtol(buf, NULL, 10);
748
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100749 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 data->autofan[nr].min_off = val;
751 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
752 | data->syncpwm3
753 | (data->autofan[0].min_off ? 0x20 : 0)
754 | (data->autofan[1].min_off ? 0x40 : 0)
755 | (data->autofan[2].min_off ? 0x80 : 0)
756 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return count;
759}
Jean Delvareb353a482007-07-05 20:36:12 +0200760
761static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
762 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Jean Delvareb353a482007-07-05 20:36:12 +0200764 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct lm85_data *data = lm85_update_device(dev);
766 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
767}
Jean Delvareb353a482007-07-05 20:36:12 +0200768
769static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
770 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Jean Delvareb353a482007-07-05 20:36:12 +0200772 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct i2c_client *client = to_i2c_client(dev);
774 struct lm85_data *data = i2c_get_clientdata(client);
775 long val = simple_strtol(buf, NULL, 10);
776
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100777 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 data->autofan[nr].freq = FREQ_TO_REG(val);
779 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
780 (data->zone[nr].range << 4)
781 | data->autofan[nr].freq
782 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100783 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return count;
785}
Jean Delvareb353a482007-07-05 20:36:12 +0200786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200788static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
789 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
790 set_pwm_auto_channels, offset - 1); \
791static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
792 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
793 set_pwm_auto_pwm_min, offset - 1); \
794static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
795 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
796 set_pwm_auto_pwm_minctl, offset - 1); \
797static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
798 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
799 set_pwm_auto_pwm_freq, offset - 1);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801pwm_auto(1);
802pwm_auto(2);
803pwm_auto(3);
804
805/* Temperature settings for automatic PWM control */
806
Jean Delvareb353a482007-07-05 20:36:12 +0200807static ssize_t show_temp_auto_temp_off(struct device *dev,
808 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Jean Delvareb353a482007-07-05 20:36:12 +0200810 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct lm85_data *data = lm85_update_device(dev);
812 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
813 HYST_FROM_REG(data->zone[nr].hyst));
814}
Jean Delvareb353a482007-07-05 20:36:12 +0200815
816static ssize_t set_temp_auto_temp_off(struct device *dev,
817 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Jean Delvareb353a482007-07-05 20:36:12 +0200819 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct i2c_client *client = to_i2c_client(dev);
821 struct lm85_data *data = i2c_get_clientdata(client);
822 int min;
823 long val = simple_strtol(buf, NULL, 10);
824
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100825 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 min = TEMP_FROM_REG(data->zone[nr].limit);
827 data->zone[nr].off_desired = TEMP_TO_REG(val);
828 data->zone[nr].hyst = HYST_TO_REG(min - val);
829 if ( nr == 0 || nr == 1 ) {
830 lm85_write_value(client, LM85_REG_AFAN_HYST1,
831 (data->zone[0].hyst << 4)
832 | data->zone[1].hyst
833 );
834 } else {
835 lm85_write_value(client, LM85_REG_AFAN_HYST2,
836 (data->zone[2].hyst << 4)
837 );
838 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100839 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return count;
841}
Jean Delvareb353a482007-07-05 20:36:12 +0200842
843static ssize_t show_temp_auto_temp_min(struct device *dev,
844 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Jean Delvareb353a482007-07-05 20:36:12 +0200846 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct lm85_data *data = lm85_update_device(dev);
848 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
849}
Jean Delvareb353a482007-07-05 20:36:12 +0200850
851static ssize_t set_temp_auto_temp_min(struct device *dev,
852 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Jean Delvareb353a482007-07-05 20:36:12 +0200854 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct i2c_client *client = to_i2c_client(dev);
856 struct lm85_data *data = i2c_get_clientdata(client);
857 long val = simple_strtol(buf, NULL, 10);
858
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100859 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 data->zone[nr].limit = TEMP_TO_REG(val);
861 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
862 data->zone[nr].limit);
863
864/* Update temp_auto_max and temp_auto_range */
865 data->zone[nr].range = RANGE_TO_REG(
866 TEMP_FROM_REG(data->zone[nr].max_desired) -
867 TEMP_FROM_REG(data->zone[nr].limit));
868 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
869 ((data->zone[nr].range & 0x0f) << 4)
870 | (data->autofan[nr].freq & 0x07));
871
872/* Update temp_auto_hyst and temp_auto_off */
873 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
874 data->zone[nr].limit) - TEMP_FROM_REG(
875 data->zone[nr].off_desired));
876 if ( nr == 0 || nr == 1 ) {
877 lm85_write_value(client, LM85_REG_AFAN_HYST1,
878 (data->zone[0].hyst << 4)
879 | data->zone[1].hyst
880 );
881 } else {
882 lm85_write_value(client, LM85_REG_AFAN_HYST2,
883 (data->zone[2].hyst << 4)
884 );
885 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100886 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return count;
888}
Jean Delvareb353a482007-07-05 20:36:12 +0200889
890static ssize_t show_temp_auto_temp_max(struct device *dev,
891 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jean Delvareb353a482007-07-05 20:36:12 +0200893 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct lm85_data *data = lm85_update_device(dev);
895 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
896 RANGE_FROM_REG(data->zone[nr].range));
897}
Jean Delvareb353a482007-07-05 20:36:12 +0200898
899static ssize_t set_temp_auto_temp_max(struct device *dev,
900 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Jean Delvareb353a482007-07-05 20:36:12 +0200902 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct i2c_client *client = to_i2c_client(dev);
904 struct lm85_data *data = i2c_get_clientdata(client);
905 int min;
906 long val = simple_strtol(buf, NULL, 10);
907
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100908 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 min = TEMP_FROM_REG(data->zone[nr].limit);
910 data->zone[nr].max_desired = TEMP_TO_REG(val);
911 data->zone[nr].range = RANGE_TO_REG(
912 val - min);
913 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
914 ((data->zone[nr].range & 0x0f) << 4)
915 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100916 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return count;
918}
Jean Delvareb353a482007-07-05 20:36:12 +0200919
920static ssize_t show_temp_auto_temp_crit(struct device *dev,
921 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Jean Delvareb353a482007-07-05 20:36:12 +0200923 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct lm85_data *data = lm85_update_device(dev);
925 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
926}
Jean Delvareb353a482007-07-05 20:36:12 +0200927
928static ssize_t set_temp_auto_temp_crit(struct device *dev,
929 struct device_attribute *attr,const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Jean Delvareb353a482007-07-05 20:36:12 +0200931 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct i2c_client *client = to_i2c_client(dev);
933 struct lm85_data *data = i2c_get_clientdata(client);
934 long val = simple_strtol(buf, NULL, 10);
935
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100936 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 data->zone[nr].critical = TEMP_TO_REG(val);
938 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
939 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100940 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return count;
942}
Jean Delvareb353a482007-07-05 20:36:12 +0200943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200945static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
946 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
947 set_temp_auto_temp_off, offset - 1); \
948static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
949 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
950 set_temp_auto_temp_min, offset - 1); \
951static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
952 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
953 set_temp_auto_temp_max, offset - 1); \
954static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
955 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
956 set_temp_auto_temp_crit, offset - 1);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958temp_auto(1);
959temp_auto(2);
960temp_auto(3);
961
Ben Dooksd8d20612005-10-26 21:05:46 +0200962static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 if (!(adapter->class & I2C_CLASS_HWMON))
965 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200966 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200969static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200970 &sensor_dev_attr_fan1_input.dev_attr.attr,
971 &sensor_dev_attr_fan2_input.dev_attr.attr,
972 &sensor_dev_attr_fan3_input.dev_attr.attr,
973 &sensor_dev_attr_fan4_input.dev_attr.attr,
974 &sensor_dev_attr_fan1_min.dev_attr.attr,
975 &sensor_dev_attr_fan2_min.dev_attr.attr,
976 &sensor_dev_attr_fan3_min.dev_attr.attr,
977 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200978 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
979 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
980 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
981 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200982
983 &sensor_dev_attr_pwm1.dev_attr.attr,
984 &sensor_dev_attr_pwm2.dev_attr.attr,
985 &sensor_dev_attr_pwm3.dev_attr.attr,
986 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
987 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
988 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
989
990 &sensor_dev_attr_in0_input.dev_attr.attr,
991 &sensor_dev_attr_in1_input.dev_attr.attr,
992 &sensor_dev_attr_in2_input.dev_attr.attr,
993 &sensor_dev_attr_in3_input.dev_attr.attr,
994 &sensor_dev_attr_in0_min.dev_attr.attr,
995 &sensor_dev_attr_in1_min.dev_attr.attr,
996 &sensor_dev_attr_in2_min.dev_attr.attr,
997 &sensor_dev_attr_in3_min.dev_attr.attr,
998 &sensor_dev_attr_in0_max.dev_attr.attr,
999 &sensor_dev_attr_in1_max.dev_attr.attr,
1000 &sensor_dev_attr_in2_max.dev_attr.attr,
1001 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001002 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1003 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1004 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1005 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001006
1007 &sensor_dev_attr_temp1_input.dev_attr.attr,
1008 &sensor_dev_attr_temp2_input.dev_attr.attr,
1009 &sensor_dev_attr_temp3_input.dev_attr.attr,
1010 &sensor_dev_attr_temp1_min.dev_attr.attr,
1011 &sensor_dev_attr_temp2_min.dev_attr.attr,
1012 &sensor_dev_attr_temp3_min.dev_attr.attr,
1013 &sensor_dev_attr_temp1_max.dev_attr.attr,
1014 &sensor_dev_attr_temp2_max.dev_attr.attr,
1015 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001016 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1017 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1018 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1019 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1020 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001021
1022 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1023 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1024 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1025 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1026 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1027 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1028 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1029 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1030 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1031 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1032 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1033 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1034
1035 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1036 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1037 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1038 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1039 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1040 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1041 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1042 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1043 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1044 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1045 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1046 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1047
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001048 &dev_attr_vrm.attr,
1049 &dev_attr_cpu0_vid.attr,
1050 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001051 NULL
1052};
1053
1054static const struct attribute_group lm85_group = {
1055 .attrs = lm85_attributes,
1056};
1057
Jean Delvare6b9aad22007-07-05 20:37:21 +02001058static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001059 &sensor_dev_attr_in4_input.dev_attr.attr,
1060 &sensor_dev_attr_in4_min.dev_attr.attr,
1061 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001062 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001063 NULL
1064};
1065
Jean Delvare6b9aad22007-07-05 20:37:21 +02001066static const struct attribute_group lm85_group_in4 = {
1067 .attrs = lm85_attributes_in4,
1068};
1069
1070static struct attribute *lm85_attributes_in567[] = {
1071 &sensor_dev_attr_in5_input.dev_attr.attr,
1072 &sensor_dev_attr_in6_input.dev_attr.attr,
1073 &sensor_dev_attr_in7_input.dev_attr.attr,
1074 &sensor_dev_attr_in5_min.dev_attr.attr,
1075 &sensor_dev_attr_in6_min.dev_attr.attr,
1076 &sensor_dev_attr_in7_min.dev_attr.attr,
1077 &sensor_dev_attr_in5_max.dev_attr.attr,
1078 &sensor_dev_attr_in6_max.dev_attr.attr,
1079 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001080 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1081 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1082 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001083 NULL
1084};
1085
1086static const struct attribute_group lm85_group_in567 = {
1087 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001088};
1089
Ben Dooksd8d20612005-10-26 21:05:46 +02001090static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int kind)
1092{
1093 int company, verstep ;
1094 struct i2c_client *new_client = NULL;
1095 struct lm85_data *data;
1096 int err = 0;
1097 const char *type_name = "";
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (!i2c_check_functionality(adapter,
1100 I2C_FUNC_SMBUS_BYTE_DATA)) {
1101 /* We need to be able to do byte I/O */
1102 goto ERROR0 ;
1103 };
1104
1105 /* OK. For now, we presume we have a valid client. We now create the
1106 client structure, even though we cannot fill it completely yet.
1107 But it allows us to access lm85_{read,write}_value. */
1108
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001109 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 err = -ENOMEM;
1111 goto ERROR0;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 new_client = &data->client;
1115 i2c_set_clientdata(new_client, data);
1116 new_client->addr = address;
1117 new_client->adapter = adapter;
1118 new_client->driver = &lm85_driver;
1119 new_client->flags = 0;
1120
1121 /* Now, we do the remaining detection. */
1122
1123 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1124 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1125
1126 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1127 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1128 i2c_adapter_id(new_client->adapter), new_client->addr,
1129 company, verstep);
1130
1131 /* If auto-detecting, Determine the chip type. */
1132 if (kind <= 0) {
1133 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1134 i2c_adapter_id(adapter), address );
1135 if( company == LM85_COMPANY_NATIONAL
1136 && verstep == LM85_VERSTEP_LM85C ) {
1137 kind = lm85c ;
1138 } else if( company == LM85_COMPANY_NATIONAL
1139 && verstep == LM85_VERSTEP_LM85B ) {
1140 kind = lm85b ;
1141 } else if( company == LM85_COMPANY_NATIONAL
1142 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1143 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1144 " Defaulting to LM85.\n", verstep);
1145 kind = any_chip ;
1146 } else if( company == LM85_COMPANY_ANALOG_DEV
1147 && verstep == LM85_VERSTEP_ADM1027 ) {
1148 kind = adm1027 ;
1149 } else if( company == LM85_COMPANY_ANALOG_DEV
1150 && (verstep == LM85_VERSTEP_ADT7463
1151 || verstep == LM85_VERSTEP_ADT7463C) ) {
1152 kind = adt7463 ;
1153 } else if( company == LM85_COMPANY_ANALOG_DEV
1154 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1155 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1156 " Defaulting to Generic LM85.\n", verstep );
1157 kind = any_chip ;
1158 } else if( company == LM85_COMPANY_SMSC
1159 && (verstep == LM85_VERSTEP_EMC6D100_A0
1160 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1161 /* Unfortunately, we can't tell a '100 from a '101
1162 * from the registers. Since a '101 is a '100
1163 * in a package with fewer pins and therefore no
1164 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1165 * inputs read 0, then it's a '101.
1166 */
1167 kind = emc6d100 ;
1168 } else if( company == LM85_COMPANY_SMSC
1169 && verstep == LM85_VERSTEP_EMC6D102) {
1170 kind = emc6d102 ;
1171 } else if( company == LM85_COMPANY_SMSC
1172 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1173 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1174 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1175 " Defaulting to Generic LM85.\n", verstep );
1176 kind = any_chip ;
1177 } else if( kind == any_chip
1178 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1179 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1180 /* Leave kind as "any_chip" */
1181 } else {
1182 dev_dbg(&adapter->dev, "Autodetection failed\n");
1183 /* Not an LM85 ... */
1184 if( kind == any_chip ) { /* User used force=x,y */
1185 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1186 " found at %d,0x%02x. Try force_lm85c.\n",
1187 i2c_adapter_id(adapter), address );
1188 }
1189 err = 0 ;
1190 goto ERROR1;
1191 }
1192 }
1193
1194 /* Fill in the chip specific driver values */
1195 if ( kind == any_chip ) {
1196 type_name = "lm85";
1197 } else if ( kind == lm85b ) {
1198 type_name = "lm85b";
1199 } else if ( kind == lm85c ) {
1200 type_name = "lm85c";
1201 } else if ( kind == adm1027 ) {
1202 type_name = "adm1027";
1203 } else if ( kind == adt7463 ) {
1204 type_name = "adt7463";
1205 } else if ( kind == emc6d100){
1206 type_name = "emc6d100";
1207 } else if ( kind == emc6d102 ) {
1208 type_name = "emc6d102";
1209 }
1210 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1211
1212 /* Fill in the remaining client fields */
1213 data->type = kind;
1214 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001215 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* Tell the I2C layer a new client has arrived */
1218 if ((err = i2c_attach_client(new_client)))
1219 goto ERROR1;
1220
1221 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001222 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* Initialize the LM85 chip */
1225 lm85_init_client(new_client);
1226
1227 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001228 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001229 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jean Delvare9c516ef2005-11-26 20:07:54 +01001231 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1232 as a sixth digital VID input rather than an analog input. */
1233 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001234 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001235 if ((err = sysfs_create_group(&new_client->dev.kobj,
1236 &lm85_group_in4)))
1237 goto ERROR3;
1238
1239 /* The EMC6D100 has 3 additional voltage inputs */
1240 if (kind == emc6d100)
1241 if ((err = sysfs_create_group(&new_client->dev.kobj,
1242 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001243 goto ERROR3;
1244
Tony Jones1beeffe2007-08-20 13:46:20 -07001245 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1246 if (IS_ERR(data->hwmon_dev)) {
1247 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001248 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001249 }
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return 0;
1252
1253 /* Error out and cleanup code */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001254 ERROR3:
1255 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001256 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1257 if (kind == emc6d100)
1258 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001259 ERROR2:
1260 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 ERROR1:
1262 kfree(data);
1263 ERROR0:
1264 return err;
1265}
1266
Ben Dooksd8d20612005-10-26 21:05:46 +02001267static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001269 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001270 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001271 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001272 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1273 if (data->type == emc6d100)
1274 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001276 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278}
1279
1280
Ben Dooksd8d20612005-10-26 21:05:46 +02001281static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 int res;
1284
1285 /* What size location is it? */
1286 switch( reg ) {
1287 case LM85_REG_FAN(0) : /* Read WORD data */
1288 case LM85_REG_FAN(1) :
1289 case LM85_REG_FAN(2) :
1290 case LM85_REG_FAN(3) :
1291 case LM85_REG_FAN_MIN(0) :
1292 case LM85_REG_FAN_MIN(1) :
1293 case LM85_REG_FAN_MIN(2) :
1294 case LM85_REG_FAN_MIN(3) :
1295 case LM85_REG_ALARM1 : /* Read both bytes at once */
1296 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1297 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1298 break ;
1299 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1300 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1301 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1302 break ;
1303 default: /* Read BYTE data */
1304 res = i2c_smbus_read_byte_data(client, reg);
1305 break ;
1306 }
1307
1308 return res ;
1309}
1310
Ben Dooksd8d20612005-10-26 21:05:46 +02001311static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int res ;
1314
1315 switch( reg ) {
1316 case LM85_REG_FAN(0) : /* Write WORD data */
1317 case LM85_REG_FAN(1) :
1318 case LM85_REG_FAN(2) :
1319 case LM85_REG_FAN(3) :
1320 case LM85_REG_FAN_MIN(0) :
1321 case LM85_REG_FAN_MIN(1) :
1322 case LM85_REG_FAN_MIN(2) :
1323 case LM85_REG_FAN_MIN(3) :
1324 /* NOTE: ALARM is read only, so not included here */
1325 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1326 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1327 break ;
1328 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1329 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1330 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1331 break ;
1332 default: /* Write BYTE data */
1333 res = i2c_smbus_write_byte_data(client, reg, value);
1334 break ;
1335 }
1336
1337 return res ;
1338}
1339
Ben Dooksd8d20612005-10-26 21:05:46 +02001340static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 int value;
1343 struct lm85_data *data = i2c_get_clientdata(client);
1344
1345 dev_dbg(&client->dev, "Initializing device\n");
1346
1347 /* Warn if part was not "READY" */
1348 value = lm85_read_value(client, LM85_REG_CONFIG);
1349 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1350 if( value & 0x02 ) {
1351 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1352 i2c_adapter_id(client->adapter), client->addr );
1353 };
1354 if( ! (value & 0x04) ) {
1355 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1356 i2c_adapter_id(client->adapter), client->addr );
1357 };
1358 if( value & 0x10
1359 && ( data->type == adm1027
1360 || data->type == adt7463 ) ) {
1361 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1362 "Please report this to the lm85 maintainer.\n",
1363 i2c_adapter_id(client->adapter), client->addr );
1364 };
1365
1366 /* WE INTENTIONALLY make no changes to the limits,
1367 * offsets, pwms, fans and zones. If they were
1368 * configured, we don't want to mess with them.
1369 * If they weren't, the default is 100% PWM, no
1370 * control and will suffice until 'sensors -s'
1371 * can be run by the user.
1372 */
1373
1374 /* Start monitoring */
1375 value = lm85_read_value(client, LM85_REG_CONFIG);
1376 /* Try to clear LOCK, Set START, save everything else */
1377 value = (value & ~ 0x02) | 0x01 ;
1378 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1379 lm85_write_value(client, LM85_REG_CONFIG, value);
1380}
1381
1382static struct lm85_data *lm85_update_device(struct device *dev)
1383{
1384 struct i2c_client *client = to_i2c_client(dev);
1385 struct lm85_data *data = i2c_get_clientdata(client);
1386 int i;
1387
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001388 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if ( !data->valid ||
1391 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1392 /* Things that change quickly */
1393 dev_dbg(&client->dev, "Reading sensor values\n");
1394
1395 /* Have to read extended bits first to "freeze" the
1396 * more significant bits that are read later.
1397 */
1398 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1399 int ext1 = lm85_read_value(client,
1400 ADM1027_REG_EXTEND_ADC1);
1401 int ext2 = lm85_read_value(client,
1402 ADM1027_REG_EXTEND_ADC2);
1403 int val = (ext1 << 8) + ext2;
1404
1405 for(i = 0; i <= 4; i++)
1406 data->in_ext[i] = (val>>(i * 2))&0x03;
1407
1408 for(i = 0; i <= 2; i++)
1409 data->temp_ext[i] = (val>>((i + 5) * 2))&0x03;
1410 }
1411
1412 /* adc_scale is 2^(number of LSBs). There are 4 extra bits in
1413 the emc6d102 and 2 in the adt7463 and adm1027. In all
1414 other chips ext is always 0 and the value of scale is
1415 irrelevant. So it is left in 4*/
1416 data->adc_scale = (data->type == emc6d102 ) ? 16 : 4;
1417
Jean Delvare9c516ef2005-11-26 20:07:54 +01001418 data->vid = lm85_read_value(client, LM85_REG_VID);
1419
1420 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 data->in[i] =
1422 lm85_read_value(client, LM85_REG_IN(i));
1423 }
1424
Jean Delvare9c516ef2005-11-26 20:07:54 +01001425 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1426 data->in[4] = lm85_read_value(client,
1427 LM85_REG_IN(4));
1428 }
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 for (i = 0; i <= 3; ++i) {
1431 data->fan[i] =
1432 lm85_read_value(client, LM85_REG_FAN(i));
1433 }
1434
1435 for (i = 0; i <= 2; ++i) {
1436 data->temp[i] =
1437 lm85_read_value(client, LM85_REG_TEMP(i));
1438 }
1439
1440 for (i = 0; i <= 2; ++i) {
1441 data->pwm[i] =
1442 lm85_read_value(client, LM85_REG_PWM(i));
1443 }
1444
1445 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1446
1447 if ( data->type == adt7463 ) {
1448 if( data->therm_total < ULONG_MAX - 256 ) {
1449 data->therm_total +=
1450 lm85_read_value(client, ADT7463_REG_THERM );
1451 }
1452 } else if ( data->type == emc6d100 ) {
1453 /* Three more voltage sensors */
1454 for (i = 5; i <= 7; ++i) {
1455 data->in[i] =
1456 lm85_read_value(client, EMC6D100_REG_IN(i));
1457 }
1458 /* More alarm bits */
1459 data->alarms |=
1460 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1461 } else if (data->type == emc6d102 ) {
1462 /* Have to read LSB bits after the MSB ones because
1463 the reading of the MSB bits has frozen the
1464 LSBs (backward from the ADM1027).
1465 */
1466 int ext1 = lm85_read_value(client,
1467 EMC6D102_REG_EXTEND_ADC1);
1468 int ext2 = lm85_read_value(client,
1469 EMC6D102_REG_EXTEND_ADC2);
1470 int ext3 = lm85_read_value(client,
1471 EMC6D102_REG_EXTEND_ADC3);
1472 int ext4 = lm85_read_value(client,
1473 EMC6D102_REG_EXTEND_ADC4);
1474 data->in_ext[0] = ext3 & 0x0f;
1475 data->in_ext[1] = ext4 & 0x0f;
1476 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1477 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1478 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1479
1480 data->temp_ext[0] = ext1 & 0x0f;
1481 data->temp_ext[1] = ext2 & 0x0f;
1482 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1483 }
1484
1485 data->last_reading = jiffies ;
1486 }; /* last_reading */
1487
1488 if ( !data->valid ||
1489 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1490 /* Things that don't change often */
1491 dev_dbg(&client->dev, "Reading config values\n");
1492
Jean Delvare9c516ef2005-11-26 20:07:54 +01001493 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 data->in_min[i] =
1495 lm85_read_value(client, LM85_REG_IN_MIN(i));
1496 data->in_max[i] =
1497 lm85_read_value(client, LM85_REG_IN_MAX(i));
1498 }
1499
Jean Delvare9c516ef2005-11-26 20:07:54 +01001500 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1501 data->in_min[4] = lm85_read_value(client,
1502 LM85_REG_IN_MIN(4));
1503 data->in_max[4] = lm85_read_value(client,
1504 LM85_REG_IN_MAX(4));
1505 }
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if ( data->type == emc6d100 ) {
1508 for (i = 5; i <= 7; ++i) {
1509 data->in_min[i] =
1510 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1511 data->in_max[i] =
1512 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1513 }
1514 }
1515
1516 for (i = 0; i <= 3; ++i) {
1517 data->fan_min[i] =
1518 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1519 }
1520
1521 for (i = 0; i <= 2; ++i) {
1522 data->temp_min[i] =
1523 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1524 data->temp_max[i] =
1525 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1526 }
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 for (i = 0; i <= 2; ++i) {
1529 int val ;
1530 data->autofan[i].config =
1531 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1532 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1533 data->autofan[i].freq = val & 0x07 ;
1534 data->zone[i].range = (val >> 4) & 0x0f ;
1535 data->autofan[i].min_pwm =
1536 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1537 data->zone[i].limit =
1538 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1539 data->zone[i].critical =
1540 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1541 }
1542
1543 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1544 data->smooth[0] = i & 0x0f ;
1545 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1546 data->autofan[0].min_off = (i & 0x20) != 0 ;
1547 data->autofan[1].min_off = (i & 0x40) != 0 ;
1548 data->autofan[2].min_off = (i & 0x80) != 0 ;
1549 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1550 data->smooth[1] = (i>>4) & 0x0f ;
1551 data->smooth[2] = i & 0x0f ;
1552
1553 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1554 data->zone[0].hyst = (i>>4) & 0x0f ;
1555 data->zone[1].hyst = i & 0x0f ;
1556
1557 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1558 data->zone[2].hyst = (i>>4) & 0x0f ;
1559
1560 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1561 data->tach_mode = lm85_read_value(client,
1562 LM85_REG_TACH_MODE );
1563 data->spinup_ctl = lm85_read_value(client,
1564 LM85_REG_SPINUP_CTL );
1565 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1566 if ( data->type == adt7463 ) {
1567 for (i = 0; i <= 2; ++i) {
1568 data->oppoint[i] = lm85_read_value(client,
1569 ADT7463_REG_OPPOINT(i) );
1570 }
1571 data->tmin_ctl = lm85_read_value(client,
1572 ADT7463_REG_TMIN_CTL1 );
1573 data->therm_limit = lm85_read_value(client,
1574 ADT7463_REG_THERM_LIMIT );
1575 }
1576 for (i = 0; i <= 2; ++i) {
1577 data->temp_offset[i] = lm85_read_value(client,
1578 ADM1027_REG_TEMP_OFFSET(i) );
1579 }
1580 data->tach_mode = lm85_read_value(client,
1581 ADM1027_REG_CONFIG3 );
1582 data->fan_ppr = lm85_read_value(client,
1583 ADM1027_REG_FAN_PPR );
1584 }
1585
1586 data->last_config = jiffies;
1587 }; /* last_config */
1588
1589 data->valid = 1;
1590
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001591 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 return data;
1594}
1595
1596
1597static int __init sm_lm85_init(void)
1598{
1599 return i2c_add_driver(&lm85_driver);
1600}
1601
1602static void __exit sm_lm85_exit(void)
1603{
1604 i2c_del_driver(&lm85_driver);
1605}
1606
1607/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1608 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1609 * post 2.7.0 CVS changes.
1610 */
1611MODULE_LICENSE("GPL");
1612MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1613MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1614
1615module_init(sm_lm85_init);
1616module_exit(sm_lm85_exit);