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