blob: b0129a54e1a6b13a36170b672a97f7ae86f52d21 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck09770b22012-01-14 20:47:36 -08002 * lm85.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5 * 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 Delvare590e8532014-06-11 18:35:56 +02008 * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de>
Guenter Roeck09770b22012-01-14 20:47:36 -08009 *
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
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 {
Jean Delvare590e8532014-06-11 18:35:56 +020042 lm85,
Jean Delvaree5e9f442009-12-14 21:17:27 +010043 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
Guenter Roeck09770b22012-01-14 20:47:36 -080049#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Guenter Roeck09770b22012-01-14 20:47:36 -080053#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* Fan speeds are LSB, MSB (2 bytes) */
Guenter Roeck09770b22012-01-14 20:47:36 -080058#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
Guenter Roeck09770b22012-01-14 20:47:36 -080061#define LM85_REG_PWM(nr) (0x30 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Guenter Roeck09770b22012-01-14 20:47:36 -080063#define LM85_REG_COMPANY 0x3e
64#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080065
Guenter Roeck09770b22012-01-14 20:47:36 -080066#define ADT7468_REG_CFG5 0x7c
67#define ADT7468_OFF64 (1 << 0)
68#define ADT7468_HFPWM (1 << 1)
69#define IS_ADT7468_OFF64(data) \
Darrick J. Wong79b92f22008-11-12 13:26:59 -080070 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
Guenter Roeck09770b22012-01-14 20:47:36 -080071#define IS_ADT7468_HFPWM(data) \
Jean Delvaref6c61cf2010-10-28 20:31:50 +020072 ((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 */
Guenter Roeck09770b22012-01-14 20:47:36 -080075#define LM85_COMPANY_NATIONAL 0x01
76#define LM85_COMPANY_ANALOG_DEV 0x41
77#define LM85_COMPANY_SMSC 0x5c
Guenter Roeck09770b22012-01-14 20:47:36 -080078#define LM85_VERSTEP_LM85C 0x60
79#define LM85_VERSTEP_LM85B 0x62
80#define LM85_VERSTEP_LM96000_1 0x68
81#define LM85_VERSTEP_LM96000_2 0x69
82#define LM85_VERSTEP_ADM1027 0x60
83#define LM85_VERSTEP_ADT7463 0x62
84#define LM85_VERSTEP_ADT7463C 0x6A
85#define LM85_VERSTEP_ADT7468_1 0x71
86#define LM85_VERSTEP_ADT7468_2 0x72
87#define LM85_VERSTEP_EMC6D100_A0 0x60
88#define LM85_VERSTEP_EMC6D100_A1 0x61
89#define LM85_VERSTEP_EMC6D102 0x65
90#define LM85_VERSTEP_EMC6D103_A0 0x68
91#define LM85_VERSTEP_EMC6D103_A1 0x69
92#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Guenter Roeck09770b22012-01-14 20:47:36 -080094#define LM85_REG_CONFIG 0x40
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Guenter Roeck09770b22012-01-14 20:47:36 -080096#define LM85_REG_ALARM1 0x41
97#define LM85_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Guenter Roeck09770b22012-01-14 20:47:36 -080099#define LM85_REG_VID 0x43
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/* Automated FAN control */
Guenter Roeck09770b22012-01-14 20:47:36 -0800102#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
103#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
104#define LM85_REG_AFAN_SPIKE1 0x62
105#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
106#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
107#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
108#define LM85_REG_AFAN_HYST1 0x6d
109#define LM85_REG_AFAN_HYST2 0x6e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Guenter Roeck09770b22012-01-14 20:47:36 -0800111#define ADM1027_REG_EXTEND_ADC1 0x76
112#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#define EMC6D100_REG_ALARM3 0x7d
115/* IN5, IN6 and IN7 */
Guenter Roeck09770b22012-01-14 20:47:36 -0800116#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
117#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
118#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
119#define EMC6D102_REG_EXTEND_ADC1 0x85
120#define EMC6D102_REG_EXTEND_ADC2 0x86
121#define EMC6D102_REG_EXTEND_ADC3 0x87
122#define EMC6D102_REG_EXTEND_ADC4 0x88
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Guenter Roeck09770b22012-01-14 20:47:36 -0800125/*
126 * Conversions. Rounding and limit checking is only done on the TO_REG
127 * variants. Note that you should be a bit careful with which arguments
128 * these macros are called: arguments may be evaluated more than once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
130
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300131/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400132static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200133 2500, 2250, 3300, 5000, 12000,
134 3300, 1500, 1800 /*EMC6D100*/
135};
136#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jean Delvare1f448092008-04-29 14:03:37 +0200138#define INS_TO_REG(n, val) \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800139 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jean Delvare1f448092008-04-29 14:03:37 +0200141#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200142 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jean Delvare1f448092008-04-29 14:03:37 +0200144#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200147static inline u16 FAN_TO_REG(unsigned long val)
148{
149 if (!val)
150 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800151 return clamp_val(5400000 / val, 1, 0xfffe);
Jean Delvare63f281a2007-07-05 20:39:40 +0200152}
Jean Delvare1f448092008-04-29 14:03:37 +0200153#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
154 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/* Temperature is reported in .001 degC increments */
157#define TEMP_TO_REG(val) \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800158 clamp_val(SCALE(val, 1000, 1), -127, 127)
Jean Delvare1f448092008-04-29 14:03:37 +0200159#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200160 SCALE(((val) << 4) + (ext), 16, 1000)
161#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Guenter Roeck2a844c12013-01-09 08:09:34 -0800163#define PWM_TO_REG(val) clamp_val(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define PWM_FROM_REG(val) (val)
165
166
Guenter Roeck09770b22012-01-14 20:47:36 -0800167/*
168 * ZONEs have the following parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Limit (low) temp, 1. degC
170 * Hysteresis (below limit), 1. degC (0-15)
171 * Range of speed control, .1 degC (2-80)
172 * Critical (high) temp, 1. degC
173 *
174 * FAN PWMs have the following parameters:
175 * Reference Zone, 1, 2, 3, etc.
176 * Spinup time, .05 sec
177 * PWM value at limit/low temp, 1 count
178 * PWM Frequency, 1. Hz
179 * PWM is Min or OFF below limit, flag
180 * Invert PWM output, flag
181 *
182 * Some chips filter the temp, others the fan.
183 * Filter constant (or disabled) .1 seconds
184 */
185
186/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400187static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200188 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
189 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
190};
191
192static int RANGE_TO_REG(int range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int i;
195
Jean Delvared38b1492008-04-03 10:40:39 +0200196 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200197 for (i = 0; i < 15; ++i) {
198 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Jean Delvared38b1492008-04-03 10:40:39 +0200201
Jean Delvare1b92ada2008-10-17 17:51:14 +0200202 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
Jean Delvare1f448092008-04-29 14:03:37 +0200204#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200207static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200208 10, 15, 23, 30, 38, 47, 61, 94
209};
210static const int adm1027_freq_map[8] = { /* 1 Hz */
211 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200212};
213
Jean Delvare8a0795d2008-10-17 17:51:14 +0200214static int FREQ_TO_REG(const int *map, int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int i;
217
Jean Delvare86010c92008-10-17 17:51:13 +0200218 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200219 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200220 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200221 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400222 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200224
225static int FREQ_FROM_REG(const int *map, u8 reg)
226{
227 return map[reg & 0x07];
228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Guenter Roeck09770b22012-01-14 20:47:36 -0800230/*
231 * Since we can't use strings, I'm abusing these numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * 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
Guenter Roeck2a844c12013-01-09 08:09:34 -0800258#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
Jean Delvare1f448092008-04-29 14:03:37 +0200259#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Guenter Roeck09770b22012-01-14 20:47:36 -0800261/*
262 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
264 * Some sensors are not updated more frequently than once per second
265 * so it doesn't make sense to read them more often than that.
266 * We cache the results and return the saved data if the driver
267 * is called again before a second has elapsed.
268 *
269 * Also, there is significant configuration data for this chip
270 * given the automatic PWM fan control that is possible. There
271 * are about 47 bytes of config data to only 22 bytes of actual
272 * readings. So, we keep the config data up to date in the cache
273 * when it is written and only sample it once every 1 *minute*
274 */
275#define LM85_DATA_INTERVAL (HZ + HZ / 2)
276#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
277
Guenter Roeck09770b22012-01-14 20:47:36 -0800278/*
279 * LM85 can automatically adjust fan speeds based on temperature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * This structure encapsulates an entire Zone config. There are
281 * three zones (one for each temperature input) on the lm85
282 */
283struct lm85_zone {
284 s8 limit; /* Low temp limit */
285 u8 hyst; /* Low limit hysteresis. (0-15) */
286 u8 range; /* Temp range, encoded */
287 s8 critical; /* "All fans ON" temp limit */
Guenter Roeck09770b22012-01-14 20:47:36 -0800288 u8 max_desired; /*
289 * Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * to prevent "drift" as other autofan control
291 * values change.
292 */
293};
294
295struct lm85_autofan {
296 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 u8 min_pwm; /* Minimum PWM value, encoded */
298 u8 min_off; /* Min PWM or OFF below "limit", flag */
299};
300
Guenter Roeck09770b22012-01-14 20:47:36 -0800301/*
302 * For each registered chip, we need to keep some data in memory.
303 * The structure is dynamically allocated.
304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700306 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200307 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 enum chips type;
309
Guenter Roeckde248802011-02-25 08:19:42 -0800310 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
311
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int valid; /* !=0 if following fields are valid */
314 unsigned long last_reading; /* In jiffies */
315 unsigned long last_config; /* In jiffies */
316
317 u8 in[8]; /* Register value */
318 u8 in_max[8]; /* Register value */
319 u8 in_min[8]; /* Register value */
320 s8 temp[3]; /* Register value */
321 s8 temp_min[3]; /* Register value */
322 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 u16 fan[4]; /* Register value */
324 u16 fan_min[4]; /* Register value */
325 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200326 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 u8 temp_ext[3]; /* Decoded values */
328 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 u8 vid; /* Register value */
330 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800332 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct lm85_autofan autofan[3];
334 struct lm85_zone zone[3];
335};
336
Jean Delvare310ec792009-12-14 21:17:23 +0100337static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200338static int lm85_probe(struct i2c_client *client,
339 const struct i2c_device_id *id);
340static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100342static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400343static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346
Jean Delvare67712d02008-10-17 17:51:14 +0200347static const struct i2c_device_id lm85_id[] = {
348 { "adm1027", adm1027 },
349 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700350 { "adt7468", adt7468 },
Jean Delvare590e8532014-06-11 18:35:56 +0200351 { "lm85", lm85 },
352 { "lm85b", lm85 },
353 { "lm85c", lm85 },
Jean Delvare67712d02008-10-17 17:51:14 +0200354 { "emc6d100", emc6d100 },
355 { "emc6d101", emc6d100 },
356 { "emc6d102", emc6d102 },
Jan Beulichf065a932011-02-18 03:18:26 -0500357 { "emc6d103", emc6d103 },
Guenter Roeck06923f82011-02-19 08:27:47 -0800358 { "emc6d103s", emc6d103s },
Jean Delvare67712d02008-10-17 17:51:14 +0200359 { }
360};
361MODULE_DEVICE_TABLE(i2c, lm85_id);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200364 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100365 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100366 .name = "lm85",
367 },
Jean Delvare67712d02008-10-17 17:51:14 +0200368 .probe = lm85_probe,
369 .remove = lm85_remove,
370 .id_table = lm85_id,
371 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100372 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375
376/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200377static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
378 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Jean Delvareb353a482007-07-05 20:36:12 +0200380 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200382 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Jean Delvareb353a482007-07-05 20:36:12 +0200384
385static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
386 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jean Delvareb353a482007-07-05 20:36:12 +0200388 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200390 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Jean Delvareb353a482007-07-05 20:36:12 +0200392
393static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Jean Delvareb353a482007-07-05 20:36:12 +0200396 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct i2c_client *client = to_i2c_client(dev);
398 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800399 unsigned long val;
400 int err;
401
402 err = kstrtoul(buf, 10, &val);
403 if (err)
404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100406 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 data->fan_min[nr] = FAN_TO_REG(val);
408 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100409 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return count;
411}
412
413#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200414static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
415 show_fan, NULL, offset - 1); \
416static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
417 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419show_fan_offset(1);
420show_fan_offset(2);
421show_fan_offset(3);
422show_fan_offset(4);
423
424/* vid, vrm, alarms */
425
Jean Delvare1f448092008-04-29 14:03:37 +0200426static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
427 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100430 int vid;
431
Guenter Roeckde248802011-02-25 08:19:42 -0800432 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100433 /* 6-pin VID (VRM 10) */
434 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
435 } else {
436 /* 5-pin VID (VRM 9) */
437 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
438 }
439
440 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
444
Jean Delvare1f448092008-04-29 14:03:37 +0200445static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
446 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Jean Delvare90d66192007-10-08 18:24:35 +0200448 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return sprintf(buf, "%ld\n", (long) data->vrm);
450}
451
Jean Delvare1f448092008-04-29 14:03:37 +0200452static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
453 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100455 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b22012-01-14 20:47:36 -0800456 unsigned long val;
457 int err;
458
459 err = kstrtoul(buf, 10, &val);
460 if (err)
461 return err;
462
463 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return count;
465}
466
467static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
468
Jean Delvare1f448092008-04-29 14:03:37 +0200469static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
470 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200473 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
477
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200478static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
479 char *buf)
480{
481 int nr = to_sensor_dev_attr(attr)->index;
482 struct lm85_data *data = lm85_update_device(dev);
483 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
484}
485
486static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
487static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
488static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
489static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
490static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
491static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
492static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
493static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
494static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
495static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
496static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
497static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
498static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
499static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
500static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
501static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
502static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/* pwm */
505
Jean Delvareb353a482007-07-05 20:36:12 +0200506static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
507 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jean Delvareb353a482007-07-05 20:36:12 +0200509 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200511 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
Jean Delvareb353a482007-07-05 20:36:12 +0200513
514static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jean Delvareb353a482007-07-05 20:36:12 +0200517 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct i2c_client *client = to_i2c_client(dev);
519 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800520 unsigned long val;
521 int err;
522
523 err = kstrtoul(buf, 10, &val);
524 if (err)
525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100527 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 data->pwm[nr] = PWM_TO_REG(val);
529 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100530 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return count;
532}
Jean Delvareb353a482007-07-05 20:36:12 +0200533
534static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
535 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Jean Delvareb353a482007-07-05 20:36:12 +0200537 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100539 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100542 switch (pwm_zone) {
543 case -1: /* PWM is always at 100% */
544 enable = 0;
545 break;
546 case 0: /* PWM is always at 0% */
547 case -2: /* PWM responds to manual control */
548 enable = 1;
549 break;
550 default: /* PWM in automatic mode */
551 enable = 2;
552 }
553 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Jean Delvare455f7912007-12-03 23:28:42 +0100556static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
557 *attr, const char *buf, size_t count)
558{
559 int nr = to_sensor_dev_attr(attr)->index;
560 struct i2c_client *client = to_i2c_client(dev);
561 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare455f7912007-12-03 23:28:42 +0100562 u8 config;
Guenter Roeck09770b22012-01-14 20:47:36 -0800563 unsigned long val;
564 int err;
565
566 err = kstrtoul(buf, 10, &val);
567 if (err)
568 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100569
570 switch (val) {
571 case 0:
572 config = 3;
573 break;
574 case 1:
575 config = 7;
576 break;
577 case 2:
Guenter Roeck09770b22012-01-14 20:47:36 -0800578 /*
579 * Here we have to choose arbitrarily one of the 5 possible
580 * configurations; I go for the safest
581 */
Jean Delvare455f7912007-12-03 23:28:42 +0100582 config = 6;
583 break;
584 default:
585 return -EINVAL;
586 }
587
588 mutex_lock(&data->update_lock);
589 data->autofan[nr].config = lm85_read_value(client,
590 LM85_REG_AFAN_CONFIG(nr));
591 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
592 | (config << 5);
593 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
594 data->autofan[nr].config);
595 mutex_unlock(&data->update_lock);
596 return count;
597}
598
Jean Delvare34e7dc62008-10-17 17:51:13 +0200599static ssize_t show_pwm_freq(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
602 int nr = to_sensor_dev_attr(attr)->index;
603 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200604 int freq;
605
606 if (IS_ADT7468_HFPWM(data))
607 freq = 22500;
608 else
609 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
610
611 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200612}
613
614static ssize_t set_pwm_freq(struct device *dev,
615 struct device_attribute *attr, const char *buf, size_t count)
616{
617 int nr = to_sensor_dev_attr(attr)->index;
618 struct i2c_client *client = to_i2c_client(dev);
619 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800620 unsigned long val;
621 int err;
622
623 err = kstrtoul(buf, 10, &val);
624 if (err)
625 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200626
627 mutex_lock(&data->update_lock);
Guenter Roeck09770b22012-01-14 20:47:36 -0800628 /*
629 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200630 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b22012-01-14 20:47:36 -0800631 * This might confuse the user, but there's not much we can do.
632 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200633 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
634 data->cfg5 &= ~ADT7468_HFPWM;
635 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
636 } else { /* Low freq. mode */
637 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
638 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
639 (data->zone[nr].range << 4)
640 | data->pwm_freq[nr]);
641 if (data->type == adt7468) {
642 data->cfg5 |= ADT7468_HFPWM;
643 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
644 }
645 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200646 mutex_unlock(&data->update_lock);
647 return count;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200651static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
652 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100653static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200654 show_pwm_enable, set_pwm_enable, offset - 1); \
655static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
656 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658show_pwm_reg(1);
659show_pwm_reg(2);
660show_pwm_reg(3);
661
662/* Voltages */
663
Jean Delvareb353a482007-07-05 20:36:12 +0200664static ssize_t show_in(struct device *dev, struct device_attribute *attr,
665 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Jean Delvareb353a482007-07-05 20:36:12 +0200667 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200669 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
670 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Jean Delvareb353a482007-07-05 20:36:12 +0200672
Jean Delvare1f448092008-04-29 14:03:37 +0200673static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200674 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jean Delvareb353a482007-07-05 20:36:12 +0200676 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200678 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
Jean Delvareb353a482007-07-05 20:36:12 +0200680
681static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
682 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Jean Delvareb353a482007-07-05 20:36:12 +0200684 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct i2c_client *client = to_i2c_client(dev);
686 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800687 long val;
688 int err;
689
690 err = kstrtol(buf, 10, &val);
691 if (err)
692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100694 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 data->in_min[nr] = INS_TO_REG(nr, val);
696 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100697 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return count;
699}
Jean Delvareb353a482007-07-05 20:36:12 +0200700
701static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
702 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jean Delvareb353a482007-07-05 20:36:12 +0200704 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200706 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
Jean Delvareb353a482007-07-05 20:36:12 +0200708
709static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
710 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jean Delvareb353a482007-07-05 20:36:12 +0200712 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct i2c_client *client = to_i2c_client(dev);
714 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800715 long val;
716 int err;
717
718 err = kstrtol(buf, 10, &val);
719 if (err)
720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100722 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 data->in_max[nr] = INS_TO_REG(nr, val);
724 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100725 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return count;
727}
Jean Delvareb353a482007-07-05 20:36:12 +0200728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200730static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
731 show_in, NULL, offset); \
732static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
733 show_in_min, set_in_min, offset); \
734static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
735 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737show_in_reg(0);
738show_in_reg(1);
739show_in_reg(2);
740show_in_reg(3);
741show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200742show_in_reg(5);
743show_in_reg(6);
744show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746/* Temps */
747
Jean Delvareb353a482007-07-05 20:36:12 +0200748static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
749 char *buf)
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 lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200753 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
754 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
Jean Delvareb353a482007-07-05 20:36:12 +0200756
757static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
758 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jean Delvareb353a482007-07-05 20:36:12 +0200760 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200762 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
Jean Delvareb353a482007-07-05 20:36:12 +0200764
765static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
766 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Jean Delvareb353a482007-07-05 20:36:12 +0200768 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct i2c_client *client = to_i2c_client(dev);
770 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800771 long val;
772 int err;
773
774 err = kstrtol(buf, 10, &val);
775 if (err)
776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800778 if (IS_ADT7468_OFF64(data))
779 val += 64;
780
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100781 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 data->temp_min[nr] = TEMP_TO_REG(val);
783 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100784 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return count;
786}
Jean Delvareb353a482007-07-05 20:36:12 +0200787
788static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
789 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Jean Delvareb353a482007-07-05 20:36:12 +0200791 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200793 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
Jean Delvareb353a482007-07-05 20:36:12 +0200795
796static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
797 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jean Delvareb353a482007-07-05 20:36:12 +0200799 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct i2c_client *client = to_i2c_client(dev);
801 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800802 long val;
803 int err;
804
805 err = kstrtol(buf, 10, &val);
806 if (err)
807 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800809 if (IS_ADT7468_OFF64(data))
810 val += 64;
811
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100812 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 data->temp_max[nr] = TEMP_TO_REG(val);
814 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100815 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return count;
817}
Jean Delvareb353a482007-07-05 20:36:12 +0200818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200820static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
821 show_temp, NULL, offset - 1); \
822static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
823 show_temp_min, set_temp_min, offset - 1); \
824static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
825 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827show_temp_reg(1);
828show_temp_reg(2);
829show_temp_reg(3);
830
831
832/* Automatic PWM control */
833
Jean Delvareb353a482007-07-05 20:36:12 +0200834static ssize_t show_pwm_auto_channels(struct device *dev,
835 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jean Delvareb353a482007-07-05 20:36:12 +0200837 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200839 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
Jean Delvareb353a482007-07-05 20:36:12 +0200841
842static ssize_t set_pwm_auto_channels(struct device *dev,
843 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Jean Delvareb353a482007-07-05 20:36:12 +0200845 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct i2c_client *client = to_i2c_client(dev);
847 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800848 long val;
849 int err;
850
851 err = kstrtol(buf, 10, &val);
852 if (err)
853 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100855 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200857 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
859 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100860 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return count;
862}
Jean Delvareb353a482007-07-05 20:36:12 +0200863
864static ssize_t show_pwm_auto_pwm_min(struct device *dev,
865 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Jean Delvareb353a482007-07-05 20:36:12 +0200867 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200869 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Jean Delvareb353a482007-07-05 20:36:12 +0200871
872static ssize_t set_pwm_auto_pwm_min(struct device *dev,
873 struct device_attribute *attr, const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
877 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800878 unsigned long val;
879 int err;
880
881 err = kstrtoul(buf, 10, &val);
882 if (err)
883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100885 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 data->autofan[nr].min_pwm = PWM_TO_REG(val);
887 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
888 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100889 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return count;
891}
Jean Delvareb353a482007-07-05 20:36:12 +0200892
893static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
894 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jean Delvareb353a482007-07-05 20:36:12 +0200896 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200898 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
Jean Delvareb353a482007-07-05 20:36:12 +0200900
901static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
902 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jean Delvareb353a482007-07-05 20:36:12 +0200904 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct i2c_client *client = to_i2c_client(dev);
906 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare7133e562008-04-12 19:56:35 +0200907 u8 tmp;
Guenter Roeck09770b22012-01-14 20:47:36 -0800908 long val;
909 int err;
910
911 err = kstrtol(buf, 10, &val);
912 if (err)
913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100915 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200917 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
918 tmp &= ~(0x20 << nr);
919 if (data->autofan[nr].min_off)
920 tmp |= 0x20 << nr;
921 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100922 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return count;
924}
Jean Delvareb353a482007-07-05 20:36:12 +0200925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200927static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
928 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
929 set_pwm_auto_channels, offset - 1); \
930static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
931 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
932 set_pwm_auto_pwm_min, offset - 1); \
933static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
934 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200935 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937pwm_auto(1);
938pwm_auto(2);
939pwm_auto(3);
940
941/* Temperature settings for automatic PWM control */
942
Jean Delvareb353a482007-07-05 20:36:12 +0200943static ssize_t show_temp_auto_temp_off(struct device *dev,
944 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jean Delvareb353a482007-07-05 20:36:12 +0200946 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200948 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 HYST_FROM_REG(data->zone[nr].hyst));
950}
Jean Delvareb353a482007-07-05 20:36:12 +0200951
952static ssize_t set_temp_auto_temp_off(struct device *dev,
953 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Jean Delvareb353a482007-07-05 20:36:12 +0200955 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct i2c_client *client = to_i2c_client(dev);
957 struct lm85_data *data = i2c_get_clientdata(client);
958 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -0800959 long val;
960 int err;
961
962 err = kstrtol(buf, 10, &val);
963 if (err)
964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100966 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200969 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 lm85_write_value(client, LM85_REG_AFAN_HYST1,
971 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200972 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 } else {
974 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200975 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100977 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return count;
979}
Jean Delvareb353a482007-07-05 20:36:12 +0200980
981static ssize_t show_temp_auto_temp_min(struct device *dev,
982 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jean Delvareb353a482007-07-05 20:36:12 +0200984 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200986 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Jean Delvareb353a482007-07-05 20:36:12 +0200988
989static ssize_t set_temp_auto_temp_min(struct device *dev,
990 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Jean Delvareb353a482007-07-05 20:36:12 +0200992 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct i2c_client *client = to_i2c_client(dev);
994 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800995 long val;
996 int err;
997
998 err = kstrtol(buf, 10, &val);
999 if (err)
1000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001002 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 data->zone[nr].limit = TEMP_TO_REG(val);
1004 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1005 data->zone[nr].limit);
1006
1007/* Update temp_auto_max and temp_auto_range */
1008 data->zone[nr].range = RANGE_TO_REG(
1009 TEMP_FROM_REG(data->zone[nr].max_desired) -
1010 TEMP_FROM_REG(data->zone[nr].limit));
1011 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1012 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001013 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001015 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return count;
1017}
Jean Delvareb353a482007-07-05 20:36:12 +02001018
1019static ssize_t show_temp_auto_temp_max(struct device *dev,
1020 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Jean Delvareb353a482007-07-05 20:36:12 +02001022 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001024 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 RANGE_FROM_REG(data->zone[nr].range));
1026}
Jean Delvareb353a482007-07-05 20:36:12 +02001027
1028static ssize_t set_temp_auto_temp_max(struct device *dev,
1029 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Jean Delvareb353a482007-07-05 20:36:12 +02001031 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct i2c_client *client = to_i2c_client(dev);
1033 struct lm85_data *data = i2c_get_clientdata(client);
1034 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001035 long val;
1036 int err;
1037
1038 err = kstrtol(buf, 10, &val);
1039 if (err)
1040 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001042 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 min = TEMP_FROM_REG(data->zone[nr].limit);
1044 data->zone[nr].max_desired = TEMP_TO_REG(val);
1045 data->zone[nr].range = RANGE_TO_REG(
1046 val - min);
1047 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1048 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001049 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001050 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return count;
1052}
Jean Delvareb353a482007-07-05 20:36:12 +02001053
1054static ssize_t show_temp_auto_temp_crit(struct device *dev,
1055 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Jean Delvareb353a482007-07-05 20:36:12 +02001057 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001059 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
Jean Delvareb353a482007-07-05 20:36:12 +02001061
1062static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +02001063 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Jean Delvareb353a482007-07-05 20:36:12 +02001065 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct i2c_client *client = to_i2c_client(dev);
1067 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -08001068 long val;
1069 int err;
1070
1071 err = kstrtol(buf, 10, &val);
1072 if (err)
1073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001075 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 data->zone[nr].critical = TEMP_TO_REG(val);
1077 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1078 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001079 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return count;
1081}
Jean Delvareb353a482007-07-05 20:36:12 +02001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001084static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1085 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1086 set_temp_auto_temp_off, offset - 1); \
1087static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1088 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1089 set_temp_auto_temp_min, offset - 1); \
1090static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1091 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1092 set_temp_auto_temp_max, offset - 1); \
1093static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1094 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1095 set_temp_auto_temp_crit, offset - 1);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097temp_auto(1);
1098temp_auto(2);
1099temp_auto(3);
1100
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001101static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001102 &sensor_dev_attr_fan1_input.dev_attr.attr,
1103 &sensor_dev_attr_fan2_input.dev_attr.attr,
1104 &sensor_dev_attr_fan3_input.dev_attr.attr,
1105 &sensor_dev_attr_fan4_input.dev_attr.attr,
1106 &sensor_dev_attr_fan1_min.dev_attr.attr,
1107 &sensor_dev_attr_fan2_min.dev_attr.attr,
1108 &sensor_dev_attr_fan3_min.dev_attr.attr,
1109 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001110 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1111 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1112 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1113 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001114
1115 &sensor_dev_attr_pwm1.dev_attr.attr,
1116 &sensor_dev_attr_pwm2.dev_attr.attr,
1117 &sensor_dev_attr_pwm3.dev_attr.attr,
1118 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1119 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1120 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001121 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1122 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1123 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001124
1125 &sensor_dev_attr_in0_input.dev_attr.attr,
1126 &sensor_dev_attr_in1_input.dev_attr.attr,
1127 &sensor_dev_attr_in2_input.dev_attr.attr,
1128 &sensor_dev_attr_in3_input.dev_attr.attr,
1129 &sensor_dev_attr_in0_min.dev_attr.attr,
1130 &sensor_dev_attr_in1_min.dev_attr.attr,
1131 &sensor_dev_attr_in2_min.dev_attr.attr,
1132 &sensor_dev_attr_in3_min.dev_attr.attr,
1133 &sensor_dev_attr_in0_max.dev_attr.attr,
1134 &sensor_dev_attr_in1_max.dev_attr.attr,
1135 &sensor_dev_attr_in2_max.dev_attr.attr,
1136 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001137 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1138 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1139 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1140 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001141
1142 &sensor_dev_attr_temp1_input.dev_attr.attr,
1143 &sensor_dev_attr_temp2_input.dev_attr.attr,
1144 &sensor_dev_attr_temp3_input.dev_attr.attr,
1145 &sensor_dev_attr_temp1_min.dev_attr.attr,
1146 &sensor_dev_attr_temp2_min.dev_attr.attr,
1147 &sensor_dev_attr_temp3_min.dev_attr.attr,
1148 &sensor_dev_attr_temp1_max.dev_attr.attr,
1149 &sensor_dev_attr_temp2_max.dev_attr.attr,
1150 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001151 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1152 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1153 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1154 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1155 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001156
1157 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1158 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1159 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1160 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1161 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1162 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001163
Jean Delvareb353a482007-07-05 20:36:12 +02001164 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1165 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1166 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1167 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1168 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1169 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1170 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1171 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1172 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1173
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001174 &dev_attr_vrm.attr,
1175 &dev_attr_cpu0_vid.attr,
1176 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001177 NULL
1178};
1179
1180static const struct attribute_group lm85_group = {
1181 .attrs = lm85_attributes,
1182};
1183
Guenter Roeck06923f82011-02-19 08:27:47 -08001184static struct attribute *lm85_attributes_minctl[] = {
1185 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1186 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1187 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001188 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001189};
1190
1191static const struct attribute_group lm85_group_minctl = {
1192 .attrs = lm85_attributes_minctl,
1193};
1194
1195static struct attribute *lm85_attributes_temp_off[] = {
1196 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1197 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1198 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001199 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001200};
1201
1202static const struct attribute_group lm85_group_temp_off = {
1203 .attrs = lm85_attributes_temp_off,
1204};
1205
Jean Delvare6b9aad22007-07-05 20:37:21 +02001206static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001207 &sensor_dev_attr_in4_input.dev_attr.attr,
1208 &sensor_dev_attr_in4_min.dev_attr.attr,
1209 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001210 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001211 NULL
1212};
1213
Jean Delvare6b9aad22007-07-05 20:37:21 +02001214static const struct attribute_group lm85_group_in4 = {
1215 .attrs = lm85_attributes_in4,
1216};
1217
1218static struct attribute *lm85_attributes_in567[] = {
1219 &sensor_dev_attr_in5_input.dev_attr.attr,
1220 &sensor_dev_attr_in6_input.dev_attr.attr,
1221 &sensor_dev_attr_in7_input.dev_attr.attr,
1222 &sensor_dev_attr_in5_min.dev_attr.attr,
1223 &sensor_dev_attr_in6_min.dev_attr.attr,
1224 &sensor_dev_attr_in7_min.dev_attr.attr,
1225 &sensor_dev_attr_in5_max.dev_attr.attr,
1226 &sensor_dev_attr_in6_max.dev_attr.attr,
1227 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001228 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1229 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1230 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001231 NULL
1232};
1233
1234static const struct attribute_group lm85_group_in567 = {
1235 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001236};
1237
Jean Delvare5f44759472008-06-25 09:10:30 -04001238static void lm85_init_client(struct i2c_client *client)
1239{
1240 int value;
1241
1242 /* Start monitoring if needed */
1243 value = lm85_read_value(client, LM85_REG_CONFIG);
1244 if (!(value & 0x01)) {
1245 dev_info(&client->dev, "Starting monitoring\n");
1246 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1247 }
1248
1249 /* Warn about unusual configuration bits */
1250 if (value & 0x02)
1251 dev_warn(&client->dev, "Device configuration is locked\n");
1252 if (!(value & 0x04))
1253 dev_warn(&client->dev, "Device is not ready\n");
1254}
1255
Jean Delvare5cfaf332009-09-15 17:18:14 +02001256static int lm85_is_fake(struct i2c_client *client)
1257{
1258 /*
1259 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1260 * emulate the former except that it has no hardware monitoring function
1261 * so the readings are always 0.
1262 */
1263 int i;
1264 u8 in_temp, fan;
1265
1266 for (i = 0; i < 8; i++) {
1267 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1268 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1269 if (in_temp != 0x00 || fan != 0xff)
1270 return 0;
1271 }
1272
1273 return 1;
1274}
1275
Jean Delvare67712d02008-10-17 17:51:14 +02001276/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001277static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Jean Delvare67712d02008-10-17 17:51:14 +02001279 struct i2c_adapter *adapter = client->adapter;
1280 int address = client->addr;
Jean Delvare590e8532014-06-11 18:35:56 +02001281 const char *type_name = NULL;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001282 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jean Delvaree89e22b2008-06-25 08:47:35 -04001284 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001286 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Jean Delvared42a2eb2009-12-09 20:35:53 +01001289 /* Determine the chip type */
1290 company = lm85_read_value(client, LM85_REG_COMPANY);
1291 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Guenter Roeckb55f3752013-01-10 10:01:24 -08001293 dev_dbg(&adapter->dev,
1294 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001295 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Jean Delvared42a2eb2009-12-09 20:35:53 +01001297 if (company == LM85_COMPANY_NATIONAL) {
1298 switch (verstep) {
1299 case LM85_VERSTEP_LM85C:
1300 type_name = "lm85c";
1301 break;
1302 case LM85_VERSTEP_LM85B:
1303 type_name = "lm85b";
1304 break;
1305 case LM85_VERSTEP_LM96000_1:
1306 case LM85_VERSTEP_LM96000_2:
1307 /* Check for Winbond WPCD377I */
1308 if (lm85_is_fake(client)) {
1309 dev_dbg(&adapter->dev,
1310 "Found Winbond WPCD377I, ignoring\n");
1311 return -ENODEV;
1312 }
Jean Delvare590e8532014-06-11 18:35:56 +02001313 type_name = "lm85";
Jean Delvared42a2eb2009-12-09 20:35:53 +01001314 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001315 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001316 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1317 switch (verstep) {
1318 case LM85_VERSTEP_ADM1027:
1319 type_name = "adm1027";
1320 break;
1321 case LM85_VERSTEP_ADT7463:
1322 case LM85_VERSTEP_ADT7463C:
1323 type_name = "adt7463";
1324 break;
1325 case LM85_VERSTEP_ADT7468_1:
1326 case LM85_VERSTEP_ADT7468_2:
1327 type_name = "adt7468";
1328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001330 } else if (company == LM85_COMPANY_SMSC) {
1331 switch (verstep) {
1332 case LM85_VERSTEP_EMC6D100_A0:
1333 case LM85_VERSTEP_EMC6D100_A1:
1334 /* Note: we can't tell a '100 from a '101 */
1335 type_name = "emc6d100";
1336 break;
1337 case LM85_VERSTEP_EMC6D102:
1338 type_name = "emc6d102";
1339 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001340 case LM85_VERSTEP_EMC6D103_A0:
1341 case LM85_VERSTEP_EMC6D103_A1:
1342 type_name = "emc6d103";
1343 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001344 case LM85_VERSTEP_EMC6D103S:
1345 type_name = "emc6d103s";
1346 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Jean Delvare590e8532014-06-11 18:35:56 +02001350 if (!type_name)
1351 return -ENODEV;
1352
Jean Delvare67712d02008-10-17 17:51:14 +02001353 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Jean Delvare67712d02008-10-17 17:51:14 +02001355 return 0;
1356}
1357
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001358static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1359{
1360 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Guenter Roeck06923f82011-02-19 08:27:47 -08001361 if (data->type != emc6d103s) {
1362 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1363 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1364 }
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001365 if (!data->has_vid5)
1366 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1367 if (data->type == emc6d100)
1368 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1369}
1370
Jean Delvare67712d02008-10-17 17:51:14 +02001371static int lm85_probe(struct i2c_client *client,
1372 const struct i2c_device_id *id)
1373{
1374 struct lm85_data *data;
1375 int err;
1376
Guenter Roeck747e5d62012-06-02 09:58:10 -07001377 data = devm_kzalloc(&client->dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001378 if (!data)
1379 return -ENOMEM;
1380
1381 i2c_set_clientdata(client, data);
1382 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001383 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Jean Delvare67712d02008-10-17 17:51:14 +02001385 /* Fill in the chip specific driver values */
1386 switch (data->type) {
1387 case adm1027:
1388 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001389 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001390 case emc6d100:
1391 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001392 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001393 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001394 data->freq_map = adm1027_freq_map;
1395 break;
1396 default:
1397 data->freq_map = lm85_freq_map;
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001401 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001404 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001407 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1408 if (err)
Guenter Roeck747e5d62012-06-02 09:58:10 -07001409 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Guenter Roeck06923f82011-02-19 08:27:47 -08001411 /* minctl and temp_off exist on all chips except emc6d103s */
1412 if (data->type != emc6d103s) {
1413 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1414 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001415 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001416 err = sysfs_create_group(&client->dev.kobj,
1417 &lm85_group_temp_off);
1418 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001419 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001420 }
1421
Guenter Roeck09770b22012-01-14 20:47:36 -08001422 /*
1423 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1424 * as a sixth digital VID input rather than an analog input.
1425 */
Guenter Roeckde248802011-02-25 08:19:42 -08001426 if (data->type == adt7463 || data->type == adt7468) {
1427 u8 vid = lm85_read_value(client, LM85_REG_VID);
1428 if (vid & 0x80)
1429 data->has_vid5 = true;
1430 }
1431
Guenter Roeck09770b22012-01-14 20:47:36 -08001432 if (!data->has_vid5) {
1433 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in4);
1434 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001435 goto err_remove_files;
Guenter Roeck09770b22012-01-14 20:47:36 -08001436 }
Jean Delvare6b9aad22007-07-05 20:37:21 +02001437
1438 /* The EMC6D100 has 3 additional voltage inputs */
Guenter Roeck09770b22012-01-14 20:47:36 -08001439 if (data->type == emc6d100) {
1440 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in567);
1441 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001442 goto err_remove_files;
Guenter Roeck09770b22012-01-14 20:47:36 -08001443 }
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001444
Jean Delvaree89e22b2008-06-25 08:47:35 -04001445 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001446 if (IS_ERR(data->hwmon_dev)) {
1447 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001448 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001449 }
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return 0;
1452
1453 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001454 err_remove_files:
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001455 lm85_remove_files(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return err;
1457}
1458
Jean Delvare67712d02008-10-17 17:51:14 +02001459static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001461 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001462 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001463 lm85_remove_files(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
1465}
1466
1467
Ben Dooksd8d20612005-10-26 21:05:46 +02001468static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 int res;
1471
1472 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001473 switch (reg) {
1474 case LM85_REG_FAN(0): /* Read WORD data */
1475 case LM85_REG_FAN(1):
1476 case LM85_REG_FAN(2):
1477 case LM85_REG_FAN(3):
1478 case LM85_REG_FAN_MIN(0):
1479 case LM85_REG_FAN_MIN(1):
1480 case LM85_REG_FAN_MIN(2):
1481 case LM85_REG_FAN_MIN(3):
1482 case LM85_REG_ALARM1: /* Read both bytes at once */
1483 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1484 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 default: /* Read BYTE data */
1487 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Jean Delvare1f448092008-04-29 14:03:37 +02001491 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Jean Delvaree89e22b2008-06-25 08:47:35 -04001494static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Jean Delvare1f448092008-04-29 14:03:37 +02001496 switch (reg) {
1497 case LM85_REG_FAN(0): /* Write WORD data */
1498 case LM85_REG_FAN(1):
1499 case LM85_REG_FAN(2):
1500 case LM85_REG_FAN(3):
1501 case LM85_REG_FAN_MIN(0):
1502 case LM85_REG_FAN_MIN(1):
1503 case LM85_REG_FAN_MIN(2):
1504 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001506 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1507 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001510 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515static struct lm85_data *lm85_update_device(struct device *dev)
1516{
1517 struct i2c_client *client = to_i2c_client(dev);
1518 struct lm85_data *data = i2c_get_clientdata(client);
1519 int i;
1520
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001521 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Jean Delvare1f448092008-04-29 14:03:37 +02001523 if (!data->valid ||
1524 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* Things that change quickly */
1526 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001527
Guenter Roeck09770b22012-01-14 20:47:36 -08001528 /*
1529 * Have to read extended bits first to "freeze" the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001531 * There are 2 additional resolution bits per channel and we
1532 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001534 if (data->type == adm1027 || data->type == adt7463 ||
1535 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int ext1 = lm85_read_value(client,
1537 ADM1027_REG_EXTEND_ADC1);
1538 int ext2 = lm85_read_value(client,
1539 ADM1027_REG_EXTEND_ADC2);
1540 int val = (ext1 << 8) + ext2;
1541
Jean Delvare1f448092008-04-29 14:03:37 +02001542 for (i = 0; i <= 4; i++)
1543 data->in_ext[i] =
1544 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jean Delvare1f448092008-04-29 14:03:37 +02001546 for (i = 0; i <= 2; i++)
1547 data->temp_ext[i] =
1548 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
Jean Delvare9c516ef2005-11-26 20:07:54 +01001551 data->vid = lm85_read_value(client, LM85_REG_VID);
1552
1553 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 data->in[i] =
1555 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001556 data->fan[i] =
1557 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Guenter Roeckde248802011-02-25 08:19:42 -08001560 if (!data->has_vid5)
1561 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
Jean Delvare9c516ef2005-11-26 20:07:54 +01001562
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001563 if (data->type == adt7468)
1564 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 for (i = 0; i <= 2; ++i) {
1567 data->temp[i] =
1568 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 data->pwm[i] =
1570 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001571
1572 if (IS_ADT7468_OFF64(data))
1573 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
1576 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1577
Jean Delvaredd1ac532008-05-01 08:47:33 +02001578 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Three more voltage sensors */
1580 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001581 data->in[i] = lm85_read_value(client,
1582 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001585 data->alarms |= lm85_read_value(client,
1586 EMC6D100_REG_ALARM3) << 16;
Guenter Roeck06923f82011-02-19 08:27:47 -08001587 } else if (data->type == emc6d102 || data->type == emc6d103 ||
1588 data->type == emc6d103s) {
Guenter Roeck09770b22012-01-14 20:47:36 -08001589 /*
1590 * Have to read LSB bits after the MSB ones because
1591 * the reading of the MSB bits has frozen the
1592 * LSBs (backward from the ADM1027).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 */
1594 int ext1 = lm85_read_value(client,
1595 EMC6D102_REG_EXTEND_ADC1);
1596 int ext2 = lm85_read_value(client,
1597 EMC6D102_REG_EXTEND_ADC2);
1598 int ext3 = lm85_read_value(client,
1599 EMC6D102_REG_EXTEND_ADC3);
1600 int ext4 = lm85_read_value(client,
1601 EMC6D102_REG_EXTEND_ADC4);
1602 data->in_ext[0] = ext3 & 0x0f;
1603 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001604 data->in_ext[2] = ext4 >> 4;
1605 data->in_ext[3] = ext3 >> 4;
1606 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 data->temp_ext[0] = ext1 & 0x0f;
1609 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001610 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
Jean Delvare1f448092008-04-29 14:03:37 +02001613 data->last_reading = jiffies;
1614 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Jean Delvare1f448092008-04-29 14:03:37 +02001616 if (!data->valid ||
1617 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* Things that don't change often */
1619 dev_dbg(&client->dev, "Reading config values\n");
1620
Jean Delvare9c516ef2005-11-26 20:07:54 +01001621 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 data->in_min[i] =
1623 lm85_read_value(client, LM85_REG_IN_MIN(i));
1624 data->in_max[i] =
1625 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001626 data->fan_min[i] =
1627 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629
Guenter Roeckde248802011-02-25 08:19:42 -08001630 if (!data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001631 data->in_min[4] = lm85_read_value(client,
1632 LM85_REG_IN_MIN(4));
1633 data->in_max[4] = lm85_read_value(client,
1634 LM85_REG_IN_MAX(4));
1635 }
1636
Jean Delvare1f448092008-04-29 14:03:37 +02001637 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001639 data->in_min[i] = lm85_read_value(client,
1640 EMC6D100_REG_IN_MIN(i));
1641 data->in_max[i] = lm85_read_value(client,
1642 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 }
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001647 int val;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 data->temp_min[i] =
1650 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1651 data->temp_max[i] =
1652 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 data->autofan[i].config =
1655 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1656 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001657 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001658 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 data->autofan[i].min_pwm =
1660 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1661 data->zone[i].limit =
1662 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1663 data->zone[i].critical =
1664 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001665
1666 if (IS_ADT7468_OFF64(data)) {
1667 data->temp_min[i] -= 64;
1668 data->temp_max[i] -= 64;
1669 data->zone[i].limit -= 64;
1670 data->zone[i].critical -= 64;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Guenter Roeck06923f82011-02-19 08:27:47 -08001674 if (data->type != emc6d103s) {
1675 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1676 data->autofan[0].min_off = (i & 0x20) != 0;
1677 data->autofan[1].min_off = (i & 0x40) != 0;
1678 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Guenter Roeck06923f82011-02-19 08:27:47 -08001680 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1681 data->zone[0].hyst = i >> 4;
1682 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Guenter Roeck06923f82011-02-19 08:27:47 -08001684 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1685 data->zone[2].hyst = i >> 4;
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001689 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 data->valid = 1;
1692
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001693 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 return data;
1696}
1697
Axel Linf0967ee2012-01-20 15:38:18 +08001698module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001701MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1702 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001703 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704MODULE_DESCRIPTION("LM85-B, LM85-C driver");