blob: 590226a7c5a2e5d6669cc8412d6d346d7d450c52 [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,
Jan Beulichf065a932011-02-18 03:18:26 -050044 emc6d100, emc6d102, emc6d103
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
133/* IN are scaled acording 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 off_desired; /* Actual "off" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * to prevent "drift" as other autofan control
288 * values change.
289 */
Jean Delvare1f448092008-04-29 14:03:37 +0200290 u8 max_desired; /* Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * to prevent "drift" as other autofan control
292 * values change.
293 */
294};
295
296struct lm85_autofan {
297 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 u8 min_pwm; /* Minimum PWM value, encoded */
299 u8 min_off; /* Min PWM or OFF below "limit", flag */
300};
301
Jean Delvareed6bafb2007-02-14 21:15:03 +0100302/* For each registered chip, we need to keep some data in memory.
303 The structure is dynamically allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700305 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200306 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 enum chips type;
308
Guenter Roeckde248802011-02-25 08:19:42 -0800309 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
310
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100311 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int valid; /* !=0 if following fields are valid */
313 unsigned long last_reading; /* In jiffies */
314 unsigned long last_config; /* In jiffies */
315
316 u8 in[8]; /* Register value */
317 u8 in_max[8]; /* Register value */
318 u8 in_min[8]; /* Register value */
319 s8 temp[3]; /* Register value */
320 s8 temp_min[3]; /* Register value */
321 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 u16 fan[4]; /* Register value */
323 u16 fan_min[4]; /* Register value */
324 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200325 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u8 temp_ext[3]; /* Decoded values */
327 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u8 vid; /* Register value */
329 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800331 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct lm85_autofan autofan[3];
333 struct lm85_zone zone[3];
334};
335
Jean Delvare310ec792009-12-14 21:17:23 +0100336static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200337static int lm85_probe(struct i2c_client *client,
338 const struct i2c_device_id *id);
339static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100341static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400342static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345
Jean Delvare67712d02008-10-17 17:51:14 +0200346static const struct i2c_device_id lm85_id[] = {
347 { "adm1027", adm1027 },
348 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700349 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200350 { "lm85", any_chip },
351 { "lm85b", lm85b },
352 { "lm85c", lm85c },
353 { "emc6d100", emc6d100 },
354 { "emc6d101", emc6d100 },
355 { "emc6d102", emc6d102 },
Jan Beulichf065a932011-02-18 03:18:26 -0500356 { "emc6d103", emc6d103 },
Jean Delvare67712d02008-10-17 17:51:14 +0200357 { }
358};
359MODULE_DEVICE_TABLE(i2c, lm85_id);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200362 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100363 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100364 .name = "lm85",
365 },
Jean Delvare67712d02008-10-17 17:51:14 +0200366 .probe = lm85_probe,
367 .remove = lm85_remove,
368 .id_table = lm85_id,
369 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100370 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
373
374/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200375static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
376 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jean Delvareb353a482007-07-05 20:36:12 +0200378 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200380 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
Jean Delvareb353a482007-07-05 20:36:12 +0200382
383static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
384 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jean Delvareb353a482007-07-05 20:36:12 +0200386 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200388 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
Jean Delvareb353a482007-07-05 20:36:12 +0200390
391static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
392 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Jean Delvareb353a482007-07-05 20:36:12 +0200394 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct i2c_client *client = to_i2c_client(dev);
396 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare63f281a2007-07-05 20:39:40 +0200397 unsigned long val = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100399 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 data->fan_min[nr] = FAN_TO_REG(val);
401 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return count;
404}
405
406#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200407static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
408 show_fan, NULL, offset - 1); \
409static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
410 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412show_fan_offset(1);
413show_fan_offset(2);
414show_fan_offset(3);
415show_fan_offset(4);
416
417/* vid, vrm, alarms */
418
Jean Delvare1f448092008-04-29 14:03:37 +0200419static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
420 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100423 int vid;
424
Guenter Roeckde248802011-02-25 08:19:42 -0800425 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100426 /* 6-pin VID (VRM 10) */
427 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
428 } else {
429 /* 5-pin VID (VRM 9) */
430 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
431 }
432
433 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
437
Jean Delvare1f448092008-04-29 14:03:37 +0200438static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
439 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Jean Delvare90d66192007-10-08 18:24:35 +0200441 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return sprintf(buf, "%ld\n", (long) data->vrm);
443}
444
Jean Delvare1f448092008-04-29 14:03:37 +0200445static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
446 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100448 struct lm85_data *data = dev_get_drvdata(dev);
449 data->vrm = simple_strtoul(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return count;
451}
452
453static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
454
Jean Delvare1f448092008-04-29 14:03:37 +0200455static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
456 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200459 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
463
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200464static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
465 char *buf)
466{
467 int nr = to_sensor_dev_attr(attr)->index;
468 struct lm85_data *data = lm85_update_device(dev);
469 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
470}
471
472static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
473static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
474static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
475static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
476static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
477static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
478static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
479static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
480static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
481static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
482static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
483static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
484static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
485static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
486static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
487static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
488static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/* pwm */
491
Jean Delvareb353a482007-07-05 20:36:12 +0200492static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
493 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jean Delvareb353a482007-07-05 20:36:12 +0200495 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200497 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
Jean Delvareb353a482007-07-05 20:36:12 +0200499
500static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
501 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Jean Delvareb353a482007-07-05 20:36:12 +0200503 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct i2c_client *client = to_i2c_client(dev);
505 struct lm85_data *data = i2c_get_clientdata(client);
506 long val = simple_strtol(buf, NULL, 10);
507
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 data->pwm[nr] = PWM_TO_REG(val);
510 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100511 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return count;
513}
Jean Delvareb353a482007-07-05 20:36:12 +0200514
515static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
516 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Jean Delvareb353a482007-07-05 20:36:12 +0200518 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100520 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100523 switch (pwm_zone) {
524 case -1: /* PWM is always at 100% */
525 enable = 0;
526 break;
527 case 0: /* PWM is always at 0% */
528 case -2: /* PWM responds to manual control */
529 enable = 1;
530 break;
531 default: /* PWM in automatic mode */
532 enable = 2;
533 }
534 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Jean Delvare455f7912007-12-03 23:28:42 +0100537static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
538 *attr, const char *buf, size_t count)
539{
540 int nr = to_sensor_dev_attr(attr)->index;
541 struct i2c_client *client = to_i2c_client(dev);
542 struct lm85_data *data = i2c_get_clientdata(client);
543 long val = simple_strtol(buf, NULL, 10);
544 u8 config;
545
546 switch (val) {
547 case 0:
548 config = 3;
549 break;
550 case 1:
551 config = 7;
552 break;
553 case 2:
554 /* Here we have to choose arbitrarily one of the 5 possible
555 configurations; I go for the safest */
556 config = 6;
557 break;
558 default:
559 return -EINVAL;
560 }
561
562 mutex_lock(&data->update_lock);
563 data->autofan[nr].config = lm85_read_value(client,
564 LM85_REG_AFAN_CONFIG(nr));
565 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
566 | (config << 5);
567 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
568 data->autofan[nr].config);
569 mutex_unlock(&data->update_lock);
570 return count;
571}
572
Jean Delvare34e7dc62008-10-17 17:51:13 +0200573static ssize_t show_pwm_freq(struct device *dev,
574 struct device_attribute *attr, char *buf)
575{
576 int nr = to_sensor_dev_attr(attr)->index;
577 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200578 int freq;
579
580 if (IS_ADT7468_HFPWM(data))
581 freq = 22500;
582 else
583 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
584
585 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200586}
587
588static ssize_t set_pwm_freq(struct device *dev,
589 struct device_attribute *attr, const char *buf, size_t count)
590{
591 int nr = to_sensor_dev_attr(attr)->index;
592 struct i2c_client *client = to_i2c_client(dev);
593 struct lm85_data *data = i2c_get_clientdata(client);
594 long val = simple_strtol(buf, NULL, 10);
595
596 mutex_lock(&data->update_lock);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200597 /* The ADT7468 has a special high-frequency PWM output mode,
598 * where all PWM outputs are driven by a 22.5 kHz clock.
599 * This might confuse the user, but there's not much we can do. */
600 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
601 data->cfg5 &= ~ADT7468_HFPWM;
602 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
603 } else { /* Low freq. mode */
604 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
605 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
606 (data->zone[nr].range << 4)
607 | data->pwm_freq[nr]);
608 if (data->type == adt7468) {
609 data->cfg5 |= ADT7468_HFPWM;
610 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
611 }
612 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200613 mutex_unlock(&data->update_lock);
614 return count;
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200618static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
619 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100620static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200621 show_pwm_enable, set_pwm_enable, offset - 1); \
622static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
623 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625show_pwm_reg(1);
626show_pwm_reg(2);
627show_pwm_reg(3);
628
629/* Voltages */
630
Jean Delvareb353a482007-07-05 20:36:12 +0200631static ssize_t show_in(struct device *dev, struct device_attribute *attr,
632 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jean Delvareb353a482007-07-05 20:36:12 +0200634 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200636 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
637 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
Jean Delvareb353a482007-07-05 20:36:12 +0200639
Jean Delvare1f448092008-04-29 14:03:37 +0200640static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200641 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Jean Delvareb353a482007-07-05 20:36:12 +0200643 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200645 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Jean Delvareb353a482007-07-05 20:36:12 +0200647
648static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
649 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Jean Delvareb353a482007-07-05 20:36:12 +0200651 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct i2c_client *client = to_i2c_client(dev);
653 struct lm85_data *data = i2c_get_clientdata(client);
654 long val = simple_strtol(buf, NULL, 10);
655
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100656 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 data->in_min[nr] = INS_TO_REG(nr, val);
658 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100659 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return count;
661}
Jean Delvareb353a482007-07-05 20:36:12 +0200662
663static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
664 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Jean Delvareb353a482007-07-05 20:36:12 +0200666 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200668 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
Jean Delvareb353a482007-07-05 20:36:12 +0200670
671static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jean Delvareb353a482007-07-05 20:36:12 +0200674 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct i2c_client *client = to_i2c_client(dev);
676 struct lm85_data *data = i2c_get_clientdata(client);
677 long val = simple_strtol(buf, NULL, 10);
678
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100679 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 data->in_max[nr] = INS_TO_REG(nr, val);
681 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100682 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return count;
684}
Jean Delvareb353a482007-07-05 20:36:12 +0200685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200687static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
688 show_in, NULL, offset); \
689static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
690 show_in_min, set_in_min, offset); \
691static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
692 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694show_in_reg(0);
695show_in_reg(1);
696show_in_reg(2);
697show_in_reg(3);
698show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200699show_in_reg(5);
700show_in_reg(6);
701show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703/* Temps */
704
Jean Delvareb353a482007-07-05 20:36:12 +0200705static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
706 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Jean Delvareb353a482007-07-05 20:36:12 +0200708 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200710 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
711 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
Jean Delvareb353a482007-07-05 20:36:12 +0200713
714static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
715 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Jean Delvareb353a482007-07-05 20:36:12 +0200717 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200719 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
Jean Delvareb353a482007-07-05 20:36:12 +0200721
722static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
723 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Jean Delvareb353a482007-07-05 20:36:12 +0200725 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct i2c_client *client = to_i2c_client(dev);
727 struct lm85_data *data = i2c_get_clientdata(client);
728 long val = simple_strtol(buf, NULL, 10);
729
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800730 if (IS_ADT7468_OFF64(data))
731 val += 64;
732
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 data->temp_min[nr] = TEMP_TO_REG(val);
735 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100736 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return count;
738}
Jean Delvareb353a482007-07-05 20:36:12 +0200739
740static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
741 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Jean Delvareb353a482007-07-05 20:36:12 +0200743 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200745 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Jean Delvareb353a482007-07-05 20:36:12 +0200747
748static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Jean Delvareb353a482007-07-05 20:36:12 +0200751 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct i2c_client *client = to_i2c_client(dev);
753 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200754 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800756 if (IS_ADT7468_OFF64(data))
757 val += 64;
758
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100759 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 data->temp_max[nr] = TEMP_TO_REG(val);
761 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100762 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return count;
764}
Jean Delvareb353a482007-07-05 20:36:12 +0200765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200767static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
768 show_temp, NULL, offset - 1); \
769static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
770 show_temp_min, set_temp_min, offset - 1); \
771static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
772 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774show_temp_reg(1);
775show_temp_reg(2);
776show_temp_reg(3);
777
778
779/* Automatic PWM control */
780
Jean Delvareb353a482007-07-05 20:36:12 +0200781static ssize_t show_pwm_auto_channels(struct device *dev,
782 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Jean Delvareb353a482007-07-05 20:36:12 +0200784 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200786 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
Jean Delvareb353a482007-07-05 20:36:12 +0200788
789static ssize_t set_pwm_auto_channels(struct device *dev,
790 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Jean Delvareb353a482007-07-05 20:36:12 +0200792 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct i2c_client *client = to_i2c_client(dev);
794 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare1f448092008-04-29 14:03:37 +0200795 long val = simple_strtol(buf, NULL, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100797 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200799 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
801 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100802 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return count;
804}
Jean Delvareb353a482007-07-05 20:36:12 +0200805
806static ssize_t show_pwm_auto_pwm_min(struct device *dev,
807 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Jean Delvareb353a482007-07-05 20:36:12 +0200809 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200811 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
Jean Delvareb353a482007-07-05 20:36:12 +0200813
814static ssize_t set_pwm_auto_pwm_min(struct device *dev,
815 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jean Delvareb353a482007-07-05 20:36:12 +0200817 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct i2c_client *client = to_i2c_client(dev);
819 struct lm85_data *data = i2c_get_clientdata(client);
820 long val = simple_strtol(buf, NULL, 10);
821
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 data->autofan[nr].min_pwm = PWM_TO_REG(val);
824 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
825 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100826 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return count;
828}
Jean Delvareb353a482007-07-05 20:36:12 +0200829
830static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
831 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Jean Delvareb353a482007-07-05 20:36:12 +0200833 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200835 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
Jean Delvareb353a482007-07-05 20:36:12 +0200837
838static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
839 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Jean Delvareb353a482007-07-05 20:36:12 +0200841 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct i2c_client *client = to_i2c_client(dev);
843 struct lm85_data *data = i2c_get_clientdata(client);
844 long val = simple_strtol(buf, NULL, 10);
Jean Delvare7133e562008-04-12 19:56:35 +0200845 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100847 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200849 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
850 tmp &= ~(0x20 << nr);
851 if (data->autofan[nr].min_off)
852 tmp |= 0x20 << nr;
853 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100854 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return count;
856}
Jean Delvareb353a482007-07-05 20:36:12 +0200857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200859static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
860 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
861 set_pwm_auto_channels, offset - 1); \
862static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
863 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
864 set_pwm_auto_pwm_min, offset - 1); \
865static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
866 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200867 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869pwm_auto(1);
870pwm_auto(2);
871pwm_auto(3);
872
873/* Temperature settings for automatic PWM control */
874
Jean Delvareb353a482007-07-05 20:36:12 +0200875static ssize_t show_temp_auto_temp_off(struct device *dev,
876 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jean Delvareb353a482007-07-05 20:36:12 +0200878 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200880 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 HYST_FROM_REG(data->zone[nr].hyst));
882}
Jean Delvareb353a482007-07-05 20:36:12 +0200883
884static ssize_t set_temp_auto_temp_off(struct device *dev,
885 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Jean Delvareb353a482007-07-05 20:36:12 +0200887 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct i2c_client *client = to_i2c_client(dev);
889 struct lm85_data *data = i2c_get_clientdata(client);
890 int min;
891 long val = simple_strtol(buf, NULL, 10);
892
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100893 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 min = TEMP_FROM_REG(data->zone[nr].limit);
895 data->zone[nr].off_desired = TEMP_TO_REG(val);
896 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200897 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 lm85_write_value(client, LM85_REG_AFAN_HYST1,
899 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200900 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 } else {
902 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200903 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100905 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return count;
907}
Jean Delvareb353a482007-07-05 20:36:12 +0200908
909static ssize_t show_temp_auto_temp_min(struct device *dev,
910 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Jean Delvareb353a482007-07-05 20:36:12 +0200912 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200914 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
Jean Delvareb353a482007-07-05 20:36:12 +0200916
917static ssize_t set_temp_auto_temp_min(struct device *dev,
918 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Jean Delvareb353a482007-07-05 20:36:12 +0200920 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct i2c_client *client = to_i2c_client(dev);
922 struct lm85_data *data = i2c_get_clientdata(client);
923 long val = simple_strtol(buf, NULL, 10);
924
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100925 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 data->zone[nr].limit = TEMP_TO_REG(val);
927 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
928 data->zone[nr].limit);
929
930/* Update temp_auto_max and temp_auto_range */
931 data->zone[nr].range = RANGE_TO_REG(
932 TEMP_FROM_REG(data->zone[nr].max_desired) -
933 TEMP_FROM_REG(data->zone[nr].limit));
934 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
935 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200936 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938/* Update temp_auto_hyst and temp_auto_off */
939 data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
940 data->zone[nr].limit) - TEMP_FROM_REG(
941 data->zone[nr].off_desired));
Jean Delvare1f448092008-04-29 14:03:37 +0200942 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 lm85_write_value(client, LM85_REG_AFAN_HYST1,
944 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200945 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 } else {
947 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200948 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
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_max(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].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 RANGE_FROM_REG(data->zone[nr].range));
961}
Jean Delvareb353a482007-07-05 20:36:12 +0200962
963static ssize_t set_temp_auto_temp_max(struct device *dev,
964 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Jean Delvareb353a482007-07-05 20:36:12 +0200966 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct i2c_client *client = to_i2c_client(dev);
968 struct lm85_data *data = i2c_get_clientdata(client);
969 int min;
970 long val = simple_strtol(buf, NULL, 10);
971
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100972 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 min = TEMP_FROM_REG(data->zone[nr].limit);
974 data->zone[nr].max_desired = TEMP_TO_REG(val);
975 data->zone[nr].range = RANGE_TO_REG(
976 val - min);
977 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
978 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +0200979 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100980 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return count;
982}
Jean Delvareb353a482007-07-05 20:36:12 +0200983
984static ssize_t show_temp_auto_temp_crit(struct device *dev,
985 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Jean Delvareb353a482007-07-05 20:36:12 +0200987 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200989 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
Jean Delvareb353a482007-07-05 20:36:12 +0200991
992static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +0200993 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Jean Delvareb353a482007-07-05 20:36:12 +0200995 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct i2c_client *client = to_i2c_client(dev);
997 struct lm85_data *data = i2c_get_clientdata(client);
998 long val = simple_strtol(buf, NULL, 10);
999
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001000 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 data->zone[nr].critical = TEMP_TO_REG(val);
1002 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1003 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001004 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return count;
1006}
Jean Delvareb353a482007-07-05 20:36:12 +02001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001009static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1010 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1011 set_temp_auto_temp_off, offset - 1); \
1012static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1013 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1014 set_temp_auto_temp_min, offset - 1); \
1015static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1016 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1017 set_temp_auto_temp_max, offset - 1); \
1018static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1019 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1020 set_temp_auto_temp_crit, offset - 1);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022temp_auto(1);
1023temp_auto(2);
1024temp_auto(3);
1025
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001026static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001027 &sensor_dev_attr_fan1_input.dev_attr.attr,
1028 &sensor_dev_attr_fan2_input.dev_attr.attr,
1029 &sensor_dev_attr_fan3_input.dev_attr.attr,
1030 &sensor_dev_attr_fan4_input.dev_attr.attr,
1031 &sensor_dev_attr_fan1_min.dev_attr.attr,
1032 &sensor_dev_attr_fan2_min.dev_attr.attr,
1033 &sensor_dev_attr_fan3_min.dev_attr.attr,
1034 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001035 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1036 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1037 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1038 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001039
1040 &sensor_dev_attr_pwm1.dev_attr.attr,
1041 &sensor_dev_attr_pwm2.dev_attr.attr,
1042 &sensor_dev_attr_pwm3.dev_attr.attr,
1043 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1044 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1045 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001046 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1047 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1048 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001049
1050 &sensor_dev_attr_in0_input.dev_attr.attr,
1051 &sensor_dev_attr_in1_input.dev_attr.attr,
1052 &sensor_dev_attr_in2_input.dev_attr.attr,
1053 &sensor_dev_attr_in3_input.dev_attr.attr,
1054 &sensor_dev_attr_in0_min.dev_attr.attr,
1055 &sensor_dev_attr_in1_min.dev_attr.attr,
1056 &sensor_dev_attr_in2_min.dev_attr.attr,
1057 &sensor_dev_attr_in3_min.dev_attr.attr,
1058 &sensor_dev_attr_in0_max.dev_attr.attr,
1059 &sensor_dev_attr_in1_max.dev_attr.attr,
1060 &sensor_dev_attr_in2_max.dev_attr.attr,
1061 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001062 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1063 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1064 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1065 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001066
1067 &sensor_dev_attr_temp1_input.dev_attr.attr,
1068 &sensor_dev_attr_temp2_input.dev_attr.attr,
1069 &sensor_dev_attr_temp3_input.dev_attr.attr,
1070 &sensor_dev_attr_temp1_min.dev_attr.attr,
1071 &sensor_dev_attr_temp2_min.dev_attr.attr,
1072 &sensor_dev_attr_temp3_min.dev_attr.attr,
1073 &sensor_dev_attr_temp1_max.dev_attr.attr,
1074 &sensor_dev_attr_temp2_max.dev_attr.attr,
1075 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001076 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1077 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1078 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1079 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1080 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001081
1082 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1083 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1084 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1085 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1086 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1087 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1088 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1089 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1090 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001091
1092 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1093 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1094 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1095 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1096 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1097 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1098 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1099 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1100 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1101 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1102 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1103 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1104
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001105 &dev_attr_vrm.attr,
1106 &dev_attr_cpu0_vid.attr,
1107 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001108 NULL
1109};
1110
1111static const struct attribute_group lm85_group = {
1112 .attrs = lm85_attributes,
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. Hoffman0501a382006-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. Hoffman0501a382006-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;
1262 /*
1263 * Registers apparently missing in EMC6D103S/EMC6D103:A2
1264 * compared to EMC6D103:A0, EMC6D103:A1, and EMC6D102
1265 * (according to the data sheets), but used unconditionally
1266 * in the driver: 62[5:7], 6D[0:7], and 6E[0:7].
1267 * So skip EMC6D103S for now.
1268 case LM85_VERSTEP_EMC6D103S:
1269 type_name = "emc6d103s";
1270 break;
1271 */
Jean Delvared42a2eb2009-12-09 20:35:53 +01001272 }
1273 } else {
1274 dev_dbg(&adapter->dev,
1275 "Autodetection failed: unknown vendor\n");
1276 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Jean Delvare67712d02008-10-17 17:51:14 +02001279 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jean Delvare67712d02008-10-17 17:51:14 +02001281 return 0;
1282}
1283
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001284static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1285{
1286 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1287 if (!data->has_vid5)
1288 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1289 if (data->type == emc6d100)
1290 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1291}
1292
Jean Delvare67712d02008-10-17 17:51:14 +02001293static int lm85_probe(struct i2c_client *client,
1294 const struct i2c_device_id *id)
1295{
1296 struct lm85_data *data;
1297 int err;
1298
1299 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1300 if (!data)
1301 return -ENOMEM;
1302
1303 i2c_set_clientdata(client, data);
1304 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001305 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Jean Delvare67712d02008-10-17 17:51:14 +02001307 /* Fill in the chip specific driver values */
1308 switch (data->type) {
1309 case adm1027:
1310 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001311 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001312 case emc6d100:
1313 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001314 case emc6d103:
Jean Delvare67712d02008-10-17 17:51:14 +02001315 data->freq_map = adm1027_freq_map;
1316 break;
1317 default:
1318 data->freq_map = lm85_freq_map;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001322 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001325 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001328 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1329 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001330 goto err_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001332 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
Jean Delvare9c516ef2005-11-26 20:07:54 +01001333 as a sixth digital VID input rather than an analog input. */
Guenter Roeckde248802011-02-25 08:19:42 -08001334 if (data->type == adt7463 || data->type == adt7468) {
1335 u8 vid = lm85_read_value(client, LM85_REG_VID);
1336 if (vid & 0x80)
1337 data->has_vid5 = true;
1338 }
1339
1340 if (!data->has_vid5)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001341 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001342 &lm85_group_in4)))
Jean Delvaref9080372008-10-17 17:51:14 +02001343 goto err_remove_files;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001344
1345 /* The EMC6D100 has 3 additional voltage inputs */
Jean Delvare67712d02008-10-17 17:51:14 +02001346 if (data->type == emc6d100)
Jean Delvaree89e22b2008-06-25 08:47:35 -04001347 if ((err = sysfs_create_group(&client->dev.kobj,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001348 &lm85_group_in567)))
Jean Delvaref9080372008-10-17 17:51:14 +02001349 goto err_remove_files;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001350
Jean Delvaree89e22b2008-06-25 08:47:35 -04001351 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001352 if (IS_ERR(data->hwmon_dev)) {
1353 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001354 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001355 }
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
1358
1359 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001360 err_remove_files:
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001361 lm85_remove_files(client, data);
Jean Delvaref9080372008-10-17 17:51:14 +02001362 err_kfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return err;
1365}
1366
Jean Delvare67712d02008-10-17 17:51:14 +02001367static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001369 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001370 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001371 lm85_remove_files(client, data);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001372 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
1376
Ben Dooksd8d20612005-10-26 21:05:46 +02001377static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int res;
1380
1381 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001382 switch (reg) {
1383 case LM85_REG_FAN(0): /* Read WORD data */
1384 case LM85_REG_FAN(1):
1385 case LM85_REG_FAN(2):
1386 case LM85_REG_FAN(3):
1387 case LM85_REG_FAN_MIN(0):
1388 case LM85_REG_FAN_MIN(1):
1389 case LM85_REG_FAN_MIN(2):
1390 case LM85_REG_FAN_MIN(3):
1391 case LM85_REG_ALARM1: /* Read both bytes at once */
1392 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1393 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 default: /* Read BYTE data */
1396 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Jean Delvare1f448092008-04-29 14:03:37 +02001400 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Jean Delvaree89e22b2008-06-25 08:47:35 -04001403static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Jean Delvare1f448092008-04-29 14:03:37 +02001405 switch (reg) {
1406 case LM85_REG_FAN(0): /* Write WORD data */
1407 case LM85_REG_FAN(1):
1408 case LM85_REG_FAN(2):
1409 case LM85_REG_FAN(3):
1410 case LM85_REG_FAN_MIN(0):
1411 case LM85_REG_FAN_MIN(1):
1412 case LM85_REG_FAN_MIN(2):
1413 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001415 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1416 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001419 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424static struct lm85_data *lm85_update_device(struct device *dev)
1425{
1426 struct i2c_client *client = to_i2c_client(dev);
1427 struct lm85_data *data = i2c_get_clientdata(client);
1428 int i;
1429
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001430 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jean Delvare1f448092008-04-29 14:03:37 +02001432 if (!data->valid ||
1433 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* Things that change quickly */
1435 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* Have to read extended bits first to "freeze" the
1438 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001439 * There are 2 additional resolution bits per channel and we
1440 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001442 if (data->type == adm1027 || data->type == adt7463 ||
1443 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int ext1 = lm85_read_value(client,
1445 ADM1027_REG_EXTEND_ADC1);
1446 int ext2 = lm85_read_value(client,
1447 ADM1027_REG_EXTEND_ADC2);
1448 int val = (ext1 << 8) + ext2;
1449
Jean Delvare1f448092008-04-29 14:03:37 +02001450 for (i = 0; i <= 4; i++)
1451 data->in_ext[i] =
1452 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jean Delvare1f448092008-04-29 14:03:37 +02001454 for (i = 0; i <= 2; i++)
1455 data->temp_ext[i] =
1456 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
Jean Delvare9c516ef2005-11-26 20:07:54 +01001459 data->vid = lm85_read_value(client, LM85_REG_VID);
1460
1461 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 data->in[i] =
1463 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001464 data->fan[i] =
1465 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Guenter Roeckde248802011-02-25 08:19:42 -08001468 if (!data->has_vid5)
1469 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
Jean Delvare9c516ef2005-11-26 20:07:54 +01001470
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001471 if (data->type == adt7468)
1472 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 for (i = 0; i <= 2; ++i) {
1475 data->temp[i] =
1476 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 data->pwm[i] =
1478 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001479
1480 if (IS_ADT7468_OFF64(data))
1481 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1485
Jean Delvaredd1ac532008-05-01 08:47:33 +02001486 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* Three more voltage sensors */
1488 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001489 data->in[i] = lm85_read_value(client,
1490 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001493 data->alarms |= lm85_read_value(client,
1494 EMC6D100_REG_ALARM3) << 16;
Jan Beulichf065a932011-02-18 03:18:26 -05001495 } else if (data->type == emc6d102 || data->type == emc6d103) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* Have to read LSB bits after the MSB ones because
1497 the reading of the MSB bits has frozen the
1498 LSBs (backward from the ADM1027).
1499 */
1500 int ext1 = lm85_read_value(client,
1501 EMC6D102_REG_EXTEND_ADC1);
1502 int ext2 = lm85_read_value(client,
1503 EMC6D102_REG_EXTEND_ADC2);
1504 int ext3 = lm85_read_value(client,
1505 EMC6D102_REG_EXTEND_ADC3);
1506 int ext4 = lm85_read_value(client,
1507 EMC6D102_REG_EXTEND_ADC4);
1508 data->in_ext[0] = ext3 & 0x0f;
1509 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001510 data->in_ext[2] = ext4 >> 4;
1511 data->in_ext[3] = ext3 >> 4;
1512 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 data->temp_ext[0] = ext1 & 0x0f;
1515 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001516 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518
Jean Delvare1f448092008-04-29 14:03:37 +02001519 data->last_reading = jiffies;
1520 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jean Delvare1f448092008-04-29 14:03:37 +02001522 if (!data->valid ||
1523 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* Things that don't change often */
1525 dev_dbg(&client->dev, "Reading config values\n");
1526
Jean Delvare9c516ef2005-11-26 20:07:54 +01001527 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 data->in_min[i] =
1529 lm85_read_value(client, LM85_REG_IN_MIN(i));
1530 data->in_max[i] =
1531 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001532 data->fan_min[i] =
1533 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
Guenter Roeckde248802011-02-25 08:19:42 -08001536 if (!data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001537 data->in_min[4] = lm85_read_value(client,
1538 LM85_REG_IN_MIN(4));
1539 data->in_max[4] = lm85_read_value(client,
1540 LM85_REG_IN_MAX(4));
1541 }
1542
Jean Delvare1f448092008-04-29 14:03:37 +02001543 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001545 data->in_min[i] = lm85_read_value(client,
1546 EMC6D100_REG_IN_MIN(i));
1547 data->in_max[i] = lm85_read_value(client,
1548 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 }
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001553 int val;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 data->temp_min[i] =
1556 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1557 data->temp_max[i] =
1558 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 data->autofan[i].config =
1561 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1562 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001563 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001564 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 data->autofan[i].min_pwm =
1566 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1567 data->zone[i].limit =
1568 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1569 data->zone[i].critical =
1570 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001571
1572 if (IS_ADT7468_OFF64(data)) {
1573 data->temp_min[i] -= 64;
1574 data->temp_max[i] -= 64;
1575 data->zone[i].limit -= 64;
1576 data->zone[i].critical -= 64;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579
1580 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
Jean Delvare1f448092008-04-29 14:03:37 +02001581 data->autofan[0].min_off = (i & 0x20) != 0;
1582 data->autofan[1].min_off = (i & 0x40) != 0;
1583 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001586 data->zone[0].hyst = i >> 4;
Jean Delvare1f448092008-04-29 14:03:37 +02001587 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
Jean Delvaree89e22b2008-06-25 08:47:35 -04001590 data->zone[2].hyst = i >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001593 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 data->valid = 1;
1596
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001597 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 return data;
1600}
1601
1602
1603static int __init sm_lm85_init(void)
1604{
1605 return i2c_add_driver(&lm85_driver);
1606}
1607
Jean Delvare1f448092008-04-29 14:03:37 +02001608static void __exit sm_lm85_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
1610 i2c_del_driver(&lm85_driver);
1611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001614MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1615 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001616 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1618
1619module_init(sm_lm85_init);
1620module_exit(sm_lm85_exit);