blob: 645b98c187c2056789f52904211ec300d3477142 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 u8 vid; /* Register value */
312 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 u32 alarms; /* Register encoding, combined */
314 struct lm85_autofan autofan[3];
315 struct lm85_zone zone[3];
316};
317
318static int lm85_attach_adapter(struct i2c_adapter *adapter);
319static int lm85_detect(struct i2c_adapter *adapter, int address,
320 int kind);
321static int lm85_detach_client(struct i2c_client *client);
322
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100323static int lm85_read_value(struct i2c_client *client, u8 reg);
324static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static struct lm85_data *lm85_update_device(struct device *dev);
326static void lm85_init_client(struct i2c_client *client);
327
328
329static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100330 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100331 .name = "lm85",
332 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .attach_adapter = lm85_attach_adapter,
334 .detach_client = lm85_detach_client,
335};
336
337
338/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200339static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
340 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Jean Delvareb353a482007-07-05 20:36:12 +0200342 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200344 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Jean Delvareb353a482007-07-05 20:36:12 +0200346
347static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
348 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Jean Delvareb353a482007-07-05 20:36:12 +0200350 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200352 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
Jean Delvareb353a482007-07-05 20:36:12 +0200354
355static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jean Delvareb353a482007-07-05 20:36:12 +0200358 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct i2c_client *client = to_i2c_client(dev);
360 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200361 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100363 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 data->fan_min[nr] = FAN_TO_REG(val);
365 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100366 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return count;
368}
369
370#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200371static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
372 show_fan, NULL, offset - 1); \
373static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
374 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376show_fan_offset(1);
377show_fan_offset(2);
378show_fan_offset(3);
379show_fan_offset(4);
380
381/* vid, vrm, alarms */
382
Jean Delvare1f448092008-04-29 14:03:37 +0200383static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
384 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100387 int vid;
388
389 if (data->type == adt7463 && (data->vid & 0x80)) {
390 /* 6-pin VID (VRM 10) */
391 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
392 } else {
393 /* 5-pin VID (VRM 9) */
394 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
395 }
396
397 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
401
Jean Delvare1f448092008-04-29 14:03:37 +0200402static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
403 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Jean Delvare90d66192007-10-08 18:24:35 +0200405 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return sprintf(buf, "%ld\n", (long) data->vrm);
407}
408
Jean Delvare1f448092008-04-29 14:03:37 +0200409static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100412 struct lm85_data *data = dev_get_drvdata(dev);
413 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return count;
415}
416
417static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
418
Jean Delvare1f448092008-04-29 14:03:37 +0200419static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
420 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200423 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
427
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200428static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
429 char *buf)
430{
431 int nr = to_sensor_dev_attr(attr)->index;
432 struct lm85_data *data = lm85_update_device(dev);
433 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
434}
435
436static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
437static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
438static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
439static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
440static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
441static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
442static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
443static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
444static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
445static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
446static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
447static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
448static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
449static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
450static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
451static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
452static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* pwm */
455
Jean Delvareb353a482007-07-05 20:36:12 +0200456static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
457 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Jean Delvareb353a482007-07-05 20:36:12 +0200459 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200461 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
Jean Delvareb353a482007-07-05 20:36:12 +0200463
464static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
465 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Jean Delvareb353a482007-07-05 20:36:12 +0200467 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct i2c_client *client = to_i2c_client(dev);
469 struct lm85_data *data = i2c_get_clientdata(client);
470 long val = simple_strtol(buf, NULL, 10);
471
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100472 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 data->pwm[nr] = PWM_TO_REG(val);
474 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100475 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return count;
477}
Jean Delvareb353a482007-07-05 20:36:12 +0200478
479static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
480 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jean Delvareb353a482007-07-05 20:36:12 +0200482 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100484 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100487 switch (pwm_zone) {
488 case -1: /* PWM is always at 100% */
489 enable = 0;
490 break;
491 case 0: /* PWM is always at 0% */
492 case -2: /* PWM responds to manual control */
493 enable = 1;
494 break;
495 default: /* PWM in automatic mode */
496 enable = 2;
497 }
498 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Jean Delvare455f7912007-12-03 23:28:42 +0100501static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
502 *attr, const char *buf, size_t count)
503{
504 int nr = to_sensor_dev_attr(attr)->index;
505 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 u8 config;
509
510 switch (val) {
511 case 0:
512 config = 3;
513 break;
514 case 1:
515 config = 7;
516 break;
517 case 2:
518 /* Here we have to choose arbitrarily one of the 5 possible
519 configurations; I go for the safest */
520 config = 6;
521 break;
522 default:
523 return -EINVAL;
524 }
525
526 mutex_lock(&data->update_lock);
527 data->autofan[nr].config = lm85_read_value(client,
528 LM85_REG_AFAN_CONFIG(nr));
529 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
530 | (config << 5);
531 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
532 data->autofan[nr].config);
533 mutex_unlock(&data->update_lock);
534 return count;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200538static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
539 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100540static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
541 show_pwm_enable, set_pwm_enable, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543show_pwm_reg(1);
544show_pwm_reg(2);
545show_pwm_reg(3);
546
547/* Voltages */
548
Jean Delvareb353a482007-07-05 20:36:12 +0200549static ssize_t show_in(struct device *dev, struct device_attribute *attr,
550 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jean Delvareb353a482007-07-05 20:36:12 +0200552 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200554 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
555 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
Jean Delvareb353a482007-07-05 20:36:12 +0200557
Jean Delvare1f448092008-04-29 14:03:37 +0200558static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200559 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jean Delvareb353a482007-07-05 20:36:12 +0200561 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200563 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
Jean Delvareb353a482007-07-05 20:36:12 +0200565
566static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
567 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Jean Delvareb353a482007-07-05 20:36:12 +0200569 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct i2c_client *client = to_i2c_client(dev);
571 struct lm85_data *data = i2c_get_clientdata(client);
572 long val = simple_strtol(buf, NULL, 10);
573
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100574 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 data->in_min[nr] = INS_TO_REG(nr, val);
576 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100577 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return count;
579}
Jean Delvareb353a482007-07-05 20:36:12 +0200580
581static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
582 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Jean Delvareb353a482007-07-05 20:36:12 +0200584 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200586 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Jean Delvareb353a482007-07-05 20:36:12 +0200588
589static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
590 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Jean Delvareb353a482007-07-05 20:36:12 +0200592 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct i2c_client *client = to_i2c_client(dev);
594 struct lm85_data *data = i2c_get_clientdata(client);
595 long val = simple_strtol(buf, NULL, 10);
596
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100597 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 data->in_max[nr] = INS_TO_REG(nr, val);
599 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100600 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return count;
602}
Jean Delvareb353a482007-07-05 20:36:12 +0200603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200605static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
606 show_in, NULL, offset); \
607static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
608 show_in_min, set_in_min, offset); \
609static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
610 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612show_in_reg(0);
613show_in_reg(1);
614show_in_reg(2);
615show_in_reg(3);
616show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200617show_in_reg(5);
618show_in_reg(6);
619show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621/* Temps */
622
Jean Delvareb353a482007-07-05 20:36:12 +0200623static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
624 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Jean Delvareb353a482007-07-05 20:36:12 +0200626 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200628 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
629 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
Jean Delvareb353a482007-07-05 20:36:12 +0200631
632static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
633 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jean Delvareb353a482007-07-05 20:36:12 +0200635 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200637 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
Jean Delvareb353a482007-07-05 20:36:12 +0200639
640static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Jean Delvareb353a482007-07-05 20:36:12 +0200643 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct i2c_client *client = to_i2c_client(dev);
645 struct lm85_data *data = i2c_get_clientdata(client);
646 long val = simple_strtol(buf, NULL, 10);
647
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100648 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 data->temp_min[nr] = TEMP_TO_REG(val);
650 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100651 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return count;
653}
Jean Delvareb353a482007-07-05 20:36:12 +0200654
655static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
656 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Jean Delvareb353a482007-07-05 20:36:12 +0200658 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200660 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
Jean Delvareb353a482007-07-05 20:36:12 +0200662
663static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
664 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Jean Delvareb353a482007-07-05 20:36:12 +0200666 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct i2c_client *client = to_i2c_client(dev);
668 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200669 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100671 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 data->temp_max[nr] = TEMP_TO_REG(val);
673 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100674 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return count;
676}
Jean Delvareb353a482007-07-05 20:36:12 +0200677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200679static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
680 show_temp, NULL, offset - 1); \
681static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
682 show_temp_min, set_temp_min, offset - 1); \
683static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
684 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686show_temp_reg(1);
687show_temp_reg(2);
688show_temp_reg(3);
689
690
691/* Automatic PWM control */
692
Jean Delvareb353a482007-07-05 20:36:12 +0200693static ssize_t show_pwm_auto_channels(struct device *dev,
694 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Jean Delvareb353a482007-07-05 20:36:12 +0200696 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200698 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
Jean Delvareb353a482007-07-05 20:36:12 +0200700
701static ssize_t set_pwm_auto_channels(struct device *dev,
702 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jean Delvareb353a482007-07-05 20:36:12 +0200704 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct i2c_client *client = to_i2c_client(dev);
706 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200707 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100709 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200711 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
713 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100714 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return count;
716}
Jean Delvareb353a482007-07-05 20:36:12 +0200717
718static ssize_t show_pwm_auto_pwm_min(struct device *dev,
719 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Jean Delvareb353a482007-07-05 20:36:12 +0200721 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200723 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
Jean Delvareb353a482007-07-05 20:36:12 +0200725
726static ssize_t set_pwm_auto_pwm_min(struct device *dev,
727 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Jean Delvareb353a482007-07-05 20:36:12 +0200729 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct i2c_client *client = to_i2c_client(dev);
731 struct lm85_data *data = i2c_get_clientdata(client);
732 long val = simple_strtol(buf, NULL, 10);
733
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100734 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 data->autofan[nr].min_pwm = PWM_TO_REG(val);
736 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
737 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100738 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return count;
740}
Jean Delvareb353a482007-07-05 20:36:12 +0200741
742static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
743 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Jean Delvareb353a482007-07-05 20:36:12 +0200745 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200747 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
Jean Delvareb353a482007-07-05 20:36:12 +0200749
750static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
751 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Jean Delvareb353a482007-07-05 20:36:12 +0200753 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct i2c_client *client = to_i2c_client(dev);
755 struct lm85_data *data = i2c_get_clientdata(client);
756 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200757 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100759 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200761 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
762 tmp &= ~(0x20 << nr);
763 if (data->autofan[nr].min_off)
764 tmp |= 0x20 << nr;
765 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100766 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return count;
768}
Jean Delvareb353a482007-07-05 20:36:12 +0200769
770static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
771 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Jean Delvareb353a482007-07-05 20:36:12 +0200773 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200775 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Jean Delvareb353a482007-07-05 20:36:12 +0200777
778static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
779 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Jean Delvareb353a482007-07-05 20:36:12 +0200781 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct i2c_client *client = to_i2c_client(dev);
783 struct lm85_data *data = i2c_get_clientdata(client);
784 long val = simple_strtol(buf, NULL, 10);
785
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100786 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 data->autofan[nr].freq = FREQ_TO_REG(val);
788 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
789 (data->zone[nr].range << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200790 | data->autofan[nr].freq);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100791 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return count;
793}
Jean Delvareb353a482007-07-05 20:36:12 +0200794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200796static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
797 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
798 set_pwm_auto_channels, offset - 1); \
799static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
800 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
801 set_pwm_auto_pwm_min, offset - 1); \
802static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
803 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
804 set_pwm_auto_pwm_minctl, offset - 1); \
805static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
806 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
807 set_pwm_auto_pwm_freq, offset - 1);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809pwm_auto(1);
810pwm_auto(2);
811pwm_auto(3);
812
813/* Temperature settings for automatic PWM control */
814
Jean Delvareb353a482007-07-05 20:36:12 +0200815static ssize_t show_temp_auto_temp_off(struct device *dev,
816 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Jean Delvareb353a482007-07-05 20:36:12 +0200818 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200820 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 HYST_FROM_REG(data->zone[nr].hyst));
822}
Jean Delvareb353a482007-07-05 20:36:12 +0200823
824static ssize_t set_temp_auto_temp_off(struct device *dev,
825 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Jean Delvareb353a482007-07-05 20:36:12 +0200827 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct i2c_client *client = to_i2c_client(dev);
829 struct lm85_data *data = i2c_get_clientdata(client);
830 int min;
831 long val = simple_strtol(buf, NULL, 10);
832
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100833 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 min = TEMP_FROM_REG(data->zone[nr].limit);
835 data->zone[nr].off_desired = TEMP_TO_REG(val);
836 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200837 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 lm85_write_value(client, LM85_REG_AFAN_HYST1,
839 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200840 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } else {
842 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200843 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100845 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return count;
847}
Jean Delvareb353a482007-07-05 20:36:12 +0200848
849static ssize_t show_temp_auto_temp_min(struct device *dev,
850 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvareb353a482007-07-05 20:36:12 +0200852 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200854 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
Jean Delvareb353a482007-07-05 20:36:12 +0200856
857static ssize_t set_temp_auto_temp_min(struct device *dev,
858 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Jean Delvareb353a482007-07-05 20:36:12 +0200860 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct i2c_client *client = to_i2c_client(dev);
862 struct lm85_data *data = i2c_get_clientdata(client);
863 long val = simple_strtol(buf, NULL, 10);
864
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100865 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 data->zone[nr].limit = TEMP_TO_REG(val);
867 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
868 data->zone[nr].limit);
869
870/* Update temp_auto_max and temp_auto_range */
871 data->zone[nr].range = RANGE_TO_REG(
872 TEMP_FROM_REG(data->zone[nr].max_desired) -
873 TEMP_FROM_REG(data->zone[nr].limit));
874 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
875 ((data->zone[nr].range & 0x0f) << 4)
876 | (data->autofan[nr].freq & 0x07));
877
878/* Update temp_auto_hyst and temp_auto_off */
879 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
880 data->zone[nr].limit) - TEMP_FROM_REG(
881 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200882 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 lm85_write_value(client, LM85_REG_AFAN_HYST1,
884 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200885 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 } else {
887 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200888 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return count;
892}
Jean Delvareb353a482007-07-05 20:36:12 +0200893
894static ssize_t show_temp_auto_temp_max(struct device *dev,
895 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Jean Delvareb353a482007-07-05 20:36:12 +0200897 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200899 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 RANGE_FROM_REG(data->zone[nr].range));
901}
Jean Delvareb353a482007-07-05 20:36:12 +0200902
903static ssize_t set_temp_auto_temp_max(struct device *dev,
904 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jean Delvareb353a482007-07-05 20:36:12 +0200906 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct i2c_client *client = to_i2c_client(dev);
908 struct lm85_data *data = i2c_get_clientdata(client);
909 int min;
910 long val = simple_strtol(buf, NULL, 10);
911
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100912 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 min = TEMP_FROM_REG(data->zone[nr].limit);
914 data->zone[nr].max_desired = TEMP_TO_REG(val);
915 data->zone[nr].range = RANGE_TO_REG(
916 val - min);
917 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
918 ((data->zone[nr].range & 0x0f) << 4)
919 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100920 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return count;
922}
Jean Delvareb353a482007-07-05 20:36:12 +0200923
924static ssize_t show_temp_auto_temp_crit(struct device *dev,
925 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Jean Delvareb353a482007-07-05 20:36:12 +0200927 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200929 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Jean Delvareb353a482007-07-05 20:36:12 +0200931
932static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200933 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Jean Delvareb353a482007-07-05 20:36:12 +0200935 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct i2c_client *client = to_i2c_client(dev);
937 struct lm85_data *data = i2c_get_clientdata(client);
938 long val = simple_strtol(buf, NULL, 10);
939
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100940 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 data->zone[nr].critical = TEMP_TO_REG(val);
942 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
943 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100944 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return count;
946}
Jean Delvareb353a482007-07-05 20:36:12 +0200947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200949static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
950 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
951 set_temp_auto_temp_off, offset - 1); \
952static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
953 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
954 set_temp_auto_temp_min, offset - 1); \
955static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
956 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
957 set_temp_auto_temp_max, offset - 1); \
958static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
959 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
960 set_temp_auto_temp_crit, offset - 1);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962temp_auto(1);
963temp_auto(2);
964temp_auto(3);
965
Ben Dooksd8d20612005-10-26 21:05:46 +0200966static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 if (!(adapter->class & I2C_CLASS_HWMON))
969 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200970 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200973static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200974 &sensor_dev_attr_fan1_input.dev_attr.attr,
975 &sensor_dev_attr_fan2_input.dev_attr.attr,
976 &sensor_dev_attr_fan3_input.dev_attr.attr,
977 &sensor_dev_attr_fan4_input.dev_attr.attr,
978 &sensor_dev_attr_fan1_min.dev_attr.attr,
979 &sensor_dev_attr_fan2_min.dev_attr.attr,
980 &sensor_dev_attr_fan3_min.dev_attr.attr,
981 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200982 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
983 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
984 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
985 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200986
987 &sensor_dev_attr_pwm1.dev_attr.attr,
988 &sensor_dev_attr_pwm2.dev_attr.attr,
989 &sensor_dev_attr_pwm3.dev_attr.attr,
990 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
991 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
992 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
993
994 &sensor_dev_attr_in0_input.dev_attr.attr,
995 &sensor_dev_attr_in1_input.dev_attr.attr,
996 &sensor_dev_attr_in2_input.dev_attr.attr,
997 &sensor_dev_attr_in3_input.dev_attr.attr,
998 &sensor_dev_attr_in0_min.dev_attr.attr,
999 &sensor_dev_attr_in1_min.dev_attr.attr,
1000 &sensor_dev_attr_in2_min.dev_attr.attr,
1001 &sensor_dev_attr_in3_min.dev_attr.attr,
1002 &sensor_dev_attr_in0_max.dev_attr.attr,
1003 &sensor_dev_attr_in1_max.dev_attr.attr,
1004 &sensor_dev_attr_in2_max.dev_attr.attr,
1005 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001006 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1007 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1008 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1009 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001010
1011 &sensor_dev_attr_temp1_input.dev_attr.attr,
1012 &sensor_dev_attr_temp2_input.dev_attr.attr,
1013 &sensor_dev_attr_temp3_input.dev_attr.attr,
1014 &sensor_dev_attr_temp1_min.dev_attr.attr,
1015 &sensor_dev_attr_temp2_min.dev_attr.attr,
1016 &sensor_dev_attr_temp3_min.dev_attr.attr,
1017 &sensor_dev_attr_temp1_max.dev_attr.attr,
1018 &sensor_dev_attr_temp2_max.dev_attr.attr,
1019 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001020 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1021 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1022 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1023 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1024 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001025
1026 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1027 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1028 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1029 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1030 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1031 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1032 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1033 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1034 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1035 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1036 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1037 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1038
1039 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1040 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1041 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1042 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1043 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1044 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1045 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1046 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1047 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1048 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1049 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1050 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1051
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001052 &dev_attr_vrm.attr,
1053 &dev_attr_cpu0_vid.attr,
1054 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001055 NULL
1056};
1057
1058static const struct attribute_group lm85_group = {
1059 .attrs = lm85_attributes,
1060};
1061
Jean Delvare6b9aad22007-07-05 20:37:21 +02001062static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001063 &sensor_dev_attr_in4_input.dev_attr.attr,
1064 &sensor_dev_attr_in4_min.dev_attr.attr,
1065 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001066 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001067 NULL
1068};
1069
Jean Delvare6b9aad22007-07-05 20:37:21 +02001070static const struct attribute_group lm85_group_in4 = {
1071 .attrs = lm85_attributes_in4,
1072};
1073
1074static struct attribute *lm85_attributes_in567[] = {
1075 &sensor_dev_attr_in5_input.dev_attr.attr,
1076 &sensor_dev_attr_in6_input.dev_attr.attr,
1077 &sensor_dev_attr_in7_input.dev_attr.attr,
1078 &sensor_dev_attr_in5_min.dev_attr.attr,
1079 &sensor_dev_attr_in6_min.dev_attr.attr,
1080 &sensor_dev_attr_in7_min.dev_attr.attr,
1081 &sensor_dev_attr_in5_max.dev_attr.attr,
1082 &sensor_dev_attr_in6_max.dev_attr.attr,
1083 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001084 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1085 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1086 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001087 NULL
1088};
1089
1090static const struct attribute_group lm85_group_in567 = {
1091 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001092};
1093
Ben Dooksd8d20612005-10-26 21:05:46 +02001094static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int kind)
1096{
Jean Delvare1f448092008-04-29 14:03:37 +02001097 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct i2c_client *new_client = NULL;
1099 struct lm85_data *data;
1100 int err = 0;
1101 const char *type_name = "";
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!i2c_check_functionality(adapter,
1104 I2C_FUNC_SMBUS_BYTE_DATA)) {
1105 /* We need to be able to do byte I/O */
Jean Delvare1f448092008-04-29 14:03:37 +02001106 goto ERROR0;
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* OK. For now, we presume we have a valid client. We now create the
1110 client structure, even though we cannot fill it completely yet.
1111 But it allows us to access lm85_{read,write}_value. */
1112
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001113 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 err = -ENOMEM;
1115 goto ERROR0;
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 new_client = &data->client;
1119 i2c_set_clientdata(new_client, data);
1120 new_client->addr = address;
1121 new_client->adapter = adapter;
1122 new_client->driver = &lm85_driver;
1123 new_client->flags = 0;
1124
1125 /* Now, we do the remaining detection. */
1126
1127 company = lm85_read_value(new_client, LM85_REG_COMPANY);
1128 verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
1129
1130 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1131 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1132 i2c_adapter_id(new_client->adapter), new_client->addr,
1133 company, verstep);
1134
1135 /* If auto-detecting, Determine the chip type. */
1136 if (kind <= 0) {
1137 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001138 i2c_adapter_id(adapter), address);
1139 if (company == LM85_COMPANY_NATIONAL
1140 && verstep == LM85_VERSTEP_LM85C) {
1141 kind = lm85c;
1142 } else if (company == LM85_COMPANY_NATIONAL
1143 && verstep == LM85_VERSTEP_LM85B) {
1144 kind = lm85b;
1145 } else if (company == LM85_COMPANY_NATIONAL
1146 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1148 " Defaulting to LM85.\n", verstep);
Jean Delvare1f448092008-04-29 14:03:37 +02001149 kind = any_chip;
1150 } else if (company == LM85_COMPANY_ANALOG_DEV
1151 && verstep == LM85_VERSTEP_ADM1027) {
1152 kind = adm1027;
1153 } else if (company == LM85_COMPANY_ANALOG_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 && (verstep == LM85_VERSTEP_ADT7463
Jean Delvare1f448092008-04-29 14:03:37 +02001155 || verstep == LM85_VERSTEP_ADT7463C)) {
1156 kind = adt7463;
1157 } else if (company == LM85_COMPANY_ANALOG_DEV
1158 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001160 " Defaulting to Generic LM85.\n", verstep);
1161 kind = any_chip;
1162 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 && (verstep == LM85_VERSTEP_EMC6D100_A0
Jean Delvare1f448092008-04-29 14:03:37 +02001164 || verstep == LM85_VERSTEP_EMC6D100_A1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* Unfortunately, we can't tell a '100 from a '101
1166 * from the registers. Since a '101 is a '100
1167 * in a package with fewer pins and therefore no
1168 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1169 * inputs read 0, then it's a '101.
1170 */
Jean Delvare1f448092008-04-29 14:03:37 +02001171 kind = emc6d100;
1172 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 && verstep == LM85_VERSTEP_EMC6D102) {
Jean Delvare1f448092008-04-29 14:03:37 +02001174 kind = emc6d102;
1175 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1177 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1178 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001179 " Defaulting to Generic LM85.\n", verstep);
1180 kind = any_chip;
1181 } else if (kind == any_chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1183 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1184 /* Leave kind as "any_chip" */
1185 } else {
1186 dev_dbg(&adapter->dev, "Autodetection failed\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001187 /* Not an LM85... */
1188 if (kind == any_chip) { /* User used force=x,y */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1190 " found at %d,0x%02x. Try force_lm85c.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001191 i2c_adapter_id(adapter), address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Jean Delvare1f448092008-04-29 14:03:37 +02001193 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto ERROR1;
1195 }
1196 }
1197
1198 /* Fill in the chip specific driver values */
Jean Delvare1f448092008-04-29 14:03:37 +02001199 if (kind == any_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 type_name = "lm85";
Jean Delvare1f448092008-04-29 14:03:37 +02001201 else if (kind == lm85b)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 type_name = "lm85b";
Jean Delvare1f448092008-04-29 14:03:37 +02001203 else if (kind == lm85c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 type_name = "lm85c";
Jean Delvare1f448092008-04-29 14:03:37 +02001205 else if (kind == adm1027)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 type_name = "adm1027";
Jean Delvare1f448092008-04-29 14:03:37 +02001207 else if (kind == adt7463)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 type_name = "adt7463";
Jean Delvare1f448092008-04-29 14:03:37 +02001209 else if (kind == emc6d100)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 type_name = "emc6d100";
Jean Delvare1f448092008-04-29 14:03:37 +02001211 else if (kind == emc6d102)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 type_name = "emc6d102";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1214
1215 /* Fill in the remaining client fields */
1216 data->type = kind;
1217 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001218 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* Tell the I2C layer a new client has arrived */
1221 if ((err = i2c_attach_client(new_client)))
1222 goto ERROR1;
1223
1224 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001225 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* Initialize the LM85 chip */
1228 lm85_init_client(new_client);
1229
1230 /* Register sysfs hooks */
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001231 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001232 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Jean Delvare9c516ef2005-11-26 20:07:54 +01001234 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1235 as a sixth digital VID input rather than an analog input. */
1236 data->vid = lm85_read_value(new_client, LM85_REG_VID);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001237 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvare6b9aad22007-07-05 20:37:21 +02001238 if ((err = sysfs_create_group(&new_client->dev.kobj,
1239 &lm85_group_in4)))
1240 goto ERROR3;
1241
1242 /* The EMC6D100 has 3 additional voltage inputs */
1243 if (kind == emc6d100)
1244 if ((err = sysfs_create_group(&new_client->dev.kobj,
1245 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001246 goto ERROR3;
1247
Tony Jones1beeffe2007-08-20 13:46:20 -07001248 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1249 if (IS_ERR(data->hwmon_dev)) {
1250 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001251 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001252 }
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255
1256 /* Error out and cleanup code */
Jean Delvare1f448092008-04-29 14:03:37 +02001257 ERROR3:
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001258 sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001259 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
1260 if (kind == emc6d100)
1261 sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
Jean Delvare1f448092008-04-29 14:03:37 +02001262 ERROR2:
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001263 i2c_detach_client(new_client);
Jean Delvare1f448092008-04-29 14:03:37 +02001264 ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 kfree(data);
Jean Delvare1f448092008-04-29 14:03:37 +02001266 ERROR0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return err;
1268}
1269
Ben Dooksd8d20612005-10-26 21:05:46 +02001270static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001272 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001273 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001274 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001275 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1276 if (data->type == emc6d100)
1277 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001279 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281}
1282
1283
Ben Dooksd8d20612005-10-26 21:05:46 +02001284static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 int res;
1287
1288 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001289 switch (reg) {
1290 case LM85_REG_FAN(0): /* Read WORD data */
1291 case LM85_REG_FAN(1):
1292 case LM85_REG_FAN(2):
1293 case LM85_REG_FAN(3):
1294 case LM85_REG_FAN_MIN(0):
1295 case LM85_REG_FAN_MIN(1):
1296 case LM85_REG_FAN_MIN(2):
1297 case LM85_REG_FAN_MIN(3):
1298 case LM85_REG_ALARM1: /* Read both bytes at once */
1299 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1300 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 default: /* Read BYTE data */
1303 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Jean Delvare1f448092008-04-29 14:03:37 +02001307 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Ben Dooksd8d20612005-10-26 21:05:46 +02001310static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Jean Delvare1f448092008-04-29 14:03:37 +02001312 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Jean Delvare1f448092008-04-29 14:03:37 +02001314 switch (reg) {
1315 case LM85_REG_FAN(0): /* Write WORD data */
1316 case LM85_REG_FAN(1):
1317 case LM85_REG_FAN(2):
1318 case LM85_REG_FAN(3):
1319 case LM85_REG_FAN_MIN(0):
1320 case LM85_REG_FAN_MIN(1):
1321 case LM85_REG_FAN_MIN(2):
1322 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* NOTE: ALARM is read only, so not included here */
Jean Delvare1f448092008-04-29 14:03:37 +02001324 res = i2c_smbus_write_byte_data(client, reg, value & 0xff);
1325 res |= i2c_smbus_write_byte_data(client, reg + 1,
1326 (value >> 8) & 0xff);
1327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 default: /* Write BYTE data */
1329 res = i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001330 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Jean Delvare1f448092008-04-29 14:03:37 +02001333 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Ben Dooksd8d20612005-10-26 21:05:46 +02001336static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 int value;
1339 struct lm85_data *data = i2c_get_clientdata(client);
1340
1341 dev_dbg(&client->dev, "Initializing device\n");
1342
1343 /* Warn if part was not "READY" */
1344 value = lm85_read_value(client, LM85_REG_CONFIG);
1345 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
Jean Delvare1f448092008-04-29 14:03:37 +02001346 if (value & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001348 i2c_adapter_id(client->adapter), client->addr);
1349 }
1350 if (!(value & 0x04)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001352 i2c_adapter_id(client->adapter), client->addr);
1353 }
1354 if (value & 0x10
1355 && (data->type == adm1027
1356 || data->type == adt7463)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1358 "Please report this to the lm85 maintainer.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001359 i2c_adapter_id(client->adapter), client->addr);
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* WE INTENTIONALLY make no changes to the limits,
1363 * offsets, pwms, fans and zones. If they were
1364 * configured, we don't want to mess with them.
1365 * If they weren't, the default is 100% PWM, no
1366 * control and will suffice until 'sensors -s'
1367 * can be run by the user.
1368 */
1369
1370 /* Start monitoring */
1371 value = lm85_read_value(client, LM85_REG_CONFIG);
1372 /* Try to clear LOCK, Set START, save everything else */
Jean Delvare1f448092008-04-29 14:03:37 +02001373 value = (value & ~0x02) | 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1375 lm85_write_value(client, LM85_REG_CONFIG, value);
1376}
1377
1378static struct lm85_data *lm85_update_device(struct device *dev)
1379{
1380 struct i2c_client *client = to_i2c_client(dev);
1381 struct lm85_data *data = i2c_get_clientdata(client);
1382 int i;
1383
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001384 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Jean Delvare1f448092008-04-29 14:03:37 +02001386 if (!data->valid ||
1387 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Things that change quickly */
1389 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* Have to read extended bits first to "freeze" the
1392 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001393 * There are 2 additional resolution bits per channel and we
1394 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
Jean Delvare1f448092008-04-29 14:03:37 +02001396 if (data->type == adm1027 || data->type == adt7463) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int ext1 = lm85_read_value(client,
1398 ADM1027_REG_EXTEND_ADC1);
1399 int ext2 = lm85_read_value(client,
1400 ADM1027_REG_EXTEND_ADC2);
1401 int val = (ext1 << 8) + ext2;
1402
Jean Delvare1f448092008-04-29 14:03:37 +02001403 for (i = 0; i <= 4; i++)
1404 data->in_ext[i] =
1405 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Jean Delvare1f448092008-04-29 14:03:37 +02001407 for (i = 0; i <= 2; i++)
1408 data->temp_ext[i] =
1409 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
Jean Delvare9c516ef2005-11-26 20:07:54 +01001412 data->vid = lm85_read_value(client, LM85_REG_VID);
1413
1414 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 data->in[i] =
1416 lm85_read_value(client, LM85_REG_IN(i));
1417 }
1418
Jean Delvare9c516ef2005-11-26 20:07:54 +01001419 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1420 data->in[4] = lm85_read_value(client,
1421 LM85_REG_IN(4));
1422 }
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 for (i = 0; i <= 3; ++i) {
1425 data->fan[i] =
1426 lm85_read_value(client, LM85_REG_FAN(i));
1427 }
1428
1429 for (i = 0; i <= 2; ++i) {
1430 data->temp[i] =
1431 lm85_read_value(client, LM85_REG_TEMP(i));
1432 }
1433
1434 for (i = 0; i <= 2; ++i) {
1435 data->pwm[i] =
1436 lm85_read_value(client, LM85_REG_PWM(i));
1437 }
1438
1439 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1440
Jean Delvaredd1ac532008-05-01 08:47:33 +02001441 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* Three more voltage sensors */
1443 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001444 data->in[i] = lm85_read_value(client,
1445 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001448 data->alarms |= lm85_read_value(client,
1449 EMC6D100_REG_ALARM3) << 16;
1450 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /* Have to read LSB bits after the MSB ones because
1452 the reading of the MSB bits has frozen the
1453 LSBs (backward from the ADM1027).
1454 */
1455 int ext1 = lm85_read_value(client,
1456 EMC6D102_REG_EXTEND_ADC1);
1457 int ext2 = lm85_read_value(client,
1458 EMC6D102_REG_EXTEND_ADC2);
1459 int ext3 = lm85_read_value(client,
1460 EMC6D102_REG_EXTEND_ADC3);
1461 int ext4 = lm85_read_value(client,
1462 EMC6D102_REG_EXTEND_ADC4);
1463 data->in_ext[0] = ext3 & 0x0f;
1464 data->in_ext[1] = ext4 & 0x0f;
1465 data->in_ext[2] = (ext4 >> 4) & 0x0f;
1466 data->in_ext[3] = (ext3 >> 4) & 0x0f;
1467 data->in_ext[4] = (ext2 >> 4) & 0x0f;
1468
1469 data->temp_ext[0] = ext1 & 0x0f;
1470 data->temp_ext[1] = ext2 & 0x0f;
1471 data->temp_ext[2] = (ext1 >> 4) & 0x0f;
1472 }
1473
Jean Delvare1f448092008-04-29 14:03:37 +02001474 data->last_reading = jiffies;
1475 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Jean Delvare1f448092008-04-29 14:03:37 +02001477 if (!data->valid ||
1478 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /* Things that don't change often */
1480 dev_dbg(&client->dev, "Reading config values\n");
1481
Jean Delvare9c516ef2005-11-26 20:07:54 +01001482 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 data->in_min[i] =
1484 lm85_read_value(client, LM85_REG_IN_MIN(i));
1485 data->in_max[i] =
1486 lm85_read_value(client, LM85_REG_IN_MAX(i));
1487 }
1488
Jean Delvare9c516ef2005-11-26 20:07:54 +01001489 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1490 data->in_min[4] = lm85_read_value(client,
1491 LM85_REG_IN_MIN(4));
1492 data->in_max[4] = lm85_read_value(client,
1493 LM85_REG_IN_MAX(4));
1494 }
1495
Jean Delvare1f448092008-04-29 14:03:37 +02001496 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001498 data->in_min[i] = lm85_read_value(client,
1499 EMC6D100_REG_IN_MIN(i));
1500 data->in_max[i] = lm85_read_value(client,
1501 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 }
1504
1505 for (i = 0; i <= 3; ++i) {
1506 data->fan_min[i] =
1507 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1508 }
1509
1510 for (i = 0; i <= 2; ++i) {
1511 data->temp_min[i] =
1512 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1513 data->temp_max[i] =
1514 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 for (i = 0; i <= 2; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001518 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 data->autofan[i].config =
1520 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1521 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare1f448092008-04-29 14:03:37 +02001522 data->autofan[i].freq = val & 0x07;
1523 data->zone[i].range = (val >> 4) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 data->autofan[i].min_pwm =
1525 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1526 data->zone[i].limit =
1527 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1528 data->zone[i].critical =
1529 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1530 }
1531
1532 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001533 data->autofan[0].min_off = (i & 0x20) != 0;
1534 data->autofan[1].min_off = (i & 0x40) != 0;
1535 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvare1f448092008-04-29 14:03:37 +02001538 data->zone[0].hyst = (i >> 4) & 0x0f;
1539 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvare1f448092008-04-29 14:03:37 +02001542 data->zone[2].hyst = (i >> 4) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001545 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 data->valid = 1;
1548
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001549 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 return data;
1552}
1553
1554
1555static int __init sm_lm85_init(void)
1556{
1557 return i2c_add_driver(&lm85_driver);
1558}
1559
Jean Delvare1f448092008-04-29 14:03:37 +02001560static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 i2c_del_driver(&lm85_driver);
1563}
1564
1565/* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
1566 * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
1567 * post 2.7.0 CVS changes.
1568 */
1569MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001570MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1571 "Margit Schubert-While <margitsw@t-online.de>, "
1572 "Justin Thiessen <jthiessen@penguincomputing.com");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1574
1575module_init(sm_lm85_init);
1576module_exit(sm_lm85_exit);