blob: ee5eca1c1921d9d1f7ba1d67bd638ed9d6ba2cb2 [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 */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050038static const 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
Jean Delvared38b1492008-04-03 10:40:39 +0200195 if (range >= lm85_range_map[15])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return 15 ;
Jean Delvared38b1492008-04-03 10:40:39 +0200197
198 /* Find the closest match */
199 for (i = 14; i >= 0; --i) {
200 if (range >= lm85_range_map[i]) {
201 if ((lm85_range_map[i + 1] - range) <
202 (range - lm85_range_map[i]))
203 return i + 1;
204 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 }
Jean Delvared38b1492008-04-03 10:40:39 +0200207
208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210#define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
211
212/* These are the Acoustic Enhancement, or Temperature smoothing encodings
213 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
214 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
215 * is ignored, or set to 0.
216 */
217/* These are the PWM frequency encodings */
218static int lm85_freq_map[] = { /* .1 Hz */
219 100, 150, 230, 300, 380, 470, 620, 940
220 };
221static int FREQ_TO_REG( int freq )
222{
223 int i;
224
225 if( freq >= lm85_freq_map[7] ) { return 7 ; }
226 for( i = 0 ; i < 7 ; ++i )
227 if( freq <= lm85_freq_map[i] )
228 break ;
229 return( i & 0x07 );
230}
231#define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
232
233/* Since we can't use strings, I'm abusing these numbers
234 * to stand in for the following meanings:
235 * 1 -- PWM responds to Zone 1
236 * 2 -- PWM responds to Zone 2
237 * 3 -- PWM responds to Zone 3
238 * 23 -- PWM responds to the higher temp of Zone 2 or 3
239 * 123 -- PWM responds to highest of Zone 1, 2, or 3
240 * 0 -- PWM is always at 0% (ie, off)
241 * -1 -- PWM is always at 100%
242 * -2 -- PWM responds to manual control
243 */
244
245static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
246#define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
247
248static int ZONE_TO_REG( int zone )
249{
250 int i;
251
252 for( i = 0 ; i <= 7 ; ++i )
253 if( zone == lm85_zone_map[i] )
254 break ;
255 if( i > 7 ) /* Not found. */
256 i = 3; /* Always 100% */
257 return( (i & 0x07)<<5 );
258}
259
260#define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
261#define HYST_FROM_REG(val) ((val)*1000)
262
263#define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
264#define OFFSET_FROM_REG(val) ((val)*25)
265
266#define PPR_MASK(fan) (0x03<<(fan *2))
267#define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
268#define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/* Chip sampling rates
271 *
272 * Some sensors are not updated more frequently than once per second
273 * so it doesn't make sense to read them more often than that.
274 * We cache the results and return the saved data if the driver
275 * is called again before a second has elapsed.
276 *
277 * Also, there is significant configuration data for this chip
278 * given the automatic PWM fan control that is possible. There
279 * are about 47 bytes of config data to only 22 bytes of actual
280 * readings. So, we keep the config data up to date in the cache
281 * when it is written and only sample it once every 1 *minute*
282 */
283#define LM85_DATA_INTERVAL (HZ + HZ / 2)
284#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/* LM85 can automatically adjust fan speeds based on temperature
287 * This structure encapsulates an entire Zone config. There are
288 * three zones (one for each temperature input) on the lm85
289 */
290struct lm85_zone {
291 s8 limit; /* Low temp limit */
292 u8 hyst; /* Low limit hysteresis. (0-15) */
293 u8 range; /* Temp range, encoded */
294 s8 critical; /* "All fans ON" temp limit */
295 u8 off_desired; /* Actual "off" temperature specified. Preserved
296 * to prevent "drift" as other autofan control
297 * values change.
298 */
299 u8 max_desired; /* Actual "max" temperature specified. Preserved
300 * to prevent "drift" as other autofan control
301 * values change.
302 */
303};
304
305struct lm85_autofan {
306 u8 config; /* Register value */
307 u8 freq; /* PWM frequency, encoded */
308 u8 min_pwm; /* Minimum PWM value, encoded */
309 u8 min_off; /* Min PWM or OFF below "limit", flag */
310};
311
Jean Delvareed6bafb2007-02-14 21:15:03 +0100312/* For each registered chip, we need to keep some data in memory.
313 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314struct lm85_data {
315 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700316 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 enum chips type;
318
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100319 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int valid; /* !=0 if following fields are valid */
321 unsigned long last_reading; /* In jiffies */
322 unsigned long last_config; /* In jiffies */
323
324 u8 in[8]; /* Register value */
325 u8 in_max[8]; /* Register value */
326 u8 in_min[8]; /* Register value */
327 s8 temp[3]; /* Register value */
328 s8 temp_min[3]; /* Register value */
329 s8 temp_max[3]; /* Register value */
330 s8 temp_offset[3]; /* Register value */
331 u16 fan[4]; /* Register value */
332 u16 fan_min[4]; /* Register value */
333 u8 pwm[3]; /* Register value */
334 u8 spinup_ctl; /* Register encoding, combined */
335 u8 tach_mode; /* Register encoding, combined */
336 u8 temp_ext[3]; /* Decoded values */
337 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 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 .attach_adapter = lm85_attach_adapter,
368 .detach_client = lm85_detach_client,
369};
370
371
372/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200373static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
374 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Jean Delvareb353a482007-07-05 20:36:12 +0200376 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct lm85_data *data = lm85_update_device(dev);
378 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
379}
Jean Delvareb353a482007-07-05 20:36:12 +0200380
381static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
382 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Jean Delvareb353a482007-07-05 20:36:12 +0200384 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct lm85_data *data = lm85_update_device(dev);
386 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
387}
Jean Delvareb353a482007-07-05 20:36:12 +0200388
389static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jean Delvareb353a482007-07-05 20:36:12 +0200392 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct i2c_client *client = to_i2c_client(dev);
394 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200395 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100397 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 data->fan_min[nr] = FAN_TO_REG(val);
399 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100400 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return count;
402}
403
404#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200405static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
406 show_fan, NULL, offset - 1); \
407static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
408 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410show_fan_offset(1);
411show_fan_offset(2);
412show_fan_offset(3);
413show_fan_offset(4);
414
415/* vid, vrm, alarms */
416
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400417static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100420 int vid;
421
422 if (data->type == adt7463 && (data->vid & 0x80)) {
423 /* 6-pin VID (VRM 10) */
424 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
425 } else {
426 /* 5-pin VID (VRM 9) */
427 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
428 }
429
430 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
434
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400435static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Jean Delvare90d66192007-10-08 18:24:35 +0200437 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return sprintf(buf, "%ld\n", (long) data->vrm);
439}
440
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400441static 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 -0700442{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100443 struct lm85_data *data = dev_get_drvdata(dev);
444 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return count;
446}
447
448static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
449
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400450static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200453 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
457
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200458static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
459 char *buf)
460{
461 int nr = to_sensor_dev_attr(attr)->index;
462 struct lm85_data *data = lm85_update_device(dev);
463 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
464}
465
466static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
467static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
468static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
469static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
470static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
471static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
472static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
473static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
474static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
475static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
476static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
477static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
478static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
479static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
480static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
481static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
482static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* pwm */
485
Jean Delvareb353a482007-07-05 20:36:12 +0200486static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
487 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jean Delvareb353a482007-07-05 20:36:12 +0200489 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct lm85_data *data = lm85_update_device(dev);
491 return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
492}
Jean Delvareb353a482007-07-05 20:36:12 +0200493
494static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
499 struct lm85_data *data = i2c_get_clientdata(client);
500 long val = simple_strtol(buf, NULL, 10);
501
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100502 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 data->pwm[nr] = PWM_TO_REG(val);
504 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100505 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return count;
507}
Jean Delvareb353a482007-07-05 20:36:12 +0200508
509static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
510 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Jean Delvareb353a482007-07-05 20:36:12 +0200512 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100514 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100517 switch (pwm_zone) {
518 case -1: /* PWM is always at 100% */
519 enable = 0;
520 break;
521 case 0: /* PWM is always at 0% */
522 case -2: /* PWM responds to manual control */
523 enable = 1;
524 break;
525 default: /* PWM in automatic mode */
526 enable = 2;
527 }
528 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Jean Delvare455f7912007-12-03 23:28:42 +0100531static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
532 *attr, const char *buf, size_t count)
533{
534 int nr = to_sensor_dev_attr(attr)->index;
535 struct i2c_client *client = to_i2c_client(dev);
536 struct lm85_data *data = i2c_get_clientdata(client);
537 long val = simple_strtol(buf, NULL, 10);
538 u8 config;
539
540 switch (val) {
541 case 0:
542 config = 3;
543 break;
544 case 1:
545 config = 7;
546 break;
547 case 2:
548 /* Here we have to choose arbitrarily one of the 5 possible
549 configurations; I go for the safest */
550 config = 6;
551 break;
552 default:
553 return -EINVAL;
554 }
555
556 mutex_lock(&data->update_lock);
557 data->autofan[nr].config = lm85_read_value(client,
558 LM85_REG_AFAN_CONFIG(nr));
559 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
560 | (config << 5);
561 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
562 data->autofan[nr].config);
563 mutex_unlock(&data->update_lock);
564 return count;
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200568static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
569 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100570static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
571 show_pwm_enable, set_pwm_enable, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573show_pwm_reg(1);
574show_pwm_reg(2);
575show_pwm_reg(3);
576
577/* Voltages */
578
Jean Delvareb353a482007-07-05 20:36:12 +0200579static ssize_t show_in(struct device *dev, struct device_attribute *attr,
580 char *buf)
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 lm85_data *data = lm85_update_device(dev);
584 return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
585 data->in[nr],
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200586 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Jean Delvareb353a482007-07-05 20:36:12 +0200588
589static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
590 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Jean Delvareb353a482007-07-05 20:36:12 +0200592 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct lm85_data *data = lm85_update_device(dev);
594 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
595}
Jean Delvareb353a482007-07-05 20:36:12 +0200596
597static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
598 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
602 struct lm85_data *data = i2c_get_clientdata(client);
603 long val = simple_strtol(buf, NULL, 10);
604
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100605 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 data->in_min[nr] = INS_TO_REG(nr, val);
607 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100608 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return count;
610}
Jean Delvareb353a482007-07-05 20:36:12 +0200611
612static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
613 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Jean Delvareb353a482007-07-05 20:36:12 +0200615 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct lm85_data *data = lm85_update_device(dev);
617 return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
618}
Jean Delvareb353a482007-07-05 20:36:12 +0200619
620static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
621 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
625 struct lm85_data *data = i2c_get_clientdata(client);
626 long val = simple_strtol(buf, NULL, 10);
627
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100628 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 data->in_max[nr] = INS_TO_REG(nr, val);
630 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return count;
633}
Jean Delvareb353a482007-07-05 20:36:12 +0200634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200636static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
637 show_in, NULL, offset); \
638static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
639 show_in_min, set_in_min, offset); \
640static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
641 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643show_in_reg(0);
644show_in_reg(1);
645show_in_reg(2);
646show_in_reg(3);
647show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200648show_in_reg(5);
649show_in_reg(6);
650show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/* Temps */
653
Jean Delvareb353a482007-07-05 20:36:12 +0200654static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
655 char *buf)
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 lm85_data *data = lm85_update_device(dev);
659 return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200660 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
Jean Delvareb353a482007-07-05 20:36:12 +0200662
663static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
664 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Jean Delvareb353a482007-07-05 20:36:12 +0200666 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct lm85_data *data = lm85_update_device(dev);
668 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
669}
Jean Delvareb353a482007-07-05 20:36:12 +0200670
671static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
676 struct lm85_data *data = i2c_get_clientdata(client);
677 long val = simple_strtol(buf, NULL, 10);
678
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100679 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 data->temp_min[nr] = TEMP_TO_REG(val);
681 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100682 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return count;
684}
Jean Delvareb353a482007-07-05 20:36:12 +0200685
686static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
687 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Jean Delvareb353a482007-07-05 20:36:12 +0200689 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct lm85_data *data = lm85_update_device(dev);
691 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
692}
Jean Delvareb353a482007-07-05 20:36:12 +0200693
694static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
699 struct lm85_data *data = i2c_get_clientdata(client);
700 long val = simple_strtol(buf, NULL, 10);
701
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100702 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 data->temp_max[nr] = TEMP_TO_REG(val);
704 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200710static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
711 show_temp, NULL, offset - 1); \
712static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
713 show_temp_min, set_temp_min, offset - 1); \
714static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
715 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717show_temp_reg(1);
718show_temp_reg(2);
719show_temp_reg(3);
720
721
722/* Automatic PWM control */
723
Jean Delvareb353a482007-07-05 20:36:12 +0200724static ssize_t show_pwm_auto_channels(struct device *dev,
725 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Jean Delvareb353a482007-07-05 20:36:12 +0200727 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct lm85_data *data = lm85_update_device(dev);
729 return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
730}
Jean Delvareb353a482007-07-05 20:36:12 +0200731
732static ssize_t set_pwm_auto_channels(struct device *dev,
733 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
737 struct lm85_data *data = i2c_get_clientdata(client);
738 long val = simple_strtol(buf, NULL, 10);
739
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100740 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
742 | ZONE_TO_REG(val) ;
743 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
744 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100745 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return count;
747}
Jean Delvareb353a482007-07-05 20:36:12 +0200748
749static ssize_t show_pwm_auto_pwm_min(struct device *dev,
750 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Jean Delvareb353a482007-07-05 20:36:12 +0200752 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct lm85_data *data = lm85_update_device(dev);
754 return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
755}
Jean Delvareb353a482007-07-05 20:36:12 +0200756
757static ssize_t set_pwm_auto_pwm_min(struct device *dev,
758 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
762 struct lm85_data *data = i2c_get_clientdata(client);
763 long val = simple_strtol(buf, NULL, 10);
764
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100765 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 data->autofan[nr].min_pwm = PWM_TO_REG(val);
767 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
768 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100769 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return count;
771}
Jean Delvareb353a482007-07-05 20:36:12 +0200772
773static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
774 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jean Delvareb353a482007-07-05 20:36:12 +0200776 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct lm85_data *data = lm85_update_device(dev);
778 return sprintf(buf,"%d\n", data->autofan[nr].min_off);
779}
Jean Delvareb353a482007-07-05 20:36:12 +0200780
781static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
782 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
786 struct lm85_data *data = i2c_get_clientdata(client);
787 long val = simple_strtol(buf, NULL, 10);
788
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100789 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 data->autofan[nr].min_off = val;
791 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
792 | data->syncpwm3
793 | (data->autofan[0].min_off ? 0x20 : 0)
794 | (data->autofan[1].min_off ? 0x40 : 0)
795 | (data->autofan[2].min_off ? 0x80 : 0)
796 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100797 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return count;
799}
Jean Delvareb353a482007-07-05 20:36:12 +0200800
801static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
802 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Jean Delvareb353a482007-07-05 20:36:12 +0200804 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct lm85_data *data = lm85_update_device(dev);
806 return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
807}
Jean Delvareb353a482007-07-05 20:36:12 +0200808
809static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
810 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
814 struct lm85_data *data = i2c_get_clientdata(client);
815 long val = simple_strtol(buf, NULL, 10);
816
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100817 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 data->autofan[nr].freq = FREQ_TO_REG(val);
819 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
820 (data->zone[nr].range << 4)
821 | data->autofan[nr].freq
822 );
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100823 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return count;
825}
Jean Delvareb353a482007-07-05 20:36:12 +0200826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200828static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
829 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
830 set_pwm_auto_channels, offset - 1); \
831static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
832 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
833 set_pwm_auto_pwm_min, offset - 1); \
834static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
835 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
836 set_pwm_auto_pwm_minctl, offset - 1); \
837static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
838 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
839 set_pwm_auto_pwm_freq, offset - 1);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841pwm_auto(1);
842pwm_auto(2);
843pwm_auto(3);
844
845/* Temperature settings for automatic PWM control */
846
Jean Delvareb353a482007-07-05 20:36:12 +0200847static ssize_t show_temp_auto_temp_off(struct device *dev,
848 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Jean Delvareb353a482007-07-05 20:36:12 +0200850 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct lm85_data *data = lm85_update_device(dev);
852 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
853 HYST_FROM_REG(data->zone[nr].hyst));
854}
Jean Delvareb353a482007-07-05 20:36:12 +0200855
856static ssize_t set_temp_auto_temp_off(struct device *dev,
857 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Jean Delvareb353a482007-07-05 20:36:12 +0200859 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct i2c_client *client = to_i2c_client(dev);
861 struct lm85_data *data = i2c_get_clientdata(client);
862 int min;
863 long val = simple_strtol(buf, NULL, 10);
864
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100865 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 min = TEMP_FROM_REG(data->zone[nr].limit);
867 data->zone[nr].off_desired = TEMP_TO_REG(val);
868 data->zone[nr].hyst = HYST_TO_REG(min - val);
869 if ( nr == 0 || nr == 1 ) {
870 lm85_write_value(client, LM85_REG_AFAN_HYST1,
871 (data->zone[0].hyst << 4)
872 | data->zone[1].hyst
873 );
874 } else {
875 lm85_write_value(client, LM85_REG_AFAN_HYST2,
876 (data->zone[2].hyst << 4)
877 );
878 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100879 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return count;
881}
Jean Delvareb353a482007-07-05 20:36:12 +0200882
883static ssize_t show_temp_auto_temp_min(struct device *dev,
884 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Jean Delvareb353a482007-07-05 20:36:12 +0200886 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct lm85_data *data = lm85_update_device(dev);
888 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
889}
Jean Delvareb353a482007-07-05 20:36:12 +0200890
891static ssize_t set_temp_auto_temp_min(struct device *dev,
892 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
896 struct lm85_data *data = i2c_get_clientdata(client);
897 long val = simple_strtol(buf, NULL, 10);
898
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100899 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 data->zone[nr].limit = TEMP_TO_REG(val);
901 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
902 data->zone[nr].limit);
903
904/* Update temp_auto_max and temp_auto_range */
905 data->zone[nr].range = RANGE_TO_REG(
906 TEMP_FROM_REG(data->zone[nr].max_desired) -
907 TEMP_FROM_REG(data->zone[nr].limit));
908 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
909 ((data->zone[nr].range & 0x0f) << 4)
910 | (data->autofan[nr].freq & 0x07));
911
912/* Update temp_auto_hyst and temp_auto_off */
913 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
914 data->zone[nr].limit) - TEMP_FROM_REG(
915 data->zone[nr].off_desired));
916 if ( nr == 0 || nr == 1 ) {
917 lm85_write_value(client, LM85_REG_AFAN_HYST1,
918 (data->zone[0].hyst << 4)
919 | data->zone[1].hyst
920 );
921 } else {
922 lm85_write_value(client, LM85_REG_AFAN_HYST2,
923 (data->zone[2].hyst << 4)
924 );
925 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100926 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return count;
928}
Jean Delvareb353a482007-07-05 20:36:12 +0200929
930static ssize_t show_temp_auto_temp_max(struct device *dev,
931 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Jean Delvareb353a482007-07-05 20:36:12 +0200933 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct lm85_data *data = lm85_update_device(dev);
935 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
936 RANGE_FROM_REG(data->zone[nr].range));
937}
Jean Delvareb353a482007-07-05 20:36:12 +0200938
939static ssize_t set_temp_auto_temp_max(struct device *dev,
940 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jean Delvareb353a482007-07-05 20:36:12 +0200942 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct i2c_client *client = to_i2c_client(dev);
944 struct lm85_data *data = i2c_get_clientdata(client);
945 int min;
946 long val = simple_strtol(buf, NULL, 10);
947
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100948 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 min = TEMP_FROM_REG(data->zone[nr].limit);
950 data->zone[nr].max_desired = TEMP_TO_REG(val);
951 data->zone[nr].range = RANGE_TO_REG(
952 val - min);
953 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
954 ((data->zone[nr].range & 0x0f) << 4)
955 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100956 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return count;
958}
Jean Delvareb353a482007-07-05 20:36:12 +0200959
960static ssize_t show_temp_auto_temp_crit(struct device *dev,
961 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jean Delvareb353a482007-07-05 20:36:12 +0200963 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct lm85_data *data = lm85_update_device(dev);
965 return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
966}
Jean Delvareb353a482007-07-05 20:36:12 +0200967
968static ssize_t set_temp_auto_temp_crit(struct device *dev,
969 struct device_attribute *attr,const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
973 struct lm85_data *data = i2c_get_clientdata(client);
974 long val = simple_strtol(buf, NULL, 10);
975
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100976 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 data->zone[nr].critical = TEMP_TO_REG(val);
978 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
979 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100980 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return count;
982}
Jean Delvareb353a482007-07-05 20:36:12 +0200983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200985static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
986 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
987 set_temp_auto_temp_off, offset - 1); \
988static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
989 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
990 set_temp_auto_temp_min, offset - 1); \
991static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
992 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
993 set_temp_auto_temp_max, offset - 1); \
994static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
995 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
996 set_temp_auto_temp_crit, offset - 1);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998temp_auto(1);
999temp_auto(2);
1000temp_auto(3);
1001
Ben Dooksd8d20612005-10-26 21:05:46 +02001002static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 if (!(adapter->class & I2C_CLASS_HWMON))
1005 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +02001006 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001009static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001010 &sensor_dev_attr_fan1_input.dev_attr.attr,
1011 &sensor_dev_attr_fan2_input.dev_attr.attr,
1012 &sensor_dev_attr_fan3_input.dev_attr.attr,
1013 &sensor_dev_attr_fan4_input.dev_attr.attr,
1014 &sensor_dev_attr_fan1_min.dev_attr.attr,
1015 &sensor_dev_attr_fan2_min.dev_attr.attr,
1016 &sensor_dev_attr_fan3_min.dev_attr.attr,
1017 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001018 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1019 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1020 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1021 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001022
1023 &sensor_dev_attr_pwm1.dev_attr.attr,
1024 &sensor_dev_attr_pwm2.dev_attr.attr,
1025 &sensor_dev_attr_pwm3.dev_attr.attr,
1026 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1027 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1028 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1029
1030 &sensor_dev_attr_in0_input.dev_attr.attr,
1031 &sensor_dev_attr_in1_input.dev_attr.attr,
1032 &sensor_dev_attr_in2_input.dev_attr.attr,
1033 &sensor_dev_attr_in3_input.dev_attr.attr,
1034 &sensor_dev_attr_in0_min.dev_attr.attr,
1035 &sensor_dev_attr_in1_min.dev_attr.attr,
1036 &sensor_dev_attr_in2_min.dev_attr.attr,
1037 &sensor_dev_attr_in3_min.dev_attr.attr,
1038 &sensor_dev_attr_in0_max.dev_attr.attr,
1039 &sensor_dev_attr_in1_max.dev_attr.attr,
1040 &sensor_dev_attr_in2_max.dev_attr.attr,
1041 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001042 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1043 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1044 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1045 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001046
1047 &sensor_dev_attr_temp1_input.dev_attr.attr,
1048 &sensor_dev_attr_temp2_input.dev_attr.attr,
1049 &sensor_dev_attr_temp3_input.dev_attr.attr,
1050 &sensor_dev_attr_temp1_min.dev_attr.attr,
1051 &sensor_dev_attr_temp2_min.dev_attr.attr,
1052 &sensor_dev_attr_temp3_min.dev_attr.attr,
1053 &sensor_dev_attr_temp1_max.dev_attr.attr,
1054 &sensor_dev_attr_temp2_max.dev_attr.attr,
1055 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001056 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1057 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1058 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1059 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1060 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001061
1062 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1063 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1064 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1065 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1066 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1067 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1068 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1069 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1070 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1071 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1072 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1073 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1074
1075 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1076 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1077 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1078 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1079 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1080 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1081 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1082 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1083 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1084 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1085 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1086 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1087
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001088 &dev_attr_vrm.attr,
1089 &dev_attr_cpu0_vid.attr,
1090 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001091 NULL
1092};
1093
1094static const struct attribute_group lm85_group = {
1095 .attrs = lm85_attributes,
1096};
1097
Jean Delvare6b9aad22007-07-05 20:37:21 +02001098static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001099 &sensor_dev_attr_in4_input.dev_attr.attr,
1100 &sensor_dev_attr_in4_min.dev_attr.attr,
1101 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001102 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001103 NULL
1104};
1105
Jean Delvare6b9aad22007-07-05 20:37:21 +02001106static const struct attribute_group lm85_group_in4 = {
1107 .attrs = lm85_attributes_in4,
1108};
1109
1110static struct attribute *lm85_attributes_in567[] = {
1111 &sensor_dev_attr_in5_input.dev_attr.attr,
1112 &sensor_dev_attr_in6_input.dev_attr.attr,
1113 &sensor_dev_attr_in7_input.dev_attr.attr,
1114 &sensor_dev_attr_in5_min.dev_attr.attr,
1115 &sensor_dev_attr_in6_min.dev_attr.attr,
1116 &sensor_dev_attr_in7_min.dev_attr.attr,
1117 &sensor_dev_attr_in5_max.dev_attr.attr,
1118 &sensor_dev_attr_in6_max.dev_attr.attr,
1119 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001120 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1121 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1122 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001123 NULL
1124};
1125
1126static const struct attribute_group lm85_group_in567 = {
1127 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001128};
1129
Ben Dooksd8d20612005-10-26 21:05:46 +02001130static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int kind)
1132{
1133 int company, verstep ;
1134 struct i2c_client *new_client = NULL;
1135 struct lm85_data *data;
1136 int err = 0;
1137 const char *type_name = "";
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!i2c_check_functionality(adapter,
1140 I2C_FUNC_SMBUS_BYTE_DATA)) {
1141 /* We need to be able to do byte I/O */
1142 goto ERROR0 ;
1143 };
1144
1145 /* OK. For now, we presume we have a valid client. We now create the
1146 client structure, even though we cannot fill it completely yet.
1147 But it allows us to access lm85_{read,write}_value. */
1148
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001149 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 err = -ENOMEM;
1151 goto ERROR0;
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 new_client = &data->client;
1155 i2c_set_clientdata(new_client, data);
1156 new_client->addr = address;
1157 new_client->adapter = adapter;
1158 new_client->driver = &lm85_driver;
1159 new_client->flags = 0;
1160
1161 /* Now, we do the remaining detection. */
1162
1163 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1164 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1165
1166 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1167 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1168 i2c_adapter_id(new_client->adapter), new_client->addr,
1169 company, verstep);
1170
1171 /* If auto-detecting, Determine the chip type. */
1172 if (kind <= 0) {
1173 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
1174 i2c_adapter_id(adapter), address );
1175 if( company == LM85_COMPANY_NATIONAL
1176 && verstep == LM85_VERSTEP_LM85C ) {
1177 kind = lm85c ;
1178 } else if( company == LM85_COMPANY_NATIONAL
1179 && verstep == LM85_VERSTEP_LM85B ) {
1180 kind = lm85b ;
1181 } else if( company == LM85_COMPANY_NATIONAL
1182 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1183 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1184 " Defaulting to LM85.\n", verstep);
1185 kind = any_chip ;
1186 } else if( company == LM85_COMPANY_ANALOG_DEV
1187 && verstep == LM85_VERSTEP_ADM1027 ) {
1188 kind = adm1027 ;
1189 } else if( company == LM85_COMPANY_ANALOG_DEV
1190 && (verstep == LM85_VERSTEP_ADT7463
1191 || verstep == LM85_VERSTEP_ADT7463C) ) {
1192 kind = adt7463 ;
1193 } else if( company == LM85_COMPANY_ANALOG_DEV
1194 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
1195 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1196 " Defaulting to Generic LM85.\n", verstep );
1197 kind = any_chip ;
1198 } else if( company == LM85_COMPANY_SMSC
1199 && (verstep == LM85_VERSTEP_EMC6D100_A0
1200 || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
1201 /* Unfortunately, we can't tell a '100 from a '101
1202 * from the registers. Since a '101 is a '100
1203 * in a package with fewer pins and therefore no
1204 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1205 * inputs read 0, then it's a '101.
1206 */
1207 kind = emc6d100 ;
1208 } else if( company == LM85_COMPANY_SMSC
1209 && verstep == LM85_VERSTEP_EMC6D102) {
1210 kind = emc6d102 ;
1211 } else if( company == LM85_COMPANY_SMSC
1212 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1213 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1214 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
1215 " Defaulting to Generic LM85.\n", verstep );
1216 kind = any_chip ;
1217 } else if( kind == any_chip
1218 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1219 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1220 /* Leave kind as "any_chip" */
1221 } else {
1222 dev_dbg(&adapter->dev, "Autodetection failed\n");
1223 /* Not an LM85 ... */
1224 if( kind == any_chip ) { /* User used force=x,y */
1225 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1226 " found at %d,0x%02x. Try force_lm85c.\n",
1227 i2c_adapter_id(adapter), address );
1228 }
1229 err = 0 ;
1230 goto ERROR1;
1231 }
1232 }
1233
1234 /* Fill in the chip specific driver values */
1235 if ( kind == any_chip ) {
1236 type_name = "lm85";
1237 } else if ( kind == lm85b ) {
1238 type_name = "lm85b";
1239 } else if ( kind == lm85c ) {
1240 type_name = "lm85c";
1241 } else if ( kind == adm1027 ) {
1242 type_name = "adm1027";
1243 } else if ( kind == adt7463 ) {
1244 type_name = "adt7463";
1245 } else if ( kind == emc6d100){
1246 type_name = "emc6d100";
1247 } else if ( kind == emc6d102 ) {
1248 type_name = "emc6d102";
1249 }
1250 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1251
1252 /* Fill in the remaining client fields */
1253 data->type = kind;
1254 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001255 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* Tell the I2C layer a new client has arrived */
1258 if ((err = i2c_attach_client(new_client)))
1259 goto ERROR1;
1260
1261 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001262 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Initialize the LM85 chip */
1265 lm85_init_client(new_client);
1266
1267 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001268 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001269 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jean Delvare9c516ef2005-11-26 20:07:54 +01001271 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1272 as a sixth digital VID input rather than an analog input. */
1273 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001274 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001275 if ((err = sysfs_create_group(&new_client->dev.kobj,
1276 &lm85_group_in4)))
1277 goto ERROR3;
1278
1279 /* The EMC6D100 has 3 additional voltage inputs */
1280 if (kind == emc6d100)
1281 if ((err = sysfs_create_group(&new_client->dev.kobj,
1282 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001283 goto ERROR3;
1284
Tony Jones1beeffe2007-08-20 13:46:20 -07001285 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1286 if (IS_ERR(data->hwmon_dev)) {
1287 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001288 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001289 }
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292
1293 /* Error out and cleanup code */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001294 ERROR3:
1295 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001296 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1297 if (kind == emc6d100)
1298 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001299 ERROR2:
1300 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 ERROR1:
1302 kfree(data);
1303 ERROR0:
1304 return err;
1305}
1306
Ben Dooksd8d20612005-10-26 21:05:46 +02001307static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001309 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001310 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001311 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001312 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1313 if (data->type == emc6d100)
1314 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001316 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 0;
1318}
1319
1320
Ben Dooksd8d20612005-10-26 21:05:46 +02001321static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int res;
1324
1325 /* What size location is it? */
1326 switch( reg ) {
1327 case LM85_REG_FAN(0) : /* Read WORD data */
1328 case LM85_REG_FAN(1) :
1329 case LM85_REG_FAN(2) :
1330 case LM85_REG_FAN(3) :
1331 case LM85_REG_FAN_MIN(0) :
1332 case LM85_REG_FAN_MIN(1) :
1333 case LM85_REG_FAN_MIN(2) :
1334 case LM85_REG_FAN_MIN(3) :
1335 case LM85_REG_ALARM1 : /* Read both bytes at once */
1336 res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
1337 res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
1338 break ;
1339 case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
1340 res = i2c_smbus_read_byte_data(client, reg) << 8 ;
1341 res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
1342 break ;
1343 default: /* Read BYTE data */
1344 res = i2c_smbus_read_byte_data(client, reg);
1345 break ;
1346 }
1347
1348 return res ;
1349}
1350
Ben Dooksd8d20612005-10-26 21:05:46 +02001351static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 int res ;
1354
1355 switch( reg ) {
1356 case LM85_REG_FAN(0) : /* Write WORD data */
1357 case LM85_REG_FAN(1) :
1358 case LM85_REG_FAN(2) :
1359 case LM85_REG_FAN(3) :
1360 case LM85_REG_FAN_MIN(0) :
1361 case LM85_REG_FAN_MIN(1) :
1362 case LM85_REG_FAN_MIN(2) :
1363 case LM85_REG_FAN_MIN(3) :
1364 /* NOTE: ALARM is read only, so not included here */
1365 res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
1366 res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
1367 break ;
1368 case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
1369 res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
1370 res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
1371 break ;
1372 default: /* Write BYTE data */
1373 res = i2c_smbus_write_byte_data(client, reg, value);
1374 break ;
1375 }
1376
1377 return res ;
1378}
1379
Ben Dooksd8d20612005-10-26 21:05:46 +02001380static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 int value;
1383 struct lm85_data *data = i2c_get_clientdata(client);
1384
1385 dev_dbg(&client->dev, "Initializing device\n");
1386
1387 /* Warn if part was not "READY" */
1388 value = lm85_read_value(client, LM85_REG_CONFIG);
1389 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
1390 if( value & 0x02 ) {
1391 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
1392 i2c_adapter_id(client->adapter), client->addr );
1393 };
1394 if( ! (value & 0x04) ) {
1395 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1396 i2c_adapter_id(client->adapter), client->addr );
1397 };
1398 if( value & 0x10
1399 && ( data->type == adm1027
1400 || data->type == adt7463 ) ) {
1401 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1402 "Please report this to the lm85 maintainer.\n",
1403 i2c_adapter_id(client->adapter), client->addr );
1404 };
1405
1406 /* WE INTENTIONALLY make no changes to the limits,
1407 * offsets, pwms, fans and zones. If they were
1408 * configured, we don't want to mess with them.
1409 * If they weren't, the default is 100% PWM, no
1410 * control and will suffice until 'sensors -s'
1411 * can be run by the user.
1412 */
1413
1414 /* Start monitoring */
1415 value = lm85_read_value(client, LM85_REG_CONFIG);
1416 /* Try to clear LOCK, Set START, save everything else */
1417 value = (value & ~ 0x02) | 0x01 ;
1418 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1419 lm85_write_value(client, LM85_REG_CONFIG, value);
1420}
1421
1422static struct lm85_data *lm85_update_device(struct device *dev)
1423{
1424 struct i2c_client *client = to_i2c_client(dev);
1425 struct lm85_data *data = i2c_get_clientdata(client);
1426 int i;
1427
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001428 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if ( !data->valid ||
1431 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
1432 /* Things that change quickly */
1433 dev_dbg(&client->dev, "Reading sensor values\n");
1434
1435 /* Have to read extended bits first to "freeze" the
1436 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001437 * There are 2 additional resolution bits per channel and we
1438 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 */
1440 if ( (data->type == adm1027) || (data->type == adt7463) ) {
1441 int ext1 = lm85_read_value(client,
1442 ADM1027_REG_EXTEND_ADC1);
1443 int ext2 = lm85_read_value(client,
1444 ADM1027_REG_EXTEND_ADC2);
1445 int val = (ext1 << 8) + ext2;
1446
1447 for(i = 0; i <= 4; i++)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001448 data->in_ext[i] = ((val>>(i * 2))&0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 for(i = 0; i <= 2; i++)
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001451 data->temp_ext[i] = (val>>((i + 4) * 2))&0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Jean Delvare9c516ef2005-11-26 20:07:54 +01001454 data->vid = lm85_read_value(client, LM85_REG_VID);
1455
1456 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 data->in[i] =
1458 lm85_read_value(client, LM85_REG_IN(i));
1459 }
1460
Jean Delvare9c516ef2005-11-26 20:07:54 +01001461 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1462 data->in[4] = lm85_read_value(client,
1463 LM85_REG_IN(4));
1464 }
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 for (i = 0; i <= 3; ++i) {
1467 data->fan[i] =
1468 lm85_read_value(client, LM85_REG_FAN(i));
1469 }
1470
1471 for (i = 0; i <= 2; ++i) {
1472 data->temp[i] =
1473 lm85_read_value(client, LM85_REG_TEMP(i));
1474 }
1475
1476 for (i = 0; i <= 2; ++i) {
1477 data->pwm[i] =
1478 lm85_read_value(client, LM85_REG_PWM(i));
1479 }
1480
1481 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1482
1483 if ( data->type == adt7463 ) {
1484 if( data->therm_total < ULONG_MAX - 256 ) {
1485 data->therm_total +=
1486 lm85_read_value(client, ADT7463_REG_THERM );
1487 }
1488 } else if ( data->type == emc6d100 ) {
1489 /* Three more voltage sensors */
1490 for (i = 5; i <= 7; ++i) {
1491 data->in[i] =
1492 lm85_read_value(client, EMC6D100_REG_IN(i));
1493 }
1494 /* More alarm bits */
1495 data->alarms |=
1496 lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
1497 } else if (data->type == emc6d102 ) {
1498 /* Have to read LSB bits after the MSB ones because
1499 the reading of the MSB bits has frozen the
1500 LSBs (backward from the ADM1027).
1501 */
1502 int ext1 = lm85_read_value(client,
1503 EMC6D102_REG_EXTEND_ADC1);
1504 int ext2 = lm85_read_value(client,
1505 EMC6D102_REG_EXTEND_ADC2);
1506 int ext3 = lm85_read_value(client,
1507 EMC6D102_REG_EXTEND_ADC3);
1508 int ext4 = lm85_read_value(client,
1509 EMC6D102_REG_EXTEND_ADC4);
1510 data->in_ext[0] = ext3 & 0x0f;
1511 data->in_ext[1] = ext4 & 0x0f;
1512 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1513 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1514 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1515
1516 data->temp_ext[0] = ext1 & 0x0f;
1517 data->temp_ext[1] = ext2 & 0x0f;
1518 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1519 }
1520
1521 data->last_reading = jiffies ;
1522 }; /* last_reading */
1523
1524 if ( !data->valid ||
1525 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
1526 /* Things that don't change often */
1527 dev_dbg(&client->dev, "Reading config values\n");
1528
Jean Delvare9c516ef2005-11-26 20:07:54 +01001529 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 data->in_min[i] =
1531 lm85_read_value(client, LM85_REG_IN_MIN(i));
1532 data->in_max[i] =
1533 lm85_read_value(client, LM85_REG_IN_MAX(i));
1534 }
1535
Jean Delvare9c516ef2005-11-26 20:07:54 +01001536 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1537 data->in_min[4] = lm85_read_value(client,
1538 LM85_REG_IN_MIN(4));
1539 data->in_max[4] = lm85_read_value(client,
1540 LM85_REG_IN_MAX(4));
1541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if ( data->type == emc6d100 ) {
1544 for (i = 5; i <= 7; ++i) {
1545 data->in_min[i] =
1546 lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
1547 data->in_max[i] =
1548 lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
1549 }
1550 }
1551
1552 for (i = 0; i <= 3; ++i) {
1553 data->fan_min[i] =
1554 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1555 }
1556
1557 for (i = 0; i <= 2; ++i) {
1558 data->temp_min[i] =
1559 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1560 data->temp_max[i] =
1561 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1562 }
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 for (i = 0; i <= 2; ++i) {
1565 int val ;
1566 data->autofan[i].config =
1567 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1568 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1569 data->autofan[i].freq = val & 0x07 ;
1570 data->zone[i].range = (val >> 4) & 0x0f ;
1571 data->autofan[i].min_pwm =
1572 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1573 data->zone[i].limit =
1574 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1575 data->zone[i].critical =
1576 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1577 }
1578
1579 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1580 data->smooth[0] = i & 0x0f ;
1581 data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
1582 data->autofan[0].min_off = (i & 0x20) != 0 ;
1583 data->autofan[1].min_off = (i & 0x40) != 0 ;
1584 data->autofan[2].min_off = (i & 0x80) != 0 ;
1585 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
1586 data->smooth[1] = (i>>4) & 0x0f ;
1587 data->smooth[2] = i & 0x0f ;
1588
1589 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1590 data->zone[0].hyst = (i>>4) & 0x0f ;
1591 data->zone[1].hyst = i & 0x0f ;
1592
1593 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1594 data->zone[2].hyst = (i>>4) & 0x0f ;
1595
1596 if ( (data->type == lm85b) || (data->type == lm85c) ) {
1597 data->tach_mode = lm85_read_value(client,
1598 LM85_REG_TACH_MODE );
1599 data->spinup_ctl = lm85_read_value(client,
1600 LM85_REG_SPINUP_CTL );
1601 } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
1602 if ( data->type == adt7463 ) {
1603 for (i = 0; i <= 2; ++i) {
1604 data->oppoint[i] = lm85_read_value(client,
1605 ADT7463_REG_OPPOINT(i) );
1606 }
1607 data->tmin_ctl = lm85_read_value(client,
1608 ADT7463_REG_TMIN_CTL1 );
1609 data->therm_limit = lm85_read_value(client,
1610 ADT7463_REG_THERM_LIMIT );
1611 }
1612 for (i = 0; i <= 2; ++i) {
1613 data->temp_offset[i] = lm85_read_value(client,
1614 ADM1027_REG_TEMP_OFFSET(i) );
1615 }
1616 data->tach_mode = lm85_read_value(client,
1617 ADM1027_REG_CONFIG3 );
1618 data->fan_ppr = lm85_read_value(client,
1619 ADM1027_REG_FAN_PPR );
1620 }
1621
1622 data->last_config = jiffies;
1623 }; /* last_config */
1624
1625 data->valid = 1;
1626
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001627 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 return data;
1630}
1631
1632
1633static int __init sm_lm85_init(void)
1634{
1635 return i2c_add_driver(&lm85_driver);
1636}
1637
1638static void __exit sm_lm85_exit(void)
1639{
1640 i2c_del_driver(&lm85_driver);
1641}
1642
1643/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1644 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1645 * post 2.7.0 CVS changes.
1646 */
1647MODULE_LICENSE("GPL");
1648MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
1649MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1650
1651module_init(sm_lm85_init);
1652module_exit(sm_lm85_exit);