blob: 9fdf1c021007c03de33689a05478933124b22383 [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 {
Tony Jones1beeffe2007-08-20 13:46:20 -0700305 struct device *hwmon_dev;
Jean Delvare8a0795d2008-10-17 17:51:14 +0200306 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 enum chips type;
308
Guenter Roeckde248802011-02-25 08:19:42 -0800309 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
310
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100311 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int valid; /* !=0 if following fields are valid */
313 unsigned long last_reading; /* In jiffies */
314 unsigned long last_config; /* In jiffies */
315
316 u8 in[8]; /* Register value */
317 u8 in_max[8]; /* Register value */
318 u8 in_min[8]; /* Register value */
319 s8 temp[3]; /* Register value */
320 s8 temp_min[3]; /* Register value */
321 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 u16 fan[4]; /* Register value */
323 u16 fan_min[4]; /* Register value */
324 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200325 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u8 temp_ext[3]; /* Decoded values */
327 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u8 vid; /* Register value */
329 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800331 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct lm85_autofan autofan[3];
333 struct lm85_zone zone[3];
334};
335
Axel Lin6fd5dd52014-07-23 12:27:39 +0800336static int lm85_read_value(struct i2c_client *client, u8 reg)
337{
338 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Axel Lin6fd5dd52014-07-23 12:27:39 +0800340 /* What size location is it? */
341 switch (reg) {
342 case LM85_REG_FAN(0): /* Read WORD data */
343 case LM85_REG_FAN(1):
344 case LM85_REG_FAN(2):
345 case LM85_REG_FAN(3):
346 case LM85_REG_FAN_MIN(0):
347 case LM85_REG_FAN_MIN(1):
348 case LM85_REG_FAN_MIN(2):
349 case LM85_REG_FAN_MIN(3):
350 case LM85_REG_ALARM1: /* Read both bytes at once */
351 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
352 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
353 break;
354 default: /* Read BYTE data */
355 res = i2c_smbus_read_byte_data(client, reg);
356 break;
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Axel Lin6fd5dd52014-07-23 12:27:39 +0800359 return res;
360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Axel Lin6fd5dd52014-07-23 12:27:39 +0800362static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
363{
364 switch (reg) {
365 case LM85_REG_FAN(0): /* Write WORD data */
366 case LM85_REG_FAN(1):
367 case LM85_REG_FAN(2):
368 case LM85_REG_FAN(3):
369 case LM85_REG_FAN_MIN(0):
370 case LM85_REG_FAN_MIN(1):
371 case LM85_REG_FAN_MIN(2):
372 case LM85_REG_FAN_MIN(3):
373 /* NOTE: ALARM is read only, so not included here */
374 i2c_smbus_write_byte_data(client, reg, value & 0xff);
375 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
376 break;
377 default: /* Write BYTE data */
378 i2c_smbus_write_byte_data(client, reg, value);
379 break;
380 }
381}
Jean Delvare67712d02008-10-17 17:51:14 +0200382
Axel Lin6fd5dd52014-07-23 12:27:39 +0800383static struct lm85_data *lm85_update_device(struct device *dev)
384{
385 struct i2c_client *client = to_i2c_client(dev);
386 struct lm85_data *data = i2c_get_clientdata(client);
387 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Axel Lin6fd5dd52014-07-23 12:27:39 +0800389 mutex_lock(&data->update_lock);
390
391 if (!data->valid ||
392 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
393 /* Things that change quickly */
394 dev_dbg(&client->dev, "Reading sensor values\n");
395
396 /*
397 * Have to read extended bits first to "freeze" the
398 * more significant bits that are read later.
399 * There are 2 additional resolution bits per channel and we
400 * have room for 4, so we shift them to the left.
401 */
402 if (data->type == adm1027 || data->type == adt7463 ||
403 data->type == adt7468) {
404 int ext1 = lm85_read_value(client,
405 ADM1027_REG_EXTEND_ADC1);
406 int ext2 = lm85_read_value(client,
407 ADM1027_REG_EXTEND_ADC2);
408 int val = (ext1 << 8) + ext2;
409
410 for (i = 0; i <= 4; i++)
411 data->in_ext[i] =
412 ((val >> (i * 2)) & 0x03) << 2;
413
414 for (i = 0; i <= 2; i++)
415 data->temp_ext[i] =
416 (val >> ((i + 4) * 2)) & 0x0c;
417 }
418
419 data->vid = lm85_read_value(client, LM85_REG_VID);
420
421 for (i = 0; i <= 3; ++i) {
422 data->in[i] =
423 lm85_read_value(client, LM85_REG_IN(i));
424 data->fan[i] =
425 lm85_read_value(client, LM85_REG_FAN(i));
426 }
427
428 if (!data->has_vid5)
429 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
430
431 if (data->type == adt7468)
432 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
433
434 for (i = 0; i <= 2; ++i) {
435 data->temp[i] =
436 lm85_read_value(client, LM85_REG_TEMP(i));
437 data->pwm[i] =
438 lm85_read_value(client, LM85_REG_PWM(i));
439
440 if (IS_ADT7468_OFF64(data))
441 data->temp[i] -= 64;
442 }
443
444 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
445
446 if (data->type == emc6d100) {
447 /* Three more voltage sensors */
448 for (i = 5; i <= 7; ++i) {
449 data->in[i] = lm85_read_value(client,
450 EMC6D100_REG_IN(i));
451 }
452 /* More alarm bits */
453 data->alarms |= lm85_read_value(client,
454 EMC6D100_REG_ALARM3) << 16;
455 } else if (data->type == emc6d102 || data->type == emc6d103 ||
456 data->type == emc6d103s) {
457 /*
458 * Have to read LSB bits after the MSB ones because
459 * the reading of the MSB bits has frozen the
460 * LSBs (backward from the ADM1027).
461 */
462 int ext1 = lm85_read_value(client,
463 EMC6D102_REG_EXTEND_ADC1);
464 int ext2 = lm85_read_value(client,
465 EMC6D102_REG_EXTEND_ADC2);
466 int ext3 = lm85_read_value(client,
467 EMC6D102_REG_EXTEND_ADC3);
468 int ext4 = lm85_read_value(client,
469 EMC6D102_REG_EXTEND_ADC4);
470 data->in_ext[0] = ext3 & 0x0f;
471 data->in_ext[1] = ext4 & 0x0f;
472 data->in_ext[2] = ext4 >> 4;
473 data->in_ext[3] = ext3 >> 4;
474 data->in_ext[4] = ext2 >> 4;
475
476 data->temp_ext[0] = ext1 & 0x0f;
477 data->temp_ext[1] = ext2 & 0x0f;
478 data->temp_ext[2] = ext1 >> 4;
479 }
480
481 data->last_reading = jiffies;
482 } /* last_reading */
483
484 if (!data->valid ||
485 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
486 /* Things that don't change often */
487 dev_dbg(&client->dev, "Reading config values\n");
488
489 for (i = 0; i <= 3; ++i) {
490 data->in_min[i] =
491 lm85_read_value(client, LM85_REG_IN_MIN(i));
492 data->in_max[i] =
493 lm85_read_value(client, LM85_REG_IN_MAX(i));
494 data->fan_min[i] =
495 lm85_read_value(client, LM85_REG_FAN_MIN(i));
496 }
497
498 if (!data->has_vid5) {
499 data->in_min[4] = lm85_read_value(client,
500 LM85_REG_IN_MIN(4));
501 data->in_max[4] = lm85_read_value(client,
502 LM85_REG_IN_MAX(4));
503 }
504
505 if (data->type == emc6d100) {
506 for (i = 5; i <= 7; ++i) {
507 data->in_min[i] = lm85_read_value(client,
508 EMC6D100_REG_IN_MIN(i));
509 data->in_max[i] = lm85_read_value(client,
510 EMC6D100_REG_IN_MAX(i));
511 }
512 }
513
514 for (i = 0; i <= 2; ++i) {
515 int val;
516
517 data->temp_min[i] =
518 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
519 data->temp_max[i] =
520 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
521
522 data->autofan[i].config =
523 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
524 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
525 data->pwm_freq[i] = val & 0x07;
526 data->zone[i].range = val >> 4;
527 data->autofan[i].min_pwm =
528 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
529 data->zone[i].limit =
530 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
531 data->zone[i].critical =
532 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
533
534 if (IS_ADT7468_OFF64(data)) {
535 data->temp_min[i] -= 64;
536 data->temp_max[i] -= 64;
537 data->zone[i].limit -= 64;
538 data->zone[i].critical -= 64;
539 }
540 }
541
542 if (data->type != emc6d103s) {
543 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
544 data->autofan[0].min_off = (i & 0x20) != 0;
545 data->autofan[1].min_off = (i & 0x40) != 0;
546 data->autofan[2].min_off = (i & 0x80) != 0;
547
548 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
549 data->zone[0].hyst = i >> 4;
550 data->zone[1].hyst = i & 0x0f;
551
552 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
553 data->zone[2].hyst = i >> 4;
554 }
555
556 data->last_config = jiffies;
557 } /* last_config */
558
559 data->valid = 1;
560
561 mutex_unlock(&data->update_lock);
562
563 return data;
564}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200567static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
568 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jean Delvareb353a482007-07-05 20:36:12 +0200570 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200572 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Jean Delvareb353a482007-07-05 20:36:12 +0200574
575static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
576 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Jean Delvareb353a482007-07-05 20:36:12 +0200578 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200580 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Jean Delvareb353a482007-07-05 20:36:12 +0200582
583static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
584 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jean Delvareb353a482007-07-05 20:36:12 +0200586 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct i2c_client *client = to_i2c_client(dev);
588 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800589 unsigned long val;
590 int err;
591
592 err = kstrtoul(buf, 10, &val);
593 if (err)
594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100596 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 data->fan_min[nr] = FAN_TO_REG(val);
598 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100599 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return count;
601}
602
603#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200604static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
605 show_fan, NULL, offset - 1); \
606static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
607 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609show_fan_offset(1);
610show_fan_offset(2);
611show_fan_offset(3);
612show_fan_offset(4);
613
614/* vid, vrm, alarms */
615
Jean Delvare1f448092008-04-29 14:03:37 +0200616static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
617 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100620 int vid;
621
Guenter Roeckde248802011-02-25 08:19:42 -0800622 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100623 /* 6-pin VID (VRM 10) */
624 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
625 } else {
626 /* 5-pin VID (VRM 9) */
627 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
628 }
629
630 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
634
Jean Delvare1f448092008-04-29 14:03:37 +0200635static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
636 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jean Delvare90d66192007-10-08 18:24:35 +0200638 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return sprintf(buf, "%ld\n", (long) data->vrm);
640}
641
Jean Delvare1f448092008-04-29 14:03:37 +0200642static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100645 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b22012-01-14 20:47:36 -0800646 unsigned long val;
647 int err;
648
649 err = kstrtoul(buf, 10, &val);
650 if (err)
651 return err;
652
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700653 if (val > 255)
654 return -EINVAL;
655
Guenter Roeck09770b22012-01-14 20:47:36 -0800656 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return count;
658}
659
660static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
661
Jean Delvare1f448092008-04-29 14:03:37 +0200662static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
663 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200666 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
670
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200671static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
672 char *buf)
673{
674 int nr = to_sensor_dev_attr(attr)->index;
675 struct lm85_data *data = lm85_update_device(dev);
676 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
677}
678
679static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
680static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
681static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
682static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
683static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
684static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
685static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
686static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
687static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
688static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
689static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
690static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
691static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
692static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
693static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
694static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
695static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/* pwm */
698
Jean Delvareb353a482007-07-05 20:36:12 +0200699static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
700 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jean Delvareb353a482007-07-05 20:36:12 +0200702 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200704 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
Jean Delvareb353a482007-07-05 20:36:12 +0200706
707static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
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 i2c_client *client = to_i2c_client(dev);
712 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800713 unsigned long val;
714 int err;
715
716 err = kstrtoul(buf, 10, &val);
717 if (err)
718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100720 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 data->pwm[nr] = PWM_TO_REG(val);
722 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100723 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return count;
725}
Jean Delvareb353a482007-07-05 20:36:12 +0200726
727static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
728 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Jean Delvareb353a482007-07-05 20:36:12 +0200730 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100732 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100735 switch (pwm_zone) {
736 case -1: /* PWM is always at 100% */
737 enable = 0;
738 break;
739 case 0: /* PWM is always at 0% */
740 case -2: /* PWM responds to manual control */
741 enable = 1;
742 break;
743 default: /* PWM in automatic mode */
744 enable = 2;
745 }
746 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Jean Delvare455f7912007-12-03 23:28:42 +0100749static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
750 *attr, const char *buf, size_t count)
751{
752 int nr = to_sensor_dev_attr(attr)->index;
753 struct i2c_client *client = to_i2c_client(dev);
754 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare455f7912007-12-03 23:28:42 +0100755 u8 config;
Guenter Roeck09770b22012-01-14 20:47:36 -0800756 unsigned long val;
757 int err;
758
759 err = kstrtoul(buf, 10, &val);
760 if (err)
761 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100762
763 switch (val) {
764 case 0:
765 config = 3;
766 break;
767 case 1:
768 config = 7;
769 break;
770 case 2:
Guenter Roeck09770b22012-01-14 20:47:36 -0800771 /*
772 * Here we have to choose arbitrarily one of the 5 possible
773 * configurations; I go for the safest
774 */
Jean Delvare455f7912007-12-03 23:28:42 +0100775 config = 6;
776 break;
777 default:
778 return -EINVAL;
779 }
780
781 mutex_lock(&data->update_lock);
782 data->autofan[nr].config = lm85_read_value(client,
783 LM85_REG_AFAN_CONFIG(nr));
784 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
785 | (config << 5);
786 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
787 data->autofan[nr].config);
788 mutex_unlock(&data->update_lock);
789 return count;
790}
791
Jean Delvare34e7dc62008-10-17 17:51:13 +0200792static ssize_t show_pwm_freq(struct device *dev,
793 struct device_attribute *attr, char *buf)
794{
795 int nr = to_sensor_dev_attr(attr)->index;
796 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200797 int freq;
798
799 if (IS_ADT7468_HFPWM(data))
800 freq = 22500;
801 else
802 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
803
804 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200805}
806
807static ssize_t set_pwm_freq(struct device *dev,
808 struct device_attribute *attr, const char *buf, size_t count)
809{
810 int nr = to_sensor_dev_attr(attr)->index;
811 struct i2c_client *client = to_i2c_client(dev);
812 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800813 unsigned long val;
814 int err;
815
816 err = kstrtoul(buf, 10, &val);
817 if (err)
818 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200819
820 mutex_lock(&data->update_lock);
Guenter Roeck09770b22012-01-14 20:47:36 -0800821 /*
822 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200823 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b22012-01-14 20:47:36 -0800824 * This might confuse the user, but there's not much we can do.
825 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200826 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
827 data->cfg5 &= ~ADT7468_HFPWM;
828 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
829 } else { /* Low freq. mode */
830 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
831 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
832 (data->zone[nr].range << 4)
833 | data->pwm_freq[nr]);
834 if (data->type == adt7468) {
835 data->cfg5 |= ADT7468_HFPWM;
836 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
837 }
838 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200839 mutex_unlock(&data->update_lock);
840 return count;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200844static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
845 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100846static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200847 show_pwm_enable, set_pwm_enable, offset - 1); \
848static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
849 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851show_pwm_reg(1);
852show_pwm_reg(2);
853show_pwm_reg(3);
854
855/* Voltages */
856
Jean Delvareb353a482007-07-05 20:36:12 +0200857static ssize_t show_in(struct device *dev, struct device_attribute *attr,
858 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Jean Delvareb353a482007-07-05 20:36:12 +0200860 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200862 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
863 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Jean Delvareb353a482007-07-05 20:36:12 +0200865
Jean Delvare1f448092008-04-29 14:03:37 +0200866static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200867 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Jean Delvareb353a482007-07-05 20:36:12 +0200869 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200871 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
Jean Delvareb353a482007-07-05 20:36:12 +0200873
874static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
875 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Jean Delvareb353a482007-07-05 20:36:12 +0200877 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct i2c_client *client = to_i2c_client(dev);
879 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800880 long val;
881 int err;
882
883 err = kstrtol(buf, 10, &val);
884 if (err)
885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100887 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 data->in_min[nr] = INS_TO_REG(nr, val);
889 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100890 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return count;
892}
Jean Delvareb353a482007-07-05 20:36:12 +0200893
894static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
895 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Jean Delvareb353a482007-07-05 20:36:12 +0200897 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200899 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
Jean Delvareb353a482007-07-05 20:36:12 +0200901
902static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
903 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Jean Delvareb353a482007-07-05 20:36:12 +0200905 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct i2c_client *client = to_i2c_client(dev);
907 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800908 long val;
909 int err;
910
911 err = kstrtol(buf, 10, &val);
912 if (err)
913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100915 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 data->in_max[nr] = INS_TO_REG(nr, val);
917 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100918 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return count;
920}
Jean Delvareb353a482007-07-05 20:36:12 +0200921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200923static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
924 show_in, NULL, offset); \
925static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
926 show_in_min, set_in_min, offset); \
927static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
928 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930show_in_reg(0);
931show_in_reg(1);
932show_in_reg(2);
933show_in_reg(3);
934show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200935show_in_reg(5);
936show_in_reg(6);
937show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939/* Temps */
940
Jean Delvareb353a482007-07-05 20:36:12 +0200941static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
942 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Jean Delvareb353a482007-07-05 20:36:12 +0200944 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200946 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
947 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
Jean Delvareb353a482007-07-05 20:36:12 +0200949
950static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
951 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jean Delvareb353a482007-07-05 20:36:12 +0200953 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200955 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
Jean Delvareb353a482007-07-05 20:36:12 +0200957
958static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
959 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);
Guenter Roeck09770b22012-01-14 20:47:36 -0800964 long val;
965 int err;
966
967 err = kstrtol(buf, 10, &val);
968 if (err)
969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800971 if (IS_ADT7468_OFF64(data))
972 val += 64;
973
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100974 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 data->temp_min[nr] = TEMP_TO_REG(val);
976 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100977 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return count;
979}
Jean Delvareb353a482007-07-05 20:36:12 +0200980
981static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
982 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jean Delvareb353a482007-07-05 20:36:12 +0200984 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200986 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Jean Delvareb353a482007-07-05 20:36:12 +0200988
989static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
990 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Jean Delvareb353a482007-07-05 20:36:12 +0200992 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct i2c_client *client = to_i2c_client(dev);
994 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -0800995 long val;
996 int err;
997
998 err = kstrtol(buf, 10, &val);
999 if (err)
1000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Darrick J. Wong79b92f22008-11-12 13:26:59 -08001002 if (IS_ADT7468_OFF64(data))
1003 val += 64;
1004
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001005 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 data->temp_max[nr] = TEMP_TO_REG(val);
1007 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001008 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return count;
1010}
Jean Delvareb353a482007-07-05 20:36:12 +02001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001013static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1014 show_temp, NULL, offset - 1); \
1015static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1016 show_temp_min, set_temp_min, offset - 1); \
1017static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1018 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020show_temp_reg(1);
1021show_temp_reg(2);
1022show_temp_reg(3);
1023
1024
1025/* Automatic PWM control */
1026
Jean Delvareb353a482007-07-05 20:36:12 +02001027static ssize_t show_pwm_auto_channels(struct device *dev,
1028 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Jean Delvareb353a482007-07-05 20:36:12 +02001030 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001032 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
Jean Delvareb353a482007-07-05 20:36:12 +02001034
1035static ssize_t set_pwm_auto_channels(struct device *dev,
1036 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Jean Delvareb353a482007-07-05 20:36:12 +02001038 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct i2c_client *client = to_i2c_client(dev);
1040 struct lm85_data *data = i2c_get_clientdata(client);
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 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +02001050 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1052 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001053 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return count;
1055}
Jean Delvareb353a482007-07-05 20:36:12 +02001056
1057static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1058 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jean Delvareb353a482007-07-05 20:36:12 +02001060 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001062 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
Jean Delvareb353a482007-07-05 20:36:12 +02001064
1065static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1066 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Jean Delvareb353a482007-07-05 20:36:12 +02001068 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct i2c_client *client = to_i2c_client(dev);
1070 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -08001071 unsigned long val;
1072 int err;
1073
1074 err = kstrtoul(buf, 10, &val);
1075 if (err)
1076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001078 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1080 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1081 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001082 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return count;
1084}
Jean Delvareb353a482007-07-05 20:36:12 +02001085
1086static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1087 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Jean Delvareb353a482007-07-05 20:36:12 +02001089 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001091 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
Jean Delvareb353a482007-07-05 20:36:12 +02001093
1094static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1095 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Jean Delvareb353a482007-07-05 20:36:12 +02001097 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct i2c_client *client = to_i2c_client(dev);
1099 struct lm85_data *data = i2c_get_clientdata(client);
Jean Delvare7133e562008-04-12 19:56:35 +02001100 u8 tmp;
Guenter Roeck09770b22012-01-14 20:47:36 -08001101 long val;
1102 int err;
1103
1104 err = kstrtol(buf, 10, &val);
1105 if (err)
1106 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001108 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +02001110 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1111 tmp &= ~(0x20 << nr);
1112 if (data->autofan[nr].min_off)
1113 tmp |= 0x20 << nr;
1114 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001115 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return count;
1117}
Jean Delvareb353a482007-07-05 20:36:12 +02001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001120static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1121 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1122 set_pwm_auto_channels, offset - 1); \
1123static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
1124 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
1125 set_pwm_auto_pwm_min, offset - 1); \
1126static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1127 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +02001128 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +02001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130pwm_auto(1);
1131pwm_auto(2);
1132pwm_auto(3);
1133
1134/* Temperature settings for automatic PWM control */
1135
Jean Delvareb353a482007-07-05 20:36:12 +02001136static ssize_t show_temp_auto_temp_off(struct device *dev,
1137 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jean Delvareb353a482007-07-05 20:36:12 +02001139 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001141 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 HYST_FROM_REG(data->zone[nr].hyst));
1143}
Jean Delvareb353a482007-07-05 20:36:12 +02001144
1145static ssize_t set_temp_auto_temp_off(struct device *dev,
1146 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Jean Delvareb353a482007-07-05 20:36:12 +02001148 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct i2c_client *client = to_i2c_client(dev);
1150 struct lm85_data *data = i2c_get_clientdata(client);
1151 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001152 long val;
1153 int err;
1154
1155 err = kstrtol(buf, 10, &val);
1156 if (err)
1157 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001159 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +02001162 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1164 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +02001165 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 } else {
1167 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +02001168 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001170 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return count;
1172}
Jean Delvareb353a482007-07-05 20:36:12 +02001173
1174static ssize_t show_temp_auto_temp_min(struct device *dev,
1175 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Jean Delvareb353a482007-07-05 20:36:12 +02001177 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001179 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
Jean Delvareb353a482007-07-05 20:36:12 +02001181
1182static ssize_t set_temp_auto_temp_min(struct device *dev,
1183 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Jean Delvareb353a482007-07-05 20:36:12 +02001185 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct i2c_client *client = to_i2c_client(dev);
1187 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -08001188 long val;
1189 int err;
1190
1191 err = kstrtol(buf, 10, &val);
1192 if (err)
1193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001195 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 data->zone[nr].limit = TEMP_TO_REG(val);
1197 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1198 data->zone[nr].limit);
1199
1200/* Update temp_auto_max and temp_auto_range */
1201 data->zone[nr].range = RANGE_TO_REG(
1202 TEMP_FROM_REG(data->zone[nr].max_desired) -
1203 TEMP_FROM_REG(data->zone[nr].limit));
1204 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1205 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001206 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001208 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return count;
1210}
Jean Delvareb353a482007-07-05 20:36:12 +02001211
1212static ssize_t show_temp_auto_temp_max(struct device *dev,
1213 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Jean Delvareb353a482007-07-05 20:36:12 +02001215 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001217 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 RANGE_FROM_REG(data->zone[nr].range));
1219}
Jean Delvareb353a482007-07-05 20:36:12 +02001220
1221static ssize_t set_temp_auto_temp_max(struct device *dev,
1222 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jean Delvareb353a482007-07-05 20:36:12 +02001224 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct i2c_client *client = to_i2c_client(dev);
1226 struct lm85_data *data = i2c_get_clientdata(client);
1227 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001228 long val;
1229 int err;
1230
1231 err = kstrtol(buf, 10, &val);
1232 if (err)
1233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001235 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 min = TEMP_FROM_REG(data->zone[nr].limit);
1237 data->zone[nr].max_desired = TEMP_TO_REG(val);
1238 data->zone[nr].range = RANGE_TO_REG(
1239 val - min);
1240 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1241 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001242 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001243 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return count;
1245}
Jean Delvareb353a482007-07-05 20:36:12 +02001246
1247static ssize_t show_temp_auto_temp_crit(struct device *dev,
1248 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Jean Delvareb353a482007-07-05 20:36:12 +02001250 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001252 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
Jean Delvareb353a482007-07-05 20:36:12 +02001254
1255static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +02001256 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Jean Delvareb353a482007-07-05 20:36:12 +02001258 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct i2c_client *client = to_i2c_client(dev);
1260 struct lm85_data *data = i2c_get_clientdata(client);
Guenter Roeck09770b22012-01-14 20:47:36 -08001261 long val;
1262 int err;
1263
1264 err = kstrtol(buf, 10, &val);
1265 if (err)
1266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001268 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 data->zone[nr].critical = TEMP_TO_REG(val);
1270 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1271 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001272 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return count;
1274}
Jean Delvareb353a482007-07-05 20:36:12 +02001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001277static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1278 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1279 set_temp_auto_temp_off, offset - 1); \
1280static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1281 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1282 set_temp_auto_temp_min, offset - 1); \
1283static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1284 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1285 set_temp_auto_temp_max, offset - 1); \
1286static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1287 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1288 set_temp_auto_temp_crit, offset - 1);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290temp_auto(1);
1291temp_auto(2);
1292temp_auto(3);
1293
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001294static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001295 &sensor_dev_attr_fan1_input.dev_attr.attr,
1296 &sensor_dev_attr_fan2_input.dev_attr.attr,
1297 &sensor_dev_attr_fan3_input.dev_attr.attr,
1298 &sensor_dev_attr_fan4_input.dev_attr.attr,
1299 &sensor_dev_attr_fan1_min.dev_attr.attr,
1300 &sensor_dev_attr_fan2_min.dev_attr.attr,
1301 &sensor_dev_attr_fan3_min.dev_attr.attr,
1302 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001303 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1304 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1305 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1306 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001307
1308 &sensor_dev_attr_pwm1.dev_attr.attr,
1309 &sensor_dev_attr_pwm2.dev_attr.attr,
1310 &sensor_dev_attr_pwm3.dev_attr.attr,
1311 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1312 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1313 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001314 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1315 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1316 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001317
1318 &sensor_dev_attr_in0_input.dev_attr.attr,
1319 &sensor_dev_attr_in1_input.dev_attr.attr,
1320 &sensor_dev_attr_in2_input.dev_attr.attr,
1321 &sensor_dev_attr_in3_input.dev_attr.attr,
1322 &sensor_dev_attr_in0_min.dev_attr.attr,
1323 &sensor_dev_attr_in1_min.dev_attr.attr,
1324 &sensor_dev_attr_in2_min.dev_attr.attr,
1325 &sensor_dev_attr_in3_min.dev_attr.attr,
1326 &sensor_dev_attr_in0_max.dev_attr.attr,
1327 &sensor_dev_attr_in1_max.dev_attr.attr,
1328 &sensor_dev_attr_in2_max.dev_attr.attr,
1329 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001330 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1331 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1332 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1333 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001334
1335 &sensor_dev_attr_temp1_input.dev_attr.attr,
1336 &sensor_dev_attr_temp2_input.dev_attr.attr,
1337 &sensor_dev_attr_temp3_input.dev_attr.attr,
1338 &sensor_dev_attr_temp1_min.dev_attr.attr,
1339 &sensor_dev_attr_temp2_min.dev_attr.attr,
1340 &sensor_dev_attr_temp3_min.dev_attr.attr,
1341 &sensor_dev_attr_temp1_max.dev_attr.attr,
1342 &sensor_dev_attr_temp2_max.dev_attr.attr,
1343 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001344 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1345 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1346 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1347 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1348 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001349
1350 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1351 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1352 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1353 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1354 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1355 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001356
Jean Delvareb353a482007-07-05 20:36:12 +02001357 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1358 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1359 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1360 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1361 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1362 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1363 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1364 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1365 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1366
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001367 &dev_attr_vrm.attr,
1368 &dev_attr_cpu0_vid.attr,
1369 &dev_attr_alarms.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001370 NULL
1371};
1372
1373static const struct attribute_group lm85_group = {
1374 .attrs = lm85_attributes,
1375};
1376
Guenter Roeck06923f82011-02-19 08:27:47 -08001377static struct attribute *lm85_attributes_minctl[] = {
1378 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1379 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1380 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001381 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001382};
1383
1384static const struct attribute_group lm85_group_minctl = {
1385 .attrs = lm85_attributes_minctl,
1386};
1387
1388static struct attribute *lm85_attributes_temp_off[] = {
1389 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1390 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1391 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001392 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001393};
1394
1395static const struct attribute_group lm85_group_temp_off = {
1396 .attrs = lm85_attributes_temp_off,
1397};
1398
Jean Delvare6b9aad22007-07-05 20:37:21 +02001399static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001400 &sensor_dev_attr_in4_input.dev_attr.attr,
1401 &sensor_dev_attr_in4_min.dev_attr.attr,
1402 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001403 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001404 NULL
1405};
1406
Jean Delvare6b9aad22007-07-05 20:37:21 +02001407static const struct attribute_group lm85_group_in4 = {
1408 .attrs = lm85_attributes_in4,
1409};
1410
1411static struct attribute *lm85_attributes_in567[] = {
1412 &sensor_dev_attr_in5_input.dev_attr.attr,
1413 &sensor_dev_attr_in6_input.dev_attr.attr,
1414 &sensor_dev_attr_in7_input.dev_attr.attr,
1415 &sensor_dev_attr_in5_min.dev_attr.attr,
1416 &sensor_dev_attr_in6_min.dev_attr.attr,
1417 &sensor_dev_attr_in7_min.dev_attr.attr,
1418 &sensor_dev_attr_in5_max.dev_attr.attr,
1419 &sensor_dev_attr_in6_max.dev_attr.attr,
1420 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001421 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1422 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1423 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001424 NULL
1425};
1426
1427static const struct attribute_group lm85_group_in567 = {
1428 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001429};
1430
Jean Delvare5f44759472008-06-25 09:10:30 -04001431static void lm85_init_client(struct i2c_client *client)
1432{
1433 int value;
1434
1435 /* Start monitoring if needed */
1436 value = lm85_read_value(client, LM85_REG_CONFIG);
1437 if (!(value & 0x01)) {
1438 dev_info(&client->dev, "Starting monitoring\n");
1439 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1440 }
1441
1442 /* Warn about unusual configuration bits */
1443 if (value & 0x02)
1444 dev_warn(&client->dev, "Device configuration is locked\n");
1445 if (!(value & 0x04))
1446 dev_warn(&client->dev, "Device is not ready\n");
1447}
1448
Jean Delvare5cfaf332009-09-15 17:18:14 +02001449static int lm85_is_fake(struct i2c_client *client)
1450{
1451 /*
1452 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1453 * emulate the former except that it has no hardware monitoring function
1454 * so the readings are always 0.
1455 */
1456 int i;
1457 u8 in_temp, fan;
1458
1459 for (i = 0; i < 8; i++) {
1460 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1461 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1462 if (in_temp != 0x00 || fan != 0xff)
1463 return 0;
1464 }
1465
1466 return 1;
1467}
1468
Jean Delvare67712d02008-10-17 17:51:14 +02001469/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001470static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Jean Delvare67712d02008-10-17 17:51:14 +02001472 struct i2c_adapter *adapter = client->adapter;
1473 int address = client->addr;
Jean Delvare590e8532014-06-11 18:35:56 +02001474 const char *type_name = NULL;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001475 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Jean Delvaree89e22b2008-06-25 08:47:35 -04001477 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001479 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jean Delvared42a2eb2009-12-09 20:35:53 +01001482 /* Determine the chip type */
1483 company = lm85_read_value(client, LM85_REG_COMPANY);
1484 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Guenter Roeckb55f3752013-01-10 10:01:24 -08001486 dev_dbg(&adapter->dev,
1487 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001488 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Jean Delvared42a2eb2009-12-09 20:35:53 +01001490 if (company == LM85_COMPANY_NATIONAL) {
1491 switch (verstep) {
1492 case LM85_VERSTEP_LM85C:
1493 type_name = "lm85c";
1494 break;
1495 case LM85_VERSTEP_LM85B:
1496 type_name = "lm85b";
1497 break;
1498 case LM85_VERSTEP_LM96000_1:
1499 case LM85_VERSTEP_LM96000_2:
1500 /* Check for Winbond WPCD377I */
1501 if (lm85_is_fake(client)) {
1502 dev_dbg(&adapter->dev,
1503 "Found Winbond WPCD377I, ignoring\n");
1504 return -ENODEV;
1505 }
Jean Delvare590e8532014-06-11 18:35:56 +02001506 type_name = "lm85";
Jean Delvared42a2eb2009-12-09 20:35:53 +01001507 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001508 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001509 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1510 switch (verstep) {
1511 case LM85_VERSTEP_ADM1027:
1512 type_name = "adm1027";
1513 break;
1514 case LM85_VERSTEP_ADT7463:
1515 case LM85_VERSTEP_ADT7463C:
1516 type_name = "adt7463";
1517 break;
1518 case LM85_VERSTEP_ADT7468_1:
1519 case LM85_VERSTEP_ADT7468_2:
1520 type_name = "adt7468";
1521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001523 } else if (company == LM85_COMPANY_SMSC) {
1524 switch (verstep) {
1525 case LM85_VERSTEP_EMC6D100_A0:
1526 case LM85_VERSTEP_EMC6D100_A1:
1527 /* Note: we can't tell a '100 from a '101 */
1528 type_name = "emc6d100";
1529 break;
1530 case LM85_VERSTEP_EMC6D102:
1531 type_name = "emc6d102";
1532 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001533 case LM85_VERSTEP_EMC6D103_A0:
1534 case LM85_VERSTEP_EMC6D103_A1:
1535 type_name = "emc6d103";
1536 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001537 case LM85_VERSTEP_EMC6D103S:
1538 type_name = "emc6d103s";
1539 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
Jean Delvare590e8532014-06-11 18:35:56 +02001543 if (!type_name)
1544 return -ENODEV;
1545
Jean Delvare67712d02008-10-17 17:51:14 +02001546 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Jean Delvare67712d02008-10-17 17:51:14 +02001548 return 0;
1549}
1550
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001551static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1552{
1553 sysfs_remove_group(&client->dev.kobj, &lm85_group);
Guenter Roeck06923f82011-02-19 08:27:47 -08001554 if (data->type != emc6d103s) {
1555 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1556 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1557 }
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001558 if (!data->has_vid5)
1559 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1560 if (data->type == emc6d100)
1561 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1562}
1563
Jean Delvare67712d02008-10-17 17:51:14 +02001564static int lm85_probe(struct i2c_client *client,
1565 const struct i2c_device_id *id)
1566{
1567 struct lm85_data *data;
1568 int err;
1569
Guenter Roeck747e5d62012-06-02 09:58:10 -07001570 data = devm_kzalloc(&client->dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001571 if (!data)
1572 return -ENOMEM;
1573
1574 i2c_set_clientdata(client, data);
1575 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001576 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jean Delvare67712d02008-10-17 17:51:14 +02001578 /* Fill in the chip specific driver values */
1579 switch (data->type) {
1580 case adm1027:
1581 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001582 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001583 case emc6d100:
1584 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001585 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001586 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001587 data->freq_map = adm1027_freq_map;
1588 break;
1589 default:
1590 data->freq_map = lm85_freq_map;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001594 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001597 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /* Register sysfs hooks */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001600 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1601 if (err)
Guenter Roeck747e5d62012-06-02 09:58:10 -07001602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Guenter Roeck06923f82011-02-19 08:27:47 -08001604 /* minctl and temp_off exist on all chips except emc6d103s */
1605 if (data->type != emc6d103s) {
1606 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1607 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001608 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001609 err = sysfs_create_group(&client->dev.kobj,
1610 &lm85_group_temp_off);
1611 if (err)
Jean Delvarebc4d45f2011-04-29 16:33:36 +02001612 goto err_remove_files;
Guenter Roeck06923f82011-02-19 08:27:47 -08001613 }
1614
Guenter Roeck09770b22012-01-14 20:47:36 -08001615 /*
1616 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1617 * as a sixth digital VID input rather than an analog input.
1618 */
Guenter Roeckde248802011-02-25 08:19:42 -08001619 if (data->type == adt7463 || data->type == adt7468) {
1620 u8 vid = lm85_read_value(client, LM85_REG_VID);
1621 if (vid & 0x80)
1622 data->has_vid5 = true;
1623 }
1624
Guenter Roeck09770b22012-01-14 20:47:36 -08001625 if (!data->has_vid5) {
1626 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in4);
1627 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001628 goto err_remove_files;
Guenter Roeck09770b22012-01-14 20:47:36 -08001629 }
Jean Delvare6b9aad22007-07-05 20:37:21 +02001630
1631 /* The EMC6D100 has 3 additional voltage inputs */
Guenter Roeck09770b22012-01-14 20:47:36 -08001632 if (data->type == emc6d100) {
1633 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in567);
1634 if (err)
Jean Delvaref9080372008-10-17 17:51:14 +02001635 goto err_remove_files;
Guenter Roeck09770b22012-01-14 20:47:36 -08001636 }
Mark M. Hoffman0501a3812006-09-24 21:14:35 +02001637
Jean Delvaree89e22b2008-06-25 08:47:35 -04001638 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001639 if (IS_ERR(data->hwmon_dev)) {
1640 err = PTR_ERR(data->hwmon_dev);
Jean Delvaref9080372008-10-17 17:51:14 +02001641 goto err_remove_files;
Jean Delvare9c516ef2005-11-26 20:07:54 +01001642 }
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645
1646 /* Error out and cleanup code */
Jean Delvaref9080372008-10-17 17:51:14 +02001647 err_remove_files:
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001648 lm85_remove_files(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return err;
1650}
1651
Jean Delvare67712d02008-10-17 17:51:14 +02001652static int lm85_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001654 struct lm85_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001655 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001656 lm85_remove_files(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return 0;
1658}
1659
Axel Lin6fd5dd52014-07-23 12:27:39 +08001660static const struct i2c_device_id lm85_id[] = {
1661 { "adm1027", adm1027 },
1662 { "adt7463", adt7463 },
1663 { "adt7468", adt7468 },
1664 { "lm85", lm85 },
1665 { "lm85b", lm85 },
1666 { "lm85c", lm85 },
1667 { "emc6d100", emc6d100 },
1668 { "emc6d101", emc6d100 },
1669 { "emc6d102", emc6d102 },
1670 { "emc6d103", emc6d103 },
1671 { "emc6d103s", emc6d103s },
1672 { }
1673};
1674MODULE_DEVICE_TABLE(i2c, lm85_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Axel Lin6fd5dd52014-07-23 12:27:39 +08001676static struct i2c_driver lm85_driver = {
1677 .class = I2C_CLASS_HWMON,
1678 .driver = {
1679 .name = "lm85",
1680 },
1681 .probe = lm85_probe,
1682 .remove = lm85_remove,
1683 .id_table = lm85_id,
1684 .detect = lm85_detect,
1685 .address_list = normal_i2c,
1686};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Axel Linf0967ee2012-01-20 15:38:18 +08001688module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001691MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1692 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001693 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694MODULE_DESCRIPTION("LM85-B, LM85-C driver");