blob: cfc1ee90f5a39d1934c686a5100a185457c2b8a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
Jean Delvare1f448092008-04-29 14:03:37 +02004 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
Jean Delvare8a0795d2008-10-17 17:51:14 +02008 Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040032#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020033#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020034#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040035#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050039static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Insmod parameters */
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
75#define LM85_VERSTEP_LM85C 0x60
76#define LM85_VERSTEP_LM85B 0x62
77#define LM85_VERSTEP_ADM1027 0x60
78#define LM85_VERSTEP_ADT7463 0x62
79#define LM85_VERSTEP_ADT7463C 0x6A
Darrick J. Wong79b92f22008-11-12 13:26:59 -080080#define LM85_VERSTEP_ADT7468_1 0x71
81#define LM85_VERSTEP_ADT7468_2 0x72
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define LM85_VERSTEP_EMC6D100_A0 0x60
83#define LM85_VERSTEP_EMC6D100_A1 0x61
84#define LM85_VERSTEP_EMC6D102 0x65
85
86#define LM85_REG_CONFIG 0x40
87
88#define LM85_REG_ALARM1 0x41
89#define LM85_REG_ALARM2 0x42
90
91#define LM85_REG_VID 0x43
92
93/* Automated FAN control */
94#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
95#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
96#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
98#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
99#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
100#define LM85_REG_AFAN_HYST1 0x6d
101#define LM85_REG_AFAN_HYST2 0x6e
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#define ADM1027_REG_EXTEND_ADC1 0x76
104#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#define EMC6D100_REG_ALARM3 0x7d
107/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200108#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
109#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
110#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define EMC6D102_REG_EXTEND_ADC1 0x85
112#define EMC6D102_REG_EXTEND_ADC2 0x86
113#define EMC6D102_REG_EXTEND_ADC3 0x87
114#define EMC6D102_REG_EXTEND_ADC4 0x88
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Jean Delvare1f448092008-04-29 14:03:37 +0200117/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 variants. Note that you should be a bit careful with which arguments
119 these macros are called: arguments may be evaluated more than once.
120 */
121
122/* IN are scaled acording to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400123static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200124 2500, 2250, 3300, 5000, 12000,
125 3300, 1500, 1800 /*EMC6D100*/
126};
127#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jean Delvare1f448092008-04-29 14:03:37 +0200129#define INS_TO_REG(n, val) \
130 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jean Delvare1f448092008-04-29 14:03:37 +0200132#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200133 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jean Delvare1f448092008-04-29 14:03:37 +0200135#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200138static inline u16 FAN_TO_REG(unsigned long val)
139{
140 if (!val)
141 return 0xffff;
142 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
143}
Jean Delvare1f448092008-04-29 14:03:37 +0200144#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
145 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* Temperature is reported in .001 degC increments */
148#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200149 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
150#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200151 SCALE(((val) << 4) + (ext), 16, 1000)
152#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Jean Delvare1f448092008-04-29 14:03:37 +0200154#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define PWM_FROM_REG(val) (val)
156
157
158/* ZONEs have the following parameters:
159 * Limit (low) temp, 1. degC
160 * Hysteresis (below limit), 1. degC (0-15)
161 * Range of speed control, .1 degC (2-80)
162 * Critical (high) temp, 1. degC
163 *
164 * FAN PWMs have the following parameters:
165 * Reference Zone, 1, 2, 3, etc.
166 * Spinup time, .05 sec
167 * PWM value at limit/low temp, 1 count
168 * PWM Frequency, 1. Hz
169 * PWM is Min or OFF below limit, flag
170 * Invert PWM output, flag
171 *
172 * Some chips filter the temp, others the fan.
173 * Filter constant (or disabled) .1 seconds
174 */
175
176/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400177static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200178 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
179 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
180};
181
182static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 int i;
185
Jean Delvared38b1492008-04-03 10:40:39 +0200186 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200187 for (i = 0; i < 15; ++i) {
188 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Jean Delvared38b1492008-04-03 10:40:39 +0200191
Jean Delvare1b92ada2008-10-17 17:51:14 +0200192 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Jean Delvare1f448092008-04-29 14:03:37 +0200194#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200197static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200198 10, 15, 23, 30, 38, 47, 61, 94
199};
200static const int adm1027_freq_map[8] = { /* 1 Hz */
201 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200202};
203
Jean Delvare8a0795d2008-10-17 17:51:14 +0200204static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int i;
207
Jean Delvare86010c92008-10-17 17:51:13 +0200208 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200209 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200210 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200211 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400212 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200214
215static int FREQ_FROM_REG(const int *map, u8 reg)
216{
217 return map[reg & 0x07];
218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220/* Since we can't use strings, I'm abusing these numbers
221 * to stand in for the following meanings:
222 * 1 -- PWM responds to Zone 1
223 * 2 -- PWM responds to Zone 2
224 * 3 -- PWM responds to Zone 3
225 * 23 -- PWM responds to the higher temp of Zone 2 or 3
226 * 123 -- PWM responds to highest of Zone 1, 2, or 3
227 * 0 -- PWM is always at 0% (ie, off)
228 * -1 -- PWM is always at 100%
229 * -2 -- PWM responds to manual control
230 */
231
Jean Delvaree89e22b2008-06-25 08:47:35 -0400232static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
233#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jean Delvare1f448092008-04-29 14:03:37 +0200235static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
238
Jean Delvare1f448092008-04-29 14:03:37 +0200239 for (i = 0; i <= 7; ++i)
240 if (zone == lm85_zone_map[i])
241 break;
242 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400244 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Jean Delvare1f448092008-04-29 14:03:37 +0200247#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
248#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/* Chip sampling rates
251 *
252 * Some sensors are not updated more frequently than once per second
253 * so it doesn't make sense to read them more often than that.
254 * We cache the results and return the saved data if the driver
255 * is called again before a second has elapsed.
256 *
257 * Also, there is significant configuration data for this chip
258 * given the automatic PWM fan control that is possible. There
259 * are about 47 bytes of config data to only 22 bytes of actual
260 * readings. So, we keep the config data up to date in the cache
261 * when it is written and only sample it once every 1 *minute*
262 */
263#define LM85_DATA_INTERVAL (HZ + HZ / 2)
264#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/* LM85 can automatically adjust fan speeds based on temperature
267 * This structure encapsulates an entire Zone config. There are
268 * three zones (one for each temperature input) on the lm85
269 */
270struct lm85_zone {
271 s8 limit; /* Low temp limit */
272 u8 hyst; /* Low limit hysteresis. (0-15) */
273 u8 range; /* Temp range, encoded */
274 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200275 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * to prevent "drift" as other autofan control
277 * values change.
278 */
Jean Delvare1f448092008-04-29 14:03:37 +0200279 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * to prevent "drift" as other autofan control
281 * values change.
282 */
283};
284
285struct lm85_autofan {
286 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 u8 min_pwm; /* Minimum PWM value, encoded */
288 u8 min_off; /* Min PWM or OFF below "limit", flag */
289};
290
Jean Delvareed6bafb2007-02-14 21:15:03 +0100291/* For each registered chip, we need to keep some data in memory.
292 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700294 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200295 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 enum chips type;
297
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100298 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int valid; /* !=0 if following fields are valid */
300 unsigned long last_reading; /* In jiffies */
301 unsigned long last_config; /* In jiffies */
302
303 u8 in[8]; /* Register value */
304 u8 in_max[8]; /* Register value */
305 u8 in_min[8]; /* Register value */
306 s8 temp[3]; /* Register value */
307 s8 temp_min[3]; /* Register value */
308 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u16 fan[4]; /* Register value */
310 u16 fan_min[4]; /* Register value */
311 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200312 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 u8 temp_ext[3]; /* Decoded values */
314 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 u8 vid; /* Register value */
316 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800318 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct lm85_autofan autofan[3];
320 struct lm85_zone zone[3];
321};
322
Jean Delvare67712d02008-10-17 17:51:14 +0200323static int lm85_detect(struct i2c_client *client, int kind,
324 struct i2c_board_info *info);
325static int lm85_probe(struct i2c_client *client,
326 const struct i2c_device_id *id);
327static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100329static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400330static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333
Jean Delvare67712d02008-10-17 17:51:14 +0200334static const struct i2c_device_id lm85_id[] = {
335 { "adm1027", adm1027 },
336 { "adt7463", adt7463 },
337 { "lm85", any_chip },
338 { "lm85b", lm85b },
339 { "lm85c", lm85c },
340 { "emc6d100", emc6d100 },
341 { "emc6d101", emc6d100 },
342 { "emc6d102", emc6d102 },
343 { }
344};
345MODULE_DEVICE_TABLE(i2c, lm85_id);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200348 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100349 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100350 .name = "lm85",
351 },
Jean Delvare67712d02008-10-17 17:51:14 +0200352 .probe = lm85_probe,
353 .remove = lm85_remove,
354 .id_table = lm85_id,
355 .detect = lm85_detect,
356 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359
360/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200361static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
362 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jean Delvareb353a482007-07-05 20:36:12 +0200364 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200366 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
Jean Delvareb353a482007-07-05 20:36:12 +0200368
369static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
370 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Jean Delvareb353a482007-07-05 20:36:12 +0200372 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200374 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Jean Delvareb353a482007-07-05 20:36:12 +0200376
377static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
378 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Jean Delvareb353a482007-07-05 20:36:12 +0200380 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct i2c_client *client = to_i2c_client(dev);
382 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200383 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100385 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 data->fan_min[nr] = FAN_TO_REG(val);
387 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100388 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return count;
390}
391
392#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200393static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
394 show_fan, NULL, offset - 1); \
395static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
396 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398show_fan_offset(1);
399show_fan_offset(2);
400show_fan_offset(3);
401show_fan_offset(4);
402
403/* vid, vrm, alarms */
404
Jean Delvare1f448092008-04-29 14:03:37 +0200405static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
406 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100409 int vid;
410
411 if (data->type == adt7463 && (data->vid & 0x80)) {
412 /* 6-pin VID (VRM 10) */
413 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
414 } else {
415 /* 5-pin VID (VRM 9) */
416 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
417 }
418
419 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
423
Jean Delvare1f448092008-04-29 14:03:37 +0200424static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
425 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jean Delvare90d66192007-10-08 18:24:35 +0200427 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return sprintf(buf, "%ld\n", (long) data->vrm);
429}
430
Jean Delvare1f448092008-04-29 14:03:37 +0200431static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
432 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100434 struct lm85_data *data = dev_get_drvdata(dev);
435 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return count;
437}
438
439static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
440
Jean Delvare1f448092008-04-29 14:03:37 +0200441static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
442 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200445 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
449
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200450static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
451 char *buf)
452{
453 int nr = to_sensor_dev_attr(attr)->index;
454 struct lm85_data *data = lm85_update_device(dev);
455 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
456}
457
458static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
459static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
460static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
461static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
462static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
463static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
464static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
465static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
466static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
467static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
468static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
469static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
470static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
471static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
472static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
473static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
474static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/* pwm */
477
Jean Delvareb353a482007-07-05 20:36:12 +0200478static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
479 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jean Delvareb353a482007-07-05 20:36:12 +0200481 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200483 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Jean Delvareb353a482007-07-05 20:36:12 +0200485
486static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
487 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jean Delvareb353a482007-07-05 20:36:12 +0200489 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct i2c_client *client = to_i2c_client(dev);
491 struct lm85_data *data = i2c_get_clientdata(client);
492 long val = simple_strtol(buf, NULL, 10);
493
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100494 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 data->pwm[nr] = PWM_TO_REG(val);
496 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100497 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return count;
499}
Jean Delvareb353a482007-07-05 20:36:12 +0200500
501static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
502 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jean Delvareb353a482007-07-05 20:36:12 +0200504 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100506 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100509 switch (pwm_zone) {
510 case -1: /* PWM is always at 100% */
511 enable = 0;
512 break;
513 case 0: /* PWM is always at 0% */
514 case -2: /* PWM responds to manual control */
515 enable = 1;
516 break;
517 default: /* PWM in automatic mode */
518 enable = 2;
519 }
520 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Jean Delvare455f7912007-12-03 23:28:42 +0100523static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
524 *attr, const char *buf, size_t count)
525{
526 int nr = to_sensor_dev_attr(attr)->index;
527 struct i2c_client *client = to_i2c_client(dev);
528 struct lm85_data *data = i2c_get_clientdata(client);
529 long val = simple_strtol(buf, NULL, 10);
530 u8 config;
531
532 switch (val) {
533 case 0:
534 config = 3;
535 break;
536 case 1:
537 config = 7;
538 break;
539 case 2:
540 /* Here we have to choose arbitrarily one of the 5 possible
541 configurations; I go for the safest */
542 config = 6;
543 break;
544 default:
545 return -EINVAL;
546 }
547
548 mutex_lock(&data->update_lock);
549 data->autofan[nr].config = lm85_read_value(client,
550 LM85_REG_AFAN_CONFIG(nr));
551 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
552 | (config << 5);
553 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
554 data->autofan[nr].config);
555 mutex_unlock(&data->update_lock);
556 return count;
557}
558
Jean Delvare34e7dc62008-10-17 17:51:13 +0200559static ssize_t show_pwm_freq(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
562 int nr = to_sensor_dev_attr(attr)->index;
563 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200564 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->freq_map,
565 data->pwm_freq[nr]));
Jean Delvare34e7dc62008-10-17 17:51:13 +0200566}
567
568static ssize_t set_pwm_freq(struct device *dev,
569 struct device_attribute *attr, const char *buf, size_t count)
570{
571 int nr = to_sensor_dev_attr(attr)->index;
572 struct i2c_client *client = to_i2c_client(dev);
573 struct lm85_data *data = i2c_get_clientdata(client);
574 long val = simple_strtol(buf, NULL, 10);
575
576 mutex_lock(&data->update_lock);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200577 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200578 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
579 (data->zone[nr].range << 4)
580 | data->pwm_freq[nr]);
581 mutex_unlock(&data->update_lock);
582 return count;
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200586static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
587 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100588static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200589 show_pwm_enable, set_pwm_enable, offset - 1); \
590static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
591 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593show_pwm_reg(1);
594show_pwm_reg(2);
595show_pwm_reg(3);
596
597/* Voltages */
598
Jean Delvareb353a482007-07-05 20:36:12 +0200599static ssize_t show_in(struct device *dev, struct device_attribute *attr,
600 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Jean Delvareb353a482007-07-05 20:36:12 +0200602 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200604 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
605 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
Jean Delvareb353a482007-07-05 20:36:12 +0200607
Jean Delvare1f448092008-04-29 14:03:37 +0200608static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200609 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Jean Delvareb353a482007-07-05 20:36:12 +0200611 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200613 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Jean Delvareb353a482007-07-05 20:36:12 +0200615
616static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Jean Delvareb353a482007-07-05 20:36:12 +0200619 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct i2c_client *client = to_i2c_client(dev);
621 struct lm85_data *data = i2c_get_clientdata(client);
622 long val = simple_strtol(buf, NULL, 10);
623
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100624 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 data->in_min[nr] = INS_TO_REG(nr, val);
626 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100627 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return count;
629}
Jean Delvareb353a482007-07-05 20:36:12 +0200630
631static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
632 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jean Delvareb353a482007-07-05 20:36:12 +0200634 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200636 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Jean Delvareb353a482007-07-05 20:36:12 +0200638
639static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
640 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Jean Delvareb353a482007-07-05 20:36:12 +0200642 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct i2c_client *client = to_i2c_client(dev);
644 struct lm85_data *data = i2c_get_clientdata(client);
645 long val = simple_strtol(buf, NULL, 10);
646
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 data->in_max[nr] = INS_TO_REG(nr, val);
649 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return count;
652}
Jean Delvareb353a482007-07-05 20:36:12 +0200653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200655static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
656 show_in, NULL, offset); \
657static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
658 show_in_min, set_in_min, offset); \
659static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
660 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662show_in_reg(0);
663show_in_reg(1);
664show_in_reg(2);
665show_in_reg(3);
666show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200667show_in_reg(5);
668show_in_reg(6);
669show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/* Temps */
672
Jean Delvareb353a482007-07-05 20:36:12 +0200673static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
674 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jean Delvareb353a482007-07-05 20:36:12 +0200676 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200678 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
679 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Jean Delvareb353a482007-07-05 20:36:12 +0200681
682static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
683 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Jean Delvareb353a482007-07-05 20:36:12 +0200685 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200687 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Jean Delvareb353a482007-07-05 20:36:12 +0200689
690static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
691 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jean Delvareb353a482007-07-05 20:36:12 +0200693 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct i2c_client *client = to_i2c_client(dev);
695 struct lm85_data *data = i2c_get_clientdata(client);
696 long val = simple_strtol(buf, NULL, 10);
697
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800698 if (IS_ADT7468_OFF64(data))
699 val += 64;
700
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100701 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 data->temp_min[nr] = TEMP_TO_REG(val);
703 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100704 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return count;
706}
Jean Delvareb353a482007-07-05 20:36:12 +0200707
708static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
709 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jean Delvareb353a482007-07-05 20:36:12 +0200711 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200713 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Jean Delvareb353a482007-07-05 20:36:12 +0200715
716static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
717 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Jean Delvareb353a482007-07-05 20:36:12 +0200719 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct i2c_client *client = to_i2c_client(dev);
721 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200722 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800724 if (IS_ADT7468_OFF64(data))
725 val += 64;
726
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100727 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 data->temp_max[nr] = TEMP_TO_REG(val);
729 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return count;
732}
Jean Delvareb353a482007-07-05 20:36:12 +0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200735static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
736 show_temp, NULL, offset - 1); \
737static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
738 show_temp_min, set_temp_min, offset - 1); \
739static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
740 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742show_temp_reg(1);
743show_temp_reg(2);
744show_temp_reg(3);
745
746
747/* Automatic PWM control */
748
Jean Delvareb353a482007-07-05 20:36:12 +0200749static ssize_t show_pwm_auto_channels(struct device *dev,
750 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Jean Delvareb353a482007-07-05 20:36:12 +0200752 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200754 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
Jean Delvareb353a482007-07-05 20:36:12 +0200756
757static ssize_t set_pwm_auto_channels(struct device *dev,
758 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jean Delvareb353a482007-07-05 20:36:12 +0200760 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct i2c_client *client = to_i2c_client(dev);
762 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200763 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100765 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200767 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
769 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100770 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return count;
772}
Jean Delvareb353a482007-07-05 20:36:12 +0200773
774static ssize_t show_pwm_auto_pwm_min(struct device *dev,
775 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Jean Delvareb353a482007-07-05 20:36:12 +0200777 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200779 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
Jean Delvareb353a482007-07-05 20:36:12 +0200781
782static ssize_t set_pwm_auto_pwm_min(struct device *dev,
783 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jean Delvareb353a482007-07-05 20:36:12 +0200785 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct i2c_client *client = to_i2c_client(dev);
787 struct lm85_data *data = i2c_get_clientdata(client);
788 long val = simple_strtol(buf, NULL, 10);
789
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100790 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 data->autofan[nr].min_pwm = PWM_TO_REG(val);
792 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
793 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100794 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return count;
796}
Jean Delvareb353a482007-07-05 20:36:12 +0200797
798static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
799 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Jean Delvareb353a482007-07-05 20:36:12 +0200801 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200803 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
Jean Delvareb353a482007-07-05 20:36:12 +0200805
806static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
807 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Jean Delvareb353a482007-07-05 20:36:12 +0200809 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct i2c_client *client = to_i2c_client(dev);
811 struct lm85_data *data = i2c_get_clientdata(client);
812 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200813 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100815 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200817 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
818 tmp &= ~(0x20 << nr);
819 if (data->autofan[nr].min_off)
820 tmp |= 0x20 << nr;
821 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return count;
824}
Jean Delvareb353a482007-07-05 20:36:12 +0200825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200827static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
828 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
829 set_pwm_auto_channels, offset - 1); \
830static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
831 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
832 set_pwm_auto_pwm_min, offset - 1); \
833static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
834 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200835 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837pwm_auto(1);
838pwm_auto(2);
839pwm_auto(3);
840
841/* Temperature settings for automatic PWM control */
842
Jean Delvareb353a482007-07-05 20:36:12 +0200843static ssize_t show_temp_auto_temp_off(struct device *dev,
844 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Jean Delvareb353a482007-07-05 20:36:12 +0200846 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200848 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 HYST_FROM_REG(data->zone[nr].hyst));
850}
Jean Delvareb353a482007-07-05 20:36:12 +0200851
852static ssize_t set_temp_auto_temp_off(struct device *dev,
853 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Jean Delvareb353a482007-07-05 20:36:12 +0200855 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct i2c_client *client = to_i2c_client(dev);
857 struct lm85_data *data = i2c_get_clientdata(client);
858 int min;
859 long val = simple_strtol(buf, NULL, 10);
860
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100861 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 min = TEMP_FROM_REG(data->zone[nr].limit);
863 data->zone[nr].off_desired = TEMP_TO_REG(val);
864 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200865 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 lm85_write_value(client, LM85_REG_AFAN_HYST1,
867 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200868 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 } else {
870 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200871 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100873 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return count;
875}
Jean Delvareb353a482007-07-05 20:36:12 +0200876
877static ssize_t show_temp_auto_temp_min(struct device *dev,
878 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Jean Delvareb353a482007-07-05 20:36:12 +0200880 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200882 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Jean Delvareb353a482007-07-05 20:36:12 +0200884
885static ssize_t set_temp_auto_temp_min(struct device *dev,
886 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Jean Delvareb353a482007-07-05 20:36:12 +0200888 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct i2c_client *client = to_i2c_client(dev);
890 struct lm85_data *data = i2c_get_clientdata(client);
891 long val = simple_strtol(buf, NULL, 10);
892
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100893 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 data->zone[nr].limit = TEMP_TO_REG(val);
895 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
896 data->zone[nr].limit);
897
898/* Update temp_auto_max and temp_auto_range */
899 data->zone[nr].range = RANGE_TO_REG(
900 TEMP_FROM_REG(data->zone[nr].max_desired) -
901 TEMP_FROM_REG(data->zone[nr].limit));
902 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
903 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200904 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906/* Update temp_auto_hyst and temp_auto_off */
907 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
908 data->zone[nr].limit) - TEMP_FROM_REG(
909 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200910 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 lm85_write_value(client, LM85_REG_AFAN_HYST1,
912 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200913 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 } else {
915 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200916 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100918 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return count;
920}
Jean Delvareb353a482007-07-05 20:36:12 +0200921
922static ssize_t show_temp_auto_temp_max(struct device *dev,
923 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jean Delvareb353a482007-07-05 20:36:12 +0200925 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200927 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 RANGE_FROM_REG(data->zone[nr].range));
929}
Jean Delvareb353a482007-07-05 20:36:12 +0200930
931static ssize_t set_temp_auto_temp_max(struct device *dev,
932 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jean Delvareb353a482007-07-05 20:36:12 +0200934 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct i2c_client *client = to_i2c_client(dev);
936 struct lm85_data *data = i2c_get_clientdata(client);
937 int min;
938 long val = simple_strtol(buf, NULL, 10);
939
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100940 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 min = TEMP_FROM_REG(data->zone[nr].limit);
942 data->zone[nr].max_desired = TEMP_TO_REG(val);
943 data->zone[nr].range = RANGE_TO_REG(
944 val - min);
945 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
946 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200947 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100948 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return count;
950}
Jean Delvareb353a482007-07-05 20:36:12 +0200951
952static ssize_t show_temp_auto_temp_crit(struct device *dev,
953 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Jean Delvareb353a482007-07-05 20:36:12 +0200955 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200957 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
Jean Delvareb353a482007-07-05 20:36:12 +0200959
960static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200961 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jean Delvareb353a482007-07-05 20:36:12 +0200963 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct i2c_client *client = to_i2c_client(dev);
965 struct lm85_data *data = i2c_get_clientdata(client);
966 long val = simple_strtol(buf, NULL, 10);
967
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100968 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 data->zone[nr].critical = TEMP_TO_REG(val);
970 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
971 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100972 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return count;
974}
Jean Delvareb353a482007-07-05 20:36:12 +0200975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200977static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
978 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
979 set_temp_auto_temp_off, offset - 1); \
980static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
981 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
982 set_temp_auto_temp_min, offset - 1); \
983static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
984 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
985 set_temp_auto_temp_max, offset - 1); \
986static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
987 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
988 set_temp_auto_temp_crit, offset - 1);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990temp_auto(1);
991temp_auto(2);
992temp_auto(3);
993
Mark M. Hoffman0501a3812006-09-24 21:14:35 +0200994static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200995 &sensor_dev_attr_fan1_input.dev_attr.attr,
996 &sensor_dev_attr_fan2_input.dev_attr.attr,
997 &sensor_dev_attr_fan3_input.dev_attr.attr,
998 &sensor_dev_attr_fan4_input.dev_attr.attr,
999 &sensor_dev_attr_fan1_min.dev_attr.attr,
1000 &sensor_dev_attr_fan2_min.dev_attr.attr,
1001 &sensor_dev_attr_fan3_min.dev_attr.attr,
1002 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001003 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1004 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1005 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1006 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001007
1008 &sensor_dev_attr_pwm1.dev_attr.attr,
1009 &sensor_dev_attr_pwm2.dev_attr.attr,
1010 &sensor_dev_attr_pwm3.dev_attr.attr,
1011 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1012 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1013 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001014 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1015 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1016 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001017
1018 &sensor_dev_attr_in0_input.dev_attr.attr,
1019 &sensor_dev_attr_in1_input.dev_attr.attr,
1020 &sensor_dev_attr_in2_input.dev_attr.attr,
1021 &sensor_dev_attr_in3_input.dev_attr.attr,
1022 &sensor_dev_attr_in0_min.dev_attr.attr,
1023 &sensor_dev_attr_in1_min.dev_attr.attr,
1024 &sensor_dev_attr_in2_min.dev_attr.attr,
1025 &sensor_dev_attr_in3_min.dev_attr.attr,
1026 &sensor_dev_attr_in0_max.dev_attr.attr,
1027 &sensor_dev_attr_in1_max.dev_attr.attr,
1028 &sensor_dev_attr_in2_max.dev_attr.attr,
1029 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001030 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1031 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1032 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1033 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001034
1035 &sensor_dev_attr_temp1_input.dev_attr.attr,
1036 &sensor_dev_attr_temp2_input.dev_attr.attr,
1037 &sensor_dev_attr_temp3_input.dev_attr.attr,
1038 &sensor_dev_attr_temp1_min.dev_attr.attr,
1039 &sensor_dev_attr_temp2_min.dev_attr.attr,
1040 &sensor_dev_attr_temp3_min.dev_attr.attr,
1041 &sensor_dev_attr_temp1_max.dev_attr.attr,
1042 &sensor_dev_attr_temp2_max.dev_attr.attr,
1043 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001044 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1045 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1046 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1047 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1048 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001049
1050 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1051 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1052 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1053 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1054 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1055 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1056 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1057 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1058 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001059
1060 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1061 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1062 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1063 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1064 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1065 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1066 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1067 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1068 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1069 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1070 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1071 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1072
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001073 &dev_attr_vrm.attr,
1074 &dev_attr_cpu0_vid.attr,
1075 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001076 NULL
1077};
1078
1079static const struct attribute_group lm85_group = {
1080 .attrs = lm85_attributes,
1081};
1082
Jean Delvare6b9aad22007-07-05 20:37:21 +02001083static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001084 &sensor_dev_attr_in4_input.dev_attr.attr,
1085 &sensor_dev_attr_in4_min.dev_attr.attr,
1086 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001087 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001088 NULL
1089};
1090
Jean Delvare6b9aad22007-07-05 20:37:21 +02001091static const struct attribute_group lm85_group_in4 = {
1092 .attrs = lm85_attributes_in4,
1093};
1094
1095static struct attribute *lm85_attributes_in567[] = {
1096 &sensor_dev_attr_in5_input.dev_attr.attr,
1097 &sensor_dev_attr_in6_input.dev_attr.attr,
1098 &sensor_dev_attr_in7_input.dev_attr.attr,
1099 &sensor_dev_attr_in5_min.dev_attr.attr,
1100 &sensor_dev_attr_in6_min.dev_attr.attr,
1101 &sensor_dev_attr_in7_min.dev_attr.attr,
1102 &sensor_dev_attr_in5_max.dev_attr.attr,
1103 &sensor_dev_attr_in6_max.dev_attr.attr,
1104 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001105 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1106 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1107 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001108 NULL
1109};
1110
1111static const struct attribute_group lm85_group_in567 = {
1112 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001113};
1114
Jean Delvare5f44759472008-06-25 09:10:30 -04001115static void lm85_init_client(struct i2c_client *client)
1116{
1117 int value;
1118
1119 /* Start monitoring if needed */
1120 value = lm85_read_value(client, LM85_REG_CONFIG);
1121 if (!(value & 0x01)) {
1122 dev_info(&client->dev, "Starting monitoring\n");
1123 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1124 }
1125
1126 /* Warn about unusual configuration bits */
1127 if (value & 0x02)
1128 dev_warn(&client->dev, "Device configuration is locked\n");
1129 if (!(value & 0x04))
1130 dev_warn(&client->dev, "Device is not ready\n");
1131}
1132
Jean Delvare67712d02008-10-17 17:51:14 +02001133/* Return 0 if detection is successful, -ENODEV otherwise */
1134static int lm85_detect(struct i2c_client *client, int kind,
1135 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Jean Delvare67712d02008-10-17 17:51:14 +02001137 struct i2c_adapter *adapter = client->adapter;
1138 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001139 const char *type_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Jean Delvaree89e22b2008-06-25 08:47:35 -04001141 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001143 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001146 /* If auto-detecting, determine the chip type */
1147 if (kind < 0) {
1148 int company = lm85_read_value(client, LM85_REG_COMPANY);
1149 int verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001151 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1152 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1153 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001155 /* All supported chips have the version in common */
1156 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC) {
1157 dev_dbg(&adapter->dev, "Autodetection failed: "
1158 "unsupported version\n");
Jean Delvare67712d02008-10-17 17:51:14 +02001159 return -ENODEV;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001160 }
1161 kind = any_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001163 /* Now, refine the detection */
1164 if (company == LM85_COMPANY_NATIONAL) {
1165 switch (verstep) {
1166 case LM85_VERSTEP_LM85C:
1167 kind = lm85c;
1168 break;
1169 case LM85_VERSTEP_LM85B:
1170 kind = lm85b;
1171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001173 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1174 switch (verstep) {
1175 case LM85_VERSTEP_ADM1027:
1176 kind = adm1027;
1177 break;
1178 case LM85_VERSTEP_ADT7463:
1179 case LM85_VERSTEP_ADT7463C:
1180 kind = adt7463;
1181 break;
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001182 case LM85_VERSTEP_ADT7468_1:
1183 case LM85_VERSTEP_ADT7468_2:
1184 kind = adt7468;
1185 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001186 }
1187 } else if (company == LM85_COMPANY_SMSC) {
1188 switch (verstep) {
1189 case LM85_VERSTEP_EMC6D100_A0:
1190 case LM85_VERSTEP_EMC6D100_A1:
1191 /* Note: we can't tell a '100 from a '101 */
1192 kind = emc6d100;
1193 break;
1194 case LM85_VERSTEP_EMC6D102:
1195 kind = emc6d102;
1196 break;
1197 }
1198 } else {
1199 dev_dbg(&adapter->dev, "Autodetection failed: "
1200 "unknown vendor\n");
Jean Delvare67712d02008-10-17 17:51:14 +02001201 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 }
1204
Jean Delvaree89e22b2008-06-25 08:47:35 -04001205 switch (kind) {
1206 case lm85b:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 type_name = "lm85b";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001208 break;
1209 case lm85c:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 type_name = "lm85c";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001211 break;
1212 case adm1027:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 type_name = "adm1027";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001214 break;
1215 case adt7463:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 type_name = "adt7463";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001217 break;
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001218 case adt7468:
1219 type_name = "adt7468";
1220 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001221 case emc6d100:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 type_name = "emc6d100";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001223 break;
1224 case emc6d102:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 type_name = "emc6d102";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001226 break;
1227 default:
1228 type_name = "lm85";
1229 }
Jean Delvare67712d02008-10-17 17:51:14 +02001230 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jean Delvare67712d02008-10-17 17:51:14 +02001232 return 0;
1233}
1234
1235static int lm85_probe(struct i2c_client *client,
1236 const struct i2c_device_id *id)
1237{
1238 struct lm85_data *data;
1239 int err;
1240
1241 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1242 if (!data)
1243 return -ENOMEM;
1244
1245 i2c_set_clientdata(client, data);
1246 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001247 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Jean Delvare67712d02008-10-17 17:51:14 +02001249 /* Fill in the chip specific driver values */
1250 switch (data->type) {
1251 case adm1027:
1252 case adt7463:
1253 case emc6d100:
1254 case emc6d102:
1255 data->freq_map = adm1027_freq_map;
1256 break;
1257 default:
1258 data->freq_map = lm85_freq_map;
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001262 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001265 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001268 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1269 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001270 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001272 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001273 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001274 data->vid = lm85_read_value(client, LM85_REG_VID);
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001275 if (!((data->type == adt7463 || data->type == adt7468) &&
1276 (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001277 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001278 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001279 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001280
1281 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001282 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001283 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001284 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001285 goto err_remove_files;
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001286
Jean Delvaree89e22b2008-06-25 08:47:35 -04001287 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001288 if (IS_ERR(data->hwmon_dev)) {
1289 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001290 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001291 }
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return 0;
1294
1295 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001296 err_remove_files:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001297 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1298 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare67712d02008-10-17 17:51:14 +02001299 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001300 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvaref9080372008-10-17 17:51:14 +02001301 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return err;
1304}
1305
Jean Delvare67712d02008-10-17 17:51:14 +02001306static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001308 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001309 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001310 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001311 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1312 if (data->type == emc6d100)
1313 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001314 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
1317
1318
Ben Dooksd8d20612005-10-26 21:05:46 +02001319static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 int res;
1322
1323 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001324 switch (reg) {
1325 case LM85_REG_FAN(0): /* Read WORD data */
1326 case LM85_REG_FAN(1):
1327 case LM85_REG_FAN(2):
1328 case LM85_REG_FAN(3):
1329 case LM85_REG_FAN_MIN(0):
1330 case LM85_REG_FAN_MIN(1):
1331 case LM85_REG_FAN_MIN(2):
1332 case LM85_REG_FAN_MIN(3):
1333 case LM85_REG_ALARM1: /* Read both bytes at once */
1334 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1335 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 default: /* Read BYTE data */
1338 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
Jean Delvare1f448092008-04-29 14:03:37 +02001342 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Jean Delvaree89e22b2008-06-25 08:47:35 -04001345static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Jean Delvare1f448092008-04-29 14:03:37 +02001347 switch (reg) {
1348 case LM85_REG_FAN(0): /* Write WORD data */
1349 case LM85_REG_FAN(1):
1350 case LM85_REG_FAN(2):
1351 case LM85_REG_FAN(3):
1352 case LM85_REG_FAN_MIN(0):
1353 case LM85_REG_FAN_MIN(1):
1354 case LM85_REG_FAN_MIN(2):
1355 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001357 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1358 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001361 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366static struct lm85_data *lm85_update_device(struct device *dev)
1367{
1368 struct i2c_client *client = to_i2c_client(dev);
1369 struct lm85_data *data = i2c_get_clientdata(client);
1370 int i;
1371
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001372 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Jean Delvare1f448092008-04-29 14:03:37 +02001374 if (!data->valid ||
1375 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* Things that change quickly */
1377 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* Have to read extended bits first to "freeze" the
1380 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001381 * There are 2 additional resolution bits per channel and we
1382 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001384 if (data->type == adm1027 || data->type == adt7463 ||
1385 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int ext1 = lm85_read_value(client,
1387 ADM1027_REG_EXTEND_ADC1);
1388 int ext2 = lm85_read_value(client,
1389 ADM1027_REG_EXTEND_ADC2);
1390 int val = (ext1 << 8) + ext2;
1391
Jean Delvare1f448092008-04-29 14:03:37 +02001392 for (i = 0; i <= 4; i++)
1393 data->in_ext[i] =
1394 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Jean Delvare1f448092008-04-29 14:03:37 +02001396 for (i = 0; i <= 2; i++)
1397 data->temp_ext[i] =
1398 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Jean Delvare9c516ef2005-11-26 20:07:54 +01001401 data->vid = lm85_read_value(client, LM85_REG_VID);
1402
1403 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 data->in[i] =
1405 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001406 data->fan[i] =
1407 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001410 if (!((data->type == adt7463 || data->type == adt7468) &&
1411 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001412 data->in[4] = lm85_read_value(client,
1413 LM85_REG_IN(4));
1414 }
1415
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001416 if (data->type == adt7468)
1417 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (i = 0; i <= 2; ++i) {
1420 data->temp[i] =
1421 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 data->pwm[i] =
1423 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001424
1425 if (IS_ADT7468_OFF64(data))
1426 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
1429 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1430
Jean Delvaredd1ac532008-05-01 08:47:33 +02001431 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* Three more voltage sensors */
1433 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001434 data->in[i] = lm85_read_value(client,
1435 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001438 data->alarms |= lm85_read_value(client,
1439 EMC6D100_REG_ALARM3) << 16;
1440 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* Have to read LSB bits after the MSB ones because
1442 the reading of the MSB bits has frozen the
1443 LSBs (backward from the ADM1027).
1444 */
1445 int ext1 = lm85_read_value(client,
1446 EMC6D102_REG_EXTEND_ADC1);
1447 int ext2 = lm85_read_value(client,
1448 EMC6D102_REG_EXTEND_ADC2);
1449 int ext3 = lm85_read_value(client,
1450 EMC6D102_REG_EXTEND_ADC3);
1451 int ext4 = lm85_read_value(client,
1452 EMC6D102_REG_EXTEND_ADC4);
1453 data->in_ext[0] = ext3 & 0x0f;
1454 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001455 data->in_ext[2] = ext4 >> 4;
1456 data->in_ext[3] = ext3 >> 4;
1457 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 data->temp_ext[0] = ext1 & 0x0f;
1460 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001461 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
Jean Delvare1f448092008-04-29 14:03:37 +02001464 data->last_reading = jiffies;
1465 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Jean Delvare1f448092008-04-29 14:03:37 +02001467 if (!data->valid ||
1468 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* Things that don't change often */
1470 dev_dbg(&client->dev, "Reading config values\n");
1471
Jean Delvare9c516ef2005-11-26 20:07:54 +01001472 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 data->in_min[i] =
1474 lm85_read_value(client, LM85_REG_IN_MIN(i));
1475 data->in_max[i] =
1476 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001477 data->fan_min[i] =
1478 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001481 if (!((data->type == adt7463 || data->type == adt7468) &&
1482 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001483 data->in_min[4] = lm85_read_value(client,
1484 LM85_REG_IN_MIN(4));
1485 data->in_max[4] = lm85_read_value(client,
1486 LM85_REG_IN_MAX(4));
1487 }
1488
Jean Delvare1f448092008-04-29 14:03:37 +02001489 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001491 data->in_min[i] = lm85_read_value(client,
1492 EMC6D100_REG_IN_MIN(i));
1493 data->in_max[i] = lm85_read_value(client,
1494 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496 }
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001499 int val;
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 data->temp_min[i] =
1502 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1503 data->temp_max[i] =
1504 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 data->autofan[i].config =
1507 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1508 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001509 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001510 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 data->autofan[i].min_pwm =
1512 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1513 data->zone[i].limit =
1514 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1515 data->zone[i].critical =
1516 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001517
1518 if (IS_ADT7468_OFF64(data)) {
1519 data->temp_min[i] -= 64;
1520 data->temp_max[i] -= 64;
1521 data->zone[i].limit -= 64;
1522 data->zone[i].critical -= 64;
1523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
1526 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001527 data->autofan[0].min_off = (i & 0x20) != 0;
1528 data->autofan[1].min_off = (i & 0x40) != 0;
1529 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001532 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001533 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001536 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001539 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 data->valid = 1;
1542
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001543 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 return data;
1546}
1547
1548
1549static int __init sm_lm85_init(void)
1550{
1551 return i2c_add_driver(&lm85_driver);
1552}
1553
Jean Delvare1f448092008-04-29 14:03:37 +02001554static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 i2c_del_driver(&lm85_driver);
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001560MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1561 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001562 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1564
1565module_init(sm_lm85_init);
1566module_exit(sm_lm85_exit);