blob: da72dc12068c11f85769f93aa57a6370ffa2ffd0 [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,
Guenter Roeck06923f82011-02-19 08:27:47 -080044 emc6d100, emc6d102, emc6d103, emc6d103s
Jean Delvaree5e9f442009-12-14 21:17:27 +010045};
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
Jan Beulichf065a932011-02-18 03:18:26 -050093#define LM85_VERSTEP_EMC6D103_A0 0x68
94#define LM85_VERSTEP_EMC6D103_A1 0x69
95#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#define LM85_REG_CONFIG 0x40
98
99#define LM85_REG_ALARM1 0x41
100#define LM85_REG_ALARM2 0x42
101
102#define LM85_REG_VID 0x43
103
104/* Automated FAN control */
105#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
106#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
107#define LM85_REG_AFAN_SPIKE1 0x62
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
109#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
110#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
111#define LM85_REG_AFAN_HYST1 0x6d
112#define LM85_REG_AFAN_HYST2 0x6e
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define ADM1027_REG_EXTEND_ADC1 0x76
115#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#define EMC6D100_REG_ALARM3 0x7d
118/* IN5, IN6 and IN7 */
Jean Delvare1f448092008-04-29 14:03:37 +0200119#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
120#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
121#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define EMC6D102_REG_EXTEND_ADC1 0x85
123#define EMC6D102_REG_EXTEND_ADC2 0x86
124#define EMC6D102_REG_EXTEND_ADC3 0x87
125#define EMC6D102_REG_EXTEND_ADC4 0x88
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jean Delvare1f448092008-04-29 14:03:37 +0200128/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 variants. Note that you should be a bit careful with which arguments
130 these macros are called: arguments may be evaluated more than once.
131 */
132
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300133/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400134static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200135 2500, 2250, 3300, 5000, 12000,
136 3300, 1500, 1800 /*EMC6D100*/
137};
138#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jean Delvare1f448092008-04-29 14:03:37 +0200140#define INS_TO_REG(n, val) \
141 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvare1f448092008-04-29 14:03:37 +0200143#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200144 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jean Delvare1f448092008-04-29 14:03:37 +0200146#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200149static inline u16 FAN_TO_REG(unsigned long val)
150{
151 if (!val)
152 return 0xffff;
153 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
154}
Jean Delvare1f448092008-04-29 14:03:37 +0200155#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* Temperature is reported in .001 degC increments */
159#define TEMP_TO_REG(val) \
Jean Delvare1f448092008-04-29 14:03:37 +0200160 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
161#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200162 SCALE(((val) << 4) + (ext), 16, 1000)
163#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jean Delvare1f448092008-04-29 14:03:37 +0200165#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define PWM_FROM_REG(val) (val)
167
168
169/* ZONEs have the following parameters:
170 * Limit (low) temp, 1. degC
171 * Hysteresis (below limit), 1. degC (0-15)
172 * Range of speed control, .1 degC (2-80)
173 * Critical (high) temp, 1. degC
174 *
175 * FAN PWMs have the following parameters:
176 * Reference Zone, 1, 2, 3, etc.
177 * Spinup time, .05 sec
178 * PWM value at limit/low temp, 1 count
179 * PWM Frequency, 1. Hz
180 * PWM is Min or OFF below limit, flag
181 * Invert PWM output, flag
182 *
183 * Some chips filter the temp, others the fan.
184 * Filter constant (or disabled) .1 seconds
185 */
186
187/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400188static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200189 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
190 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
191};
192
193static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 int i;
196
Jean Delvared38b1492008-04-03 10:40:39 +0200197 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200198 for (i = 0; i < 15; ++i) {
199 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Jean Delvared38b1492008-04-03 10:40:39 +0200202
Jean Delvare1b92ada2008-10-17 17:51:14 +0200203 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Jean Delvare1f448092008-04-29 14:03:37 +0200205#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200208static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200209 10, 15, 23, 30, 38, 47, 61, 94
210};
211static const int adm1027_freq_map[8] = { /* 1 Hz */
212 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200213};
214
Jean Delvare8a0795d2008-10-17 17:51:14 +0200215static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int i;
218
Jean Delvare86010c92008-10-17 17:51:13 +0200219 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200220 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200221 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200222 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400223 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200225
226static int FREQ_FROM_REG(const int *map, u8 reg)
227{
228 return map[reg & 0x07];
229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/* Since we can't use strings, I'm abusing these numbers
232 * to stand in for the following meanings:
233 * 1 -- PWM responds to Zone 1
234 * 2 -- PWM responds to Zone 2
235 * 3 -- PWM responds to Zone 3
236 * 23 -- PWM responds to the higher temp of Zone 2 or 3
237 * 123 -- PWM responds to highest of Zone 1, 2, or 3
238 * 0 -- PWM is always at 0% (ie, off)
239 * -1 -- PWM is always at 100%
240 * -2 -- PWM responds to manual control
241 */
242
Jean Delvaree89e22b2008-06-25 08:47:35 -0400243static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
244#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Jean Delvare1f448092008-04-29 14:03:37 +0200246static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int i;
249
Jean Delvare1f448092008-04-29 14:03:37 +0200250 for (i = 0; i <= 7; ++i)
251 if (zone == lm85_zone_map[i])
252 break;
253 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400255 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Jean Delvare1f448092008-04-29 14:03:37 +0200258#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
259#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/* Chip sampling rates
262 *
263 * Some sensors are not updated more frequently than once per second
264 * so it doesn't make sense to read them more often than that.
265 * We cache the results and return the saved data if the driver
266 * is called again before a second has elapsed.
267 *
268 * Also, there is significant configuration data for this chip
269 * given the automatic PWM fan control that is possible. There
270 * are about 47 bytes of config data to only 22 bytes of actual
271 * readings. So, we keep the config data up to date in the cache
272 * when it is written and only sample it once every 1 *minute*
273 */
274#define LM85_DATA_INTERVAL (HZ + HZ / 2)
275#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/* LM85 can automatically adjust fan speeds based on temperature
278 * This structure encapsulates an entire Zone config. There are
279 * three zones (one for each temperature input) on the lm85
280 */
281struct lm85_zone {
282 s8 limit; /* Low temp limit */
283 u8 hyst; /* Low limit hysteresis. (0-15) */
284 u8 range; /* Temp range, encoded */
285 s8 critical; /* "All fans ON" temp limit */
Jean Delvare1f448092008-04-29 14:03:37 +0200286 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * to prevent "drift" as other autofan control
288 * values change.
289 */
290};
291
292struct lm85_autofan {
293 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 u8 min_pwm; /* Minimum PWM value, encoded */
295 u8 min_off; /* Min PWM or OFF below "limit", flag */
296};
297
Jean Delvareed6bafb2007-02-14 21:15:03 +0100298/* For each registered chip, we need to keep some data in memory.
299 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700301 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200302 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 enum chips type;
304
Guenter Roeckde248802011-02-25 08:19:42 -0800305 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
306
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100307 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int valid; /* !=0 if following fields are valid */
309 unsigned long last_reading; /* In jiffies */
310 unsigned long last_config; /* In jiffies */
311
312 u8 in[8]; /* Register value */
313 u8 in_max[8]; /* Register value */
314 u8 in_min[8]; /* Register value */
315 s8 temp[3]; /* Register value */
316 s8 temp_min[3]; /* Register value */
317 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 u16 fan[4]; /* Register value */
319 u16 fan_min[4]; /* Register value */
320 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200321 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 u8 temp_ext[3]; /* Decoded values */
323 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 u8 vid; /* Register value */
325 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800327 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct lm85_autofan autofan[3];
329 struct lm85_zone zone[3];
330};
331
Jean Delvare310ec792009-12-14 21:17:23 +0100332static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200333static int lm85_probe(struct i2c_client *client,
334 const struct i2c_device_id *id);
335static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100337static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400338static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341
Jean Delvare67712d02008-10-17 17:51:14 +0200342static const struct i2c_device_id lm85_id[] = {
343 { "adm1027", adm1027 },
344 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700345 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200346 { "lm85", any_chip },
347 { "lm85b", lm85b },
348 { "lm85c", lm85c },
349 { "emc6d100", emc6d100 },
350 { "emc6d101", emc6d100 },
351 { "emc6d102", emc6d102 },
Jan Beulichf065a932011-02-18 03:18:26 -0500352 { "emc6d103", emc6d103 },
Guenter Roeck06923f82011-02-19 08:27:47 -0800353 { "emc6d103s", emc6d103s },
Jean Delvare67712d02008-10-17 17:51:14 +0200354 { }
355};
356MODULE_DEVICE_TABLE(i2c, lm85_id);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200359 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100360 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100361 .name = "lm85",
362 },
Jean Delvare67712d02008-10-17 17:51:14 +0200363 .probe = lm85_probe,
364 .remove = lm85_remove,
365 .id_table = lm85_id,
366 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100367 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
370
371/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200372static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
373 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jean Delvareb353a482007-07-05 20:36:12 +0200375 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200377 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Jean Delvareb353a482007-07-05 20:36:12 +0200379
380static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
381 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvareb353a482007-07-05 20:36:12 +0200383 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200385 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Jean Delvareb353a482007-07-05 20:36:12 +0200387
388static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Jean Delvareb353a482007-07-05 20:36:12 +0200391 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct i2c_client *client = to_i2c_client(dev);
393 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200394 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100396 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 data->fan_min[nr] = FAN_TO_REG(val);
398 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return count;
401}
402
403#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200404static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
405 show_fan, NULL, offset - 1); \
406static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
407 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409show_fan_offset(1);
410show_fan_offset(2);
411show_fan_offset(3);
412show_fan_offset(4);
413
414/* vid, vrm, alarms */
415
Jean Delvare1f448092008-04-29 14:03:37 +0200416static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
417 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100420 int vid;
421
Guenter Roeckde248802011-02-25 08:19:42 -0800422 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100423 /* 6-pin VID (VRM 10) */
424 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
425 } else {
426 /* 5-pin VID (VRM 9) */
427 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
428 }
429
430 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
434
Jean Delvare1f448092008-04-29 14:03:37 +0200435static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
436 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare90d66192007-10-08 18:24:35 +0200438 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return sprintf(buf, "%ld\n", (long) data->vrm);
440}
441
Jean Delvare1f448092008-04-29 14:03:37 +0200442static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
443 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100445 struct lm85_data *data = dev_get_drvdata(dev);
446 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return count;
448}
449
450static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
451
Jean Delvare1f448092008-04-29 14:03:37 +0200452static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
453 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200456 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
460
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200461static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
462 char *buf)
463{
464 int nr = to_sensor_dev_attr(attr)->index;
465 struct lm85_data *data = lm85_update_device(dev);
466 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
467}
468
469static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
470static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
471static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
472static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
473static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
474static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
475static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
476static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
477static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
478static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
479static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
480static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
481static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
482static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
483static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
484static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
485static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/* pwm */
488
Jean Delvareb353a482007-07-05 20:36:12 +0200489static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
490 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jean Delvareb353a482007-07-05 20:36:12 +0200492 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200494 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
Jean Delvareb353a482007-07-05 20:36:12 +0200496
497static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jean Delvareb353a482007-07-05 20:36:12 +0200500 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct i2c_client *client = to_i2c_client(dev);
502 struct lm85_data *data = i2c_get_clientdata(client);
503 long val = simple_strtol(buf, NULL, 10);
504
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100505 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 data->pwm[nr] = PWM_TO_REG(val);
507 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return count;
510}
Jean Delvareb353a482007-07-05 20:36:12 +0200511
512static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
513 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jean Delvareb353a482007-07-05 20:36:12 +0200515 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100517 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100520 switch (pwm_zone) {
521 case -1: /* PWM is always at 100% */
522 enable = 0;
523 break;
524 case 0: /* PWM is always at 0% */
525 case -2: /* PWM responds to manual control */
526 enable = 1;
527 break;
528 default: /* PWM in automatic mode */
529 enable = 2;
530 }
531 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Jean Delvare455f7912007-12-03 23:28:42 +0100534static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
535 *attr, const char *buf, size_t count)
536{
537 int nr = to_sensor_dev_attr(attr)->index;
538 struct i2c_client *client = to_i2c_client(dev);
539 struct lm85_data *data = i2c_get_clientdata(client);
540 long val = simple_strtol(buf, NULL, 10);
541 u8 config;
542
543 switch (val) {
544 case 0:
545 config = 3;
546 break;
547 case 1:
548 config = 7;
549 break;
550 case 2:
551 /* Here we have to choose arbitrarily one of the 5 possible
552 configurations; I go for the safest */
553 config = 6;
554 break;
555 default:
556 return -EINVAL;
557 }
558
559 mutex_lock(&data->update_lock);
560 data->autofan[nr].config = lm85_read_value(client,
561 LM85_REG_AFAN_CONFIG(nr));
562 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
563 | (config << 5);
564 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
565 data->autofan[nr].config);
566 mutex_unlock(&data->update_lock);
567 return count;
568}
569
Jean Delvare34e7dc62008-10-17 17:51:13 +0200570static ssize_t show_pwm_freq(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 int nr = to_sensor_dev_attr(attr)->index;
574 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200575 int freq;
576
577 if (IS_ADT7468_HFPWM(data))
578 freq = 22500;
579 else
580 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
581
582 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200583}
584
585static ssize_t set_pwm_freq(struct device *dev,
586 struct device_attribute *attr, const char *buf, size_t count)
587{
588 int nr = to_sensor_dev_attr(attr)->index;
589 struct i2c_client *client = to_i2c_client(dev);
590 struct lm85_data *data = i2c_get_clientdata(client);
591 long val = simple_strtol(buf, NULL, 10);
592
593 mutex_lock(&data->update_lock);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200594 /* The ADT7468 has a special high-frequency PWM output mode,
595 * where all PWM outputs are driven by a 22.5 kHz clock.
596 * This might confuse the user, but there's not much we can do. */
597 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
598 data->cfg5 &= ~ADT7468_HFPWM;
599 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
600 } else { /* Low freq. mode */
601 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
602 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
603 (data->zone[nr].range << 4)
604 | data->pwm_freq[nr]);
605 if (data->type == adt7468) {
606 data->cfg5 |= ADT7468_HFPWM;
607 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
608 }
609 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200610 mutex_unlock(&data->update_lock);
611 return count;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200615static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
616 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100617static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200618 show_pwm_enable, set_pwm_enable, offset - 1); \
619static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
620 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622show_pwm_reg(1);
623show_pwm_reg(2);
624show_pwm_reg(3);
625
626/* Voltages */
627
Jean Delvareb353a482007-07-05 20:36:12 +0200628static ssize_t show_in(struct device *dev, struct device_attribute *attr,
629 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Jean Delvareb353a482007-07-05 20:36:12 +0200631 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200633 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
634 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Jean Delvareb353a482007-07-05 20:36:12 +0200636
Jean Delvare1f448092008-04-29 14:03:37 +0200637static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200638 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Jean Delvareb353a482007-07-05 20:36:12 +0200640 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200642 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
Jean Delvareb353a482007-07-05 20:36:12 +0200644
645static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
646 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jean Delvareb353a482007-07-05 20:36:12 +0200648 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct i2c_client *client = to_i2c_client(dev);
650 struct lm85_data *data = i2c_get_clientdata(client);
651 long val = simple_strtol(buf, NULL, 10);
652
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100653 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 data->in_min[nr] = INS_TO_REG(nr, val);
655 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100656 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return count;
658}
Jean Delvareb353a482007-07-05 20:36:12 +0200659
660static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
661 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Jean Delvareb353a482007-07-05 20:36:12 +0200663 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200665 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Jean Delvareb353a482007-07-05 20:36:12 +0200667
668static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
669 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Jean Delvareb353a482007-07-05 20:36:12 +0200671 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct i2c_client *client = to_i2c_client(dev);
673 struct lm85_data *data = i2c_get_clientdata(client);
674 long val = simple_strtol(buf, NULL, 10);
675
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100676 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 data->in_max[nr] = INS_TO_REG(nr, val);
678 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100679 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return count;
681}
Jean Delvareb353a482007-07-05 20:36:12 +0200682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200684static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
685 show_in, NULL, offset); \
686static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
687 show_in_min, set_in_min, offset); \
688static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
689 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691show_in_reg(0);
692show_in_reg(1);
693show_in_reg(2);
694show_in_reg(3);
695show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200696show_in_reg(5);
697show_in_reg(6);
698show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700/* Temps */
701
Jean Delvareb353a482007-07-05 20:36:12 +0200702static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
703 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Jean Delvareb353a482007-07-05 20:36:12 +0200705 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200707 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
708 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Jean Delvareb353a482007-07-05 20:36:12 +0200710
711static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
712 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Jean Delvareb353a482007-07-05 20:36:12 +0200714 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200716 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
Jean Delvareb353a482007-07-05 20:36:12 +0200718
719static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
720 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Jean Delvareb353a482007-07-05 20:36:12 +0200722 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct i2c_client *client = to_i2c_client(dev);
724 struct lm85_data *data = i2c_get_clientdata(client);
725 long val = simple_strtol(buf, NULL, 10);
726
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800727 if (IS_ADT7468_OFF64(data))
728 val += 64;
729
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 data->temp_min[nr] = TEMP_TO_REG(val);
732 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return count;
735}
Jean Delvareb353a482007-07-05 20:36:12 +0200736
737static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
738 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jean Delvareb353a482007-07-05 20:36:12 +0200740 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200742 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Jean Delvareb353a482007-07-05 20:36:12 +0200744
745static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Jean Delvareb353a482007-07-05 20:36:12 +0200748 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct i2c_client *client = to_i2c_client(dev);
750 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200751 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800753 if (IS_ADT7468_OFF64(data))
754 val += 64;
755
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100756 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 data->temp_max[nr] = TEMP_TO_REG(val);
758 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100759 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return count;
761}
Jean Delvareb353a482007-07-05 20:36:12 +0200762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200764static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
765 show_temp, NULL, offset - 1); \
766static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
767 show_temp_min, set_temp_min, offset - 1); \
768static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
769 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771show_temp_reg(1);
772show_temp_reg(2);
773show_temp_reg(3);
774
775
776/* Automatic PWM control */
777
Jean Delvareb353a482007-07-05 20:36:12 +0200778static ssize_t show_pwm_auto_channels(struct device *dev,
779 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Jean Delvareb353a482007-07-05 20:36:12 +0200781 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200783 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
Jean Delvareb353a482007-07-05 20:36:12 +0200785
786static ssize_t set_pwm_auto_channels(struct device *dev,
787 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Jean Delvareb353a482007-07-05 20:36:12 +0200789 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct i2c_client *client = to_i2c_client(dev);
791 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200792 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100794 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200796 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
798 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100799 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return count;
801}
Jean Delvareb353a482007-07-05 20:36:12 +0200802
803static ssize_t show_pwm_auto_pwm_min(struct device *dev,
804 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Jean Delvareb353a482007-07-05 20:36:12 +0200806 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200808 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
Jean Delvareb353a482007-07-05 20:36:12 +0200810
811static ssize_t set_pwm_auto_pwm_min(struct device *dev,
812 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Jean Delvareb353a482007-07-05 20:36:12 +0200814 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 struct i2c_client *client = to_i2c_client(dev);
816 struct lm85_data *data = i2c_get_clientdata(client);
817 long val = simple_strtol(buf, NULL, 10);
818
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100819 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 data->autofan[nr].min_pwm = PWM_TO_REG(val);
821 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
822 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100823 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return count;
825}
Jean Delvareb353a482007-07-05 20:36:12 +0200826
827static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
828 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Jean Delvareb353a482007-07-05 20:36:12 +0200830 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200832 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
Jean Delvareb353a482007-07-05 20:36:12 +0200834
835static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
836 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Jean Delvareb353a482007-07-05 20:36:12 +0200838 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct i2c_client *client = to_i2c_client(dev);
840 struct lm85_data *data = i2c_get_clientdata(client);
841 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200842 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100844 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200846 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
847 tmp &= ~(0x20 << nr);
848 if (data->autofan[nr].min_off)
849 tmp |= 0x20 << nr;
850 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100851 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return count;
853}
Jean Delvareb353a482007-07-05 20:36:12 +0200854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200856static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
857 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
858 set_pwm_auto_channels, offset - 1); \
859static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
860 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
861 set_pwm_auto_pwm_min, offset - 1); \
862static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
863 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200864 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866pwm_auto(1);
867pwm_auto(2);
868pwm_auto(3);
869
870/* Temperature settings for automatic PWM control */
871
Jean Delvareb353a482007-07-05 20:36:12 +0200872static ssize_t show_temp_auto_temp_off(struct device *dev,
873 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jean Delvareb353a482007-07-05 20:36:12 +0200875 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200877 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 HYST_FROM_REG(data->zone[nr].hyst));
879}
Jean Delvareb353a482007-07-05 20:36:12 +0200880
881static ssize_t set_temp_auto_temp_off(struct device *dev,
882 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Jean Delvareb353a482007-07-05 20:36:12 +0200884 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct i2c_client *client = to_i2c_client(dev);
886 struct lm85_data *data = i2c_get_clientdata(client);
887 int min;
888 long val = simple_strtol(buf, NULL, 10);
889
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200893 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 lm85_write_value(client, LM85_REG_AFAN_HYST1,
895 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200896 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 } else {
898 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200899 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100901 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return count;
903}
Jean Delvareb353a482007-07-05 20:36:12 +0200904
905static ssize_t show_temp_auto_temp_min(struct device *dev,
906 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Jean Delvareb353a482007-07-05 20:36:12 +0200908 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200910 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
Jean Delvareb353a482007-07-05 20:36:12 +0200912
913static ssize_t set_temp_auto_temp_min(struct device *dev,
914 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Jean Delvareb353a482007-07-05 20:36:12 +0200916 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct i2c_client *client = to_i2c_client(dev);
918 struct lm85_data *data = i2c_get_clientdata(client);
919 long val = simple_strtol(buf, NULL, 10);
920
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100921 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 data->zone[nr].limit = TEMP_TO_REG(val);
923 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
924 data->zone[nr].limit);
925
926/* Update temp_auto_max and temp_auto_range */
927 data->zone[nr].range = RANGE_TO_REG(
928 TEMP_FROM_REG(data->zone[nr].max_desired) -
929 TEMP_FROM_REG(data->zone[nr].limit));
930 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
931 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200932 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100934 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return count;
936}
Jean Delvareb353a482007-07-05 20:36:12 +0200937
938static ssize_t show_temp_auto_temp_max(struct device *dev,
939 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Jean Delvareb353a482007-07-05 20:36:12 +0200941 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200943 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 RANGE_FROM_REG(data->zone[nr].range));
945}
Jean Delvareb353a482007-07-05 20:36:12 +0200946
947static ssize_t set_temp_auto_temp_max(struct device *dev,
948 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Jean Delvareb353a482007-07-05 20:36:12 +0200950 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct i2c_client *client = to_i2c_client(dev);
952 struct lm85_data *data = i2c_get_clientdata(client);
953 int min;
954 long val = simple_strtol(buf, NULL, 10);
955
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100956 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 min = TEMP_FROM_REG(data->zone[nr].limit);
958 data->zone[nr].max_desired = TEMP_TO_REG(val);
959 data->zone[nr].range = RANGE_TO_REG(
960 val - min);
961 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
962 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200963 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100964 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return count;
966}
Jean Delvareb353a482007-07-05 20:36:12 +0200967
968static ssize_t show_temp_auto_temp_crit(struct device *dev,
969 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Jean Delvareb353a482007-07-05 20:36:12 +0200971 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200973 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
Jean Delvareb353a482007-07-05 20:36:12 +0200975
976static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200977 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Jean Delvareb353a482007-07-05 20:36:12 +0200979 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 struct i2c_client *client = to_i2c_client(dev);
981 struct lm85_data *data = i2c_get_clientdata(client);
982 long val = simple_strtol(buf, NULL, 10);
983
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100984 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 data->zone[nr].critical = TEMP_TO_REG(val);
986 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
987 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100988 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return count;
990}
Jean Delvareb353a482007-07-05 20:36:12 +0200991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200993static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
994 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
995 set_temp_auto_temp_off, offset - 1); \
996static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
997 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
998 set_temp_auto_temp_min, offset - 1); \
999static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1000 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1001 set_temp_auto_temp_max, offset - 1); \
1002static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1003 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1004 set_temp_auto_temp_crit, offset - 1);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006temp_auto(1);
1007temp_auto(2);
1008temp_auto(3);
1009
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001010static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001011 &sensor_dev_attr_fan1_input.dev_attr.attr,
1012 &sensor_dev_attr_fan2_input.dev_attr.attr,
1013 &sensor_dev_attr_fan3_input.dev_attr.attr,
1014 &sensor_dev_attr_fan4_input.dev_attr.attr,
1015 &sensor_dev_attr_fan1_min.dev_attr.attr,
1016 &sensor_dev_attr_fan2_min.dev_attr.attr,
1017 &sensor_dev_attr_fan3_min.dev_attr.attr,
1018 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001019 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1020 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1021 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1022 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001023
1024 &sensor_dev_attr_pwm1.dev_attr.attr,
1025 &sensor_dev_attr_pwm2.dev_attr.attr,
1026 &sensor_dev_attr_pwm3.dev_attr.attr,
1027 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1028 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1029 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001030 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1031 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1032 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001033
1034 &sensor_dev_attr_in0_input.dev_attr.attr,
1035 &sensor_dev_attr_in1_input.dev_attr.attr,
1036 &sensor_dev_attr_in2_input.dev_attr.attr,
1037 &sensor_dev_attr_in3_input.dev_attr.attr,
1038 &sensor_dev_attr_in0_min.dev_attr.attr,
1039 &sensor_dev_attr_in1_min.dev_attr.attr,
1040 &sensor_dev_attr_in2_min.dev_attr.attr,
1041 &sensor_dev_attr_in3_min.dev_attr.attr,
1042 &sensor_dev_attr_in0_max.dev_attr.attr,
1043 &sensor_dev_attr_in1_max.dev_attr.attr,
1044 &sensor_dev_attr_in2_max.dev_attr.attr,
1045 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001046 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1047 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1048 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1049 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001050
1051 &sensor_dev_attr_temp1_input.dev_attr.attr,
1052 &sensor_dev_attr_temp2_input.dev_attr.attr,
1053 &sensor_dev_attr_temp3_input.dev_attr.attr,
1054 &sensor_dev_attr_temp1_min.dev_attr.attr,
1055 &sensor_dev_attr_temp2_min.dev_attr.attr,
1056 &sensor_dev_attr_temp3_min.dev_attr.attr,
1057 &sensor_dev_attr_temp1_max.dev_attr.attr,
1058 &sensor_dev_attr_temp2_max.dev_attr.attr,
1059 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001060 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1061 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1062 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1063 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1064 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001065
1066 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1067 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1068 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1069 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1070 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1071 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001072
Jean Delvareb353a482007-07-05 20:36:12 +02001073 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1074 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1075 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1076 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1077 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1078 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1079 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1080 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1081 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1082
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001083 &dev_attr_vrm.attr,
1084 &dev_attr_cpu0_vid.attr,
1085 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001086 NULL
1087};
1088
1089static const struct attribute_group lm85_group = {
1090 .attrs = lm85_attributes,
1091};
1092
Guenter Roeck06923f82011-02-19 08:27:47 -08001093static struct attribute *lm85_attributes_minctl[] = {
1094 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1095 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1096 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001097 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001098};
1099
1100static const struct attribute_group lm85_group_minctl = {
1101 .attrs = lm85_attributes_minctl,
1102};
1103
1104static struct attribute *lm85_attributes_temp_off[] = {
1105 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1106 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1107 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001108 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001109};
1110
1111static const struct attribute_group lm85_group_temp_off = {
1112 .attrs = lm85_attributes_temp_off,
1113};
1114
Jean Delvare6b9aad22007-07-05 20:37:21 +02001115static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001116 &sensor_dev_attr_in4_input.dev_attr.attr,
1117 &sensor_dev_attr_in4_min.dev_attr.attr,
1118 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001119 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001120 NULL
1121};
1122
Jean Delvare6b9aad22007-07-05 20:37:21 +02001123static const struct attribute_group lm85_group_in4 = {
1124 .attrs = lm85_attributes_in4,
1125};
1126
1127static struct attribute *lm85_attributes_in567[] = {
1128 &sensor_dev_attr_in5_input.dev_attr.attr,
1129 &sensor_dev_attr_in6_input.dev_attr.attr,
1130 &sensor_dev_attr_in7_input.dev_attr.attr,
1131 &sensor_dev_attr_in5_min.dev_attr.attr,
1132 &sensor_dev_attr_in6_min.dev_attr.attr,
1133 &sensor_dev_attr_in7_min.dev_attr.attr,
1134 &sensor_dev_attr_in5_max.dev_attr.attr,
1135 &sensor_dev_attr_in6_max.dev_attr.attr,
1136 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001137 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1138 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1139 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001140 NULL
1141};
1142
1143static const struct attribute_group lm85_group_in567 = {
1144 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001145};
1146
Jean Delvare5f44759472008-06-25 09:10:30 -04001147static void lm85_init_client(struct i2c_client *client)
1148{
1149 int value;
1150
1151 /* Start monitoring if needed */
1152 value = lm85_read_value(client, LM85_REG_CONFIG);
1153 if (!(value & 0x01)) {
1154 dev_info(&client->dev, "Starting monitoring\n");
1155 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1156 }
1157
1158 /* Warn about unusual configuration bits */
1159 if (value & 0x02)
1160 dev_warn(&client->dev, "Device configuration is locked\n");
1161 if (!(value & 0x04))
1162 dev_warn(&client->dev, "Device is not ready\n");
1163}
1164
Jean Delvare5cfaf332009-09-15 17:18:14 +02001165static int lm85_is_fake(struct i2c_client *client)
1166{
1167 /*
1168 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1169 * emulate the former except that it has no hardware monitoring function
1170 * so the readings are always 0.
1171 */
1172 int i;
1173 u8 in_temp, fan;
1174
1175 for (i = 0; i < 8; i++) {
1176 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1177 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1178 if (in_temp != 0x00 || fan != 0xff)
1179 return 0;
1180 }
1181
1182 return 1;
1183}
1184
Jean Delvare67712d02008-10-17 17:51:14 +02001185/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001186static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Jean Delvare67712d02008-10-17 17:51:14 +02001188 struct i2c_adapter *adapter = client->adapter;
1189 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001190 const char *type_name;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001191 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Jean Delvaree89e22b2008-06-25 08:47:35 -04001193 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001195 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jean Delvared42a2eb2009-12-09 20:35:53 +01001198 /* Determine the chip type */
1199 company = lm85_read_value(client, LM85_REG_COMPANY);
1200 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jean Delvared42a2eb2009-12-09 20:35:53 +01001202 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1203 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1204 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jean Delvared42a2eb2009-12-09 20:35:53 +01001206 /* All supported chips have the version in common */
1207 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1208 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1209 dev_dbg(&adapter->dev,
1210 "Autodetection failed: unsupported version\n");
1211 return -ENODEV;
1212 }
1213 type_name = "lm85";
1214
1215 /* Now, refine the detection */
1216 if (company == LM85_COMPANY_NATIONAL) {
1217 switch (verstep) {
1218 case LM85_VERSTEP_LM85C:
1219 type_name = "lm85c";
1220 break;
1221 case LM85_VERSTEP_LM85B:
1222 type_name = "lm85b";
1223 break;
1224 case LM85_VERSTEP_LM96000_1:
1225 case LM85_VERSTEP_LM96000_2:
1226 /* Check for Winbond WPCD377I */
1227 if (lm85_is_fake(client)) {
1228 dev_dbg(&adapter->dev,
1229 "Found Winbond WPCD377I, ignoring\n");
1230 return -ENODEV;
1231 }
1232 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001233 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001234 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1235 switch (verstep) {
1236 case LM85_VERSTEP_ADM1027:
1237 type_name = "adm1027";
1238 break;
1239 case LM85_VERSTEP_ADT7463:
1240 case LM85_VERSTEP_ADT7463C:
1241 type_name = "adt7463";
1242 break;
1243 case LM85_VERSTEP_ADT7468_1:
1244 case LM85_VERSTEP_ADT7468_2:
1245 type_name = "adt7468";
1246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001248 } else if (company == LM85_COMPANY_SMSC) {
1249 switch (verstep) {
1250 case LM85_VERSTEP_EMC6D100_A0:
1251 case LM85_VERSTEP_EMC6D100_A1:
1252 /* Note: we can't tell a '100 from a '101 */
1253 type_name = "emc6d100";
1254 break;
1255 case LM85_VERSTEP_EMC6D102:
1256 type_name = "emc6d102";
1257 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001258 case LM85_VERSTEP_EMC6D103_A0:
1259 case LM85_VERSTEP_EMC6D103_A1:
1260 type_name = "emc6d103";
1261 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001262 case LM85_VERSTEP_EMC6D103S:
1263 type_name = "emc6d103s";
1264 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001265 }
1266 } else {
1267 dev_dbg(&adapter->dev,
1268 "Autodetection failed: unknown vendor\n");
1269 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Jean Delvare67712d02008-10-17 17:51:14 +02001272 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Jean Delvare67712d02008-10-17 17:51:14 +02001274 return 0;
1275}
1276
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001277static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1278{
1279 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Guenter Roeck06923f82011-02-19 08:27:47 -08001280 if (data->type != emc6d103s) {
1281 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1282 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1283 }
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001284 if (!data->has_vid5)
1285 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1286 if (data->type == emc6d100)
1287 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1288}
1289
Jean Delvare67712d02008-10-17 17:51:14 +02001290static int lm85_probe(struct i2c_client *client,
1291 const struct i2c_device_id *id)
1292{
1293 struct lm85_data *data;
1294 int err;
1295
1296 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1297 if (!data)
1298 return -ENOMEM;
1299
1300 i2c_set_clientdata(client, data);
1301 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001302 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jean Delvare67712d02008-10-17 17:51:14 +02001304 /* Fill in the chip specific driver values */
1305 switch (data->type) {
1306 case adm1027:
1307 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001308 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001309 case emc6d100:
1310 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001311 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001312 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001313 data->freq_map = adm1027_freq_map;
1314 break;
1315 default:
1316 data->freq_map = lm85_freq_map;
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001320 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001323 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001326 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1327 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001328 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Guenter Roeck06923f82011-02-19 08:27:47 -08001330 /* minctl and temp_off exist on all chips except emc6d103s */
1331 if (data->type != emc6d103s) {
1332 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1333 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001334 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001335 err = sysfs_create_group(&client->dev.kobj,
1336 &lm85_group_temp_off);
1337 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001338 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001339 }
1340
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001341 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001342 as a sixth digital VID input rather than an analog input. */
Guenter Roeckde248802011-02-25 08:19:42 -08001343 if (data->type == adt7463 || data->type == adt7468) {
1344 u8 vid = lm85_read_value(client, LM85_REG_VID);
1345 if (vid & 0x80)
1346 data->has_vid5 = true;
1347 }
1348
1349 if (!data->has_vid5)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001350 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001351 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001352 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001353
1354 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001355 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001356 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001357 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001358 goto err_remove_files;
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001359
Jean Delvaree89e22b2008-06-25 08:47:35 -04001360 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001361 if (IS_ERR(data->hwmon_dev)) {
1362 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001363 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001364 }
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367
1368 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001369 err_remove_files:
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001370 lm85_remove_files(client, data);
Jean Delvaref9080372008-10-17 17:51:14 +02001371 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return err;
1374}
1375
Jean Delvare67712d02008-10-17 17:51:14 +02001376static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001378 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001379 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001380 lm85_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001381 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383}
1384
1385
Ben Dooksd8d20612005-10-26 21:05:46 +02001386static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int res;
1389
1390 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001391 switch (reg) {
1392 case LM85_REG_FAN(0): /* Read WORD data */
1393 case LM85_REG_FAN(1):
1394 case LM85_REG_FAN(2):
1395 case LM85_REG_FAN(3):
1396 case LM85_REG_FAN_MIN(0):
1397 case LM85_REG_FAN_MIN(1):
1398 case LM85_REG_FAN_MIN(2):
1399 case LM85_REG_FAN_MIN(3):
1400 case LM85_REG_ALARM1: /* Read both bytes at once */
1401 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1402 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 default: /* Read BYTE data */
1405 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
Jean Delvare1f448092008-04-29 14:03:37 +02001409 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Jean Delvaree89e22b2008-06-25 08:47:35 -04001412static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Jean Delvare1f448092008-04-29 14:03:37 +02001414 switch (reg) {
1415 case LM85_REG_FAN(0): /* Write WORD data */
1416 case LM85_REG_FAN(1):
1417 case LM85_REG_FAN(2):
1418 case LM85_REG_FAN(3):
1419 case LM85_REG_FAN_MIN(0):
1420 case LM85_REG_FAN_MIN(1):
1421 case LM85_REG_FAN_MIN(2):
1422 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001424 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1425 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001428 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433static struct lm85_data *lm85_update_device(struct device *dev)
1434{
1435 struct i2c_client *client = to_i2c_client(dev);
1436 struct lm85_data *data = i2c_get_clientdata(client);
1437 int i;
1438
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001439 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jean Delvare1f448092008-04-29 14:03:37 +02001441 if (!data->valid ||
1442 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Things that change quickly */
1444 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* Have to read extended bits first to "freeze" the
1447 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001448 * There are 2 additional resolution bits per channel and we
1449 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001451 if (data->type == adm1027 || data->type == adt7463 ||
1452 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int ext1 = lm85_read_value(client,
1454 ADM1027_REG_EXTEND_ADC1);
1455 int ext2 = lm85_read_value(client,
1456 ADM1027_REG_EXTEND_ADC2);
1457 int val = (ext1 << 8) + ext2;
1458
Jean Delvare1f448092008-04-29 14:03:37 +02001459 for (i = 0; i <= 4; i++)
1460 data->in_ext[i] =
1461 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Jean Delvare1f448092008-04-29 14:03:37 +02001463 for (i = 0; i <= 2; i++)
1464 data->temp_ext[i] =
1465 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Jean Delvare9c516ef2005-11-26 20:07:54 +01001468 data->vid = lm85_read_value(client, LM85_REG_VID);
1469
1470 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 data->in[i] =
1472 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001473 data->fan[i] =
1474 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476
Guenter Roeckde248802011-02-25 08:19:42 -08001477 if (!data->has_vid5)
1478 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
Jean Delvare9c516ef2005-11-26 20:07:54 +01001479
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001480 if (data->type == adt7468)
1481 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 for (i = 0; i <= 2; ++i) {
1484 data->temp[i] =
1485 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 data->pwm[i] =
1487 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001488
1489 if (IS_ADT7468_OFF64(data))
1490 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492
1493 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1494
Jean Delvaredd1ac532008-05-01 08:47:33 +02001495 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* Three more voltage sensors */
1497 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001498 data->in[i] = lm85_read_value(client,
1499 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001502 data->alarms |= lm85_read_value(client,
1503 EMC6D100_REG_ALARM3) << 16;
Guenter Roeck06923f82011-02-19 08:27:47 -08001504 } else if (data->type == emc6d102 || data->type == emc6d103 ||
1505 data->type == emc6d103s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /* Have to read LSB bits after the MSB ones because
1507 the reading of the MSB bits has frozen the
1508 LSBs (backward from the ADM1027).
1509 */
1510 int ext1 = lm85_read_value(client,
1511 EMC6D102_REG_EXTEND_ADC1);
1512 int ext2 = lm85_read_value(client,
1513 EMC6D102_REG_EXTEND_ADC2);
1514 int ext3 = lm85_read_value(client,
1515 EMC6D102_REG_EXTEND_ADC3);
1516 int ext4 = lm85_read_value(client,
1517 EMC6D102_REG_EXTEND_ADC4);
1518 data->in_ext[0] = ext3 & 0x0f;
1519 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001520 data->in_ext[2] = ext4 >> 4;
1521 data->in_ext[3] = ext3 >> 4;
1522 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 data->temp_ext[0] = ext1 & 0x0f;
1525 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001526 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
Jean Delvare1f448092008-04-29 14:03:37 +02001529 data->last_reading = jiffies;
1530 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Jean Delvare1f448092008-04-29 14:03:37 +02001532 if (!data->valid ||
1533 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Things that don't change often */
1535 dev_dbg(&client->dev, "Reading config values\n");
1536
Jean Delvare9c516ef2005-11-26 20:07:54 +01001537 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 data->in_min[i] =
1539 lm85_read_value(client, LM85_REG_IN_MIN(i));
1540 data->in_max[i] =
1541 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001542 data->fan_min[i] =
1543 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Guenter Roeckde248802011-02-25 08:19:42 -08001546 if (!data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001547 data->in_min[4] = lm85_read_value(client,
1548 LM85_REG_IN_MIN(4));
1549 data->in_max[4] = lm85_read_value(client,
1550 LM85_REG_IN_MAX(4));
1551 }
1552
Jean Delvare1f448092008-04-29 14:03:37 +02001553 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001555 data->in_min[i] = lm85_read_value(client,
1556 EMC6D100_REG_IN_MIN(i));
1557 data->in_max[i] = lm85_read_value(client,
1558 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 }
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001563 int val;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 data->temp_min[i] =
1566 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1567 data->temp_max[i] =
1568 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 data->autofan[i].config =
1571 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1572 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001573 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001574 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 data->autofan[i].min_pwm =
1576 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1577 data->zone[i].limit =
1578 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1579 data->zone[i].critical =
1580 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001581
1582 if (IS_ADT7468_OFF64(data)) {
1583 data->temp_min[i] -= 64;
1584 data->temp_max[i] -= 64;
1585 data->zone[i].limit -= 64;
1586 data->zone[i].critical -= 64;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589
Guenter Roeck06923f82011-02-19 08:27:47 -08001590 if (data->type != emc6d103s) {
1591 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1592 data->autofan[0].min_off = (i & 0x20) != 0;
1593 data->autofan[1].min_off = (i & 0x40) != 0;
1594 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Guenter Roeck06923f82011-02-19 08:27:47 -08001596 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1597 data->zone[0].hyst = i >> 4;
1598 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Guenter Roeck06923f82011-02-19 08:27:47 -08001600 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1601 data->zone[2].hyst = i >> 4;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001605 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 data->valid = 1;
1608
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001609 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 return data;
1612}
1613
1614
1615static int __init sm_lm85_init(void)
1616{
1617 return i2c_add_driver(&lm85_driver);
1618}
1619
Jean Delvare1f448092008-04-29 14:03:37 +02001620static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 i2c_del_driver(&lm85_driver);
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001626MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1627 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001628 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1630
1631module_init(sm_lm85_init);
1632module_exit(sm_lm85_exit);