blob: 1e229847f37ac66da8aabdf246104400c047f7bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
Jean Delvare1f448092008-04-29 14:03:37 +02004 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
6 Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
7 Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
Jean Delvared42a2eb2009-12-09 20:35:53 +01008 Copyright (C) 2007--2009 Jean Delvare <khali@linux-fr.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10 Chip details at <http://www.national.com/ds/LM/LM85.pdf>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/jiffies.h>
31#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040032#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020033#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020034#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040035#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050039static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jean Delvaree5e9f442009-12-14 21:17:27 +010041enum chips {
42 any_chip, lm85b, lm85c,
43 adm1027, adt7463, adt7468,
44 emc6d100, emc6d102
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* The LM85 registers */
48
49#define LM85_REG_IN(nr) (0x20 + (nr))
50#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
51#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
52
53#define LM85_REG_TEMP(nr) (0x25 + (nr))
54#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
55#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
56
57/* Fan speeds are LSB, MSB (2 bytes) */
Jean Delvare1f448092008-04-29 14:03:37 +020058#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
59#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define LM85_REG_PWM(nr) (0x30 + (nr))
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define LM85_REG_COMPANY 0x3e
64#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080065
66#define ADT7468_REG_CFG5 0x7c
Jean Delvaref6c61cf2010-10-28 20:31:50 +020067#define ADT7468_OFF64 (1 << 0)
68#define ADT7468_HFPWM (1 << 1)
Darrick J. Wong79b92f22008-11-12 13:26:59 -080069#define IS_ADT7468_OFF64(data) \
70 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
Jean Delvaref6c61cf2010-10-28 20:31:50 +020071#define IS_ADT7468_HFPWM(data) \
72 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
Darrick J. Wong79b92f22008-11-12 13:26:59 -080073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* These are the recognized values for the above regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define LM85_COMPANY_NATIONAL 0x01
76#define LM85_COMPANY_ANALOG_DEV 0x41
Jean Delvare1f448092008-04-29 14:03:37 +020077#define LM85_COMPANY_SMSC 0x5c
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define LM85_VERSTEP_VMASK 0xf0
79#define LM85_VERSTEP_GENERIC 0x60
Darrick J. Wongc15ade62009-03-10 12:55:47 -070080#define LM85_VERSTEP_GENERIC2 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define LM85_VERSTEP_LM85C 0x60
82#define LM85_VERSTEP_LM85B 0x62
Jean Delvare5cfaf332009-09-15 17:18:14 +020083#define LM85_VERSTEP_LM96000_1 0x68
84#define LM85_VERSTEP_LM96000_2 0x69
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define LM85_VERSTEP_ADM1027 0x60
86#define LM85_VERSTEP_ADT7463 0x62
87#define LM85_VERSTEP_ADT7463C 0x6A
Darrick J. Wong79b92f22008-11-12 13:26:59 -080088#define LM85_VERSTEP_ADT7468_1 0x71
89#define LM85_VERSTEP_ADT7468_2 0x72
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define LM85_VERSTEP_EMC6D100_A0 0x60
91#define LM85_VERSTEP_EMC6D100_A1 0x61
92#define LM85_VERSTEP_EMC6D102 0x65
93
94#define LM85_REG_CONFIG 0x40
95
96#define LM85_REG_ALARM1 0x41
97#define LM85_REG_ALARM2 0x42
98
99#define LM85_REG_VID 0x43
100
101/* Automated FAN control */
102#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
103#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
104#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
106#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
107#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
108#define LM85_REG_AFAN_HYST1 0x6d
109#define LM85_REG_AFAN_HYST2 0x6e
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define ADM1027_REG_EXTEND_ADC1 0x76
112#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#define EMC6D100_REG_ALARM3 0x7d
115/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200116#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
117#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
118#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define EMC6D102_REG_EXTEND_ADC1 0x85
120#define EMC6D102_REG_EXTEND_ADC2 0x86
121#define EMC6D102_REG_EXTEND_ADC3 0x87
122#define EMC6D102_REG_EXTEND_ADC4 0x88
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jean Delvare1f448092008-04-29 14:03:37 +0200125/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 variants. Note that you should be a bit careful with which arguments
127 these macros are called: arguments may be evaluated more than once.
128 */
129
130/* IN are scaled acording to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400131static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200132 2500, 2250, 3300, 5000, 12000,
133 3300, 1500, 1800 /*EMC6D100*/
134};
135#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jean Delvare1f448092008-04-29 14:03:37 +0200137#define INS_TO_REG(n, val) \
138 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jean Delvare1f448092008-04-29 14:03:37 +0200140#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200141 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvare1f448092008-04-29 14:03:37 +0200143#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200146static inline u16 FAN_TO_REG(unsigned long val)
147{
148 if (!val)
149 return 0xffff;
150 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
151}
Jean Delvare1f448092008-04-29 14:03:37 +0200152#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
153 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/* Temperature is reported in .001 degC increments */
156#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200157 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
158#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200159 SCALE(((val) << 4) + (ext), 16, 1000)
160#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jean Delvare1f448092008-04-29 14:03:37 +0200162#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define PWM_FROM_REG(val) (val)
164
165
166/* ZONEs have the following parameters:
167 * Limit (low) temp, 1. degC
168 * Hysteresis (below limit), 1. degC (0-15)
169 * Range of speed control, .1 degC (2-80)
170 * Critical (high) temp, 1. degC
171 *
172 * FAN PWMs have the following parameters:
173 * Reference Zone, 1, 2, 3, etc.
174 * Spinup time, .05 sec
175 * PWM value at limit/low temp, 1 count
176 * PWM Frequency, 1. Hz
177 * PWM is Min or OFF below limit, flag
178 * Invert PWM output, flag
179 *
180 * Some chips filter the temp, others the fan.
181 * Filter constant (or disabled) .1 seconds
182 */
183
184/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400185static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200186 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
187 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
188};
189
190static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int i;
193
Jean Delvared38b1492008-04-03 10:40:39 +0200194 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200195 for (i = 0; i < 15; ++i) {
196 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Jean Delvared38b1492008-04-03 10:40:39 +0200199
Jean Delvare1b92ada2008-10-17 17:51:14 +0200200 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
Jean Delvare1f448092008-04-29 14:03:37 +0200202#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200205static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200206 10, 15, 23, 30, 38, 47, 61, 94
207};
208static const int adm1027_freq_map[8] = { /* 1 Hz */
209 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200210};
211
Jean Delvare8a0795d2008-10-17 17:51:14 +0200212static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int i;
215
Jean Delvare86010c92008-10-17 17:51:13 +0200216 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200217 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200218 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200219 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400220 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200222
223static int FREQ_FROM_REG(const int *map, u8 reg)
224{
225 return map[reg & 0x07];
226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228/* Since we can't use strings, I'm abusing these numbers
229 * to stand in for the following meanings:
230 * 1 -- PWM responds to Zone 1
231 * 2 -- PWM responds to Zone 2
232 * 3 -- PWM responds to Zone 3
233 * 23 -- PWM responds to the higher temp of Zone 2 or 3
234 * 123 -- PWM responds to highest of Zone 1, 2, or 3
235 * 0 -- PWM is always at 0% (ie, off)
236 * -1 -- PWM is always at 100%
237 * -2 -- PWM responds to manual control
238 */
239
Jean Delvaree89e22b2008-06-25 08:47:35 -0400240static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
241#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jean Delvare1f448092008-04-29 14:03:37 +0200243static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int i;
246
Jean Delvare1f448092008-04-29 14:03:37 +0200247 for (i = 0; i <= 7; ++i)
248 if (zone == lm85_zone_map[i])
249 break;
250 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400252 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Jean Delvare1f448092008-04-29 14:03:37 +0200255#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
256#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* Chip sampling rates
259 *
260 * Some sensors are not updated more frequently than once per second
261 * so it doesn't make sense to read them more often than that.
262 * We cache the results and return the saved data if the driver
263 * is called again before a second has elapsed.
264 *
265 * Also, there is significant configuration data for this chip
266 * given the automatic PWM fan control that is possible. There
267 * are about 47 bytes of config data to only 22 bytes of actual
268 * readings. So, we keep the config data up to date in the cache
269 * when it is written and only sample it once every 1 *minute*
270 */
271#define LM85_DATA_INTERVAL (HZ + HZ / 2)
272#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/* LM85 can automatically adjust fan speeds based on temperature
275 * This structure encapsulates an entire Zone config. There are
276 * three zones (one for each temperature input) on the lm85
277 */
278struct lm85_zone {
279 s8 limit; /* Low temp limit */
280 u8 hyst; /* Low limit hysteresis. (0-15) */
281 u8 range; /* Temp range, encoded */
282 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200283 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * to prevent "drift" as other autofan control
285 * values change.
286 */
Jean Delvare1f448092008-04-29 14:03:37 +0200287 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * to prevent "drift" as other autofan control
289 * values change.
290 */
291};
292
293struct lm85_autofan {
294 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 u8 min_pwm; /* Minimum PWM value, encoded */
296 u8 min_off; /* Min PWM or OFF below "limit", flag */
297};
298
Jean Delvareed6bafb2007-02-14 21:15:03 +0100299/* For each registered chip, we need to keep some data in memory.
300 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700302 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200303 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 enum chips type;
305
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100306 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int valid; /* !=0 if following fields are valid */
308 unsigned long last_reading; /* In jiffies */
309 unsigned long last_config; /* In jiffies */
310
311 u8 in[8]; /* Register value */
312 u8 in_max[8]; /* Register value */
313 u8 in_min[8]; /* Register value */
314 s8 temp[3]; /* Register value */
315 s8 temp_min[3]; /* Register value */
316 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 u16 fan[4]; /* Register value */
318 u16 fan_min[4]; /* Register value */
319 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200320 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 u8 temp_ext[3]; /* Decoded values */
322 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 u8 vid; /* Register value */
324 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800326 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct lm85_autofan autofan[3];
328 struct lm85_zone zone[3];
329};
330
Jean Delvare310ec792009-12-14 21:17:23 +0100331static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200332static int lm85_probe(struct i2c_client *client,
333 const struct i2c_device_id *id);
334static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100336static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400337static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340
Jean Delvare67712d02008-10-17 17:51:14 +0200341static const struct i2c_device_id lm85_id[] = {
342 { "adm1027", adm1027 },
343 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700344 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200345 { "lm85", any_chip },
346 { "lm85b", lm85b },
347 { "lm85c", lm85c },
348 { "emc6d100", emc6d100 },
349 { "emc6d101", emc6d100 },
350 { "emc6d102", emc6d102 },
351 { }
352};
353MODULE_DEVICE_TABLE(i2c, lm85_id);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200356 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100357 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100358 .name = "lm85",
359 },
Jean Delvare67712d02008-10-17 17:51:14 +0200360 .probe = lm85_probe,
361 .remove = lm85_remove,
362 .id_table = lm85_id,
363 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100364 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365};
366
367
368/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200369static ssize_t show_fan(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[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Jean Delvareb353a482007-07-05 20:36:12 +0200376
377static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
378 char *buf)
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 lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200382 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Jean Delvareb353a482007-07-05 20:36:12 +0200384
385static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jean Delvareb353a482007-07-05 20:36:12 +0200388 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct i2c_client *client = to_i2c_client(dev);
390 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200391 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100393 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 data->fan_min[nr] = FAN_TO_REG(val);
395 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return count;
398}
399
400#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200401static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
402 show_fan, NULL, offset - 1); \
403static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
404 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406show_fan_offset(1);
407show_fan_offset(2);
408show_fan_offset(3);
409show_fan_offset(4);
410
411/* vid, vrm, alarms */
412
Jean Delvare1f448092008-04-29 14:03:37 +0200413static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
414 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100417 int vid;
418
Darrick J. Wong8ef1f022009-03-10 12:55:48 -0700419 if ((data->type == adt7463 || data->type == adt7468) &&
420 (data->vid & 0x80)) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100421 /* 6-pin VID (VRM 10) */
422 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
423 } else {
424 /* 5-pin VID (VRM 9) */
425 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
426 }
427
428 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
432
Jean Delvare1f448092008-04-29 14:03:37 +0200433static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
434 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jean Delvare90d66192007-10-08 18:24:35 +0200436 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return sprintf(buf, "%ld\n", (long) data->vrm);
438}
439
Jean Delvare1f448092008-04-29 14:03:37 +0200440static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
441 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100443 struct lm85_data *data = dev_get_drvdata(dev);
444 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return count;
446}
447
448static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
449
Jean Delvare1f448092008-04-29 14:03:37 +0200450static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
451 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200454 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
458
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200459static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
460 char *buf)
461{
462 int nr = to_sensor_dev_attr(attr)->index;
463 struct lm85_data *data = lm85_update_device(dev);
464 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
465}
466
467static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
468static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
469static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
470static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
471static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
472static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
473static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
474static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
475static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
476static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
477static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
478static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
479static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
480static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
481static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
482static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
483static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/* pwm */
486
Jean Delvareb353a482007-07-05 20:36:12 +0200487static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
488 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jean Delvareb353a482007-07-05 20:36:12 +0200490 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200492 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
Jean Delvareb353a482007-07-05 20:36:12 +0200494
495static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
496 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Jean Delvareb353a482007-07-05 20:36:12 +0200498 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct i2c_client *client = to_i2c_client(dev);
500 struct lm85_data *data = i2c_get_clientdata(client);
501 long val = simple_strtol(buf, NULL, 10);
502
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100503 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 data->pwm[nr] = PWM_TO_REG(val);
505 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100506 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return count;
508}
Jean Delvareb353a482007-07-05 20:36:12 +0200509
510static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
511 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Jean Delvareb353a482007-07-05 20:36:12 +0200513 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100515 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100518 switch (pwm_zone) {
519 case -1: /* PWM is always at 100% */
520 enable = 0;
521 break;
522 case 0: /* PWM is always at 0% */
523 case -2: /* PWM responds to manual control */
524 enable = 1;
525 break;
526 default: /* PWM in automatic mode */
527 enable = 2;
528 }
529 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Jean Delvare455f7912007-12-03 23:28:42 +0100532static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
533 *attr, const char *buf, size_t count)
534{
535 int nr = to_sensor_dev_attr(attr)->index;
536 struct i2c_client *client = to_i2c_client(dev);
537 struct lm85_data *data = i2c_get_clientdata(client);
538 long val = simple_strtol(buf, NULL, 10);
539 u8 config;
540
541 switch (val) {
542 case 0:
543 config = 3;
544 break;
545 case 1:
546 config = 7;
547 break;
548 case 2:
549 /* Here we have to choose arbitrarily one of the 5 possible
550 configurations; I go for the safest */
551 config = 6;
552 break;
553 default:
554 return -EINVAL;
555 }
556
557 mutex_lock(&data->update_lock);
558 data->autofan[nr].config = lm85_read_value(client,
559 LM85_REG_AFAN_CONFIG(nr));
560 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
561 | (config << 5);
562 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
563 data->autofan[nr].config);
564 mutex_unlock(&data->update_lock);
565 return count;
566}
567
Jean Delvare34e7dc62008-10-17 17:51:13 +0200568static ssize_t show_pwm_freq(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
571 int nr = to_sensor_dev_attr(attr)->index;
572 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200573 int freq;
574
575 if (IS_ADT7468_HFPWM(data))
576 freq = 22500;
577 else
578 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
579
580 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200581}
582
583static ssize_t set_pwm_freq(struct device *dev,
584 struct device_attribute *attr, const char *buf, size_t count)
585{
586 int nr = to_sensor_dev_attr(attr)->index;
587 struct i2c_client *client = to_i2c_client(dev);
588 struct lm85_data *data = i2c_get_clientdata(client);
589 long val = simple_strtol(buf, NULL, 10);
590
591 mutex_lock(&data->update_lock);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200592 /* The ADT7468 has a special high-frequency PWM output mode,
593 * where all PWM outputs are driven by a 22.5 kHz clock.
594 * This might confuse the user, but there's not much we can do. */
595 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
596 data->cfg5 &= ~ADT7468_HFPWM;
597 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
598 } else { /* Low freq. mode */
599 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
600 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
601 (data->zone[nr].range << 4)
602 | data->pwm_freq[nr]);
603 if (data->type == adt7468) {
604 data->cfg5 |= ADT7468_HFPWM;
605 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
606 }
607 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200608 mutex_unlock(&data->update_lock);
609 return count;
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200613static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
614 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100615static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200616 show_pwm_enable, set_pwm_enable, offset - 1); \
617static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
618 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620show_pwm_reg(1);
621show_pwm_reg(2);
622show_pwm_reg(3);
623
624/* Voltages */
625
Jean Delvareb353a482007-07-05 20:36:12 +0200626static ssize_t show_in(struct device *dev, struct device_attribute *attr,
627 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Jean Delvareb353a482007-07-05 20:36:12 +0200629 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200631 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
632 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Jean Delvareb353a482007-07-05 20:36:12 +0200634
Jean Delvare1f448092008-04-29 14:03:37 +0200635static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200636 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_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
Jean Delvareb353a482007-07-05 20:36:12 +0200642
643static ssize_t set_in_min(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_min[nr] = INS_TO_REG(nr, val);
653 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[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
658static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
659 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Jean Delvareb353a482007-07-05 20:36:12 +0200661 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200663 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
Jean Delvareb353a482007-07-05 20:36:12 +0200665
666static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
667 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Jean Delvareb353a482007-07-05 20:36:12 +0200669 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct i2c_client *client = to_i2c_client(dev);
671 struct lm85_data *data = i2c_get_clientdata(client);
672 long val = simple_strtol(buf, NULL, 10);
673
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100674 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 data->in_max[nr] = INS_TO_REG(nr, val);
676 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100677 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return count;
679}
Jean Delvareb353a482007-07-05 20:36:12 +0200680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200682static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
683 show_in, NULL, offset); \
684static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
685 show_in_min, set_in_min, offset); \
686static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
687 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689show_in_reg(0);
690show_in_reg(1);
691show_in_reg(2);
692show_in_reg(3);
693show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200694show_in_reg(5);
695show_in_reg(6);
696show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/* Temps */
699
Jean Delvareb353a482007-07-05 20:36:12 +0200700static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
701 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jean Delvareb353a482007-07-05 20:36:12 +0200703 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200705 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
706 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
Jean Delvareb353a482007-07-05 20:36:12 +0200708
709static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
710 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jean Delvareb353a482007-07-05 20:36:12 +0200712 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200714 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
Jean Delvareb353a482007-07-05 20:36:12 +0200716
717static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
718 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jean Delvareb353a482007-07-05 20:36:12 +0200720 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct i2c_client *client = to_i2c_client(dev);
722 struct lm85_data *data = i2c_get_clientdata(client);
723 long val = simple_strtol(buf, NULL, 10);
724
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800725 if (IS_ADT7468_OFF64(data))
726 val += 64;
727
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100728 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 data->temp_min[nr] = TEMP_TO_REG(val);
730 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100731 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return count;
733}
Jean Delvareb353a482007-07-05 20:36:12 +0200734
735static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
736 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Jean Delvareb353a482007-07-05 20:36:12 +0200738 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200740 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
Jean Delvareb353a482007-07-05 20:36:12 +0200742
743static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
744 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Jean Delvareb353a482007-07-05 20:36:12 +0200746 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct i2c_client *client = to_i2c_client(dev);
748 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200749 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800751 if (IS_ADT7468_OFF64(data))
752 val += 64;
753
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100754 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 data->temp_max[nr] = TEMP_TO_REG(val);
756 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100757 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return count;
759}
Jean Delvareb353a482007-07-05 20:36:12 +0200760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200762static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
763 show_temp, NULL, offset - 1); \
764static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
765 show_temp_min, set_temp_min, offset - 1); \
766static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
767 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769show_temp_reg(1);
770show_temp_reg(2);
771show_temp_reg(3);
772
773
774/* Automatic PWM control */
775
Jean Delvareb353a482007-07-05 20:36:12 +0200776static ssize_t show_pwm_auto_channels(struct device *dev,
777 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Jean Delvareb353a482007-07-05 20:36:12 +0200779 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200781 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
Jean Delvareb353a482007-07-05 20:36:12 +0200783
784static ssize_t set_pwm_auto_channels(struct device *dev,
785 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Jean Delvareb353a482007-07-05 20:36:12 +0200787 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct i2c_client *client = to_i2c_client(dev);
789 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200790 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100792 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200794 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
796 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100797 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return count;
799}
Jean Delvareb353a482007-07-05 20:36:12 +0200800
801static ssize_t show_pwm_auto_pwm_min(struct device *dev,
802 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Jean Delvareb353a482007-07-05 20:36:12 +0200804 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200806 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Jean Delvareb353a482007-07-05 20:36:12 +0200808
809static ssize_t set_pwm_auto_pwm_min(struct device *dev,
810 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Jean Delvareb353a482007-07-05 20:36:12 +0200812 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct i2c_client *client = to_i2c_client(dev);
814 struct lm85_data *data = i2c_get_clientdata(client);
815 long val = simple_strtol(buf, NULL, 10);
816
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100817 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 data->autofan[nr].min_pwm = PWM_TO_REG(val);
819 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
820 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100821 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return count;
823}
Jean Delvareb353a482007-07-05 20:36:12 +0200824
825static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
826 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Jean Delvareb353a482007-07-05 20:36:12 +0200828 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200830 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
Jean Delvareb353a482007-07-05 20:36:12 +0200832
833static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
834 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jean Delvareb353a482007-07-05 20:36:12 +0200836 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct i2c_client *client = to_i2c_client(dev);
838 struct lm85_data *data = i2c_get_clientdata(client);
839 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200840 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100842 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200844 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
845 tmp &= ~(0x20 << nr);
846 if (data->autofan[nr].min_off)
847 tmp |= 0x20 << nr;
848 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100849 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return count;
851}
Jean Delvareb353a482007-07-05 20:36:12 +0200852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200854static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
855 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
856 set_pwm_auto_channels, offset - 1); \
857static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
858 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
859 set_pwm_auto_pwm_min, offset - 1); \
860static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
861 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200862 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864pwm_auto(1);
865pwm_auto(2);
866pwm_auto(3);
867
868/* Temperature settings for automatic PWM control */
869
Jean Delvareb353a482007-07-05 20:36:12 +0200870static ssize_t show_temp_auto_temp_off(struct device *dev,
871 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Jean Delvareb353a482007-07-05 20:36:12 +0200873 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200875 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 HYST_FROM_REG(data->zone[nr].hyst));
877}
Jean Delvareb353a482007-07-05 20:36:12 +0200878
879static ssize_t set_temp_auto_temp_off(struct device *dev,
880 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jean Delvareb353a482007-07-05 20:36:12 +0200882 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct i2c_client *client = to_i2c_client(dev);
884 struct lm85_data *data = i2c_get_clientdata(client);
885 int min;
886 long val = simple_strtol(buf, NULL, 10);
887
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100888 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 min = TEMP_FROM_REG(data->zone[nr].limit);
890 data->zone[nr].off_desired = TEMP_TO_REG(val);
891 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200892 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 lm85_write_value(client, LM85_REG_AFAN_HYST1,
894 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200895 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200898 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100900 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return count;
902}
Jean Delvareb353a482007-07-05 20:36:12 +0200903
904static ssize_t show_temp_auto_temp_min(struct device *dev,
905 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jean Delvareb353a482007-07-05 20:36:12 +0200907 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200909 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
Jean Delvareb353a482007-07-05 20:36:12 +0200911
912static ssize_t set_temp_auto_temp_min(struct device *dev,
913 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Jean Delvareb353a482007-07-05 20:36:12 +0200915 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct i2c_client *client = to_i2c_client(dev);
917 struct lm85_data *data = i2c_get_clientdata(client);
918 long val = simple_strtol(buf, NULL, 10);
919
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100920 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 data->zone[nr].limit = TEMP_TO_REG(val);
922 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
923 data->zone[nr].limit);
924
925/* Update temp_auto_max and temp_auto_range */
926 data->zone[nr].range = RANGE_TO_REG(
927 TEMP_FROM_REG(data->zone[nr].max_desired) -
928 TEMP_FROM_REG(data->zone[nr].limit));
929 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
930 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200931 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933/* Update temp_auto_hyst and temp_auto_off */
934 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
935 data->zone[nr].limit) - TEMP_FROM_REG(
936 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200937 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 lm85_write_value(client, LM85_REG_AFAN_HYST1,
939 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200940 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 } else {
942 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200943 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100945 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return count;
947}
Jean Delvareb353a482007-07-05 20:36:12 +0200948
949static ssize_t show_temp_auto_temp_max(struct device *dev,
950 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Jean Delvareb353a482007-07-05 20:36:12 +0200952 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200954 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 RANGE_FROM_REG(data->zone[nr].range));
956}
Jean Delvareb353a482007-07-05 20:36:12 +0200957
958static ssize_t set_temp_auto_temp_max(struct device *dev,
959 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jean Delvareb353a482007-07-05 20:36:12 +0200961 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct i2c_client *client = to_i2c_client(dev);
963 struct lm85_data *data = i2c_get_clientdata(client);
964 int min;
965 long val = simple_strtol(buf, NULL, 10);
966
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100967 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 min = TEMP_FROM_REG(data->zone[nr].limit);
969 data->zone[nr].max_desired = TEMP_TO_REG(val);
970 data->zone[nr].range = RANGE_TO_REG(
971 val - min);
972 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
973 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200974 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100975 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return count;
977}
Jean Delvareb353a482007-07-05 20:36:12 +0200978
979static ssize_t show_temp_auto_temp_crit(struct device *dev,
980 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Jean Delvareb353a482007-07-05 20:36:12 +0200982 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200984 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
Jean Delvareb353a482007-07-05 20:36:12 +0200986
987static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200988 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Jean Delvareb353a482007-07-05 20:36:12 +0200990 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct i2c_client *client = to_i2c_client(dev);
992 struct lm85_data *data = i2c_get_clientdata(client);
993 long val = simple_strtol(buf, NULL, 10);
994
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100995 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 data->zone[nr].critical = TEMP_TO_REG(val);
997 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
998 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100999 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return count;
1001}
Jean Delvareb353a482007-07-05 20:36:12 +02001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001004static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1005 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1006 set_temp_auto_temp_off, offset - 1); \
1007static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1008 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1009 set_temp_auto_temp_min, offset - 1); \
1010static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1011 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1012 set_temp_auto_temp_max, offset - 1); \
1013static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1014 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1015 set_temp_auto_temp_crit, offset - 1);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017temp_auto(1);
1018temp_auto(2);
1019temp_auto(3);
1020
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001021static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001022 &sensor_dev_attr_fan1_input.dev_attr.attr,
1023 &sensor_dev_attr_fan2_input.dev_attr.attr,
1024 &sensor_dev_attr_fan3_input.dev_attr.attr,
1025 &sensor_dev_attr_fan4_input.dev_attr.attr,
1026 &sensor_dev_attr_fan1_min.dev_attr.attr,
1027 &sensor_dev_attr_fan2_min.dev_attr.attr,
1028 &sensor_dev_attr_fan3_min.dev_attr.attr,
1029 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001030 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1031 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1032 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1033 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001034
1035 &sensor_dev_attr_pwm1.dev_attr.attr,
1036 &sensor_dev_attr_pwm2.dev_attr.attr,
1037 &sensor_dev_attr_pwm3.dev_attr.attr,
1038 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1039 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1040 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001041 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1042 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1043 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001044
1045 &sensor_dev_attr_in0_input.dev_attr.attr,
1046 &sensor_dev_attr_in1_input.dev_attr.attr,
1047 &sensor_dev_attr_in2_input.dev_attr.attr,
1048 &sensor_dev_attr_in3_input.dev_attr.attr,
1049 &sensor_dev_attr_in0_min.dev_attr.attr,
1050 &sensor_dev_attr_in1_min.dev_attr.attr,
1051 &sensor_dev_attr_in2_min.dev_attr.attr,
1052 &sensor_dev_attr_in3_min.dev_attr.attr,
1053 &sensor_dev_attr_in0_max.dev_attr.attr,
1054 &sensor_dev_attr_in1_max.dev_attr.attr,
1055 &sensor_dev_attr_in2_max.dev_attr.attr,
1056 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001057 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1058 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1059 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1060 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001061
1062 &sensor_dev_attr_temp1_input.dev_attr.attr,
1063 &sensor_dev_attr_temp2_input.dev_attr.attr,
1064 &sensor_dev_attr_temp3_input.dev_attr.attr,
1065 &sensor_dev_attr_temp1_min.dev_attr.attr,
1066 &sensor_dev_attr_temp2_min.dev_attr.attr,
1067 &sensor_dev_attr_temp3_min.dev_attr.attr,
1068 &sensor_dev_attr_temp1_max.dev_attr.attr,
1069 &sensor_dev_attr_temp2_max.dev_attr.attr,
1070 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001071 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1072 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1073 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1074 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1075 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001076
1077 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1078 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1079 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1080 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1081 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1082 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1083 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1084 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1085 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001086
1087 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1088 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1089 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1090 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1091 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1092 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1093 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1094 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1095 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1096 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1097 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1098 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1099
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001100 &dev_attr_vrm.attr,
1101 &dev_attr_cpu0_vid.attr,
1102 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001103 NULL
1104};
1105
1106static const struct attribute_group lm85_group = {
1107 .attrs = lm85_attributes,
1108};
1109
Jean Delvare6b9aad22007-07-05 20:37:21 +02001110static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001111 &sensor_dev_attr_in4_input.dev_attr.attr,
1112 &sensor_dev_attr_in4_min.dev_attr.attr,
1113 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001114 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001115 NULL
1116};
1117
Jean Delvare6b9aad22007-07-05 20:37:21 +02001118static const struct attribute_group lm85_group_in4 = {
1119 .attrs = lm85_attributes_in4,
1120};
1121
1122static struct attribute *lm85_attributes_in567[] = {
1123 &sensor_dev_attr_in5_input.dev_attr.attr,
1124 &sensor_dev_attr_in6_input.dev_attr.attr,
1125 &sensor_dev_attr_in7_input.dev_attr.attr,
1126 &sensor_dev_attr_in5_min.dev_attr.attr,
1127 &sensor_dev_attr_in6_min.dev_attr.attr,
1128 &sensor_dev_attr_in7_min.dev_attr.attr,
1129 &sensor_dev_attr_in5_max.dev_attr.attr,
1130 &sensor_dev_attr_in6_max.dev_attr.attr,
1131 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001132 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1133 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1134 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001135 NULL
1136};
1137
1138static const struct attribute_group lm85_group_in567 = {
1139 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001140};
1141
Jean Delvare5f44759472008-06-25 09:10:30 -04001142static void lm85_init_client(struct i2c_client *client)
1143{
1144 int value;
1145
1146 /* Start monitoring if needed */
1147 value = lm85_read_value(client, LM85_REG_CONFIG);
1148 if (!(value & 0x01)) {
1149 dev_info(&client->dev, "Starting monitoring\n");
1150 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1151 }
1152
1153 /* Warn about unusual configuration bits */
1154 if (value & 0x02)
1155 dev_warn(&client->dev, "Device configuration is locked\n");
1156 if (!(value & 0x04))
1157 dev_warn(&client->dev, "Device is not ready\n");
1158}
1159
Jean Delvare5cfaf332009-09-15 17:18:14 +02001160static int lm85_is_fake(struct i2c_client *client)
1161{
1162 /*
1163 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1164 * emulate the former except that it has no hardware monitoring function
1165 * so the readings are always 0.
1166 */
1167 int i;
1168 u8 in_temp, fan;
1169
1170 for (i = 0; i < 8; i++) {
1171 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1172 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1173 if (in_temp != 0x00 || fan != 0xff)
1174 return 0;
1175 }
1176
1177 return 1;
1178}
1179
Jean Delvare67712d02008-10-17 17:51:14 +02001180/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001181static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Jean Delvare67712d02008-10-17 17:51:14 +02001183 struct i2c_adapter *adapter = client->adapter;
1184 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001185 const char *type_name;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001186 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jean Delvaree89e22b2008-06-25 08:47:35 -04001188 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001190 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Jean Delvared42a2eb2009-12-09 20:35:53 +01001193 /* Determine the chip type */
1194 company = lm85_read_value(client, LM85_REG_COMPANY);
1195 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Jean Delvared42a2eb2009-12-09 20:35:53 +01001197 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1198 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1199 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Jean Delvared42a2eb2009-12-09 20:35:53 +01001201 /* All supported chips have the version in common */
1202 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1203 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1204 dev_dbg(&adapter->dev,
1205 "Autodetection failed: unsupported version\n");
1206 return -ENODEV;
1207 }
1208 type_name = "lm85";
1209
1210 /* Now, refine the detection */
1211 if (company == LM85_COMPANY_NATIONAL) {
1212 switch (verstep) {
1213 case LM85_VERSTEP_LM85C:
1214 type_name = "lm85c";
1215 break;
1216 case LM85_VERSTEP_LM85B:
1217 type_name = "lm85b";
1218 break;
1219 case LM85_VERSTEP_LM96000_1:
1220 case LM85_VERSTEP_LM96000_2:
1221 /* Check for Winbond WPCD377I */
1222 if (lm85_is_fake(client)) {
1223 dev_dbg(&adapter->dev,
1224 "Found Winbond WPCD377I, ignoring\n");
1225 return -ENODEV;
1226 }
1227 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001228 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001229 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1230 switch (verstep) {
1231 case LM85_VERSTEP_ADM1027:
1232 type_name = "adm1027";
1233 break;
1234 case LM85_VERSTEP_ADT7463:
1235 case LM85_VERSTEP_ADT7463C:
1236 type_name = "adt7463";
1237 break;
1238 case LM85_VERSTEP_ADT7468_1:
1239 case LM85_VERSTEP_ADT7468_2:
1240 type_name = "adt7468";
1241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001243 } else if (company == LM85_COMPANY_SMSC) {
1244 switch (verstep) {
1245 case LM85_VERSTEP_EMC6D100_A0:
1246 case LM85_VERSTEP_EMC6D100_A1:
1247 /* Note: we can't tell a '100 from a '101 */
1248 type_name = "emc6d100";
1249 break;
1250 case LM85_VERSTEP_EMC6D102:
1251 type_name = "emc6d102";
1252 break;
1253 }
1254 } else {
1255 dev_dbg(&adapter->dev,
1256 "Autodetection failed: unknown vendor\n");
1257 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Jean Delvare67712d02008-10-17 17:51:14 +02001260 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Jean Delvare67712d02008-10-17 17:51:14 +02001262 return 0;
1263}
1264
1265static int lm85_probe(struct i2c_client *client,
1266 const struct i2c_device_id *id)
1267{
1268 struct lm85_data *data;
1269 int err;
1270
1271 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1272 if (!data)
1273 return -ENOMEM;
1274
1275 i2c_set_clientdata(client, data);
1276 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001277 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Jean Delvare67712d02008-10-17 17:51:14 +02001279 /* Fill in the chip specific driver values */
1280 switch (data->type) {
1281 case adm1027:
1282 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001283 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001284 case emc6d100:
1285 case emc6d102:
1286 data->freq_map = adm1027_freq_map;
1287 break;
1288 default:
1289 data->freq_map = lm85_freq_map;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001293 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001296 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001299 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1300 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001301 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001303 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001304 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001305 data->vid = lm85_read_value(client, LM85_REG_VID);
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001306 if (!((data->type == adt7463 || data->type == adt7468) &&
1307 (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001308 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001309 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001310 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001311
1312 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001313 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001314 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001315 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001316 goto err_remove_files;
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001317
Jean Delvaree89e22b2008-06-25 08:47:35 -04001318 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001319 if (IS_ERR(data->hwmon_dev)) {
1320 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001321 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325
1326 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001327 err_remove_files:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001328 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1329 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare67712d02008-10-17 17:51:14 +02001330 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001331 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvaref9080372008-10-17 17:51:14 +02001332 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return err;
1335}
1336
Jean Delvare67712d02008-10-17 17:51:14 +02001337static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001339 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001340 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001341 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001342 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1343 if (data->type == emc6d100)
1344 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001345 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347}
1348
1349
Ben Dooksd8d20612005-10-26 21:05:46 +02001350static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int res;
1353
1354 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001355 switch (reg) {
1356 case LM85_REG_FAN(0): /* Read WORD data */
1357 case LM85_REG_FAN(1):
1358 case LM85_REG_FAN(2):
1359 case LM85_REG_FAN(3):
1360 case LM85_REG_FAN_MIN(0):
1361 case LM85_REG_FAN_MIN(1):
1362 case LM85_REG_FAN_MIN(2):
1363 case LM85_REG_FAN_MIN(3):
1364 case LM85_REG_ALARM1: /* Read both bytes at once */
1365 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1366 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 default: /* Read BYTE data */
1369 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
Jean Delvare1f448092008-04-29 14:03:37 +02001373 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Jean Delvaree89e22b2008-06-25 08:47:35 -04001376static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Jean Delvare1f448092008-04-29 14:03:37 +02001378 switch (reg) {
1379 case LM85_REG_FAN(0): /* Write WORD data */
1380 case LM85_REG_FAN(1):
1381 case LM85_REG_FAN(2):
1382 case LM85_REG_FAN(3):
1383 case LM85_REG_FAN_MIN(0):
1384 case LM85_REG_FAN_MIN(1):
1385 case LM85_REG_FAN_MIN(2):
1386 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001388 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1389 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001392 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397static struct lm85_data *lm85_update_device(struct device *dev)
1398{
1399 struct i2c_client *client = to_i2c_client(dev);
1400 struct lm85_data *data = i2c_get_clientdata(client);
1401 int i;
1402
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001403 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jean Delvare1f448092008-04-29 14:03:37 +02001405 if (!data->valid ||
1406 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* Things that change quickly */
1408 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /* Have to read extended bits first to "freeze" the
1411 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001412 * There are 2 additional resolution bits per channel and we
1413 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001415 if (data->type == adm1027 || data->type == adt7463 ||
1416 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int ext1 = lm85_read_value(client,
1418 ADM1027_REG_EXTEND_ADC1);
1419 int ext2 = lm85_read_value(client,
1420 ADM1027_REG_EXTEND_ADC2);
1421 int val = (ext1 << 8) + ext2;
1422
Jean Delvare1f448092008-04-29 14:03:37 +02001423 for (i = 0; i <= 4; i++)
1424 data->in_ext[i] =
1425 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Jean Delvare1f448092008-04-29 14:03:37 +02001427 for (i = 0; i <= 2; i++)
1428 data->temp_ext[i] =
1429 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Jean Delvare9c516ef2005-11-26 20:07:54 +01001432 data->vid = lm85_read_value(client, LM85_REG_VID);
1433
1434 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 data->in[i] =
1436 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001437 data->fan[i] =
1438 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001441 if (!((data->type == adt7463 || data->type == adt7468) &&
1442 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001443 data->in[4] = lm85_read_value(client,
1444 LM85_REG_IN(4));
1445 }
1446
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001447 if (data->type == adt7468)
1448 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 for (i = 0; i <= 2; ++i) {
1451 data->temp[i] =
1452 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 data->pwm[i] =
1454 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001455
1456 if (IS_ADT7468_OFF64(data))
1457 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
1460 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1461
Jean Delvaredd1ac532008-05-01 08:47:33 +02001462 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Three more voltage sensors */
1464 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001465 data->in[i] = lm85_read_value(client,
1466 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001469 data->alarms |= lm85_read_value(client,
1470 EMC6D100_REG_ALARM3) << 16;
1471 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Have to read LSB bits after the MSB ones because
1473 the reading of the MSB bits has frozen the
1474 LSBs (backward from the ADM1027).
1475 */
1476 int ext1 = lm85_read_value(client,
1477 EMC6D102_REG_EXTEND_ADC1);
1478 int ext2 = lm85_read_value(client,
1479 EMC6D102_REG_EXTEND_ADC2);
1480 int ext3 = lm85_read_value(client,
1481 EMC6D102_REG_EXTEND_ADC3);
1482 int ext4 = lm85_read_value(client,
1483 EMC6D102_REG_EXTEND_ADC4);
1484 data->in_ext[0] = ext3 & 0x0f;
1485 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001486 data->in_ext[2] = ext4 >> 4;
1487 data->in_ext[3] = ext3 >> 4;
1488 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 data->temp_ext[0] = ext1 & 0x0f;
1491 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001492 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
Jean Delvare1f448092008-04-29 14:03:37 +02001495 data->last_reading = jiffies;
1496 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Jean Delvare1f448092008-04-29 14:03:37 +02001498 if (!data->valid ||
1499 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* Things that don't change often */
1501 dev_dbg(&client->dev, "Reading config values\n");
1502
Jean Delvare9c516ef2005-11-26 20:07:54 +01001503 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 data->in_min[i] =
1505 lm85_read_value(client, LM85_REG_IN_MIN(i));
1506 data->in_max[i] =
1507 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001508 data->fan_min[i] =
1509 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001512 if (!((data->type == adt7463 || data->type == adt7468) &&
1513 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001514 data->in_min[4] = lm85_read_value(client,
1515 LM85_REG_IN_MIN(4));
1516 data->in_max[4] = lm85_read_value(client,
1517 LM85_REG_IN_MAX(4));
1518 }
1519
Jean Delvare1f448092008-04-29 14:03:37 +02001520 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001522 data->in_min[i] = lm85_read_value(client,
1523 EMC6D100_REG_IN_MIN(i));
1524 data->in_max[i] = lm85_read_value(client,
1525 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 }
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001530 int val;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 data->temp_min[i] =
1533 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1534 data->temp_max[i] =
1535 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 data->autofan[i].config =
1538 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1539 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001540 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001541 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 data->autofan[i].min_pwm =
1543 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1544 data->zone[i].limit =
1545 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1546 data->zone[i].critical =
1547 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001548
1549 if (IS_ADT7468_OFF64(data)) {
1550 data->temp_min[i] -= 64;
1551 data->temp_max[i] -= 64;
1552 data->zone[i].limit -= 64;
1553 data->zone[i].critical -= 64;
1554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
1557 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001558 data->autofan[0].min_off = (i & 0x20) != 0;
1559 data->autofan[1].min_off = (i & 0x40) != 0;
1560 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001563 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001564 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001567 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001570 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 data->valid = 1;
1573
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001574 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 return data;
1577}
1578
1579
1580static int __init sm_lm85_init(void)
1581{
1582 return i2c_add_driver(&lm85_driver);
1583}
1584
Jean Delvare1f448092008-04-29 14:03:37 +02001585static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 i2c_del_driver(&lm85_driver);
1588}
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001591MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1592 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001593 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1595
1596module_init(sm_lm85_init);
1597module_exit(sm_lm85_exit);