blob: c7e10af0173fd8a2870425df56081364f530e2e7 [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 */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400113static const 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 Delvaree89e22b2008-06-25 08:47:35 -0400167static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200168 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* These are the PWM frequency encodings */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400194static const int lm85_freq_map[] = { /* .1 Hz */
Jean Delvare1f448092008-04-29 14:03:37 +0200195 100, 150, 230, 300, 380, 470, 620, 940
196};
197
198static int FREQ_TO_REG(int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int i;
201
Jean Delvare1f448092008-04-29 14:03:37 +0200202 if (freq >= lm85_freq_map[7])
203 return 7;
204 for (i = 0; i < 7; ++i)
205 if (freq <= lm85_freq_map[i])
206 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400207 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
Jean Delvare1f448092008-04-29 14:03:37 +0200209#define FREQ_FROM_REG(val) lm85_freq_map[(val) & 0x07]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/* Since we can't use strings, I'm abusing these numbers
212 * to stand in for the following meanings:
213 * 1 -- PWM responds to Zone 1
214 * 2 -- PWM responds to Zone 2
215 * 3 -- PWM responds to Zone 3
216 * 23 -- PWM responds to the higher temp of Zone 2 or 3
217 * 123 -- PWM responds to highest of Zone 1, 2, or 3
218 * 0 -- PWM is always at 0% (ie, off)
219 * -1 -- PWM is always at 100%
220 * -2 -- PWM responds to manual control
221 */
222
Jean Delvaree89e22b2008-06-25 08:47:35 -0400223static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
224#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Jean Delvare1f448092008-04-29 14:03:37 +0200226static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 int i;
229
Jean Delvare1f448092008-04-29 14:03:37 +0200230 for (i = 0; i <= 7; ++i)
231 if (zone == lm85_zone_map[i])
232 break;
233 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400235 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Jean Delvare1f448092008-04-29 14:03:37 +0200238#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
239#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* Chip sampling rates
242 *
243 * Some sensors are not updated more frequently than once per second
244 * so it doesn't make sense to read them more often than that.
245 * We cache the results and return the saved data if the driver
246 * is called again before a second has elapsed.
247 *
248 * Also, there is significant configuration data for this chip
249 * given the automatic PWM fan control that is possible. There
250 * are about 47 bytes of config data to only 22 bytes of actual
251 * readings. So, we keep the config data up to date in the cache
252 * when it is written and only sample it once every 1 *minute*
253 */
254#define LM85_DATA_INTERVAL (HZ + HZ / 2)
255#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* LM85 can automatically adjust fan speeds based on temperature
258 * This structure encapsulates an entire Zone config. There are
259 * three zones (one for each temperature input) on the lm85
260 */
261struct lm85_zone {
262 s8 limit; /* Low temp limit */
263 u8 hyst; /* Low limit hysteresis. (0-15) */
264 u8 range; /* Temp range, encoded */
265 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200266 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * to prevent "drift" as other autofan control
268 * values change.
269 */
Jean Delvare1f448092008-04-29 14:03:37 +0200270 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * to prevent "drift" as other autofan control
272 * values change.
273 */
274};
275
276struct lm85_autofan {
277 u8 config; /* Register value */
278 u8 freq; /* PWM frequency, encoded */
279 u8 min_pwm; /* Minimum PWM value, encoded */
280 u8 min_off; /* Min PWM or OFF below "limit", flag */
281};
282
Jean Delvareed6bafb2007-02-14 21:15:03 +0100283/* For each registered chip, we need to keep some data in memory.
284 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285struct lm85_data {
286 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700287 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 enum chips type;
289
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100290 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int valid; /* !=0 if following fields are valid */
292 unsigned long last_reading; /* In jiffies */
293 unsigned long last_config; /* In jiffies */
294
295 u8 in[8]; /* Register value */
296 u8 in_max[8]; /* Register value */
297 u8 in_min[8]; /* Register value */
298 s8 temp[3]; /* Register value */
299 s8 temp_min[3]; /* Register value */
300 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 u16 fan[4]; /* Register value */
302 u16 fan_min[4]; /* Register value */
303 u8 pwm[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 u8 temp_ext[3]; /* Decoded values */
305 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 u8 vid; /* Register value */
307 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 u32 alarms; /* Register encoding, combined */
309 struct lm85_autofan autofan[3];
310 struct lm85_zone zone[3];
311};
312
313static int lm85_attach_adapter(struct i2c_adapter *adapter);
314static int lm85_detect(struct i2c_adapter *adapter, int address,
315 int kind);
316static int lm85_detach_client(struct i2c_client *client);
317
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100318static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400319static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static struct lm85_data *lm85_update_device(struct device *dev);
321static void lm85_init_client(struct i2c_client *client);
322
323
324static struct i2c_driver lm85_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100325 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100326 .name = "lm85",
327 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .attach_adapter = lm85_attach_adapter,
329 .detach_client = lm85_detach_client,
330};
331
332
333/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200334static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
335 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Jean Delvareb353a482007-07-05 20:36:12 +0200337 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200339 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
Jean Delvareb353a482007-07-05 20:36:12 +0200341
342static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
343 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Jean Delvareb353a482007-07-05 20:36:12 +0200345 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200347 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Jean Delvareb353a482007-07-05 20:36:12 +0200349
350static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
351 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Jean Delvareb353a482007-07-05 20:36:12 +0200353 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct i2c_client *client = to_i2c_client(dev);
355 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200356 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100358 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 data->fan_min[nr] = FAN_TO_REG(val);
360 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100361 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return count;
363}
364
365#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200366static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
367 show_fan, NULL, offset - 1); \
368static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
369 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371show_fan_offset(1);
372show_fan_offset(2);
373show_fan_offset(3);
374show_fan_offset(4);
375
376/* vid, vrm, alarms */
377
Jean Delvare1f448092008-04-29 14:03:37 +0200378static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
379 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100382 int vid;
383
384 if (data->type == adt7463 && (data->vid & 0x80)) {
385 /* 6-pin VID (VRM 10) */
386 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
387 } else {
388 /* 5-pin VID (VRM 9) */
389 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
390 }
391
392 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
396
Jean Delvare1f448092008-04-29 14:03:37 +0200397static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
398 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Jean Delvare90d66192007-10-08 18:24:35 +0200400 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return sprintf(buf, "%ld\n", (long) data->vrm);
402}
403
Jean Delvare1f448092008-04-29 14:03:37 +0200404static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
405 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100407 struct lm85_data *data = dev_get_drvdata(dev);
408 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return count;
410}
411
412static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
413
Jean Delvare1f448092008-04-29 14:03:37 +0200414static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
415 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200418 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
422
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200423static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
424 char *buf)
425{
426 int nr = to_sensor_dev_attr(attr)->index;
427 struct lm85_data *data = lm85_update_device(dev);
428 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
429}
430
431static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
432static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
433static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
434static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
435static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
436static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
437static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
438static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
439static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
440static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
441static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
442static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
443static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
444static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
445static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
446static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
447static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* pwm */
450
Jean Delvareb353a482007-07-05 20:36:12 +0200451static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
452 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jean Delvareb353a482007-07-05 20:36:12 +0200454 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200456 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
Jean Delvareb353a482007-07-05 20:36:12 +0200458
459static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
460 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Jean Delvareb353a482007-07-05 20:36:12 +0200462 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct i2c_client *client = to_i2c_client(dev);
464 struct lm85_data *data = i2c_get_clientdata(client);
465 long val = simple_strtol(buf, NULL, 10);
466
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100467 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 data->pwm[nr] = PWM_TO_REG(val);
469 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100470 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return count;
472}
Jean Delvareb353a482007-07-05 20:36:12 +0200473
474static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
475 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jean Delvareb353a482007-07-05 20:36:12 +0200477 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100479 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100482 switch (pwm_zone) {
483 case -1: /* PWM is always at 100% */
484 enable = 0;
485 break;
486 case 0: /* PWM is always at 0% */
487 case -2: /* PWM responds to manual control */
488 enable = 1;
489 break;
490 default: /* PWM in automatic mode */
491 enable = 2;
492 }
493 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Jean Delvare455f7912007-12-03 23:28:42 +0100496static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
497 *attr, const char *buf, size_t count)
498{
499 int nr = to_sensor_dev_attr(attr)->index;
500 struct i2c_client *client = to_i2c_client(dev);
501 struct lm85_data *data = i2c_get_clientdata(client);
502 long val = simple_strtol(buf, NULL, 10);
503 u8 config;
504
505 switch (val) {
506 case 0:
507 config = 3;
508 break;
509 case 1:
510 config = 7;
511 break;
512 case 2:
513 /* Here we have to choose arbitrarily one of the 5 possible
514 configurations; I go for the safest */
515 config = 6;
516 break;
517 default:
518 return -EINVAL;
519 }
520
521 mutex_lock(&data->update_lock);
522 data->autofan[nr].config = lm85_read_value(client,
523 LM85_REG_AFAN_CONFIG(nr));
524 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
525 | (config << 5);
526 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
527 data->autofan[nr].config);
528 mutex_unlock(&data->update_lock);
529 return count;
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200533static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
534 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100535static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
536 show_pwm_enable, set_pwm_enable, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538show_pwm_reg(1);
539show_pwm_reg(2);
540show_pwm_reg(3);
541
542/* Voltages */
543
Jean Delvareb353a482007-07-05 20:36:12 +0200544static ssize_t show_in(struct device *dev, struct device_attribute *attr,
545 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Jean Delvareb353a482007-07-05 20:36:12 +0200547 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200549 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
550 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Jean Delvareb353a482007-07-05 20:36:12 +0200552
Jean Delvare1f448092008-04-29 14:03:37 +0200553static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200554 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jean Delvareb353a482007-07-05 20:36:12 +0200556 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200558 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Jean Delvareb353a482007-07-05 20:36:12 +0200560
561static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
562 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jean Delvareb353a482007-07-05 20:36:12 +0200564 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct i2c_client *client = to_i2c_client(dev);
566 struct lm85_data *data = i2c_get_clientdata(client);
567 long val = simple_strtol(buf, NULL, 10);
568
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100569 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 data->in_min[nr] = INS_TO_REG(nr, val);
571 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100572 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return count;
574}
Jean Delvareb353a482007-07-05 20:36:12 +0200575
576static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
577 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jean Delvareb353a482007-07-05 20:36:12 +0200579 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200581 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
Jean Delvareb353a482007-07-05 20:36:12 +0200583
584static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
585 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Jean Delvareb353a482007-07-05 20:36:12 +0200587 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct i2c_client *client = to_i2c_client(dev);
589 struct lm85_data *data = i2c_get_clientdata(client);
590 long val = simple_strtol(buf, NULL, 10);
591
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100592 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 data->in_max[nr] = INS_TO_REG(nr, val);
594 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100595 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return count;
597}
Jean Delvareb353a482007-07-05 20:36:12 +0200598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200600static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
601 show_in, NULL, offset); \
602static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
603 show_in_min, set_in_min, offset); \
604static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
605 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607show_in_reg(0);
608show_in_reg(1);
609show_in_reg(2);
610show_in_reg(3);
611show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200612show_in_reg(5);
613show_in_reg(6);
614show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/* Temps */
617
Jean Delvareb353a482007-07-05 20:36:12 +0200618static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
619 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jean Delvareb353a482007-07-05 20:36:12 +0200621 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200623 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
624 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Jean Delvareb353a482007-07-05 20:36:12 +0200626
627static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
628 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jean Delvareb353a482007-07-05 20:36:12 +0200630 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200632 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Jean Delvareb353a482007-07-05 20:36:12 +0200634
635static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
636 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jean Delvareb353a482007-07-05 20:36:12 +0200638 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct i2c_client *client = to_i2c_client(dev);
640 struct lm85_data *data = i2c_get_clientdata(client);
641 long val = simple_strtol(buf, NULL, 10);
642
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100643 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 data->temp_min[nr] = TEMP_TO_REG(val);
645 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100646 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return count;
648}
Jean Delvareb353a482007-07-05 20:36:12 +0200649
650static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
651 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Jean Delvareb353a482007-07-05 20:36:12 +0200653 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200655 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Jean Delvareb353a482007-07-05 20:36:12 +0200657
658static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
659 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Jean Delvareb353a482007-07-05 20:36:12 +0200661 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 struct i2c_client *client = to_i2c_client(dev);
663 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200664 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100666 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 data->temp_max[nr] = TEMP_TO_REG(val);
668 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100669 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return count;
671}
Jean Delvareb353a482007-07-05 20:36:12 +0200672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200674static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
675 show_temp, NULL, offset - 1); \
676static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
677 show_temp_min, set_temp_min, offset - 1); \
678static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
679 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681show_temp_reg(1);
682show_temp_reg(2);
683show_temp_reg(3);
684
685
686/* Automatic PWM control */
687
Jean Delvareb353a482007-07-05 20:36:12 +0200688static ssize_t show_pwm_auto_channels(struct device *dev,
689 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jean Delvareb353a482007-07-05 20:36:12 +0200691 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200693 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
Jean Delvareb353a482007-07-05 20:36:12 +0200695
696static ssize_t set_pwm_auto_channels(struct device *dev,
697 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Jean Delvareb353a482007-07-05 20:36:12 +0200699 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct i2c_client *client = to_i2c_client(dev);
701 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200702 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100704 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200706 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
708 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100709 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return count;
711}
Jean Delvareb353a482007-07-05 20:36:12 +0200712
713static ssize_t show_pwm_auto_pwm_min(struct device *dev,
714 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Jean Delvareb353a482007-07-05 20:36:12 +0200716 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200718 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Jean Delvareb353a482007-07-05 20:36:12 +0200720
721static ssize_t set_pwm_auto_pwm_min(struct device *dev,
722 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Jean Delvareb353a482007-07-05 20:36:12 +0200724 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct i2c_client *client = to_i2c_client(dev);
726 struct lm85_data *data = i2c_get_clientdata(client);
727 long val = simple_strtol(buf, NULL, 10);
728
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 data->autofan[nr].min_pwm = PWM_TO_REG(val);
731 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
732 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return count;
735}
Jean Delvareb353a482007-07-05 20:36:12 +0200736
737static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
738 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jean Delvareb353a482007-07-05 20:36:12 +0200740 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200742 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Jean Delvareb353a482007-07-05 20:36:12 +0200744
745static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
746 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Jean Delvareb353a482007-07-05 20:36:12 +0200748 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct i2c_client *client = to_i2c_client(dev);
750 struct lm85_data *data = i2c_get_clientdata(client);
751 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200752 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100754 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200756 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
757 tmp &= ~(0x20 << nr);
758 if (data->autofan[nr].min_off)
759 tmp |= 0x20 << nr;
760 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100761 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return count;
763}
Jean Delvareb353a482007-07-05 20:36:12 +0200764
765static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
766 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Jean Delvareb353a482007-07-05 20:36:12 +0200768 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200770 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Jean Delvareb353a482007-07-05 20:36:12 +0200772
773static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
774 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jean Delvareb353a482007-07-05 20:36:12 +0200776 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct i2c_client *client = to_i2c_client(dev);
778 struct lm85_data *data = i2c_get_clientdata(client);
779 long val = simple_strtol(buf, NULL, 10);
780
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100781 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 data->autofan[nr].freq = FREQ_TO_REG(val);
783 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
784 (data->zone[nr].range << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200785 | data->autofan[nr].freq);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100786 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return count;
788}
Jean Delvareb353a482007-07-05 20:36:12 +0200789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200791static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
792 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
793 set_pwm_auto_channels, offset - 1); \
794static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
795 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
796 set_pwm_auto_pwm_min, offset - 1); \
797static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
798 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
799 set_pwm_auto_pwm_minctl, offset - 1); \
800static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
801 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
802 set_pwm_auto_pwm_freq, offset - 1);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804pwm_auto(1);
805pwm_auto(2);
806pwm_auto(3);
807
808/* Temperature settings for automatic PWM control */
809
Jean Delvareb353a482007-07-05 20:36:12 +0200810static ssize_t show_temp_auto_temp_off(struct device *dev,
811 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Jean Delvareb353a482007-07-05 20:36:12 +0200813 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200815 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 HYST_FROM_REG(data->zone[nr].hyst));
817}
Jean Delvareb353a482007-07-05 20:36:12 +0200818
819static ssize_t set_temp_auto_temp_off(struct device *dev,
820 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Jean Delvareb353a482007-07-05 20:36:12 +0200822 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct i2c_client *client = to_i2c_client(dev);
824 struct lm85_data *data = i2c_get_clientdata(client);
825 int min;
826 long val = simple_strtol(buf, NULL, 10);
827
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100828 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 min = TEMP_FROM_REG(data->zone[nr].limit);
830 data->zone[nr].off_desired = TEMP_TO_REG(val);
831 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200832 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 lm85_write_value(client, LM85_REG_AFAN_HYST1,
834 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200835 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 } else {
837 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200838 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100840 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return count;
842}
Jean Delvareb353a482007-07-05 20:36:12 +0200843
844static ssize_t show_temp_auto_temp_min(struct device *dev,
845 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Jean Delvareb353a482007-07-05 20:36:12 +0200847 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200849 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Jean Delvareb353a482007-07-05 20:36:12 +0200851
852static ssize_t set_temp_auto_temp_min(struct device *dev,
853 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Jean Delvareb353a482007-07-05 20:36:12 +0200855 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct i2c_client *client = to_i2c_client(dev);
857 struct lm85_data *data = i2c_get_clientdata(client);
858 long val = simple_strtol(buf, NULL, 10);
859
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100860 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 data->zone[nr].limit = TEMP_TO_REG(val);
862 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
863 data->zone[nr].limit);
864
865/* Update temp_auto_max and temp_auto_range */
866 data->zone[nr].range = RANGE_TO_REG(
867 TEMP_FROM_REG(data->zone[nr].max_desired) -
868 TEMP_FROM_REG(data->zone[nr].limit));
869 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
870 ((data->zone[nr].range & 0x0f) << 4)
871 | (data->autofan[nr].freq & 0x07));
872
873/* Update temp_auto_hyst and temp_auto_off */
874 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
875 data->zone[nr].limit) - TEMP_FROM_REG(
876 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200877 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 lm85_write_value(client, LM85_REG_AFAN_HYST1,
879 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200880 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 } else {
882 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200883 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100885 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return count;
887}
Jean Delvareb353a482007-07-05 20:36:12 +0200888
889static ssize_t show_temp_auto_temp_max(struct device *dev,
890 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jean Delvareb353a482007-07-05 20:36:12 +0200892 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200894 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 RANGE_FROM_REG(data->zone[nr].range));
896}
Jean Delvareb353a482007-07-05 20:36:12 +0200897
898static ssize_t set_temp_auto_temp_max(struct device *dev,
899 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jean Delvareb353a482007-07-05 20:36:12 +0200901 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct i2c_client *client = to_i2c_client(dev);
903 struct lm85_data *data = i2c_get_clientdata(client);
904 int min;
905 long val = simple_strtol(buf, NULL, 10);
906
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100907 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 min = TEMP_FROM_REG(data->zone[nr].limit);
909 data->zone[nr].max_desired = TEMP_TO_REG(val);
910 data->zone[nr].range = RANGE_TO_REG(
911 val - min);
912 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
913 ((data->zone[nr].range & 0x0f) << 4)
914 | (data->autofan[nr].freq & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100915 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return count;
917}
Jean Delvareb353a482007-07-05 20:36:12 +0200918
919static ssize_t show_temp_auto_temp_crit(struct device *dev,
920 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Jean Delvareb353a482007-07-05 20:36:12 +0200922 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200924 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
Jean Delvareb353a482007-07-05 20:36:12 +0200926
927static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200928 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Jean Delvareb353a482007-07-05 20:36:12 +0200930 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct i2c_client *client = to_i2c_client(dev);
932 struct lm85_data *data = i2c_get_clientdata(client);
933 long val = simple_strtol(buf, NULL, 10);
934
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100935 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 data->zone[nr].critical = TEMP_TO_REG(val);
937 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
938 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100939 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return count;
941}
Jean Delvareb353a482007-07-05 20:36:12 +0200942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200944static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
945 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
946 set_temp_auto_temp_off, offset - 1); \
947static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
948 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
949 set_temp_auto_temp_min, offset - 1); \
950static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
951 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
952 set_temp_auto_temp_max, offset - 1); \
953static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
954 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
955 set_temp_auto_temp_crit, offset - 1);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957temp_auto(1);
958temp_auto(2);
959temp_auto(3);
960
Ben Dooksd8d20612005-10-26 21:05:46 +0200961static int lm85_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 if (!(adapter->class & I2C_CLASS_HWMON))
964 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200965 return i2c_probe(adapter, &addr_data, lm85_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200968static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200969 &sensor_dev_attr_fan1_input.dev_attr.attr,
970 &sensor_dev_attr_fan2_input.dev_attr.attr,
971 &sensor_dev_attr_fan3_input.dev_attr.attr,
972 &sensor_dev_attr_fan4_input.dev_attr.attr,
973 &sensor_dev_attr_fan1_min.dev_attr.attr,
974 &sensor_dev_attr_fan2_min.dev_attr.attr,
975 &sensor_dev_attr_fan3_min.dev_attr.attr,
976 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200977 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
978 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
979 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
980 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200981
982 &sensor_dev_attr_pwm1.dev_attr.attr,
983 &sensor_dev_attr_pwm2.dev_attr.attr,
984 &sensor_dev_attr_pwm3.dev_attr.attr,
985 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
986 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
987 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
988
989 &sensor_dev_attr_in0_input.dev_attr.attr,
990 &sensor_dev_attr_in1_input.dev_attr.attr,
991 &sensor_dev_attr_in2_input.dev_attr.attr,
992 &sensor_dev_attr_in3_input.dev_attr.attr,
993 &sensor_dev_attr_in0_min.dev_attr.attr,
994 &sensor_dev_attr_in1_min.dev_attr.attr,
995 &sensor_dev_attr_in2_min.dev_attr.attr,
996 &sensor_dev_attr_in3_min.dev_attr.attr,
997 &sensor_dev_attr_in0_max.dev_attr.attr,
998 &sensor_dev_attr_in1_max.dev_attr.attr,
999 &sensor_dev_attr_in2_max.dev_attr.attr,
1000 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001001 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1002 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1003 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1004 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001005
1006 &sensor_dev_attr_temp1_input.dev_attr.attr,
1007 &sensor_dev_attr_temp2_input.dev_attr.attr,
1008 &sensor_dev_attr_temp3_input.dev_attr.attr,
1009 &sensor_dev_attr_temp1_min.dev_attr.attr,
1010 &sensor_dev_attr_temp2_min.dev_attr.attr,
1011 &sensor_dev_attr_temp3_min.dev_attr.attr,
1012 &sensor_dev_attr_temp1_max.dev_attr.attr,
1013 &sensor_dev_attr_temp2_max.dev_attr.attr,
1014 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001015 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1016 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1017 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1018 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1019 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001020
1021 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1022 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1023 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1024 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1025 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1026 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1027 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1028 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1029 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1030 &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
1031 &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
1032 &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
1033
1034 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1035 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1036 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1037 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1038 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1039 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1040 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1041 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1042 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1043 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1044 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1045 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1046
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001047 &dev_attr_vrm.attr,
1048 &dev_attr_cpu0_vid.attr,
1049 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001050 NULL
1051};
1052
1053static const struct attribute_group lm85_group = {
1054 .attrs = lm85_attributes,
1055};
1056
Jean Delvare6b9aad22007-07-05 20:37:21 +02001057static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001058 &sensor_dev_attr_in4_input.dev_attr.attr,
1059 &sensor_dev_attr_in4_min.dev_attr.attr,
1060 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001061 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001062 NULL
1063};
1064
Jean Delvare6b9aad22007-07-05 20:37:21 +02001065static const struct attribute_group lm85_group_in4 = {
1066 .attrs = lm85_attributes_in4,
1067};
1068
1069static struct attribute *lm85_attributes_in567[] = {
1070 &sensor_dev_attr_in5_input.dev_attr.attr,
1071 &sensor_dev_attr_in6_input.dev_attr.attr,
1072 &sensor_dev_attr_in7_input.dev_attr.attr,
1073 &sensor_dev_attr_in5_min.dev_attr.attr,
1074 &sensor_dev_attr_in6_min.dev_attr.attr,
1075 &sensor_dev_attr_in7_min.dev_attr.attr,
1076 &sensor_dev_attr_in5_max.dev_attr.attr,
1077 &sensor_dev_attr_in6_max.dev_attr.attr,
1078 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001079 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1080 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1081 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001082 NULL
1083};
1084
1085static const struct attribute_group lm85_group_in567 = {
1086 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001087};
1088
Ben Dooksd8d20612005-10-26 21:05:46 +02001089static int lm85_detect(struct i2c_adapter *adapter, int address,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int kind)
1091{
Jean Delvare1f448092008-04-29 14:03:37 +02001092 int company, verstep;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001093 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct lm85_data *data;
1095 int err = 0;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001096 const char *type_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jean Delvaree89e22b2008-06-25 08:47:35 -04001098 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* We need to be able to do byte I/O */
Jean Delvare1f448092008-04-29 14:03:37 +02001100 goto ERROR0;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* OK. For now, we presume we have a valid client. We now create the
1104 client structure, even though we cannot fill it completely yet.
1105 But it allows us to access lm85_{read,write}_value. */
1106
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001107 if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 err = -ENOMEM;
1109 goto ERROR0;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Jean Delvaree89e22b2008-06-25 08:47:35 -04001112 client = &data->client;
1113 i2c_set_clientdata(client, data);
1114 client->addr = address;
1115 client->adapter = adapter;
1116 client->driver = &lm85_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* Now, we do the remaining detection. */
1119
Jean Delvaree89e22b2008-06-25 08:47:35 -04001120 company = lm85_read_value(client, LM85_REG_COMPANY);
1121 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1124 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvaree89e22b2008-06-25 08:47:35 -04001125 i2c_adapter_id(client->adapter), client->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 company, verstep);
1127
1128 /* If auto-detecting, Determine the chip type. */
1129 if (kind <= 0) {
1130 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001131 i2c_adapter_id(adapter), address);
1132 if (company == LM85_COMPANY_NATIONAL
1133 && verstep == LM85_VERSTEP_LM85C) {
1134 kind = lm85c;
1135 } else if (company == LM85_COMPANY_NATIONAL
1136 && verstep == LM85_VERSTEP_LM85B) {
1137 kind = lm85b;
1138 } else if (company == LM85_COMPANY_NATIONAL
1139 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
1141 " Defaulting to LM85.\n", verstep);
Jean Delvare1f448092008-04-29 14:03:37 +02001142 kind = any_chip;
1143 } else if (company == LM85_COMPANY_ANALOG_DEV
1144 && verstep == LM85_VERSTEP_ADM1027) {
1145 kind = adm1027;
1146 } else if (company == LM85_COMPANY_ANALOG_DEV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 && (verstep == LM85_VERSTEP_ADT7463
Jean Delvare1f448092008-04-29 14:03:37 +02001148 || verstep == LM85_VERSTEP_ADT7463C)) {
1149 kind = adt7463;
1150 } else if (company == LM85_COMPANY_ANALOG_DEV
1151 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001153 " Defaulting to Generic LM85.\n", verstep);
1154 kind = any_chip;
1155 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 && (verstep == LM85_VERSTEP_EMC6D100_A0
Jean Delvare1f448092008-04-29 14:03:37 +02001157 || verstep == LM85_VERSTEP_EMC6D100_A1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /* Unfortunately, we can't tell a '100 from a '101
1159 * from the registers. Since a '101 is a '100
1160 * in a package with fewer pins and therefore no
1161 * 3.3V, 1.5V or 1.8V inputs, perhaps if those
1162 * inputs read 0, then it's a '101.
1163 */
Jean Delvare1f448092008-04-29 14:03:37 +02001164 kind = emc6d100;
1165 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 && verstep == LM85_VERSTEP_EMC6D102) {
Jean Delvare1f448092008-04-29 14:03:37 +02001167 kind = emc6d102;
1168 } else if (company == LM85_COMPANY_SMSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1170 dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
1171 dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
Jean Delvare1f448092008-04-29 14:03:37 +02001172 " Defaulting to Generic LM85.\n", verstep);
1173 kind = any_chip;
1174 } else if (kind == any_chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
1176 dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
1177 /* Leave kind as "any_chip" */
1178 } else {
1179 dev_dbg(&adapter->dev, "Autodetection failed\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001180 /* Not an LM85... */
1181 if (kind == any_chip) { /* User used force=x,y */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 dev_err(&adapter->dev, "Generic LM85 Version 6 not"
1183 " found at %d,0x%02x. Try force_lm85c.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001184 i2c_adapter_id(adapter), address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Jean Delvare1f448092008-04-29 14:03:37 +02001186 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto ERROR1;
1188 }
1189 }
1190
1191 /* Fill in the chip specific driver values */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001192 switch (kind) {
1193 case lm85b:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 type_name = "lm85b";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001195 break;
1196 case lm85c:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 type_name = "lm85c";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001198 break;
1199 case adm1027:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 type_name = "adm1027";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001201 break;
1202 case adt7463:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 type_name = "adt7463";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001204 break;
1205 case emc6d100:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 type_name = "emc6d100";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001207 break;
1208 case emc6d102:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 type_name = "emc6d102";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001210 break;
1211 default:
1212 type_name = "lm85";
1213 }
1214 strlcpy(client->name, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Fill in the remaining client fields */
1217 data->type = kind;
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 */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001221 err = i2c_attach_client(client);
1222 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 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 */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001229 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001232 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1233 if (err)
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001234 goto ERROR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Jean Delvare9c516ef2005-11-26 20:07:54 +01001236 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1237 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001238 data->vid = lm85_read_value(client, LM85_REG_VID);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001239 if (!(kind == adt7463 && (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001240 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001241 &lm85_group_in4)))
1242 goto ERROR3;
1243
1244 /* The EMC6D100 has 3 additional voltage inputs */
1245 if (kind == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001246 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001247 &lm85_group_in567)))
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001248 goto ERROR3;
1249
Jean Delvaree89e22b2008-06-25 08:47:35 -04001250 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001251 if (IS_ERR(data->hwmon_dev)) {
1252 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001253 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001254 }
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return 0;
1257
1258 /* Error out and cleanup code */
Jean Delvare1f448092008-04-29 14:03:37 +02001259 ERROR3:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001260 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1261 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001262 if (kind == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001263 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvare1f448092008-04-29 14:03:37 +02001264 ERROR2:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001265 i2c_detach_client(client);
Jean Delvare1f448092008-04-29 14:03:37 +02001266 ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 kfree(data);
Jean Delvare1f448092008-04-29 14:03:37 +02001268 ERROR0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return err;
1270}
1271
Ben Dooksd8d20612005-10-26 21:05:46 +02001272static int lm85_detach_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001274 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001275 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001276 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001277 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1278 if (data->type == emc6d100)
1279 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 i2c_detach_client(client);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001281 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return 0;
1283}
1284
1285
Ben Dooksd8d20612005-10-26 21:05:46 +02001286static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 int res;
1289
1290 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001291 switch (reg) {
1292 case LM85_REG_FAN(0): /* Read WORD data */
1293 case LM85_REG_FAN(1):
1294 case LM85_REG_FAN(2):
1295 case LM85_REG_FAN(3):
1296 case LM85_REG_FAN_MIN(0):
1297 case LM85_REG_FAN_MIN(1):
1298 case LM85_REG_FAN_MIN(2):
1299 case LM85_REG_FAN_MIN(3):
1300 case LM85_REG_ALARM1: /* Read both bytes at once */
1301 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1302 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 default: /* Read BYTE data */
1305 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Jean Delvare1f448092008-04-29 14:03:37 +02001309 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Jean Delvaree89e22b2008-06-25 08:47:35 -04001312static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
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 Delvaree89e22b2008-06-25 08:47:35 -04001324 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1325 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001328 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Ben Dooksd8d20612005-10-26 21:05:46 +02001333static void lm85_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 int value;
1336 struct lm85_data *data = i2c_get_clientdata(client);
1337
1338 dev_dbg(&client->dev, "Initializing device\n");
1339
1340 /* Warn if part was not "READY" */
1341 value = lm85_read_value(client, LM85_REG_CONFIG);
1342 dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
Jean Delvare1f448092008-04-29 14:03:37 +02001343 if (value & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001345 i2c_adapter_id(client->adapter), client->addr);
1346 }
1347 if (!(value & 0x04)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001349 i2c_adapter_id(client->adapter), client->addr);
1350 }
1351 if (value & 0x10
1352 && (data->type == adm1027
1353 || data->type == adt7463)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
1355 "Please report this to the lm85 maintainer.\n",
Jean Delvare1f448092008-04-29 14:03:37 +02001356 i2c_adapter_id(client->adapter), client->addr);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* WE INTENTIONALLY make no changes to the limits,
1360 * offsets, pwms, fans and zones. If they were
1361 * configured, we don't want to mess with them.
1362 * If they weren't, the default is 100% PWM, no
1363 * control and will suffice until 'sensors -s'
1364 * can be run by the user.
1365 */
1366
1367 /* Start monitoring */
1368 value = lm85_read_value(client, LM85_REG_CONFIG);
1369 /* Try to clear LOCK, Set START, save everything else */
Jean Delvare1f448092008-04-29 14:03:37 +02001370 value = (value & ~0x02) | 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1372 lm85_write_value(client, LM85_REG_CONFIG, value);
1373}
1374
1375static struct lm85_data *lm85_update_device(struct device *dev)
1376{
1377 struct i2c_client *client = to_i2c_client(dev);
1378 struct lm85_data *data = i2c_get_clientdata(client);
1379 int i;
1380
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001381 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Jean Delvare1f448092008-04-29 14:03:37 +02001383 if (!data->valid ||
1384 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* Things that change quickly */
1386 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Have to read extended bits first to "freeze" the
1389 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001390 * There are 2 additional resolution bits per channel and we
1391 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
Jean Delvare1f448092008-04-29 14:03:37 +02001393 if (data->type == adm1027 || data->type == adt7463) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int ext1 = lm85_read_value(client,
1395 ADM1027_REG_EXTEND_ADC1);
1396 int ext2 = lm85_read_value(client,
1397 ADM1027_REG_EXTEND_ADC2);
1398 int val = (ext1 << 8) + ext2;
1399
Jean Delvare1f448092008-04-29 14:03:37 +02001400 for (i = 0; i <= 4; i++)
1401 data->in_ext[i] =
1402 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Jean Delvare1f448092008-04-29 14:03:37 +02001404 for (i = 0; i <= 2; i++)
1405 data->temp_ext[i] =
1406 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
Jean Delvare9c516ef2005-11-26 20:07:54 +01001409 data->vid = lm85_read_value(client, LM85_REG_VID);
1410
1411 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 data->in[i] =
1413 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001414 data->fan[i] =
1415 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Jean Delvare9c516ef2005-11-26 20:07:54 +01001418 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1419 data->in[4] = lm85_read_value(client,
1420 LM85_REG_IN(4));
1421 }
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 for (i = 0; i <= 2; ++i) {
1424 data->temp[i] =
1425 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 data->pwm[i] =
1427 lm85_read_value(client, LM85_REG_PWM(i));
1428 }
1429
1430 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1431
Jean Delvaredd1ac532008-05-01 08:47:33 +02001432 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Three more voltage sensors */
1434 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001435 data->in[i] = lm85_read_value(client,
1436 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001439 data->alarms |= lm85_read_value(client,
1440 EMC6D100_REG_ALARM3) << 16;
1441 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* Have to read LSB bits after the MSB ones because
1443 the reading of the MSB bits has frozen the
1444 LSBs (backward from the ADM1027).
1445 */
1446 int ext1 = lm85_read_value(client,
1447 EMC6D102_REG_EXTEND_ADC1);
1448 int ext2 = lm85_read_value(client,
1449 EMC6D102_REG_EXTEND_ADC2);
1450 int ext3 = lm85_read_value(client,
1451 EMC6D102_REG_EXTEND_ADC3);
1452 int ext4 = lm85_read_value(client,
1453 EMC6D102_REG_EXTEND_ADC4);
1454 data->in_ext[0] = ext3 & 0x0f;
1455 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001456 data->in_ext[2] = ext4 >> 4;
1457 data->in_ext[3] = ext3 >> 4;
1458 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 data->temp_ext[0] = ext1 & 0x0f;
1461 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001462 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
Jean Delvare1f448092008-04-29 14:03:37 +02001465 data->last_reading = jiffies;
1466 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Jean Delvare1f448092008-04-29 14:03:37 +02001468 if (!data->valid ||
1469 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Things that don't change often */
1471 dev_dbg(&client->dev, "Reading config values\n");
1472
Jean Delvare9c516ef2005-11-26 20:07:54 +01001473 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 data->in_min[i] =
1475 lm85_read_value(client, LM85_REG_IN_MIN(i));
1476 data->in_max[i] =
1477 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001478 data->fan_min[i] =
1479 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
Jean Delvare9c516ef2005-11-26 20:07:54 +01001482 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1483 data->in_min[4] = lm85_read_value(client,
1484 LM85_REG_IN_MIN(4));
1485 data->in_max[4] = lm85_read_value(client,
1486 LM85_REG_IN_MAX(4));
1487 }
1488
Jean Delvare1f448092008-04-29 14:03:37 +02001489 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001491 data->in_min[i] = lm85_read_value(client,
1492 EMC6D100_REG_IN_MIN(i));
1493 data->in_max[i] = lm85_read_value(client,
1494 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496 }
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001499 int val;
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 data->temp_min[i] =
1502 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1503 data->temp_max[i] =
1504 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 data->autofan[i].config =
1507 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1508 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare1f448092008-04-29 14:03:37 +02001509 data->autofan[i].freq = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001510 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 data->autofan[i].min_pwm =
1512 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1513 data->zone[i].limit =
1514 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1515 data->zone[i].critical =
1516 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1517 }
1518
1519 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001520 data->autofan[0].min_off = (i & 0x20) != 0;
1521 data->autofan[1].min_off = (i & 0x40) != 0;
1522 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001525 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001526 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001529 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001532 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 data->valid = 1;
1535
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001536 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 return data;
1539}
1540
1541
1542static int __init sm_lm85_init(void)
1543{
1544 return i2c_add_driver(&lm85_driver);
1545}
1546
Jean Delvare1f448092008-04-29 14:03:37 +02001547static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 i2c_del_driver(&lm85_driver);
1550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001553MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1554 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001555 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1557
1558module_init(sm_lm85_init);
1559module_exit(sm_lm85_exit);