blob: 0d31435b333cf738eec2de726404bfde343c434d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define LM85_REG_COMPANY 0x3e
60#define LM85_REG_VERSTEP 0x3f
61/* These are the recognized values for the above regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define LM85_COMPANY_NATIONAL 0x01
63#define LM85_COMPANY_ANALOG_DEV 0x41
Jean Delvare1f448092008-04-29 14:03:37 +020064#define LM85_COMPANY_SMSC 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define LM85_VERSTEP_VMASK 0xf0
66#define LM85_VERSTEP_GENERIC 0x60
67#define LM85_VERSTEP_LM85C 0x60
68#define LM85_VERSTEP_LM85B 0x62
69#define LM85_VERSTEP_ADM1027 0x60
70#define LM85_VERSTEP_ADT7463 0x62
71#define LM85_VERSTEP_ADT7463C 0x6A
72#define LM85_VERSTEP_EMC6D100_A0 0x60
73#define LM85_VERSTEP_EMC6D100_A1 0x61
74#define LM85_VERSTEP_EMC6D102 0x65
75
76#define LM85_REG_CONFIG 0x40
77
78#define LM85_REG_ALARM1 0x41
79#define LM85_REG_ALARM2 0x42
80
81#define LM85_REG_VID 0x43
82
83/* Automated FAN control */
84#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
85#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
86#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
88#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
89#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
90#define LM85_REG_AFAN_HYST1 0x6d
91#define LM85_REG_AFAN_HYST2 0x6e
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define ADM1027_REG_EXTEND_ADC1 0x76
94#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#define EMC6D100_REG_ALARM3 0x7d
97/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +020098#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
99#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
100#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define EMC6D102_REG_EXTEND_ADC1 0x85
102#define EMC6D102_REG_EXTEND_ADC2 0x86
103#define EMC6D102_REG_EXTEND_ADC3 0x87
104#define EMC6D102_REG_EXTEND_ADC4 0x88
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jean Delvare1f448092008-04-29 14:03:37 +0200107/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 variants. Note that you should be a bit careful with which arguments
109 these macros are called: arguments may be evaluated more than once.
110 */
111
112/* IN are scaled acording to built-in resistors */
113static int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200114 2500, 2250, 3300, 5000, 12000,
115 3300, 1500, 1800 /*EMC6D100*/
116};
117#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jean Delvare1f448092008-04-29 14:03:37 +0200119#define INS_TO_REG(n, val) \
120 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jean Delvare1f448092008-04-29 14:03:37 +0200122#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200123 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jean Delvare1f448092008-04-29 14:03:37 +0200125#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200128static inline u16 FAN_TO_REG(unsigned long val)
129{
130 if (!val)
131 return 0xffff;
132 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
133}
Jean Delvare1f448092008-04-29 14:03:37 +0200134#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
135 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* Temperature is reported in .001 degC increments */
138#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200139 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
140#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200141 SCALE(((val) << 4) + (ext), 16, 1000)
142#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jean Delvare1f448092008-04-29 14:03:37 +0200144#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define PWM_FROM_REG(val) (val)
146
147
148/* ZONEs have the following parameters:
149 * Limit (low) temp, 1. degC
150 * Hysteresis (below limit), 1. degC (0-15)
151 * Range of speed control, .1 degC (2-80)
152 * Critical (high) temp, 1. degC
153 *
154 * FAN PWMs have the following parameters:
155 * Reference Zone, 1, 2, 3, etc.
156 * Spinup time, .05 sec
157 * PWM value at limit/low temp, 1 count
158 * PWM Frequency, 1. Hz
159 * PWM is Min or OFF below limit, flag
160 * Invert PWM output, flag
161 *
162 * Some chips filter the temp, others the fan.
163 * Filter constant (or disabled) .1 seconds
164 */
165
166/* These are the zone temperature range encodings in .001 degree C */
Jean Delvare1f448092008-04-29 14:03:37 +0200167static int lm85_range_map[] = {
168 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
169 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
170};
171
172static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int i;
175
Jean Delvared38b1492008-04-03 10:40:39 +0200176 if (range >= lm85_range_map[15])
Jean Delvare1f448092008-04-29 14:03:37 +0200177 return 15;
Jean Delvared38b1492008-04-03 10:40:39 +0200178
179 /* Find the closest match */
180 for (i = 14; i >= 0; --i) {
181 if (range >= lm85_range_map[i]) {
182 if ((lm85_range_map[i + 1] - range) <
183 (range - lm85_range_map[i]))
184 return i + 1;
185 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 }
Jean Delvared38b1492008-04-03 10:40:39 +0200188
189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
Jean Delvare1f448092008-04-29 14:03:37 +0200191#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/* These are the Acoustic Enhancement, or Temperature smoothing encodings
194 * NOTE: The enable/disable bit is INCLUDED in these encodings as the
195 * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
196 * is ignored, or set to 0.
197 */
198/* These are the PWM frequency encodings */
199static int lm85_freq_map[] = { /* .1 Hz */
Jean Delvare1f448092008-04-29 14:03:37 +0200200 100, 150, 230, 300, 380, 470, 620, 940
201};
202
203static int FREQ_TO_REG(int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int i;
206
Jean Delvare1f448092008-04-29 14:03:37 +0200207 if (freq >= lm85_freq_map[7])
208 return 7;
209 for (i = 0; i < 7; ++i)
210 if (freq <= lm85_freq_map[i])
211 break;
212 return i & 0x07;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Jean Delvare1f448092008-04-29 14:03:37 +0200214#define FREQ_FROM_REG(val) lm85_freq_map[(val) & 0x07]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216/* Since we can't use strings, I'm abusing these numbers
217 * to stand in for the following meanings:
218 * 1 -- PWM responds to Zone 1
219 * 2 -- PWM responds to Zone 2
220 * 3 -- PWM responds to Zone 3
221 * 23 -- PWM responds to the higher temp of Zone 2 or 3
222 * 123 -- PWM responds to highest of Zone 1, 2, or 3
223 * 0 -- PWM is always at 0% (ie, off)
224 * -1 -- PWM is always at 100%
225 * -2 -- PWM responds to manual control
226 */
227
228static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
Jean Delvare1f448092008-04-29 14:03:37 +0200229#define ZONE_FROM_REG(val) lm85_zone_map[((val) >> 5) & 0x07]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jean Delvare1f448092008-04-29 14:03:37 +0200231static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int i;
234
Jean Delvare1f448092008-04-29 14:03:37 +0200235 for (i = 0; i <= 7; ++i)
236 if (zone == lm85_zone_map[i])
237 break;
238 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 i = 3; /* Always 100% */
Jean Delvare1f448092008-04-29 14:03:37 +0200240 return (i & 0x07) << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Jean Delvare1f448092008-04-29 14:03:37 +0200243#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
244#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/* Chip sampling rates
247 *
248 * Some sensors are not updated more frequently than once per second
249 * so it doesn't make sense to read them more often than that.
250 * We cache the results and return the saved data if the driver
251 * is called again before a second has elapsed.
252 *
253 * Also, there is significant configuration data for this chip
254 * given the automatic PWM fan control that is possible. There
255 * are about 47 bytes of config data to only 22 bytes of actual
256 * readings. So, we keep the config data up to date in the cache
257 * when it is written and only sample it once every 1 *minute*
258 */
259#define LM85_DATA_INTERVAL (HZ + HZ / 2)
260#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* LM85 can automatically adjust fan speeds based on temperature
263 * This structure encapsulates an entire Zone config. There are
264 * three zones (one for each temperature input) on the lm85
265 */
266struct lm85_zone {
267 s8 limit; /* Low temp limit */
268 u8 hyst; /* Low limit hysteresis. (0-15) */
269 u8 range; /* Temp range, encoded */
270 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200271 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * to prevent "drift" as other autofan control
273 * values change.
274 */
Jean Delvare1f448092008-04-29 14:03:37 +0200275 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * to prevent "drift" as other autofan control
277 * values change.
278 */
279};
280
281struct lm85_autofan {
282 u8 config; /* Register value */
283 u8 freq; /* PWM frequency, encoded */
284 u8 min_pwm; /* Minimum PWM value, encoded */
285 u8 min_off; /* Min PWM or OFF below "limit", flag */
286};
287
Jean Delvareed6bafb2007-02-14 21:15:03 +0100288/* For each registered chip, we need to keep some data in memory.
289 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290struct lm85_data {
291 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700292 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 enum chips type;
294
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100295 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int valid; /* !=0 if following fields are valid */
297 unsigned long last_reading; /* In jiffies */
298 unsigned long last_config; /* In jiffies */
299
300 u8 in[8]; /* Register value */
301 u8 in_max[8]; /* Register value */
302 u8 in_min[8]; /* Register value */
303 s8 temp[3]; /* Register value */
304 s8 temp_min[3]; /* Register value */
305 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 u16 fan[4]; /* Register value */
307 u16 fan_min[4]; /* Register value */
308 u8 pwm[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u8 temp_ext[3]; /* Decoded values */
310 u8 in_ext[8]; /* Decoded values */
Jean Delvaredd1ac532008-05-01 08:47:33 +0200311 u8 smooth[1]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 u8 vid; /* Register value */
313 u8 vrm; /* VRM version */
314 u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 u32 alarms; /* Register encoding, combined */
316 struct lm85_autofan autofan[3];
317 struct lm85_zone zone[3];
318};
319
320static int lm85_attach_adapter(struct i2c_adapter *adapter);
321static int lm85_detect(struct i2c_adapter *adapter, int address,
322 int kind);
323static int lm85_detach_client(struct i2c_client *client);
324
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100325static int lm85_read_value(struct i2c_client *client, u8 reg);
326static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct lm85_data *lm85_update_device(struct device *dev);
328static void lm85_init_client(struct i2c_client *client);
329
330
331static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100332 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100333 .name = "lm85",
334 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 .attach_adapter = lm85_attach_adapter,
336 .detach_client = lm85_detach_client,
337};
338
339
340/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200341static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
342 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Jean Delvareb353a482007-07-05 20:36:12 +0200344 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200346 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Jean Delvareb353a482007-07-05 20:36:12 +0200348
349static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
350 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jean Delvareb353a482007-07-05 20:36:12 +0200352 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200354 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Jean Delvareb353a482007-07-05 20:36:12 +0200356
357static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
358 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Jean Delvareb353a482007-07-05 20:36:12 +0200360 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct i2c_client *client = to_i2c_client(dev);
362 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200363 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100365 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 data->fan_min[nr] = FAN_TO_REG(val);
367 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100368 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return count;
370}
371
372#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200373static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
374 show_fan, NULL, offset - 1); \
375static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
376 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378show_fan_offset(1);
379show_fan_offset(2);
380show_fan_offset(3);
381show_fan_offset(4);
382
383/* vid, vrm, alarms */
384
Jean Delvare1f448092008-04-29 14:03:37 +0200385static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
386 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100389 int vid;
390
391 if (data->type == adt7463 && (data->vid & 0x80)) {
392 /* 6-pin VID (VRM 10) */
393 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
394 } else {
395 /* 5-pin VID (VRM 9) */
396 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
397 }
398
399 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
403
Jean Delvare1f448092008-04-29 14:03:37 +0200404static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
405 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Jean Delvare90d66192007-10-08 18:24:35 +0200407 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return sprintf(buf, "%ld\n", (long) data->vrm);
409}
410
Jean Delvare1f448092008-04-29 14:03:37 +0200411static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
412 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100414 struct lm85_data *data = dev_get_drvdata(dev);
415 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return count;
417}
418
419static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
420
Jean Delvare1f448092008-04-29 14:03:37 +0200421static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
422 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200425 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
429
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200430static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
431 char *buf)
432{
433 int nr = to_sensor_dev_attr(attr)->index;
434 struct lm85_data *data = lm85_update_device(dev);
435 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
436}
437
438static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
439static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
440static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
441static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
442static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
443static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
444static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
445static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
446static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
447static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
448static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
449static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
450static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
451static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
452static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
453static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
454static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/* pwm */
457
Jean Delvareb353a482007-07-05 20:36:12 +0200458static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
459 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Jean Delvareb353a482007-07-05 20:36:12 +0200461 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200463 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
Jean Delvareb353a482007-07-05 20:36:12 +0200465
466static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
467 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Jean Delvareb353a482007-07-05 20:36:12 +0200469 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct i2c_client *client = to_i2c_client(dev);
471 struct lm85_data *data = i2c_get_clientdata(client);
472 long val = simple_strtol(buf, NULL, 10);
473
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100474 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 data->pwm[nr] = PWM_TO_REG(val);
476 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100477 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return count;
479}
Jean Delvareb353a482007-07-05 20:36:12 +0200480
481static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
482 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Jean Delvareb353a482007-07-05 20:36:12 +0200484 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100486 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100489 switch (pwm_zone) {
490 case -1: /* PWM is always at 100% */
491 enable = 0;
492 break;
493 case 0: /* PWM is always at 0% */
494 case -2: /* PWM responds to manual control */
495 enable = 1;
496 break;
497 default: /* PWM in automatic mode */
498 enable = 2;
499 }
500 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Jean Delvare455f7912007-12-03 23:28:42 +0100503static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
504 *attr, const char *buf, size_t count)
505{
506 int nr = to_sensor_dev_attr(attr)->index;
507 struct i2c_client *client = to_i2c_client(dev);
508 struct lm85_data *data = i2c_get_clientdata(client);
509 long val = simple_strtol(buf, NULL, 10);
510 u8 config;
511
512 switch (val) {
513 case 0:
514 config = 3;
515 break;
516 case 1:
517 config = 7;
518 break;
519 case 2:
520 /* Here we have to choose arbitrarily one of the 5 possible
521 configurations; I go for the safest */
522 config = 6;
523 break;
524 default:
525 return -EINVAL;
526 }
527
528 mutex_lock(&data->update_lock);
529 data->autofan[nr].config = lm85_read_value(client,
530 LM85_REG_AFAN_CONFIG(nr));
531 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
532 | (config << 5);
533 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
534 data->autofan[nr].config);
535 mutex_unlock(&data->update_lock);
536 return count;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200540static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
541 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100542static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
543 show_pwm_enable, set_pwm_enable, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545show_pwm_reg(1);
546show_pwm_reg(2);
547show_pwm_reg(3);
548
549/* Voltages */
550
Jean Delvareb353a482007-07-05 20:36:12 +0200551static ssize_t show_in(struct device *dev, struct device_attribute *attr,
552 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Jean Delvareb353a482007-07-05 20:36:12 +0200554 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200556 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
557 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Jean Delvareb353a482007-07-05 20:36:12 +0200559
Jean Delvare1f448092008-04-29 14:03:37 +0200560static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200561 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Jean Delvareb353a482007-07-05 20:36:12 +0200563 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200565 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Jean Delvareb353a482007-07-05 20:36:12 +0200567
568static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
569 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jean Delvareb353a482007-07-05 20:36:12 +0200571 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct i2c_client *client = to_i2c_client(dev);
573 struct lm85_data *data = i2c_get_clientdata(client);
574 long val = simple_strtol(buf, NULL, 10);
575
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100576 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 data->in_min[nr] = INS_TO_REG(nr, val);
578 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100579 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return count;
581}
Jean Delvareb353a482007-07-05 20:36:12 +0200582
583static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
584 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jean Delvareb353a482007-07-05 20:36:12 +0200586 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200588 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Jean Delvareb353a482007-07-05 20:36:12 +0200590
591static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Jean Delvareb353a482007-07-05 20:36:12 +0200594 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct i2c_client *client = to_i2c_client(dev);
596 struct lm85_data *data = i2c_get_clientdata(client);
597 long val = simple_strtol(buf, NULL, 10);
598
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100599 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 data->in_max[nr] = INS_TO_REG(nr, val);
601 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100602 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return count;
604}
Jean Delvareb353a482007-07-05 20:36:12 +0200605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200607static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
608 show_in, NULL, offset); \
609static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
610 show_in_min, set_in_min, offset); \
611static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
612 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614show_in_reg(0);
615show_in_reg(1);
616show_in_reg(2);
617show_in_reg(3);
618show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200619show_in_reg(5);
620show_in_reg(6);
621show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623/* Temps */
624
Jean Delvareb353a482007-07-05 20:36:12 +0200625static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
626 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Jean Delvareb353a482007-07-05 20:36:12 +0200628 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200630 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
631 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Jean Delvareb353a482007-07-05 20:36:12 +0200633
634static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
635 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jean Delvareb353a482007-07-05 20:36:12 +0200637 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200639 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Jean Delvareb353a482007-07-05 20:36:12 +0200641
642static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jean Delvareb353a482007-07-05 20:36:12 +0200645 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct i2c_client *client = to_i2c_client(dev);
647 struct lm85_data *data = i2c_get_clientdata(client);
648 long val = simple_strtol(buf, NULL, 10);
649
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 data->temp_min[nr] = TEMP_TO_REG(val);
652 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return count;
655}
Jean Delvareb353a482007-07-05 20:36:12 +0200656
657static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
658 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Jean Delvareb353a482007-07-05 20:36:12 +0200660 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200662 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
Jean Delvareb353a482007-07-05 20:36:12 +0200664
665static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
666 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Jean Delvareb353a482007-07-05 20:36:12 +0200668 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct i2c_client *client = to_i2c_client(dev);
670 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200671 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100673 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 data->temp_max[nr] = TEMP_TO_REG(val);
675 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100676 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return count;
678}
Jean Delvareb353a482007-07-05 20:36:12 +0200679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200681static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
682 show_temp, NULL, offset - 1); \
683static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
684 show_temp_min, set_temp_min, offset - 1); \
685static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
686 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688show_temp_reg(1);
689show_temp_reg(2);
690show_temp_reg(3);
691
692
693/* Automatic PWM control */
694
Jean Delvareb353a482007-07-05 20:36:12 +0200695static ssize_t show_pwm_auto_channels(struct device *dev,
696 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Jean Delvareb353a482007-07-05 20:36:12 +0200698 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200700 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
Jean Delvareb353a482007-07-05 20:36:12 +0200702
703static ssize_t set_pwm_auto_channels(struct device *dev,
704 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Jean Delvareb353a482007-07-05 20:36:12 +0200706 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct i2c_client *client = to_i2c_client(dev);
708 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200709 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100711 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200713 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
715 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100716 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return count;
718}
Jean Delvareb353a482007-07-05 20:36:12 +0200719
720static ssize_t show_pwm_auto_pwm_min(struct device *dev,
721 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jean Delvareb353a482007-07-05 20:36:12 +0200723 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200725 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Jean Delvareb353a482007-07-05 20:36:12 +0200727
728static ssize_t set_pwm_auto_pwm_min(struct device *dev,
729 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Jean Delvareb353a482007-07-05 20:36:12 +0200731 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct i2c_client *client = to_i2c_client(dev);
733 struct lm85_data *data = i2c_get_clientdata(client);
734 long val = simple_strtol(buf, NULL, 10);
735
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100736 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 data->autofan[nr].min_pwm = PWM_TO_REG(val);
738 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
739 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100740 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return count;
742}
Jean Delvareb353a482007-07-05 20:36:12 +0200743
744static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
745 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Jean Delvareb353a482007-07-05 20:36:12 +0200747 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200749 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Jean Delvareb353a482007-07-05 20:36:12 +0200751
752static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
753 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Jean Delvareb353a482007-07-05 20:36:12 +0200755 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct i2c_client *client = to_i2c_client(dev);
757 struct lm85_data *data = i2c_get_clientdata(client);
758 long val = simple_strtol(buf, NULL, 10);
759
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100760 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 data->autofan[nr].min_off = val;
762 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
763 | data->syncpwm3
764 | (data->autofan[0].min_off ? 0x20 : 0)
765 | (data->autofan[1].min_off ? 0x40 : 0)
Jean Delvare1f448092008-04-29 14:03:37 +0200766 | (data->autofan[2].min_off ? 0x80 : 0));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100767 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return count;
769}
Jean Delvareb353a482007-07-05 20:36:12 +0200770
771static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
772 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Jean Delvareb353a482007-07-05 20:36:12 +0200774 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200776 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
Jean Delvareb353a482007-07-05 20:36:12 +0200778
779static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
780 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
784 struct lm85_data *data = i2c_get_clientdata(client);
785 long val = simple_strtol(buf, NULL, 10);
786
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100787 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 data->autofan[nr].freq = FREQ_TO_REG(val);
789 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
790 (data->zone[nr].range << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200791 | data->autofan[nr].freq);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100792 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return count;
794}
Jean Delvareb353a482007-07-05 20:36:12 +0200795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200797static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
798 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
799 set_pwm_auto_channels, offset - 1); \
800static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
801 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
802 set_pwm_auto_pwm_min, offset - 1); \
803static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
804 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
805 set_pwm_auto_pwm_minctl, offset - 1); \
806static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
807 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
808 set_pwm_auto_pwm_freq, offset - 1);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810pwm_auto(1);
811pwm_auto(2);
812pwm_auto(3);
813
814/* Temperature settings for automatic PWM control */
815
Jean Delvareb353a482007-07-05 20:36:12 +0200816static ssize_t show_temp_auto_temp_off(struct device *dev,
817 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Jean Delvareb353a482007-07-05 20:36:12 +0200819 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200821 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 HYST_FROM_REG(data->zone[nr].hyst));
823}
Jean Delvareb353a482007-07-05 20:36:12 +0200824
825static ssize_t set_temp_auto_temp_off(struct device *dev,
826 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Jean Delvareb353a482007-07-05 20:36:12 +0200828 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct i2c_client *client = to_i2c_client(dev);
830 struct lm85_data *data = i2c_get_clientdata(client);
831 int min;
832 long val = simple_strtol(buf, NULL, 10);
833
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100834 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 min = TEMP_FROM_REG(data->zone[nr].limit);
836 data->zone[nr].off_desired = TEMP_TO_REG(val);
837 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200838 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 lm85_write_value(client, LM85_REG_AFAN_HYST1,
840 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200841 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else {
843 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200844 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100846 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return count;
848}
Jean Delvareb353a482007-07-05 20:36:12 +0200849
850static ssize_t show_temp_auto_temp_min(struct device *dev,
851 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Jean Delvareb353a482007-07-05 20:36:12 +0200853 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200855 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Jean Delvareb353a482007-07-05 20:36:12 +0200857
858static ssize_t set_temp_auto_temp_min(struct device *dev,
859 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jean Delvareb353a482007-07-05 20:36:12 +0200861 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct i2c_client *client = to_i2c_client(dev);
863 struct lm85_data *data = i2c_get_clientdata(client);
864 long val = simple_strtol(buf, NULL, 10);
865
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100866 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 data->zone[nr].limit = TEMP_TO_REG(val);
868 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
869 data->zone[nr].limit);
870
871/* Update temp_auto_max and temp_auto_range */
872 data->zone[nr].range = RANGE_TO_REG(
873 TEMP_FROM_REG(data->zone[nr].max_desired) -
874 TEMP_FROM_REG(data->zone[nr].limit));
875 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
876 ((data->zone[nr].range & 0x0f) << 4)
877 | (data->autofan[nr].freq & 0x07));
878
879/* Update temp_auto_hyst and temp_auto_off */
880 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
881 data->zone[nr].limit) - TEMP_FROM_REG(
882 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200883 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 lm85_write_value(client, LM85_REG_AFAN_HYST1,
885 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200886 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else {
888 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200889 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100891 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return count;
893}
Jean Delvareb353a482007-07-05 20:36:12 +0200894
895static ssize_t show_temp_auto_temp_max(struct device *dev,
896 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jean Delvareb353a482007-07-05 20:36:12 +0200898 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200900 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 RANGE_FROM_REG(data->zone[nr].range));
902}
Jean Delvareb353a482007-07-05 20:36:12 +0200903
904static ssize_t set_temp_auto_temp_max(struct device *dev,
905 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jean Delvareb353a482007-07-05 20:36:12 +0200907 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct i2c_client *client = to_i2c_client(dev);
909 struct lm85_data *data = i2c_get_clientdata(client);
910 int min;
911 long val = simple_strtol(buf, NULL, 10);
912
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100913 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 min = TEMP_FROM_REG(data->zone[nr].limit);
915 data->zone[nr].max_desired = TEMP_TO_REG(val);
916 data->zone[nr].range = RANGE_TO_REG(
917 val - min);
918 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
919 ((data->zone[nr].range & 0x0f) << 4)
920 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100921 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return count;
923}
Jean Delvareb353a482007-07-05 20:36:12 +0200924
925static ssize_t show_temp_auto_temp_crit(struct device *dev,
926 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Jean Delvareb353a482007-07-05 20:36:12 +0200928 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200930 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
Jean Delvareb353a482007-07-05 20:36:12 +0200932
933static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200934 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jean Delvareb353a482007-07-05 20:36:12 +0200936 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct i2c_client *client = to_i2c_client(dev);
938 struct lm85_data *data = i2c_get_clientdata(client);
939 long val = simple_strtol(buf, NULL, 10);
940
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100941 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 data->zone[nr].critical = TEMP_TO_REG(val);
943 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
944 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100945 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return count;
947}
Jean Delvareb353a482007-07-05 20:36:12 +0200948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200950static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
951 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
952 set_temp_auto_temp_off, offset - 1); \
953static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
954 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
955 set_temp_auto_temp_min, offset - 1); \
956static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
957 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
958 set_temp_auto_temp_max, offset - 1); \
959static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
960 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
961 set_temp_auto_temp_crit, offset - 1);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963temp_auto(1);
964temp_auto(2);
965temp_auto(3);
966
Ben Dooksd8d20612005-10-26 21:05:46 +0200967static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 if (!(adapter->class & I2C_CLASS_HWMON))
970 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200971 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200974static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200975 &sensor_dev_attr_fan1_input.dev_attr.attr,
976 &sensor_dev_attr_fan2_input.dev_attr.attr,
977 &sensor_dev_attr_fan3_input.dev_attr.attr,
978 &sensor_dev_attr_fan4_input.dev_attr.attr,
979 &sensor_dev_attr_fan1_min.dev_attr.attr,
980 &sensor_dev_attr_fan2_min.dev_attr.attr,
981 &sensor_dev_attr_fan3_min.dev_attr.attr,
982 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200983 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
984 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
985 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
986 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200987
988 &sensor_dev_attr_pwm1.dev_attr.attr,
989 &sensor_dev_attr_pwm2.dev_attr.attr,
990 &sensor_dev_attr_pwm3.dev_attr.attr,
991 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
992 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
993 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
994
995 &sensor_dev_attr_in0_input.dev_attr.attr,
996 &sensor_dev_attr_in1_input.dev_attr.attr,
997 &sensor_dev_attr_in2_input.dev_attr.attr,
998 &sensor_dev_attr_in3_input.dev_attr.attr,
999 &sensor_dev_attr_in0_min.dev_attr.attr,
1000 &sensor_dev_attr_in1_min.dev_attr.attr,
1001 &sensor_dev_attr_in2_min.dev_attr.attr,
1002 &sensor_dev_attr_in3_min.dev_attr.attr,
1003 &sensor_dev_attr_in0_max.dev_attr.attr,
1004 &sensor_dev_attr_in1_max.dev_attr.attr,
1005 &sensor_dev_attr_in2_max.dev_attr.attr,
1006 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001007 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1008 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1009 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1010 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001011
1012 &sensor_dev_attr_temp1_input.dev_attr.attr,
1013 &sensor_dev_attr_temp2_input.dev_attr.attr,
1014 &sensor_dev_attr_temp3_input.dev_attr.attr,
1015 &sensor_dev_attr_temp1_min.dev_attr.attr,
1016 &sensor_dev_attr_temp2_min.dev_attr.attr,
1017 &sensor_dev_attr_temp3_min.dev_attr.attr,
1018 &sensor_dev_attr_temp1_max.dev_attr.attr,
1019 &sensor_dev_attr_temp2_max.dev_attr.attr,
1020 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001021 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1022 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1023 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1024 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1025 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001026
1027 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1028 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1029 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1030 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1031 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1032 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1033 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1034 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1035 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1036 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1037 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1038 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1039
1040 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1041 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1042 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1043 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1044 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1045 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1046 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1047 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1048 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1049 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1050 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1051 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1052
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001053 &dev_attr_vrm.attr,
1054 &dev_attr_cpu0_vid.attr,
1055 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001056 NULL
1057};
1058
1059static const struct attribute_group lm85_group = {
1060 .attrs = lm85_attributes,
1061};
1062
Jean Delvare6b9aad22007-07-05 20:37:21 +02001063static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001064 &sensor_dev_attr_in4_input.dev_attr.attr,
1065 &sensor_dev_attr_in4_min.dev_attr.attr,
1066 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001067 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001068 NULL
1069};
1070
Jean Delvare6b9aad22007-07-05 20:37:21 +02001071static const struct attribute_group lm85_group_in4 = {
1072 .attrs = lm85_attributes_in4,
1073};
1074
1075static struct attribute *lm85_attributes_in567[] = {
1076 &sensor_dev_attr_in5_input.dev_attr.attr,
1077 &sensor_dev_attr_in6_input.dev_attr.attr,
1078 &sensor_dev_attr_in7_input.dev_attr.attr,
1079 &sensor_dev_attr_in5_min.dev_attr.attr,
1080 &sensor_dev_attr_in6_min.dev_attr.attr,
1081 &sensor_dev_attr_in7_min.dev_attr.attr,
1082 &sensor_dev_attr_in5_max.dev_attr.attr,
1083 &sensor_dev_attr_in6_max.dev_attr.attr,
1084 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001085 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1086 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1087 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001088 NULL
1089};
1090
1091static const struct attribute_group lm85_group_in567 = {
1092 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001093};
1094
Ben Dooksd8d20612005-10-26 21:05:46 +02001095static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int kind)
1097{
Jean Delvare1f448092008-04-29 14:03:37 +02001098 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct i2c_client *new_client = NULL;
1100 struct lm85_data *data;
1101 int err = 0;
1102 const char *type_name = "";
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!i2c_check_functionality(adapter,
1105 I2C_FUNC_SMBUS_BYTE_DATA)) {
1106 /* We need to be able to do byte I/O */
Jean Delvare1f448092008-04-29 14:03:37 +02001107 goto ERROR0;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 /* OK. For now, we presume we have a valid client. We now create the
1111 client structure, even though we cannot fill it completely yet.
1112 But it allows us to access lm85_{read,write}_value. */
1113
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001114 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 err = -ENOMEM;
1116 goto ERROR0;
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 new_client = &data->client;
1120 i2c_set_clientdata(new_client, data);
1121 new_client->addr = address;
1122 new_client->adapter = adapter;
1123 new_client->driver = &lm85_driver;
1124 new_client->flags = 0;
1125
1126 /* Now, we do the remaining detection. */
1127
1128 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1129 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1130
1131 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1132 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1133 i2c_adapter_id(new_client->adapter), new_client->addr,
1134 company, verstep);
1135
1136 /* If auto-detecting, Determine the chip type. */
1137 if (kind <= 0) {
1138 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001139 i2c_adapter_id(adapter), address);
1140 if (company == LM85_COMPANY_NATIONAL
1141 && verstep == LM85_VERSTEP_LM85C) {
1142 kind = lm85c;
1143 } else if (company == LM85_COMPANY_NATIONAL
1144 && verstep == LM85_VERSTEP_LM85B) {
1145 kind = lm85b;
1146 } else if (company == LM85_COMPANY_NATIONAL
1147 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1149 " Defaulting to LM85.\n", verstep);
Jean Delvare1f448092008-04-29 14:03:37 +02001150 kind = any_chip;
1151 } else if (company == LM85_COMPANY_ANALOG_DEV
1152 && verstep == LM85_VERSTEP_ADM1027) {
1153 kind = adm1027;
1154 } else if (company == LM85_COMPANY_ANALOG_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 && (verstep == LM85_VERSTEP_ADT7463
Jean Delvare1f448092008-04-29 14:03:37 +02001156 || verstep == LM85_VERSTEP_ADT7463C)) {
1157 kind = adt7463;
1158 } else if (company == LM85_COMPANY_ANALOG_DEV
1159 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001161 " Defaulting to Generic LM85.\n", verstep);
1162 kind = any_chip;
1163 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 && (verstep == LM85_VERSTEP_EMC6D100_A0
Jean Delvare1f448092008-04-29 14:03:37 +02001165 || verstep == LM85_VERSTEP_EMC6D100_A1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Unfortunately, we can't tell a '100 from a '101
1167 * from the registers. Since a '101 is a '100
1168 * in a package with fewer pins and therefore no
1169 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1170 * inputs read 0, then it's a '101.
1171 */
Jean Delvare1f448092008-04-29 14:03:37 +02001172 kind = emc6d100;
1173 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 && verstep == LM85_VERSTEP_EMC6D102) {
Jean Delvare1f448092008-04-29 14:03:37 +02001175 kind = emc6d102;
1176 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1178 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1179 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001180 " Defaulting to Generic LM85.\n", verstep);
1181 kind = any_chip;
1182 } else if (kind == any_chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1184 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1185 /* Leave kind as "any_chip" */
1186 } else {
1187 dev_dbg(&adapter->dev, "Autodetection failed\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001188 /* Not an LM85... */
1189 if (kind == any_chip) { /* User used force=x,y */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1191 " found at %d,0x%02x. Try force_lm85c.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001192 i2c_adapter_id(adapter), address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Jean Delvare1f448092008-04-29 14:03:37 +02001194 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto ERROR1;
1196 }
1197 }
1198
1199 /* Fill in the chip specific driver values */
Jean Delvare1f448092008-04-29 14:03:37 +02001200 if (kind == any_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 type_name = "lm85";
Jean Delvare1f448092008-04-29 14:03:37 +02001202 else if (kind == lm85b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 type_name = "lm85b";
Jean Delvare1f448092008-04-29 14:03:37 +02001204 else if (kind == lm85c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 type_name = "lm85c";
Jean Delvare1f448092008-04-29 14:03:37 +02001206 else if (kind == adm1027)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 type_name = "adm1027";
Jean Delvare1f448092008-04-29 14:03:37 +02001208 else if (kind == adt7463)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 type_name = "adt7463";
Jean Delvare1f448092008-04-29 14:03:37 +02001210 else if (kind == emc6d100)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 type_name = "emc6d100";
Jean Delvare1f448092008-04-29 14:03:37 +02001212 else if (kind == emc6d102)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 type_name = "emc6d102";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1215
1216 /* Fill in the remaining client fields */
1217 data->type = kind;
1218 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001219 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* Tell the I2C layer a new client has arrived */
1222 if ((err = i2c_attach_client(new_client)))
1223 goto ERROR1;
1224
1225 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001226 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* Initialize the LM85 chip */
1229 lm85_init_client(new_client);
1230
1231 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001232 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001233 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Jean Delvare9c516ef2005-11-26 20:07:54 +01001235 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1236 as a sixth digital VID input rather than an analog input. */
1237 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001238 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001239 if ((err = sysfs_create_group(&new_client->dev.kobj,
1240 &lm85_group_in4)))
1241 goto ERROR3;
1242
1243 /* The EMC6D100 has 3 additional voltage inputs */
1244 if (kind == emc6d100)
1245 if ((err = sysfs_create_group(&new_client->dev.kobj,
1246 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001247 goto ERROR3;
1248
Tony Jones1beeffe2007-08-20 13:46:20 -07001249 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1250 if (IS_ERR(data->hwmon_dev)) {
1251 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001252 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001253 }
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return 0;
1256
1257 /* Error out and cleanup code */
Jean Delvare1f448092008-04-29 14:03:37 +02001258 ERROR3:
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001259 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001260 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1261 if (kind == emc6d100)
1262 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Jean Delvare1f448092008-04-29 14:03:37 +02001263 ERROR2:
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001264 i2c_detach_client(new_client);
Jean Delvare1f448092008-04-29 14:03:37 +02001265 ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 kfree(data);
Jean Delvare1f448092008-04-29 14:03:37 +02001267 ERROR0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return err;
1269}
1270
Ben Dooksd8d20612005-10-26 21:05:46 +02001271static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001273 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001274 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001275 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001276 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1277 if (data->type == emc6d100)
1278 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001280 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282}
1283
1284
Ben Dooksd8d20612005-10-26 21:05:46 +02001285static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 int res;
1288
1289 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001290 switch (reg) {
1291 case LM85_REG_FAN(0): /* Read WORD data */
1292 case LM85_REG_FAN(1):
1293 case LM85_REG_FAN(2):
1294 case LM85_REG_FAN(3):
1295 case LM85_REG_FAN_MIN(0):
1296 case LM85_REG_FAN_MIN(1):
1297 case LM85_REG_FAN_MIN(2):
1298 case LM85_REG_FAN_MIN(3):
1299 case LM85_REG_ALARM1: /* Read both bytes at once */
1300 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1301 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 default: /* Read BYTE data */
1304 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Jean Delvare1f448092008-04-29 14:03:37 +02001308 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Ben Dooksd8d20612005-10-26 21:05:46 +02001311static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Jean Delvare1f448092008-04-29 14:03:37 +02001313 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Jean Delvare1f448092008-04-29 14:03:37 +02001315 switch (reg) {
1316 case LM85_REG_FAN(0): /* Write WORD data */
1317 case LM85_REG_FAN(1):
1318 case LM85_REG_FAN(2):
1319 case LM85_REG_FAN(3):
1320 case LM85_REG_FAN_MIN(0):
1321 case LM85_REG_FAN_MIN(1):
1322 case LM85_REG_FAN_MIN(2):
1323 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /* NOTE: ALARM is read only, so not included here */
Jean Delvare1f448092008-04-29 14:03:37 +02001325 res = i2c_smbus_write_byte_data(client, reg, value & 0xff);
1326 res |= i2c_smbus_write_byte_data(client, reg + 1,
1327 (value >> 8) & 0xff);
1328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 default: /* Write BYTE data */
1330 res = i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Jean Delvare1f448092008-04-29 14:03:37 +02001334 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Ben Dooksd8d20612005-10-26 21:05:46 +02001337static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 int value;
1340 struct lm85_data *data = i2c_get_clientdata(client);
1341
1342 dev_dbg(&client->dev, "Initializing device\n");
1343
1344 /* Warn if part was not "READY" */
1345 value = lm85_read_value(client, LM85_REG_CONFIG);
1346 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
Jean Delvare1f448092008-04-29 14:03:37 +02001347 if (value & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001349 i2c_adapter_id(client->adapter), client->addr);
1350 }
1351 if (!(value & 0x04)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001353 i2c_adapter_id(client->adapter), client->addr);
1354 }
1355 if (value & 0x10
1356 && (data->type == adm1027
1357 || data->type == adt7463)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1359 "Please report this to the lm85 maintainer.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001360 i2c_adapter_id(client->adapter), client->addr);
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* WE INTENTIONALLY make no changes to the limits,
1364 * offsets, pwms, fans and zones. If they were
1365 * configured, we don't want to mess with them.
1366 * If they weren't, the default is 100% PWM, no
1367 * control and will suffice until 'sensors -s'
1368 * can be run by the user.
1369 */
1370
1371 /* Start monitoring */
1372 value = lm85_read_value(client, LM85_REG_CONFIG);
1373 /* Try to clear LOCK, Set START, save everything else */
Jean Delvare1f448092008-04-29 14:03:37 +02001374 value = (value & ~0x02) | 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1376 lm85_write_value(client, LM85_REG_CONFIG, value);
1377}
1378
1379static struct lm85_data *lm85_update_device(struct device *dev)
1380{
1381 struct i2c_client *client = to_i2c_client(dev);
1382 struct lm85_data *data = i2c_get_clientdata(client);
1383 int i;
1384
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001385 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jean Delvare1f448092008-04-29 14:03:37 +02001387 if (!data->valid ||
1388 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* Things that change quickly */
1390 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* Have to read extended bits first to "freeze" the
1393 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001394 * There are 2 additional resolution bits per channel and we
1395 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Jean Delvare1f448092008-04-29 14:03:37 +02001397 if (data->type == adm1027 || data->type == adt7463) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int ext1 = lm85_read_value(client,
1399 ADM1027_REG_EXTEND_ADC1);
1400 int ext2 = lm85_read_value(client,
1401 ADM1027_REG_EXTEND_ADC2);
1402 int val = (ext1 << 8) + ext2;
1403
Jean Delvare1f448092008-04-29 14:03:37 +02001404 for (i = 0; i <= 4; i++)
1405 data->in_ext[i] =
1406 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Jean Delvare1f448092008-04-29 14:03:37 +02001408 for (i = 0; i <= 2; i++)
1409 data->temp_ext[i] =
1410 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Jean Delvare9c516ef2005-11-26 20:07:54 +01001413 data->vid = lm85_read_value(client, LM85_REG_VID);
1414
1415 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 data->in[i] =
1417 lm85_read_value(client, LM85_REG_IN(i));
1418 }
1419
Jean Delvare9c516ef2005-11-26 20:07:54 +01001420 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1421 data->in[4] = lm85_read_value(client,
1422 LM85_REG_IN(4));
1423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 for (i = 0; i <= 3; ++i) {
1426 data->fan[i] =
1427 lm85_read_value(client, LM85_REG_FAN(i));
1428 }
1429
1430 for (i = 0; i <= 2; ++i) {
1431 data->temp[i] =
1432 lm85_read_value(client, LM85_REG_TEMP(i));
1433 }
1434
1435 for (i = 0; i <= 2; ++i) {
1436 data->pwm[i] =
1437 lm85_read_value(client, LM85_REG_PWM(i));
1438 }
1439
1440 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1441
Jean Delvaredd1ac532008-05-01 08:47:33 +02001442 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Three more voltage sensors */
1444 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001445 data->in[i] = lm85_read_value(client,
1446 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001449 data->alarms |= lm85_read_value(client,
1450 EMC6D100_REG_ALARM3) << 16;
1451 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* Have to read LSB bits after the MSB ones because
1453 the reading of the MSB bits has frozen the
1454 LSBs (backward from the ADM1027).
1455 */
1456 int ext1 = lm85_read_value(client,
1457 EMC6D102_REG_EXTEND_ADC1);
1458 int ext2 = lm85_read_value(client,
1459 EMC6D102_REG_EXTEND_ADC2);
1460 int ext3 = lm85_read_value(client,
1461 EMC6D102_REG_EXTEND_ADC3);
1462 int ext4 = lm85_read_value(client,
1463 EMC6D102_REG_EXTEND_ADC4);
1464 data->in_ext[0] = ext3 & 0x0f;
1465 data->in_ext[1] = ext4 & 0x0f;
1466 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1467 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1468 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1469
1470 data->temp_ext[0] = ext1 & 0x0f;
1471 data->temp_ext[1] = ext2 & 0x0f;
1472 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1473 }
1474
Jean Delvare1f448092008-04-29 14:03:37 +02001475 data->last_reading = jiffies;
1476 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Jean Delvare1f448092008-04-29 14:03:37 +02001478 if (!data->valid ||
1479 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* Things that don't change often */
1481 dev_dbg(&client->dev, "Reading config values\n");
1482
Jean Delvare9c516ef2005-11-26 20:07:54 +01001483 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 data->in_min[i] =
1485 lm85_read_value(client, LM85_REG_IN_MIN(i));
1486 data->in_max[i] =
1487 lm85_read_value(client, LM85_REG_IN_MAX(i));
1488 }
1489
Jean Delvare9c516ef2005-11-26 20:07:54 +01001490 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1491 data->in_min[4] = lm85_read_value(client,
1492 LM85_REG_IN_MIN(4));
1493 data->in_max[4] = lm85_read_value(client,
1494 LM85_REG_IN_MAX(4));
1495 }
1496
Jean Delvare1f448092008-04-29 14:03:37 +02001497 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001499 data->in_min[i] = lm85_read_value(client,
1500 EMC6D100_REG_IN_MIN(i));
1501 data->in_max[i] = lm85_read_value(client,
1502 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 }
1505
1506 for (i = 0; i <= 3; ++i) {
1507 data->fan_min[i] =
1508 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1509 }
1510
1511 for (i = 0; i <= 2; ++i) {
1512 data->temp_min[i] =
1513 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1514 data->temp_max[i] =
1515 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1516 }
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 for (i = 0; i <= 2; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001519 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 data->autofan[i].config =
1521 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1522 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare1f448092008-04-29 14:03:37 +02001523 data->autofan[i].freq = val & 0x07;
1524 data->zone[i].range = (val >> 4) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 data->autofan[i].min_pwm =
1526 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1527 data->zone[i].limit =
1528 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1529 data->zone[i].critical =
1530 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1531 }
1532
1533 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001534 data->smooth[0] = i & 0x0f;
1535 data->syncpwm3 = i & 0x10; /* Save PWM3 config */
1536 data->autofan[0].min_off = (i & 0x20) != 0;
1537 data->autofan[1].min_off = (i & 0x40) != 0;
1538 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvare1f448092008-04-29 14:03:37 +02001541 data->zone[0].hyst = (i >> 4) & 0x0f;
1542 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvare1f448092008-04-29 14:03:37 +02001545 data->zone[2].hyst = (i >> 4) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001548 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 data->valid = 1;
1551
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001552 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 return data;
1555}
1556
1557
1558static int __init sm_lm85_init(void)
1559{
1560 return i2c_add_driver(&lm85_driver);
1561}
1562
Jean Delvare1f448092008-04-29 14:03:37 +02001563static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 i2c_del_driver(&lm85_driver);
1566}
1567
1568/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1569 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1570 * post 2.7.0 CVS changes.
1571 */
1572MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001573MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1574 "Margit Schubert-While <margitsw@t-online.de>, "
1575 "Justin Thiessen <jthiessen@penguincomputing.com");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1577
1578module_init(sm_lm85_init);
1579module_exit(sm_lm85_exit);