blob: b3841a615595658cc3ddd0675d827becaf53fc45 [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 Delvared42a2eb2009-12-09 20:35:53 +01008 Copyright (C) 2007--2009 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
Jean Delvaree5e9f442009-12-14 21:17:27 +010041enum chips {
42 any_chip, lm85b, lm85c,
43 adm1027, adt7463, adt7468,
44 emc6d100, emc6d102
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* The LM85 registers */
48
49#define LM85_REG_IN(nr) (0x20 + (nr))
50#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
51#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
52
53#define LM85_REG_TEMP(nr) (0x25 + (nr))
54#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
55#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
56
57/* Fan speeds are LSB, MSB (2 bytes) */
Jean Delvare1f448092008-04-29 14:03:37 +020058#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
59#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define LM85_REG_PWM(nr) (0x30 + (nr))
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define LM85_REG_COMPANY 0x3e
64#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080065
66#define ADT7468_REG_CFG5 0x7c
67#define ADT7468_OFF64 0x01
68#define IS_ADT7468_OFF64(data) \
69 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* These are the recognized values for the above regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define LM85_COMPANY_NATIONAL 0x01
73#define LM85_COMPANY_ANALOG_DEV 0x41
Jean Delvare1f448092008-04-29 14:03:37 +020074#define LM85_COMPANY_SMSC 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define LM85_VERSTEP_VMASK 0xf0
76#define LM85_VERSTEP_GENERIC 0x60
Darrick J. Wongc15ade62009-03-10 12:55:47 -070077#define LM85_VERSTEP_GENERIC2 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define LM85_VERSTEP_LM85C 0x60
79#define LM85_VERSTEP_LM85B 0x62
Jean Delvare5cfaf332009-09-15 17:18:14 +020080#define LM85_VERSTEP_LM96000_1 0x68
81#define LM85_VERSTEP_LM96000_2 0x69
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define LM85_VERSTEP_ADM1027 0x60
83#define LM85_VERSTEP_ADT7463 0x62
84#define LM85_VERSTEP_ADT7463C 0x6A
Darrick J. Wong79b92f22008-11-12 13:26:59 -080085#define LM85_VERSTEP_ADT7468_1 0x71
86#define LM85_VERSTEP_ADT7468_2 0x72
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define LM85_VERSTEP_EMC6D100_A0 0x60
88#define LM85_VERSTEP_EMC6D100_A1 0x61
89#define LM85_VERSTEP_EMC6D102 0x65
90
91#define LM85_REG_CONFIG 0x40
92
93#define LM85_REG_ALARM1 0x41
94#define LM85_REG_ALARM2 0x42
95
96#define LM85_REG_VID 0x43
97
98/* Automated FAN control */
99#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
100#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
101#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
103#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
104#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
105#define LM85_REG_AFAN_HYST1 0x6d
106#define LM85_REG_AFAN_HYST2 0x6e
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define ADM1027_REG_EXTEND_ADC1 0x76
109#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111#define EMC6D100_REG_ALARM3 0x7d
112/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200113#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
114#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
115#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define EMC6D102_REG_EXTEND_ADC1 0x85
117#define EMC6D102_REG_EXTEND_ADC2 0x86
118#define EMC6D102_REG_EXTEND_ADC3 0x87
119#define EMC6D102_REG_EXTEND_ADC4 0x88
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jean Delvare1f448092008-04-29 14:03:37 +0200122/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 variants. Note that you should be a bit careful with which arguments
124 these macros are called: arguments may be evaluated more than once.
125 */
126
127/* IN are scaled acording to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400128static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200129 2500, 2250, 3300, 5000, 12000,
130 3300, 1500, 1800 /*EMC6D100*/
131};
132#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jean Delvare1f448092008-04-29 14:03:37 +0200134#define INS_TO_REG(n, val) \
135 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jean Delvare1f448092008-04-29 14:03:37 +0200137#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200138 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jean Delvare1f448092008-04-29 14:03:37 +0200140#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200143static inline u16 FAN_TO_REG(unsigned long val)
144{
145 if (!val)
146 return 0xffff;
147 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
148}
Jean Delvare1f448092008-04-29 14:03:37 +0200149#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
150 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/* Temperature is reported in .001 degC increments */
153#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200154 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
155#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200156 SCALE(((val) << 4) + (ext), 16, 1000)
157#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jean Delvare1f448092008-04-29 14:03:37 +0200159#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define PWM_FROM_REG(val) (val)
161
162
163/* ZONEs have the following parameters:
164 * Limit (low) temp, 1. degC
165 * Hysteresis (below limit), 1. degC (0-15)
166 * Range of speed control, .1 degC (2-80)
167 * Critical (high) temp, 1. degC
168 *
169 * FAN PWMs have the following parameters:
170 * Reference Zone, 1, 2, 3, etc.
171 * Spinup time, .05 sec
172 * PWM value at limit/low temp, 1 count
173 * PWM Frequency, 1. Hz
174 * PWM is Min or OFF below limit, flag
175 * Invert PWM output, flag
176 *
177 * Some chips filter the temp, others the fan.
178 * Filter constant (or disabled) .1 seconds
179 */
180
181/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400182static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200183 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
184 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
185};
186
187static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int i;
190
Jean Delvared38b1492008-04-03 10:40:39 +0200191 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200192 for (i = 0; i < 15; ++i) {
193 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Jean Delvared38b1492008-04-03 10:40:39 +0200196
Jean Delvare1b92ada2008-10-17 17:51:14 +0200197 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Jean Delvare1f448092008-04-29 14:03:37 +0200199#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200202static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200203 10, 15, 23, 30, 38, 47, 61, 94
204};
205static const int adm1027_freq_map[8] = { /* 1 Hz */
206 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200207};
208
Jean Delvare8a0795d2008-10-17 17:51:14 +0200209static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int i;
212
Jean Delvare86010c92008-10-17 17:51:13 +0200213 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200214 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200215 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200216 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400217 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200219
220static int FREQ_FROM_REG(const int *map, u8 reg)
221{
222 return map[reg & 0x07];
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225/* Since we can't use strings, I'm abusing these numbers
226 * to stand in for the following meanings:
227 * 1 -- PWM responds to Zone 1
228 * 2 -- PWM responds to Zone 2
229 * 3 -- PWM responds to Zone 3
230 * 23 -- PWM responds to the higher temp of Zone 2 or 3
231 * 123 -- PWM responds to highest of Zone 1, 2, or 3
232 * 0 -- PWM is always at 0% (ie, off)
233 * -1 -- PWM is always at 100%
234 * -2 -- PWM responds to manual control
235 */
236
Jean Delvaree89e22b2008-06-25 08:47:35 -0400237static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
238#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Jean Delvare1f448092008-04-29 14:03:37 +0200240static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int i;
243
Jean Delvare1f448092008-04-29 14:03:37 +0200244 for (i = 0; i <= 7; ++i)
245 if (zone == lm85_zone_map[i])
246 break;
247 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400249 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Jean Delvare1f448092008-04-29 14:03:37 +0200252#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
253#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* Chip sampling rates
256 *
257 * Some sensors are not updated more frequently than once per second
258 * so it doesn't make sense to read them more often than that.
259 * We cache the results and return the saved data if the driver
260 * is called again before a second has elapsed.
261 *
262 * Also, there is significant configuration data for this chip
263 * given the automatic PWM fan control that is possible. There
264 * are about 47 bytes of config data to only 22 bytes of actual
265 * readings. So, we keep the config data up to date in the cache
266 * when it is written and only sample it once every 1 *minute*
267 */
268#define LM85_DATA_INTERVAL (HZ + HZ / 2)
269#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/* LM85 can automatically adjust fan speeds based on temperature
272 * This structure encapsulates an entire Zone config. There are
273 * three zones (one for each temperature input) on the lm85
274 */
275struct lm85_zone {
276 s8 limit; /* Low temp limit */
277 u8 hyst; /* Low limit hysteresis. (0-15) */
278 u8 range; /* Temp range, encoded */
279 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200280 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * to prevent "drift" as other autofan control
282 * values change.
283 */
Jean Delvare1f448092008-04-29 14:03:37 +0200284 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * to prevent "drift" as other autofan control
286 * values change.
287 */
288};
289
290struct lm85_autofan {
291 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 u8 min_pwm; /* Minimum PWM value, encoded */
293 u8 min_off; /* Min PWM or OFF below "limit", flag */
294};
295
Jean Delvareed6bafb2007-02-14 21:15:03 +0100296/* For each registered chip, we need to keep some data in memory.
297 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700299 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200300 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 enum chips type;
302
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100303 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int valid; /* !=0 if following fields are valid */
305 unsigned long last_reading; /* In jiffies */
306 unsigned long last_config; /* In jiffies */
307
308 u8 in[8]; /* Register value */
309 u8 in_max[8]; /* Register value */
310 u8 in_min[8]; /* Register value */
311 s8 temp[3]; /* Register value */
312 s8 temp_min[3]; /* Register value */
313 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 u16 fan[4]; /* Register value */
315 u16 fan_min[4]; /* Register value */
316 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200317 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 u8 temp_ext[3]; /* Decoded values */
319 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u8 vid; /* Register value */
321 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800323 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct lm85_autofan autofan[3];
325 struct lm85_zone zone[3];
326};
327
Jean Delvare310ec792009-12-14 21:17:23 +0100328static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200329static int lm85_probe(struct i2c_client *client,
330 const struct i2c_device_id *id);
331static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100333static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400334static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337
Jean Delvare67712d02008-10-17 17:51:14 +0200338static const struct i2c_device_id lm85_id[] = {
339 { "adm1027", adm1027 },
340 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700341 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200342 { "lm85", any_chip },
343 { "lm85b", lm85b },
344 { "lm85c", lm85c },
345 { "emc6d100", emc6d100 },
346 { "emc6d101", emc6d100 },
347 { "emc6d102", emc6d102 },
348 { }
349};
350MODULE_DEVICE_TABLE(i2c, lm85_id);
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200353 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100354 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100355 .name = "lm85",
356 },
Jean Delvare67712d02008-10-17 17:51:14 +0200357 .probe = lm85_probe,
358 .remove = lm85_remove,
359 .id_table = lm85_id,
360 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100361 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364
365/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200366static ssize_t show_fan(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[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Jean Delvareb353a482007-07-05 20:36:12 +0200373
374static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
375 char *buf)
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 lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200379 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
Jean Delvareb353a482007-07-05 20:36:12 +0200381
382static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
383 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jean Delvareb353a482007-07-05 20:36:12 +0200385 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct i2c_client *client = to_i2c_client(dev);
387 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200388 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100390 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 data->fan_min[nr] = FAN_TO_REG(val);
392 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100393 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return count;
395}
396
397#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200398static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
399 show_fan, NULL, offset - 1); \
400static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
401 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403show_fan_offset(1);
404show_fan_offset(2);
405show_fan_offset(3);
406show_fan_offset(4);
407
408/* vid, vrm, alarms */
409
Jean Delvare1f448092008-04-29 14:03:37 +0200410static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
411 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100414 int vid;
415
Darrick J. Wong8ef1f022009-03-10 12:55:48 -0700416 if ((data->type == adt7463 || data->type == adt7468) &&
417 (data->vid & 0x80)) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100418 /* 6-pin VID (VRM 10) */
419 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
420 } else {
421 /* 5-pin VID (VRM 9) */
422 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
423 }
424
425 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
429
Jean Delvare1f448092008-04-29 14:03:37 +0200430static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
431 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Jean Delvare90d66192007-10-08 18:24:35 +0200433 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return sprintf(buf, "%ld\n", (long) data->vrm);
435}
436
Jean Delvare1f448092008-04-29 14:03:37 +0200437static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100440 struct lm85_data *data = dev_get_drvdata(dev);
441 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return count;
443}
444
445static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
446
Jean Delvare1f448092008-04-29 14:03:37 +0200447static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
448 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200451 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
455
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200456static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
457 char *buf)
458{
459 int nr = to_sensor_dev_attr(attr)->index;
460 struct lm85_data *data = lm85_update_device(dev);
461 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
462}
463
464static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
465static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
466static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
467static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
468static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
469static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
470static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
471static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
472static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
473static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
474static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
475static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
476static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
477static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
478static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
479static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
480static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/* pwm */
483
Jean Delvareb353a482007-07-05 20:36:12 +0200484static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
485 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Jean Delvareb353a482007-07-05 20:36:12 +0200487 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200489 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
Jean Delvareb353a482007-07-05 20:36:12 +0200491
492static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
493 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jean Delvareb353a482007-07-05 20:36:12 +0200495 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct i2c_client *client = to_i2c_client(dev);
497 struct lm85_data *data = i2c_get_clientdata(client);
498 long val = simple_strtol(buf, NULL, 10);
499
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100500 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 data->pwm[nr] = PWM_TO_REG(val);
502 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100503 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return count;
505}
Jean Delvareb353a482007-07-05 20:36:12 +0200506
507static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
508 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Jean Delvareb353a482007-07-05 20:36:12 +0200510 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100512 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100515 switch (pwm_zone) {
516 case -1: /* PWM is always at 100% */
517 enable = 0;
518 break;
519 case 0: /* PWM is always at 0% */
520 case -2: /* PWM responds to manual control */
521 enable = 1;
522 break;
523 default: /* PWM in automatic mode */
524 enable = 2;
525 }
526 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Jean Delvare455f7912007-12-03 23:28:42 +0100529static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
530 *attr, const char *buf, size_t count)
531{
532 int nr = to_sensor_dev_attr(attr)->index;
533 struct i2c_client *client = to_i2c_client(dev);
534 struct lm85_data *data = i2c_get_clientdata(client);
535 long val = simple_strtol(buf, NULL, 10);
536 u8 config;
537
538 switch (val) {
539 case 0:
540 config = 3;
541 break;
542 case 1:
543 config = 7;
544 break;
545 case 2:
546 /* Here we have to choose arbitrarily one of the 5 possible
547 configurations; I go for the safest */
548 config = 6;
549 break;
550 default:
551 return -EINVAL;
552 }
553
554 mutex_lock(&data->update_lock);
555 data->autofan[nr].config = lm85_read_value(client,
556 LM85_REG_AFAN_CONFIG(nr));
557 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
558 | (config << 5);
559 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
560 data->autofan[nr].config);
561 mutex_unlock(&data->update_lock);
562 return count;
563}
564
Jean Delvare34e7dc62008-10-17 17:51:13 +0200565static ssize_t show_pwm_freq(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 int nr = to_sensor_dev_attr(attr)->index;
569 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200570 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->freq_map,
571 data->pwm_freq[nr]));
Jean Delvare34e7dc62008-10-17 17:51:13 +0200572}
573
574static ssize_t set_pwm_freq(struct device *dev,
575 struct device_attribute *attr, const char *buf, size_t count)
576{
577 int nr = to_sensor_dev_attr(attr)->index;
578 struct i2c_client *client = to_i2c_client(dev);
579 struct lm85_data *data = i2c_get_clientdata(client);
580 long val = simple_strtol(buf, NULL, 10);
581
582 mutex_lock(&data->update_lock);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200583 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200584 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
585 (data->zone[nr].range << 4)
586 | data->pwm_freq[nr]);
587 mutex_unlock(&data->update_lock);
588 return count;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200592static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
593 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100594static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200595 show_pwm_enable, set_pwm_enable, offset - 1); \
596static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
597 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599show_pwm_reg(1);
600show_pwm_reg(2);
601show_pwm_reg(3);
602
603/* Voltages */
604
Jean Delvareb353a482007-07-05 20:36:12 +0200605static ssize_t show_in(struct device *dev, struct device_attribute *attr,
606 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", INSEXT_FROM_REG(nr, data->in[nr],
611 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Jean Delvareb353a482007-07-05 20:36:12 +0200613
Jean Delvare1f448092008-04-29 14:03:37 +0200614static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200615 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jean Delvareb353a482007-07-05 20:36:12 +0200617 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200619 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
Jean Delvareb353a482007-07-05 20:36:12 +0200621
622static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
623 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jean Delvareb353a482007-07-05 20:36:12 +0200625 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct i2c_client *client = to_i2c_client(dev);
627 struct lm85_data *data = i2c_get_clientdata(client);
628 long val = simple_strtol(buf, NULL, 10);
629
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100630 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 data->in_min[nr] = INS_TO_REG(nr, val);
632 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100633 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return count;
635}
Jean Delvareb353a482007-07-05 20:36:12 +0200636
637static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
638 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Jean Delvareb353a482007-07-05 20:36:12 +0200640 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200642 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
Jean Delvareb353a482007-07-05 20:36:12 +0200644
645static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
646 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jean Delvareb353a482007-07-05 20:36:12 +0200648 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct i2c_client *client = to_i2c_client(dev);
650 struct lm85_data *data = i2c_get_clientdata(client);
651 long val = simple_strtol(buf, NULL, 10);
652
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 data->in_max[nr] = INS_TO_REG(nr, val);
655 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100656 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return count;
658}
Jean Delvareb353a482007-07-05 20:36:12 +0200659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200661static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
662 show_in, NULL, offset); \
663static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
664 show_in_min, set_in_min, offset); \
665static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
666 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668show_in_reg(0);
669show_in_reg(1);
670show_in_reg(2);
671show_in_reg(3);
672show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200673show_in_reg(5);
674show_in_reg(6);
675show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677/* Temps */
678
Jean Delvareb353a482007-07-05 20:36:12 +0200679static ssize_t show_temp(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", TEMPEXT_FROM_REG(data->temp[nr],
685 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Jean Delvareb353a482007-07-05 20:36:12 +0200687
688static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
689 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", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
Jean Delvareb353a482007-07-05 20:36:12 +0200695
696static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
697 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);
702 long val = simple_strtol(buf, NULL, 10);
703
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800704 if (IS_ADT7468_OFF64(data))
705 val += 64;
706
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100707 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 data->temp_min[nr] = TEMP_TO_REG(val);
709 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100710 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return count;
712}
Jean Delvareb353a482007-07-05 20:36:12 +0200713
714static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
715 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Jean Delvareb353a482007-07-05 20:36:12 +0200717 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200719 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
Jean Delvareb353a482007-07-05 20:36:12 +0200721
722static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
723 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Jean Delvareb353a482007-07-05 20:36:12 +0200725 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct i2c_client *client = to_i2c_client(dev);
727 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200728 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800730 if (IS_ADT7468_OFF64(data))
731 val += 64;
732
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 data->temp_max[nr] = TEMP_TO_REG(val);
735 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100736 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return count;
738}
Jean Delvareb353a482007-07-05 20:36:12 +0200739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200741static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
742 show_temp, NULL, offset - 1); \
743static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
744 show_temp_min, set_temp_min, offset - 1); \
745static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
746 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748show_temp_reg(1);
749show_temp_reg(2);
750show_temp_reg(3);
751
752
753/* Automatic PWM control */
754
Jean Delvareb353a482007-07-05 20:36:12 +0200755static ssize_t show_pwm_auto_channels(struct device *dev,
756 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jean Delvareb353a482007-07-05 20:36:12 +0200758 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200760 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Jean Delvareb353a482007-07-05 20:36:12 +0200762
763static ssize_t set_pwm_auto_channels(struct device *dev,
764 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Jean Delvareb353a482007-07-05 20:36:12 +0200766 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct i2c_client *client = to_i2c_client(dev);
768 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200769 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100771 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200773 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
775 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100776 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return count;
778}
Jean Delvareb353a482007-07-05 20:36:12 +0200779
780static ssize_t show_pwm_auto_pwm_min(struct device *dev,
781 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Jean Delvareb353a482007-07-05 20:36:12 +0200783 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200785 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
Jean Delvareb353a482007-07-05 20:36:12 +0200787
788static ssize_t set_pwm_auto_pwm_min(struct device *dev,
789 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Jean Delvareb353a482007-07-05 20:36:12 +0200791 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct i2c_client *client = to_i2c_client(dev);
793 struct lm85_data *data = i2c_get_clientdata(client);
794 long val = simple_strtol(buf, NULL, 10);
795
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100796 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 data->autofan[nr].min_pwm = PWM_TO_REG(val);
798 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
799 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100800 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return count;
802}
Jean Delvareb353a482007-07-05 20:36:12 +0200803
804static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
805 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Jean Delvareb353a482007-07-05 20:36:12 +0200807 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200809 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
Jean Delvareb353a482007-07-05 20:36:12 +0200811
812static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
813 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jean Delvareb353a482007-07-05 20:36:12 +0200815 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct i2c_client *client = to_i2c_client(dev);
817 struct lm85_data *data = i2c_get_clientdata(client);
818 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200819 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100821 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200823 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
824 tmp &= ~(0x20 << nr);
825 if (data->autofan[nr].min_off)
826 tmp |= 0x20 << nr;
827 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100828 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return count;
830}
Jean Delvareb353a482007-07-05 20:36:12 +0200831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200833static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
834 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
835 set_pwm_auto_channels, offset - 1); \
836static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
837 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
838 set_pwm_auto_pwm_min, offset - 1); \
839static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
840 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200841 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843pwm_auto(1);
844pwm_auto(2);
845pwm_auto(3);
846
847/* Temperature settings for automatic PWM control */
848
Jean Delvareb353a482007-07-05 20:36:12 +0200849static ssize_t show_temp_auto_temp_off(struct device *dev,
850 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvareb353a482007-07-05 20:36:12 +0200852 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200854 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 HYST_FROM_REG(data->zone[nr].hyst));
856}
Jean Delvareb353a482007-07-05 20:36:12 +0200857
858static ssize_t set_temp_auto_temp_off(struct device *dev,
859 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jean Delvareb353a482007-07-05 20:36:12 +0200861 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct i2c_client *client = to_i2c_client(dev);
863 struct lm85_data *data = i2c_get_clientdata(client);
864 int min;
865 long val = simple_strtol(buf, NULL, 10);
866
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100867 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 min = TEMP_FROM_REG(data->zone[nr].limit);
869 data->zone[nr].off_desired = TEMP_TO_REG(val);
870 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200871 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 lm85_write_value(client, LM85_REG_AFAN_HYST1,
873 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200874 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 } else {
876 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200877 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100879 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return count;
881}
Jean Delvareb353a482007-07-05 20:36:12 +0200882
883static ssize_t show_temp_auto_temp_min(struct device *dev,
884 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Jean Delvareb353a482007-07-05 20:36:12 +0200886 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200888 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
Jean Delvareb353a482007-07-05 20:36:12 +0200890
891static ssize_t set_temp_auto_temp_min(struct device *dev,
892 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Jean Delvareb353a482007-07-05 20:36:12 +0200894 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct i2c_client *client = to_i2c_client(dev);
896 struct lm85_data *data = i2c_get_clientdata(client);
897 long val = simple_strtol(buf, NULL, 10);
898
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100899 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 data->zone[nr].limit = TEMP_TO_REG(val);
901 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
902 data->zone[nr].limit);
903
904/* Update temp_auto_max and temp_auto_range */
905 data->zone[nr].range = RANGE_TO_REG(
906 TEMP_FROM_REG(data->zone[nr].max_desired) -
907 TEMP_FROM_REG(data->zone[nr].limit));
908 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
909 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200910 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912/* Update temp_auto_hyst and temp_auto_off */
913 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
914 data->zone[nr].limit) - TEMP_FROM_REG(
915 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200916 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 lm85_write_value(client, LM85_REG_AFAN_HYST1,
918 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200919 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } else {
921 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200922 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100924 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return count;
926}
Jean Delvareb353a482007-07-05 20:36:12 +0200927
928static ssize_t show_temp_auto_temp_max(struct device *dev,
929 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Jean Delvareb353a482007-07-05 20:36:12 +0200931 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200933 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 RANGE_FROM_REG(data->zone[nr].range));
935}
Jean Delvareb353a482007-07-05 20:36:12 +0200936
937static ssize_t set_temp_auto_temp_max(struct device *dev,
938 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Jean Delvareb353a482007-07-05 20:36:12 +0200940 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct i2c_client *client = to_i2c_client(dev);
942 struct lm85_data *data = i2c_get_clientdata(client);
943 int min;
944 long val = simple_strtol(buf, NULL, 10);
945
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100946 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 min = TEMP_FROM_REG(data->zone[nr].limit);
948 data->zone[nr].max_desired = TEMP_TO_REG(val);
949 data->zone[nr].range = RANGE_TO_REG(
950 val - min);
951 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
952 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200953 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100954 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return count;
956}
Jean Delvareb353a482007-07-05 20:36:12 +0200957
958static ssize_t show_temp_auto_temp_crit(struct device *dev,
959 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jean Delvareb353a482007-07-05 20:36:12 +0200961 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200963 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
Jean Delvareb353a482007-07-05 20:36:12 +0200965
966static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200967 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Jean Delvareb353a482007-07-05 20:36:12 +0200969 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct i2c_client *client = to_i2c_client(dev);
971 struct lm85_data *data = i2c_get_clientdata(client);
972 long val = simple_strtol(buf, NULL, 10);
973
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100974 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 data->zone[nr].critical = TEMP_TO_REG(val);
976 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
977 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100978 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return count;
980}
Jean Delvareb353a482007-07-05 20:36:12 +0200981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200983static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
984 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
985 set_temp_auto_temp_off, offset - 1); \
986static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
987 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
988 set_temp_auto_temp_min, offset - 1); \
989static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
990 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
991 set_temp_auto_temp_max, offset - 1); \
992static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
993 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
994 set_temp_auto_temp_crit, offset - 1);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996temp_auto(1);
997temp_auto(2);
998temp_auto(3);
999
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001000static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001001 &sensor_dev_attr_fan1_input.dev_attr.attr,
1002 &sensor_dev_attr_fan2_input.dev_attr.attr,
1003 &sensor_dev_attr_fan3_input.dev_attr.attr,
1004 &sensor_dev_attr_fan4_input.dev_attr.attr,
1005 &sensor_dev_attr_fan1_min.dev_attr.attr,
1006 &sensor_dev_attr_fan2_min.dev_attr.attr,
1007 &sensor_dev_attr_fan3_min.dev_attr.attr,
1008 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001009 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1010 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1011 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1012 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001013
1014 &sensor_dev_attr_pwm1.dev_attr.attr,
1015 &sensor_dev_attr_pwm2.dev_attr.attr,
1016 &sensor_dev_attr_pwm3.dev_attr.attr,
1017 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1018 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1019 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001020 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1021 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1022 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001023
1024 &sensor_dev_attr_in0_input.dev_attr.attr,
1025 &sensor_dev_attr_in1_input.dev_attr.attr,
1026 &sensor_dev_attr_in2_input.dev_attr.attr,
1027 &sensor_dev_attr_in3_input.dev_attr.attr,
1028 &sensor_dev_attr_in0_min.dev_attr.attr,
1029 &sensor_dev_attr_in1_min.dev_attr.attr,
1030 &sensor_dev_attr_in2_min.dev_attr.attr,
1031 &sensor_dev_attr_in3_min.dev_attr.attr,
1032 &sensor_dev_attr_in0_max.dev_attr.attr,
1033 &sensor_dev_attr_in1_max.dev_attr.attr,
1034 &sensor_dev_attr_in2_max.dev_attr.attr,
1035 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001036 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1037 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1038 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1039 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001040
1041 &sensor_dev_attr_temp1_input.dev_attr.attr,
1042 &sensor_dev_attr_temp2_input.dev_attr.attr,
1043 &sensor_dev_attr_temp3_input.dev_attr.attr,
1044 &sensor_dev_attr_temp1_min.dev_attr.attr,
1045 &sensor_dev_attr_temp2_min.dev_attr.attr,
1046 &sensor_dev_attr_temp3_min.dev_attr.attr,
1047 &sensor_dev_attr_temp1_max.dev_attr.attr,
1048 &sensor_dev_attr_temp2_max.dev_attr.attr,
1049 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001050 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1051 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1052 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1053 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1054 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001055
1056 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1057 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1058 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1059 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1060 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1061 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1062 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1063 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1064 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001065
1066 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1067 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1068 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1069 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1070 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1071 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1072 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1073 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1074 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1075 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1076 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1077 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1078
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001079 &dev_attr_vrm.attr,
1080 &dev_attr_cpu0_vid.attr,
1081 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001082 NULL
1083};
1084
1085static const struct attribute_group lm85_group = {
1086 .attrs = lm85_attributes,
1087};
1088
Jean Delvare6b9aad22007-07-05 20:37:21 +02001089static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001090 &sensor_dev_attr_in4_input.dev_attr.attr,
1091 &sensor_dev_attr_in4_min.dev_attr.attr,
1092 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001093 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001094 NULL
1095};
1096
Jean Delvare6b9aad22007-07-05 20:37:21 +02001097static const struct attribute_group lm85_group_in4 = {
1098 .attrs = lm85_attributes_in4,
1099};
1100
1101static struct attribute *lm85_attributes_in567[] = {
1102 &sensor_dev_attr_in5_input.dev_attr.attr,
1103 &sensor_dev_attr_in6_input.dev_attr.attr,
1104 &sensor_dev_attr_in7_input.dev_attr.attr,
1105 &sensor_dev_attr_in5_min.dev_attr.attr,
1106 &sensor_dev_attr_in6_min.dev_attr.attr,
1107 &sensor_dev_attr_in7_min.dev_attr.attr,
1108 &sensor_dev_attr_in5_max.dev_attr.attr,
1109 &sensor_dev_attr_in6_max.dev_attr.attr,
1110 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001111 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1112 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1113 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001114 NULL
1115};
1116
1117static const struct attribute_group lm85_group_in567 = {
1118 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001119};
1120
Jean Delvare5f44759472008-06-25 09:10:30 -04001121static void lm85_init_client(struct i2c_client *client)
1122{
1123 int value;
1124
1125 /* Start monitoring if needed */
1126 value = lm85_read_value(client, LM85_REG_CONFIG);
1127 if (!(value & 0x01)) {
1128 dev_info(&client->dev, "Starting monitoring\n");
1129 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1130 }
1131
1132 /* Warn about unusual configuration bits */
1133 if (value & 0x02)
1134 dev_warn(&client->dev, "Device configuration is locked\n");
1135 if (!(value & 0x04))
1136 dev_warn(&client->dev, "Device is not ready\n");
1137}
1138
Jean Delvare5cfaf332009-09-15 17:18:14 +02001139static int lm85_is_fake(struct i2c_client *client)
1140{
1141 /*
1142 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1143 * emulate the former except that it has no hardware monitoring function
1144 * so the readings are always 0.
1145 */
1146 int i;
1147 u8 in_temp, fan;
1148
1149 for (i = 0; i < 8; i++) {
1150 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1151 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1152 if (in_temp != 0x00 || fan != 0xff)
1153 return 0;
1154 }
1155
1156 return 1;
1157}
1158
Jean Delvare67712d02008-10-17 17:51:14 +02001159/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001160static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Jean Delvare67712d02008-10-17 17:51:14 +02001162 struct i2c_adapter *adapter = client->adapter;
1163 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001164 const char *type_name;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001165 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jean Delvaree89e22b2008-06-25 08:47:35 -04001167 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001169 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Jean Delvared42a2eb2009-12-09 20:35:53 +01001172 /* Determine the chip type */
1173 company = lm85_read_value(client, LM85_REG_COMPANY);
1174 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jean Delvared42a2eb2009-12-09 20:35:53 +01001176 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1177 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1178 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Jean Delvared42a2eb2009-12-09 20:35:53 +01001180 /* All supported chips have the version in common */
1181 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1182 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1183 dev_dbg(&adapter->dev,
1184 "Autodetection failed: unsupported version\n");
1185 return -ENODEV;
1186 }
1187 type_name = "lm85";
1188
1189 /* Now, refine the detection */
1190 if (company == LM85_COMPANY_NATIONAL) {
1191 switch (verstep) {
1192 case LM85_VERSTEP_LM85C:
1193 type_name = "lm85c";
1194 break;
1195 case LM85_VERSTEP_LM85B:
1196 type_name = "lm85b";
1197 break;
1198 case LM85_VERSTEP_LM96000_1:
1199 case LM85_VERSTEP_LM96000_2:
1200 /* Check for Winbond WPCD377I */
1201 if (lm85_is_fake(client)) {
1202 dev_dbg(&adapter->dev,
1203 "Found Winbond WPCD377I, ignoring\n");
1204 return -ENODEV;
1205 }
1206 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001207 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001208 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1209 switch (verstep) {
1210 case LM85_VERSTEP_ADM1027:
1211 type_name = "adm1027";
1212 break;
1213 case LM85_VERSTEP_ADT7463:
1214 case LM85_VERSTEP_ADT7463C:
1215 type_name = "adt7463";
1216 break;
1217 case LM85_VERSTEP_ADT7468_1:
1218 case LM85_VERSTEP_ADT7468_2:
1219 type_name = "adt7468";
1220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001222 } else if (company == LM85_COMPANY_SMSC) {
1223 switch (verstep) {
1224 case LM85_VERSTEP_EMC6D100_A0:
1225 case LM85_VERSTEP_EMC6D100_A1:
1226 /* Note: we can't tell a '100 from a '101 */
1227 type_name = "emc6d100";
1228 break;
1229 case LM85_VERSTEP_EMC6D102:
1230 type_name = "emc6d102";
1231 break;
1232 }
1233 } else {
1234 dev_dbg(&adapter->dev,
1235 "Autodetection failed: unknown vendor\n");
1236 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
Jean Delvare67712d02008-10-17 17:51:14 +02001239 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Jean Delvare67712d02008-10-17 17:51:14 +02001241 return 0;
1242}
1243
1244static int lm85_probe(struct i2c_client *client,
1245 const struct i2c_device_id *id)
1246{
1247 struct lm85_data *data;
1248 int err;
1249
1250 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1251 if (!data)
1252 return -ENOMEM;
1253
1254 i2c_set_clientdata(client, data);
1255 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001256 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jean Delvare67712d02008-10-17 17:51:14 +02001258 /* Fill in the chip specific driver values */
1259 switch (data->type) {
1260 case adm1027:
1261 case adt7463:
1262 case emc6d100:
1263 case emc6d102:
1264 data->freq_map = adm1027_freq_map;
1265 break;
1266 default:
1267 data->freq_map = lm85_freq_map;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001271 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001274 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001277 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1278 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001279 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001281 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001282 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001283 data->vid = lm85_read_value(client, LM85_REG_VID);
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001284 if (!((data->type == adt7463 || data->type == adt7468) &&
1285 (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001286 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001287 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001288 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001289
1290 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001291 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001292 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001293 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001294 goto err_remove_files;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001295
Jean Delvaree89e22b2008-06-25 08:47:35 -04001296 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001297 if (IS_ERR(data->hwmon_dev)) {
1298 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001299 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001300 }
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303
1304 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001305 err_remove_files:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001306 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1307 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare67712d02008-10-17 17:51:14 +02001308 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001309 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvaref9080372008-10-17 17:51:14 +02001310 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return err;
1313}
1314
Jean Delvare67712d02008-10-17 17:51:14 +02001315static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001317 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001318 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001319 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001320 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1321 if (data->type == emc6d100)
1322 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001323 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
1327
Ben Dooksd8d20612005-10-26 21:05:46 +02001328static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 int res;
1331
1332 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001333 switch (reg) {
1334 case LM85_REG_FAN(0): /* Read WORD data */
1335 case LM85_REG_FAN(1):
1336 case LM85_REG_FAN(2):
1337 case LM85_REG_FAN(3):
1338 case LM85_REG_FAN_MIN(0):
1339 case LM85_REG_FAN_MIN(1):
1340 case LM85_REG_FAN_MIN(2):
1341 case LM85_REG_FAN_MIN(3):
1342 case LM85_REG_ALARM1: /* Read both bytes at once */
1343 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1344 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 default: /* Read BYTE data */
1347 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Jean Delvare1f448092008-04-29 14:03:37 +02001351 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Jean Delvaree89e22b2008-06-25 08:47:35 -04001354static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Jean Delvare1f448092008-04-29 14:03:37 +02001356 switch (reg) {
1357 case LM85_REG_FAN(0): /* Write WORD data */
1358 case LM85_REG_FAN(1):
1359 case LM85_REG_FAN(2):
1360 case LM85_REG_FAN(3):
1361 case LM85_REG_FAN_MIN(0):
1362 case LM85_REG_FAN_MIN(1):
1363 case LM85_REG_FAN_MIN(2):
1364 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001366 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1367 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001370 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375static 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 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001393 if (data->type == adm1027 || data->type == adt7463 ||
1394 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int ext1 = lm85_read_value(client,
1396 ADM1027_REG_EXTEND_ADC1);
1397 int ext2 = lm85_read_value(client,
1398 ADM1027_REG_EXTEND_ADC2);
1399 int val = (ext1 << 8) + ext2;
1400
Jean Delvare1f448092008-04-29 14:03:37 +02001401 for (i = 0; i <= 4; i++)
1402 data->in_ext[i] =
1403 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jean Delvare1f448092008-04-29 14:03:37 +02001405 for (i = 0; i <= 2; i++)
1406 data->temp_ext[i] =
1407 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Jean Delvare9c516ef2005-11-26 20:07:54 +01001410 data->vid = lm85_read_value(client, LM85_REG_VID);
1411
1412 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 data->in[i] =
1414 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001415 data->fan[i] =
1416 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001419 if (!((data->type == adt7463 || data->type == adt7468) &&
1420 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001421 data->in[4] = lm85_read_value(client,
1422 LM85_REG_IN(4));
1423 }
1424
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001425 if (data->type == adt7468)
1426 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 for (i = 0; i <= 2; ++i) {
1429 data->temp[i] =
1430 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 data->pwm[i] =
1432 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001433
1434 if (IS_ADT7468_OFF64(data))
1435 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
1438 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1439
Jean Delvaredd1ac532008-05-01 08:47:33 +02001440 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* Three more voltage sensors */
1442 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001443 data->in[i] = lm85_read_value(client,
1444 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001447 data->alarms |= lm85_read_value(client,
1448 EMC6D100_REG_ALARM3) << 16;
1449 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /* Have to read LSB bits after the MSB ones because
1451 the reading of the MSB bits has frozen the
1452 LSBs (backward from the ADM1027).
1453 */
1454 int ext1 = lm85_read_value(client,
1455 EMC6D102_REG_EXTEND_ADC1);
1456 int ext2 = lm85_read_value(client,
1457 EMC6D102_REG_EXTEND_ADC2);
1458 int ext3 = lm85_read_value(client,
1459 EMC6D102_REG_EXTEND_ADC3);
1460 int ext4 = lm85_read_value(client,
1461 EMC6D102_REG_EXTEND_ADC4);
1462 data->in_ext[0] = ext3 & 0x0f;
1463 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001464 data->in_ext[2] = ext4 >> 4;
1465 data->in_ext[3] = ext3 >> 4;
1466 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 data->temp_ext[0] = ext1 & 0x0f;
1469 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001470 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
Jean Delvare1f448092008-04-29 14:03:37 +02001473 data->last_reading = jiffies;
1474 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Jean Delvare1f448092008-04-29 14:03:37 +02001476 if (!data->valid ||
1477 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* Things that don't change often */
1479 dev_dbg(&client->dev, "Reading config values\n");
1480
Jean Delvare9c516ef2005-11-26 20:07:54 +01001481 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 data->in_min[i] =
1483 lm85_read_value(client, LM85_REG_IN_MIN(i));
1484 data->in_max[i] =
1485 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001486 data->fan_min[i] =
1487 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001490 if (!((data->type == adt7463 || data->type == adt7468) &&
1491 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001492 data->in_min[4] = lm85_read_value(client,
1493 LM85_REG_IN_MIN(4));
1494 data->in_max[4] = lm85_read_value(client,
1495 LM85_REG_IN_MAX(4));
1496 }
1497
Jean Delvare1f448092008-04-29 14:03:37 +02001498 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001500 data->in_min[i] = lm85_read_value(client,
1501 EMC6D100_REG_IN_MIN(i));
1502 data->in_max[i] = lm85_read_value(client,
1503 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 }
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001508 int val;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 data->temp_min[i] =
1511 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1512 data->temp_max[i] =
1513 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 data->autofan[i].config =
1516 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1517 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001518 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001519 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 data->autofan[i].min_pwm =
1521 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1522 data->zone[i].limit =
1523 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1524 data->zone[i].critical =
1525 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001526
1527 if (IS_ADT7468_OFF64(data)) {
1528 data->temp_min[i] -= 64;
1529 data->temp_max[i] -= 64;
1530 data->zone[i].limit -= 64;
1531 data->zone[i].critical -= 64;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
1535 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001536 data->autofan[0].min_off = (i & 0x20) != 0;
1537 data->autofan[1].min_off = (i & 0x40) != 0;
1538 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001541 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001542 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001545 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001548 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 data->valid = 1;
1551
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001552 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 return data;
1555}
1556
1557
1558static int __init sm_lm85_init(void)
1559{
1560 return i2c_add_driver(&lm85_driver);
1561}
1562
Jean Delvare1f448092008-04-29 14:03:37 +02001563static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 i2c_del_driver(&lm85_driver);
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001569MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1570 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001571 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1573
1574module_init(sm_lm85_init);
1575module_exit(sm_lm85_exit);