blob: d29bd34a265ee3497b45dc6645a7b06e9f30c62c [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
41/* Insmod parameters */
Darrick J. Wong79b92f22008-11-12 13:26:59 -080042I2C_CLIENT_INSMOD_7(lm85b, lm85c, adm1027, adt7463, adt7468, emc6d100,
43 emc6d102);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* The LM85 registers */
46
47#define LM85_REG_IN(nr) (0x20 + (nr))
48#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
49#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
50
51#define LM85_REG_TEMP(nr) (0x25 + (nr))
52#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
53#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
54
55/* Fan speeds are LSB, MSB (2 bytes) */
Jean Delvare1f448092008-04-29 14:03:37 +020056#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
57#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define LM85_REG_PWM(nr) (0x30 + (nr))
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define LM85_REG_COMPANY 0x3e
62#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080063
64#define ADT7468_REG_CFG5 0x7c
65#define ADT7468_OFF64 0x01
66#define IS_ADT7468_OFF64(data) \
67 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* These are the recognized values for the above regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define LM85_COMPANY_NATIONAL 0x01
71#define LM85_COMPANY_ANALOG_DEV 0x41
Jean Delvare1f448092008-04-29 14:03:37 +020072#define LM85_COMPANY_SMSC 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define LM85_VERSTEP_VMASK 0xf0
74#define LM85_VERSTEP_GENERIC 0x60
Darrick J. Wongc15ade62009-03-10 12:55:47 -070075#define LM85_VERSTEP_GENERIC2 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define LM85_VERSTEP_LM85C 0x60
77#define LM85_VERSTEP_LM85B 0x62
Jean Delvare5cfaf332009-09-15 17:18:14 +020078#define LM85_VERSTEP_LM96000_1 0x68
79#define LM85_VERSTEP_LM96000_2 0x69
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define LM85_VERSTEP_ADM1027 0x60
81#define LM85_VERSTEP_ADT7463 0x62
82#define LM85_VERSTEP_ADT7463C 0x6A
Darrick J. Wong79b92f22008-11-12 13:26:59 -080083#define LM85_VERSTEP_ADT7468_1 0x71
84#define LM85_VERSTEP_ADT7468_2 0x72
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define LM85_VERSTEP_EMC6D100_A0 0x60
86#define LM85_VERSTEP_EMC6D100_A1 0x61
87#define LM85_VERSTEP_EMC6D102 0x65
88
89#define LM85_REG_CONFIG 0x40
90
91#define LM85_REG_ALARM1 0x41
92#define LM85_REG_ALARM2 0x42
93
94#define LM85_REG_VID 0x43
95
96/* Automated FAN control */
97#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
98#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
99#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
101#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
102#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
103#define LM85_REG_AFAN_HYST1 0x6d
104#define LM85_REG_AFAN_HYST2 0x6e
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define ADM1027_REG_EXTEND_ADC1 0x76
107#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109#define EMC6D100_REG_ALARM3 0x7d
110/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200111#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
112#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
113#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define EMC6D102_REG_EXTEND_ADC1 0x85
115#define EMC6D102_REG_EXTEND_ADC2 0x86
116#define EMC6D102_REG_EXTEND_ADC3 0x87
117#define EMC6D102_REG_EXTEND_ADC4 0x88
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jean Delvare1f448092008-04-29 14:03:37 +0200120/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 variants. Note that you should be a bit careful with which arguments
122 these macros are called: arguments may be evaluated more than once.
123 */
124
125/* IN are scaled acording to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400126static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200127 2500, 2250, 3300, 5000, 12000,
128 3300, 1500, 1800 /*EMC6D100*/
129};
130#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jean Delvare1f448092008-04-29 14:03:37 +0200132#define INS_TO_REG(n, val) \
133 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jean Delvare1f448092008-04-29 14:03:37 +0200135#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200136 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jean Delvare1f448092008-04-29 14:03:37 +0200138#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200141static inline u16 FAN_TO_REG(unsigned long val)
142{
143 if (!val)
144 return 0xffff;
145 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
146}
Jean Delvare1f448092008-04-29 14:03:37 +0200147#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
148 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* Temperature is reported in .001 degC increments */
151#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200152 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
153#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200154 SCALE(((val) << 4) + (ext), 16, 1000)
155#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Jean Delvare1f448092008-04-29 14:03:37 +0200157#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define PWM_FROM_REG(val) (val)
159
160
161/* ZONEs have the following parameters:
162 * Limit (low) temp, 1. degC
163 * Hysteresis (below limit), 1. degC (0-15)
164 * Range of speed control, .1 degC (2-80)
165 * Critical (high) temp, 1. degC
166 *
167 * FAN PWMs have the following parameters:
168 * Reference Zone, 1, 2, 3, etc.
169 * Spinup time, .05 sec
170 * PWM value at limit/low temp, 1 count
171 * PWM Frequency, 1. Hz
172 * PWM is Min or OFF below limit, flag
173 * Invert PWM output, flag
174 *
175 * Some chips filter the temp, others the fan.
176 * Filter constant (or disabled) .1 seconds
177 */
178
179/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400180static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200181 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
182 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
183};
184
185static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int i;
188
Jean Delvared38b1492008-04-03 10:40:39 +0200189 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200190 for (i = 0; i < 15; ++i) {
191 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Jean Delvared38b1492008-04-03 10:40:39 +0200194
Jean Delvare1b92ada2008-10-17 17:51:14 +0200195 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
Jean Delvare1f448092008-04-29 14:03:37 +0200197#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200200static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200201 10, 15, 23, 30, 38, 47, 61, 94
202};
203static const int adm1027_freq_map[8] = { /* 1 Hz */
204 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200205};
206
Jean Delvare8a0795d2008-10-17 17:51:14 +0200207static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int i;
210
Jean Delvare86010c92008-10-17 17:51:13 +0200211 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200212 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200213 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200214 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400215 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200217
218static int FREQ_FROM_REG(const int *map, u8 reg)
219{
220 return map[reg & 0x07];
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223/* Since we can't use strings, I'm abusing these numbers
224 * to stand in for the following meanings:
225 * 1 -- PWM responds to Zone 1
226 * 2 -- PWM responds to Zone 2
227 * 3 -- PWM responds to Zone 3
228 * 23 -- PWM responds to the higher temp of Zone 2 or 3
229 * 123 -- PWM responds to highest of Zone 1, 2, or 3
230 * 0 -- PWM is always at 0% (ie, off)
231 * -1 -- PWM is always at 100%
232 * -2 -- PWM responds to manual control
233 */
234
Jean Delvaree89e22b2008-06-25 08:47:35 -0400235static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
236#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jean Delvare1f448092008-04-29 14:03:37 +0200238static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int i;
241
Jean Delvare1f448092008-04-29 14:03:37 +0200242 for (i = 0; i <= 7; ++i)
243 if (zone == lm85_zone_map[i])
244 break;
245 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400247 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Jean Delvare1f448092008-04-29 14:03:37 +0200250#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
251#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* Chip sampling rates
254 *
255 * Some sensors are not updated more frequently than once per second
256 * so it doesn't make sense to read them more often than that.
257 * We cache the results and return the saved data if the driver
258 * is called again before a second has elapsed.
259 *
260 * Also, there is significant configuration data for this chip
261 * given the automatic PWM fan control that is possible. There
262 * are about 47 bytes of config data to only 22 bytes of actual
263 * readings. So, we keep the config data up to date in the cache
264 * when it is written and only sample it once every 1 *minute*
265 */
266#define LM85_DATA_INTERVAL (HZ + HZ / 2)
267#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* LM85 can automatically adjust fan speeds based on temperature
270 * This structure encapsulates an entire Zone config. There are
271 * three zones (one for each temperature input) on the lm85
272 */
273struct lm85_zone {
274 s8 limit; /* Low temp limit */
275 u8 hyst; /* Low limit hysteresis. (0-15) */
276 u8 range; /* Temp range, encoded */
277 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200278 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * to prevent "drift" as other autofan control
280 * values change.
281 */
Jean Delvare1f448092008-04-29 14:03:37 +0200282 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * to prevent "drift" as other autofan control
284 * values change.
285 */
286};
287
288struct lm85_autofan {
289 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 u8 min_pwm; /* Minimum PWM value, encoded */
291 u8 min_off; /* Min PWM or OFF below "limit", flag */
292};
293
Jean Delvareed6bafb2007-02-14 21:15:03 +0100294/* For each registered chip, we need to keep some data in memory.
295 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700297 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200298 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 enum chips type;
300
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100301 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int valid; /* !=0 if following fields are valid */
303 unsigned long last_reading; /* In jiffies */
304 unsigned long last_config; /* In jiffies */
305
306 u8 in[8]; /* Register value */
307 u8 in_max[8]; /* Register value */
308 u8 in_min[8]; /* Register value */
309 s8 temp[3]; /* Register value */
310 s8 temp_min[3]; /* Register value */
311 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 u16 fan[4]; /* Register value */
313 u16 fan_min[4]; /* Register value */
314 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200315 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 u8 temp_ext[3]; /* Decoded values */
317 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 u8 vid; /* Register value */
319 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800321 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct lm85_autofan autofan[3];
323 struct lm85_zone zone[3];
324};
325
Jean Delvare310ec792009-12-14 21:17:23 +0100326static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200327static int lm85_probe(struct i2c_client *client,
328 const struct i2c_device_id *id);
329static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100331static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400332static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335
Jean Delvare67712d02008-10-17 17:51:14 +0200336static const struct i2c_device_id lm85_id[] = {
337 { "adm1027", adm1027 },
338 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700339 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200340 { "lm85", any_chip },
341 { "lm85b", lm85b },
342 { "lm85c", lm85c },
343 { "emc6d100", emc6d100 },
344 { "emc6d101", emc6d100 },
345 { "emc6d102", emc6d102 },
346 { }
347};
348MODULE_DEVICE_TABLE(i2c, lm85_id);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200351 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100352 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100353 .name = "lm85",
354 },
Jean Delvare67712d02008-10-17 17:51:14 +0200355 .probe = lm85_probe,
356 .remove = lm85_remove,
357 .id_table = lm85_id,
358 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100359 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
362
363/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200364static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
365 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Jean Delvareb353a482007-07-05 20:36:12 +0200367 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200369 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
Jean Delvareb353a482007-07-05 20:36:12 +0200371
372static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
373 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jean Delvareb353a482007-07-05 20:36:12 +0200375 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200377 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Jean Delvareb353a482007-07-05 20:36:12 +0200379
380static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
381 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvareb353a482007-07-05 20:36:12 +0200383 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct i2c_client *client = to_i2c_client(dev);
385 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200386 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100388 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 data->fan_min[nr] = FAN_TO_REG(val);
390 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100391 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return count;
393}
394
395#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200396static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
397 show_fan, NULL, offset - 1); \
398static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
399 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401show_fan_offset(1);
402show_fan_offset(2);
403show_fan_offset(3);
404show_fan_offset(4);
405
406/* vid, vrm, alarms */
407
Jean Delvare1f448092008-04-29 14:03:37 +0200408static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
409 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100412 int vid;
413
Darrick J. Wong8ef1f022009-03-10 12:55:48 -0700414 if ((data->type == adt7463 || data->type == adt7468) &&
415 (data->vid & 0x80)) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100416 /* 6-pin VID (VRM 10) */
417 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
418 } else {
419 /* 5-pin VID (VRM 9) */
420 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
421 }
422
423 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
427
Jean Delvare1f448092008-04-29 14:03:37 +0200428static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
429 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Jean Delvare90d66192007-10-08 18:24:35 +0200431 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return sprintf(buf, "%ld\n", (long) data->vrm);
433}
434
Jean Delvare1f448092008-04-29 14:03:37 +0200435static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100438 struct lm85_data *data = dev_get_drvdata(dev);
439 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return count;
441}
442
443static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
444
Jean Delvare1f448092008-04-29 14:03:37 +0200445static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
446 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200449 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
453
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200454static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
455 char *buf)
456{
457 int nr = to_sensor_dev_attr(attr)->index;
458 struct lm85_data *data = lm85_update_device(dev);
459 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
460}
461
462static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
463static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
464static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
465static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
466static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
467static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
468static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
469static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
470static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
471static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
472static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
473static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
474static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
475static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
476static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
477static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
478static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/* pwm */
481
Jean Delvareb353a482007-07-05 20:36:12 +0200482static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
483 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Jean Delvareb353a482007-07-05 20:36:12 +0200485 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200487 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
Jean Delvareb353a482007-07-05 20:36:12 +0200489
490static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jean Delvareb353a482007-07-05 20:36:12 +0200493 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct i2c_client *client = to_i2c_client(dev);
495 struct lm85_data *data = i2c_get_clientdata(client);
496 long val = simple_strtol(buf, NULL, 10);
497
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100498 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 data->pwm[nr] = PWM_TO_REG(val);
500 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100501 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return count;
503}
Jean Delvareb353a482007-07-05 20:36:12 +0200504
505static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
506 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Jean Delvareb353a482007-07-05 20:36:12 +0200508 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100510 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100513 switch (pwm_zone) {
514 case -1: /* PWM is always at 100% */
515 enable = 0;
516 break;
517 case 0: /* PWM is always at 0% */
518 case -2: /* PWM responds to manual control */
519 enable = 1;
520 break;
521 default: /* PWM in automatic mode */
522 enable = 2;
523 }
524 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Jean Delvare455f7912007-12-03 23:28:42 +0100527static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
528 *attr, const char *buf, size_t count)
529{
530 int nr = to_sensor_dev_attr(attr)->index;
531 struct i2c_client *client = to_i2c_client(dev);
532 struct lm85_data *data = i2c_get_clientdata(client);
533 long val = simple_strtol(buf, NULL, 10);
534 u8 config;
535
536 switch (val) {
537 case 0:
538 config = 3;
539 break;
540 case 1:
541 config = 7;
542 break;
543 case 2:
544 /* Here we have to choose arbitrarily one of the 5 possible
545 configurations; I go for the safest */
546 config = 6;
547 break;
548 default:
549 return -EINVAL;
550 }
551
552 mutex_lock(&data->update_lock);
553 data->autofan[nr].config = lm85_read_value(client,
554 LM85_REG_AFAN_CONFIG(nr));
555 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
556 | (config << 5);
557 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
558 data->autofan[nr].config);
559 mutex_unlock(&data->update_lock);
560 return count;
561}
562
Jean Delvare34e7dc62008-10-17 17:51:13 +0200563static ssize_t show_pwm_freq(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
566 int nr = to_sensor_dev_attr(attr)->index;
567 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200568 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->freq_map,
569 data->pwm_freq[nr]));
Jean Delvare34e7dc62008-10-17 17:51:13 +0200570}
571
572static ssize_t set_pwm_freq(struct device *dev,
573 struct device_attribute *attr, const char *buf, size_t count)
574{
575 int nr = to_sensor_dev_attr(attr)->index;
576 struct i2c_client *client = to_i2c_client(dev);
577 struct lm85_data *data = i2c_get_clientdata(client);
578 long val = simple_strtol(buf, NULL, 10);
579
580 mutex_lock(&data->update_lock);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200581 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200582 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
583 (data->zone[nr].range << 4)
584 | data->pwm_freq[nr]);
585 mutex_unlock(&data->update_lock);
586 return count;
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200590static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
591 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100592static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200593 show_pwm_enable, set_pwm_enable, offset - 1); \
594static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
595 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597show_pwm_reg(1);
598show_pwm_reg(2);
599show_pwm_reg(3);
600
601/* Voltages */
602
Jean Delvareb353a482007-07-05 20:36:12 +0200603static ssize_t show_in(struct device *dev, struct device_attribute *attr,
604 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jean Delvareb353a482007-07-05 20:36:12 +0200606 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200608 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
609 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Jean Delvareb353a482007-07-05 20:36:12 +0200611
Jean Delvare1f448092008-04-29 14:03:37 +0200612static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200613 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Jean Delvareb353a482007-07-05 20:36:12 +0200615 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200617 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
Jean Delvareb353a482007-07-05 20:36:12 +0200619
620static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
621 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Jean Delvareb353a482007-07-05 20:36:12 +0200623 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct i2c_client *client = to_i2c_client(dev);
625 struct lm85_data *data = i2c_get_clientdata(client);
626 long val = simple_strtol(buf, NULL, 10);
627
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100628 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 data->in_min[nr] = INS_TO_REG(nr, val);
630 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return count;
633}
Jean Delvareb353a482007-07-05 20:36:12 +0200634
635static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
636 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jean Delvareb353a482007-07-05 20:36:12 +0200638 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200640 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
Jean Delvareb353a482007-07-05 20:36:12 +0200642
643static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
644 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Jean Delvareb353a482007-07-05 20:36:12 +0200646 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct i2c_client *client = to_i2c_client(dev);
648 struct lm85_data *data = i2c_get_clientdata(client);
649 long val = simple_strtol(buf, NULL, 10);
650
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100651 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 data->in_max[nr] = INS_TO_REG(nr, val);
653 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100654 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return count;
656}
Jean Delvareb353a482007-07-05 20:36:12 +0200657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200659static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
660 show_in, NULL, offset); \
661static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
662 show_in_min, set_in_min, offset); \
663static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
664 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666show_in_reg(0);
667show_in_reg(1);
668show_in_reg(2);
669show_in_reg(3);
670show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200671show_in_reg(5);
672show_in_reg(6);
673show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675/* Temps */
676
Jean Delvareb353a482007-07-05 20:36:12 +0200677static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
678 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Jean Delvareb353a482007-07-05 20:36:12 +0200680 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200682 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
683 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
Jean Delvareb353a482007-07-05 20:36:12 +0200685
686static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
687 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Jean Delvareb353a482007-07-05 20:36:12 +0200689 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200691 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Jean Delvareb353a482007-07-05 20:36:12 +0200693
694static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Jean Delvareb353a482007-07-05 20:36:12 +0200697 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct i2c_client *client = to_i2c_client(dev);
699 struct lm85_data *data = i2c_get_clientdata(client);
700 long val = simple_strtol(buf, NULL, 10);
701
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800702 if (IS_ADT7468_OFF64(data))
703 val += 64;
704
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100705 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 data->temp_min[nr] = TEMP_TO_REG(val);
707 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100708 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return count;
710}
Jean Delvareb353a482007-07-05 20:36:12 +0200711
712static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
713 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Jean Delvareb353a482007-07-05 20:36:12 +0200715 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200717 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
Jean Delvareb353a482007-07-05 20:36:12 +0200719
720static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
721 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jean Delvareb353a482007-07-05 20:36:12 +0200723 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct i2c_client *client = to_i2c_client(dev);
725 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200726 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800728 if (IS_ADT7468_OFF64(data))
729 val += 64;
730
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100731 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 data->temp_max[nr] = TEMP_TO_REG(val);
733 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100734 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return count;
736}
Jean Delvareb353a482007-07-05 20:36:12 +0200737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200739static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
740 show_temp, NULL, offset - 1); \
741static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
742 show_temp_min, set_temp_min, offset - 1); \
743static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
744 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746show_temp_reg(1);
747show_temp_reg(2);
748show_temp_reg(3);
749
750
751/* Automatic PWM control */
752
Jean Delvareb353a482007-07-05 20:36:12 +0200753static ssize_t show_pwm_auto_channels(struct device *dev,
754 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Jean Delvareb353a482007-07-05 20:36:12 +0200756 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200758 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Jean Delvareb353a482007-07-05 20:36:12 +0200760
761static ssize_t set_pwm_auto_channels(struct device *dev,
762 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Jean Delvareb353a482007-07-05 20:36:12 +0200764 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct i2c_client *client = to_i2c_client(dev);
766 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200767 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100769 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200771 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
773 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100774 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return count;
776}
Jean Delvareb353a482007-07-05 20:36:12 +0200777
778static ssize_t show_pwm_auto_pwm_min(struct device *dev,
779 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Jean Delvareb353a482007-07-05 20:36:12 +0200781 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200783 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
Jean Delvareb353a482007-07-05 20:36:12 +0200785
786static ssize_t set_pwm_auto_pwm_min(struct device *dev,
787 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Jean Delvareb353a482007-07-05 20:36:12 +0200789 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct i2c_client *client = to_i2c_client(dev);
791 struct lm85_data *data = i2c_get_clientdata(client);
792 long val = simple_strtol(buf, NULL, 10);
793
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100794 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 data->autofan[nr].min_pwm = PWM_TO_REG(val);
796 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
797 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100798 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return count;
800}
Jean Delvareb353a482007-07-05 20:36:12 +0200801
802static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
803 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Jean Delvareb353a482007-07-05 20:36:12 +0200805 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200807 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
Jean Delvareb353a482007-07-05 20:36:12 +0200809
810static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
811 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Jean Delvareb353a482007-07-05 20:36:12 +0200813 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct i2c_client *client = to_i2c_client(dev);
815 struct lm85_data *data = i2c_get_clientdata(client);
816 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200817 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100819 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200821 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
822 tmp &= ~(0x20 << nr);
823 if (data->autofan[nr].min_off)
824 tmp |= 0x20 << nr;
825 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100826 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return count;
828}
Jean Delvareb353a482007-07-05 20:36:12 +0200829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200831static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
832 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
833 set_pwm_auto_channels, offset - 1); \
834static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
835 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
836 set_pwm_auto_pwm_min, offset - 1); \
837static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
838 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200839 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841pwm_auto(1);
842pwm_auto(2);
843pwm_auto(3);
844
845/* Temperature settings for automatic PWM control */
846
Jean Delvareb353a482007-07-05 20:36:12 +0200847static ssize_t show_temp_auto_temp_off(struct device *dev,
848 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Jean Delvareb353a482007-07-05 20:36:12 +0200850 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200852 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 HYST_FROM_REG(data->zone[nr].hyst));
854}
Jean Delvareb353a482007-07-05 20:36:12 +0200855
856static ssize_t set_temp_auto_temp_off(struct device *dev,
857 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Jean Delvareb353a482007-07-05 20:36:12 +0200859 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct i2c_client *client = to_i2c_client(dev);
861 struct lm85_data *data = i2c_get_clientdata(client);
862 int min;
863 long val = simple_strtol(buf, NULL, 10);
864
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100865 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 min = TEMP_FROM_REG(data->zone[nr].limit);
867 data->zone[nr].off_desired = TEMP_TO_REG(val);
868 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200869 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 lm85_write_value(client, LM85_REG_AFAN_HYST1,
871 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200872 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
874 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200875 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100877 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return count;
879}
Jean Delvareb353a482007-07-05 20:36:12 +0200880
881static ssize_t show_temp_auto_temp_min(struct device *dev,
882 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Jean Delvareb353a482007-07-05 20:36:12 +0200884 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200886 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
Jean Delvareb353a482007-07-05 20:36:12 +0200888
889static ssize_t set_temp_auto_temp_min(struct device *dev,
890 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jean Delvareb353a482007-07-05 20:36:12 +0200892 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct i2c_client *client = to_i2c_client(dev);
894 struct lm85_data *data = i2c_get_clientdata(client);
895 long val = simple_strtol(buf, NULL, 10);
896
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100897 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 data->zone[nr].limit = TEMP_TO_REG(val);
899 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
900 data->zone[nr].limit);
901
902/* Update temp_auto_max and temp_auto_range */
903 data->zone[nr].range = RANGE_TO_REG(
904 TEMP_FROM_REG(data->zone[nr].max_desired) -
905 TEMP_FROM_REG(data->zone[nr].limit));
906 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
907 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200908 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910/* Update temp_auto_hyst and temp_auto_off */
911 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
912 data->zone[nr].limit) - TEMP_FROM_REG(
913 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200914 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 lm85_write_value(client, LM85_REG_AFAN_HYST1,
916 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200917 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 } else {
919 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200920 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100922 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return count;
924}
Jean Delvareb353a482007-07-05 20:36:12 +0200925
926static ssize_t show_temp_auto_temp_max(struct device *dev,
927 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Jean Delvareb353a482007-07-05 20:36:12 +0200929 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200931 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 RANGE_FROM_REG(data->zone[nr].range));
933}
Jean Delvareb353a482007-07-05 20:36:12 +0200934
935static ssize_t set_temp_auto_temp_max(struct device *dev,
936 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jean Delvareb353a482007-07-05 20:36:12 +0200938 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct i2c_client *client = to_i2c_client(dev);
940 struct lm85_data *data = i2c_get_clientdata(client);
941 int min;
942 long val = simple_strtol(buf, NULL, 10);
943
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100944 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 min = TEMP_FROM_REG(data->zone[nr].limit);
946 data->zone[nr].max_desired = TEMP_TO_REG(val);
947 data->zone[nr].range = RANGE_TO_REG(
948 val - min);
949 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
950 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200951 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100952 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return count;
954}
Jean Delvareb353a482007-07-05 20:36:12 +0200955
956static ssize_t show_temp_auto_temp_crit(struct device *dev,
957 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Jean Delvareb353a482007-07-05 20:36:12 +0200959 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200961 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
Jean Delvareb353a482007-07-05 20:36:12 +0200963
964static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200965 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Jean Delvareb353a482007-07-05 20:36:12 +0200967 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct i2c_client *client = to_i2c_client(dev);
969 struct lm85_data *data = i2c_get_clientdata(client);
970 long val = simple_strtol(buf, NULL, 10);
971
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100972 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 data->zone[nr].critical = TEMP_TO_REG(val);
974 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
975 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100976 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return count;
978}
Jean Delvareb353a482007-07-05 20:36:12 +0200979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200981static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
982 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
983 set_temp_auto_temp_off, offset - 1); \
984static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
985 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
986 set_temp_auto_temp_min, offset - 1); \
987static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
988 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
989 set_temp_auto_temp_max, offset - 1); \
990static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
991 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
992 set_temp_auto_temp_crit, offset - 1);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994temp_auto(1);
995temp_auto(2);
996temp_auto(3);
997
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200998static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200999 &sensor_dev_attr_fan1_input.dev_attr.attr,
1000 &sensor_dev_attr_fan2_input.dev_attr.attr,
1001 &sensor_dev_attr_fan3_input.dev_attr.attr,
1002 &sensor_dev_attr_fan4_input.dev_attr.attr,
1003 &sensor_dev_attr_fan1_min.dev_attr.attr,
1004 &sensor_dev_attr_fan2_min.dev_attr.attr,
1005 &sensor_dev_attr_fan3_min.dev_attr.attr,
1006 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001007 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1008 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1009 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1010 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001011
1012 &sensor_dev_attr_pwm1.dev_attr.attr,
1013 &sensor_dev_attr_pwm2.dev_attr.attr,
1014 &sensor_dev_attr_pwm3.dev_attr.attr,
1015 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1016 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1017 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001018 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1019 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1020 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001021
1022 &sensor_dev_attr_in0_input.dev_attr.attr,
1023 &sensor_dev_attr_in1_input.dev_attr.attr,
1024 &sensor_dev_attr_in2_input.dev_attr.attr,
1025 &sensor_dev_attr_in3_input.dev_attr.attr,
1026 &sensor_dev_attr_in0_min.dev_attr.attr,
1027 &sensor_dev_attr_in1_min.dev_attr.attr,
1028 &sensor_dev_attr_in2_min.dev_attr.attr,
1029 &sensor_dev_attr_in3_min.dev_attr.attr,
1030 &sensor_dev_attr_in0_max.dev_attr.attr,
1031 &sensor_dev_attr_in1_max.dev_attr.attr,
1032 &sensor_dev_attr_in2_max.dev_attr.attr,
1033 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001034 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1035 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1036 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1037 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001038
1039 &sensor_dev_attr_temp1_input.dev_attr.attr,
1040 &sensor_dev_attr_temp2_input.dev_attr.attr,
1041 &sensor_dev_attr_temp3_input.dev_attr.attr,
1042 &sensor_dev_attr_temp1_min.dev_attr.attr,
1043 &sensor_dev_attr_temp2_min.dev_attr.attr,
1044 &sensor_dev_attr_temp3_min.dev_attr.attr,
1045 &sensor_dev_attr_temp1_max.dev_attr.attr,
1046 &sensor_dev_attr_temp2_max.dev_attr.attr,
1047 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001048 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1049 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1050 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1051 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1052 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001053
1054 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1055 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1056 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1057 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1058 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1059 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1060 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1061 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1062 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001063
1064 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1065 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1066 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1067 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1068 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1069 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1070 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1071 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1072 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1073 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1074 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1075 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1076
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001077 &dev_attr_vrm.attr,
1078 &dev_attr_cpu0_vid.attr,
1079 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001080 NULL
1081};
1082
1083static const struct attribute_group lm85_group = {
1084 .attrs = lm85_attributes,
1085};
1086
Jean Delvare6b9aad22007-07-05 20:37:21 +02001087static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001088 &sensor_dev_attr_in4_input.dev_attr.attr,
1089 &sensor_dev_attr_in4_min.dev_attr.attr,
1090 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001091 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001092 NULL
1093};
1094
Jean Delvare6b9aad22007-07-05 20:37:21 +02001095static const struct attribute_group lm85_group_in4 = {
1096 .attrs = lm85_attributes_in4,
1097};
1098
1099static struct attribute *lm85_attributes_in567[] = {
1100 &sensor_dev_attr_in5_input.dev_attr.attr,
1101 &sensor_dev_attr_in6_input.dev_attr.attr,
1102 &sensor_dev_attr_in7_input.dev_attr.attr,
1103 &sensor_dev_attr_in5_min.dev_attr.attr,
1104 &sensor_dev_attr_in6_min.dev_attr.attr,
1105 &sensor_dev_attr_in7_min.dev_attr.attr,
1106 &sensor_dev_attr_in5_max.dev_attr.attr,
1107 &sensor_dev_attr_in6_max.dev_attr.attr,
1108 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001109 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1110 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1111 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001112 NULL
1113};
1114
1115static const struct attribute_group lm85_group_in567 = {
1116 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001117};
1118
Jean Delvare5f44759472008-06-25 09:10:30 -04001119static void lm85_init_client(struct i2c_client *client)
1120{
1121 int value;
1122
1123 /* Start monitoring if needed */
1124 value = lm85_read_value(client, LM85_REG_CONFIG);
1125 if (!(value & 0x01)) {
1126 dev_info(&client->dev, "Starting monitoring\n");
1127 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1128 }
1129
1130 /* Warn about unusual configuration bits */
1131 if (value & 0x02)
1132 dev_warn(&client->dev, "Device configuration is locked\n");
1133 if (!(value & 0x04))
1134 dev_warn(&client->dev, "Device is not ready\n");
1135}
1136
Jean Delvare5cfaf332009-09-15 17:18:14 +02001137static int lm85_is_fake(struct i2c_client *client)
1138{
1139 /*
1140 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1141 * emulate the former except that it has no hardware monitoring function
1142 * so the readings are always 0.
1143 */
1144 int i;
1145 u8 in_temp, fan;
1146
1147 for (i = 0; i < 8; i++) {
1148 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1149 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1150 if (in_temp != 0x00 || fan != 0xff)
1151 return 0;
1152 }
1153
1154 return 1;
1155}
1156
Jean Delvare67712d02008-10-17 17:51:14 +02001157/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001158static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Jean Delvare67712d02008-10-17 17:51:14 +02001160 struct i2c_adapter *adapter = client->adapter;
1161 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001162 const char *type_name;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001163 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jean Delvaree89e22b2008-06-25 08:47:35 -04001165 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001167 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jean Delvared42a2eb2009-12-09 20:35:53 +01001170 /* Determine the chip type */
1171 company = lm85_read_value(client, LM85_REG_COMPANY);
1172 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jean Delvared42a2eb2009-12-09 20:35:53 +01001174 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1175 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1176 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Jean Delvared42a2eb2009-12-09 20:35:53 +01001178 /* All supported chips have the version in common */
1179 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1180 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1181 dev_dbg(&adapter->dev,
1182 "Autodetection failed: unsupported version\n");
1183 return -ENODEV;
1184 }
1185 type_name = "lm85";
1186
1187 /* Now, refine the detection */
1188 if (company == LM85_COMPANY_NATIONAL) {
1189 switch (verstep) {
1190 case LM85_VERSTEP_LM85C:
1191 type_name = "lm85c";
1192 break;
1193 case LM85_VERSTEP_LM85B:
1194 type_name = "lm85b";
1195 break;
1196 case LM85_VERSTEP_LM96000_1:
1197 case LM85_VERSTEP_LM96000_2:
1198 /* Check for Winbond WPCD377I */
1199 if (lm85_is_fake(client)) {
1200 dev_dbg(&adapter->dev,
1201 "Found Winbond WPCD377I, ignoring\n");
1202 return -ENODEV;
1203 }
1204 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001205 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001206 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1207 switch (verstep) {
1208 case LM85_VERSTEP_ADM1027:
1209 type_name = "adm1027";
1210 break;
1211 case LM85_VERSTEP_ADT7463:
1212 case LM85_VERSTEP_ADT7463C:
1213 type_name = "adt7463";
1214 break;
1215 case LM85_VERSTEP_ADT7468_1:
1216 case LM85_VERSTEP_ADT7468_2:
1217 type_name = "adt7468";
1218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001220 } else if (company == LM85_COMPANY_SMSC) {
1221 switch (verstep) {
1222 case LM85_VERSTEP_EMC6D100_A0:
1223 case LM85_VERSTEP_EMC6D100_A1:
1224 /* Note: we can't tell a '100 from a '101 */
1225 type_name = "emc6d100";
1226 break;
1227 case LM85_VERSTEP_EMC6D102:
1228 type_name = "emc6d102";
1229 break;
1230 }
1231 } else {
1232 dev_dbg(&adapter->dev,
1233 "Autodetection failed: unknown vendor\n");
1234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Jean Delvare67712d02008-10-17 17:51:14 +02001237 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Jean Delvare67712d02008-10-17 17:51:14 +02001239 return 0;
1240}
1241
1242static int lm85_probe(struct i2c_client *client,
1243 const struct i2c_device_id *id)
1244{
1245 struct lm85_data *data;
1246 int err;
1247
1248 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1249 if (!data)
1250 return -ENOMEM;
1251
1252 i2c_set_clientdata(client, data);
1253 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001254 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jean Delvare67712d02008-10-17 17:51:14 +02001256 /* Fill in the chip specific driver values */
1257 switch (data->type) {
1258 case adm1027:
1259 case adt7463:
1260 case emc6d100:
1261 case emc6d102:
1262 data->freq_map = adm1027_freq_map;
1263 break;
1264 default:
1265 data->freq_map = lm85_freq_map;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001269 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001272 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001275 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1276 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001277 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001279 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001280 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001281 data->vid = lm85_read_value(client, LM85_REG_VID);
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001282 if (!((data->type == adt7463 || data->type == adt7468) &&
1283 (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001284 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001285 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001286 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001287
1288 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001289 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001290 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001291 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001292 goto err_remove_files;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001293
Jean Delvaree89e22b2008-06-25 08:47:35 -04001294 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001295 if (IS_ERR(data->hwmon_dev)) {
1296 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001297 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
1301
1302 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001303 err_remove_files:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001304 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1305 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare67712d02008-10-17 17:51:14 +02001306 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001307 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvaref9080372008-10-17 17:51:14 +02001308 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return err;
1311}
1312
Jean Delvare67712d02008-10-17 17:51:14 +02001313static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001315 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001316 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001317 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001318 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1319 if (data->type == emc6d100)
1320 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001321 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return 0;
1323}
1324
1325
Ben Dooksd8d20612005-10-26 21:05:46 +02001326static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 int res;
1329
1330 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001331 switch (reg) {
1332 case LM85_REG_FAN(0): /* Read WORD data */
1333 case LM85_REG_FAN(1):
1334 case LM85_REG_FAN(2):
1335 case LM85_REG_FAN(3):
1336 case LM85_REG_FAN_MIN(0):
1337 case LM85_REG_FAN_MIN(1):
1338 case LM85_REG_FAN_MIN(2):
1339 case LM85_REG_FAN_MIN(3):
1340 case LM85_REG_ALARM1: /* Read both bytes at once */
1341 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1342 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 default: /* Read BYTE data */
1345 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348
Jean Delvare1f448092008-04-29 14:03:37 +02001349 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Jean Delvaree89e22b2008-06-25 08:47:35 -04001352static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Jean Delvare1f448092008-04-29 14:03:37 +02001354 switch (reg) {
1355 case LM85_REG_FAN(0): /* Write WORD data */
1356 case LM85_REG_FAN(1):
1357 case LM85_REG_FAN(2):
1358 case LM85_REG_FAN(3):
1359 case LM85_REG_FAN_MIN(0):
1360 case LM85_REG_FAN_MIN(1):
1361 case LM85_REG_FAN_MIN(2):
1362 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001364 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1365 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001368 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373static struct lm85_data *lm85_update_device(struct device *dev)
1374{
1375 struct i2c_client *client = to_i2c_client(dev);
1376 struct lm85_data *data = i2c_get_clientdata(client);
1377 int i;
1378
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001379 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jean Delvare1f448092008-04-29 14:03:37 +02001381 if (!data->valid ||
1382 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* Things that change quickly */
1384 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* Have to read extended bits first to "freeze" the
1387 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001388 * There are 2 additional resolution bits per channel and we
1389 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001391 if (data->type == adm1027 || data->type == adt7463 ||
1392 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int ext1 = lm85_read_value(client,
1394 ADM1027_REG_EXTEND_ADC1);
1395 int ext2 = lm85_read_value(client,
1396 ADM1027_REG_EXTEND_ADC2);
1397 int val = (ext1 << 8) + ext2;
1398
Jean Delvare1f448092008-04-29 14:03:37 +02001399 for (i = 0; i <= 4; i++)
1400 data->in_ext[i] =
1401 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Jean Delvare1f448092008-04-29 14:03:37 +02001403 for (i = 0; i <= 2; i++)
1404 data->temp_ext[i] =
1405 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Jean Delvare9c516ef2005-11-26 20:07:54 +01001408 data->vid = lm85_read_value(client, LM85_REG_VID);
1409
1410 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 data->in[i] =
1412 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001413 data->fan[i] =
1414 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001417 if (!((data->type == adt7463 || data->type == adt7468) &&
1418 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001419 data->in[4] = lm85_read_value(client,
1420 LM85_REG_IN(4));
1421 }
1422
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001423 if (data->type == adt7468)
1424 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 for (i = 0; i <= 2; ++i) {
1427 data->temp[i] =
1428 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 data->pwm[i] =
1430 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001431
1432 if (IS_ADT7468_OFF64(data))
1433 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
1436 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1437
Jean Delvaredd1ac532008-05-01 08:47:33 +02001438 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* Three more voltage sensors */
1440 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001441 data->in[i] = lm85_read_value(client,
1442 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001445 data->alarms |= lm85_read_value(client,
1446 EMC6D100_REG_ALARM3) << 16;
1447 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* Have to read LSB bits after the MSB ones because
1449 the reading of the MSB bits has frozen the
1450 LSBs (backward from the ADM1027).
1451 */
1452 int ext1 = lm85_read_value(client,
1453 EMC6D102_REG_EXTEND_ADC1);
1454 int ext2 = lm85_read_value(client,
1455 EMC6D102_REG_EXTEND_ADC2);
1456 int ext3 = lm85_read_value(client,
1457 EMC6D102_REG_EXTEND_ADC3);
1458 int ext4 = lm85_read_value(client,
1459 EMC6D102_REG_EXTEND_ADC4);
1460 data->in_ext[0] = ext3 & 0x0f;
1461 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001462 data->in_ext[2] = ext4 >> 4;
1463 data->in_ext[3] = ext3 >> 4;
1464 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 data->temp_ext[0] = ext1 & 0x0f;
1467 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001468 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
Jean Delvare1f448092008-04-29 14:03:37 +02001471 data->last_reading = jiffies;
1472 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Jean Delvare1f448092008-04-29 14:03:37 +02001474 if (!data->valid ||
1475 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* Things that don't change often */
1477 dev_dbg(&client->dev, "Reading config values\n");
1478
Jean Delvare9c516ef2005-11-26 20:07:54 +01001479 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 data->in_min[i] =
1481 lm85_read_value(client, LM85_REG_IN_MIN(i));
1482 data->in_max[i] =
1483 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001484 data->fan_min[i] =
1485 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001488 if (!((data->type == adt7463 || data->type == adt7468) &&
1489 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001490 data->in_min[4] = lm85_read_value(client,
1491 LM85_REG_IN_MIN(4));
1492 data->in_max[4] = lm85_read_value(client,
1493 LM85_REG_IN_MAX(4));
1494 }
1495
Jean Delvare1f448092008-04-29 14:03:37 +02001496 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001498 data->in_min[i] = lm85_read_value(client,
1499 EMC6D100_REG_IN_MIN(i));
1500 data->in_max[i] = lm85_read_value(client,
1501 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001506 int val;
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 data->temp_min[i] =
1509 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1510 data->temp_max[i] =
1511 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 data->autofan[i].config =
1514 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1515 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001516 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001517 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 data->autofan[i].min_pwm =
1519 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1520 data->zone[i].limit =
1521 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1522 data->zone[i].critical =
1523 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001524
1525 if (IS_ADT7468_OFF64(data)) {
1526 data->temp_min[i] -= 64;
1527 data->temp_max[i] -= 64;
1528 data->zone[i].limit -= 64;
1529 data->zone[i].critical -= 64;
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
1533 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001534 data->autofan[0].min_off = (i & 0x20) != 0;
1535 data->autofan[1].min_off = (i & 0x40) != 0;
1536 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001539 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001540 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001543 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001546 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 data->valid = 1;
1549
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001550 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 return data;
1553}
1554
1555
1556static int __init sm_lm85_init(void)
1557{
1558 return i2c_add_driver(&lm85_driver);
1559}
1560
Jean Delvare1f448092008-04-29 14:03:37 +02001561static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 i2c_del_driver(&lm85_driver);
1564}
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001567MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1568 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001569 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1571
1572module_init(sm_lm85_init);
1573module_exit(sm_lm85_exit);