blob: 2b4b419273fe064c7a33e49f2e574253dce185c7 [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
Guenter Roeck09770b22012-01-14 20:47:36 -0800124/*
125 * Conversions. Rounding and limit checking is only done on the TO_REG
126 * variants. Note that you should be a bit careful with which arguments
127 * these macros are called: arguments may be evaluated more than once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300130/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400131static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200132 2500, 2250, 3300, 5000, 12000,
133 3300, 1500, 1800 /*EMC6D100*/
134};
135#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jean Delvare1f448092008-04-29 14:03:37 +0200137#define INS_TO_REG(n, val) \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800138 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jean Delvare1f448092008-04-29 14:03:37 +0200140#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200141 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvare1f448092008-04-29 14:03:37 +0200143#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200146static inline u16 FAN_TO_REG(unsigned long val)
147{
148 if (!val)
149 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800150 return clamp_val(5400000 / val, 1, 0xfffe);
Jean Delvare63f281a2007-07-05 20:39:40 +0200151}
Jean Delvare1f448092008-04-29 14:03:37 +0200152#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
153 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/* Temperature is reported in .001 degC increments */
156#define TEMP_TO_REG(val) \
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700157 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
Jean Delvare1f448092008-04-29 14:03:37 +0200158#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200159 SCALE(((val) << 4) + (ext), 16, 1000)
160#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Guenter Roeck2a844c12013-01-09 08:09:34 -0800162#define PWM_TO_REG(val) clamp_val(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define PWM_FROM_REG(val) (val)
164
165
Guenter Roeck09770b22012-01-14 20:47:36 -0800166/*
167 * ZONEs have the following parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * Limit (low) temp, 1. degC
169 * Hysteresis (below limit), 1. degC (0-15)
170 * Range of speed control, .1 degC (2-80)
171 * Critical (high) temp, 1. degC
172 *
173 * FAN PWMs have the following parameters:
174 * Reference Zone, 1, 2, 3, etc.
175 * Spinup time, .05 sec
176 * PWM value at limit/low temp, 1 count
177 * PWM Frequency, 1. Hz
178 * PWM is Min or OFF below limit, flag
179 * Invert PWM output, flag
180 *
181 * Some chips filter the temp, others the fan.
182 * Filter constant (or disabled) .1 seconds
183 */
184
185/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400186static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200187 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
188 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
189};
190
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700191static int RANGE_TO_REG(long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int i;
194
Jean Delvared38b1492008-04-03 10:40:39 +0200195 /* Find the closest match */
Jean Delvare1b92ada2008-10-17 17:51:14 +0200196 for (i = 0; i < 15; ++i) {
197 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Jean Delvared38b1492008-04-03 10:40:39 +0200200
Jean Delvare1b92ada2008-10-17 17:51:14 +0200201 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
Jean Delvare1f448092008-04-29 14:03:37 +0200203#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200206static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200207 10, 15, 23, 30, 38, 47, 61, 94
208};
209static const int adm1027_freq_map[8] = { /* 1 Hz */
210 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200211};
212
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700213static int FREQ_TO_REG(const int *map, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int i;
216
Jean Delvare86010c92008-10-17 17:51:13 +0200217 /* Find the closest match */
Jean Delvare1f448092008-04-29 14:03:37 +0200218 for (i = 0; i < 7; ++i)
Jean Delvare8a0795d2008-10-17 17:51:14 +0200219 if (freq <= (map[i] + map[i + 1]) / 2)
Jean Delvare1f448092008-04-29 14:03:37 +0200220 break;
Jean Delvaree89e22b2008-06-25 08:47:35 -0400221 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200223
224static int FREQ_FROM_REG(const int *map, u8 reg)
225{
226 return map[reg & 0x07];
227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Guenter Roeck09770b22012-01-14 20:47:36 -0800229/*
230 * Since we can't use strings, I'm abusing these numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * to stand in for the following meanings:
232 * 1 -- PWM responds to Zone 1
233 * 2 -- PWM responds to Zone 2
234 * 3 -- PWM responds to Zone 3
235 * 23 -- PWM responds to the higher temp of Zone 2 or 3
236 * 123 -- PWM responds to highest of Zone 1, 2, or 3
237 * 0 -- PWM is always at 0% (ie, off)
238 * -1 -- PWM is always at 100%
239 * -2 -- PWM responds to manual control
240 */
241
Jean Delvaree89e22b2008-06-25 08:47:35 -0400242static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
243#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jean Delvare1f448092008-04-29 14:03:37 +0200245static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 int i;
248
Jean Delvare1f448092008-04-29 14:03:37 +0200249 for (i = 0; i <= 7; ++i)
250 if (zone == lm85_zone_map[i])
251 break;
252 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400254 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Guenter Roeck2a844c12013-01-09 08:09:34 -0800257#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
Jean Delvare1f448092008-04-29 14:03:37 +0200258#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Guenter Roeck09770b22012-01-14 20:47:36 -0800260/*
261 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 * Some sensors are not updated more frequently than once per second
264 * so it doesn't make sense to read them more often than that.
265 * We cache the results and return the saved data if the driver
266 * is called again before a second has elapsed.
267 *
268 * Also, there is significant configuration data for this chip
269 * given the automatic PWM fan control that is possible. There
270 * are about 47 bytes of config data to only 22 bytes of actual
271 * readings. So, we keep the config data up to date in the cache
272 * when it is written and only sample it once every 1 *minute*
273 */
274#define LM85_DATA_INTERVAL (HZ + HZ / 2)
275#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
276
Guenter Roeck09770b22012-01-14 20:47:36 -0800277/*
278 * LM85 can automatically adjust fan speeds based on temperature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * This structure encapsulates an entire Zone config. There are
280 * three zones (one for each temperature input) on the lm85
281 */
282struct lm85_zone {
283 s8 limit; /* Low temp limit */
284 u8 hyst; /* Low limit hysteresis. (0-15) */
285 u8 range; /* Temp range, encoded */
286 s8 critical; /* "All fans ON" temp limit */
Guenter Roeck09770b22012-01-14 20:47:36 -0800287 u8 max_desired; /*
288 * Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * to prevent "drift" as other autofan control
290 * values change.
291 */
292};
293
294struct lm85_autofan {
295 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 u8 min_pwm; /* Minimum PWM value, encoded */
297 u8 min_off; /* Min PWM or OFF below "limit", flag */
298};
299
Guenter Roeck09770b22012-01-14 20:47:36 -0800300/*
301 * For each registered chip, we need to keep some data in memory.
302 * The structure is dynamically allocated.
303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304struct lm85_data {
Axel Lin746f6882014-07-23 12:29:11 +0800305 struct i2c_client *client;
306 const struct attribute_group *groups[6];
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
Axel Lin6fd5dd52014-07-23 12:27:39 +0800337static int lm85_read_value(struct i2c_client *client, u8 reg)
338{
339 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Axel Lin6fd5dd52014-07-23 12:27:39 +0800341 /* What size location is it? */
342 switch (reg) {
343 case LM85_REG_FAN(0): /* Read WORD data */
344 case LM85_REG_FAN(1):
345 case LM85_REG_FAN(2):
346 case LM85_REG_FAN(3):
347 case LM85_REG_FAN_MIN(0):
348 case LM85_REG_FAN_MIN(1):
349 case LM85_REG_FAN_MIN(2):
350 case LM85_REG_FAN_MIN(3):
351 case LM85_REG_ALARM1: /* Read both bytes at once */
352 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
353 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
354 break;
355 default: /* Read BYTE data */
356 res = i2c_smbus_read_byte_data(client, reg);
357 break;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Axel Lin6fd5dd52014-07-23 12:27:39 +0800360 return res;
361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Axel Lin6fd5dd52014-07-23 12:27:39 +0800363static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
364{
365 switch (reg) {
366 case LM85_REG_FAN(0): /* Write WORD data */
367 case LM85_REG_FAN(1):
368 case LM85_REG_FAN(2):
369 case LM85_REG_FAN(3):
370 case LM85_REG_FAN_MIN(0):
371 case LM85_REG_FAN_MIN(1):
372 case LM85_REG_FAN_MIN(2):
373 case LM85_REG_FAN_MIN(3):
374 /* NOTE: ALARM is read only, so not included here */
375 i2c_smbus_write_byte_data(client, reg, value & 0xff);
376 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
377 break;
378 default: /* Write BYTE data */
379 i2c_smbus_write_byte_data(client, reg, value);
380 break;
381 }
382}
Jean Delvare67712d02008-10-17 17:51:14 +0200383
Axel Lin6fd5dd52014-07-23 12:27:39 +0800384static struct lm85_data *lm85_update_device(struct device *dev)
385{
Axel Lin746f6882014-07-23 12:29:11 +0800386 struct lm85_data *data = dev_get_drvdata(dev);
387 struct i2c_client *client = data->client;
Axel Lin6fd5dd52014-07-23 12:27:39 +0800388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Axel Lin6fd5dd52014-07-23 12:27:39 +0800390 mutex_lock(&data->update_lock);
391
392 if (!data->valid ||
393 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
394 /* Things that change quickly */
395 dev_dbg(&client->dev, "Reading sensor values\n");
396
397 /*
398 * Have to read extended bits first to "freeze" the
399 * more significant bits that are read later.
400 * There are 2 additional resolution bits per channel and we
401 * have room for 4, so we shift them to the left.
402 */
403 if (data->type == adm1027 || data->type == adt7463 ||
404 data->type == adt7468) {
405 int ext1 = lm85_read_value(client,
406 ADM1027_REG_EXTEND_ADC1);
407 int ext2 = lm85_read_value(client,
408 ADM1027_REG_EXTEND_ADC2);
409 int val = (ext1 << 8) + ext2;
410
411 for (i = 0; i <= 4; i++)
412 data->in_ext[i] =
413 ((val >> (i * 2)) & 0x03) << 2;
414
415 for (i = 0; i <= 2; i++)
416 data->temp_ext[i] =
417 (val >> ((i + 4) * 2)) & 0x0c;
418 }
419
420 data->vid = lm85_read_value(client, LM85_REG_VID);
421
422 for (i = 0; i <= 3; ++i) {
423 data->in[i] =
424 lm85_read_value(client, LM85_REG_IN(i));
425 data->fan[i] =
426 lm85_read_value(client, LM85_REG_FAN(i));
427 }
428
429 if (!data->has_vid5)
430 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
431
432 if (data->type == adt7468)
433 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
434
435 for (i = 0; i <= 2; ++i) {
436 data->temp[i] =
437 lm85_read_value(client, LM85_REG_TEMP(i));
438 data->pwm[i] =
439 lm85_read_value(client, LM85_REG_PWM(i));
440
441 if (IS_ADT7468_OFF64(data))
442 data->temp[i] -= 64;
443 }
444
445 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
446
447 if (data->type == emc6d100) {
448 /* Three more voltage sensors */
449 for (i = 5; i <= 7; ++i) {
450 data->in[i] = lm85_read_value(client,
451 EMC6D100_REG_IN(i));
452 }
453 /* More alarm bits */
454 data->alarms |= lm85_read_value(client,
455 EMC6D100_REG_ALARM3) << 16;
456 } else if (data->type == emc6d102 || data->type == emc6d103 ||
457 data->type == emc6d103s) {
458 /*
459 * Have to read LSB bits after the MSB ones because
460 * the reading of the MSB bits has frozen the
461 * LSBs (backward from the ADM1027).
462 */
463 int ext1 = lm85_read_value(client,
464 EMC6D102_REG_EXTEND_ADC1);
465 int ext2 = lm85_read_value(client,
466 EMC6D102_REG_EXTEND_ADC2);
467 int ext3 = lm85_read_value(client,
468 EMC6D102_REG_EXTEND_ADC3);
469 int ext4 = lm85_read_value(client,
470 EMC6D102_REG_EXTEND_ADC4);
471 data->in_ext[0] = ext3 & 0x0f;
472 data->in_ext[1] = ext4 & 0x0f;
473 data->in_ext[2] = ext4 >> 4;
474 data->in_ext[3] = ext3 >> 4;
475 data->in_ext[4] = ext2 >> 4;
476
477 data->temp_ext[0] = ext1 & 0x0f;
478 data->temp_ext[1] = ext2 & 0x0f;
479 data->temp_ext[2] = ext1 >> 4;
480 }
481
482 data->last_reading = jiffies;
483 } /* last_reading */
484
485 if (!data->valid ||
486 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
487 /* Things that don't change often */
488 dev_dbg(&client->dev, "Reading config values\n");
489
490 for (i = 0; i <= 3; ++i) {
491 data->in_min[i] =
492 lm85_read_value(client, LM85_REG_IN_MIN(i));
493 data->in_max[i] =
494 lm85_read_value(client, LM85_REG_IN_MAX(i));
495 data->fan_min[i] =
496 lm85_read_value(client, LM85_REG_FAN_MIN(i));
497 }
498
499 if (!data->has_vid5) {
500 data->in_min[4] = lm85_read_value(client,
501 LM85_REG_IN_MIN(4));
502 data->in_max[4] = lm85_read_value(client,
503 LM85_REG_IN_MAX(4));
504 }
505
506 if (data->type == emc6d100) {
507 for (i = 5; i <= 7; ++i) {
508 data->in_min[i] = lm85_read_value(client,
509 EMC6D100_REG_IN_MIN(i));
510 data->in_max[i] = lm85_read_value(client,
511 EMC6D100_REG_IN_MAX(i));
512 }
513 }
514
515 for (i = 0; i <= 2; ++i) {
516 int val;
517
518 data->temp_min[i] =
519 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
520 data->temp_max[i] =
521 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
522
523 data->autofan[i].config =
524 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
525 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
526 data->pwm_freq[i] = val & 0x07;
527 data->zone[i].range = val >> 4;
528 data->autofan[i].min_pwm =
529 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
530 data->zone[i].limit =
531 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
532 data->zone[i].critical =
533 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
534
535 if (IS_ADT7468_OFF64(data)) {
536 data->temp_min[i] -= 64;
537 data->temp_max[i] -= 64;
538 data->zone[i].limit -= 64;
539 data->zone[i].critical -= 64;
540 }
541 }
542
543 if (data->type != emc6d103s) {
544 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
545 data->autofan[0].min_off = (i & 0x20) != 0;
546 data->autofan[1].min_off = (i & 0x40) != 0;
547 data->autofan[2].min_off = (i & 0x80) != 0;
548
549 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
550 data->zone[0].hyst = i >> 4;
551 data->zone[1].hyst = i & 0x0f;
552
553 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
554 data->zone[2].hyst = i >> 4;
555 }
556
557 data->last_config = jiffies;
558 } /* last_config */
559
560 data->valid = 1;
561
562 mutex_unlock(&data->update_lock);
563
564 return data;
565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200568static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
569 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jean Delvareb353a482007-07-05 20:36:12 +0200571 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200573 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Jean Delvareb353a482007-07-05 20:36:12 +0200575
576static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
577 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jean Delvareb353a482007-07-05 20:36:12 +0200579 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200581 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
Jean Delvareb353a482007-07-05 20:36:12 +0200583
584static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
585 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Jean Delvareb353a482007-07-05 20:36:12 +0200587 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800588 struct lm85_data *data = dev_get_drvdata(dev);
589 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800590 unsigned long val;
591 int err;
592
593 err = kstrtoul(buf, 10, &val);
594 if (err)
595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100597 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 data->fan_min[nr] = FAN_TO_REG(val);
599 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100600 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return count;
602}
603
604#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200605static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
606 show_fan, NULL, offset - 1); \
607static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
608 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610show_fan_offset(1);
611show_fan_offset(2);
612show_fan_offset(3);
613show_fan_offset(4);
614
615/* vid, vrm, alarms */
616
Jean Delvare1f448092008-04-29 14:03:37 +0200617static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
618 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100621 int vid;
622
Guenter Roeckde248802011-02-25 08:19:42 -0800623 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100624 /* 6-pin VID (VRM 10) */
625 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
626 } else {
627 /* 5-pin VID (VRM 9) */
628 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
629 }
630
631 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
635
Jean Delvare1f448092008-04-29 14:03:37 +0200636static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
637 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Jean Delvare90d66192007-10-08 18:24:35 +0200639 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return sprintf(buf, "%ld\n", (long) data->vrm);
641}
642
Jean Delvare1f448092008-04-29 14:03:37 +0200643static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
644 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100646 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b22012-01-14 20:47:36 -0800647 unsigned long val;
648 int err;
649
650 err = kstrtoul(buf, 10, &val);
651 if (err)
652 return err;
653
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700654 if (val > 255)
655 return -EINVAL;
656
Guenter Roeck09770b22012-01-14 20:47:36 -0800657 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return count;
659}
660
661static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
662
Jean Delvare1f448092008-04-29 14:03:37 +0200663static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
664 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200667 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
671
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200672static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
673 char *buf)
674{
675 int nr = to_sensor_dev_attr(attr)->index;
676 struct lm85_data *data = lm85_update_device(dev);
677 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
678}
679
680static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
681static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
682static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
683static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
684static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
685static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
686static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
687static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
688static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
689static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
690static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
691static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
692static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
693static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
694static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
695static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
696static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/* pwm */
699
Jean Delvareb353a482007-07-05 20:36:12 +0200700static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
701 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jean Delvareb353a482007-07-05 20:36:12 +0200703 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200705 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Jean Delvareb353a482007-07-05 20:36:12 +0200707
708static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
709 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jean Delvareb353a482007-07-05 20:36:12 +0200711 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800712 struct lm85_data *data = dev_get_drvdata(dev);
713 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800714 unsigned long val;
715 int err;
716
717 err = kstrtoul(buf, 10, &val);
718 if (err)
719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100721 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 data->pwm[nr] = PWM_TO_REG(val);
723 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100724 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return count;
726}
Jean Delvareb353a482007-07-05 20:36:12 +0200727
728static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
729 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Jean Delvareb353a482007-07-05 20:36:12 +0200731 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100733 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100736 switch (pwm_zone) {
737 case -1: /* PWM is always at 100% */
738 enable = 0;
739 break;
740 case 0: /* PWM is always at 0% */
741 case -2: /* PWM responds to manual control */
742 enable = 1;
743 break;
744 default: /* PWM in automatic mode */
745 enable = 2;
746 }
747 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Jean Delvare455f7912007-12-03 23:28:42 +0100750static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
751 *attr, const char *buf, size_t count)
752{
753 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800754 struct lm85_data *data = dev_get_drvdata(dev);
755 struct i2c_client *client = data->client;
Jean Delvare455f7912007-12-03 23:28:42 +0100756 u8 config;
Guenter Roeck09770b22012-01-14 20:47:36 -0800757 unsigned long val;
758 int err;
759
760 err = kstrtoul(buf, 10, &val);
761 if (err)
762 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100763
764 switch (val) {
765 case 0:
766 config = 3;
767 break;
768 case 1:
769 config = 7;
770 break;
771 case 2:
Guenter Roeck09770b22012-01-14 20:47:36 -0800772 /*
773 * Here we have to choose arbitrarily one of the 5 possible
774 * configurations; I go for the safest
775 */
Jean Delvare455f7912007-12-03 23:28:42 +0100776 config = 6;
777 break;
778 default:
779 return -EINVAL;
780 }
781
782 mutex_lock(&data->update_lock);
783 data->autofan[nr].config = lm85_read_value(client,
784 LM85_REG_AFAN_CONFIG(nr));
785 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
786 | (config << 5);
787 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
788 data->autofan[nr].config);
789 mutex_unlock(&data->update_lock);
790 return count;
791}
792
Jean Delvare34e7dc62008-10-17 17:51:13 +0200793static ssize_t show_pwm_freq(struct device *dev,
794 struct device_attribute *attr, char *buf)
795{
796 int nr = to_sensor_dev_attr(attr)->index;
797 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200798 int freq;
799
800 if (IS_ADT7468_HFPWM(data))
801 freq = 22500;
802 else
803 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
804
805 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200806}
807
808static ssize_t set_pwm_freq(struct device *dev,
809 struct device_attribute *attr, const char *buf, size_t count)
810{
811 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800812 struct lm85_data *data = dev_get_drvdata(dev);
813 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800814 unsigned long val;
815 int err;
816
817 err = kstrtoul(buf, 10, &val);
818 if (err)
819 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200820
821 mutex_lock(&data->update_lock);
Guenter Roeck09770b22012-01-14 20:47:36 -0800822 /*
823 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200824 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b22012-01-14 20:47:36 -0800825 * This might confuse the user, but there's not much we can do.
826 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200827 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
828 data->cfg5 &= ~ADT7468_HFPWM;
829 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
830 } else { /* Low freq. mode */
831 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
832 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
833 (data->zone[nr].range << 4)
834 | data->pwm_freq[nr]);
835 if (data->type == adt7468) {
836 data->cfg5 |= ADT7468_HFPWM;
837 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
838 }
839 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200840 mutex_unlock(&data->update_lock);
841 return count;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200845static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
846 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100847static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200848 show_pwm_enable, set_pwm_enable, offset - 1); \
849static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
850 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852show_pwm_reg(1);
853show_pwm_reg(2);
854show_pwm_reg(3);
855
856/* Voltages */
857
Jean Delvareb353a482007-07-05 20:36:12 +0200858static ssize_t show_in(struct device *dev, struct device_attribute *attr,
859 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jean Delvareb353a482007-07-05 20:36:12 +0200861 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200863 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
864 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Jean Delvareb353a482007-07-05 20:36:12 +0200866
Jean Delvare1f448092008-04-29 14:03:37 +0200867static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200868 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Jean Delvareb353a482007-07-05 20:36:12 +0200870 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200872 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Jean Delvareb353a482007-07-05 20:36:12 +0200874
875static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
876 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jean Delvareb353a482007-07-05 20:36:12 +0200878 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800879 struct lm85_data *data = dev_get_drvdata(dev);
880 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800881 long val;
882 int err;
883
884 err = kstrtol(buf, 10, &val);
885 if (err)
886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100888 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 data->in_min[nr] = INS_TO_REG(nr, val);
890 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100891 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return count;
893}
Jean Delvareb353a482007-07-05 20:36:12 +0200894
895static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
896 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jean Delvareb353a482007-07-05 20:36:12 +0200898 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200900 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Jean Delvareb353a482007-07-05 20:36:12 +0200902
903static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
904 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jean Delvareb353a482007-07-05 20:36:12 +0200906 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800907 struct lm85_data *data = dev_get_drvdata(dev);
908 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800909 long val;
910 int err;
911
912 err = kstrtol(buf, 10, &val);
913 if (err)
914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100916 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 data->in_max[nr] = INS_TO_REG(nr, val);
918 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100919 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return count;
921}
Jean Delvareb353a482007-07-05 20:36:12 +0200922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200924static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
925 show_in, NULL, offset); \
926static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
927 show_in_min, set_in_min, offset); \
928static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
929 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931show_in_reg(0);
932show_in_reg(1);
933show_in_reg(2);
934show_in_reg(3);
935show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200936show_in_reg(5);
937show_in_reg(6);
938show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940/* Temps */
941
Jean Delvareb353a482007-07-05 20:36:12 +0200942static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
943 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jean Delvareb353a482007-07-05 20:36:12 +0200945 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200947 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
948 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Jean Delvareb353a482007-07-05 20:36:12 +0200950
951static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
952 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Jean Delvareb353a482007-07-05 20:36:12 +0200954 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200956 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
Jean Delvareb353a482007-07-05 20:36:12 +0200958
959static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
960 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Jean Delvareb353a482007-07-05 20:36:12 +0200962 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800963 struct lm85_data *data = dev_get_drvdata(dev);
964 struct i2c_client *client = data->client;
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
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800972 if (IS_ADT7468_OFF64(data))
973 val += 64;
974
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100975 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 data->temp_min[nr] = TEMP_TO_REG(val);
977 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100978 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return count;
980}
Jean Delvareb353a482007-07-05 20:36:12 +0200981
982static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
983 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jean Delvareb353a482007-07-05 20:36:12 +0200985 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200987 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Jean Delvareb353a482007-07-05 20:36:12 +0200989
990static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
991 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Jean Delvareb353a482007-07-05 20:36:12 +0200993 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800994 struct lm85_data *data = dev_get_drvdata(dev);
995 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800996 long val;
997 int err;
998
999 err = kstrtol(buf, 10, &val);
1000 if (err)
1001 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001003 if (IS_ADT7468_OFF64(data))
1004 val += 64;
1005
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001006 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 data->temp_max[nr] = TEMP_TO_REG(val);
1008 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001009 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return count;
1011}
Jean Delvareb353a482007-07-05 20:36:12 +02001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001014static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1015 show_temp, NULL, offset - 1); \
1016static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1017 show_temp_min, set_temp_min, offset - 1); \
1018static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1019 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021show_temp_reg(1);
1022show_temp_reg(2);
1023show_temp_reg(3);
1024
1025
1026/* Automatic PWM control */
1027
Jean Delvareb353a482007-07-05 20:36:12 +02001028static ssize_t show_pwm_auto_channels(struct device *dev,
1029 struct device_attribute *attr, char *buf)
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 lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001033 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
Jean Delvareb353a482007-07-05 20:36:12 +02001035
1036static ssize_t set_pwm_auto_channels(struct device *dev,
1037 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Jean Delvareb353a482007-07-05 20:36:12 +02001039 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001040 struct lm85_data *data = dev_get_drvdata(dev);
1041 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001042 long val;
1043 int err;
1044
1045 err = kstrtol(buf, 10, &val);
1046 if (err)
1047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001049 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +02001051 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1053 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001054 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return count;
1056}
Jean Delvareb353a482007-07-05 20:36:12 +02001057
1058static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1059 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Jean Delvareb353a482007-07-05 20:36:12 +02001061 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001063 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
Jean Delvareb353a482007-07-05 20:36:12 +02001065
1066static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1067 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Jean Delvareb353a482007-07-05 20:36:12 +02001069 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001070 struct lm85_data *data = dev_get_drvdata(dev);
1071 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001072 unsigned long val;
1073 int err;
1074
1075 err = kstrtoul(buf, 10, &val);
1076 if (err)
1077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001079 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1081 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1082 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001083 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return count;
1085}
Jean Delvareb353a482007-07-05 20:36:12 +02001086
1087static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1088 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Jean Delvareb353a482007-07-05 20:36:12 +02001090 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001092 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
Jean Delvareb353a482007-07-05 20:36:12 +02001094
1095static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1096 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Jean Delvareb353a482007-07-05 20:36:12 +02001098 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001099 struct lm85_data *data = dev_get_drvdata(dev);
1100 struct i2c_client *client = data->client;
Jean Delvare7133e562008-04-12 19:56:35 +02001101 u8 tmp;
Guenter Roeck09770b22012-01-14 20:47:36 -08001102 long val;
1103 int err;
1104
1105 err = kstrtol(buf, 10, &val);
1106 if (err)
1107 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001109 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +02001111 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1112 tmp &= ~(0x20 << nr);
1113 if (data->autofan[nr].min_off)
1114 tmp |= 0x20 << nr;
1115 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001116 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return count;
1118}
Jean Delvareb353a482007-07-05 20:36:12 +02001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001121static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1122 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1123 set_pwm_auto_channels, offset - 1); \
1124static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
1125 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
1126 set_pwm_auto_pwm_min, offset - 1); \
1127static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1128 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +02001129 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +02001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131pwm_auto(1);
1132pwm_auto(2);
1133pwm_auto(3);
1134
1135/* Temperature settings for automatic PWM control */
1136
Jean Delvareb353a482007-07-05 20:36:12 +02001137static ssize_t show_temp_auto_temp_off(struct device *dev,
1138 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Jean Delvareb353a482007-07-05 20:36:12 +02001140 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001142 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 HYST_FROM_REG(data->zone[nr].hyst));
1144}
Jean Delvareb353a482007-07-05 20:36:12 +02001145
1146static ssize_t set_temp_auto_temp_off(struct device *dev,
1147 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Jean Delvareb353a482007-07-05 20:36:12 +02001149 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001150 struct lm85_data *data = dev_get_drvdata(dev);
1151 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001153 long val;
1154 int err;
1155
1156 err = kstrtol(buf, 10, &val);
1157 if (err)
1158 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001160 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +02001163 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1165 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +02001166 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else {
1168 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +02001169 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001171 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return count;
1173}
Jean Delvareb353a482007-07-05 20:36:12 +02001174
1175static ssize_t show_temp_auto_temp_min(struct device *dev,
1176 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Jean Delvareb353a482007-07-05 20:36:12 +02001178 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001180 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
Jean Delvareb353a482007-07-05 20:36:12 +02001182
1183static ssize_t set_temp_auto_temp_min(struct device *dev,
1184 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Jean Delvareb353a482007-07-05 20:36:12 +02001186 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001187 struct lm85_data *data = dev_get_drvdata(dev);
1188 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001189 long val;
1190 int err;
1191
1192 err = kstrtol(buf, 10, &val);
1193 if (err)
1194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001196 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 data->zone[nr].limit = TEMP_TO_REG(val);
1198 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1199 data->zone[nr].limit);
1200
1201/* Update temp_auto_max and temp_auto_range */
1202 data->zone[nr].range = RANGE_TO_REG(
1203 TEMP_FROM_REG(data->zone[nr].max_desired) -
1204 TEMP_FROM_REG(data->zone[nr].limit));
1205 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1206 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001207 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001209 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return count;
1211}
Jean Delvareb353a482007-07-05 20:36:12 +02001212
1213static ssize_t show_temp_auto_temp_max(struct device *dev,
1214 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Jean Delvareb353a482007-07-05 20:36:12 +02001216 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001218 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 RANGE_FROM_REG(data->zone[nr].range));
1220}
Jean Delvareb353a482007-07-05 20:36:12 +02001221
1222static ssize_t set_temp_auto_temp_max(struct device *dev,
1223 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Jean Delvareb353a482007-07-05 20:36:12 +02001225 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001226 struct lm85_data *data = dev_get_drvdata(dev);
1227 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001229 long val;
1230 int err;
1231
1232 err = kstrtol(buf, 10, &val);
1233 if (err)
1234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001236 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 min = TEMP_FROM_REG(data->zone[nr].limit);
1238 data->zone[nr].max_desired = TEMP_TO_REG(val);
1239 data->zone[nr].range = RANGE_TO_REG(
1240 val - min);
1241 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1242 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001243 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001244 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return count;
1246}
Jean Delvareb353a482007-07-05 20:36:12 +02001247
1248static ssize_t show_temp_auto_temp_crit(struct device *dev,
1249 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Jean Delvareb353a482007-07-05 20:36:12 +02001251 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001253 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
Jean Delvareb353a482007-07-05 20:36:12 +02001255
1256static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +02001257 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Jean Delvareb353a482007-07-05 20:36:12 +02001259 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001260 struct lm85_data *data = dev_get_drvdata(dev);
1261 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001262 long val;
1263 int err;
1264
1265 err = kstrtol(buf, 10, &val);
1266 if (err)
1267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001269 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 data->zone[nr].critical = TEMP_TO_REG(val);
1271 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1272 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001273 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return count;
1275}
Jean Delvareb353a482007-07-05 20:36:12 +02001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001278static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1279 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1280 set_temp_auto_temp_off, offset - 1); \
1281static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1282 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1283 set_temp_auto_temp_min, offset - 1); \
1284static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1285 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1286 set_temp_auto_temp_max, offset - 1); \
1287static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1288 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1289 set_temp_auto_temp_crit, offset - 1);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291temp_auto(1);
1292temp_auto(2);
1293temp_auto(3);
1294
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001295static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001296 &sensor_dev_attr_fan1_input.dev_attr.attr,
1297 &sensor_dev_attr_fan2_input.dev_attr.attr,
1298 &sensor_dev_attr_fan3_input.dev_attr.attr,
1299 &sensor_dev_attr_fan4_input.dev_attr.attr,
1300 &sensor_dev_attr_fan1_min.dev_attr.attr,
1301 &sensor_dev_attr_fan2_min.dev_attr.attr,
1302 &sensor_dev_attr_fan3_min.dev_attr.attr,
1303 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001304 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1305 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1306 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1307 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001308
1309 &sensor_dev_attr_pwm1.dev_attr.attr,
1310 &sensor_dev_attr_pwm2.dev_attr.attr,
1311 &sensor_dev_attr_pwm3.dev_attr.attr,
1312 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1313 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1314 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001315 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1316 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1317 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001318
1319 &sensor_dev_attr_in0_input.dev_attr.attr,
1320 &sensor_dev_attr_in1_input.dev_attr.attr,
1321 &sensor_dev_attr_in2_input.dev_attr.attr,
1322 &sensor_dev_attr_in3_input.dev_attr.attr,
1323 &sensor_dev_attr_in0_min.dev_attr.attr,
1324 &sensor_dev_attr_in1_min.dev_attr.attr,
1325 &sensor_dev_attr_in2_min.dev_attr.attr,
1326 &sensor_dev_attr_in3_min.dev_attr.attr,
1327 &sensor_dev_attr_in0_max.dev_attr.attr,
1328 &sensor_dev_attr_in1_max.dev_attr.attr,
1329 &sensor_dev_attr_in2_max.dev_attr.attr,
1330 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001331 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1332 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1333 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1334 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001335
1336 &sensor_dev_attr_temp1_input.dev_attr.attr,
1337 &sensor_dev_attr_temp2_input.dev_attr.attr,
1338 &sensor_dev_attr_temp3_input.dev_attr.attr,
1339 &sensor_dev_attr_temp1_min.dev_attr.attr,
1340 &sensor_dev_attr_temp2_min.dev_attr.attr,
1341 &sensor_dev_attr_temp3_min.dev_attr.attr,
1342 &sensor_dev_attr_temp1_max.dev_attr.attr,
1343 &sensor_dev_attr_temp2_max.dev_attr.attr,
1344 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001345 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1346 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1347 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1348 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1349 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001350
1351 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1352 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1353 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1354 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1355 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1356 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001357
Jean Delvareb353a482007-07-05 20:36:12 +02001358 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1359 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1360 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1361 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1362 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1363 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1364 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1365 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1366 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1367
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001368 &dev_attr_vrm.attr,
1369 &dev_attr_cpu0_vid.attr,
1370 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001371 NULL
1372};
1373
1374static const struct attribute_group lm85_group = {
1375 .attrs = lm85_attributes,
1376};
1377
Guenter Roeck06923f82011-02-19 08:27:47 -08001378static struct attribute *lm85_attributes_minctl[] = {
1379 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1380 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1381 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001382 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001383};
1384
1385static const struct attribute_group lm85_group_minctl = {
1386 .attrs = lm85_attributes_minctl,
1387};
1388
1389static struct attribute *lm85_attributes_temp_off[] = {
1390 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1391 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1392 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001393 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001394};
1395
1396static const struct attribute_group lm85_group_temp_off = {
1397 .attrs = lm85_attributes_temp_off,
1398};
1399
Jean Delvare6b9aad22007-07-05 20:37:21 +02001400static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001401 &sensor_dev_attr_in4_input.dev_attr.attr,
1402 &sensor_dev_attr_in4_min.dev_attr.attr,
1403 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001404 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001405 NULL
1406};
1407
Jean Delvare6b9aad22007-07-05 20:37:21 +02001408static const struct attribute_group lm85_group_in4 = {
1409 .attrs = lm85_attributes_in4,
1410};
1411
1412static struct attribute *lm85_attributes_in567[] = {
1413 &sensor_dev_attr_in5_input.dev_attr.attr,
1414 &sensor_dev_attr_in6_input.dev_attr.attr,
1415 &sensor_dev_attr_in7_input.dev_attr.attr,
1416 &sensor_dev_attr_in5_min.dev_attr.attr,
1417 &sensor_dev_attr_in6_min.dev_attr.attr,
1418 &sensor_dev_attr_in7_min.dev_attr.attr,
1419 &sensor_dev_attr_in5_max.dev_attr.attr,
1420 &sensor_dev_attr_in6_max.dev_attr.attr,
1421 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001422 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1423 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1424 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001425 NULL
1426};
1427
1428static const struct attribute_group lm85_group_in567 = {
1429 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001430};
1431
Jean Delvare5f44759472008-06-25 09:10:30 -04001432static void lm85_init_client(struct i2c_client *client)
1433{
1434 int value;
1435
1436 /* Start monitoring if needed */
1437 value = lm85_read_value(client, LM85_REG_CONFIG);
1438 if (!(value & 0x01)) {
1439 dev_info(&client->dev, "Starting monitoring\n");
1440 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1441 }
1442
1443 /* Warn about unusual configuration bits */
1444 if (value & 0x02)
1445 dev_warn(&client->dev, "Device configuration is locked\n");
1446 if (!(value & 0x04))
1447 dev_warn(&client->dev, "Device is not ready\n");
1448}
1449
Jean Delvare5cfaf332009-09-15 17:18:14 +02001450static int lm85_is_fake(struct i2c_client *client)
1451{
1452 /*
1453 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1454 * emulate the former except that it has no hardware monitoring function
1455 * so the readings are always 0.
1456 */
1457 int i;
1458 u8 in_temp, fan;
1459
1460 for (i = 0; i < 8; i++) {
1461 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1462 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1463 if (in_temp != 0x00 || fan != 0xff)
1464 return 0;
1465 }
1466
1467 return 1;
1468}
1469
Jean Delvare67712d02008-10-17 17:51:14 +02001470/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001471static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Jean Delvare67712d02008-10-17 17:51:14 +02001473 struct i2c_adapter *adapter = client->adapter;
1474 int address = client->addr;
Jean Delvare590e8532014-06-11 18:35:56 +02001475 const char *type_name = NULL;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001476 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Jean Delvaree89e22b2008-06-25 08:47:35 -04001478 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001480 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jean Delvared42a2eb2009-12-09 20:35:53 +01001483 /* Determine the chip type */
1484 company = lm85_read_value(client, LM85_REG_COMPANY);
1485 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Guenter Roeckb55f3752013-01-10 10:01:24 -08001487 dev_dbg(&adapter->dev,
1488 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001489 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jean Delvared42a2eb2009-12-09 20:35:53 +01001491 if (company == LM85_COMPANY_NATIONAL) {
1492 switch (verstep) {
1493 case LM85_VERSTEP_LM85C:
1494 type_name = "lm85c";
1495 break;
1496 case LM85_VERSTEP_LM85B:
1497 type_name = "lm85b";
1498 break;
1499 case LM85_VERSTEP_LM96000_1:
1500 case LM85_VERSTEP_LM96000_2:
1501 /* Check for Winbond WPCD377I */
1502 if (lm85_is_fake(client)) {
1503 dev_dbg(&adapter->dev,
1504 "Found Winbond WPCD377I, ignoring\n");
1505 return -ENODEV;
1506 }
Jean Delvare590e8532014-06-11 18:35:56 +02001507 type_name = "lm85";
Jean Delvared42a2eb2009-12-09 20:35:53 +01001508 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001509 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001510 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1511 switch (verstep) {
1512 case LM85_VERSTEP_ADM1027:
1513 type_name = "adm1027";
1514 break;
1515 case LM85_VERSTEP_ADT7463:
1516 case LM85_VERSTEP_ADT7463C:
1517 type_name = "adt7463";
1518 break;
1519 case LM85_VERSTEP_ADT7468_1:
1520 case LM85_VERSTEP_ADT7468_2:
1521 type_name = "adt7468";
1522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001524 } else if (company == LM85_COMPANY_SMSC) {
1525 switch (verstep) {
1526 case LM85_VERSTEP_EMC6D100_A0:
1527 case LM85_VERSTEP_EMC6D100_A1:
1528 /* Note: we can't tell a '100 from a '101 */
1529 type_name = "emc6d100";
1530 break;
1531 case LM85_VERSTEP_EMC6D102:
1532 type_name = "emc6d102";
1533 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001534 case LM85_VERSTEP_EMC6D103_A0:
1535 case LM85_VERSTEP_EMC6D103_A1:
1536 type_name = "emc6d103";
1537 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001538 case LM85_VERSTEP_EMC6D103S:
1539 type_name = "emc6d103s";
1540 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Jean Delvare590e8532014-06-11 18:35:56 +02001544 if (!type_name)
1545 return -ENODEV;
1546
Jean Delvare67712d02008-10-17 17:51:14 +02001547 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Jean Delvare67712d02008-10-17 17:51:14 +02001549 return 0;
1550}
1551
Axel Lin746f6882014-07-23 12:29:11 +08001552static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001553{
Axel Lin746f6882014-07-23 12:29:11 +08001554 struct device *dev = &client->dev;
1555 struct device *hwmon_dev;
Jean Delvare67712d02008-10-17 17:51:14 +02001556 struct lm85_data *data;
Axel Lin746f6882014-07-23 12:29:11 +08001557 int idx = 0;
Jean Delvare67712d02008-10-17 17:51:14 +02001558
Axel Lin746f6882014-07-23 12:29:11 +08001559 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001560 if (!data)
1561 return -ENOMEM;
1562
Axel Lin746f6882014-07-23 12:29:11 +08001563 data->client = client;
Jean Delvare67712d02008-10-17 17:51:14 +02001564 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001565 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Jean Delvare67712d02008-10-17 17:51:14 +02001567 /* Fill in the chip specific driver values */
1568 switch (data->type) {
1569 case adm1027:
1570 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001571 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001572 case emc6d100:
1573 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001574 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001575 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001576 data->freq_map = adm1027_freq_map;
1577 break;
1578 default:
1579 data->freq_map = lm85_freq_map;
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001583 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001586 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Axel Lin746f6882014-07-23 12:29:11 +08001588 /* sysfs hooks */
1589 data->groups[idx++] = &lm85_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Guenter Roeck06923f82011-02-19 08:27:47 -08001591 /* minctl and temp_off exist on all chips except emc6d103s */
1592 if (data->type != emc6d103s) {
Axel Lin746f6882014-07-23 12:29:11 +08001593 data->groups[idx++] = &lm85_group_minctl;
1594 data->groups[idx++] = &lm85_group_temp_off;
Guenter Roeck06923f82011-02-19 08:27:47 -08001595 }
1596
Guenter Roeck09770b22012-01-14 20:47:36 -08001597 /*
1598 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1599 * as a sixth digital VID input rather than an analog input.
1600 */
Guenter Roeckde248802011-02-25 08:19:42 -08001601 if (data->type == adt7463 || data->type == adt7468) {
1602 u8 vid = lm85_read_value(client, LM85_REG_VID);
1603 if (vid & 0x80)
1604 data->has_vid5 = true;
1605 }
1606
Axel Lin746f6882014-07-23 12:29:11 +08001607 if (!data->has_vid5)
1608 data->groups[idx++] = &lm85_group_in4;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001609
1610 /* The EMC6D100 has 3 additional voltage inputs */
Axel Lin746f6882014-07-23 12:29:11 +08001611 if (data->type == emc6d100)
1612 data->groups[idx++] = &lm85_group_in567;
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001613
Axel Lin746f6882014-07-23 12:29:11 +08001614 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1615 data, data->groups);
1616 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Axel Lin6fd5dd52014-07-23 12:27:39 +08001619static const struct i2c_device_id lm85_id[] = {
1620 { "adm1027", adm1027 },
1621 { "adt7463", adt7463 },
1622 { "adt7468", adt7468 },
1623 { "lm85", lm85 },
1624 { "lm85b", lm85 },
1625 { "lm85c", lm85 },
1626 { "emc6d100", emc6d100 },
1627 { "emc6d101", emc6d100 },
1628 { "emc6d102", emc6d102 },
1629 { "emc6d103", emc6d103 },
1630 { "emc6d103s", emc6d103s },
1631 { }
1632};
1633MODULE_DEVICE_TABLE(i2c, lm85_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Axel Lin6fd5dd52014-07-23 12:27:39 +08001635static struct i2c_driver lm85_driver = {
1636 .class = I2C_CLASS_HWMON,
1637 .driver = {
1638 .name = "lm85",
1639 },
1640 .probe = lm85_probe,
Axel Lin6fd5dd52014-07-23 12:27:39 +08001641 .id_table = lm85_id,
1642 .detect = lm85_detect,
1643 .address_list = normal_i2c,
1644};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Axel Linf0967ee2012-01-20 15:38:18 +08001646module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001649MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1650 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001651 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652MODULE_DESCRIPTION("LM85-B, LM85-C driver");