blob: 4ee9388df983e06c6b3a285f1f79a0edbf1c51c6 [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>
Jean Delvare8a0795d2008-10-17 17:51:14 +02008 Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040032#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020033#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020034#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040035#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050039static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020042I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* The LM85 registers */
45
46#define LM85_REG_IN(nr) (0x20 + (nr))
47#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
48#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
49
50#define LM85_REG_TEMP(nr) (0x25 + (nr))
51#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
52#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
53
54/* Fan speeds are LSB, MSB (2 bytes) */
Jean Delvare1f448092008-04-29 14:03:37 +020055#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
56#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define LM85_REG_PWM(nr) (0x30 + (nr))
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define LM85_REG_COMPANY 0x3e
61#define LM85_REG_VERSTEP 0x3f
62/* These are the recognized values for the above regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define LM85_COMPANY_NATIONAL 0x01
64#define LM85_COMPANY_ANALOG_DEV 0x41
Jean Delvare1f448092008-04-29 14:03:37 +020065#define LM85_COMPANY_SMSC 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define LM85_VERSTEP_VMASK 0xf0
67#define LM85_VERSTEP_GENERIC 0x60
68#define LM85_VERSTEP_LM85C 0x60
69#define LM85_VERSTEP_LM85B 0x62
70#define LM85_VERSTEP_ADM1027 0x60
71#define LM85_VERSTEP_ADT7463 0x62
72#define LM85_VERSTEP_ADT7463C 0x6A
73#define LM85_VERSTEP_EMC6D100_A0 0x60
74#define LM85_VERSTEP_EMC6D100_A1 0x61
75#define LM85_VERSTEP_EMC6D102 0x65
76
77#define LM85_REG_CONFIG 0x40
78
79#define LM85_REG_ALARM1 0x41
80#define LM85_REG_ALARM2 0x42
81
82#define LM85_REG_VID 0x43
83
84/* Automated FAN control */
85#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
86#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
87#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
89#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
90#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
91#define LM85_REG_AFAN_HYST1 0x6d
92#define LM85_REG_AFAN_HYST2 0x6e
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define ADM1027_REG_EXTEND_ADC1 0x76
95#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#define EMC6D100_REG_ALARM3 0x7d
98/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +020099#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
100#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
101#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define EMC6D102_REG_EXTEND_ADC1 0x85
103#define EMC6D102_REG_EXTEND_ADC2 0x86
104#define EMC6D102_REG_EXTEND_ADC3 0x87
105#define EMC6D102_REG_EXTEND_ADC4 0x88
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Jean Delvare1f448092008-04-29 14:03:37 +0200108/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 variants. Note that you should be a bit careful with which arguments
110 these macros are called: arguments may be evaluated more than once.
111 */
112
113/* IN are scaled acording to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400114static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200115 2500, 2250, 3300, 5000, 12000,
116 3300, 1500, 1800 /*EMC6D100*/
117};
118#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jean Delvare1f448092008-04-29 14:03:37 +0200120#define INS_TO_REG(n, val) \
121 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jean Delvare1f448092008-04-29 14:03:37 +0200123#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200124 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jean Delvare1f448092008-04-29 14:03:37 +0200126#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200129static inline u16 FAN_TO_REG(unsigned long val)
130{
131 if (!val)
132 return 0xffff;
133 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
134}
Jean Delvare1f448092008-04-29 14:03:37 +0200135#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
136 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* Temperature is reported in .001 degC increments */
139#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200140 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
141#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200142 SCALE(((val) << 4) + (ext), 16, 1000)
143#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jean Delvare1f448092008-04-29 14:03:37 +0200145#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define PWM_FROM_REG(val) (val)
147
148
149/* ZONEs have the following parameters:
150 * Limit (low) temp, 1. degC
151 * Hysteresis (below limit), 1. degC (0-15)
152 * Range of speed control, .1 degC (2-80)
153 * Critical (high) temp, 1. degC
154 *
155 * FAN PWMs have the following parameters:
156 * Reference Zone, 1, 2, 3, etc.
157 * Spinup time, .05 sec
158 * PWM value at limit/low temp, 1 count
159 * PWM Frequency, 1. Hz
160 * PWM is Min or OFF below limit, flag
161 * Invert PWM output, flag
162 *
163 * Some chips filter the temp, others the fan.
164 * Filter constant (or disabled) .1 seconds
165 */
166
167/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400168static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200169 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
170 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
171};
172
173static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int i;
176
Jean Delvared38b1492008-04-03 10:40:39 +0200177 if (range >= lm85_range_map[15])
Jean Delvare1f448092008-04-29 14:03:37 +0200178 return 15;
Jean Delvared38b1492008-04-03 10:40:39 +0200179
180 /* Find the closest match */
181 for (i = 14; i >= 0; --i) {
182 if (range >= lm85_range_map[i]) {
183 if ((lm85_range_map[i + 1] - range) <
184 (range - lm85_range_map[i]))
185 return i + 1;
186 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 }
Jean Delvared38b1492008-04-03 10:40:39 +0200189
190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Jean Delvare1f448092008-04-29 14:03:37 +0200192#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200195static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200196 10, 15, 23, 30, 38, 47, 61, 94
197};
198static const int adm1027_freq_map[8] = { /* 1 Hz */
199 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200200};
201
Jean Delvare8a0795d2008-10-17 17:51:14 +0200202static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int i;
205
Jean Delvare86010c92008-10-17 17:51:13 +0200206 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200207 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200208 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200209 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400210 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200212
213static int FREQ_FROM_REG(const int *map, u8 reg)
214{
215 return map[reg & 0x07];
216}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/* Since we can't use strings, I'm abusing these numbers
219 * to stand in for the following meanings:
220 * 1 -- PWM responds to Zone 1
221 * 2 -- PWM responds to Zone 2
222 * 3 -- PWM responds to Zone 3
223 * 23 -- PWM responds to the higher temp of Zone 2 or 3
224 * 123 -- PWM responds to highest of Zone 1, 2, or 3
225 * 0 -- PWM is always at 0% (ie, off)
226 * -1 -- PWM is always at 100%
227 * -2 -- PWM responds to manual control
228 */
229
Jean Delvaree89e22b2008-06-25 08:47:35 -0400230static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
231#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jean Delvare1f448092008-04-29 14:03:37 +0200233static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int i;
236
Jean Delvare1f448092008-04-29 14:03:37 +0200237 for (i = 0; i <= 7; ++i)
238 if (zone == lm85_zone_map[i])
239 break;
240 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400242 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Jean Delvare1f448092008-04-29 14:03:37 +0200245#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
246#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* Chip sampling rates
249 *
250 * Some sensors are not updated more frequently than once per second
251 * so it doesn't make sense to read them more often than that.
252 * We cache the results and return the saved data if the driver
253 * is called again before a second has elapsed.
254 *
255 * Also, there is significant configuration data for this chip
256 * given the automatic PWM fan control that is possible. There
257 * are about 47 bytes of config data to only 22 bytes of actual
258 * readings. So, we keep the config data up to date in the cache
259 * when it is written and only sample it once every 1 *minute*
260 */
261#define LM85_DATA_INTERVAL (HZ + HZ / 2)
262#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/* LM85 can automatically adjust fan speeds based on temperature
265 * This structure encapsulates an entire Zone config. There are
266 * three zones (one for each temperature input) on the lm85
267 */
268struct lm85_zone {
269 s8 limit; /* Low temp limit */
270 u8 hyst; /* Low limit hysteresis. (0-15) */
271 u8 range; /* Temp range, encoded */
272 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200273 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * to prevent "drift" as other autofan control
275 * values change.
276 */
Jean Delvare1f448092008-04-29 14:03:37 +0200277 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * to prevent "drift" as other autofan control
279 * values change.
280 */
281};
282
283struct lm85_autofan {
284 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 u8 min_pwm; /* Minimum PWM value, encoded */
286 u8 min_off; /* Min PWM or OFF below "limit", flag */
287};
288
Jean Delvareed6bafb2007-02-14 21:15:03 +0100289/* For each registered chip, we need to keep some data in memory.
290 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700292 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200293 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 enum chips type;
295
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100296 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int valid; /* !=0 if following fields are valid */
298 unsigned long last_reading; /* In jiffies */
299 unsigned long last_config; /* In jiffies */
300
301 u8 in[8]; /* Register value */
302 u8 in_max[8]; /* Register value */
303 u8 in_min[8]; /* Register value */
304 s8 temp[3]; /* Register value */
305 s8 temp_min[3]; /* Register value */
306 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u16 fan[4]; /* Register value */
308 u16 fan_min[4]; /* Register value */
309 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200310 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 u8 temp_ext[3]; /* Decoded values */
312 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 u8 vid; /* Register value */
314 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 u32 alarms; /* Register encoding, combined */
316 struct lm85_autofan autofan[3];
317 struct lm85_zone zone[3];
318};
319
Jean Delvare67712d02008-10-17 17:51:14 +0200320static int lm85_detect(struct i2c_client *client, int kind,
321 struct i2c_board_info *info);
322static int lm85_probe(struct i2c_client *client,
323 const struct i2c_device_id *id);
324static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100326static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400327static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330
Jean Delvare67712d02008-10-17 17:51:14 +0200331static const struct i2c_device_id lm85_id[] = {
332 { "adm1027", adm1027 },
333 { "adt7463", adt7463 },
334 { "lm85", any_chip },
335 { "lm85b", lm85b },
336 { "lm85c", lm85c },
337 { "emc6d100", emc6d100 },
338 { "emc6d101", emc6d100 },
339 { "emc6d102", emc6d102 },
340 { }
341};
342MODULE_DEVICE_TABLE(i2c, lm85_id);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200345 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100346 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100347 .name = "lm85",
348 },
Jean Delvare67712d02008-10-17 17:51:14 +0200349 .probe = lm85_probe,
350 .remove = lm85_remove,
351 .id_table = lm85_id,
352 .detect = lm85_detect,
353 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356
357/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200358static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
359 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jean Delvareb353a482007-07-05 20:36:12 +0200361 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200363 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Jean Delvareb353a482007-07-05 20:36:12 +0200365
366static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
367 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Jean Delvareb353a482007-07-05 20:36:12 +0200369 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200371 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Jean Delvareb353a482007-07-05 20:36:12 +0200373
374static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
375 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Jean Delvareb353a482007-07-05 20:36:12 +0200377 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct i2c_client *client = to_i2c_client(dev);
379 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200380 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100382 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 data->fan_min[nr] = FAN_TO_REG(val);
384 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100385 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return count;
387}
388
389#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200390static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
391 show_fan, NULL, offset - 1); \
392static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
393 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395show_fan_offset(1);
396show_fan_offset(2);
397show_fan_offset(3);
398show_fan_offset(4);
399
400/* vid, vrm, alarms */
401
Jean Delvare1f448092008-04-29 14:03:37 +0200402static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
403 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100406 int vid;
407
408 if (data->type == adt7463 && (data->vid & 0x80)) {
409 /* 6-pin VID (VRM 10) */
410 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
411 } else {
412 /* 5-pin VID (VRM 9) */
413 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
414 }
415
416 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
420
Jean Delvare1f448092008-04-29 14:03:37 +0200421static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
422 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Jean Delvare90d66192007-10-08 18:24:35 +0200424 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return sprintf(buf, "%ld\n", (long) data->vrm);
426}
427
Jean Delvare1f448092008-04-29 14:03:37 +0200428static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
429 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100431 struct lm85_data *data = dev_get_drvdata(dev);
432 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return count;
434}
435
436static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
437
Jean Delvare1f448092008-04-29 14:03:37 +0200438static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
439 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200442 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
446
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200447static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
448 char *buf)
449{
450 int nr = to_sensor_dev_attr(attr)->index;
451 struct lm85_data *data = lm85_update_device(dev);
452 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
453}
454
455static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
456static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
457static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
458static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
459static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
460static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
461static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
462static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
463static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
464static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
465static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
466static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
467static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
468static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
469static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
470static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
471static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/* pwm */
474
Jean Delvareb353a482007-07-05 20:36:12 +0200475static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
476 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Jean Delvareb353a482007-07-05 20:36:12 +0200478 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200480 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
Jean Delvareb353a482007-07-05 20:36:12 +0200482
483static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
484 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Jean Delvareb353a482007-07-05 20:36:12 +0200486 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct i2c_client *client = to_i2c_client(dev);
488 struct lm85_data *data = i2c_get_clientdata(client);
489 long val = simple_strtol(buf, NULL, 10);
490
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100491 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 data->pwm[nr] = PWM_TO_REG(val);
493 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100494 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return count;
496}
Jean Delvareb353a482007-07-05 20:36:12 +0200497
498static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
499 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jean Delvareb353a482007-07-05 20:36:12 +0200501 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100503 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100506 switch (pwm_zone) {
507 case -1: /* PWM is always at 100% */
508 enable = 0;
509 break;
510 case 0: /* PWM is always at 0% */
511 case -2: /* PWM responds to manual control */
512 enable = 1;
513 break;
514 default: /* PWM in automatic mode */
515 enable = 2;
516 }
517 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Jean Delvare455f7912007-12-03 23:28:42 +0100520static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
521 *attr, const char *buf, size_t count)
522{
523 int nr = to_sensor_dev_attr(attr)->index;
524 struct i2c_client *client = to_i2c_client(dev);
525 struct lm85_data *data = i2c_get_clientdata(client);
526 long val = simple_strtol(buf, NULL, 10);
527 u8 config;
528
529 switch (val) {
530 case 0:
531 config = 3;
532 break;
533 case 1:
534 config = 7;
535 break;
536 case 2:
537 /* Here we have to choose arbitrarily one of the 5 possible
538 configurations; I go for the safest */
539 config = 6;
540 break;
541 default:
542 return -EINVAL;
543 }
544
545 mutex_lock(&data->update_lock);
546 data->autofan[nr].config = lm85_read_value(client,
547 LM85_REG_AFAN_CONFIG(nr));
548 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
549 | (config << 5);
550 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
551 data->autofan[nr].config);
552 mutex_unlock(&data->update_lock);
553 return count;
554}
555
Jean Delvare34e7dc62008-10-17 17:51:13 +0200556static ssize_t show_pwm_freq(struct device *dev,
557 struct device_attribute *attr, char *buf)
558{
559 int nr = to_sensor_dev_attr(attr)->index;
560 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200561 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->freq_map,
562 data->pwm_freq[nr]));
Jean Delvare34e7dc62008-10-17 17:51:13 +0200563}
564
565static ssize_t set_pwm_freq(struct device *dev,
566 struct device_attribute *attr, const char *buf, size_t count)
567{
568 int nr = to_sensor_dev_attr(attr)->index;
569 struct i2c_client *client = to_i2c_client(dev);
570 struct lm85_data *data = i2c_get_clientdata(client);
571 long val = simple_strtol(buf, NULL, 10);
572
573 mutex_lock(&data->update_lock);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200574 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200575 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
576 (data->zone[nr].range << 4)
577 | data->pwm_freq[nr]);
578 mutex_unlock(&data->update_lock);
579 return count;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200583static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
584 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100585static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200586 show_pwm_enable, set_pwm_enable, offset - 1); \
587static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
588 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590show_pwm_reg(1);
591show_pwm_reg(2);
592show_pwm_reg(3);
593
594/* Voltages */
595
Jean Delvareb353a482007-07-05 20:36:12 +0200596static ssize_t show_in(struct device *dev, struct device_attribute *attr,
597 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Jean Delvareb353a482007-07-05 20:36:12 +0200599 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200601 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
602 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Jean Delvareb353a482007-07-05 20:36:12 +0200604
Jean Delvare1f448092008-04-29 14:03:37 +0200605static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200606 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jean Delvareb353a482007-07-05 20:36:12 +0200608 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200610 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Jean Delvareb353a482007-07-05 20:36:12 +0200612
613static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Jean Delvareb353a482007-07-05 20:36:12 +0200616 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct i2c_client *client = to_i2c_client(dev);
618 struct lm85_data *data = i2c_get_clientdata(client);
619 long val = simple_strtol(buf, NULL, 10);
620
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100621 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 data->in_min[nr] = INS_TO_REG(nr, val);
623 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100624 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return count;
626}
Jean Delvareb353a482007-07-05 20:36:12 +0200627
628static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
629 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Jean Delvareb353a482007-07-05 20:36:12 +0200631 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200633 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Jean Delvareb353a482007-07-05 20:36:12 +0200635
636static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
637 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Jean Delvareb353a482007-07-05 20:36:12 +0200639 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct i2c_client *client = to_i2c_client(dev);
641 struct lm85_data *data = i2c_get_clientdata(client);
642 long val = simple_strtol(buf, NULL, 10);
643
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100644 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 data->in_max[nr] = INS_TO_REG(nr, val);
646 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return count;
649}
Jean Delvareb353a482007-07-05 20:36:12 +0200650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200652static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
653 show_in, NULL, offset); \
654static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
655 show_in_min, set_in_min, offset); \
656static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
657 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659show_in_reg(0);
660show_in_reg(1);
661show_in_reg(2);
662show_in_reg(3);
663show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200664show_in_reg(5);
665show_in_reg(6);
666show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/* Temps */
669
Jean Delvareb353a482007-07-05 20:36:12 +0200670static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
671 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Jean Delvareb353a482007-07-05 20:36:12 +0200673 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200675 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
676 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
Jean Delvareb353a482007-07-05 20:36:12 +0200678
679static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
680 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jean Delvareb353a482007-07-05 20:36:12 +0200682 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200684 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Jean Delvareb353a482007-07-05 20:36:12 +0200686
687static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jean Delvareb353a482007-07-05 20:36:12 +0200690 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct i2c_client *client = to_i2c_client(dev);
692 struct lm85_data *data = i2c_get_clientdata(client);
693 long val = simple_strtol(buf, NULL, 10);
694
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100695 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 data->temp_min[nr] = TEMP_TO_REG(val);
697 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100698 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return count;
700}
Jean Delvareb353a482007-07-05 20:36:12 +0200701
702static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
703 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Jean Delvareb353a482007-07-05 20:36:12 +0200705 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200707 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
Jean Delvareb353a482007-07-05 20:36:12 +0200709
710static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Jean Delvareb353a482007-07-05 20:36:12 +0200713 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct i2c_client *client = to_i2c_client(dev);
715 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200716 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100718 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 data->temp_max[nr] = TEMP_TO_REG(val);
720 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return count;
723}
Jean Delvareb353a482007-07-05 20:36:12 +0200724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200726static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
727 show_temp, NULL, offset - 1); \
728static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
729 show_temp_min, set_temp_min, offset - 1); \
730static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
731 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733show_temp_reg(1);
734show_temp_reg(2);
735show_temp_reg(3);
736
737
738/* Automatic PWM control */
739
Jean Delvareb353a482007-07-05 20:36:12 +0200740static ssize_t show_pwm_auto_channels(struct device *dev,
741 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Jean Delvareb353a482007-07-05 20:36:12 +0200743 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200745 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Jean Delvareb353a482007-07-05 20:36:12 +0200747
748static ssize_t set_pwm_auto_channels(struct device *dev,
749 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Jean Delvareb353a482007-07-05 20:36:12 +0200751 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct i2c_client *client = to_i2c_client(dev);
753 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200754 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100756 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200758 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
760 data->autofan[nr].config);
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_min(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", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Jean Delvareb353a482007-07-05 20:36:12 +0200772
773static ssize_t set_pwm_auto_pwm_min(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].min_pwm = PWM_TO_REG(val);
783 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
784 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100785 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return count;
787}
Jean Delvareb353a482007-07-05 20:36:12 +0200788
789static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
790 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jean Delvareb353a482007-07-05 20:36:12 +0200792 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200794 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Jean Delvareb353a482007-07-05 20:36:12 +0200796
797static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
798 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Jean Delvareb353a482007-07-05 20:36:12 +0200800 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct i2c_client *client = to_i2c_client(dev);
802 struct lm85_data *data = i2c_get_clientdata(client);
803 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200804 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100806 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200808 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
809 tmp &= ~(0x20 << nr);
810 if (data->autofan[nr].min_off)
811 tmp |= 0x20 << nr;
812 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100813 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return count;
815}
Jean Delvareb353a482007-07-05 20:36:12 +0200816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200818static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
819 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
820 set_pwm_auto_channels, offset - 1); \
821static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
822 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
823 set_pwm_auto_pwm_min, offset - 1); \
824static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
825 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200826 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828pwm_auto(1);
829pwm_auto(2);
830pwm_auto(3);
831
832/* Temperature settings for automatic PWM control */
833
Jean Delvareb353a482007-07-05 20:36:12 +0200834static ssize_t show_temp_auto_temp_off(struct device *dev,
835 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jean Delvareb353a482007-07-05 20:36:12 +0200837 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200839 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 HYST_FROM_REG(data->zone[nr].hyst));
841}
Jean Delvareb353a482007-07-05 20:36:12 +0200842
843static ssize_t set_temp_auto_temp_off(struct device *dev,
844 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Jean Delvareb353a482007-07-05 20:36:12 +0200846 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct i2c_client *client = to_i2c_client(dev);
848 struct lm85_data *data = i2c_get_clientdata(client);
849 int min;
850 long val = simple_strtol(buf, NULL, 10);
851
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100852 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 min = TEMP_FROM_REG(data->zone[nr].limit);
854 data->zone[nr].off_desired = TEMP_TO_REG(val);
855 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200856 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 lm85_write_value(client, LM85_REG_AFAN_HYST1,
858 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200859 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 } else {
861 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200862 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100864 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return count;
866}
Jean Delvareb353a482007-07-05 20:36:12 +0200867
868static ssize_t show_temp_auto_temp_min(struct device *dev,
869 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Jean Delvareb353a482007-07-05 20:36:12 +0200871 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200873 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
Jean Delvareb353a482007-07-05 20:36:12 +0200875
876static ssize_t set_temp_auto_temp_min(struct device *dev,
877 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jean Delvareb353a482007-07-05 20:36:12 +0200879 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct i2c_client *client = to_i2c_client(dev);
881 struct lm85_data *data = i2c_get_clientdata(client);
882 long val = simple_strtol(buf, NULL, 10);
883
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100884 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 data->zone[nr].limit = TEMP_TO_REG(val);
886 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
887 data->zone[nr].limit);
888
889/* Update temp_auto_max and temp_auto_range */
890 data->zone[nr].range = RANGE_TO_REG(
891 TEMP_FROM_REG(data->zone[nr].max_desired) -
892 TEMP_FROM_REG(data->zone[nr].limit));
893 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
894 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200895 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897/* Update temp_auto_hyst and temp_auto_off */
898 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
899 data->zone[nr].limit) - TEMP_FROM_REG(
900 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200901 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 lm85_write_value(client, LM85_REG_AFAN_HYST1,
903 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200904 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } else {
906 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200907 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100909 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return count;
911}
Jean Delvareb353a482007-07-05 20:36:12 +0200912
913static ssize_t show_temp_auto_temp_max(struct device *dev,
914 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Jean Delvareb353a482007-07-05 20:36:12 +0200916 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200918 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 RANGE_FROM_REG(data->zone[nr].range));
920}
Jean Delvareb353a482007-07-05 20:36:12 +0200921
922static ssize_t set_temp_auto_temp_max(struct device *dev,
923 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jean Delvareb353a482007-07-05 20:36:12 +0200925 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct i2c_client *client = to_i2c_client(dev);
927 struct lm85_data *data = i2c_get_clientdata(client);
928 int min;
929 long val = simple_strtol(buf, NULL, 10);
930
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100931 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 min = TEMP_FROM_REG(data->zone[nr].limit);
933 data->zone[nr].max_desired = TEMP_TO_REG(val);
934 data->zone[nr].range = RANGE_TO_REG(
935 val - min);
936 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
937 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200938 | (data->pwm_freq[nr] & 0x07));
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
943static ssize_t show_temp_auto_temp_crit(struct device *dev,
944 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jean Delvareb353a482007-07-05 20:36:12 +0200946 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200948 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Jean Delvareb353a482007-07-05 20:36:12 +0200950
951static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200952 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Jean Delvareb353a482007-07-05 20:36:12 +0200954 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct i2c_client *client = to_i2c_client(dev);
956 struct lm85_data *data = i2c_get_clientdata(client);
957 long val = simple_strtol(buf, NULL, 10);
958
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100959 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 data->zone[nr].critical = TEMP_TO_REG(val);
961 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
962 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100963 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return count;
965}
Jean Delvareb353a482007-07-05 20:36:12 +0200966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200968static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
969 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
970 set_temp_auto_temp_off, offset - 1); \
971static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
972 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
973 set_temp_auto_temp_min, offset - 1); \
974static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
975 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
976 set_temp_auto_temp_max, offset - 1); \
977static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
978 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
979 set_temp_auto_temp_crit, offset - 1);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981temp_auto(1);
982temp_auto(2);
983temp_auto(3);
984
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200985static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200986 &sensor_dev_attr_fan1_input.dev_attr.attr,
987 &sensor_dev_attr_fan2_input.dev_attr.attr,
988 &sensor_dev_attr_fan3_input.dev_attr.attr,
989 &sensor_dev_attr_fan4_input.dev_attr.attr,
990 &sensor_dev_attr_fan1_min.dev_attr.attr,
991 &sensor_dev_attr_fan2_min.dev_attr.attr,
992 &sensor_dev_attr_fan3_min.dev_attr.attr,
993 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200994 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
995 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
996 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
997 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200998
999 &sensor_dev_attr_pwm1.dev_attr.attr,
1000 &sensor_dev_attr_pwm2.dev_attr.attr,
1001 &sensor_dev_attr_pwm3.dev_attr.attr,
1002 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1003 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1004 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001005 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1006 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1007 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001008
1009 &sensor_dev_attr_in0_input.dev_attr.attr,
1010 &sensor_dev_attr_in1_input.dev_attr.attr,
1011 &sensor_dev_attr_in2_input.dev_attr.attr,
1012 &sensor_dev_attr_in3_input.dev_attr.attr,
1013 &sensor_dev_attr_in0_min.dev_attr.attr,
1014 &sensor_dev_attr_in1_min.dev_attr.attr,
1015 &sensor_dev_attr_in2_min.dev_attr.attr,
1016 &sensor_dev_attr_in3_min.dev_attr.attr,
1017 &sensor_dev_attr_in0_max.dev_attr.attr,
1018 &sensor_dev_attr_in1_max.dev_attr.attr,
1019 &sensor_dev_attr_in2_max.dev_attr.attr,
1020 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001021 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1022 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1023 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1024 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001025
1026 &sensor_dev_attr_temp1_input.dev_attr.attr,
1027 &sensor_dev_attr_temp2_input.dev_attr.attr,
1028 &sensor_dev_attr_temp3_input.dev_attr.attr,
1029 &sensor_dev_attr_temp1_min.dev_attr.attr,
1030 &sensor_dev_attr_temp2_min.dev_attr.attr,
1031 &sensor_dev_attr_temp3_min.dev_attr.attr,
1032 &sensor_dev_attr_temp1_max.dev_attr.attr,
1033 &sensor_dev_attr_temp2_max.dev_attr.attr,
1034 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001035 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1036 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1037 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1038 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1039 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001040
1041 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1042 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1043 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1044 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1045 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1046 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1047 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1048 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1049 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001050
1051 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1052 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1053 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1054 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1055 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1056 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1057 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1058 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1059 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1060 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1061 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1062 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1063
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001064 &dev_attr_vrm.attr,
1065 &dev_attr_cpu0_vid.attr,
1066 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001067 NULL
1068};
1069
1070static const struct attribute_group lm85_group = {
1071 .attrs = lm85_attributes,
1072};
1073
Jean Delvare6b9aad22007-07-05 20:37:21 +02001074static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001075 &sensor_dev_attr_in4_input.dev_attr.attr,
1076 &sensor_dev_attr_in4_min.dev_attr.attr,
1077 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001078 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001079 NULL
1080};
1081
Jean Delvare6b9aad22007-07-05 20:37:21 +02001082static const struct attribute_group lm85_group_in4 = {
1083 .attrs = lm85_attributes_in4,
1084};
1085
1086static struct attribute *lm85_attributes_in567[] = {
1087 &sensor_dev_attr_in5_input.dev_attr.attr,
1088 &sensor_dev_attr_in6_input.dev_attr.attr,
1089 &sensor_dev_attr_in7_input.dev_attr.attr,
1090 &sensor_dev_attr_in5_min.dev_attr.attr,
1091 &sensor_dev_attr_in6_min.dev_attr.attr,
1092 &sensor_dev_attr_in7_min.dev_attr.attr,
1093 &sensor_dev_attr_in5_max.dev_attr.attr,
1094 &sensor_dev_attr_in6_max.dev_attr.attr,
1095 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001096 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1097 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1098 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001099 NULL
1100};
1101
1102static const struct attribute_group lm85_group_in567 = {
1103 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001104};
1105
Jean Delvare5f44759472008-06-25 09:10:30 -04001106static void lm85_init_client(struct i2c_client *client)
1107{
1108 int value;
1109
1110 /* Start monitoring if needed */
1111 value = lm85_read_value(client, LM85_REG_CONFIG);
1112 if (!(value & 0x01)) {
1113 dev_info(&client->dev, "Starting monitoring\n");
1114 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1115 }
1116
1117 /* Warn about unusual configuration bits */
1118 if (value & 0x02)
1119 dev_warn(&client->dev, "Device configuration is locked\n");
1120 if (!(value & 0x04))
1121 dev_warn(&client->dev, "Device is not ready\n");
1122}
1123
Jean Delvare67712d02008-10-17 17:51:14 +02001124/* Return 0 if detection is successful, -ENODEV otherwise */
1125static int lm85_detect(struct i2c_client *client, int kind,
1126 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Jean Delvare67712d02008-10-17 17:51:14 +02001128 struct i2c_adapter *adapter = client->adapter;
1129 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001130 const char *type_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Jean Delvaree89e22b2008-06-25 08:47:35 -04001132 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001134 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001137 /* If auto-detecting, determine the chip type */
1138 if (kind < 0) {
1139 int company = lm85_read_value(client, LM85_REG_COMPANY);
1140 int verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001142 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1143 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1144 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001146 /* All supported chips have the version in common */
1147 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC) {
1148 dev_dbg(&adapter->dev, "Autodetection failed: "
1149 "unsupported version\n");
Jean Delvare67712d02008-10-17 17:51:14 +02001150 return -ENODEV;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001151 }
1152 kind = any_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001154 /* Now, refine the detection */
1155 if (company == LM85_COMPANY_NATIONAL) {
1156 switch (verstep) {
1157 case LM85_VERSTEP_LM85C:
1158 kind = lm85c;
1159 break;
1160 case LM85_VERSTEP_LM85B:
1161 kind = lm85b;
1162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001164 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1165 switch (verstep) {
1166 case LM85_VERSTEP_ADM1027:
1167 kind = adm1027;
1168 break;
1169 case LM85_VERSTEP_ADT7463:
1170 case LM85_VERSTEP_ADT7463C:
1171 kind = adt7463;
1172 break;
1173 }
1174 } else if (company == LM85_COMPANY_SMSC) {
1175 switch (verstep) {
1176 case LM85_VERSTEP_EMC6D100_A0:
1177 case LM85_VERSTEP_EMC6D100_A1:
1178 /* Note: we can't tell a '100 from a '101 */
1179 kind = emc6d100;
1180 break;
1181 case LM85_VERSTEP_EMC6D102:
1182 kind = emc6d102;
1183 break;
1184 }
1185 } else {
1186 dev_dbg(&adapter->dev, "Autodetection failed: "
1187 "unknown vendor\n");
Jean Delvare67712d02008-10-17 17:51:14 +02001188 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 }
1191
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 }
Jean Delvare67712d02008-10-17 17:51:14 +02001214 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jean Delvare67712d02008-10-17 17:51:14 +02001216 return 0;
1217}
1218
1219static int lm85_probe(struct i2c_client *client,
1220 const struct i2c_device_id *id)
1221{
1222 struct lm85_data *data;
1223 int err;
1224
1225 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1226 if (!data)
1227 return -ENOMEM;
1228
1229 i2c_set_clientdata(client, data);
1230 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001231 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Jean Delvare67712d02008-10-17 17:51:14 +02001233 /* Fill in the chip specific driver values */
1234 switch (data->type) {
1235 case adm1027:
1236 case adt7463:
1237 case emc6d100:
1238 case emc6d102:
1239 data->freq_map = adm1027_freq_map;
1240 break;
1241 default:
1242 data->freq_map = lm85_freq_map;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001246 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001249 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001252 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1253 if (err)
Jean Delvare67712d02008-10-17 17:51:14 +02001254 goto ERROR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jean Delvare9c516ef2005-11-26 20:07:54 +01001256 /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
1257 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001258 data->vid = lm85_read_value(client, LM85_REG_VID);
Jean Delvare67712d02008-10-17 17:51:14 +02001259 if (!(data->type == adt7463 && (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001260 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001261 &lm85_group_in4)))
1262 goto ERROR3;
1263
1264 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001265 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001266 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001267 &lm85_group_in567)))
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001268 goto ERROR3;
1269
Jean Delvaree89e22b2008-06-25 08:47:35 -04001270 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001271 if (IS_ERR(data->hwmon_dev)) {
1272 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001273 goto ERROR3;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001274 }
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277
1278 /* Error out and cleanup code */
Jean Delvare1f448092008-04-29 14:03:37 +02001279 ERROR3:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001280 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1281 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare67712d02008-10-17 17:51:14 +02001282 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001283 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvare1f448092008-04-29 14:03:37 +02001284 ERROR1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return err;
1287}
1288
Jean Delvare67712d02008-10-17 17:51:14 +02001289static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001291 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001292 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001293 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001294 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1295 if (data->type == emc6d100)
1296 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001297 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return 0;
1299}
1300
1301
Ben Dooksd8d20612005-10-26 21:05:46 +02001302static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 int res;
1305
1306 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001307 switch (reg) {
1308 case LM85_REG_FAN(0): /* Read WORD data */
1309 case LM85_REG_FAN(1):
1310 case LM85_REG_FAN(2):
1311 case LM85_REG_FAN(3):
1312 case LM85_REG_FAN_MIN(0):
1313 case LM85_REG_FAN_MIN(1):
1314 case LM85_REG_FAN_MIN(2):
1315 case LM85_REG_FAN_MIN(3):
1316 case LM85_REG_ALARM1: /* Read both bytes at once */
1317 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1318 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 default: /* Read BYTE data */
1321 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
Jean Delvare1f448092008-04-29 14:03:37 +02001325 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Jean Delvaree89e22b2008-06-25 08:47:35 -04001328static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Jean Delvare1f448092008-04-29 14:03:37 +02001330 switch (reg) {
1331 case LM85_REG_FAN(0): /* Write WORD data */
1332 case LM85_REG_FAN(1):
1333 case LM85_REG_FAN(2):
1334 case LM85_REG_FAN(3):
1335 case LM85_REG_FAN_MIN(0):
1336 case LM85_REG_FAN_MIN(1):
1337 case LM85_REG_FAN_MIN(2):
1338 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001340 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1341 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001344 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349static struct lm85_data *lm85_update_device(struct device *dev)
1350{
1351 struct i2c_client *client = to_i2c_client(dev);
1352 struct lm85_data *data = i2c_get_clientdata(client);
1353 int i;
1354
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001355 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jean Delvare1f448092008-04-29 14:03:37 +02001357 if (!data->valid ||
1358 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* Things that change quickly */
1360 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* Have to read extended bits first to "freeze" the
1363 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001364 * There are 2 additional resolution bits per channel and we
1365 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
Jean Delvare1f448092008-04-29 14:03:37 +02001367 if (data->type == adm1027 || data->type == adt7463) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int ext1 = lm85_read_value(client,
1369 ADM1027_REG_EXTEND_ADC1);
1370 int ext2 = lm85_read_value(client,
1371 ADM1027_REG_EXTEND_ADC2);
1372 int val = (ext1 << 8) + ext2;
1373
Jean Delvare1f448092008-04-29 14:03:37 +02001374 for (i = 0; i <= 4; i++)
1375 data->in_ext[i] =
1376 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Jean Delvare1f448092008-04-29 14:03:37 +02001378 for (i = 0; i <= 2; i++)
1379 data->temp_ext[i] =
1380 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Jean Delvare9c516ef2005-11-26 20:07:54 +01001383 data->vid = lm85_read_value(client, LM85_REG_VID);
1384
1385 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 data->in[i] =
1387 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001388 data->fan[i] =
1389 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
Jean Delvare9c516ef2005-11-26 20:07:54 +01001392 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1393 data->in[4] = lm85_read_value(client,
1394 LM85_REG_IN(4));
1395 }
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 for (i = 0; i <= 2; ++i) {
1398 data->temp[i] =
1399 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 data->pwm[i] =
1401 lm85_read_value(client, LM85_REG_PWM(i));
1402 }
1403
1404 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1405
Jean Delvaredd1ac532008-05-01 08:47:33 +02001406 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* Three more voltage sensors */
1408 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001409 data->in[i] = lm85_read_value(client,
1410 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001413 data->alarms |= lm85_read_value(client,
1414 EMC6D100_REG_ALARM3) << 16;
1415 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /* Have to read LSB bits after the MSB ones because
1417 the reading of the MSB bits has frozen the
1418 LSBs (backward from the ADM1027).
1419 */
1420 int ext1 = lm85_read_value(client,
1421 EMC6D102_REG_EXTEND_ADC1);
1422 int ext2 = lm85_read_value(client,
1423 EMC6D102_REG_EXTEND_ADC2);
1424 int ext3 = lm85_read_value(client,
1425 EMC6D102_REG_EXTEND_ADC3);
1426 int ext4 = lm85_read_value(client,
1427 EMC6D102_REG_EXTEND_ADC4);
1428 data->in_ext[0] = ext3 & 0x0f;
1429 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001430 data->in_ext[2] = ext4 >> 4;
1431 data->in_ext[3] = ext3 >> 4;
1432 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 data->temp_ext[0] = ext1 & 0x0f;
1435 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001436 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
Jean Delvare1f448092008-04-29 14:03:37 +02001439 data->last_reading = jiffies;
1440 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Jean Delvare1f448092008-04-29 14:03:37 +02001442 if (!data->valid ||
1443 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Things that don't change often */
1445 dev_dbg(&client->dev, "Reading config values\n");
1446
Jean Delvare9c516ef2005-11-26 20:07:54 +01001447 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 data->in_min[i] =
1449 lm85_read_value(client, LM85_REG_IN_MIN(i));
1450 data->in_max[i] =
1451 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001452 data->fan_min[i] =
1453 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
Jean Delvare9c516ef2005-11-26 20:07:54 +01001456 if (!(data->type == adt7463 && (data->vid & 0x80))) {
1457 data->in_min[4] = lm85_read_value(client,
1458 LM85_REG_IN_MIN(4));
1459 data->in_max[4] = lm85_read_value(client,
1460 LM85_REG_IN_MAX(4));
1461 }
1462
Jean Delvare1f448092008-04-29 14:03:37 +02001463 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001465 data->in_min[i] = lm85_read_value(client,
1466 EMC6D100_REG_IN_MIN(i));
1467 data->in_max[i] = lm85_read_value(client,
1468 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001473 int val;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 data->temp_min[i] =
1476 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1477 data->temp_max[i] =
1478 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 data->autofan[i].config =
1481 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1482 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001483 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001484 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 data->autofan[i].min_pwm =
1486 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1487 data->zone[i].limit =
1488 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1489 data->zone[i].critical =
1490 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1491 }
1492
1493 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001494 data->autofan[0].min_off = (i & 0x20) != 0;
1495 data->autofan[1].min_off = (i & 0x40) != 0;
1496 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001499 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001500 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001503 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001506 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 data->valid = 1;
1509
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001510 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 return data;
1513}
1514
1515
1516static int __init sm_lm85_init(void)
1517{
1518 return i2c_add_driver(&lm85_driver);
1519}
1520
Jean Delvare1f448092008-04-29 14:03:37 +02001521static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 i2c_del_driver(&lm85_driver);
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001527MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1528 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001529 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1531
1532module_init(sm_lm85_init);
1533module_exit(sm_lm85_exit);