blob: 9f1c6f1ee035d764352ac0e59cc5735721a8b6bd [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 */
Jean Delvare63f281a2007-07-05 20:39:40 +0200148static inline u16 FAN_TO_REG(unsigned long val)
149{
150 if (!val)
151 return 0xffff;
152 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
155
156/* Temperature is reported in .001 degC increments */
157#define TEMP_TO_REG(val) \
158 SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200159#define TEMPEXT_FROM_REG(val,ext) \
160 SCALE(((val) << 4) + (ext), 16, 1000)
161#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
164#define PWM_FROM_REG(val) (val)
165
166
167/* ZONEs have the following parameters:
168 * Limit (low) temp, 1. degC
169 * Hysteresis (below limit), 1. degC (0-15)
170 * Range of speed control, .1 degC (2-80)
171 * Critical (high) temp, 1. degC
172 *
173 * FAN PWMs have the following parameters:
174 * Reference Zone, 1, 2, 3, etc.
175 * Spinup time, .05 sec
176 * PWM value at limit/low temp, 1 count
177 * PWM Frequency, 1. Hz
178 * PWM is Min or OFF below limit, flag
179 * Invert PWM output, flag
180 *
181 * Some chips filter the temp, others the fan.
182 * Filter constant (or disabled) .1 seconds
183 */
184
185/* These are the zone temperature range encodings in .001 degree C */
186static int lm85_range_map[] = {
187 2000, 2500, 3300, 4000, 5000, 6600,
188 8000, 10000, 13300, 16000, 20000, 26600,
189 32000, 40000, 53300, 80000
190 };
191static int RANGE_TO_REG( int range )
192{
193 int i;
194
195 if ( range < lm85_range_map[0] ) {
196 return 0 ;
197 } else if ( range > lm85_range_map[15] ) {
198 return 15 ;
199 } else { /* find closest match */
200 for ( i = 14 ; i >= 0 ; --i ) {
201 if ( range > lm85_range_map[i] ) { /* range bracketed */
202 if ((lm85_range_map[i+1] - range) <
203 (range - lm85_range_map[i])) {
204 i++;
205 break;
206 }
207 break;
208 }
209 }
210 }
211 return( i & 0x0f );
212}
213#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
214
215/* These are the Acoustic Enhancement, or Temperature smoothing encodings
216 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
217 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
218 * is ignored, or set to 0.
219 */
220/* These are the PWM frequency encodings */
221static int lm85_freq_map[] = { /* .1 Hz */
222 100, 150, 230, 300, 380, 470, 620, 940
223 };
224static int FREQ_TO_REG( int freq )
225{
226 int i;
227
228 if( freq >= lm85_freq_map[7] ) { return 7 ; }
229 for( i = 0 ; i < 7 ; ++i )
230 if( freq <= lm85_freq_map[i] )
231 break ;
232 return( i & 0x07 );
233}
234#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
235
236/* Since we can't use strings, I'm abusing these numbers
237 * to stand in for the following meanings:
238 * 1 -- PWM responds to Zone 1
239 * 2 -- PWM responds to Zone 2
240 * 3 -- PWM responds to Zone 3
241 * 23 -- PWM responds to the higher temp of Zone 2 or 3
242 * 123 -- PWM responds to highest of Zone 1, 2, or 3
243 * 0 -- PWM is always at 0% (ie, off)
244 * -1 -- PWM is always at 100%
245 * -2 -- PWM responds to manual control
246 */
247
248static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
249#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
250
251static int ZONE_TO_REG( int zone )
252{
253 int i;
254
255 for( i = 0 ; i <= 7 ; ++i )
256 if( zone == lm85_zone_map[i] )
257 break ;
258 if( i > 7 ) /* Not found. */
259 i = 3; /* Always 100% */
260 return( (i & 0x07)<<5 );
261}
262
263#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
264#define HYST_FROM_REG(val) ((val)*1000)
265
266#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
267#define OFFSET_FROM_REG(val) ((val)*25)
268
269#define PPR_MASK(fan) (0x03<<(fan *2))
270#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
271#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* Chip sampling rates
274 *
275 * Some sensors are not updated more frequently than once per second
276 * so it doesn't make sense to read them more often than that.
277 * We cache the results and return the saved data if the driver
278 * is called again before a second has elapsed.
279 *
280 * Also, there is significant configuration data for this chip
281 * given the automatic PWM fan control that is possible. There
282 * are about 47 bytes of config data to only 22 bytes of actual
283 * readings. So, we keep the config data up to date in the cache
284 * when it is written and only sample it once every 1 *minute*
285 */
286#define LM85_DATA_INTERVAL (HZ + HZ / 2)
287#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/* LM85 can automatically adjust fan speeds based on temperature
290 * This structure encapsulates an entire Zone config. There are
291 * three zones (one for each temperature input) on the lm85
292 */
293struct lm85_zone {
294 s8 limit; /* Low temp limit */
295 u8 hyst; /* Low limit hysteresis. (0-15) */
296 u8 range; /* Temp range, encoded */
297 s8 critical; /* "All fans ON" temp limit */
298 u8 off_desired; /* Actual "off" temperature specified. Preserved
299 * to prevent "drift" as other autofan control
300 * values change.
301 */
302 u8 max_desired; /* Actual "max" temperature specified. Preserved
303 * to prevent "drift" as other autofan control
304 * values change.
305 */
306};
307
308struct lm85_autofan {
309 u8 config; /* Register value */
310 u8 freq; /* PWM frequency, encoded */
311 u8 min_pwm; /* Minimum PWM value, encoded */
312 u8 min_off; /* Min PWM or OFF below "limit", flag */
313};
314
Jean Delvareed6bafb2007-02-14 21:15:03 +0100315/* For each registered chip, we need to keep some data in memory.
316 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317struct lm85_data {
318 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700319 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 enum chips type;
321
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100322 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int valid; /* !=0 if following fields are valid */
324 unsigned long last_reading; /* In jiffies */
325 unsigned long last_config; /* In jiffies */
326
327 u8 in[8]; /* Register value */
328 u8 in_max[8]; /* Register value */
329 u8 in_min[8]; /* Register value */
330 s8 temp[3]; /* Register value */
331 s8 temp_min[3]; /* Register value */
332 s8 temp_max[3]; /* Register value */
333 s8 temp_offset[3]; /* Register value */
334 u16 fan[4]; /* Register value */
335 u16 fan_min[4]; /* Register value */
336 u8 pwm[3]; /* Register value */
337 u8 spinup_ctl; /* Register encoding, combined */
338 u8 tach_mode; /* Register encoding, combined */
339 u8 temp_ext[3]; /* Decoded values */
340 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 u8 fan_ppr; /* Register value */
342 u8 smooth[3]; /* Register encoding */
343 u8 vid; /* Register value */
344 u8 vrm; /* VRM version */
345 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
346 u8 oppoint[3]; /* Register value */
347 u16 tmin_ctl; /* Register value */
348 unsigned long therm_total; /* Cummulative therm count */
349 u8 therm_limit; /* Register value */
350 u32 alarms; /* Register encoding, combined */
351 struct lm85_autofan autofan[3];
352 struct lm85_zone zone[3];
353};
354
355static int lm85_attach_adapter(struct i2c_adapter *adapter);
356static int lm85_detect(struct i2c_adapter *adapter, int address,
357 int kind);
358static int lm85_detach_client(struct i2c_client *client);
359
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100360static int lm85_read_value(struct i2c_client *client, u8 reg);
361static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static struct lm85_data *lm85_update_device(struct device *dev);
363static void lm85_init_client(struct i2c_client *client);
364
365
366static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100367 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100368 .name = "lm85",
369 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 .id = I2C_DRIVERID_LM85,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .attach_adapter = lm85_attach_adapter,
372 .detach_client = lm85_detach_client,
373};
374
375
376/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200377static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
378 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Jean Delvareb353a482007-07-05 20:36:12 +0200380 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct lm85_data *data = lm85_update_device(dev);
382 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
383}
Jean Delvareb353a482007-07-05 20:36:12 +0200384
385static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
386 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jean Delvareb353a482007-07-05 20:36:12 +0200388 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct lm85_data *data = lm85_update_device(dev);
390 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
391}
Jean Delvareb353a482007-07-05 20:36:12 +0200392
393static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Jean Delvareb353a482007-07-05 20:36:12 +0200396 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct i2c_client *client = to_i2c_client(dev);
398 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200399 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 data->fan_min[nr] = FAN_TO_REG(val);
403 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100404 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return count;
406}
407
408#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200409static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
410 show_fan, NULL, offset - 1); \
411static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
412 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414show_fan_offset(1);
415show_fan_offset(2);
416show_fan_offset(3);
417show_fan_offset(4);
418
419/* vid, vrm, alarms */
420
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400421static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100424 int vid;
425
426 if (data->type == adt7463 && (data->vid & 0x80)) {
427 /* 6-pin VID (VRM 10) */
428 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
429 } else {
430 /* 5-pin VID (VRM 9) */
431 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
432 }
433
434 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
438
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400439static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Jean Delvare90d66192007-10-08 18:24:35 +0200441 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return sprintf(buf, "%ld\n", (long) data->vrm);
443}
444
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400445static 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 -0700446{
447 struct i2c_client *client = to_i2c_client(dev);
448 struct lm85_data *data = i2c_get_clientdata(client);
449 u32 val;
450
451 val = simple_strtoul(buf, NULL, 10);
452 data->vrm = val;
453 return count;
454}
455
456static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
457
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400458static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200461 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
465
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200466static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
467 char *buf)
468{
469 int nr = to_sensor_dev_attr(attr)->index;
470 struct lm85_data *data = lm85_update_device(dev);
471 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
472}
473
474static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
475static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
476static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
477static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
478static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
479static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
480static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
481static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
482static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
483static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
484static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
485static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
486static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
487static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
488static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
489static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
490static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/* pwm */
493
Jean Delvareb353a482007-07-05 20:36:12 +0200494static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
495 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Jean Delvareb353a482007-07-05 20:36:12 +0200497 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct lm85_data *data = lm85_update_device(dev);
499 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
500}
Jean Delvareb353a482007-07-05 20:36:12 +0200501
502static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Jean Delvareb353a482007-07-05 20:36:12 +0200505 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct i2c_client *client = to_i2c_client(dev);
507 struct lm85_data *data = i2c_get_clientdata(client);
508 long val = simple_strtol(buf, NULL, 10);
509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 data->pwm[nr] = PWM_TO_REG(val);
512 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100513 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return count;
515}
Jean Delvareb353a482007-07-05 20:36:12 +0200516
517static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
518 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Jean Delvareb353a482007-07-05 20:36:12 +0200520 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100522 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100525 switch (pwm_zone) {
526 case -1: /* PWM is always at 100% */
527 enable = 0;
528 break;
529 case 0: /* PWM is always at 0% */
530 case -2: /* PWM responds to manual control */
531 enable = 1;
532 break;
533 default: /* PWM in automatic mode */
534 enable = 2;
535 }
536 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Jean Delvare455f7912007-12-03 23:28:42 +0100539static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
540 *attr, const char *buf, size_t count)
541{
542 int nr = to_sensor_dev_attr(attr)->index;
543 struct i2c_client *client = to_i2c_client(dev);
544 struct lm85_data *data = i2c_get_clientdata(client);
545 long val = simple_strtol(buf, NULL, 10);
546 u8 config;
547
548 switch (val) {
549 case 0:
550 config = 3;
551 break;
552 case 1:
553 config = 7;
554 break;
555 case 2:
556 /* Here we have to choose arbitrarily one of the 5 possible
557 configurations; I go for the safest */
558 config = 6;
559 break;
560 default:
561 return -EINVAL;
562 }
563
564 mutex_lock(&data->update_lock);
565 data->autofan[nr].config = lm85_read_value(client,
566 LM85_REG_AFAN_CONFIG(nr));
567 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
568 | (config << 5);
569 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
570 data->autofan[nr].config);
571 mutex_unlock(&data->update_lock);
572 return count;
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200576static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
577 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100578static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
579 show_pwm_enable, set_pwm_enable, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581show_pwm_reg(1);
582show_pwm_reg(2);
583show_pwm_reg(3);
584
585/* Voltages */
586
Jean Delvareb353a482007-07-05 20:36:12 +0200587static ssize_t show_in(struct device *dev, struct device_attribute *attr,
588 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Jean Delvareb353a482007-07-05 20:36:12 +0200590 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct lm85_data *data = lm85_update_device(dev);
592 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
593 data->in[nr],
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200594 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
Jean Delvareb353a482007-07-05 20:36:12 +0200596
597static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
598 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Jean Delvareb353a482007-07-05 20:36:12 +0200600 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct lm85_data *data = lm85_update_device(dev);
602 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
603}
Jean Delvareb353a482007-07-05 20:36:12 +0200604
605static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jean Delvareb353a482007-07-05 20:36:12 +0200608 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct i2c_client *client = to_i2c_client(dev);
610 struct lm85_data *data = i2c_get_clientdata(client);
611 long val = simple_strtol(buf, NULL, 10);
612
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100613 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 data->in_min[nr] = INS_TO_REG(nr, val);
615 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100616 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return count;
618}
Jean Delvareb353a482007-07-05 20:36:12 +0200619
620static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
621 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Jean Delvareb353a482007-07-05 20:36:12 +0200623 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct lm85_data *data = lm85_update_device(dev);
625 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
626}
Jean Delvareb353a482007-07-05 20:36:12 +0200627
628static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
629 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Jean Delvareb353a482007-07-05 20:36:12 +0200631 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct i2c_client *client = to_i2c_client(dev);
633 struct lm85_data *data = i2c_get_clientdata(client);
634 long val = simple_strtol(buf, NULL, 10);
635
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100636 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 data->in_max[nr] = INS_TO_REG(nr, val);
638 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100639 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return count;
641}
Jean Delvareb353a482007-07-05 20:36:12 +0200642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200644static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
645 show_in, NULL, offset); \
646static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
647 show_in_min, set_in_min, offset); \
648static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
649 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651show_in_reg(0);
652show_in_reg(1);
653show_in_reg(2);
654show_in_reg(3);
655show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200656show_in_reg(5);
657show_in_reg(6);
658show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660/* Temps */
661
Jean Delvareb353a482007-07-05 20:36:12 +0200662static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
663 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Jean Delvareb353a482007-07-05 20:36:12 +0200665 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct lm85_data *data = lm85_update_device(dev);
667 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200668 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
Jean Delvareb353a482007-07-05 20:36:12 +0200670
671static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
672 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jean Delvareb353a482007-07-05 20:36:12 +0200674 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct lm85_data *data = lm85_update_device(dev);
676 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
677}
Jean Delvareb353a482007-07-05 20:36:12 +0200678
679static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
680 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jean Delvareb353a482007-07-05 20:36:12 +0200682 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct i2c_client *client = to_i2c_client(dev);
684 struct lm85_data *data = i2c_get_clientdata(client);
685 long val = simple_strtol(buf, NULL, 10);
686
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100687 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 data->temp_min[nr] = TEMP_TO_REG(val);
689 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100690 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return count;
692}
Jean Delvareb353a482007-07-05 20:36:12 +0200693
694static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
695 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Jean Delvareb353a482007-07-05 20:36:12 +0200697 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct lm85_data *data = lm85_update_device(dev);
699 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
700}
Jean Delvareb353a482007-07-05 20:36:12 +0200701
702static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Jean Delvareb353a482007-07-05 20:36:12 +0200705 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct i2c_client *client = to_i2c_client(dev);
707 struct lm85_data *data = i2c_get_clientdata(client);
708 long val = simple_strtol(buf, NULL, 10);
709
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100710 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 data->temp_max[nr] = TEMP_TO_REG(val);
712 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100713 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return count;
715}
Jean Delvareb353a482007-07-05 20:36:12 +0200716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200718static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
719 show_temp, NULL, offset - 1); \
720static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
721 show_temp_min, set_temp_min, offset - 1); \
722static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
723 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725show_temp_reg(1);
726show_temp_reg(2);
727show_temp_reg(3);
728
729
730/* Automatic PWM control */
731
Jean Delvareb353a482007-07-05 20:36:12 +0200732static ssize_t show_pwm_auto_channels(struct device *dev,
733 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Jean Delvareb353a482007-07-05 20:36:12 +0200735 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct lm85_data *data = lm85_update_device(dev);
737 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
738}
Jean Delvareb353a482007-07-05 20:36:12 +0200739
740static ssize_t set_pwm_auto_channels(struct device *dev,
741 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Jean Delvareb353a482007-07-05 20:36:12 +0200743 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct i2c_client *client = to_i2c_client(dev);
745 struct lm85_data *data = i2c_get_clientdata(client);
746 long val = simple_strtol(buf, NULL, 10);
747
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100748 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
750 | ZONE_TO_REG(val) ;
751 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
752 data->autofan[nr].config);
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_min(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", PWM_FROM_REG(data->autofan[nr].min_pwm));
763}
Jean Delvareb353a482007-07-05 20:36:12 +0200764
765static ssize_t set_pwm_auto_pwm_min(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].min_pwm = PWM_TO_REG(val);
775 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
776 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100777 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return count;
779}
Jean Delvareb353a482007-07-05 20:36:12 +0200780
781static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
782 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Jean Delvareb353a482007-07-05 20:36:12 +0200784 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct lm85_data *data = lm85_update_device(dev);
786 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
787}
Jean Delvareb353a482007-07-05 20:36:12 +0200788
789static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
790 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jean Delvareb353a482007-07-05 20:36:12 +0200792 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct i2c_client *client = to_i2c_client(dev);
794 struct lm85_data *data = i2c_get_clientdata(client);
795 long val = simple_strtol(buf, NULL, 10);
796
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100797 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 data->autofan[nr].min_off = val;
799 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
800 | data->syncpwm3
801 | (data->autofan[0].min_off ? 0x20 : 0)
802 | (data->autofan[1].min_off ? 0x40 : 0)
803 | (data->autofan[2].min_off ? 0x80 : 0)
804 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100805 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return count;
807}
Jean Delvareb353a482007-07-05 20:36:12 +0200808
809static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
810 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Jean Delvareb353a482007-07-05 20:36:12 +0200812 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct lm85_data *data = lm85_update_device(dev);
814 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
815}
Jean Delvareb353a482007-07-05 20:36:12 +0200816
817static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
818 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Jean Delvareb353a482007-07-05 20:36:12 +0200820 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct i2c_client *client = to_i2c_client(dev);
822 struct lm85_data *data = i2c_get_clientdata(client);
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 data->autofan[nr].freq = FREQ_TO_REG(val);
827 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
828 (data->zone[nr].range << 4)
829 | data->autofan[nr].freq
830 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100831 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return count;
833}
Jean Delvareb353a482007-07-05 20:36:12 +0200834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200836static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
837 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
838 set_pwm_auto_channels, offset - 1); \
839static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
840 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
841 set_pwm_auto_pwm_min, offset - 1); \
842static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
843 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
844 set_pwm_auto_pwm_minctl, offset - 1); \
845static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
846 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
847 set_pwm_auto_pwm_freq, offset - 1);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849pwm_auto(1);
850pwm_auto(2);
851pwm_auto(3);
852
853/* Temperature settings for automatic PWM control */
854
Jean Delvareb353a482007-07-05 20:36:12 +0200855static ssize_t show_temp_auto_temp_off(struct device *dev,
856 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Jean Delvareb353a482007-07-05 20:36:12 +0200858 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct lm85_data *data = lm85_update_device(dev);
860 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
861 HYST_FROM_REG(data->zone[nr].hyst));
862}
Jean Delvareb353a482007-07-05 20:36:12 +0200863
864static ssize_t set_temp_auto_temp_off(struct device *dev,
865 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Jean Delvareb353a482007-07-05 20:36:12 +0200867 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct i2c_client *client = to_i2c_client(dev);
869 struct lm85_data *data = i2c_get_clientdata(client);
870 int min;
871 long val = simple_strtol(buf, NULL, 10);
872
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100873 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 min = TEMP_FROM_REG(data->zone[nr].limit);
875 data->zone[nr].off_desired = TEMP_TO_REG(val);
876 data->zone[nr].hyst = HYST_TO_REG(min - val);
877 if ( nr == 0 || nr == 1 ) {
878 lm85_write_value(client, LM85_REG_AFAN_HYST1,
879 (data->zone[0].hyst << 4)
880 | data->zone[1].hyst
881 );
882 } else {
883 lm85_write_value(client, LM85_REG_AFAN_HYST2,
884 (data->zone[2].hyst << 4)
885 );
886 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100887 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return count;
889}
Jean Delvareb353a482007-07-05 20:36:12 +0200890
891static ssize_t show_temp_auto_temp_min(struct device *dev,
892 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Jean Delvareb353a482007-07-05 20:36:12 +0200894 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct lm85_data *data = lm85_update_device(dev);
896 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
897}
Jean Delvareb353a482007-07-05 20:36:12 +0200898
899static ssize_t set_temp_auto_temp_min(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 long val = simple_strtol(buf, NULL, 10);
906
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100907 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 data->zone[nr].limit = TEMP_TO_REG(val);
909 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
910 data->zone[nr].limit);
911
912/* Update temp_auto_max and temp_auto_range */
913 data->zone[nr].range = RANGE_TO_REG(
914 TEMP_FROM_REG(data->zone[nr].max_desired) -
915 TEMP_FROM_REG(data->zone[nr].limit));
916 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
917 ((data->zone[nr].range & 0x0f) << 4)
918 | (data->autofan[nr].freq & 0x07));
919
920/* Update temp_auto_hyst and temp_auto_off */
921 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
922 data->zone[nr].limit) - TEMP_FROM_REG(
923 data->zone[nr].off_desired));
924 if ( nr == 0 || nr == 1 ) {
925 lm85_write_value(client, LM85_REG_AFAN_HYST1,
926 (data->zone[0].hyst << 4)
927 | data->zone[1].hyst
928 );
929 } else {
930 lm85_write_value(client, LM85_REG_AFAN_HYST2,
931 (data->zone[2].hyst << 4)
932 );
933 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100934 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return count;
936}
Jean Delvareb353a482007-07-05 20:36:12 +0200937
938static ssize_t show_temp_auto_temp_max(struct device *dev,
939 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Jean Delvareb353a482007-07-05 20:36:12 +0200941 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct lm85_data *data = lm85_update_device(dev);
943 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
944 RANGE_FROM_REG(data->zone[nr].range));
945}
Jean Delvareb353a482007-07-05 20:36:12 +0200946
947static ssize_t set_temp_auto_temp_max(struct device *dev,
948 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Jean Delvareb353a482007-07-05 20:36:12 +0200950 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct i2c_client *client = to_i2c_client(dev);
952 struct lm85_data *data = i2c_get_clientdata(client);
953 int min;
954 long val = simple_strtol(buf, NULL, 10);
955
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100956 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 min = TEMP_FROM_REG(data->zone[nr].limit);
958 data->zone[nr].max_desired = TEMP_TO_REG(val);
959 data->zone[nr].range = RANGE_TO_REG(
960 val - min);
961 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
962 ((data->zone[nr].range & 0x0f) << 4)
963 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100964 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return count;
966}
Jean Delvareb353a482007-07-05 20:36:12 +0200967
968static ssize_t show_temp_auto_temp_crit(struct device *dev,
969 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Jean Delvareb353a482007-07-05 20:36:12 +0200971 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct lm85_data *data = lm85_update_device(dev);
973 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
974}
Jean Delvareb353a482007-07-05 20:36:12 +0200975
976static ssize_t set_temp_auto_temp_crit(struct device *dev,
977 struct device_attribute *attr,const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Jean Delvareb353a482007-07-05 20:36:12 +0200979 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct i2c_client *client = to_i2c_client(dev);
981 struct lm85_data *data = i2c_get_clientdata(client);
982 long val = simple_strtol(buf, NULL, 10);
983
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100984 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 data->zone[nr].critical = TEMP_TO_REG(val);
986 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
987 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100988 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return count;
990}
Jean Delvareb353a482007-07-05 20:36:12 +0200991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200993static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
994 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
995 set_temp_auto_temp_off, offset - 1); \
996static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
997 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
998 set_temp_auto_temp_min, offset - 1); \
999static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1000 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1001 set_temp_auto_temp_max, offset - 1); \
1002static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1003 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1004 set_temp_auto_temp_crit, offset - 1);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006temp_auto(1);
1007temp_auto(2);
1008temp_auto(3);
1009
Ben Dooksd8d20612005-10-26 21:05:46 +02001010static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 if (!(adapter->class & I2C_CLASS_HWMON))
1013 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +02001014 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001017static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001018 &sensor_dev_attr_fan1_input.dev_attr.attr,
1019 &sensor_dev_attr_fan2_input.dev_attr.attr,
1020 &sensor_dev_attr_fan3_input.dev_attr.attr,
1021 &sensor_dev_attr_fan4_input.dev_attr.attr,
1022 &sensor_dev_attr_fan1_min.dev_attr.attr,
1023 &sensor_dev_attr_fan2_min.dev_attr.attr,
1024 &sensor_dev_attr_fan3_min.dev_attr.attr,
1025 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001026 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1027 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1028 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1029 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001030
1031 &sensor_dev_attr_pwm1.dev_attr.attr,
1032 &sensor_dev_attr_pwm2.dev_attr.attr,
1033 &sensor_dev_attr_pwm3.dev_attr.attr,
1034 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1035 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1036 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1037
1038 &sensor_dev_attr_in0_input.dev_attr.attr,
1039 &sensor_dev_attr_in1_input.dev_attr.attr,
1040 &sensor_dev_attr_in2_input.dev_attr.attr,
1041 &sensor_dev_attr_in3_input.dev_attr.attr,
1042 &sensor_dev_attr_in0_min.dev_attr.attr,
1043 &sensor_dev_attr_in1_min.dev_attr.attr,
1044 &sensor_dev_attr_in2_min.dev_attr.attr,
1045 &sensor_dev_attr_in3_min.dev_attr.attr,
1046 &sensor_dev_attr_in0_max.dev_attr.attr,
1047 &sensor_dev_attr_in1_max.dev_attr.attr,
1048 &sensor_dev_attr_in2_max.dev_attr.attr,
1049 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001050 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1051 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1052 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1053 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001054
1055 &sensor_dev_attr_temp1_input.dev_attr.attr,
1056 &sensor_dev_attr_temp2_input.dev_attr.attr,
1057 &sensor_dev_attr_temp3_input.dev_attr.attr,
1058 &sensor_dev_attr_temp1_min.dev_attr.attr,
1059 &sensor_dev_attr_temp2_min.dev_attr.attr,
1060 &sensor_dev_attr_temp3_min.dev_attr.attr,
1061 &sensor_dev_attr_temp1_max.dev_attr.attr,
1062 &sensor_dev_attr_temp2_max.dev_attr.attr,
1063 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001064 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1065 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1066 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1067 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1068 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001069
1070 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1071 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1072 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1073 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1074 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1075 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1076 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1077 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1078 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1079 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1080 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1081 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1082
1083 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1084 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1085 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1086 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1087 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1088 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1089 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1090 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1091 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1092 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1093 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1094 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1095
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001096 &dev_attr_vrm.attr,
1097 &dev_attr_cpu0_vid.attr,
1098 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001099 NULL
1100};
1101
1102static const struct attribute_group lm85_group = {
1103 .attrs = lm85_attributes,
1104};
1105
Jean Delvare6b9aad22007-07-05 20:37:21 +02001106static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001107 &sensor_dev_attr_in4_input.dev_attr.attr,
1108 &sensor_dev_attr_in4_min.dev_attr.attr,
1109 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001110 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001111 NULL
1112};
1113
Jean Delvare6b9aad22007-07-05 20:37:21 +02001114static const struct attribute_group lm85_group_in4 = {
1115 .attrs = lm85_attributes_in4,
1116};
1117
1118static struct attribute *lm85_attributes_in567[] = {
1119 &sensor_dev_attr_in5_input.dev_attr.attr,
1120 &sensor_dev_attr_in6_input.dev_attr.attr,
1121 &sensor_dev_attr_in7_input.dev_attr.attr,
1122 &sensor_dev_attr_in5_min.dev_attr.attr,
1123 &sensor_dev_attr_in6_min.dev_attr.attr,
1124 &sensor_dev_attr_in7_min.dev_attr.attr,
1125 &sensor_dev_attr_in5_max.dev_attr.attr,
1126 &sensor_dev_attr_in6_max.dev_attr.attr,
1127 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001128 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1129 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1130 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001131 NULL
1132};
1133
1134static const struct attribute_group lm85_group_in567 = {
1135 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001136};
1137
Ben Dooksd8d20612005-10-26 21:05:46 +02001138static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int kind)
1140{
1141 int company, verstep ;
1142 struct i2c_client *new_client = NULL;
1143 struct lm85_data *data;
1144 int err = 0;
1145 const char *type_name = "";
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!i2c_check_functionality(adapter,
1148 I2C_FUNC_SMBUS_BYTE_DATA)) {
1149 /* We need to be able to do byte I/O */
1150 goto ERROR0 ;
1151 };
1152
1153 /* OK. For now, we presume we have a valid client. We now create the
1154 client structure, even though we cannot fill it completely yet.
1155 But it allows us to access lm85_{read,write}_value. */
1156
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001157 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 err = -ENOMEM;
1159 goto ERROR0;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 new_client = &data->client;
1163 i2c_set_clientdata(new_client, data);
1164 new_client->addr = address;
1165 new_client->adapter = adapter;
1166 new_client->driver = &lm85_driver;
1167 new_client->flags = 0;
1168
1169 /* Now, we do the remaining detection. */
1170
1171 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1172 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1173
1174 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1175 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1176 i2c_adapter_id(new_client->adapter), new_client->addr,
1177 company, verstep);
1178
1179 /* If auto-detecting, Determine the chip type. */
1180 if (kind <= 0) {
1181 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1182 i2c_adapter_id(adapter), address );
1183 if( company == LM85_COMPANY_NATIONAL
1184 && verstep == LM85_VERSTEP_LM85C ) {
1185 kind = lm85c ;
1186 } else if( company == LM85_COMPANY_NATIONAL
1187 && verstep == LM85_VERSTEP_LM85B ) {
1188 kind = lm85b ;
1189 } else if( company == LM85_COMPANY_NATIONAL
1190 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1191 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1192 " Defaulting to LM85.\n", verstep);
1193 kind = any_chip ;
1194 } else if( company == LM85_COMPANY_ANALOG_DEV
1195 && verstep == LM85_VERSTEP_ADM1027 ) {
1196 kind = adm1027 ;
1197 } else if( company == LM85_COMPANY_ANALOG_DEV
1198 && (verstep == LM85_VERSTEP_ADT7463
1199 || verstep == LM85_VERSTEP_ADT7463C) ) {
1200 kind = adt7463 ;
1201 } else if( company == LM85_COMPANY_ANALOG_DEV
1202 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1203 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1204 " Defaulting to Generic LM85.\n", verstep );
1205 kind = any_chip ;
1206 } else if( company == LM85_COMPANY_SMSC
1207 && (verstep == LM85_VERSTEP_EMC6D100_A0
1208 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1209 /* Unfortunately, we can't tell a '100 from a '101
1210 * from the registers. Since a '101 is a '100
1211 * in a package with fewer pins and therefore no
1212 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1213 * inputs read 0, then it's a '101.
1214 */
1215 kind = emc6d100 ;
1216 } else if( company == LM85_COMPANY_SMSC
1217 && verstep == LM85_VERSTEP_EMC6D102) {
1218 kind = emc6d102 ;
1219 } else if( company == LM85_COMPANY_SMSC
1220 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1221 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1222 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1223 " Defaulting to Generic LM85.\n", verstep );
1224 kind = any_chip ;
1225 } else if( kind == any_chip
1226 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1227 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1228 /* Leave kind as "any_chip" */
1229 } else {
1230 dev_dbg(&adapter->dev, "Autodetection failed\n");
1231 /* Not an LM85 ... */
1232 if( kind == any_chip ) { /* User used force=x,y */
1233 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1234 " found at %d,0x%02x. Try force_lm85c.\n",
1235 i2c_adapter_id(adapter), address );
1236 }
1237 err = 0 ;
1238 goto ERROR1;
1239 }
1240 }
1241
1242 /* Fill in the chip specific driver values */
1243 if ( kind == any_chip ) {
1244 type_name = "lm85";
1245 } else if ( kind == lm85b ) {
1246 type_name = "lm85b";
1247 } else if ( kind == lm85c ) {
1248 type_name = "lm85c";
1249 } else if ( kind == adm1027 ) {
1250 type_name = "adm1027";
1251 } else if ( kind == adt7463 ) {
1252 type_name = "adt7463";
1253 } else if ( kind == emc6d100){
1254 type_name = "emc6d100";
1255 } else if ( kind == emc6d102 ) {
1256 type_name = "emc6d102";
1257 }
1258 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1259
1260 /* Fill in the remaining client fields */
1261 data->type = kind;
1262 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001263 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* Tell the I2C layer a new client has arrived */
1266 if ((err = i2c_attach_client(new_client)))
1267 goto ERROR1;
1268
1269 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001270 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* Initialize the LM85 chip */
1273 lm85_init_client(new_client);
1274
1275 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001276 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001277 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Jean Delvare9c516ef2005-11-26 20:07:54 +01001279 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1280 as a sixth digital VID input rather than an analog input. */
1281 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001282 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001283 if ((err = sysfs_create_group(&new_client->dev.kobj,
1284 &lm85_group_in4)))
1285 goto ERROR3;
1286
1287 /* The EMC6D100 has 3 additional voltage inputs */
1288 if (kind == emc6d100)
1289 if ((err = sysfs_create_group(&new_client->dev.kobj,
1290 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001291 goto ERROR3;
1292
Tony Jones1beeffe2007-08-20 13:46:20 -07001293 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1294 if (IS_ERR(data->hwmon_dev)) {
1295 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001296 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001297 }
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
1300
1301 /* Error out and cleanup code */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001302 ERROR3:
1303 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001304 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1305 if (kind == emc6d100)
1306 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001307 ERROR2:
1308 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ERROR1:
1310 kfree(data);
1311 ERROR0:
1312 return err;
1313}
1314
Ben Dooksd8d20612005-10-26 21:05:46 +02001315static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001317 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001318 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001319 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001320 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1321 if (data->type == emc6d100)
1322 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001324 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327
1328
Ben Dooksd8d20612005-10-26 21:05:46 +02001329static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 int res;
1332
1333 /* What size location is it? */
1334 switch( reg ) {
1335 case LM85_REG_FAN(0) : /* Read WORD data */
1336 case LM85_REG_FAN(1) :
1337 case LM85_REG_FAN(2) :
1338 case LM85_REG_FAN(3) :
1339 case LM85_REG_FAN_MIN(0) :
1340 case LM85_REG_FAN_MIN(1) :
1341 case LM85_REG_FAN_MIN(2) :
1342 case LM85_REG_FAN_MIN(3) :
1343 case LM85_REG_ALARM1 : /* Read both bytes at once */
1344 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1345 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1346 break ;
1347 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1348 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1349 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1350 break ;
1351 default: /* Read BYTE data */
1352 res = i2c_smbus_read_byte_data(client, reg);
1353 break ;
1354 }
1355
1356 return res ;
1357}
1358
Ben Dooksd8d20612005-10-26 21:05:46 +02001359static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 int res ;
1362
1363 switch( reg ) {
1364 case LM85_REG_FAN(0) : /* Write WORD data */
1365 case LM85_REG_FAN(1) :
1366 case LM85_REG_FAN(2) :
1367 case LM85_REG_FAN(3) :
1368 case LM85_REG_FAN_MIN(0) :
1369 case LM85_REG_FAN_MIN(1) :
1370 case LM85_REG_FAN_MIN(2) :
1371 case LM85_REG_FAN_MIN(3) :
1372 /* NOTE: ALARM is read only, so not included here */
1373 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1374 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1375 break ;
1376 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1377 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1378 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1379 break ;
1380 default: /* Write BYTE data */
1381 res = i2c_smbus_write_byte_data(client, reg, value);
1382 break ;
1383 }
1384
1385 return res ;
1386}
1387
Ben Dooksd8d20612005-10-26 21:05:46 +02001388static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 int value;
1391 struct lm85_data *data = i2c_get_clientdata(client);
1392
1393 dev_dbg(&client->dev, "Initializing device\n");
1394
1395 /* Warn if part was not "READY" */
1396 value = lm85_read_value(client, LM85_REG_CONFIG);
1397 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1398 if( value & 0x02 ) {
1399 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1400 i2c_adapter_id(client->adapter), client->addr );
1401 };
1402 if( ! (value & 0x04) ) {
1403 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1404 i2c_adapter_id(client->adapter), client->addr );
1405 };
1406 if( value & 0x10
1407 && ( data->type == adm1027
1408 || data->type == adt7463 ) ) {
1409 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1410 "Please report this to the lm85 maintainer.\n",
1411 i2c_adapter_id(client->adapter), client->addr );
1412 };
1413
1414 /* WE INTENTIONALLY make no changes to the limits,
1415 * offsets, pwms, fans and zones. If they were
1416 * configured, we don't want to mess with them.
1417 * If they weren't, the default is 100% PWM, no
1418 * control and will suffice until 'sensors -s'
1419 * can be run by the user.
1420 */
1421
1422 /* Start monitoring */
1423 value = lm85_read_value(client, LM85_REG_CONFIG);
1424 /* Try to clear LOCK, Set START, save everything else */
1425 value = (value & ~ 0x02) | 0x01 ;
1426 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1427 lm85_write_value(client, LM85_REG_CONFIG, value);
1428}
1429
1430static struct lm85_data *lm85_update_device(struct device *dev)
1431{
1432 struct i2c_client *client = to_i2c_client(dev);
1433 struct lm85_data *data = i2c_get_clientdata(client);
1434 int i;
1435
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001436 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if ( !data->valid ||
1439 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1440 /* Things that change quickly */
1441 dev_dbg(&client->dev, "Reading sensor values\n");
1442
1443 /* Have to read extended bits first to "freeze" the
1444 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001445 * There are 2 additional resolution bits per channel and we
1446 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
1448 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1449 int ext1 = lm85_read_value(client,
1450 ADM1027_REG_EXTEND_ADC1);
1451 int ext2 = lm85_read_value(client,
1452 ADM1027_REG_EXTEND_ADC2);
1453 int val = (ext1 << 8) + ext2;
1454
1455 for(i = 0; i <= 4; i++)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001456 data->in_ext[i] = ((val>>(i * 2))&0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 for(i = 0; i <= 2; i++)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001459 data->temp_ext[i] = (val>>((i + 4) * 2))&0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
Jean Delvare9c516ef2005-11-26 20:07:54 +01001462 data->vid = lm85_read_value(client, LM85_REG_VID);
1463
1464 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 data->in[i] =
1466 lm85_read_value(client, LM85_REG_IN(i));
1467 }
1468
Jean Delvare9c516ef2005-11-26 20:07:54 +01001469 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1470 data->in[4] = lm85_read_value(client,
1471 LM85_REG_IN(4));
1472 }
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 for (i = 0; i <= 3; ++i) {
1475 data->fan[i] =
1476 lm85_read_value(client, LM85_REG_FAN(i));
1477 }
1478
1479 for (i = 0; i <= 2; ++i) {
1480 data->temp[i] =
1481 lm85_read_value(client, LM85_REG_TEMP(i));
1482 }
1483
1484 for (i = 0; i <= 2; ++i) {
1485 data->pwm[i] =
1486 lm85_read_value(client, LM85_REG_PWM(i));
1487 }
1488
1489 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1490
1491 if ( data->type == adt7463 ) {
1492 if( data->therm_total < ULONG_MAX - 256 ) {
1493 data->therm_total +=
1494 lm85_read_value(client, ADT7463_REG_THERM );
1495 }
1496 } else if ( data->type == emc6d100 ) {
1497 /* Three more voltage sensors */
1498 for (i = 5; i <= 7; ++i) {
1499 data->in[i] =
1500 lm85_read_value(client, EMC6D100_REG_IN(i));
1501 }
1502 /* More alarm bits */
1503 data->alarms |=
1504 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1505 } else if (data->type == emc6d102 ) {
1506 /* Have to read LSB bits after the MSB ones because
1507 the reading of the MSB bits has frozen the
1508 LSBs (backward from the ADM1027).
1509 */
1510 int ext1 = lm85_read_value(client,
1511 EMC6D102_REG_EXTEND_ADC1);
1512 int ext2 = lm85_read_value(client,
1513 EMC6D102_REG_EXTEND_ADC2);
1514 int ext3 = lm85_read_value(client,
1515 EMC6D102_REG_EXTEND_ADC3);
1516 int ext4 = lm85_read_value(client,
1517 EMC6D102_REG_EXTEND_ADC4);
1518 data->in_ext[0] = ext3 & 0x0f;
1519 data->in_ext[1] = ext4 & 0x0f;
1520 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1521 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1522 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1523
1524 data->temp_ext[0] = ext1 & 0x0f;
1525 data->temp_ext[1] = ext2 & 0x0f;
1526 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1527 }
1528
1529 data->last_reading = jiffies ;
1530 }; /* last_reading */
1531
1532 if ( !data->valid ||
1533 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1534 /* Things that don't change often */
1535 dev_dbg(&client->dev, "Reading config values\n");
1536
Jean Delvare9c516ef2005-11-26 20:07:54 +01001537 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 data->in_min[i] =
1539 lm85_read_value(client, LM85_REG_IN_MIN(i));
1540 data->in_max[i] =
1541 lm85_read_value(client, LM85_REG_IN_MAX(i));
1542 }
1543
Jean Delvare9c516ef2005-11-26 20:07:54 +01001544 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1545 data->in_min[4] = lm85_read_value(client,
1546 LM85_REG_IN_MIN(4));
1547 data->in_max[4] = lm85_read_value(client,
1548 LM85_REG_IN_MAX(4));
1549 }
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if ( data->type == emc6d100 ) {
1552 for (i = 5; i <= 7; ++i) {
1553 data->in_min[i] =
1554 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1555 data->in_max[i] =
1556 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1557 }
1558 }
1559
1560 for (i = 0; i <= 3; ++i) {
1561 data->fan_min[i] =
1562 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1563 }
1564
1565 for (i = 0; i <= 2; ++i) {
1566 data->temp_min[i] =
1567 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1568 data->temp_max[i] =
1569 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1570 }
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 for (i = 0; i <= 2; ++i) {
1573 int val ;
1574 data->autofan[i].config =
1575 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1576 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1577 data->autofan[i].freq = val & 0x07 ;
1578 data->zone[i].range = (val >> 4) & 0x0f ;
1579 data->autofan[i].min_pwm =
1580 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1581 data->zone[i].limit =
1582 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1583 data->zone[i].critical =
1584 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1585 }
1586
1587 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1588 data->smooth[0] = i & 0x0f ;
1589 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1590 data->autofan[0].min_off = (i & 0x20) != 0 ;
1591 data->autofan[1].min_off = (i & 0x40) != 0 ;
1592 data->autofan[2].min_off = (i & 0x80) != 0 ;
1593 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1594 data->smooth[1] = (i>>4) & 0x0f ;
1595 data->smooth[2] = i & 0x0f ;
1596
1597 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1598 data->zone[0].hyst = (i>>4) & 0x0f ;
1599 data->zone[1].hyst = i & 0x0f ;
1600
1601 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1602 data->zone[2].hyst = (i>>4) & 0x0f ;
1603
1604 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1605 data->tach_mode = lm85_read_value(client,
1606 LM85_REG_TACH_MODE );
1607 data->spinup_ctl = lm85_read_value(client,
1608 LM85_REG_SPINUP_CTL );
1609 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1610 if ( data->type == adt7463 ) {
1611 for (i = 0; i <= 2; ++i) {
1612 data->oppoint[i] = lm85_read_value(client,
1613 ADT7463_REG_OPPOINT(i) );
1614 }
1615 data->tmin_ctl = lm85_read_value(client,
1616 ADT7463_REG_TMIN_CTL1 );
1617 data->therm_limit = lm85_read_value(client,
1618 ADT7463_REG_THERM_LIMIT );
1619 }
1620 for (i = 0; i <= 2; ++i) {
1621 data->temp_offset[i] = lm85_read_value(client,
1622 ADM1027_REG_TEMP_OFFSET(i) );
1623 }
1624 data->tach_mode = lm85_read_value(client,
1625 ADM1027_REG_CONFIG3 );
1626 data->fan_ppr = lm85_read_value(client,
1627 ADM1027_REG_FAN_PPR );
1628 }
1629
1630 data->last_config = jiffies;
1631 }; /* last_config */
1632
1633 data->valid = 1;
1634
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001635 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 return data;
1638}
1639
1640
1641static int __init sm_lm85_init(void)
1642{
1643 return i2c_add_driver(&lm85_driver);
1644}
1645
1646static void __exit sm_lm85_exit(void)
1647{
1648 i2c_del_driver(&lm85_driver);
1649}
1650
1651/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1652 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1653 * post 2.7.0 CVS changes.
1654 */
1655MODULE_LICENSE("GPL");
1656MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1657MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1658
1659module_init(sm_lm85_init);
1660module_exit(sm_lm85_exit);