blob: cf1422ef6ef0acb8fc222d813392a625b00282e8 [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
Darrick J. Wongc15ade62009-03-10 12:55:47 -070075#define LM85_VERSTEP_GENERIC2 0x70
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define LM85_VERSTEP_LM85C 0x60
77#define LM85_VERSTEP_LM85B 0x62
78#define LM85_VERSTEP_ADM1027 0x60
79#define LM85_VERSTEP_ADT7463 0x62
80#define LM85_VERSTEP_ADT7463C 0x6A
Darrick J. Wong79b92f22008-11-12 13:26:59 -080081#define LM85_VERSTEP_ADT7468_1 0x71
82#define LM85_VERSTEP_ADT7468_2 0x72
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define LM85_VERSTEP_EMC6D100_A0 0x60
84#define LM85_VERSTEP_EMC6D100_A1 0x61
85#define LM85_VERSTEP_EMC6D102 0x65
86
87#define LM85_REG_CONFIG 0x40
88
89#define LM85_REG_ALARM1 0x41
90#define LM85_REG_ALARM2 0x42
91
92#define LM85_REG_VID 0x43
93
94/* Automated FAN control */
95#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
96#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
97#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
99#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
100#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
101#define LM85_REG_AFAN_HYST1 0x6d
102#define LM85_REG_AFAN_HYST2 0x6e
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define ADM1027_REG_EXTEND_ADC1 0x76
105#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107#define EMC6D100_REG_ALARM3 0x7d
108/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200109#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
110#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
111#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define EMC6D102_REG_EXTEND_ADC1 0x85
113#define EMC6D102_REG_EXTEND_ADC2 0x86
114#define EMC6D102_REG_EXTEND_ADC3 0x87
115#define EMC6D102_REG_EXTEND_ADC4 0x88
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jean Delvare1f448092008-04-29 14:03:37 +0200118/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 variants. Note that you should be a bit careful with which arguments
120 these macros are called: arguments may be evaluated more than once.
121 */
122
123/* IN are scaled acording to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400124static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200125 2500, 2250, 3300, 5000, 12000,
126 3300, 1500, 1800 /*EMC6D100*/
127};
128#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jean Delvare1f448092008-04-29 14:03:37 +0200130#define INS_TO_REG(n, val) \
131 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jean Delvare1f448092008-04-29 14:03:37 +0200133#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200134 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jean Delvare1f448092008-04-29 14:03:37 +0200136#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200139static inline u16 FAN_TO_REG(unsigned long val)
140{
141 if (!val)
142 return 0xffff;
143 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
144}
Jean Delvare1f448092008-04-29 14:03:37 +0200145#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
146 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* Temperature is reported in .001 degC increments */
149#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200150 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
151#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200152 SCALE(((val) << 4) + (ext), 16, 1000)
153#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jean Delvare1f448092008-04-29 14:03:37 +0200155#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define PWM_FROM_REG(val) (val)
157
158
159/* ZONEs have the following parameters:
160 * Limit (low) temp, 1. degC
161 * Hysteresis (below limit), 1. degC (0-15)
162 * Range of speed control, .1 degC (2-80)
163 * Critical (high) temp, 1. degC
164 *
165 * FAN PWMs have the following parameters:
166 * Reference Zone, 1, 2, 3, etc.
167 * Spinup time, .05 sec
168 * PWM value at limit/low temp, 1 count
169 * PWM Frequency, 1. Hz
170 * PWM is Min or OFF below limit, flag
171 * Invert PWM output, flag
172 *
173 * Some chips filter the temp, others the fan.
174 * Filter constant (or disabled) .1 seconds
175 */
176
177/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400178static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200179 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
180 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
181};
182
183static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int i;
186
Jean Delvared38b1492008-04-03 10:40:39 +0200187 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200188 for (i = 0; i < 15; ++i) {
189 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Jean Delvared38b1492008-04-03 10:40:39 +0200192
Jean Delvare1b92ada2008-10-17 17:51:14 +0200193 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
Jean Delvare1f448092008-04-29 14:03:37 +0200195#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200198static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200199 10, 15, 23, 30, 38, 47, 61, 94
200};
201static const int adm1027_freq_map[8] = { /* 1 Hz */
202 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200203};
204
Jean Delvare8a0795d2008-10-17 17:51:14 +0200205static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int i;
208
Jean Delvare86010c92008-10-17 17:51:13 +0200209 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200210 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200211 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200212 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400213 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200215
216static int FREQ_FROM_REG(const int *map, u8 reg)
217{
218 return map[reg & 0x07];
219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/* Since we can't use strings, I'm abusing these numbers
222 * to stand in for the following meanings:
223 * 1 -- PWM responds to Zone 1
224 * 2 -- PWM responds to Zone 2
225 * 3 -- PWM responds to Zone 3
226 * 23 -- PWM responds to the higher temp of Zone 2 or 3
227 * 123 -- PWM responds to highest of Zone 1, 2, or 3
228 * 0 -- PWM is always at 0% (ie, off)
229 * -1 -- PWM is always at 100%
230 * -2 -- PWM responds to manual control
231 */
232
Jean Delvaree89e22b2008-06-25 08:47:35 -0400233static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
234#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jean Delvare1f448092008-04-29 14:03:37 +0200236static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int i;
239
Jean Delvare1f448092008-04-29 14:03:37 +0200240 for (i = 0; i <= 7; ++i)
241 if (zone == lm85_zone_map[i])
242 break;
243 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400245 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Jean Delvare1f448092008-04-29 14:03:37 +0200248#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
249#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* Chip sampling rates
252 *
253 * Some sensors are not updated more frequently than once per second
254 * so it doesn't make sense to read them more often than that.
255 * We cache the results and return the saved data if the driver
256 * is called again before a second has elapsed.
257 *
258 * Also, there is significant configuration data for this chip
259 * given the automatic PWM fan control that is possible. There
260 * are about 47 bytes of config data to only 22 bytes of actual
261 * readings. So, we keep the config data up to date in the cache
262 * when it is written and only sample it once every 1 *minute*
263 */
264#define LM85_DATA_INTERVAL (HZ + HZ / 2)
265#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/* LM85 can automatically adjust fan speeds based on temperature
268 * This structure encapsulates an entire Zone config. There are
269 * three zones (one for each temperature input) on the lm85
270 */
271struct lm85_zone {
272 s8 limit; /* Low temp limit */
273 u8 hyst; /* Low limit hysteresis. (0-15) */
274 u8 range; /* Temp range, encoded */
275 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200276 u8 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * to prevent "drift" as other autofan control
278 * values change.
279 */
Jean Delvare1f448092008-04-29 14:03:37 +0200280 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * to prevent "drift" as other autofan control
282 * values change.
283 */
284};
285
286struct lm85_autofan {
287 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 u8 min_pwm; /* Minimum PWM value, encoded */
289 u8 min_off; /* Min PWM or OFF below "limit", flag */
290};
291
Jean Delvareed6bafb2007-02-14 21:15:03 +0100292/* For each registered chip, we need to keep some data in memory.
293 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700295 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200296 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 enum chips type;
298
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100299 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int valid; /* !=0 if following fields are valid */
301 unsigned long last_reading; /* In jiffies */
302 unsigned long last_config; /* In jiffies */
303
304 u8 in[8]; /* Register value */
305 u8 in_max[8]; /* Register value */
306 u8 in_min[8]; /* Register value */
307 s8 temp[3]; /* Register value */
308 s8 temp_min[3]; /* Register value */
309 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 u16 fan[4]; /* Register value */
311 u16 fan_min[4]; /* Register value */
312 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200313 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 u8 temp_ext[3]; /* Decoded values */
315 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 u8 vid; /* Register value */
317 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800319 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct lm85_autofan autofan[3];
321 struct lm85_zone zone[3];
322};
323
Jean Delvare67712d02008-10-17 17:51:14 +0200324static int lm85_detect(struct i2c_client *client, int kind,
325 struct i2c_board_info *info);
326static int lm85_probe(struct i2c_client *client,
327 const struct i2c_device_id *id);
328static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100330static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400331static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334
Jean Delvare67712d02008-10-17 17:51:14 +0200335static const struct i2c_device_id lm85_id[] = {
336 { "adm1027", adm1027 },
337 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700338 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200339 { "lm85", any_chip },
340 { "lm85b", lm85b },
341 { "lm85c", lm85c },
342 { "emc6d100", emc6d100 },
343 { "emc6d101", emc6d100 },
344 { "emc6d102", emc6d102 },
345 { }
346};
347MODULE_DEVICE_TABLE(i2c, lm85_id);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200350 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100351 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100352 .name = "lm85",
353 },
Jean Delvare67712d02008-10-17 17:51:14 +0200354 .probe = lm85_probe,
355 .remove = lm85_remove,
356 .id_table = lm85_id,
357 .detect = lm85_detect,
358 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
361
362/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200363static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
364 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Jean Delvareb353a482007-07-05 20:36:12 +0200366 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200368 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
Jean Delvareb353a482007-07-05 20:36:12 +0200370
371static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
372 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jean Delvareb353a482007-07-05 20:36:12 +0200374 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200376 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Jean Delvareb353a482007-07-05 20:36:12 +0200378
379static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jean Delvareb353a482007-07-05 20:36:12 +0200382 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct i2c_client *client = to_i2c_client(dev);
384 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200385 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100387 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 data->fan_min[nr] = FAN_TO_REG(val);
389 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100390 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return count;
392}
393
394#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200395static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
396 show_fan, NULL, offset - 1); \
397static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
398 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400show_fan_offset(1);
401show_fan_offset(2);
402show_fan_offset(3);
403show_fan_offset(4);
404
405/* vid, vrm, alarms */
406
Jean Delvare1f448092008-04-29 14:03:37 +0200407static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
408 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100411 int vid;
412
413 if (data->type == adt7463 && (data->vid & 0x80)) {
414 /* 6-pin VID (VRM 10) */
415 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
416 } else {
417 /* 5-pin VID (VRM 9) */
418 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
419 }
420
421 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
425
Jean Delvare1f448092008-04-29 14:03:37 +0200426static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
427 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Jean Delvare90d66192007-10-08 18:24:35 +0200429 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return sprintf(buf, "%ld\n", (long) data->vrm);
431}
432
Jean Delvare1f448092008-04-29 14:03:37 +0200433static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100436 struct lm85_data *data = dev_get_drvdata(dev);
437 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return count;
439}
440
441static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
442
Jean Delvare1f448092008-04-29 14:03:37 +0200443static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
444 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200447 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
451
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200452static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
453 char *buf)
454{
455 int nr = to_sensor_dev_attr(attr)->index;
456 struct lm85_data *data = lm85_update_device(dev);
457 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
458}
459
460static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
461static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
462static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
463static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
464static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
465static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
466static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
467static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
468static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
469static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
470static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
471static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
472static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
473static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
474static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
475static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
476static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/* pwm */
479
Jean Delvareb353a482007-07-05 20:36:12 +0200480static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
481 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jean Delvareb353a482007-07-05 20:36:12 +0200483 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200485 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Jean Delvareb353a482007-07-05 20:36:12 +0200487
488static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
489 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Jean Delvareb353a482007-07-05 20:36:12 +0200491 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct i2c_client *client = to_i2c_client(dev);
493 struct lm85_data *data = i2c_get_clientdata(client);
494 long val = simple_strtol(buf, NULL, 10);
495
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100496 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 data->pwm[nr] = PWM_TO_REG(val);
498 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100499 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return count;
501}
Jean Delvareb353a482007-07-05 20:36:12 +0200502
503static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
504 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jean Delvareb353a482007-07-05 20:36:12 +0200506 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100508 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100511 switch (pwm_zone) {
512 case -1: /* PWM is always at 100% */
513 enable = 0;
514 break;
515 case 0: /* PWM is always at 0% */
516 case -2: /* PWM responds to manual control */
517 enable = 1;
518 break;
519 default: /* PWM in automatic mode */
520 enable = 2;
521 }
522 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Jean Delvare455f7912007-12-03 23:28:42 +0100525static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
526 *attr, const char *buf, size_t count)
527{
528 int nr = to_sensor_dev_attr(attr)->index;
529 struct i2c_client *client = to_i2c_client(dev);
530 struct lm85_data *data = i2c_get_clientdata(client);
531 long val = simple_strtol(buf, NULL, 10);
532 u8 config;
533
534 switch (val) {
535 case 0:
536 config = 3;
537 break;
538 case 1:
539 config = 7;
540 break;
541 case 2:
542 /* Here we have to choose arbitrarily one of the 5 possible
543 configurations; I go for the safest */
544 config = 6;
545 break;
546 default:
547 return -EINVAL;
548 }
549
550 mutex_lock(&data->update_lock);
551 data->autofan[nr].config = lm85_read_value(client,
552 LM85_REG_AFAN_CONFIG(nr));
553 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
554 | (config << 5);
555 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
556 data->autofan[nr].config);
557 mutex_unlock(&data->update_lock);
558 return count;
559}
560
Jean Delvare34e7dc62008-10-17 17:51:13 +0200561static ssize_t show_pwm_freq(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
564 int nr = to_sensor_dev_attr(attr)->index;
565 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200566 return sprintf(buf, "%d\n", FREQ_FROM_REG(data->freq_map,
567 data->pwm_freq[nr]));
Jean Delvare34e7dc62008-10-17 17:51:13 +0200568}
569
570static ssize_t set_pwm_freq(struct device *dev,
571 struct device_attribute *attr, const char *buf, size_t count)
572{
573 int nr = to_sensor_dev_attr(attr)->index;
574 struct i2c_client *client = to_i2c_client(dev);
575 struct lm85_data *data = i2c_get_clientdata(client);
576 long val = simple_strtol(buf, NULL, 10);
577
578 mutex_lock(&data->update_lock);
Jean Delvare8a0795d2008-10-17 17:51:14 +0200579 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200580 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
581 (data->zone[nr].range << 4)
582 | data->pwm_freq[nr]);
583 mutex_unlock(&data->update_lock);
584 return count;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200588static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
589 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100590static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200591 show_pwm_enable, set_pwm_enable, offset - 1); \
592static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
593 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595show_pwm_reg(1);
596show_pwm_reg(2);
597show_pwm_reg(3);
598
599/* Voltages */
600
Jean Delvareb353a482007-07-05 20:36:12 +0200601static ssize_t show_in(struct device *dev, struct device_attribute *attr,
602 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Jean Delvareb353a482007-07-05 20:36:12 +0200604 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200606 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
607 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
Jean Delvareb353a482007-07-05 20:36:12 +0200609
Jean Delvare1f448092008-04-29 14:03:37 +0200610static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200611 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jean Delvareb353a482007-07-05 20:36:12 +0200613 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200615 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Jean Delvareb353a482007-07-05 20:36:12 +0200617
618static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
619 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jean Delvareb353a482007-07-05 20:36:12 +0200621 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct i2c_client *client = to_i2c_client(dev);
623 struct lm85_data *data = i2c_get_clientdata(client);
624 long val = simple_strtol(buf, NULL, 10);
625
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100626 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 data->in_min[nr] = INS_TO_REG(nr, val);
628 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100629 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return count;
631}
Jean Delvareb353a482007-07-05 20:36:12 +0200632
633static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
634 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jean Delvareb353a482007-07-05 20:36:12 +0200636 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200638 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Jean Delvareb353a482007-07-05 20:36:12 +0200640
641static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
642 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Jean Delvareb353a482007-07-05 20:36:12 +0200644 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct i2c_client *client = to_i2c_client(dev);
646 struct lm85_data *data = i2c_get_clientdata(client);
647 long val = simple_strtol(buf, NULL, 10);
648
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100649 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 data->in_max[nr] = INS_TO_REG(nr, val);
651 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100652 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return count;
654}
Jean Delvareb353a482007-07-05 20:36:12 +0200655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200657static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
658 show_in, NULL, offset); \
659static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
660 show_in_min, set_in_min, offset); \
661static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
662 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664show_in_reg(0);
665show_in_reg(1);
666show_in_reg(2);
667show_in_reg(3);
668show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200669show_in_reg(5);
670show_in_reg(6);
671show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/* Temps */
674
Jean Delvareb353a482007-07-05 20:36:12 +0200675static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
676 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Jean Delvareb353a482007-07-05 20:36:12 +0200678 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200680 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
681 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Jean Delvareb353a482007-07-05 20:36:12 +0200683
684static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
685 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jean Delvareb353a482007-07-05 20:36:12 +0200687 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200689 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Jean Delvareb353a482007-07-05 20:36:12 +0200691
692static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
693 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Jean Delvareb353a482007-07-05 20:36:12 +0200695 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct i2c_client *client = to_i2c_client(dev);
697 struct lm85_data *data = i2c_get_clientdata(client);
698 long val = simple_strtol(buf, NULL, 10);
699
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800700 if (IS_ADT7468_OFF64(data))
701 val += 64;
702
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100703 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 data->temp_min[nr] = TEMP_TO_REG(val);
705 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100706 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return count;
708}
Jean Delvareb353a482007-07-05 20:36:12 +0200709
710static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
711 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Jean Delvareb353a482007-07-05 20:36:12 +0200713 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200715 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Jean Delvareb353a482007-07-05 20:36:12 +0200717
718static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Jean Delvareb353a482007-07-05 20:36:12 +0200721 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct i2c_client *client = to_i2c_client(dev);
723 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200724 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800726 if (IS_ADT7468_OFF64(data))
727 val += 64;
728
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 data->temp_max[nr] = TEMP_TO_REG(val);
731 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100732 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return count;
734}
Jean Delvareb353a482007-07-05 20:36:12 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200737static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
738 show_temp, NULL, offset - 1); \
739static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
740 show_temp_min, set_temp_min, offset - 1); \
741static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
742 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744show_temp_reg(1);
745show_temp_reg(2);
746show_temp_reg(3);
747
748
749/* Automatic PWM control */
750
Jean Delvareb353a482007-07-05 20:36:12 +0200751static ssize_t show_pwm_auto_channels(struct device *dev,
752 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Jean Delvareb353a482007-07-05 20:36:12 +0200754 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200756 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
Jean Delvareb353a482007-07-05 20:36:12 +0200758
759static ssize_t set_pwm_auto_channels(struct device *dev,
760 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Jean Delvareb353a482007-07-05 20:36:12 +0200762 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct i2c_client *client = to_i2c_client(dev);
764 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200765 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100767 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200769 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
771 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100772 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return count;
774}
Jean Delvareb353a482007-07-05 20:36:12 +0200775
776static ssize_t show_pwm_auto_pwm_min(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", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
Jean Delvareb353a482007-07-05 20:36:12 +0200783
784static ssize_t set_pwm_auto_pwm_min(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);
790 long val = simple_strtol(buf, NULL, 10);
791
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100792 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 data->autofan[nr].min_pwm = PWM_TO_REG(val);
794 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
795 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100796 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return count;
798}
Jean Delvareb353a482007-07-05 20:36:12 +0200799
800static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
801 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Jean Delvareb353a482007-07-05 20:36:12 +0200803 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200805 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
Jean Delvareb353a482007-07-05 20:36:12 +0200807
808static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
809 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Jean Delvareb353a482007-07-05 20:36:12 +0200811 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct i2c_client *client = to_i2c_client(dev);
813 struct lm85_data *data = i2c_get_clientdata(client);
814 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200815 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
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_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200819 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
820 tmp &= ~(0x20 << nr);
821 if (data->autofan[nr].min_off)
822 tmp |= 0x20 << nr;
823 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100824 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return count;
826}
Jean Delvareb353a482007-07-05 20:36:12 +0200827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200829static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
830 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
831 set_pwm_auto_channels, offset - 1); \
832static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
833 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
834 set_pwm_auto_pwm_min, offset - 1); \
835static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
836 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200837 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839pwm_auto(1);
840pwm_auto(2);
841pwm_auto(3);
842
843/* Temperature settings for automatic PWM control */
844
Jean Delvareb353a482007-07-05 20:36:12 +0200845static ssize_t show_temp_auto_temp_off(struct device *dev,
846 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Jean Delvareb353a482007-07-05 20:36:12 +0200848 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200850 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 HYST_FROM_REG(data->zone[nr].hyst));
852}
Jean Delvareb353a482007-07-05 20:36:12 +0200853
854static ssize_t set_temp_auto_temp_off(struct device *dev,
855 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Jean Delvareb353a482007-07-05 20:36:12 +0200857 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct i2c_client *client = to_i2c_client(dev);
859 struct lm85_data *data = i2c_get_clientdata(client);
860 int min;
861 long val = simple_strtol(buf, NULL, 10);
862
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100863 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 min = TEMP_FROM_REG(data->zone[nr].limit);
865 data->zone[nr].off_desired = TEMP_TO_REG(val);
866 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200867 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 lm85_write_value(client, LM85_REG_AFAN_HYST1,
869 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200870 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } else {
872 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200873 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100875 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return count;
877}
Jean Delvareb353a482007-07-05 20:36:12 +0200878
879static ssize_t show_temp_auto_temp_min(struct device *dev,
880 struct device_attribute *attr, char *buf)
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 lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200884 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Jean Delvareb353a482007-07-05 20:36:12 +0200886
887static ssize_t set_temp_auto_temp_min(struct device *dev,
888 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jean Delvareb353a482007-07-05 20:36:12 +0200890 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct i2c_client *client = to_i2c_client(dev);
892 struct lm85_data *data = i2c_get_clientdata(client);
893 long val = simple_strtol(buf, NULL, 10);
894
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100895 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 data->zone[nr].limit = TEMP_TO_REG(val);
897 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
898 data->zone[nr].limit);
899
900/* Update temp_auto_max and temp_auto_range */
901 data->zone[nr].range = RANGE_TO_REG(
902 TEMP_FROM_REG(data->zone[nr].max_desired) -
903 TEMP_FROM_REG(data->zone[nr].limit));
904 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
905 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200906 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908/* Update temp_auto_hyst and temp_auto_off */
909 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
910 data->zone[nr].limit) - TEMP_FROM_REG(
911 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200912 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 lm85_write_value(client, LM85_REG_AFAN_HYST1,
914 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200915 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 } else {
917 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200918 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100920 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return count;
922}
Jean Delvareb353a482007-07-05 20:36:12 +0200923
924static ssize_t show_temp_auto_temp_max(struct device *dev,
925 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Jean Delvareb353a482007-07-05 20:36:12 +0200927 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200929 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 RANGE_FROM_REG(data->zone[nr].range));
931}
Jean Delvareb353a482007-07-05 20:36:12 +0200932
933static ssize_t set_temp_auto_temp_max(struct device *dev,
934 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jean Delvareb353a482007-07-05 20:36:12 +0200936 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct i2c_client *client = to_i2c_client(dev);
938 struct lm85_data *data = i2c_get_clientdata(client);
939 int min;
940 long val = simple_strtol(buf, NULL, 10);
941
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100942 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 min = TEMP_FROM_REG(data->zone[nr].limit);
944 data->zone[nr].max_desired = TEMP_TO_REG(val);
945 data->zone[nr].range = RANGE_TO_REG(
946 val - min);
947 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
948 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200949 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100950 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return count;
952}
Jean Delvareb353a482007-07-05 20:36:12 +0200953
954static ssize_t show_temp_auto_temp_crit(struct device *dev,
955 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Jean Delvareb353a482007-07-05 20:36:12 +0200957 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200959 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
Jean Delvareb353a482007-07-05 20:36:12 +0200961
962static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200963 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Jean Delvareb353a482007-07-05 20:36:12 +0200965 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct i2c_client *client = to_i2c_client(dev);
967 struct lm85_data *data = i2c_get_clientdata(client);
968 long val = simple_strtol(buf, NULL, 10);
969
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100970 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 data->zone[nr].critical = TEMP_TO_REG(val);
972 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
973 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100974 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return count;
976}
Jean Delvareb353a482007-07-05 20:36:12 +0200977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200979static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
980 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
981 set_temp_auto_temp_off, offset - 1); \
982static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
983 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
984 set_temp_auto_temp_min, offset - 1); \
985static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
986 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
987 set_temp_auto_temp_max, offset - 1); \
988static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
989 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
990 set_temp_auto_temp_crit, offset - 1);
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992temp_auto(1);
993temp_auto(2);
994temp_auto(3);
995
Mark M. Hoffman0501a382006-09-24 21:14:35 +0200996static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +0200997 &sensor_dev_attr_fan1_input.dev_attr.attr,
998 &sensor_dev_attr_fan2_input.dev_attr.attr,
999 &sensor_dev_attr_fan3_input.dev_attr.attr,
1000 &sensor_dev_attr_fan4_input.dev_attr.attr,
1001 &sensor_dev_attr_fan1_min.dev_attr.attr,
1002 &sensor_dev_attr_fan2_min.dev_attr.attr,
1003 &sensor_dev_attr_fan3_min.dev_attr.attr,
1004 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001005 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1006 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1007 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1008 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001009
1010 &sensor_dev_attr_pwm1.dev_attr.attr,
1011 &sensor_dev_attr_pwm2.dev_attr.attr,
1012 &sensor_dev_attr_pwm3.dev_attr.attr,
1013 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1014 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1015 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001016 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1017 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1018 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001019
1020 &sensor_dev_attr_in0_input.dev_attr.attr,
1021 &sensor_dev_attr_in1_input.dev_attr.attr,
1022 &sensor_dev_attr_in2_input.dev_attr.attr,
1023 &sensor_dev_attr_in3_input.dev_attr.attr,
1024 &sensor_dev_attr_in0_min.dev_attr.attr,
1025 &sensor_dev_attr_in1_min.dev_attr.attr,
1026 &sensor_dev_attr_in2_min.dev_attr.attr,
1027 &sensor_dev_attr_in3_min.dev_attr.attr,
1028 &sensor_dev_attr_in0_max.dev_attr.attr,
1029 &sensor_dev_attr_in1_max.dev_attr.attr,
1030 &sensor_dev_attr_in2_max.dev_attr.attr,
1031 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001032 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1033 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1034 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1035 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001036
1037 &sensor_dev_attr_temp1_input.dev_attr.attr,
1038 &sensor_dev_attr_temp2_input.dev_attr.attr,
1039 &sensor_dev_attr_temp3_input.dev_attr.attr,
1040 &sensor_dev_attr_temp1_min.dev_attr.attr,
1041 &sensor_dev_attr_temp2_min.dev_attr.attr,
1042 &sensor_dev_attr_temp3_min.dev_attr.attr,
1043 &sensor_dev_attr_temp1_max.dev_attr.attr,
1044 &sensor_dev_attr_temp2_max.dev_attr.attr,
1045 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001046 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1047 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1048 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1049 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1050 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001051
1052 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1053 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1054 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1055 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1056 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1057 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1058 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1059 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1060 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001061
1062 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1063 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1064 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1065 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1066 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1067 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1068 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1069 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1070 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1071 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1072 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1073 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1074
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001075 &dev_attr_vrm.attr,
1076 &dev_attr_cpu0_vid.attr,
1077 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001078 NULL
1079};
1080
1081static const struct attribute_group lm85_group = {
1082 .attrs = lm85_attributes,
1083};
1084
Jean Delvare6b9aad22007-07-05 20:37:21 +02001085static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001086 &sensor_dev_attr_in4_input.dev_attr.attr,
1087 &sensor_dev_attr_in4_min.dev_attr.attr,
1088 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001089 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001090 NULL
1091};
1092
Jean Delvare6b9aad22007-07-05 20:37:21 +02001093static const struct attribute_group lm85_group_in4 = {
1094 .attrs = lm85_attributes_in4,
1095};
1096
1097static struct attribute *lm85_attributes_in567[] = {
1098 &sensor_dev_attr_in5_input.dev_attr.attr,
1099 &sensor_dev_attr_in6_input.dev_attr.attr,
1100 &sensor_dev_attr_in7_input.dev_attr.attr,
1101 &sensor_dev_attr_in5_min.dev_attr.attr,
1102 &sensor_dev_attr_in6_min.dev_attr.attr,
1103 &sensor_dev_attr_in7_min.dev_attr.attr,
1104 &sensor_dev_attr_in5_max.dev_attr.attr,
1105 &sensor_dev_attr_in6_max.dev_attr.attr,
1106 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001107 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1108 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1109 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001110 NULL
1111};
1112
1113static const struct attribute_group lm85_group_in567 = {
1114 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001115};
1116
Jean Delvare5f44759472008-06-25 09:10:30 -04001117static void lm85_init_client(struct i2c_client *client)
1118{
1119 int value;
1120
1121 /* Start monitoring if needed */
1122 value = lm85_read_value(client, LM85_REG_CONFIG);
1123 if (!(value & 0x01)) {
1124 dev_info(&client->dev, "Starting monitoring\n");
1125 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1126 }
1127
1128 /* Warn about unusual configuration bits */
1129 if (value & 0x02)
1130 dev_warn(&client->dev, "Device configuration is locked\n");
1131 if (!(value & 0x04))
1132 dev_warn(&client->dev, "Device is not ready\n");
1133}
1134
Jean Delvare67712d02008-10-17 17:51:14 +02001135/* Return 0 if detection is successful, -ENODEV otherwise */
1136static int lm85_detect(struct i2c_client *client, int kind,
1137 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jean Delvare67712d02008-10-17 17:51:14 +02001139 struct i2c_adapter *adapter = client->adapter;
1140 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001141 const char *type_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Jean Delvaree89e22b2008-06-25 08:47:35 -04001143 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001145 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001148 /* If auto-detecting, determine the chip type */
1149 if (kind < 0) {
1150 int company = lm85_read_value(client, LM85_REG_COMPANY);
1151 int verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001153 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1154 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1155 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001157 /* All supported chips have the version in common */
Darrick J. Wongc15ade62009-03-10 12:55:47 -07001158 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1159 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001160 dev_dbg(&adapter->dev, "Autodetection failed: "
1161 "unsupported version\n");
Jean Delvare67712d02008-10-17 17:51:14 +02001162 return -ENODEV;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001163 }
1164 kind = any_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001166 /* Now, refine the detection */
1167 if (company == LM85_COMPANY_NATIONAL) {
1168 switch (verstep) {
1169 case LM85_VERSTEP_LM85C:
1170 kind = lm85c;
1171 break;
1172 case LM85_VERSTEP_LM85B:
1173 kind = lm85b;
1174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001176 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1177 switch (verstep) {
1178 case LM85_VERSTEP_ADM1027:
1179 kind = adm1027;
1180 break;
1181 case LM85_VERSTEP_ADT7463:
1182 case LM85_VERSTEP_ADT7463C:
1183 kind = adt7463;
1184 break;
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001185 case LM85_VERSTEP_ADT7468_1:
1186 case LM85_VERSTEP_ADT7468_2:
1187 kind = adt7468;
1188 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001189 }
1190 } else if (company == LM85_COMPANY_SMSC) {
1191 switch (verstep) {
1192 case LM85_VERSTEP_EMC6D100_A0:
1193 case LM85_VERSTEP_EMC6D100_A1:
1194 /* Note: we can't tell a '100 from a '101 */
1195 kind = emc6d100;
1196 break;
1197 case LM85_VERSTEP_EMC6D102:
1198 kind = emc6d102;
1199 break;
1200 }
1201 } else {
1202 dev_dbg(&adapter->dev, "Autodetection failed: "
1203 "unknown vendor\n");
Jean Delvare67712d02008-10-17 17:51:14 +02001204 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 }
1207
Jean Delvaree89e22b2008-06-25 08:47:35 -04001208 switch (kind) {
1209 case lm85b:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 type_name = "lm85b";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001211 break;
1212 case lm85c:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 type_name = "lm85c";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001214 break;
1215 case adm1027:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 type_name = "adm1027";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001217 break;
1218 case adt7463:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 type_name = "adt7463";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001220 break;
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001221 case adt7468:
1222 type_name = "adt7468";
1223 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001224 case emc6d100:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 type_name = "emc6d100";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001226 break;
1227 case emc6d102:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 type_name = "emc6d102";
Jean Delvaree89e22b2008-06-25 08:47:35 -04001229 break;
1230 default:
1231 type_name = "lm85";
1232 }
Jean Delvare67712d02008-10-17 17:51:14 +02001233 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Jean Delvare67712d02008-10-17 17:51:14 +02001235 return 0;
1236}
1237
1238static int lm85_probe(struct i2c_client *client,
1239 const struct i2c_device_id *id)
1240{
1241 struct lm85_data *data;
1242 int err;
1243
1244 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1245 if (!data)
1246 return -ENOMEM;
1247
1248 i2c_set_clientdata(client, data);
1249 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001250 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jean Delvare67712d02008-10-17 17:51:14 +02001252 /* Fill in the chip specific driver values */
1253 switch (data->type) {
1254 case adm1027:
1255 case adt7463:
1256 case emc6d100:
1257 case emc6d102:
1258 data->freq_map = adm1027_freq_map;
1259 break;
1260 default:
1261 data->freq_map = lm85_freq_map;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001265 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001268 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001271 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1272 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001273 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001275 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001276 as a sixth digital VID input rather than an analog input. */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001277 data->vid = lm85_read_value(client, LM85_REG_VID);
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001278 if (!((data->type == adt7463 || data->type == adt7468) &&
1279 (data->vid & 0x80)))
Jean Delvaree89e22b2008-06-25 08:47:35 -04001280 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001281 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001282 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001283
1284 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001285 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001286 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001287 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001288 goto err_remove_files;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001289
Jean Delvaree89e22b2008-06-25 08:47:35 -04001290 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001291 if (IS_ERR(data->hwmon_dev)) {
1292 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001293 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297
1298 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001299 err_remove_files:
Jean Delvaree89e22b2008-06-25 08:47:35 -04001300 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1301 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
Jean Delvare67712d02008-10-17 17:51:14 +02001302 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001303 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Jean Delvaref9080372008-10-17 17:51:14 +02001304 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return err;
1307}
1308
Jean Delvare67712d02008-10-17 17:51:14 +02001309static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001311 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001312 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001313 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Jean Delvare6b9aad22007-07-05 20:37:21 +02001314 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1315 if (data->type == emc6d100)
1316 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001317 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
1319}
1320
1321
Ben Dooksd8d20612005-10-26 21:05:46 +02001322static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 int res;
1325
1326 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001327 switch (reg) {
1328 case LM85_REG_FAN(0): /* Read WORD data */
1329 case LM85_REG_FAN(1):
1330 case LM85_REG_FAN(2):
1331 case LM85_REG_FAN(3):
1332 case LM85_REG_FAN_MIN(0):
1333 case LM85_REG_FAN_MIN(1):
1334 case LM85_REG_FAN_MIN(2):
1335 case LM85_REG_FAN_MIN(3):
1336 case LM85_REG_ALARM1: /* Read both bytes at once */
1337 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1338 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 default: /* Read BYTE data */
1341 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Jean Delvare1f448092008-04-29 14:03:37 +02001345 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Jean Delvaree89e22b2008-06-25 08:47:35 -04001348static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Jean Delvare1f448092008-04-29 14:03:37 +02001350 switch (reg) {
1351 case LM85_REG_FAN(0): /* Write WORD data */
1352 case LM85_REG_FAN(1):
1353 case LM85_REG_FAN(2):
1354 case LM85_REG_FAN(3):
1355 case LM85_REG_FAN_MIN(0):
1356 case LM85_REG_FAN_MIN(1):
1357 case LM85_REG_FAN_MIN(2):
1358 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001360 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1361 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001364 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369static struct lm85_data *lm85_update_device(struct device *dev)
1370{
1371 struct i2c_client *client = to_i2c_client(dev);
1372 struct lm85_data *data = i2c_get_clientdata(client);
1373 int i;
1374
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001375 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jean Delvare1f448092008-04-29 14:03:37 +02001377 if (!data->valid ||
1378 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* Things that change quickly */
1380 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Have to read extended bits first to "freeze" the
1383 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001384 * There are 2 additional resolution bits per channel and we
1385 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001387 if (data->type == adm1027 || data->type == adt7463 ||
1388 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int ext1 = lm85_read_value(client,
1390 ADM1027_REG_EXTEND_ADC1);
1391 int ext2 = lm85_read_value(client,
1392 ADM1027_REG_EXTEND_ADC2);
1393 int val = (ext1 << 8) + ext2;
1394
Jean Delvare1f448092008-04-29 14:03:37 +02001395 for (i = 0; i <= 4; i++)
1396 data->in_ext[i] =
1397 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Jean Delvare1f448092008-04-29 14:03:37 +02001399 for (i = 0; i <= 2; i++)
1400 data->temp_ext[i] =
1401 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Jean Delvare9c516ef2005-11-26 20:07:54 +01001404 data->vid = lm85_read_value(client, LM85_REG_VID);
1405
1406 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 data->in[i] =
1408 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001409 data->fan[i] =
1410 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001413 if (!((data->type == adt7463 || data->type == adt7468) &&
1414 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001415 data->in[4] = lm85_read_value(client,
1416 LM85_REG_IN(4));
1417 }
1418
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001419 if (data->type == adt7468)
1420 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 for (i = 0; i <= 2; ++i) {
1423 data->temp[i] =
1424 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 data->pwm[i] =
1426 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001427
1428 if (IS_ADT7468_OFF64(data))
1429 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1433
Jean Delvaredd1ac532008-05-01 08:47:33 +02001434 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* Three more voltage sensors */
1436 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001437 data->in[i] = lm85_read_value(client,
1438 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001441 data->alarms |= lm85_read_value(client,
1442 EMC6D100_REG_ALARM3) << 16;
1443 } else if (data->type == emc6d102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Have to read LSB bits after the MSB ones because
1445 the reading of the MSB bits has frozen the
1446 LSBs (backward from the ADM1027).
1447 */
1448 int ext1 = lm85_read_value(client,
1449 EMC6D102_REG_EXTEND_ADC1);
1450 int ext2 = lm85_read_value(client,
1451 EMC6D102_REG_EXTEND_ADC2);
1452 int ext3 = lm85_read_value(client,
1453 EMC6D102_REG_EXTEND_ADC3);
1454 int ext4 = lm85_read_value(client,
1455 EMC6D102_REG_EXTEND_ADC4);
1456 data->in_ext[0] = ext3 & 0x0f;
1457 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001458 data->in_ext[2] = ext4 >> 4;
1459 data->in_ext[3] = ext3 >> 4;
1460 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 data->temp_ext[0] = ext1 & 0x0f;
1463 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001464 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
Jean Delvare1f448092008-04-29 14:03:37 +02001467 data->last_reading = jiffies;
1468 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Jean Delvare1f448092008-04-29 14:03:37 +02001470 if (!data->valid ||
1471 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Things that don't change often */
1473 dev_dbg(&client->dev, "Reading config values\n");
1474
Jean Delvare9c516ef2005-11-26 20:07:54 +01001475 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 data->in_min[i] =
1477 lm85_read_value(client, LM85_REG_IN_MIN(i));
1478 data->in_max[i] =
1479 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001480 data->fan_min[i] =
1481 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001484 if (!((data->type == adt7463 || data->type == adt7468) &&
1485 (data->vid & 0x80))) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001486 data->in_min[4] = lm85_read_value(client,
1487 LM85_REG_IN_MIN(4));
1488 data->in_max[4] = lm85_read_value(client,
1489 LM85_REG_IN_MAX(4));
1490 }
1491
Jean Delvare1f448092008-04-29 14:03:37 +02001492 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001494 data->in_min[i] = lm85_read_value(client,
1495 EMC6D100_REG_IN_MIN(i));
1496 data->in_max[i] = lm85_read_value(client,
1497 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 }
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001502 int val;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 data->temp_min[i] =
1505 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1506 data->temp_max[i] =
1507 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 data->autofan[i].config =
1510 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1511 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001512 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001513 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 data->autofan[i].min_pwm =
1515 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1516 data->zone[i].limit =
1517 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1518 data->zone[i].critical =
1519 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001520
1521 if (IS_ADT7468_OFF64(data)) {
1522 data->temp_min[i] -= 64;
1523 data->temp_max[i] -= 64;
1524 data->zone[i].limit -= 64;
1525 data->zone[i].critical -= 64;
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
1529 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001530 data->autofan[0].min_off = (i & 0x20) != 0;
1531 data->autofan[1].min_off = (i & 0x40) != 0;
1532 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001535 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001536 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001539 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001542 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 data->valid = 1;
1545
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001546 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 return data;
1549}
1550
1551
1552static int __init sm_lm85_init(void)
1553{
1554 return i2c_add_driver(&lm85_driver);
1555}
1556
Jean Delvare1f448092008-04-29 14:03:37 +02001557static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 i2c_del_driver(&lm85_driver);
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001563MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1564 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001565 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1567
1568module_init(sm_lm85_init);
1569module_exit(sm_lm85_exit);