blob: 0c0fede8dde995318d98d3017ff4c821afa81ac5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
Jean Delvare1f448092008-04-29 14:03:37 +02004 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
8
9 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020032#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020033#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050038static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020041I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* The LM85 registers */
44
45#define LM85_REG_IN(nr) (0x20 + (nr))
46#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
47#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
48
49#define LM85_REG_TEMP(nr) (0x25 + (nr))
50#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
51#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
52
53/* Fan speeds are LSB, MSB (2 bytes) */
Jean Delvare1f448092008-04-29 14:03:37 +020054#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
55#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define LM85_REG_PWM(nr) (0x30 + (nr))
58
59#define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
60
61#define ADT7463_REG_TMIN_CTL1 0x36
62#define ADT7463_REG_TMIN_CTL2 0x37
63
64#define LM85_REG_DEVICE 0x3d
65#define LM85_REG_COMPANY 0x3e
66#define LM85_REG_VERSTEP 0x3f
67/* These are the recognized values for the above regs */
68#define LM85_DEVICE_ADX 0x27
69#define LM85_COMPANY_NATIONAL 0x01
70#define LM85_COMPANY_ANALOG_DEV 0x41
Jean Delvare1f448092008-04-29 14:03:37 +020071#define LM85_COMPANY_SMSC 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define LM85_VERSTEP_VMASK 0xf0
73#define LM85_VERSTEP_GENERIC 0x60
74#define LM85_VERSTEP_LM85C 0x60
75#define LM85_VERSTEP_LM85B 0x62
76#define LM85_VERSTEP_ADM1027 0x60
77#define LM85_VERSTEP_ADT7463 0x62
78#define LM85_VERSTEP_ADT7463C 0x6A
79#define LM85_VERSTEP_EMC6D100_A0 0x60
80#define LM85_VERSTEP_EMC6D100_A1 0x61
81#define LM85_VERSTEP_EMC6D102 0x65
82
83#define LM85_REG_CONFIG 0x40
84
85#define LM85_REG_ALARM1 0x41
86#define LM85_REG_ALARM2 0x42
87
88#define LM85_REG_VID 0x43
89
90/* Automated FAN control */
91#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
92#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
93#define LM85_REG_AFAN_SPIKE1 0x62
94#define LM85_REG_AFAN_SPIKE2 0x63
95#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
96#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
97#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
98#define LM85_REG_AFAN_HYST1 0x6d
99#define LM85_REG_AFAN_HYST2 0x6e
100
101#define LM85_REG_TACH_MODE 0x74
102#define LM85_REG_SPINUP_CTL 0x75
103
104#define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
105#define ADM1027_REG_CONFIG2 0x73
106#define ADM1027_REG_INTMASK1 0x74
107#define ADM1027_REG_INTMASK2 0x75
108#define ADM1027_REG_EXTEND_ADC1 0x76
109#define ADM1027_REG_EXTEND_ADC2 0x77
110#define ADM1027_REG_CONFIG3 0x78
111#define ADM1027_REG_FAN_PPR 0x7b
112
113#define ADT7463_REG_THERM 0x79
114#define ADT7463_REG_THERM_LIMIT 0x7A
115
116#define EMC6D100_REG_ALARM3 0x7d
117/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200118#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
119#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
120#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define EMC6D102_REG_EXTEND_ADC1 0x85
122#define EMC6D102_REG_EXTEND_ADC2 0x86
123#define EMC6D102_REG_EXTEND_ADC3 0x87
124#define EMC6D102_REG_EXTEND_ADC4 0x88
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jean Delvare1f448092008-04-29 14:03:37 +0200127/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 variants. Note that you should be a bit careful with which arguments
129 these macros are called: arguments may be evaluated more than once.
130 */
131
132/* IN are scaled acording to built-in resistors */
133static int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200134 2500, 2250, 3300, 5000, 12000,
135 3300, 1500, 1800 /*EMC6D100*/
136};
137#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jean Delvare1f448092008-04-29 14:03:37 +0200139#define INS_TO_REG(n, val) \
140 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jean Delvare1f448092008-04-29 14:03:37 +0200142#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200143 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jean Delvare1f448092008-04-29 14:03:37 +0200145#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200148static inline u16 FAN_TO_REG(unsigned long val)
149{
150 if (!val)
151 return 0xffff;
152 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
153}
Jean Delvare1f448092008-04-29 14:03:37 +0200154#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
155 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/* Temperature is reported in .001 degC increments */
158#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200159 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
160#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200161 SCALE(((val) << 4) + (ext), 16, 1000)
162#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jean Delvare1f448092008-04-29 14:03:37 +0200164#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define PWM_FROM_REG(val) (val)
166
167
168/* ZONEs have the following parameters:
169 * Limit (low) temp, 1. degC
170 * Hysteresis (below limit), 1. degC (0-15)
171 * Range of speed control, .1 degC (2-80)
172 * Critical (high) temp, 1. degC
173 *
174 * FAN PWMs have the following parameters:
175 * Reference Zone, 1, 2, 3, etc.
176 * Spinup time, .05 sec
177 * PWM value at limit/low temp, 1 count
178 * PWM Frequency, 1. Hz
179 * PWM is Min or OFF below limit, flag
180 * Invert PWM output, flag
181 *
182 * Some chips filter the temp, others the fan.
183 * Filter constant (or disabled) .1 seconds
184 */
185
186/* These are the zone temperature range encodings in .001 degree C */
Jean Delvare1f448092008-04-29 14:03:37 +0200187static int lm85_range_map[] = {
188 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
189 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
190};
191
192static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int i;
195
Jean Delvared38b1492008-04-03 10:40:39 +0200196 if (range >= lm85_range_map[15])
Jean Delvare1f448092008-04-29 14:03:37 +0200197 return 15;
Jean Delvared38b1492008-04-03 10:40:39 +0200198
199 /* Find the closest match */
200 for (i = 14; i >= 0; --i) {
201 if (range >= lm85_range_map[i]) {
202 if ((lm85_range_map[i + 1] - range) <
203 (range - lm85_range_map[i]))
204 return i + 1;
205 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 }
Jean Delvared38b1492008-04-03 10:40:39 +0200208
209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Jean Delvare1f448092008-04-29 14:03:37 +0200211#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/* These are the Acoustic Enhancement, or Temperature smoothing encodings
214 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
215 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
216 * is ignored, or set to 0.
217 */
218/* These are the PWM frequency encodings */
219static int lm85_freq_map[] = { /* .1 Hz */
Jean Delvare1f448092008-04-29 14:03:37 +0200220 100, 150, 230, 300, 380, 470, 620, 940
221};
222
223static int FREQ_TO_REG(int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 int i;
226
Jean Delvare1f448092008-04-29 14:03:37 +0200227 if (freq >= lm85_freq_map[7])
228 return 7;
229 for (i = 0; i < 7; ++i)
230 if (freq <= lm85_freq_map[i])
231 break;
232 return i & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
Jean Delvare1f448092008-04-29 14:03:37 +0200234#define FREQ_FROM_REG(val) lm85_freq_map[(val) & 0x07]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/* Since we can't use strings, I'm abusing these numbers
237 * to stand in for the following meanings:
238 * 1 -- PWM responds to Zone 1
239 * 2 -- PWM responds to Zone 2
240 * 3 -- PWM responds to Zone 3
241 * 23 -- PWM responds to the higher temp of Zone 2 or 3
242 * 123 -- PWM responds to highest of Zone 1, 2, or 3
243 * 0 -- PWM is always at 0% (ie, off)
244 * -1 -- PWM is always at 100%
245 * -2 -- PWM responds to manual control
246 */
247
248static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
Jean Delvare1f448092008-04-29 14:03:37 +0200249#define ZONE_FROM_REG(val) lm85_zone_map[((val) >> 5) & 0x07]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Jean Delvare1f448092008-04-29 14:03:37 +0200251static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 int i;
254
Jean Delvare1f448092008-04-29 14:03:37 +0200255 for (i = 0; i <= 7; ++i)
256 if (zone == lm85_zone_map[i])
257 break;
258 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 i = 3; /* Always 100% */
Jean Delvare1f448092008-04-29 14:03:37 +0200260 return (i & 0x07) << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Jean Delvare1f448092008-04-29 14:03:37 +0200263#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
264#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jean Delvare1f448092008-04-29 14:03:37 +0200266#define OFFSET_TO_REG(val) SENSORS_LIMIT((val) / 25, -127, 127)
267#define OFFSET_FROM_REG(val) ((val) * 25)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jean Delvare1f448092008-04-29 14:03:37 +0200269#define PPR_MASK(fan) (0x03 << ((fan) * 2))
270#define PPR_TO_REG(val, fan) (SENSORS_LIMIT((val) - 1, 0, 3) << ((fan) * 2))
271#define PPR_FROM_REG(val, fan) ((((val) >> ((fan) * 2)) & 0x03) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* Chip sampling rates
274 *
275 * Some sensors are not updated more frequently than once per second
276 * so it doesn't make sense to read them more often than that.
277 * We cache the results and return the saved data if the driver
278 * is called again before a second has elapsed.
279 *
280 * Also, there is significant configuration data for this chip
281 * given the automatic PWM fan control that is possible. There
282 * are about 47 bytes of config data to only 22 bytes of actual
283 * readings. So, we keep the config data up to date in the cache
284 * when it is written and only sample it once every 1 *minute*
285 */
286#define LM85_DATA_INTERVAL (HZ + HZ / 2)
287#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/* LM85 can automatically adjust fan speeds based on temperature
290 * This structure encapsulates an entire Zone config. There are
291 * three zones (one for each temperature input) on the lm85
292 */
293struct lm85_zone {
294 s8 limit; /* Low temp limit */
295 u8 hyst; /* Low limit hysteresis. (0-15) */
296 u8 range; /* Temp range, encoded */
297 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200298 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * to prevent "drift" as other autofan control
300 * values change.
301 */
Jean Delvare1f448092008-04-29 14:03:37 +0200302 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * to prevent "drift" as other autofan control
304 * values change.
305 */
306};
307
308struct lm85_autofan {
309 u8 config; /* Register value */
310 u8 freq; /* PWM frequency, encoded */
311 u8 min_pwm; /* Minimum PWM value, encoded */
312 u8 min_off; /* Min PWM or OFF below "limit", flag */
313};
314
Jean Delvareed6bafb2007-02-14 21:15:03 +0100315/* For each registered chip, we need to keep some data in memory.
316 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317struct lm85_data {
318 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700319 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 enum chips type;
321
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100322 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int valid; /* !=0 if following fields are valid */
324 unsigned long last_reading; /* In jiffies */
325 unsigned long last_config; /* In jiffies */
326
327 u8 in[8]; /* Register value */
328 u8 in_max[8]; /* Register value */
329 u8 in_min[8]; /* Register value */
330 s8 temp[3]; /* Register value */
331 s8 temp_min[3]; /* Register value */
332 s8 temp_max[3]; /* Register value */
333 s8 temp_offset[3]; /* Register value */
334 u16 fan[4]; /* Register value */
335 u16 fan_min[4]; /* Register value */
336 u8 pwm[3]; /* Register value */
337 u8 spinup_ctl; /* Register encoding, combined */
338 u8 tach_mode; /* Register encoding, combined */
339 u8 temp_ext[3]; /* Decoded values */
340 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 u8 fan_ppr; /* Register value */
342 u8 smooth[3]; /* Register encoding */
343 u8 vid; /* Register value */
344 u8 vrm; /* VRM version */
345 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
346 u8 oppoint[3]; /* Register value */
347 u16 tmin_ctl; /* Register value */
348 unsigned long therm_total; /* Cummulative therm count */
349 u8 therm_limit; /* Register value */
350 u32 alarms; /* Register encoding, combined */
351 struct lm85_autofan autofan[3];
352 struct lm85_zone zone[3];
353};
354
355static int lm85_attach_adapter(struct i2c_adapter *adapter);
356static int lm85_detect(struct i2c_adapter *adapter, int address,
357 int kind);
358static int lm85_detach_client(struct i2c_client *client);
359
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100360static int lm85_read_value(struct i2c_client *client, u8 reg);
361static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static struct lm85_data *lm85_update_device(struct device *dev);
363static void lm85_init_client(struct i2c_client *client);
364
365
366static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100367 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100368 .name = "lm85",
369 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 .attach_adapter = lm85_attach_adapter,
371 .detach_client = lm85_detach_client,
372};
373
374
375/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200376static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
377 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Jean Delvareb353a482007-07-05 20:36:12 +0200379 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200381 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
Jean Delvareb353a482007-07-05 20:36:12 +0200383
384static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
385 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Jean Delvareb353a482007-07-05 20:36:12 +0200387 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200389 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Jean Delvareb353a482007-07-05 20:36:12 +0200391
392static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
393 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Jean Delvareb353a482007-07-05 20:36:12 +0200395 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct i2c_client *client = to_i2c_client(dev);
397 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200398 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100400 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 data->fan_min[nr] = FAN_TO_REG(val);
402 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100403 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return count;
405}
406
407#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200408static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
409 show_fan, NULL, offset - 1); \
410static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
411 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413show_fan_offset(1);
414show_fan_offset(2);
415show_fan_offset(3);
416show_fan_offset(4);
417
418/* vid, vrm, alarms */
419
Jean Delvare1f448092008-04-29 14:03:37 +0200420static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
421 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100424 int vid;
425
426 if (data->type == adt7463 && (data->vid & 0x80)) {
427 /* 6-pin VID (VRM 10) */
428 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
429 } else {
430 /* 5-pin VID (VRM 9) */
431 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
432 }
433
434 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
438
Jean Delvare1f448092008-04-29 14:03:37 +0200439static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
440 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Jean Delvare90d66192007-10-08 18:24:35 +0200442 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return sprintf(buf, "%ld\n", (long) data->vrm);
444}
445
Jean Delvare1f448092008-04-29 14:03:37 +0200446static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
447 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100449 struct lm85_data *data = dev_get_drvdata(dev);
450 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return count;
452}
453
454static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
455
Jean Delvare1f448092008-04-29 14:03:37 +0200456static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
457 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200460 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
464
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200465static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
466 char *buf)
467{
468 int nr = to_sensor_dev_attr(attr)->index;
469 struct lm85_data *data = lm85_update_device(dev);
470 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
471}
472
473static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
474static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
475static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
476static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
477static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
478static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
479static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
480static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
481static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
482static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
483static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
484static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
485static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
486static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
487static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
488static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
489static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/* pwm */
492
Jean Delvareb353a482007-07-05 20:36:12 +0200493static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
494 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Jean Delvareb353a482007-07-05 20:36:12 +0200496 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200498 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
Jean Delvareb353a482007-07-05 20:36:12 +0200500
501static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
502 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jean Delvareb353a482007-07-05 20:36:12 +0200504 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct i2c_client *client = to_i2c_client(dev);
506 struct lm85_data *data = i2c_get_clientdata(client);
507 long val = simple_strtol(buf, NULL, 10);
508
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100509 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 data->pwm[nr] = PWM_TO_REG(val);
511 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100512 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return count;
514}
Jean Delvareb353a482007-07-05 20:36:12 +0200515
516static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
517 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Jean Delvareb353a482007-07-05 20:36:12 +0200519 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100521 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100524 switch (pwm_zone) {
525 case -1: /* PWM is always at 100% */
526 enable = 0;
527 break;
528 case 0: /* PWM is always at 0% */
529 case -2: /* PWM responds to manual control */
530 enable = 1;
531 break;
532 default: /* PWM in automatic mode */
533 enable = 2;
534 }
535 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Jean Delvare455f7912007-12-03 23:28:42 +0100538static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
539 *attr, const char *buf, size_t count)
540{
541 int nr = to_sensor_dev_attr(attr)->index;
542 struct i2c_client *client = to_i2c_client(dev);
543 struct lm85_data *data = i2c_get_clientdata(client);
544 long val = simple_strtol(buf, NULL, 10);
545 u8 config;
546
547 switch (val) {
548 case 0:
549 config = 3;
550 break;
551 case 1:
552 config = 7;
553 break;
554 case 2:
555 /* Here we have to choose arbitrarily one of the 5 possible
556 configurations; I go for the safest */
557 config = 6;
558 break;
559 default:
560 return -EINVAL;
561 }
562
563 mutex_lock(&data->update_lock);
564 data->autofan[nr].config = lm85_read_value(client,
565 LM85_REG_AFAN_CONFIG(nr));
566 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
567 | (config << 5);
568 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
569 data->autofan[nr].config);
570 mutex_unlock(&data->update_lock);
571 return count;
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200575static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
576 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100577static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
578 show_pwm_enable, set_pwm_enable, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580show_pwm_reg(1);
581show_pwm_reg(2);
582show_pwm_reg(3);
583
584/* Voltages */
585
Jean Delvareb353a482007-07-05 20:36:12 +0200586static ssize_t show_in(struct device *dev, struct device_attribute *attr,
587 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Jean Delvareb353a482007-07-05 20:36:12 +0200589 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200591 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
592 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
Jean Delvareb353a482007-07-05 20:36:12 +0200594
Jean Delvare1f448092008-04-29 14:03:37 +0200595static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200596 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jean Delvareb353a482007-07-05 20:36:12 +0200598 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200600 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
Jean Delvareb353a482007-07-05 20:36:12 +0200602
603static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
604 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jean Delvareb353a482007-07-05 20:36:12 +0200606 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct i2c_client *client = to_i2c_client(dev);
608 struct lm85_data *data = i2c_get_clientdata(client);
609 long val = simple_strtol(buf, NULL, 10);
610
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100611 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 data->in_min[nr] = INS_TO_REG(nr, val);
613 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100614 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return count;
616}
Jean Delvareb353a482007-07-05 20:36:12 +0200617
618static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
619 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jean Delvareb353a482007-07-05 20:36:12 +0200621 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200623 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Jean Delvareb353a482007-07-05 20:36:12 +0200625
626static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Jean Delvareb353a482007-07-05 20:36:12 +0200629 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct i2c_client *client = to_i2c_client(dev);
631 struct lm85_data *data = i2c_get_clientdata(client);
632 long val = simple_strtol(buf, NULL, 10);
633
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100634 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 data->in_max[nr] = INS_TO_REG(nr, val);
636 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100637 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return count;
639}
Jean Delvareb353a482007-07-05 20:36:12 +0200640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200642static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
643 show_in, NULL, offset); \
644static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
645 show_in_min, set_in_min, offset); \
646static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
647 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649show_in_reg(0);
650show_in_reg(1);
651show_in_reg(2);
652show_in_reg(3);
653show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200654show_in_reg(5);
655show_in_reg(6);
656show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658/* Temps */
659
Jean Delvareb353a482007-07-05 20:36:12 +0200660static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
661 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Jean Delvareb353a482007-07-05 20:36:12 +0200663 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200665 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
666 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Jean Delvareb353a482007-07-05 20:36:12 +0200668
669static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
670 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Jean Delvareb353a482007-07-05 20:36:12 +0200672 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200674 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
Jean Delvareb353a482007-07-05 20:36:12 +0200676
677static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
678 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jean Delvareb353a482007-07-05 20:36:12 +0200680 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct i2c_client *client = to_i2c_client(dev);
682 struct lm85_data *data = i2c_get_clientdata(client);
683 long val = simple_strtol(buf, NULL, 10);
684
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100685 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 data->temp_min[nr] = TEMP_TO_REG(val);
687 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100688 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return count;
690}
Jean Delvareb353a482007-07-05 20:36:12 +0200691
692static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
693 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Jean Delvareb353a482007-07-05 20:36:12 +0200695 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200697 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
Jean Delvareb353a482007-07-05 20:36:12 +0200699
700static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
701 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jean Delvareb353a482007-07-05 20:36:12 +0200703 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct i2c_client *client = to_i2c_client(dev);
705 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200706 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100708 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 data->temp_max[nr] = TEMP_TO_REG(val);
710 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100711 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return count;
713}
Jean Delvareb353a482007-07-05 20:36:12 +0200714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200716static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
717 show_temp, NULL, offset - 1); \
718static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
719 show_temp_min, set_temp_min, offset - 1); \
720static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
721 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723show_temp_reg(1);
724show_temp_reg(2);
725show_temp_reg(3);
726
727
728/* Automatic PWM control */
729
Jean Delvareb353a482007-07-05 20:36:12 +0200730static ssize_t show_pwm_auto_channels(struct device *dev,
731 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Jean Delvareb353a482007-07-05 20:36:12 +0200733 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200735 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
Jean Delvareb353a482007-07-05 20:36:12 +0200737
738static ssize_t set_pwm_auto_channels(struct device *dev,
739 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Jean Delvareb353a482007-07-05 20:36:12 +0200741 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct i2c_client *client = to_i2c_client(dev);
743 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200744 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100746 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200748 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
750 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100751 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return count;
753}
Jean Delvareb353a482007-07-05 20:36:12 +0200754
755static ssize_t show_pwm_auto_pwm_min(struct device *dev,
756 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jean Delvareb353a482007-07-05 20:36:12 +0200758 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200760 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Jean Delvareb353a482007-07-05 20:36:12 +0200762
763static ssize_t set_pwm_auto_pwm_min(struct device *dev,
764 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Jean Delvareb353a482007-07-05 20:36:12 +0200766 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct i2c_client *client = to_i2c_client(dev);
768 struct lm85_data *data = i2c_get_clientdata(client);
769 long val = simple_strtol(buf, NULL, 10);
770
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100771 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 data->autofan[nr].min_pwm = PWM_TO_REG(val);
773 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
774 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100775 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return count;
777}
Jean Delvareb353a482007-07-05 20:36:12 +0200778
779static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
780 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Jean Delvareb353a482007-07-05 20:36:12 +0200782 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200784 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
Jean Delvareb353a482007-07-05 20:36:12 +0200786
787static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
788 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jean Delvareb353a482007-07-05 20:36:12 +0200790 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct i2c_client *client = to_i2c_client(dev);
792 struct lm85_data *data = i2c_get_clientdata(client);
793 long val = simple_strtol(buf, NULL, 10);
794
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100795 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 data->autofan[nr].min_off = val;
797 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
798 | data->syncpwm3
799 | (data->autofan[0].min_off ? 0x20 : 0)
800 | (data->autofan[1].min_off ? 0x40 : 0)
Jean Delvare1f448092008-04-29 14:03:37 +0200801 | (data->autofan[2].min_off ? 0x80 : 0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100802 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return count;
804}
Jean Delvareb353a482007-07-05 20:36:12 +0200805
806static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
807 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Jean Delvareb353a482007-07-05 20:36:12 +0200809 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200811 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
Jean Delvareb353a482007-07-05 20:36:12 +0200813
814static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
815 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jean Delvareb353a482007-07-05 20:36:12 +0200817 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct i2c_client *client = to_i2c_client(dev);
819 struct lm85_data *data = i2c_get_clientdata(client);
820 long val = simple_strtol(buf, NULL, 10);
821
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 data->autofan[nr].freq = FREQ_TO_REG(val);
824 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
825 (data->zone[nr].range << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200826 | data->autofan[nr].freq);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100827 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return count;
829}
Jean Delvareb353a482007-07-05 20:36:12 +0200830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200832static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
833 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
834 set_pwm_auto_channels, offset - 1); \
835static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
836 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
837 set_pwm_auto_pwm_min, offset - 1); \
838static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
839 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
840 set_pwm_auto_pwm_minctl, offset - 1); \
841static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
842 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
843 set_pwm_auto_pwm_freq, offset - 1);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845pwm_auto(1);
846pwm_auto(2);
847pwm_auto(3);
848
849/* Temperature settings for automatic PWM control */
850
Jean Delvareb353a482007-07-05 20:36:12 +0200851static ssize_t show_temp_auto_temp_off(struct device *dev,
852 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Jean Delvareb353a482007-07-05 20:36:12 +0200854 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200856 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 HYST_FROM_REG(data->zone[nr].hyst));
858}
Jean Delvareb353a482007-07-05 20:36:12 +0200859
860static ssize_t set_temp_auto_temp_off(struct device *dev,
861 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Jean Delvareb353a482007-07-05 20:36:12 +0200863 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct i2c_client *client = to_i2c_client(dev);
865 struct lm85_data *data = i2c_get_clientdata(client);
866 int min;
867 long val = simple_strtol(buf, NULL, 10);
868
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100869 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 min = TEMP_FROM_REG(data->zone[nr].limit);
871 data->zone[nr].off_desired = TEMP_TO_REG(val);
872 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200873 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 lm85_write_value(client, LM85_REG_AFAN_HYST1,
875 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200876 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 } else {
878 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200879 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100881 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return count;
883}
Jean Delvareb353a482007-07-05 20:36:12 +0200884
885static ssize_t show_temp_auto_temp_min(struct device *dev,
886 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Jean Delvareb353a482007-07-05 20:36:12 +0200888 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200890 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
Jean Delvareb353a482007-07-05 20:36:12 +0200892
893static ssize_t set_temp_auto_temp_min(struct device *dev,
894 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jean Delvareb353a482007-07-05 20:36:12 +0200896 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct i2c_client *client = to_i2c_client(dev);
898 struct lm85_data *data = i2c_get_clientdata(client);
899 long val = simple_strtol(buf, NULL, 10);
900
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100901 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 data->zone[nr].limit = TEMP_TO_REG(val);
903 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
904 data->zone[nr].limit);
905
906/* Update temp_auto_max and temp_auto_range */
907 data->zone[nr].range = RANGE_TO_REG(
908 TEMP_FROM_REG(data->zone[nr].max_desired) -
909 TEMP_FROM_REG(data->zone[nr].limit));
910 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
911 ((data->zone[nr].range & 0x0f) << 4)
912 | (data->autofan[nr].freq & 0x07));
913
914/* Update temp_auto_hyst and temp_auto_off */
915 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
916 data->zone[nr].limit) - TEMP_FROM_REG(
917 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200918 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 lm85_write_value(client, LM85_REG_AFAN_HYST1,
920 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200921 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 } else {
923 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200924 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100926 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return count;
928}
Jean Delvareb353a482007-07-05 20:36:12 +0200929
930static ssize_t show_temp_auto_temp_max(struct device *dev,
931 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Jean Delvareb353a482007-07-05 20:36:12 +0200933 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200935 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 RANGE_FROM_REG(data->zone[nr].range));
937}
Jean Delvareb353a482007-07-05 20:36:12 +0200938
939static ssize_t set_temp_auto_temp_max(struct device *dev,
940 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jean Delvareb353a482007-07-05 20:36:12 +0200942 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct i2c_client *client = to_i2c_client(dev);
944 struct lm85_data *data = i2c_get_clientdata(client);
945 int min;
946 long val = simple_strtol(buf, NULL, 10);
947
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100948 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 min = TEMP_FROM_REG(data->zone[nr].limit);
950 data->zone[nr].max_desired = TEMP_TO_REG(val);
951 data->zone[nr].range = RANGE_TO_REG(
952 val - min);
953 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
954 ((data->zone[nr].range & 0x0f) << 4)
955 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100956 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return count;
958}
Jean Delvareb353a482007-07-05 20:36:12 +0200959
960static ssize_t show_temp_auto_temp_crit(struct device *dev,
961 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jean Delvareb353a482007-07-05 20:36:12 +0200963 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200965 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
Jean Delvareb353a482007-07-05 20:36:12 +0200967
968static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200969 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Jean Delvareb353a482007-07-05 20:36:12 +0200971 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct i2c_client *client = to_i2c_client(dev);
973 struct lm85_data *data = i2c_get_clientdata(client);
974 long val = simple_strtol(buf, NULL, 10);
975
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100976 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 data->zone[nr].critical = TEMP_TO_REG(val);
978 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
979 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100980 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return count;
982}
Jean Delvareb353a482007-07-05 20:36:12 +0200983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200985static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
986 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
987 set_temp_auto_temp_off, offset - 1); \
988static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
989 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
990 set_temp_auto_temp_min, offset - 1); \
991static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
992 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
993 set_temp_auto_temp_max, offset - 1); \
994static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
995 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
996 set_temp_auto_temp_crit, offset - 1);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998temp_auto(1);
999temp_auto(2);
1000temp_auto(3);
1001
Ben Dooksd8d20612005-10-26 21:05:46 +02001002static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 if (!(adapter->class & I2C_CLASS_HWMON))
1005 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +02001006 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001009static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001010 &sensor_dev_attr_fan1_input.dev_attr.attr,
1011 &sensor_dev_attr_fan2_input.dev_attr.attr,
1012 &sensor_dev_attr_fan3_input.dev_attr.attr,
1013 &sensor_dev_attr_fan4_input.dev_attr.attr,
1014 &sensor_dev_attr_fan1_min.dev_attr.attr,
1015 &sensor_dev_attr_fan2_min.dev_attr.attr,
1016 &sensor_dev_attr_fan3_min.dev_attr.attr,
1017 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001018 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1019 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1020 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1021 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001022
1023 &sensor_dev_attr_pwm1.dev_attr.attr,
1024 &sensor_dev_attr_pwm2.dev_attr.attr,
1025 &sensor_dev_attr_pwm3.dev_attr.attr,
1026 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1027 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1028 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1029
1030 &sensor_dev_attr_in0_input.dev_attr.attr,
1031 &sensor_dev_attr_in1_input.dev_attr.attr,
1032 &sensor_dev_attr_in2_input.dev_attr.attr,
1033 &sensor_dev_attr_in3_input.dev_attr.attr,
1034 &sensor_dev_attr_in0_min.dev_attr.attr,
1035 &sensor_dev_attr_in1_min.dev_attr.attr,
1036 &sensor_dev_attr_in2_min.dev_attr.attr,
1037 &sensor_dev_attr_in3_min.dev_attr.attr,
1038 &sensor_dev_attr_in0_max.dev_attr.attr,
1039 &sensor_dev_attr_in1_max.dev_attr.attr,
1040 &sensor_dev_attr_in2_max.dev_attr.attr,
1041 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001042 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1043 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1044 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1045 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001046
1047 &sensor_dev_attr_temp1_input.dev_attr.attr,
1048 &sensor_dev_attr_temp2_input.dev_attr.attr,
1049 &sensor_dev_attr_temp3_input.dev_attr.attr,
1050 &sensor_dev_attr_temp1_min.dev_attr.attr,
1051 &sensor_dev_attr_temp2_min.dev_attr.attr,
1052 &sensor_dev_attr_temp3_min.dev_attr.attr,
1053 &sensor_dev_attr_temp1_max.dev_attr.attr,
1054 &sensor_dev_attr_temp2_max.dev_attr.attr,
1055 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001056 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1057 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1058 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1059 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1060 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001061
1062 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1063 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1064 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1065 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1066 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1067 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1068 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1069 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1070 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1071 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1072 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1073 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1074
1075 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1076 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1077 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1078 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1079 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1080 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1081 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1082 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1083 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1084 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1085 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1086 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1087
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001088 &dev_attr_vrm.attr,
1089 &dev_attr_cpu0_vid.attr,
1090 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001091 NULL
1092};
1093
1094static const struct attribute_group lm85_group = {
1095 .attrs = lm85_attributes,
1096};
1097
Jean Delvare6b9aad22007-07-05 20:37:21 +02001098static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001099 &sensor_dev_attr_in4_input.dev_attr.attr,
1100 &sensor_dev_attr_in4_min.dev_attr.attr,
1101 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001102 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001103 NULL
1104};
1105
Jean Delvare6b9aad22007-07-05 20:37:21 +02001106static const struct attribute_group lm85_group_in4 = {
1107 .attrs = lm85_attributes_in4,
1108};
1109
1110static struct attribute *lm85_attributes_in567[] = {
1111 &sensor_dev_attr_in5_input.dev_attr.attr,
1112 &sensor_dev_attr_in6_input.dev_attr.attr,
1113 &sensor_dev_attr_in7_input.dev_attr.attr,
1114 &sensor_dev_attr_in5_min.dev_attr.attr,
1115 &sensor_dev_attr_in6_min.dev_attr.attr,
1116 &sensor_dev_attr_in7_min.dev_attr.attr,
1117 &sensor_dev_attr_in5_max.dev_attr.attr,
1118 &sensor_dev_attr_in6_max.dev_attr.attr,
1119 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001120 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1121 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1122 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001123 NULL
1124};
1125
1126static const struct attribute_group lm85_group_in567 = {
1127 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001128};
1129
Ben Dooksd8d20612005-10-26 21:05:46 +02001130static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int kind)
1132{
Jean Delvare1f448092008-04-29 14:03:37 +02001133 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct i2c_client *new_client = NULL;
1135 struct lm85_data *data;
1136 int err = 0;
1137 const char *type_name = "";
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!i2c_check_functionality(adapter,
1140 I2C_FUNC_SMBUS_BYTE_DATA)) {
1141 /* We need to be able to do byte I/O */
Jean Delvare1f448092008-04-29 14:03:37 +02001142 goto ERROR0;
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /* OK. For now, we presume we have a valid client. We now create the
1146 client structure, even though we cannot fill it completely yet.
1147 But it allows us to access lm85_{read,write}_value. */
1148
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001149 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 err = -ENOMEM;
1151 goto ERROR0;
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 new_client = &data->client;
1155 i2c_set_clientdata(new_client, data);
1156 new_client->addr = address;
1157 new_client->adapter = adapter;
1158 new_client->driver = &lm85_driver;
1159 new_client->flags = 0;
1160
1161 /* Now, we do the remaining detection. */
1162
1163 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1164 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1165
1166 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1167 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1168 i2c_adapter_id(new_client->adapter), new_client->addr,
1169 company, verstep);
1170
1171 /* If auto-detecting, Determine the chip type. */
1172 if (kind <= 0) {
1173 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001174 i2c_adapter_id(adapter), address);
1175 if (company == LM85_COMPANY_NATIONAL
1176 && verstep == LM85_VERSTEP_LM85C) {
1177 kind = lm85c;
1178 } else if (company == LM85_COMPANY_NATIONAL
1179 && verstep == LM85_VERSTEP_LM85B) {
1180 kind = lm85b;
1181 } else if (company == LM85_COMPANY_NATIONAL
1182 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1184 " Defaulting to LM85.\n", verstep);
Jean Delvare1f448092008-04-29 14:03:37 +02001185 kind = any_chip;
1186 } else if (company == LM85_COMPANY_ANALOG_DEV
1187 && verstep == LM85_VERSTEP_ADM1027) {
1188 kind = adm1027;
1189 } else if (company == LM85_COMPANY_ANALOG_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 && (verstep == LM85_VERSTEP_ADT7463
Jean Delvare1f448092008-04-29 14:03:37 +02001191 || verstep == LM85_VERSTEP_ADT7463C)) {
1192 kind = adt7463;
1193 } else if (company == LM85_COMPANY_ANALOG_DEV
1194 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001196 " Defaulting to Generic LM85.\n", verstep);
1197 kind = any_chip;
1198 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 && (verstep == LM85_VERSTEP_EMC6D100_A0
Jean Delvare1f448092008-04-29 14:03:37 +02001200 || verstep == LM85_VERSTEP_EMC6D100_A1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Unfortunately, we can't tell a '100 from a '101
1202 * from the registers. Since a '101 is a '100
1203 * in a package with fewer pins and therefore no
1204 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1205 * inputs read 0, then it's a '101.
1206 */
Jean Delvare1f448092008-04-29 14:03:37 +02001207 kind = emc6d100;
1208 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 && verstep == LM85_VERSTEP_EMC6D102) {
Jean Delvare1f448092008-04-29 14:03:37 +02001210 kind = emc6d102;
1211 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1213 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1214 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001215 " Defaulting to Generic LM85.\n", verstep);
1216 kind = any_chip;
1217 } else if (kind == any_chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1219 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1220 /* Leave kind as "any_chip" */
1221 } else {
1222 dev_dbg(&adapter->dev, "Autodetection failed\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001223 /* Not an LM85... */
1224 if (kind == any_chip) { /* User used force=x,y */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1226 " found at %d,0x%02x. Try force_lm85c.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001227 i2c_adapter_id(adapter), address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Jean Delvare1f448092008-04-29 14:03:37 +02001229 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto ERROR1;
1231 }
1232 }
1233
1234 /* Fill in the chip specific driver values */
Jean Delvare1f448092008-04-29 14:03:37 +02001235 if (kind == any_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 type_name = "lm85";
Jean Delvare1f448092008-04-29 14:03:37 +02001237 else if (kind == lm85b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 type_name = "lm85b";
Jean Delvare1f448092008-04-29 14:03:37 +02001239 else if (kind == lm85c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 type_name = "lm85c";
Jean Delvare1f448092008-04-29 14:03:37 +02001241 else if (kind == adm1027)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 type_name = "adm1027";
Jean Delvare1f448092008-04-29 14:03:37 +02001243 else if (kind == adt7463)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 type_name = "adt7463";
Jean Delvare1f448092008-04-29 14:03:37 +02001245 else if (kind == emc6d100)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 type_name = "emc6d100";
Jean Delvare1f448092008-04-29 14:03:37 +02001247 else if (kind == emc6d102)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 type_name = "emc6d102";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1250
1251 /* Fill in the remaining client fields */
1252 data->type = kind;
1253 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001254 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* Tell the I2C layer a new client has arrived */
1257 if ((err = i2c_attach_client(new_client)))
1258 goto ERROR1;
1259
1260 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001261 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /* Initialize the LM85 chip */
1264 lm85_init_client(new_client);
1265
1266 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001267 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001268 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jean Delvare9c516ef2005-11-26 20:07:54 +01001270 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1271 as a sixth digital VID input rather than an analog input. */
1272 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001273 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001274 if ((err = sysfs_create_group(&new_client->dev.kobj,
1275 &lm85_group_in4)))
1276 goto ERROR3;
1277
1278 /* The EMC6D100 has 3 additional voltage inputs */
1279 if (kind == emc6d100)
1280 if ((err = sysfs_create_group(&new_client->dev.kobj,
1281 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001282 goto ERROR3;
1283
Tony Jones1beeffe2007-08-20 13:46:20 -07001284 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1285 if (IS_ERR(data->hwmon_dev)) {
1286 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001287 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001288 }
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291
1292 /* Error out and cleanup code */
Jean Delvare1f448092008-04-29 14:03:37 +02001293 ERROR3:
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001294 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001295 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1296 if (kind == emc6d100)
1297 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Jean Delvare1f448092008-04-29 14:03:37 +02001298 ERROR2:
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001299 i2c_detach_client(new_client);
Jean Delvare1f448092008-04-29 14:03:37 +02001300 ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 kfree(data);
Jean Delvare1f448092008-04-29 14:03:37 +02001302 ERROR0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return err;
1304}
1305
Ben Dooksd8d20612005-10-26 21:05:46 +02001306static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001308 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001309 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001310 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001311 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1312 if (data->type == emc6d100)
1313 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001315 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return 0;
1317}
1318
1319
Ben Dooksd8d20612005-10-26 21:05:46 +02001320static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 int res;
1323
1324 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001325 switch (reg) {
1326 case LM85_REG_FAN(0): /* Read WORD data */
1327 case LM85_REG_FAN(1):
1328 case LM85_REG_FAN(2):
1329 case LM85_REG_FAN(3):
1330 case LM85_REG_FAN_MIN(0):
1331 case LM85_REG_FAN_MIN(1):
1332 case LM85_REG_FAN_MIN(2):
1333 case LM85_REG_FAN_MIN(3):
1334 case LM85_REG_ALARM1: /* Read both bytes at once */
1335 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1336 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1337 break;
1338 case ADT7463_REG_TMIN_CTL1: /* Read WORD MSB, LSB */
1339 res = i2c_smbus_read_byte_data(client, reg) << 8;
1340 res |= i2c_smbus_read_byte_data(client, reg + 1) & 0xff;
1341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 default: /* Read BYTE data */
1343 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
Jean Delvare1f448092008-04-29 14:03:37 +02001347 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Ben Dooksd8d20612005-10-26 21:05:46 +02001350static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Jean Delvare1f448092008-04-29 14:03:37 +02001352 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Jean Delvare1f448092008-04-29 14:03:37 +02001354 switch (reg) {
1355 case LM85_REG_FAN(0): /* Write WORD data */
1356 case LM85_REG_FAN(1):
1357 case LM85_REG_FAN(2):
1358 case LM85_REG_FAN(3):
1359 case LM85_REG_FAN_MIN(0):
1360 case LM85_REG_FAN_MIN(1):
1361 case LM85_REG_FAN_MIN(2):
1362 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* NOTE: ALARM is read only, so not included here */
Jean Delvare1f448092008-04-29 14:03:37 +02001364 res = i2c_smbus_write_byte_data(client, reg, value & 0xff);
1365 res |= i2c_smbus_write_byte_data(client, reg + 1,
1366 (value >> 8) & 0xff);
1367 break;
1368 case ADT7463_REG_TMIN_CTL1: /* Write WORD MSB, LSB */
1369 res = i2c_smbus_write_byte_data(client, reg,
1370 (value >> 8) & 0xff);
1371 res |= i2c_smbus_write_byte_data(client, reg + 1, value & 0xff);
1372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 default: /* Write BYTE data */
1374 res = i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Jean Delvare1f448092008-04-29 14:03:37 +02001378 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Ben Dooksd8d20612005-10-26 21:05:46 +02001381static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 int value;
1384 struct lm85_data *data = i2c_get_clientdata(client);
1385
1386 dev_dbg(&client->dev, "Initializing device\n");
1387
1388 /* Warn if part was not "READY" */
1389 value = lm85_read_value(client, LM85_REG_CONFIG);
1390 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
Jean Delvare1f448092008-04-29 14:03:37 +02001391 if (value & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001393 i2c_adapter_id(client->adapter), client->addr);
1394 }
1395 if (!(value & 0x04)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001397 i2c_adapter_id(client->adapter), client->addr);
1398 }
1399 if (value & 0x10
1400 && (data->type == adm1027
1401 || data->type == adt7463)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1403 "Please report this to the lm85 maintainer.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001404 i2c_adapter_id(client->adapter), client->addr);
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* WE INTENTIONALLY make no changes to the limits,
1408 * offsets, pwms, fans and zones. If they were
1409 * configured, we don't want to mess with them.
1410 * If they weren't, the default is 100% PWM, no
1411 * control and will suffice until 'sensors -s'
1412 * can be run by the user.
1413 */
1414
1415 /* Start monitoring */
1416 value = lm85_read_value(client, LM85_REG_CONFIG);
1417 /* Try to clear LOCK, Set START, save everything else */
Jean Delvare1f448092008-04-29 14:03:37 +02001418 value = (value & ~0x02) | 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1420 lm85_write_value(client, LM85_REG_CONFIG, value);
1421}
1422
1423static struct lm85_data *lm85_update_device(struct device *dev)
1424{
1425 struct i2c_client *client = to_i2c_client(dev);
1426 struct lm85_data *data = i2c_get_clientdata(client);
1427 int i;
1428
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001429 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jean Delvare1f448092008-04-29 14:03:37 +02001431 if (!data->valid ||
1432 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Things that change quickly */
1434 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* Have to read extended bits first to "freeze" the
1437 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001438 * There are 2 additional resolution bits per channel and we
1439 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Jean Delvare1f448092008-04-29 14:03:37 +02001441 if (data->type == adm1027 || data->type == adt7463) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int ext1 = lm85_read_value(client,
1443 ADM1027_REG_EXTEND_ADC1);
1444 int ext2 = lm85_read_value(client,
1445 ADM1027_REG_EXTEND_ADC2);
1446 int val = (ext1 << 8) + ext2;
1447
Jean Delvare1f448092008-04-29 14:03:37 +02001448 for (i = 0; i <= 4; i++)
1449 data->in_ext[i] =
1450 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jean Delvare1f448092008-04-29 14:03:37 +02001452 for (i = 0; i <= 2; i++)
1453 data->temp_ext[i] =
1454 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Jean Delvare9c516ef2005-11-26 20:07:54 +01001457 data->vid = lm85_read_value(client, LM85_REG_VID);
1458
1459 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 data->in[i] =
1461 lm85_read_value(client, LM85_REG_IN(i));
1462 }
1463
Jean Delvare9c516ef2005-11-26 20:07:54 +01001464 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1465 data->in[4] = lm85_read_value(client,
1466 LM85_REG_IN(4));
1467 }
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 for (i = 0; i <= 3; ++i) {
1470 data->fan[i] =
1471 lm85_read_value(client, LM85_REG_FAN(i));
1472 }
1473
1474 for (i = 0; i <= 2; ++i) {
1475 data->temp[i] =
1476 lm85_read_value(client, LM85_REG_TEMP(i));
1477 }
1478
1479 for (i = 0; i <= 2; ++i) {
1480 data->pwm[i] =
1481 lm85_read_value(client, LM85_REG_PWM(i));
1482 }
1483
1484 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1485
Jean Delvare1f448092008-04-29 14:03:37 +02001486 if (data->type == adt7463) {
1487 if (data->therm_total < ULONG_MAX - 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 data->therm_total +=
Jean Delvare1f448092008-04-29 14:03:37 +02001489 lm85_read_value(client, ADT7463_REG_THERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Jean Delvare1f448092008-04-29 14:03:37 +02001491 } else if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* Three more voltage sensors */
1493 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001494 data->in[i] = lm85_read_value(client,
1495 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001498 data->alarms |= lm85_read_value(client,
1499 EMC6D100_REG_ALARM3) << 16;
1500 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* Have to read LSB bits after the MSB ones because
1502 the reading of the MSB bits has frozen the
1503 LSBs (backward from the ADM1027).
1504 */
1505 int ext1 = lm85_read_value(client,
1506 EMC6D102_REG_EXTEND_ADC1);
1507 int ext2 = lm85_read_value(client,
1508 EMC6D102_REG_EXTEND_ADC2);
1509 int ext3 = lm85_read_value(client,
1510 EMC6D102_REG_EXTEND_ADC3);
1511 int ext4 = lm85_read_value(client,
1512 EMC6D102_REG_EXTEND_ADC4);
1513 data->in_ext[0] = ext3 & 0x0f;
1514 data->in_ext[1] = ext4 & 0x0f;
1515 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1516 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1517 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1518
1519 data->temp_ext[0] = ext1 & 0x0f;
1520 data->temp_ext[1] = ext2 & 0x0f;
1521 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1522 }
1523
Jean Delvare1f448092008-04-29 14:03:37 +02001524 data->last_reading = jiffies;
1525 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Jean Delvare1f448092008-04-29 14:03:37 +02001527 if (!data->valid ||
1528 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* Things that don't change often */
1530 dev_dbg(&client->dev, "Reading config values\n");
1531
Jean Delvare9c516ef2005-11-26 20:07:54 +01001532 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 data->in_min[i] =
1534 lm85_read_value(client, LM85_REG_IN_MIN(i));
1535 data->in_max[i] =
1536 lm85_read_value(client, LM85_REG_IN_MAX(i));
1537 }
1538
Jean Delvare9c516ef2005-11-26 20:07:54 +01001539 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1540 data->in_min[4] = lm85_read_value(client,
1541 LM85_REG_IN_MIN(4));
1542 data->in_max[4] = lm85_read_value(client,
1543 LM85_REG_IN_MAX(4));
1544 }
1545
Jean Delvare1f448092008-04-29 14:03:37 +02001546 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001548 data->in_min[i] = lm85_read_value(client,
1549 EMC6D100_REG_IN_MIN(i));
1550 data->in_max[i] = lm85_read_value(client,
1551 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 }
1554
1555 for (i = 0; i <= 3; ++i) {
1556 data->fan_min[i] =
1557 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1558 }
1559
1560 for (i = 0; i <= 2; ++i) {
1561 data->temp_min[i] =
1562 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1563 data->temp_max[i] =
1564 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1565 }
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 for (i = 0; i <= 2; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001568 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 data->autofan[i].config =
1570 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1571 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare1f448092008-04-29 14:03:37 +02001572 data->autofan[i].freq = val & 0x07;
1573 data->zone[i].range = (val >> 4) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 data->autofan[i].min_pwm =
1575 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1576 data->zone[i].limit =
1577 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1578 data->zone[i].critical =
1579 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1580 }
1581
1582 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001583 data->smooth[0] = i & 0x0f;
1584 data->syncpwm3 = i & 0x10; /* Save PWM3 config */
1585 data->autofan[0].min_off = (i & 0x20) != 0;
1586 data->autofan[1].min_off = (i & 0x40) != 0;
1587 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
Jean Delvare1f448092008-04-29 14:03:37 +02001589 data->smooth[1] = (i >> 4) & 0x0f;
1590 data->smooth[2] = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvare1f448092008-04-29 14:03:37 +02001593 data->zone[0].hyst = (i >> 4) & 0x0f;
1594 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvare1f448092008-04-29 14:03:37 +02001597 data->zone[2].hyst = (i >> 4) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Jean Delvare1f448092008-04-29 14:03:37 +02001599 if (data->type == lm85b || data->type == lm85c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 data->tach_mode = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001601 LM85_REG_TACH_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 data->spinup_ctl = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001603 LM85_REG_SPINUP_CTL);
1604 } else if (data->type == adt7463 || data->type == adm1027) {
1605 if (data->type == adt7463) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 for (i = 0; i <= 2; ++i) {
1607 data->oppoint[i] = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001608 ADT7463_REG_OPPOINT(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 data->tmin_ctl = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001611 ADT7463_REG_TMIN_CTL1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 data->therm_limit = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001613 ADT7463_REG_THERM_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615 for (i = 0; i <= 2; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001616 data->temp_offset[i] = lm85_read_value(client,
1617 ADM1027_REG_TEMP_OFFSET(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 data->tach_mode = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001620 ADM1027_REG_CONFIG3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 data->fan_ppr = lm85_read_value(client,
Jean Delvare1f448092008-04-29 14:03:37 +02001622 ADM1027_REG_FAN_PPR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Jean Delvare1f448092008-04-29 14:03:37 +02001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001626 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 data->valid = 1;
1629
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001630 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 return data;
1633}
1634
1635
1636static int __init sm_lm85_init(void)
1637{
1638 return i2c_add_driver(&lm85_driver);
1639}
1640
Jean Delvare1f448092008-04-29 14:03:37 +02001641static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 i2c_del_driver(&lm85_driver);
1644}
1645
1646/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1647 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1648 * post 2.7.0 CVS changes.
1649 */
1650MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001651MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1652 "Margit Schubert-While <margitsw@t-online.de>, "
1653 "Justin Thiessen <jthiessen@penguincomputing.com");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1655
1656module_init(sm_lm85_init);
1657module_exit(sm_lm85_exit);