blob: b9d6e7d0ba37c3207a0fe414c9199c93fb2f0111 [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>
8 * Copyright (C) 2007--2009 Jean Delvare <khali@linux-fr.org>
9 *
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 {
42 any_chip, lm85b, lm85c,
43 adm1027, adt7463, adt7468,
Guenter Roeck06923f82011-02-19 08:27:47 -080044 emc6d100, emc6d102, emc6d103, emc6d103s
Jean Delvaree5e9f442009-12-14 21:17:27 +010045};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* The LM85 registers */
48
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
78#define LM85_VERSTEP_VMASK 0xf0
79#define LM85_VERSTEP_GENERIC 0x60
80#define LM85_VERSTEP_GENERIC2 0x70
81#define LM85_VERSTEP_LM85C 0x60
82#define LM85_VERSTEP_LM85B 0x62
83#define LM85_VERSTEP_LM96000_1 0x68
84#define LM85_VERSTEP_LM96000_2 0x69
85#define LM85_VERSTEP_ADM1027 0x60
86#define LM85_VERSTEP_ADT7463 0x62
87#define LM85_VERSTEP_ADT7463C 0x6A
88#define LM85_VERSTEP_ADT7468_1 0x71
89#define LM85_VERSTEP_ADT7468_2 0x72
90#define LM85_VERSTEP_EMC6D100_A0 0x60
91#define LM85_VERSTEP_EMC6D100_A1 0x61
92#define LM85_VERSTEP_EMC6D102 0x65
93#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
Guenter Roeck09770b22012-01-14 20:47:36 -080097#define LM85_REG_CONFIG 0x40
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Guenter Roeck09770b22012-01-14 20:47:36 -080099#define LM85_REG_ALARM1 0x41
100#define LM85_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Guenter Roeck09770b22012-01-14 20:47:36 -0800102#define LM85_REG_VID 0x43
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/* Automated FAN control */
Guenter Roeck09770b22012-01-14 20:47:36 -0800105#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
106#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
107#define LM85_REG_AFAN_SPIKE1 0x62
108#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Guenter Roeck09770b22012-01-14 20:47:36 -0800114#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 */
Guenter Roeck09770b22012-01-14 20:47:36 -0800119#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)
122#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Guenter Roeck09770b22012-01-14 20:47:36 -0800128/*
129 * Conversions. Rounding and limit checking is only done on the TO_REG
130 * variants. Note that you should be a bit careful with which arguments
131 * these macros are called: arguments may be evaluated more than once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
133
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300134/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400135static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200136 2500, 2250, 3300, 5000, 12000,
137 3300, 1500, 1800 /*EMC6D100*/
138};
139#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jean Delvare1f448092008-04-29 14:03:37 +0200141#define INS_TO_REG(n, val) \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800142 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jean Delvare1f448092008-04-29 14:03:37 +0200144#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200145 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jean Delvare1f448092008-04-29 14:03:37 +0200147#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200150static inline u16 FAN_TO_REG(unsigned long val)
151{
152 if (!val)
153 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800154 return clamp_val(5400000 / val, 1, 0xfffe);
Jean Delvare63f281a2007-07-05 20:39:40 +0200155}
Jean Delvare1f448092008-04-29 14:03:37 +0200156#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
157 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/* Temperature is reported in .001 degC increments */
160#define TEMP_TO_REG(val) \
Guenter Roeck53f281f2014-07-29 22:23:12 -0700161 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
Jean Delvare1f448092008-04-29 14:03:37 +0200162#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200163 SCALE(((val) << 4) + (ext), 16, 1000)
164#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Guenter Roeck2a844c12013-01-09 08:09:34 -0800166#define PWM_TO_REG(val) clamp_val(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define PWM_FROM_REG(val) (val)
168
169
Guenter Roeck09770b22012-01-14 20:47:36 -0800170/*
171 * ZONEs have the following parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * Limit (low) temp, 1. degC
173 * Hysteresis (below limit), 1. degC (0-15)
174 * Range of speed control, .1 degC (2-80)
175 * Critical (high) temp, 1. degC
176 *
177 * FAN PWMs have the following parameters:
178 * Reference Zone, 1, 2, 3, etc.
179 * Spinup time, .05 sec
180 * PWM value at limit/low temp, 1 count
181 * PWM Frequency, 1. Hz
182 * PWM is Min or OFF below limit, flag
183 * Invert PWM output, flag
184 *
185 * Some chips filter the temp, others the fan.
186 * Filter constant (or disabled) .1 seconds
187 */
188
189/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400190static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200191 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
192 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
193};
194
Guenter Roeck53f281f2014-07-29 22:23:12 -0700195static int RANGE_TO_REG(long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int i;
198
Jean Delvared38b1492008-04-03 10:40:39 +0200199 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200200 for (i = 0; i < 15; ++i) {
201 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Jean Delvared38b1492008-04-03 10:40:39 +0200204
Jean Delvare1b92ada2008-10-17 17:51:14 +0200205 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Jean Delvare1f448092008-04-29 14:03:37 +0200207#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200210static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200211 10, 15, 23, 30, 38, 47, 61, 94
212};
213static const int adm1027_freq_map[8] = { /* 1 Hz */
214 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200215};
216
Guenter Roeck53f281f2014-07-29 22:23:12 -0700217static int FREQ_TO_REG(const int *map, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int i;
220
Jean Delvare86010c92008-10-17 17:51:13 +0200221 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200222 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200223 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200224 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400225 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200227
228static int FREQ_FROM_REG(const int *map, u8 reg)
229{
230 return map[reg & 0x07];
231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Guenter Roeck09770b22012-01-14 20:47:36 -0800233/*
234 * Since we can't use strings, I'm abusing these numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * to stand in for the following meanings:
236 * 1 -- PWM responds to Zone 1
237 * 2 -- PWM responds to Zone 2
238 * 3 -- PWM responds to Zone 3
239 * 23 -- PWM responds to the higher temp of Zone 2 or 3
240 * 123 -- PWM responds to highest of Zone 1, 2, or 3
241 * 0 -- PWM is always at 0% (ie, off)
242 * -1 -- PWM is always at 100%
243 * -2 -- PWM responds to manual control
244 */
245
Jean Delvaree89e22b2008-06-25 08:47:35 -0400246static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
247#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jean Delvare1f448092008-04-29 14:03:37 +0200249static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 int i;
252
Jean Delvare1f448092008-04-29 14:03:37 +0200253 for (i = 0; i <= 7; ++i)
254 if (zone == lm85_zone_map[i])
255 break;
256 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400258 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Guenter Roeck2a844c12013-01-09 08:09:34 -0800261#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
Jean Delvare1f448092008-04-29 14:03:37 +0200262#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Guenter Roeck09770b22012-01-14 20:47:36 -0800264/*
265 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
267 * Some sensors are not updated more frequently than once per second
268 * so it doesn't make sense to read them more often than that.
269 * We cache the results and return the saved data if the driver
270 * is called again before a second has elapsed.
271 *
272 * Also, there is significant configuration data for this chip
273 * given the automatic PWM fan control that is possible. There
274 * are about 47 bytes of config data to only 22 bytes of actual
275 * readings. So, we keep the config data up to date in the cache
276 * when it is written and only sample it once every 1 *minute*
277 */
278#define LM85_DATA_INTERVAL (HZ + HZ / 2)
279#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
280
Guenter Roeck09770b22012-01-14 20:47:36 -0800281/*
282 * LM85 can automatically adjust fan speeds based on temperature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * This structure encapsulates an entire Zone config. There are
284 * three zones (one for each temperature input) on the lm85
285 */
286struct lm85_zone {
287 s8 limit; /* Low temp limit */
288 u8 hyst; /* Low limit hysteresis. (0-15) */
289 u8 range; /* Temp range, encoded */
290 s8 critical; /* "All fans ON" temp limit */
Guenter Roeck09770b22012-01-14 20:47:36 -0800291 u8 max_desired; /*
292 * Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * to prevent "drift" as other autofan control
294 * values change.
295 */
296};
297
298struct lm85_autofan {
299 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 u8 min_pwm; /* Minimum PWM value, encoded */
301 u8 min_off; /* Min PWM or OFF below "limit", flag */
302};
303
Guenter Roeck09770b22012-01-14 20:47:36 -0800304/*
305 * For each registered chip, we need to keep some data in memory.
306 * The structure is dynamically allocated.
307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308struct lm85_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700309 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200310 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 enum chips type;
312
Guenter Roeckde248802011-02-25 08:19:42 -0800313 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
314
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int valid; /* !=0 if following fields are valid */
317 unsigned long last_reading; /* In jiffies */
318 unsigned long last_config; /* In jiffies */
319
320 u8 in[8]; /* Register value */
321 u8 in_max[8]; /* Register value */
322 u8 in_min[8]; /* Register value */
323 s8 temp[3]; /* Register value */
324 s8 temp_min[3]; /* Register value */
325 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 fan[4]; /* Register value */
327 u16 fan_min[4]; /* Register value */
328 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200329 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 u8 temp_ext[3]; /* Decoded values */
331 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 u8 vid; /* Register value */
333 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800335 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct lm85_autofan autofan[3];
337 struct lm85_zone zone[3];
338};
339
Jean Delvare310ec792009-12-14 21:17:23 +0100340static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
Jean Delvare67712d02008-10-17 17:51:14 +0200341static int lm85_probe(struct i2c_client *client,
342 const struct i2c_device_id *id);
343static int lm85_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100345static int lm85_read_value(struct i2c_client *client, u8 reg);
Jean Delvaree89e22b2008-06-25 08:47:35 -0400346static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static struct lm85_data *lm85_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349
Jean Delvare67712d02008-10-17 17:51:14 +0200350static const struct i2c_device_id lm85_id[] = {
351 { "adm1027", adm1027 },
352 { "adt7463", adt7463 },
Darrick J. Wongc15ade62009-03-10 12:55:47 -0700353 { "adt7468", adt7468 },
Jean Delvare67712d02008-10-17 17:51:14 +0200354 { "lm85", any_chip },
355 { "lm85b", lm85b },
356 { "lm85c", lm85c },
357 { "emc6d100", emc6d100 },
358 { "emc6d101", emc6d100 },
359 { "emc6d102", emc6d102 },
Jan Beulichf065a932011-02-18 03:18:26 -0500360 { "emc6d103", emc6d103 },
Guenter Roeck06923f82011-02-19 08:27:47 -0800361 { "emc6d103s", emc6d103s },
Jean Delvare67712d02008-10-17 17:51:14 +0200362 { }
363};
364MODULE_DEVICE_TABLE(i2c, lm85_id);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static struct i2c_driver lm85_driver = {
Jean Delvare67712d02008-10-17 17:51:14 +0200367 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100368 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100369 .name = "lm85",
370 },
Jean Delvare67712d02008-10-17 17:51:14 +0200371 .probe = lm85_probe,
372 .remove = lm85_remove,
373 .id_table = lm85_id,
374 .detect = lm85_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100375 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
378
379/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200380static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
381 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvareb353a482007-07-05 20:36:12 +0200383 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200385 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Jean Delvareb353a482007-07-05 20:36:12 +0200387
388static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
389 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Jean Delvareb353a482007-07-05 20:36:12 +0200391 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200393 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Jean Delvareb353a482007-07-05 20:36:12 +0200395
396static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jean Delvareb353a482007-07-05 20:36:12 +0200399 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct i2c_client *client = to_i2c_client(dev);
401 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800402 unsigned long val;
403 int err;
404
405 err = kstrtoul(buf, 10, &val);
406 if (err)
407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100409 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 data->fan_min[nr] = FAN_TO_REG(val);
411 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100412 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return count;
414}
415
416#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200417static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan, NULL, offset - 1); \
419static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
420 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422show_fan_offset(1);
423show_fan_offset(2);
424show_fan_offset(3);
425show_fan_offset(4);
426
427/* vid, vrm, alarms */
428
Jean Delvare1f448092008-04-29 14:03:37 +0200429static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
430 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100433 int vid;
434
Guenter Roeckde248802011-02-25 08:19:42 -0800435 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100436 /* 6-pin VID (VRM 10) */
437 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
438 } else {
439 /* 5-pin VID (VRM 9) */
440 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
441 }
442
443 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
447
Jean Delvare1f448092008-04-29 14:03:37 +0200448static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
449 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Jean Delvare90d66192007-10-08 18:24:35 +0200451 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return sprintf(buf, "%ld\n", (long) data->vrm);
453}
454
Jean Delvare1f448092008-04-29 14:03:37 +0200455static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
456 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100458 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b22012-01-14 20:47:36 -0800459 unsigned long val;
460 int err;
461
462 err = kstrtoul(buf, 10, &val);
463 if (err)
464 return err;
465
Guenter Roeck53f281f2014-07-29 22:23:12 -0700466 if (val > 255)
467 return -EINVAL;
468
Guenter Roeck09770b22012-01-14 20:47:36 -0800469 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return count;
471}
472
473static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
474
Jean Delvare1f448092008-04-29 14:03:37 +0200475static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
476 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200479 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
483
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200484static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
485 char *buf)
486{
487 int nr = to_sensor_dev_attr(attr)->index;
488 struct lm85_data *data = lm85_update_device(dev);
489 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
490}
491
492static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
493static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
494static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
495static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
496static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
497static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
498static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
499static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
500static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
501static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
502static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
503static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
504static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
505static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
506static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
507static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
508static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/* pwm */
511
Jean Delvareb353a482007-07-05 20:36:12 +0200512static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
513 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jean Delvareb353a482007-07-05 20:36:12 +0200515 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200517 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
Jean Delvareb353a482007-07-05 20:36:12 +0200519
520static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
521 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Jean Delvareb353a482007-07-05 20:36:12 +0200523 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct i2c_client *client = to_i2c_client(dev);
525 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800526 unsigned long val;
527 int err;
528
529 err = kstrtoul(buf, 10, &val);
530 if (err)
531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100533 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 data->pwm[nr] = PWM_TO_REG(val);
535 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100536 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return count;
538}
Jean Delvareb353a482007-07-05 20:36:12 +0200539
540static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
541 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Jean Delvareb353a482007-07-05 20:36:12 +0200543 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100545 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100548 switch (pwm_zone) {
549 case -1: /* PWM is always at 100% */
550 enable = 0;
551 break;
552 case 0: /* PWM is always at 0% */
553 case -2: /* PWM responds to manual control */
554 enable = 1;
555 break;
556 default: /* PWM in automatic mode */
557 enable = 2;
558 }
559 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Jean Delvare455f7912007-12-03 23:28:42 +0100562static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
563 *attr, const char *buf, size_t count)
564{
565 int nr = to_sensor_dev_attr(attr)->index;
566 struct i2c_client *client = to_i2c_client(dev);
567 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare455f7912007-12-03 23:28:42 +0100568 u8 config;
Guenter Roeck09770b22012-01-14 20:47:36 -0800569 unsigned long val;
570 int err;
571
572 err = kstrtoul(buf, 10, &val);
573 if (err)
574 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100575
576 switch (val) {
577 case 0:
578 config = 3;
579 break;
580 case 1:
581 config = 7;
582 break;
583 case 2:
Guenter Roeck09770b22012-01-14 20:47:36 -0800584 /*
585 * Here we have to choose arbitrarily one of the 5 possible
586 * configurations; I go for the safest
587 */
Jean Delvare455f7912007-12-03 23:28:42 +0100588 config = 6;
589 break;
590 default:
591 return -EINVAL;
592 }
593
594 mutex_lock(&data->update_lock);
595 data->autofan[nr].config = lm85_read_value(client,
596 LM85_REG_AFAN_CONFIG(nr));
597 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
598 | (config << 5);
599 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
600 data->autofan[nr].config);
601 mutex_unlock(&data->update_lock);
602 return count;
603}
604
Jean Delvare34e7dc62008-10-17 17:51:13 +0200605static ssize_t show_pwm_freq(struct device *dev,
606 struct device_attribute *attr, char *buf)
607{
608 int nr = to_sensor_dev_attr(attr)->index;
609 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200610 int freq;
611
612 if (IS_ADT7468_HFPWM(data))
613 freq = 22500;
614 else
615 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
616
617 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200618}
619
620static ssize_t set_pwm_freq(struct device *dev,
621 struct device_attribute *attr, const char *buf, size_t count)
622{
623 int nr = to_sensor_dev_attr(attr)->index;
624 struct i2c_client *client = to_i2c_client(dev);
625 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800626 unsigned long val;
627 int err;
628
629 err = kstrtoul(buf, 10, &val);
630 if (err)
631 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200632
633 mutex_lock(&data->update_lock);
Guenter Roeck09770b22012-01-14 20:47:36 -0800634 /*
635 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200636 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b22012-01-14 20:47:36 -0800637 * This might confuse the user, but there's not much we can do.
638 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200639 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
640 data->cfg5 &= ~ADT7468_HFPWM;
641 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
642 } else { /* Low freq. mode */
643 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
644 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
645 (data->zone[nr].range << 4)
646 | data->pwm_freq[nr]);
647 if (data->type == adt7468) {
648 data->cfg5 |= ADT7468_HFPWM;
649 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
650 }
651 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200652 mutex_unlock(&data->update_lock);
653 return count;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200657static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
658 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100659static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200660 show_pwm_enable, set_pwm_enable, offset - 1); \
661static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
662 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664show_pwm_reg(1);
665show_pwm_reg(2);
666show_pwm_reg(3);
667
668/* Voltages */
669
Jean Delvareb353a482007-07-05 20:36:12 +0200670static ssize_t show_in(struct device *dev, struct device_attribute *attr,
671 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Jean Delvareb353a482007-07-05 20:36:12 +0200673 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200675 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
676 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
Jean Delvareb353a482007-07-05 20:36:12 +0200678
Jean Delvare1f448092008-04-29 14:03:37 +0200679static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200680 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jean Delvareb353a482007-07-05 20:36:12 +0200682 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200684 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Jean Delvareb353a482007-07-05 20:36:12 +0200686
687static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jean Delvareb353a482007-07-05 20:36:12 +0200690 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct i2c_client *client = to_i2c_client(dev);
692 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800693 long val;
694 int err;
695
696 err = kstrtol(buf, 10, &val);
697 if (err)
698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100700 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 data->in_min[nr] = INS_TO_REG(nr, val);
702 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100703 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return count;
705}
Jean Delvareb353a482007-07-05 20:36:12 +0200706
707static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
708 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jean Delvareb353a482007-07-05 20:36:12 +0200710 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200712 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
Jean Delvareb353a482007-07-05 20:36:12 +0200714
715static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
716 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Jean Delvareb353a482007-07-05 20:36:12 +0200718 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct i2c_client *client = to_i2c_client(dev);
720 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800721 long val;
722 int err;
723
724 err = kstrtol(buf, 10, &val);
725 if (err)
726 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100728 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 data->in_max[nr] = INS_TO_REG(nr, val);
730 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100731 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return count;
733}
Jean Delvareb353a482007-07-05 20:36:12 +0200734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200736static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
737 show_in, NULL, offset); \
738static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
739 show_in_min, set_in_min, offset); \
740static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
741 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743show_in_reg(0);
744show_in_reg(1);
745show_in_reg(2);
746show_in_reg(3);
747show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200748show_in_reg(5);
749show_in_reg(6);
750show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752/* Temps */
753
Jean Delvareb353a482007-07-05 20:36:12 +0200754static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
755 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Jean Delvareb353a482007-07-05 20:36:12 +0200757 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200759 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
760 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Jean Delvareb353a482007-07-05 20:36:12 +0200762
763static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
764 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Jean Delvareb353a482007-07-05 20:36:12 +0200766 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200768 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
Jean Delvareb353a482007-07-05 20:36:12 +0200770
771static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
772 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Jean Delvareb353a482007-07-05 20:36:12 +0200774 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct i2c_client *client = to_i2c_client(dev);
776 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800777 long val;
778 int err;
779
780 err = kstrtol(buf, 10, &val);
781 if (err)
782 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800784 if (IS_ADT7468_OFF64(data))
785 val += 64;
786
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100787 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 data->temp_min[nr] = TEMP_TO_REG(val);
789 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100790 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return count;
792}
Jean Delvareb353a482007-07-05 20:36:12 +0200793
794static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
795 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Jean Delvareb353a482007-07-05 20:36:12 +0200797 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200799 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
Jean Delvareb353a482007-07-05 20:36:12 +0200801
802static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Jean Delvareb353a482007-07-05 20:36:12 +0200805 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct i2c_client *client = to_i2c_client(dev);
807 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800808 long val;
809 int err;
810
811 err = kstrtol(buf, 10, &val);
812 if (err)
813 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800815 if (IS_ADT7468_OFF64(data))
816 val += 64;
817
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100818 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 data->temp_max[nr] = TEMP_TO_REG(val);
820 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100821 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return count;
823}
Jean Delvareb353a482007-07-05 20:36:12 +0200824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200826static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
827 show_temp, NULL, offset - 1); \
828static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
829 show_temp_min, set_temp_min, offset - 1); \
830static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
831 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833show_temp_reg(1);
834show_temp_reg(2);
835show_temp_reg(3);
836
837
838/* Automatic PWM control */
839
Jean Delvareb353a482007-07-05 20:36:12 +0200840static ssize_t show_pwm_auto_channels(struct device *dev,
841 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Jean Delvareb353a482007-07-05 20:36:12 +0200843 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200845 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
Jean Delvareb353a482007-07-05 20:36:12 +0200847
848static ssize_t set_pwm_auto_channels(struct device *dev,
849 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Jean Delvareb353a482007-07-05 20:36:12 +0200851 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct i2c_client *client = to_i2c_client(dev);
853 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800854 long val;
855 int err;
856
857 err = kstrtol(buf, 10, &val);
858 if (err)
859 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100861 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +0200863 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
865 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100866 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return count;
868}
Jean Delvareb353a482007-07-05 20:36:12 +0200869
870static ssize_t show_pwm_auto_pwm_min(struct device *dev,
871 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Jean Delvareb353a482007-07-05 20:36:12 +0200873 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200875 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Jean Delvareb353a482007-07-05 20:36:12 +0200877
878static ssize_t set_pwm_auto_pwm_min(struct device *dev,
879 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jean Delvareb353a482007-07-05 20:36:12 +0200881 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct i2c_client *client = to_i2c_client(dev);
883 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800884 unsigned long val;
885 int err;
886
887 err = kstrtoul(buf, 10, &val);
888 if (err)
889 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100891 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 data->autofan[nr].min_pwm = PWM_TO_REG(val);
893 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
894 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100895 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return count;
897}
Jean Delvareb353a482007-07-05 20:36:12 +0200898
899static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
900 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Jean Delvareb353a482007-07-05 20:36:12 +0200902 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200904 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Jean Delvareb353a482007-07-05 20:36:12 +0200906
907static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
908 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Jean Delvareb353a482007-07-05 20:36:12 +0200910 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct i2c_client *client = to_i2c_client(dev);
912 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare7133e562008-04-12 19:56:35 +0200913 u8 tmp;
Guenter Roeck09770b22012-01-14 20:47:36 -0800914 long val;
915 int err;
916
917 err = kstrtol(buf, 10, &val);
918 if (err)
919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100921 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +0200923 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
924 tmp &= ~(0x20 << nr);
925 if (data->autofan[nr].min_off)
926 tmp |= 0x20 << nr;
927 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100928 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return count;
930}
Jean Delvareb353a482007-07-05 20:36:12 +0200931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200933static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
934 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
935 set_pwm_auto_channels, offset - 1); \
936static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
937 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
938 set_pwm_auto_pwm_min, offset - 1); \
939static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
940 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200941 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +0200942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943pwm_auto(1);
944pwm_auto(2);
945pwm_auto(3);
946
947/* Temperature settings for automatic PWM control */
948
Jean Delvareb353a482007-07-05 20:36:12 +0200949static ssize_t show_temp_auto_temp_off(struct device *dev,
950 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Jean Delvareb353a482007-07-05 20:36:12 +0200952 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200954 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 HYST_FROM_REG(data->zone[nr].hyst));
956}
Jean Delvareb353a482007-07-05 20:36:12 +0200957
958static ssize_t set_temp_auto_temp_off(struct device *dev,
959 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jean Delvareb353a482007-07-05 20:36:12 +0200961 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct i2c_client *client = to_i2c_client(dev);
963 struct lm85_data *data = i2c_get_clientdata(client);
964 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -0800965 long val;
966 int err;
967
968 err = kstrtol(buf, 10, &val);
969 if (err)
970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +0200975 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 lm85_write_value(client, LM85_REG_AFAN_HYST1,
977 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +0200978 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 } else {
980 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +0200981 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100983 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return count;
985}
Jean Delvareb353a482007-07-05 20:36:12 +0200986
987static ssize_t show_temp_auto_temp_min(struct device *dev,
988 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Jean Delvareb353a482007-07-05 20:36:12 +0200990 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200992 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
Jean Delvareb353a482007-07-05 20:36:12 +0200994
995static ssize_t set_temp_auto_temp_min(struct device *dev,
996 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Jean Delvareb353a482007-07-05 20:36:12 +0200998 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct i2c_client *client = to_i2c_client(dev);
1000 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -08001001 long val;
1002 int err;
1003
1004 err = kstrtol(buf, 10, &val);
1005 if (err)
1006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001008 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 data->zone[nr].limit = TEMP_TO_REG(val);
1010 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1011 data->zone[nr].limit);
1012
1013/* Update temp_auto_max and temp_auto_range */
1014 data->zone[nr].range = RANGE_TO_REG(
1015 TEMP_FROM_REG(data->zone[nr].max_desired) -
1016 TEMP_FROM_REG(data->zone[nr].limit));
1017 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1018 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001019 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001021 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return count;
1023}
Jean Delvareb353a482007-07-05 20:36:12 +02001024
1025static ssize_t show_temp_auto_temp_max(struct device *dev,
1026 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Jean Delvareb353a482007-07-05 20:36:12 +02001028 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001030 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 RANGE_FROM_REG(data->zone[nr].range));
1032}
Jean Delvareb353a482007-07-05 20:36:12 +02001033
1034static ssize_t set_temp_auto_temp_max(struct device *dev,
1035 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jean Delvareb353a482007-07-05 20:36:12 +02001037 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct i2c_client *client = to_i2c_client(dev);
1039 struct lm85_data *data = i2c_get_clientdata(client);
1040 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001041 long val;
1042 int err;
1043
1044 err = kstrtol(buf, 10, &val);
1045 if (err)
1046 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001048 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 min = TEMP_FROM_REG(data->zone[nr].limit);
1050 data->zone[nr].max_desired = TEMP_TO_REG(val);
1051 data->zone[nr].range = RANGE_TO_REG(
1052 val - min);
1053 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1054 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001055 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001056 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return count;
1058}
Jean Delvareb353a482007-07-05 20:36:12 +02001059
1060static ssize_t show_temp_auto_temp_crit(struct device *dev,
1061 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jean Delvareb353a482007-07-05 20:36:12 +02001063 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001065 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Jean Delvareb353a482007-07-05 20:36:12 +02001067
1068static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +02001069 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Jean Delvareb353a482007-07-05 20:36:12 +02001071 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct i2c_client *client = to_i2c_client(dev);
1073 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -08001074 long val;
1075 int err;
1076
1077 err = kstrtol(buf, 10, &val);
1078 if (err)
1079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001081 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 data->zone[nr].critical = TEMP_TO_REG(val);
1083 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1084 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001085 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return count;
1087}
Jean Delvareb353a482007-07-05 20:36:12 +02001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001090static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1091 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1092 set_temp_auto_temp_off, offset - 1); \
1093static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1094 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1095 set_temp_auto_temp_min, offset - 1); \
1096static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1097 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1098 set_temp_auto_temp_max, offset - 1); \
1099static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1100 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1101 set_temp_auto_temp_crit, offset - 1);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103temp_auto(1);
1104temp_auto(2);
1105temp_auto(3);
1106
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001107static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001108 &sensor_dev_attr_fan1_input.dev_attr.attr,
1109 &sensor_dev_attr_fan2_input.dev_attr.attr,
1110 &sensor_dev_attr_fan3_input.dev_attr.attr,
1111 &sensor_dev_attr_fan4_input.dev_attr.attr,
1112 &sensor_dev_attr_fan1_min.dev_attr.attr,
1113 &sensor_dev_attr_fan2_min.dev_attr.attr,
1114 &sensor_dev_attr_fan3_min.dev_attr.attr,
1115 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001116 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1117 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1118 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1119 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001120
1121 &sensor_dev_attr_pwm1.dev_attr.attr,
1122 &sensor_dev_attr_pwm2.dev_attr.attr,
1123 &sensor_dev_attr_pwm3.dev_attr.attr,
1124 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1125 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1126 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001127 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1128 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1129 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001130
1131 &sensor_dev_attr_in0_input.dev_attr.attr,
1132 &sensor_dev_attr_in1_input.dev_attr.attr,
1133 &sensor_dev_attr_in2_input.dev_attr.attr,
1134 &sensor_dev_attr_in3_input.dev_attr.attr,
1135 &sensor_dev_attr_in0_min.dev_attr.attr,
1136 &sensor_dev_attr_in1_min.dev_attr.attr,
1137 &sensor_dev_attr_in2_min.dev_attr.attr,
1138 &sensor_dev_attr_in3_min.dev_attr.attr,
1139 &sensor_dev_attr_in0_max.dev_attr.attr,
1140 &sensor_dev_attr_in1_max.dev_attr.attr,
1141 &sensor_dev_attr_in2_max.dev_attr.attr,
1142 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001143 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1144 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1145 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1146 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001147
1148 &sensor_dev_attr_temp1_input.dev_attr.attr,
1149 &sensor_dev_attr_temp2_input.dev_attr.attr,
1150 &sensor_dev_attr_temp3_input.dev_attr.attr,
1151 &sensor_dev_attr_temp1_min.dev_attr.attr,
1152 &sensor_dev_attr_temp2_min.dev_attr.attr,
1153 &sensor_dev_attr_temp3_min.dev_attr.attr,
1154 &sensor_dev_attr_temp1_max.dev_attr.attr,
1155 &sensor_dev_attr_temp2_max.dev_attr.attr,
1156 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001157 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1158 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1159 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1160 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1161 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001162
1163 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1164 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1165 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1166 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1167 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1168 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001169
Jean Delvareb353a482007-07-05 20:36:12 +02001170 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1171 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1172 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1173 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1174 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1175 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1176 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1177 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1178 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1179
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001180 &dev_attr_vrm.attr,
1181 &dev_attr_cpu0_vid.attr,
1182 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001183 NULL
1184};
1185
1186static const struct attribute_group lm85_group = {
1187 .attrs = lm85_attributes,
1188};
1189
Guenter Roeck06923f82011-02-19 08:27:47 -08001190static struct attribute *lm85_attributes_minctl[] = {
1191 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1192 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1193 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001194 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001195};
1196
1197static const struct attribute_group lm85_group_minctl = {
1198 .attrs = lm85_attributes_minctl,
1199};
1200
1201static struct attribute *lm85_attributes_temp_off[] = {
1202 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1203 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1204 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001205 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001206};
1207
1208static const struct attribute_group lm85_group_temp_off = {
1209 .attrs = lm85_attributes_temp_off,
1210};
1211
Jean Delvare6b9aad22007-07-05 20:37:21 +02001212static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001213 &sensor_dev_attr_in4_input.dev_attr.attr,
1214 &sensor_dev_attr_in4_min.dev_attr.attr,
1215 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001216 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001217 NULL
1218};
1219
Jean Delvare6b9aad22007-07-05 20:37:21 +02001220static const struct attribute_group lm85_group_in4 = {
1221 .attrs = lm85_attributes_in4,
1222};
1223
1224static struct attribute *lm85_attributes_in567[] = {
1225 &sensor_dev_attr_in5_input.dev_attr.attr,
1226 &sensor_dev_attr_in6_input.dev_attr.attr,
1227 &sensor_dev_attr_in7_input.dev_attr.attr,
1228 &sensor_dev_attr_in5_min.dev_attr.attr,
1229 &sensor_dev_attr_in6_min.dev_attr.attr,
1230 &sensor_dev_attr_in7_min.dev_attr.attr,
1231 &sensor_dev_attr_in5_max.dev_attr.attr,
1232 &sensor_dev_attr_in6_max.dev_attr.attr,
1233 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001234 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1235 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1236 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001237 NULL
1238};
1239
1240static const struct attribute_group lm85_group_in567 = {
1241 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001242};
1243
Jean Delvare5f44759472008-06-25 09:10:30 -04001244static void lm85_init_client(struct i2c_client *client)
1245{
1246 int value;
1247
1248 /* Start monitoring if needed */
1249 value = lm85_read_value(client, LM85_REG_CONFIG);
1250 if (!(value & 0x01)) {
1251 dev_info(&client->dev, "Starting monitoring\n");
1252 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1253 }
1254
1255 /* Warn about unusual configuration bits */
1256 if (value & 0x02)
1257 dev_warn(&client->dev, "Device configuration is locked\n");
1258 if (!(value & 0x04))
1259 dev_warn(&client->dev, "Device is not ready\n");
1260}
1261
Jean Delvare5cfaf332009-09-15 17:18:14 +02001262static int lm85_is_fake(struct i2c_client *client)
1263{
1264 /*
1265 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1266 * emulate the former except that it has no hardware monitoring function
1267 * so the readings are always 0.
1268 */
1269 int i;
1270 u8 in_temp, fan;
1271
1272 for (i = 0; i < 8; i++) {
1273 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1274 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1275 if (in_temp != 0x00 || fan != 0xff)
1276 return 0;
1277 }
1278
1279 return 1;
1280}
1281
Jean Delvare67712d02008-10-17 17:51:14 +02001282/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001283static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Jean Delvare67712d02008-10-17 17:51:14 +02001285 struct i2c_adapter *adapter = client->adapter;
1286 int address = client->addr;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001287 const char *type_name;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001288 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jean Delvaree89e22b2008-06-25 08:47:35 -04001290 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001292 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Jean Delvared42a2eb2009-12-09 20:35:53 +01001295 /* Determine the chip type */
1296 company = lm85_read_value(client, LM85_REG_COMPANY);
1297 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Guenter Roeckb55f3752013-01-10 10:01:24 -08001299 dev_dbg(&adapter->dev,
1300 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001301 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Jean Delvared42a2eb2009-12-09 20:35:53 +01001303 /* All supported chips have the version in common */
1304 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1305 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1306 dev_dbg(&adapter->dev,
1307 "Autodetection failed: unsupported version\n");
1308 return -ENODEV;
1309 }
1310 type_name = "lm85";
1311
1312 /* Now, refine the detection */
1313 if (company == LM85_COMPANY_NATIONAL) {
1314 switch (verstep) {
1315 case LM85_VERSTEP_LM85C:
1316 type_name = "lm85c";
1317 break;
1318 case LM85_VERSTEP_LM85B:
1319 type_name = "lm85b";
1320 break;
1321 case LM85_VERSTEP_LM96000_1:
1322 case LM85_VERSTEP_LM96000_2:
1323 /* Check for Winbond WPCD377I */
1324 if (lm85_is_fake(client)) {
1325 dev_dbg(&adapter->dev,
1326 "Found Winbond WPCD377I, ignoring\n");
1327 return -ENODEV;
1328 }
1329 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001330 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001331 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1332 switch (verstep) {
1333 case LM85_VERSTEP_ADM1027:
1334 type_name = "adm1027";
1335 break;
1336 case LM85_VERSTEP_ADT7463:
1337 case LM85_VERSTEP_ADT7463C:
1338 type_name = "adt7463";
1339 break;
1340 case LM85_VERSTEP_ADT7468_1:
1341 case LM85_VERSTEP_ADT7468_2:
1342 type_name = "adt7468";
1343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001345 } else if (company == LM85_COMPANY_SMSC) {
1346 switch (verstep) {
1347 case LM85_VERSTEP_EMC6D100_A0:
1348 case LM85_VERSTEP_EMC6D100_A1:
1349 /* Note: we can't tell a '100 from a '101 */
1350 type_name = "emc6d100";
1351 break;
1352 case LM85_VERSTEP_EMC6D102:
1353 type_name = "emc6d102";
1354 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001355 case LM85_VERSTEP_EMC6D103_A0:
1356 case LM85_VERSTEP_EMC6D103_A1:
1357 type_name = "emc6d103";
1358 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001359 case LM85_VERSTEP_EMC6D103S:
1360 type_name = "emc6d103s";
1361 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001362 }
1363 } else {
1364 dev_dbg(&adapter->dev,
1365 "Autodetection failed: unknown vendor\n");
1366 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
Jean Delvare67712d02008-10-17 17:51:14 +02001369 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Jean Delvare67712d02008-10-17 17:51:14 +02001371 return 0;
1372}
1373
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001374static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1375{
1376 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Guenter Roeck06923f82011-02-19 08:27:47 -08001377 if (data->type != emc6d103s) {
1378 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1379 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1380 }
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001381 if (!data->has_vid5)
1382 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1383 if (data->type == emc6d100)
1384 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1385}
1386
Jean Delvare67712d02008-10-17 17:51:14 +02001387static int lm85_probe(struct i2c_client *client,
1388 const struct i2c_device_id *id)
1389{
1390 struct lm85_data *data;
1391 int err;
1392
Guenter Roeck747e5d62012-06-02 09:58:10 -07001393 data = devm_kzalloc(&client->dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001394 if (!data)
1395 return -ENOMEM;
1396
1397 i2c_set_clientdata(client, data);
1398 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001399 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Jean Delvare67712d02008-10-17 17:51:14 +02001401 /* Fill in the chip specific driver values */
1402 switch (data->type) {
1403 case adm1027:
1404 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001405 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001406 case emc6d100:
1407 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001408 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001409 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001410 data->freq_map = adm1027_freq_map;
1411 break;
1412 default:
1413 data->freq_map = lm85_freq_map;
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001417 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001420 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001423 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1424 if (err)
Guenter Roeck747e5d62012-06-02 09:58:10 -07001425 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Guenter Roeck06923f82011-02-19 08:27:47 -08001427 /* minctl and temp_off exist on all chips except emc6d103s */
1428 if (data->type != emc6d103s) {
1429 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1430 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001431 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001432 err = sysfs_create_group(&client->dev.kobj,
1433 &lm85_group_temp_off);
1434 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001435 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001436 }
1437
Guenter Roeck09770b22012-01-14 20:47:36 -08001438 /*
1439 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1440 * as a sixth digital VID input rather than an analog input.
1441 */
Guenter Roeckde248802011-02-25 08:19:42 -08001442 if (data->type == adt7463 || data->type == adt7468) {
1443 u8 vid = lm85_read_value(client, LM85_REG_VID);
1444 if (vid & 0x80)
1445 data->has_vid5 = true;
1446 }
1447
Guenter Roeck09770b22012-01-14 20:47:36 -08001448 if (!data->has_vid5) {
1449 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in4);
1450 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001451 goto err_remove_files;
Guenter Roeck09770b22012-01-14 20:47:36 -08001452 }
Jean Delvare6b9aad22007-07-05 20:37:21 +02001453
1454 /* The EMC6D100 has 3 additional voltage inputs */
Guenter Roeck09770b22012-01-14 20:47:36 -08001455 if (data->type == emc6d100) {
1456 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in567);
1457 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001458 goto err_remove_files;
Guenter Roeck09770b22012-01-14 20:47:36 -08001459 }
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001460
Jean Delvaree89e22b2008-06-25 08:47:35 -04001461 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001462 if (IS_ERR(data->hwmon_dev)) {
1463 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001464 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468
1469 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001470 err_remove_files:
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001471 lm85_remove_files(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return err;
1473}
1474
Jean Delvare67712d02008-10-17 17:51:14 +02001475static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001477 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001478 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001479 lm85_remove_files(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481}
1482
1483
Ben Dooksd8d20612005-10-26 21:05:46 +02001484static int lm85_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 int res;
1487
1488 /* What size location is it? */
Jean Delvare1f448092008-04-29 14:03:37 +02001489 switch (reg) {
1490 case LM85_REG_FAN(0): /* Read WORD data */
1491 case LM85_REG_FAN(1):
1492 case LM85_REG_FAN(2):
1493 case LM85_REG_FAN(3):
1494 case LM85_REG_FAN_MIN(0):
1495 case LM85_REG_FAN_MIN(1):
1496 case LM85_REG_FAN_MIN(2):
1497 case LM85_REG_FAN_MIN(3):
1498 case LM85_REG_ALARM1: /* Read both bytes at once */
1499 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1500 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 default: /* Read BYTE data */
1503 res = i2c_smbus_read_byte_data(client, reg);
Jean Delvare1f448092008-04-29 14:03:37 +02001504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506
Jean Delvare1f448092008-04-29 14:03:37 +02001507 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Jean Delvaree89e22b2008-06-25 08:47:35 -04001510static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Jean Delvare1f448092008-04-29 14:03:37 +02001512 switch (reg) {
1513 case LM85_REG_FAN(0): /* Write WORD data */
1514 case LM85_REG_FAN(1):
1515 case LM85_REG_FAN(2):
1516 case LM85_REG_FAN(3):
1517 case LM85_REG_FAN_MIN(0):
1518 case LM85_REG_FAN_MIN(1):
1519 case LM85_REG_FAN_MIN(2):
1520 case LM85_REG_FAN_MIN(3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* NOTE: ALARM is read only, so not included here */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001522 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1523 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
Jean Delvare1f448092008-04-29 14:03:37 +02001524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 default: /* Write BYTE data */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001526 i2c_smbus_write_byte_data(client, reg, value);
Jean Delvare1f448092008-04-29 14:03:37 +02001527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static struct lm85_data *lm85_update_device(struct device *dev)
1532{
1533 struct i2c_client *client = to_i2c_client(dev);
1534 struct lm85_data *data = i2c_get_clientdata(client);
1535 int i;
1536
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001537 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Jean Delvare1f448092008-04-29 14:03:37 +02001539 if (!data->valid ||
1540 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /* Things that change quickly */
1542 dev_dbg(&client->dev, "Reading sensor values\n");
Jean Delvare1f448092008-04-29 14:03:37 +02001543
Guenter Roeck09770b22012-01-14 20:47:36 -08001544 /*
1545 * Have to read extended bits first to "freeze" the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * more significant bits that are read later.
Jean Delvare5a4d3ef2007-07-05 20:38:32 +02001547 * There are 2 additional resolution bits per channel and we
1548 * have room for 4, so we shift them to the left.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 */
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001550 if (data->type == adm1027 || data->type == adt7463 ||
1551 data->type == adt7468) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int ext1 = lm85_read_value(client,
1553 ADM1027_REG_EXTEND_ADC1);
1554 int ext2 = lm85_read_value(client,
1555 ADM1027_REG_EXTEND_ADC2);
1556 int val = (ext1 << 8) + ext2;
1557
Jean Delvare1f448092008-04-29 14:03:37 +02001558 for (i = 0; i <= 4; i++)
1559 data->in_ext[i] =
1560 ((val >> (i * 2)) & 0x03) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jean Delvare1f448092008-04-29 14:03:37 +02001562 for (i = 0; i <= 2; i++)
1563 data->temp_ext[i] =
1564 (val >> ((i + 4) * 2)) & 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
Jean Delvare9c516ef2005-11-26 20:07:54 +01001567 data->vid = lm85_read_value(client, LM85_REG_VID);
1568
1569 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 data->in[i] =
1571 lm85_read_value(client, LM85_REG_IN(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001572 data->fan[i] =
1573 lm85_read_value(client, LM85_REG_FAN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
Guenter Roeckde248802011-02-25 08:19:42 -08001576 if (!data->has_vid5)
1577 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
Jean Delvare9c516ef2005-11-26 20:07:54 +01001578
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001579 if (data->type == adt7468)
1580 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 for (i = 0; i <= 2; ++i) {
1583 data->temp[i] =
1584 lm85_read_value(client, LM85_REG_TEMP(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 data->pwm[i] =
1586 lm85_read_value(client, LM85_REG_PWM(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001587
1588 if (IS_ADT7468_OFF64(data))
1589 data->temp[i] -= 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
1592 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1593
Jean Delvaredd1ac532008-05-01 08:47:33 +02001594 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /* Three more voltage sensors */
1596 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001597 data->in[i] = lm85_read_value(client,
1598 EMC6D100_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600 /* More alarm bits */
Jean Delvare1f448092008-04-29 14:03:37 +02001601 data->alarms |= lm85_read_value(client,
1602 EMC6D100_REG_ALARM3) << 16;
Guenter Roeck06923f82011-02-19 08:27:47 -08001603 } else if (data->type == emc6d102 || data->type == emc6d103 ||
1604 data->type == emc6d103s) {
Guenter Roeck09770b22012-01-14 20:47:36 -08001605 /*
1606 * Have to read LSB bits after the MSB ones because
1607 * the reading of the MSB bits has frozen the
1608 * LSBs (backward from the ADM1027).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
1610 int ext1 = lm85_read_value(client,
1611 EMC6D102_REG_EXTEND_ADC1);
1612 int ext2 = lm85_read_value(client,
1613 EMC6D102_REG_EXTEND_ADC2);
1614 int ext3 = lm85_read_value(client,
1615 EMC6D102_REG_EXTEND_ADC3);
1616 int ext4 = lm85_read_value(client,
1617 EMC6D102_REG_EXTEND_ADC4);
1618 data->in_ext[0] = ext3 & 0x0f;
1619 data->in_ext[1] = ext4 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001620 data->in_ext[2] = ext4 >> 4;
1621 data->in_ext[3] = ext3 >> 4;
1622 data->in_ext[4] = ext2 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 data->temp_ext[0] = ext1 & 0x0f;
1625 data->temp_ext[1] = ext2 & 0x0f;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001626 data->temp_ext[2] = ext1 >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628
Jean Delvare1f448092008-04-29 14:03:37 +02001629 data->last_reading = jiffies;
1630 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Jean Delvare1f448092008-04-29 14:03:37 +02001632 if (!data->valid ||
1633 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Things that don't change often */
1635 dev_dbg(&client->dev, "Reading config values\n");
1636
Jean Delvare9c516ef2005-11-26 20:07:54 +01001637 for (i = 0; i <= 3; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 data->in_min[i] =
1639 lm85_read_value(client, LM85_REG_IN_MIN(i));
1640 data->in_max[i] =
1641 lm85_read_value(client, LM85_REG_IN_MAX(i));
Jean Delvaree89e22b2008-06-25 08:47:35 -04001642 data->fan_min[i] =
1643 lm85_read_value(client, LM85_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
Guenter Roeckde248802011-02-25 08:19:42 -08001646 if (!data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +01001647 data->in_min[4] = lm85_read_value(client,
1648 LM85_REG_IN_MIN(4));
1649 data->in_max[4] = lm85_read_value(client,
1650 LM85_REG_IN_MAX(4));
1651 }
1652
Jean Delvare1f448092008-04-29 14:03:37 +02001653 if (data->type == emc6d100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 for (i = 5; i <= 7; ++i) {
Jean Delvare1f448092008-04-29 14:03:37 +02001655 data->in_min[i] = lm85_read_value(client,
1656 EMC6D100_REG_IN_MIN(i));
1657 data->in_max[i] = lm85_read_value(client,
1658 EMC6D100_REG_IN_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 }
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 for (i = 0; i <= 2; ++i) {
Jean Delvaree89e22b2008-06-25 08:47:35 -04001663 int val;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 data->temp_min[i] =
1666 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1667 data->temp_max[i] =
1668 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 data->autofan[i].config =
1671 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1672 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
Jean Delvare34e7dc62008-10-17 17:51:13 +02001673 data->pwm_freq[i] = val & 0x07;
Jean Delvaree89e22b2008-06-25 08:47:35 -04001674 data->zone[i].range = val >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 data->autofan[i].min_pwm =
1676 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1677 data->zone[i].limit =
1678 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1679 data->zone[i].critical =
1680 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001681
1682 if (IS_ADT7468_OFF64(data)) {
1683 data->temp_min[i] -= 64;
1684 data->temp_max[i] -= 64;
1685 data->zone[i].limit -= 64;
1686 data->zone[i].critical -= 64;
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
Guenter Roeck06923f82011-02-19 08:27:47 -08001690 if (data->type != emc6d103s) {
1691 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1692 data->autofan[0].min_off = (i & 0x20) != 0;
1693 data->autofan[1].min_off = (i & 0x40) != 0;
1694 data->autofan[2].min_off = (i & 0x80) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Guenter Roeck06923f82011-02-19 08:27:47 -08001696 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1697 data->zone[0].hyst = i >> 4;
1698 data->zone[1].hyst = i & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Guenter Roeck06923f82011-02-19 08:27:47 -08001700 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1701 data->zone[2].hyst = i >> 4;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 data->last_config = jiffies;
Jean Delvare1f448092008-04-29 14:03:37 +02001705 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 data->valid = 1;
1708
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001709 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 return data;
1712}
1713
Axel Linf0967ee2012-01-20 15:38:18 +08001714module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001717MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1718 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001719 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720MODULE_DESCRIPTION("LM85-B, LM85-C driver");