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