blob: 0a325878e8f52c9ed216332a81104cd418aaa26d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck09770b262012-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 Roeck09770b262012-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>
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -030028#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040033#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020034#include <linux/hwmon-vid.h>
Jean Delvareb353a482007-07-05 20:36:12 +020035#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040036#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010037#include <linux/mutex.h>
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -070038#include <linux/util_macros.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050041static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvaree5e9f442009-12-14 21:17:27 +010043enum chips {
Jean Delvare590e8532014-06-11 18:35:56 +020044 lm85,
Jean Delvaree5e9f442009-12-14 21:17:27 +010045 adm1027, adt7463, adt7468,
Guenter Roeck06923f82011-02-19 08:27:47 -080046 emc6d100, emc6d102, emc6d103, emc6d103s
Jean Delvaree5e9f442009-12-14 21:17:27 +010047};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* The LM85 registers */
50
Guenter Roeck09770b262012-01-14 20:47:36 -080051#define LM85_REG_IN(nr) (0x20 + (nr))
52#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
53#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Guenter Roeck09770b262012-01-14 20:47:36 -080055#define LM85_REG_TEMP(nr) (0x25 + (nr))
56#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
57#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Fan speeds are LSB, MSB (2 bytes) */
Guenter Roeck09770b262012-01-14 20:47:36 -080060#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
61#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Guenter Roeck09770b262012-01-14 20:47:36 -080063#define LM85_REG_PWM(nr) (0x30 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Guenter Roeck09770b262012-01-14 20:47:36 -080065#define LM85_REG_COMPANY 0x3e
66#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080067
Guenter Roeck09770b262012-01-14 20:47:36 -080068#define ADT7468_REG_CFG5 0x7c
69#define ADT7468_OFF64 (1 << 0)
70#define ADT7468_HFPWM (1 << 1)
71#define IS_ADT7468_OFF64(data) \
Darrick J. Wong79b92f22008-11-12 13:26:59 -080072 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
Guenter Roeck09770b262012-01-14 20:47:36 -080073#define IS_ADT7468_HFPWM(data) \
Jean Delvaref6c61cf2010-10-28 20:31:50 +020074 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
Darrick J. Wong79b92f22008-11-12 13:26:59 -080075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* These are the recognized values for the above regs */
Guenter Roeck09770b262012-01-14 20:47:36 -080077#define LM85_COMPANY_NATIONAL 0x01
78#define LM85_COMPANY_ANALOG_DEV 0x41
79#define LM85_COMPANY_SMSC 0x5c
Guenter Roeck09770b262012-01-14 20:47:36 -080080#define LM85_VERSTEP_LM85C 0x60
81#define LM85_VERSTEP_LM85B 0x62
82#define LM85_VERSTEP_LM96000_1 0x68
83#define LM85_VERSTEP_LM96000_2 0x69
84#define LM85_VERSTEP_ADM1027 0x60
85#define LM85_VERSTEP_ADT7463 0x62
86#define LM85_VERSTEP_ADT7463C 0x6A
87#define LM85_VERSTEP_ADT7468_1 0x71
88#define LM85_VERSTEP_ADT7468_2 0x72
89#define LM85_VERSTEP_EMC6D100_A0 0x60
90#define LM85_VERSTEP_EMC6D100_A1 0x61
91#define LM85_VERSTEP_EMC6D102 0x65
92#define LM85_VERSTEP_EMC6D103_A0 0x68
93#define LM85_VERSTEP_EMC6D103_A1 0x69
94#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Guenter Roeck09770b262012-01-14 20:47:36 -080096#define LM85_REG_CONFIG 0x40
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Guenter Roeck09770b262012-01-14 20:47:36 -080098#define LM85_REG_ALARM1 0x41
99#define LM85_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Guenter Roeck09770b262012-01-14 20:47:36 -0800101#define LM85_REG_VID 0x43
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* Automated FAN control */
Guenter Roeck09770b262012-01-14 20:47:36 -0800104#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
105#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
106#define LM85_REG_AFAN_SPIKE1 0x62
107#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
108#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
109#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
110#define LM85_REG_AFAN_HYST1 0x6d
111#define LM85_REG_AFAN_HYST2 0x6e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Guenter Roeck09770b262012-01-14 20:47:36 -0800113#define ADM1027_REG_EXTEND_ADC1 0x76
114#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define EMC6D100_REG_ALARM3 0x7d
117/* IN5, IN6 and IN7 */
Guenter Roeck09770b262012-01-14 20:47:36 -0800118#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
119#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
120#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
121#define EMC6D102_REG_EXTEND_ADC1 0x85
122#define EMC6D102_REG_EXTEND_ADC2 0x86
123#define EMC6D102_REG_EXTEND_ADC3 0x87
124#define EMC6D102_REG_EXTEND_ADC4 0x88
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Guenter Roeck09770b262012-01-14 20:47:36 -0800126/*
127 * Conversions. Rounding and limit checking is only done on the TO_REG
128 * variants. Note that you should be a bit careful with which arguments
129 * these macros are called: arguments may be evaluated more than once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300132/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400133static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200134 2500, 2250, 3300, 5000, 12000,
135 3300, 1500, 1800 /*EMC6D100*/
136};
137#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jean Delvare1f448092008-04-29 14:03:37 +0200139#define INS_TO_REG(n, val) \
Guenter Roeck67b20032016-12-04 18:16:48 -0800140 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
141 lm85_scaling[n], 192)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvare1f448092008-04-29 14:03:37 +0200143#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200144 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jean Delvare1f448092008-04-29 14:03:37 +0200146#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200149static inline u16 FAN_TO_REG(unsigned long val)
150{
151 if (!val)
152 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800153 return clamp_val(5400000 / val, 1, 0xfffe);
Jean Delvare63f281a2007-07-05 20:39:40 +0200154}
Jean Delvare1f448092008-04-29 14:03:37 +0200155#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* Temperature is reported in .001 degC increments */
159#define TEMP_TO_REG(val) \
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700160 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
Jean Delvare1f448092008-04-29 14:03:37 +0200161#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200162 SCALE(((val) << 4) + (ext), 16, 1000)
163#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Guenter Roeck2a844c12013-01-09 08:09:34 -0800165#define PWM_TO_REG(val) clamp_val(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define PWM_FROM_REG(val) (val)
167
168
Guenter Roeck09770b262012-01-14 20:47:36 -0800169/*
170 * ZONEs have the following parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Limit (low) temp, 1. degC
172 * Hysteresis (below limit), 1. degC (0-15)
173 * Range of speed control, .1 degC (2-80)
174 * Critical (high) temp, 1. degC
175 *
176 * FAN PWMs have the following parameters:
177 * Reference Zone, 1, 2, 3, etc.
178 * Spinup time, .05 sec
179 * PWM value at limit/low temp, 1 count
180 * PWM Frequency, 1. Hz
181 * PWM is Min or OFF below limit, flag
182 * Invert PWM output, flag
183 *
184 * Some chips filter the temp, others the fan.
185 * Filter constant (or disabled) .1 seconds
186 */
187
188/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400189static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200190 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
191 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
192};
193
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700194static int RANGE_TO_REG(long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700196 return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
Jean Delvare1f448092008-04-29 14:03:37 +0200198#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200201static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200202 10, 15, 23, 30, 38, 47, 61, 94
203};
204static const int adm1027_freq_map[8] = { /* 1 Hz */
205 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200206};
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700207#define FREQ_MAP_LEN 8
Jean Delvare1f448092008-04-29 14:03:37 +0200208
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700209static int FREQ_TO_REG(const int *map,
210 unsigned int map_size, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700212 return find_closest(freq, map, map_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200214
215static int FREQ_FROM_REG(const int *map, u8 reg)
216{
217 return map[reg & 0x07];
218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Guenter Roeck09770b262012-01-14 20:47:36 -0800220/*
221 * Since we can't use strings, I'm abusing these numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * to stand in for the following meanings:
223 * 1 -- PWM responds to Zone 1
224 * 2 -- PWM responds to Zone 2
225 * 3 -- PWM responds to Zone 3
226 * 23 -- PWM responds to the higher temp of Zone 2 or 3
227 * 123 -- PWM responds to highest of Zone 1, 2, or 3
228 * 0 -- PWM is always at 0% (ie, off)
229 * -1 -- PWM is always at 100%
230 * -2 -- PWM responds to manual control
231 */
232
Jean Delvaree89e22b2008-06-25 08:47:35 -0400233static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
234#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jean Delvare1f448092008-04-29 14:03:37 +0200236static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int i;
239
Jean Delvare1f448092008-04-29 14:03:37 +0200240 for (i = 0; i <= 7; ++i)
241 if (zone == lm85_zone_map[i])
242 break;
243 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400245 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Guenter Roeck2a844c12013-01-09 08:09:34 -0800248#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
Jean Delvare1f448092008-04-29 14:03:37 +0200249#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Guenter Roeck09770b262012-01-14 20:47:36 -0800251/*
252 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
254 * Some sensors are not updated more frequently than once per second
255 * so it doesn't make sense to read them more often than that.
256 * We cache the results and return the saved data if the driver
257 * is called again before a second has elapsed.
258 *
259 * Also, there is significant configuration data for this chip
260 * given the automatic PWM fan control that is possible. There
261 * are about 47 bytes of config data to only 22 bytes of actual
262 * readings. So, we keep the config data up to date in the cache
263 * when it is written and only sample it once every 1 *minute*
264 */
265#define LM85_DATA_INTERVAL (HZ + HZ / 2)
266#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
267
Guenter Roeck09770b262012-01-14 20:47:36 -0800268/*
269 * LM85 can automatically adjust fan speeds based on temperature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * This structure encapsulates an entire Zone config. There are
271 * three zones (one for each temperature input) on the lm85
272 */
273struct lm85_zone {
274 s8 limit; /* Low temp limit */
275 u8 hyst; /* Low limit hysteresis. (0-15) */
276 u8 range; /* Temp range, encoded */
277 s8 critical; /* "All fans ON" temp limit */
Guenter Roeck09770b262012-01-14 20:47:36 -0800278 u8 max_desired; /*
279 * Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * to prevent "drift" as other autofan control
281 * values change.
282 */
283};
284
285struct lm85_autofan {
286 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 u8 min_pwm; /* Minimum PWM value, encoded */
288 u8 min_off; /* Min PWM or OFF below "limit", flag */
289};
290
Guenter Roeck09770b262012-01-14 20:47:36 -0800291/*
292 * For each registered chip, we need to keep some data in memory.
293 * The structure is dynamically allocated.
294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295struct lm85_data {
Axel Lin746f6882014-07-23 12:29:11 +0800296 struct i2c_client *client;
297 const struct attribute_group *groups[6];
Jean Delvare8a0795d2008-10-17 17:51:14 +0200298 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 enum chips type;
300
Guenter Roeckde248802011-02-25 08:19:42 -0800301 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
302
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100303 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int valid; /* !=0 if following fields are valid */
305 unsigned long last_reading; /* In jiffies */
306 unsigned long last_config; /* In jiffies */
307
308 u8 in[8]; /* Register value */
309 u8 in_max[8]; /* Register value */
310 u8 in_min[8]; /* Register value */
311 s8 temp[3]; /* Register value */
312 s8 temp_min[3]; /* Register value */
313 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 u16 fan[4]; /* Register value */
315 u16 fan_min[4]; /* Register value */
316 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200317 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 u8 temp_ext[3]; /* Decoded values */
319 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u8 vid; /* Register value */
321 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800323 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct lm85_autofan autofan[3];
325 struct lm85_zone zone[3];
326};
327
Axel Lin6fd5dd52014-07-23 12:27:39 +0800328static int lm85_read_value(struct i2c_client *client, u8 reg)
329{
330 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Axel Lin6fd5dd52014-07-23 12:27:39 +0800332 /* What size location is it? */
333 switch (reg) {
334 case LM85_REG_FAN(0): /* Read WORD data */
335 case LM85_REG_FAN(1):
336 case LM85_REG_FAN(2):
337 case LM85_REG_FAN(3):
338 case LM85_REG_FAN_MIN(0):
339 case LM85_REG_FAN_MIN(1):
340 case LM85_REG_FAN_MIN(2):
341 case LM85_REG_FAN_MIN(3):
342 case LM85_REG_ALARM1: /* Read both bytes at once */
343 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
344 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
345 break;
346 default: /* Read BYTE data */
347 res = i2c_smbus_read_byte_data(client, reg);
348 break;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Axel Lin6fd5dd52014-07-23 12:27:39 +0800351 return res;
352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Axel Lin6fd5dd52014-07-23 12:27:39 +0800354static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
355{
356 switch (reg) {
357 case LM85_REG_FAN(0): /* Write WORD data */
358 case LM85_REG_FAN(1):
359 case LM85_REG_FAN(2):
360 case LM85_REG_FAN(3):
361 case LM85_REG_FAN_MIN(0):
362 case LM85_REG_FAN_MIN(1):
363 case LM85_REG_FAN_MIN(2):
364 case LM85_REG_FAN_MIN(3):
365 /* NOTE: ALARM is read only, so not included here */
366 i2c_smbus_write_byte_data(client, reg, value & 0xff);
367 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
368 break;
369 default: /* Write BYTE data */
370 i2c_smbus_write_byte_data(client, reg, value);
371 break;
372 }
373}
Jean Delvare67712d02008-10-17 17:51:14 +0200374
Axel Lin6fd5dd52014-07-23 12:27:39 +0800375static struct lm85_data *lm85_update_device(struct device *dev)
376{
Axel Lin746f6882014-07-23 12:29:11 +0800377 struct lm85_data *data = dev_get_drvdata(dev);
378 struct i2c_client *client = data->client;
Axel Lin6fd5dd52014-07-23 12:27:39 +0800379 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Axel Lin6fd5dd52014-07-23 12:27:39 +0800381 mutex_lock(&data->update_lock);
382
383 if (!data->valid ||
384 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
385 /* Things that change quickly */
386 dev_dbg(&client->dev, "Reading sensor values\n");
387
388 /*
389 * Have to read extended bits first to "freeze" the
390 * more significant bits that are read later.
391 * There are 2 additional resolution bits per channel and we
392 * have room for 4, so we shift them to the left.
393 */
394 if (data->type == adm1027 || data->type == adt7463 ||
395 data->type == adt7468) {
396 int ext1 = lm85_read_value(client,
397 ADM1027_REG_EXTEND_ADC1);
398 int ext2 = lm85_read_value(client,
399 ADM1027_REG_EXTEND_ADC2);
400 int val = (ext1 << 8) + ext2;
401
402 for (i = 0; i <= 4; i++)
403 data->in_ext[i] =
404 ((val >> (i * 2)) & 0x03) << 2;
405
406 for (i = 0; i <= 2; i++)
407 data->temp_ext[i] =
408 (val >> ((i + 4) * 2)) & 0x0c;
409 }
410
411 data->vid = lm85_read_value(client, LM85_REG_VID);
412
413 for (i = 0; i <= 3; ++i) {
414 data->in[i] =
415 lm85_read_value(client, LM85_REG_IN(i));
416 data->fan[i] =
417 lm85_read_value(client, LM85_REG_FAN(i));
418 }
419
420 if (!data->has_vid5)
421 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
422
423 if (data->type == adt7468)
424 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
425
426 for (i = 0; i <= 2; ++i) {
427 data->temp[i] =
428 lm85_read_value(client, LM85_REG_TEMP(i));
429 data->pwm[i] =
430 lm85_read_value(client, LM85_REG_PWM(i));
431
432 if (IS_ADT7468_OFF64(data))
433 data->temp[i] -= 64;
434 }
435
436 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
437
438 if (data->type == emc6d100) {
439 /* Three more voltage sensors */
440 for (i = 5; i <= 7; ++i) {
441 data->in[i] = lm85_read_value(client,
442 EMC6D100_REG_IN(i));
443 }
444 /* More alarm bits */
445 data->alarms |= lm85_read_value(client,
446 EMC6D100_REG_ALARM3) << 16;
447 } else if (data->type == emc6d102 || data->type == emc6d103 ||
448 data->type == emc6d103s) {
449 /*
450 * Have to read LSB bits after the MSB ones because
451 * the reading of the MSB bits has frozen the
452 * LSBs (backward from the ADM1027).
453 */
454 int ext1 = lm85_read_value(client,
455 EMC6D102_REG_EXTEND_ADC1);
456 int ext2 = lm85_read_value(client,
457 EMC6D102_REG_EXTEND_ADC2);
458 int ext3 = lm85_read_value(client,
459 EMC6D102_REG_EXTEND_ADC3);
460 int ext4 = lm85_read_value(client,
461 EMC6D102_REG_EXTEND_ADC4);
462 data->in_ext[0] = ext3 & 0x0f;
463 data->in_ext[1] = ext4 & 0x0f;
464 data->in_ext[2] = ext4 >> 4;
465 data->in_ext[3] = ext3 >> 4;
466 data->in_ext[4] = ext2 >> 4;
467
468 data->temp_ext[0] = ext1 & 0x0f;
469 data->temp_ext[1] = ext2 & 0x0f;
470 data->temp_ext[2] = ext1 >> 4;
471 }
472
473 data->last_reading = jiffies;
474 } /* last_reading */
475
476 if (!data->valid ||
477 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
478 /* Things that don't change often */
479 dev_dbg(&client->dev, "Reading config values\n");
480
481 for (i = 0; i <= 3; ++i) {
482 data->in_min[i] =
483 lm85_read_value(client, LM85_REG_IN_MIN(i));
484 data->in_max[i] =
485 lm85_read_value(client, LM85_REG_IN_MAX(i));
486 data->fan_min[i] =
487 lm85_read_value(client, LM85_REG_FAN_MIN(i));
488 }
489
490 if (!data->has_vid5) {
491 data->in_min[4] = lm85_read_value(client,
492 LM85_REG_IN_MIN(4));
493 data->in_max[4] = lm85_read_value(client,
494 LM85_REG_IN_MAX(4));
495 }
496
497 if (data->type == emc6d100) {
498 for (i = 5; i <= 7; ++i) {
499 data->in_min[i] = lm85_read_value(client,
500 EMC6D100_REG_IN_MIN(i));
501 data->in_max[i] = lm85_read_value(client,
502 EMC6D100_REG_IN_MAX(i));
503 }
504 }
505
506 for (i = 0; i <= 2; ++i) {
507 int val;
508
509 data->temp_min[i] =
510 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
511 data->temp_max[i] =
512 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
513
514 data->autofan[i].config =
515 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
516 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
517 data->pwm_freq[i] = val & 0x07;
518 data->zone[i].range = val >> 4;
519 data->autofan[i].min_pwm =
520 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
521 data->zone[i].limit =
522 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
523 data->zone[i].critical =
524 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
525
526 if (IS_ADT7468_OFF64(data)) {
527 data->temp_min[i] -= 64;
528 data->temp_max[i] -= 64;
529 data->zone[i].limit -= 64;
530 data->zone[i].critical -= 64;
531 }
532 }
533
534 if (data->type != emc6d103s) {
535 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
536 data->autofan[0].min_off = (i & 0x20) != 0;
537 data->autofan[1].min_off = (i & 0x40) != 0;
538 data->autofan[2].min_off = (i & 0x80) != 0;
539
540 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
541 data->zone[0].hyst = i >> 4;
542 data->zone[1].hyst = i & 0x0f;
543
544 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
545 data->zone[2].hyst = i >> 4;
546 }
547
548 data->last_config = jiffies;
549 } /* last_config */
550
551 data->valid = 1;
552
553 mutex_unlock(&data->update_lock);
554
555 return data;
556}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200559static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
560 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Jean Delvareb353a482007-07-05 20:36:12 +0200562 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200564 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
Jean Delvareb353a482007-07-05 20:36:12 +0200566
567static ssize_t show_fan_min(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_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Jean Delvareb353a482007-07-05 20:36:12 +0200574
575static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Jean Delvareb353a482007-07-05 20:36:12 +0200578 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800579 struct lm85_data *data = dev_get_drvdata(dev);
580 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800581 unsigned long val;
582 int err;
583
584 err = kstrtoul(buf, 10, &val);
585 if (err)
586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100588 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 data->fan_min[nr] = FAN_TO_REG(val);
590 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100591 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return count;
593}
594
595#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200596static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
597 show_fan, NULL, offset - 1); \
598static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
599 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601show_fan_offset(1);
602show_fan_offset(2);
603show_fan_offset(3);
604show_fan_offset(4);
605
606/* vid, vrm, alarms */
607
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100608static ssize_t cpu0_vid_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100612 int vid;
613
Guenter Roeckde248802011-02-25 08:19:42 -0800614 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100615 /* 6-pin VID (VRM 10) */
616 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
617 } else {
618 /* 5-pin VID (VRM 9) */
619 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
620 }
621
622 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100625static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100627static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
628 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jean Delvare90d66192007-10-08 18:24:35 +0200630 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return sprintf(buf, "%ld\n", (long) data->vrm);
632}
633
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100634static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100637 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b262012-01-14 20:47:36 -0800638 unsigned long val;
639 int err;
640
641 err = kstrtoul(buf, 10, &val);
642 if (err)
643 return err;
644
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700645 if (val > 255)
646 return -EINVAL;
647
Guenter Roeck09770b262012-01-14 20:47:36 -0800648 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return count;
650}
651
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100652static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100654static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
655 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200658 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Julia Lawalld0ed69d2016-12-22 13:04:52 +0100661static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200663static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
664 char *buf)
665{
666 int nr = to_sensor_dev_attr(attr)->index;
667 struct lm85_data *data = lm85_update_device(dev);
668 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
669}
670
671static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
672static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
673static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
674static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
675static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
676static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
677static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
678static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
679static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
680static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
681static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
682static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
683static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
684static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
685static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
686static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
687static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* pwm */
690
Jean Delvareb353a482007-07-05 20:36:12 +0200691static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
692 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Jean Delvareb353a482007-07-05 20:36:12 +0200694 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200696 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Jean Delvareb353a482007-07-05 20:36:12 +0200698
699static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jean Delvareb353a482007-07-05 20:36:12 +0200702 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800703 struct lm85_data *data = dev_get_drvdata(dev);
704 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800705 unsigned long val;
706 int err;
707
708 err = kstrtoul(buf, 10, &val);
709 if (err)
710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100712 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 data->pwm[nr] = PWM_TO_REG(val);
714 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100715 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return count;
717}
Jean Delvareb353a482007-07-05 20:36:12 +0200718
719static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
720 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Jean Delvareb353a482007-07-05 20:36:12 +0200722 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100724 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100727 switch (pwm_zone) {
728 case -1: /* PWM is always at 100% */
729 enable = 0;
730 break;
731 case 0: /* PWM is always at 0% */
732 case -2: /* PWM responds to manual control */
733 enable = 1;
734 break;
735 default: /* PWM in automatic mode */
736 enable = 2;
737 }
738 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Jean Delvare455f7912007-12-03 23:28:42 +0100741static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
742 *attr, const char *buf, size_t count)
743{
744 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800745 struct lm85_data *data = dev_get_drvdata(dev);
746 struct i2c_client *client = data->client;
Jean Delvare455f7912007-12-03 23:28:42 +0100747 u8 config;
Guenter Roeck09770b262012-01-14 20:47:36 -0800748 unsigned long val;
749 int err;
750
751 err = kstrtoul(buf, 10, &val);
752 if (err)
753 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100754
755 switch (val) {
756 case 0:
757 config = 3;
758 break;
759 case 1:
760 config = 7;
761 break;
762 case 2:
Guenter Roeck09770b262012-01-14 20:47:36 -0800763 /*
764 * Here we have to choose arbitrarily one of the 5 possible
765 * configurations; I go for the safest
766 */
Jean Delvare455f7912007-12-03 23:28:42 +0100767 config = 6;
768 break;
769 default:
770 return -EINVAL;
771 }
772
773 mutex_lock(&data->update_lock);
774 data->autofan[nr].config = lm85_read_value(client,
775 LM85_REG_AFAN_CONFIG(nr));
776 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
777 | (config << 5);
778 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
779 data->autofan[nr].config);
780 mutex_unlock(&data->update_lock);
781 return count;
782}
783
Jean Delvare34e7dc62008-10-17 17:51:13 +0200784static ssize_t show_pwm_freq(struct device *dev,
785 struct device_attribute *attr, char *buf)
786{
787 int nr = to_sensor_dev_attr(attr)->index;
788 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200789 int freq;
790
791 if (IS_ADT7468_HFPWM(data))
792 freq = 22500;
793 else
794 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
795
796 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200797}
798
799static ssize_t set_pwm_freq(struct device *dev,
800 struct device_attribute *attr, const char *buf, size_t count)
801{
802 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800803 struct lm85_data *data = dev_get_drvdata(dev);
804 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800805 unsigned long val;
806 int err;
807
808 err = kstrtoul(buf, 10, &val);
809 if (err)
810 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200811
812 mutex_lock(&data->update_lock);
Guenter Roeck09770b262012-01-14 20:47:36 -0800813 /*
814 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200815 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b262012-01-14 20:47:36 -0800816 * This might confuse the user, but there's not much we can do.
817 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200818 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
819 data->cfg5 &= ~ADT7468_HFPWM;
820 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
821 } else { /* Low freq. mode */
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700822 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
823 FREQ_MAP_LEN, val);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200824 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
825 (data->zone[nr].range << 4)
826 | data->pwm_freq[nr]);
827 if (data->type == adt7468) {
828 data->cfg5 |= ADT7468_HFPWM;
829 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
830 }
831 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200832 mutex_unlock(&data->update_lock);
833 return count;
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200837static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
838 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100839static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200840 show_pwm_enable, set_pwm_enable, offset - 1); \
841static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
842 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844show_pwm_reg(1);
845show_pwm_reg(2);
846show_pwm_reg(3);
847
848/* Voltages */
849
Jean Delvareb353a482007-07-05 20:36:12 +0200850static ssize_t show_in(struct device *dev, struct device_attribute *attr,
851 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Jean Delvareb353a482007-07-05 20:36:12 +0200853 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200855 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
856 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
Jean Delvareb353a482007-07-05 20:36:12 +0200858
Jean Delvare1f448092008-04-29 14:03:37 +0200859static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200860 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Jean Delvareb353a482007-07-05 20:36:12 +0200862 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200864 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Jean Delvareb353a482007-07-05 20:36:12 +0200866
867static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
868 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Jean Delvareb353a482007-07-05 20:36:12 +0200870 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800871 struct lm85_data *data = dev_get_drvdata(dev);
872 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800873 long val;
874 int err;
875
876 err = kstrtol(buf, 10, &val);
877 if (err)
878 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100880 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 data->in_min[nr] = INS_TO_REG(nr, val);
882 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100883 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return count;
885}
Jean Delvareb353a482007-07-05 20:36:12 +0200886
887static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
888 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jean Delvareb353a482007-07-05 20:36:12 +0200890 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200892 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
Jean Delvareb353a482007-07-05 20:36:12 +0200894
895static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
896 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jean Delvareb353a482007-07-05 20:36:12 +0200898 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800899 struct lm85_data *data = dev_get_drvdata(dev);
900 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800901 long val;
902 int err;
903
904 err = kstrtol(buf, 10, &val);
905 if (err)
906 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100908 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 data->in_max[nr] = INS_TO_REG(nr, val);
910 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100911 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return count;
913}
Jean Delvareb353a482007-07-05 20:36:12 +0200914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200916static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
917 show_in, NULL, offset); \
918static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
919 show_in_min, set_in_min, offset); \
920static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
921 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923show_in_reg(0);
924show_in_reg(1);
925show_in_reg(2);
926show_in_reg(3);
927show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200928show_in_reg(5);
929show_in_reg(6);
930show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932/* Temps */
933
Jean Delvareb353a482007-07-05 20:36:12 +0200934static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
935 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Jean Delvareb353a482007-07-05 20:36:12 +0200937 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200939 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
940 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
Jean Delvareb353a482007-07-05 20:36:12 +0200942
943static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
944 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jean Delvareb353a482007-07-05 20:36:12 +0200946 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200948 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Jean Delvareb353a482007-07-05 20:36:12 +0200950
951static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
952 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Jean Delvareb353a482007-07-05 20:36:12 +0200954 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800955 struct lm85_data *data = dev_get_drvdata(dev);
956 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800957 long val;
958 int err;
959
960 err = kstrtol(buf, 10, &val);
961 if (err)
962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800964 if (IS_ADT7468_OFF64(data))
965 val += 64;
966
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100967 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 data->temp_min[nr] = TEMP_TO_REG(val);
969 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100970 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return count;
972}
Jean Delvareb353a482007-07-05 20:36:12 +0200973
974static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
975 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Jean Delvareb353a482007-07-05 20:36:12 +0200977 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200979 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
Jean Delvareb353a482007-07-05 20:36:12 +0200981
982static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
983 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jean Delvareb353a482007-07-05 20:36:12 +0200985 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800986 struct lm85_data *data = dev_get_drvdata(dev);
987 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -0800988 long val;
989 int err;
990
991 err = kstrtol(buf, 10, &val);
992 if (err)
993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800995 if (IS_ADT7468_OFF64(data))
996 val += 64;
997
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100998 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 data->temp_max[nr] = TEMP_TO_REG(val);
1000 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001001 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return count;
1003}
Jean Delvareb353a482007-07-05 20:36:12 +02001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001006static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1007 show_temp, NULL, offset - 1); \
1008static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1009 show_temp_min, set_temp_min, offset - 1); \
1010static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1011 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013show_temp_reg(1);
1014show_temp_reg(2);
1015show_temp_reg(3);
1016
1017
1018/* Automatic PWM control */
1019
Jean Delvareb353a482007-07-05 20:36:12 +02001020static ssize_t show_pwm_auto_channels(struct device *dev,
1021 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Jean Delvareb353a482007-07-05 20:36:12 +02001023 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001025 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
Jean Delvareb353a482007-07-05 20:36:12 +02001027
1028static ssize_t set_pwm_auto_channels(struct device *dev,
1029 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Jean Delvareb353a482007-07-05 20:36:12 +02001031 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001032 struct lm85_data *data = dev_get_drvdata(dev);
1033 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -08001034 long val;
1035 int err;
1036
1037 err = kstrtol(buf, 10, &val);
1038 if (err)
1039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001041 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +02001043 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1045 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001046 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return count;
1048}
Jean Delvareb353a482007-07-05 20:36:12 +02001049
1050static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1051 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Jean Delvareb353a482007-07-05 20:36:12 +02001053 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001055 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
Jean Delvareb353a482007-07-05 20:36:12 +02001057
1058static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1059 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Jean Delvareb353a482007-07-05 20:36:12 +02001061 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001062 struct lm85_data *data = dev_get_drvdata(dev);
1063 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -08001064 unsigned long val;
1065 int err;
1066
1067 err = kstrtoul(buf, 10, &val);
1068 if (err)
1069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001071 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1073 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1074 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001075 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return count;
1077}
Jean Delvareb353a482007-07-05 20:36:12 +02001078
1079static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1080 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Jean Delvareb353a482007-07-05 20:36:12 +02001082 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001084 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
Jean Delvareb353a482007-07-05 20:36:12 +02001086
1087static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1088 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Jean Delvareb353a482007-07-05 20:36:12 +02001090 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001091 struct lm85_data *data = dev_get_drvdata(dev);
1092 struct i2c_client *client = data->client;
Jean Delvare7133e562008-04-12 19:56:35 +02001093 u8 tmp;
Guenter Roeck09770b262012-01-14 20:47:36 -08001094 long val;
1095 int err;
1096
1097 err = kstrtol(buf, 10, &val);
1098 if (err)
1099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001101 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +02001103 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1104 tmp &= ~(0x20 << nr);
1105 if (data->autofan[nr].min_off)
1106 tmp |= 0x20 << nr;
1107 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001108 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return count;
1110}
Jean Delvareb353a482007-07-05 20:36:12 +02001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001113static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1114 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1115 set_pwm_auto_channels, offset - 1); \
1116static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
1117 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
1118 set_pwm_auto_pwm_min, offset - 1); \
1119static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1120 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +02001121 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +02001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123pwm_auto(1);
1124pwm_auto(2);
1125pwm_auto(3);
1126
1127/* Temperature settings for automatic PWM control */
1128
Jean Delvareb353a482007-07-05 20:36:12 +02001129static ssize_t show_temp_auto_temp_off(struct device *dev,
1130 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Jean Delvareb353a482007-07-05 20:36:12 +02001132 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001134 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 HYST_FROM_REG(data->zone[nr].hyst));
1136}
Jean Delvareb353a482007-07-05 20:36:12 +02001137
1138static ssize_t set_temp_auto_temp_off(struct device *dev,
1139 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Jean Delvareb353a482007-07-05 20:36:12 +02001141 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001142 struct lm85_data *data = dev_get_drvdata(dev);
1143 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int min;
Guenter Roeck09770b262012-01-14 20:47:36 -08001145 long val;
1146 int err;
1147
1148 err = kstrtol(buf, 10, &val);
1149 if (err)
1150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001152 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +02001155 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1157 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +02001158 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 } else {
1160 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +02001161 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001163 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return count;
1165}
Jean Delvareb353a482007-07-05 20:36:12 +02001166
1167static ssize_t show_temp_auto_temp_min(struct device *dev,
1168 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jean Delvareb353a482007-07-05 20:36:12 +02001170 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001172 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Jean Delvareb353a482007-07-05 20:36:12 +02001174
1175static ssize_t set_temp_auto_temp_min(struct device *dev,
1176 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Jean Delvareb353a482007-07-05 20:36:12 +02001178 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001179 struct lm85_data *data = dev_get_drvdata(dev);
1180 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -08001181 long val;
1182 int err;
1183
1184 err = kstrtol(buf, 10, &val);
1185 if (err)
1186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001188 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 data->zone[nr].limit = TEMP_TO_REG(val);
1190 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1191 data->zone[nr].limit);
1192
1193/* Update temp_auto_max and temp_auto_range */
1194 data->zone[nr].range = RANGE_TO_REG(
1195 TEMP_FROM_REG(data->zone[nr].max_desired) -
1196 TEMP_FROM_REG(data->zone[nr].limit));
1197 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1198 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001199 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001201 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return count;
1203}
Jean Delvareb353a482007-07-05 20:36:12 +02001204
1205static ssize_t show_temp_auto_temp_max(struct device *dev,
1206 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Jean Delvareb353a482007-07-05 20:36:12 +02001208 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001210 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 RANGE_FROM_REG(data->zone[nr].range));
1212}
Jean Delvareb353a482007-07-05 20:36:12 +02001213
1214static ssize_t set_temp_auto_temp_max(struct device *dev,
1215 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Jean Delvareb353a482007-07-05 20:36:12 +02001217 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001218 struct lm85_data *data = dev_get_drvdata(dev);
1219 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int min;
Guenter Roeck09770b262012-01-14 20:47:36 -08001221 long val;
1222 int err;
1223
1224 err = kstrtol(buf, 10, &val);
1225 if (err)
1226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001228 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 min = TEMP_FROM_REG(data->zone[nr].limit);
1230 data->zone[nr].max_desired = TEMP_TO_REG(val);
1231 data->zone[nr].range = RANGE_TO_REG(
1232 val - min);
1233 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1234 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001235 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001236 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return count;
1238}
Jean Delvareb353a482007-07-05 20:36:12 +02001239
1240static ssize_t show_temp_auto_temp_crit(struct device *dev,
1241 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Jean Delvareb353a482007-07-05 20:36:12 +02001243 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001245 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
Jean Delvareb353a482007-07-05 20:36:12 +02001247
1248static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +02001249 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Jean Delvareb353a482007-07-05 20:36:12 +02001251 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001252 struct lm85_data *data = dev_get_drvdata(dev);
1253 struct i2c_client *client = data->client;
Guenter Roeck09770b262012-01-14 20:47:36 -08001254 long val;
1255 int err;
1256
1257 err = kstrtol(buf, 10, &val);
1258 if (err)
1259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001261 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 data->zone[nr].critical = TEMP_TO_REG(val);
1263 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1264 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001265 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return count;
1267}
Jean Delvareb353a482007-07-05 20:36:12 +02001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001270static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1271 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1272 set_temp_auto_temp_off, offset - 1); \
1273static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1274 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1275 set_temp_auto_temp_min, offset - 1); \
1276static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1277 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1278 set_temp_auto_temp_max, offset - 1); \
1279static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1280 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1281 set_temp_auto_temp_crit, offset - 1);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283temp_auto(1);
1284temp_auto(2);
1285temp_auto(3);
1286
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001287static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001288 &sensor_dev_attr_fan1_input.dev_attr.attr,
1289 &sensor_dev_attr_fan2_input.dev_attr.attr,
1290 &sensor_dev_attr_fan3_input.dev_attr.attr,
1291 &sensor_dev_attr_fan4_input.dev_attr.attr,
1292 &sensor_dev_attr_fan1_min.dev_attr.attr,
1293 &sensor_dev_attr_fan2_min.dev_attr.attr,
1294 &sensor_dev_attr_fan3_min.dev_attr.attr,
1295 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001296 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1297 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1298 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1299 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001300
1301 &sensor_dev_attr_pwm1.dev_attr.attr,
1302 &sensor_dev_attr_pwm2.dev_attr.attr,
1303 &sensor_dev_attr_pwm3.dev_attr.attr,
1304 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1305 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1306 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001307 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1308 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1309 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001310
1311 &sensor_dev_attr_in0_input.dev_attr.attr,
1312 &sensor_dev_attr_in1_input.dev_attr.attr,
1313 &sensor_dev_attr_in2_input.dev_attr.attr,
1314 &sensor_dev_attr_in3_input.dev_attr.attr,
1315 &sensor_dev_attr_in0_min.dev_attr.attr,
1316 &sensor_dev_attr_in1_min.dev_attr.attr,
1317 &sensor_dev_attr_in2_min.dev_attr.attr,
1318 &sensor_dev_attr_in3_min.dev_attr.attr,
1319 &sensor_dev_attr_in0_max.dev_attr.attr,
1320 &sensor_dev_attr_in1_max.dev_attr.attr,
1321 &sensor_dev_attr_in2_max.dev_attr.attr,
1322 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001323 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1324 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1325 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1326 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001327
1328 &sensor_dev_attr_temp1_input.dev_attr.attr,
1329 &sensor_dev_attr_temp2_input.dev_attr.attr,
1330 &sensor_dev_attr_temp3_input.dev_attr.attr,
1331 &sensor_dev_attr_temp1_min.dev_attr.attr,
1332 &sensor_dev_attr_temp2_min.dev_attr.attr,
1333 &sensor_dev_attr_temp3_min.dev_attr.attr,
1334 &sensor_dev_attr_temp1_max.dev_attr.attr,
1335 &sensor_dev_attr_temp2_max.dev_attr.attr,
1336 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001337 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1338 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1339 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1340 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1341 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001342
1343 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1344 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1345 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1346 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1347 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1348 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001349
Jean Delvareb353a482007-07-05 20:36:12 +02001350 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1351 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1352 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1353 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1354 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1355 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1356 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1357 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1358 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1359
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001360 &dev_attr_vrm.attr,
1361 &dev_attr_cpu0_vid.attr,
1362 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001363 NULL
1364};
1365
1366static const struct attribute_group lm85_group = {
1367 .attrs = lm85_attributes,
1368};
1369
Guenter Roeck06923f82011-02-19 08:27:47 -08001370static struct attribute *lm85_attributes_minctl[] = {
1371 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1372 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1373 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001374 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001375};
1376
1377static const struct attribute_group lm85_group_minctl = {
1378 .attrs = lm85_attributes_minctl,
1379};
1380
1381static struct attribute *lm85_attributes_temp_off[] = {
1382 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1383 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1384 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001385 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001386};
1387
1388static const struct attribute_group lm85_group_temp_off = {
1389 .attrs = lm85_attributes_temp_off,
1390};
1391
Jean Delvare6b9aad22007-07-05 20:37:21 +02001392static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001393 &sensor_dev_attr_in4_input.dev_attr.attr,
1394 &sensor_dev_attr_in4_min.dev_attr.attr,
1395 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001396 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001397 NULL
1398};
1399
Jean Delvare6b9aad22007-07-05 20:37:21 +02001400static const struct attribute_group lm85_group_in4 = {
1401 .attrs = lm85_attributes_in4,
1402};
1403
1404static struct attribute *lm85_attributes_in567[] = {
1405 &sensor_dev_attr_in5_input.dev_attr.attr,
1406 &sensor_dev_attr_in6_input.dev_attr.attr,
1407 &sensor_dev_attr_in7_input.dev_attr.attr,
1408 &sensor_dev_attr_in5_min.dev_attr.attr,
1409 &sensor_dev_attr_in6_min.dev_attr.attr,
1410 &sensor_dev_attr_in7_min.dev_attr.attr,
1411 &sensor_dev_attr_in5_max.dev_attr.attr,
1412 &sensor_dev_attr_in6_max.dev_attr.attr,
1413 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001414 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1415 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1416 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001417 NULL
1418};
1419
1420static const struct attribute_group lm85_group_in567 = {
1421 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001422};
1423
Jean Delvare5f44759472008-06-25 09:10:30 -04001424static void lm85_init_client(struct i2c_client *client)
1425{
1426 int value;
1427
1428 /* Start monitoring if needed */
1429 value = lm85_read_value(client, LM85_REG_CONFIG);
1430 if (!(value & 0x01)) {
1431 dev_info(&client->dev, "Starting monitoring\n");
1432 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1433 }
1434
1435 /* Warn about unusual configuration bits */
1436 if (value & 0x02)
1437 dev_warn(&client->dev, "Device configuration is locked\n");
1438 if (!(value & 0x04))
1439 dev_warn(&client->dev, "Device is not ready\n");
1440}
1441
Jean Delvare5cfaf332009-09-15 17:18:14 +02001442static int lm85_is_fake(struct i2c_client *client)
1443{
1444 /*
1445 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1446 * emulate the former except that it has no hardware monitoring function
1447 * so the readings are always 0.
1448 */
1449 int i;
1450 u8 in_temp, fan;
1451
1452 for (i = 0; i < 8; i++) {
1453 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1454 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1455 if (in_temp != 0x00 || fan != 0xff)
1456 return 0;
1457 }
1458
1459 return 1;
1460}
1461
Jean Delvare67712d02008-10-17 17:51:14 +02001462/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001463static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Jean Delvare67712d02008-10-17 17:51:14 +02001465 struct i2c_adapter *adapter = client->adapter;
1466 int address = client->addr;
Jean Delvare590e8532014-06-11 18:35:56 +02001467 const char *type_name = NULL;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001468 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Jean Delvaree89e22b2008-06-25 08:47:35 -04001470 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001472 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Jean Delvared42a2eb2009-12-09 20:35:53 +01001475 /* Determine the chip type */
1476 company = lm85_read_value(client, LM85_REG_COMPANY);
1477 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Guenter Roeckb55f3752013-01-10 10:01:24 -08001479 dev_dbg(&adapter->dev,
1480 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001481 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jean Delvared42a2eb2009-12-09 20:35:53 +01001483 if (company == LM85_COMPANY_NATIONAL) {
1484 switch (verstep) {
1485 case LM85_VERSTEP_LM85C:
1486 type_name = "lm85c";
1487 break;
1488 case LM85_VERSTEP_LM85B:
1489 type_name = "lm85b";
1490 break;
1491 case LM85_VERSTEP_LM96000_1:
1492 case LM85_VERSTEP_LM96000_2:
1493 /* Check for Winbond WPCD377I */
1494 if (lm85_is_fake(client)) {
1495 dev_dbg(&adapter->dev,
1496 "Found Winbond WPCD377I, ignoring\n");
1497 return -ENODEV;
1498 }
Jean Delvare590e8532014-06-11 18:35:56 +02001499 type_name = "lm85";
Jean Delvared42a2eb2009-12-09 20:35:53 +01001500 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001501 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001502 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1503 switch (verstep) {
1504 case LM85_VERSTEP_ADM1027:
1505 type_name = "adm1027";
1506 break;
1507 case LM85_VERSTEP_ADT7463:
1508 case LM85_VERSTEP_ADT7463C:
1509 type_name = "adt7463";
1510 break;
1511 case LM85_VERSTEP_ADT7468_1:
1512 case LM85_VERSTEP_ADT7468_2:
1513 type_name = "adt7468";
1514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001516 } else if (company == LM85_COMPANY_SMSC) {
1517 switch (verstep) {
1518 case LM85_VERSTEP_EMC6D100_A0:
1519 case LM85_VERSTEP_EMC6D100_A1:
1520 /* Note: we can't tell a '100 from a '101 */
1521 type_name = "emc6d100";
1522 break;
1523 case LM85_VERSTEP_EMC6D102:
1524 type_name = "emc6d102";
1525 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001526 case LM85_VERSTEP_EMC6D103_A0:
1527 case LM85_VERSTEP_EMC6D103_A1:
1528 type_name = "emc6d103";
1529 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001530 case LM85_VERSTEP_EMC6D103S:
1531 type_name = "emc6d103s";
1532 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
Jean Delvare590e8532014-06-11 18:35:56 +02001536 if (!type_name)
1537 return -ENODEV;
1538
Jean Delvare67712d02008-10-17 17:51:14 +02001539 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Jean Delvare67712d02008-10-17 17:51:14 +02001541 return 0;
1542}
1543
Axel Lin746f6882014-07-23 12:29:11 +08001544static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001545{
Axel Lin746f6882014-07-23 12:29:11 +08001546 struct device *dev = &client->dev;
1547 struct device *hwmon_dev;
Jean Delvare67712d02008-10-17 17:51:14 +02001548 struct lm85_data *data;
Axel Lin746f6882014-07-23 12:29:11 +08001549 int idx = 0;
Jean Delvare67712d02008-10-17 17:51:14 +02001550
Axel Lin746f6882014-07-23 12:29:11 +08001551 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001552 if (!data)
1553 return -ENOMEM;
1554
Axel Lin746f6882014-07-23 12:29:11 +08001555 data->client = client;
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001556 if (client->dev.of_node)
1557 data->type = (enum chips)of_device_get_match_data(&client->dev);
1558 else
1559 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001560 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jean Delvare67712d02008-10-17 17:51:14 +02001562 /* Fill in the chip specific driver values */
1563 switch (data->type) {
1564 case adm1027:
1565 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001566 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001567 case emc6d100:
1568 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001569 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001570 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001571 data->freq_map = adm1027_freq_map;
1572 break;
1573 default:
1574 data->freq_map = lm85_freq_map;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001578 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001581 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Axel Lin746f6882014-07-23 12:29:11 +08001583 /* sysfs hooks */
1584 data->groups[idx++] = &lm85_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Guenter Roeck06923f82011-02-19 08:27:47 -08001586 /* minctl and temp_off exist on all chips except emc6d103s */
1587 if (data->type != emc6d103s) {
Axel Lin746f6882014-07-23 12:29:11 +08001588 data->groups[idx++] = &lm85_group_minctl;
1589 data->groups[idx++] = &lm85_group_temp_off;
Guenter Roeck06923f82011-02-19 08:27:47 -08001590 }
1591
Guenter Roeck09770b262012-01-14 20:47:36 -08001592 /*
1593 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1594 * as a sixth digital VID input rather than an analog input.
1595 */
Guenter Roeckde248802011-02-25 08:19:42 -08001596 if (data->type == adt7463 || data->type == adt7468) {
1597 u8 vid = lm85_read_value(client, LM85_REG_VID);
1598 if (vid & 0x80)
1599 data->has_vid5 = true;
1600 }
1601
Axel Lin746f6882014-07-23 12:29:11 +08001602 if (!data->has_vid5)
1603 data->groups[idx++] = &lm85_group_in4;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001604
1605 /* The EMC6D100 has 3 additional voltage inputs */
Axel Lin746f6882014-07-23 12:29:11 +08001606 if (data->type == emc6d100)
1607 data->groups[idx++] = &lm85_group_in567;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001608
Axel Lin746f6882014-07-23 12:29:11 +08001609 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1610 data, data->groups);
1611 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Axel Lin6fd5dd52014-07-23 12:27:39 +08001614static const struct i2c_device_id lm85_id[] = {
1615 { "adm1027", adm1027 },
1616 { "adt7463", adt7463 },
1617 { "adt7468", adt7468 },
1618 { "lm85", lm85 },
1619 { "lm85b", lm85 },
1620 { "lm85c", lm85 },
1621 { "emc6d100", emc6d100 },
1622 { "emc6d101", emc6d100 },
1623 { "emc6d102", emc6d102 },
1624 { "emc6d103", emc6d103 },
1625 { "emc6d103s", emc6d103s },
1626 { }
1627};
1628MODULE_DEVICE_TABLE(i2c, lm85_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001630static const struct of_device_id lm85_of_match[] = {
1631 {
1632 .compatible = "adi,adm1027",
1633 .data = (void *)adm1027
1634 },
1635 {
1636 .compatible = "adi,adt7463",
1637 .data = (void *)adt7463
1638 },
1639 {
1640 .compatible = "adi,adt7468",
1641 .data = (void *)adt7468
1642 },
1643 {
1644 .compatible = "national,lm85",
1645 .data = (void *)lm85
1646 },
1647 {
1648 .compatible = "national,lm85b",
1649 .data = (void *)lm85
1650 },
1651 {
1652 .compatible = "national,lm85c",
1653 .data = (void *)lm85
1654 },
1655 {
1656 .compatible = "smsc,emc6d100",
1657 .data = (void *)emc6d100
1658 },
1659 {
1660 .compatible = "smsc,emc6d101",
1661 .data = (void *)emc6d100
1662 },
1663 {
1664 .compatible = "smsc,emc6d102",
1665 .data = (void *)emc6d102
1666 },
1667 {
1668 .compatible = "smsc,emc6d103",
1669 .data = (void *)emc6d103
1670 },
1671 {
1672 .compatible = "smsc,emc6d103s",
1673 .data = (void *)emc6d103s
1674 },
1675 { },
1676};
1677MODULE_DEVICE_TABLE(of, lm85_of_match);
1678
Axel Lin6fd5dd52014-07-23 12:27:39 +08001679static struct i2c_driver lm85_driver = {
1680 .class = I2C_CLASS_HWMON,
1681 .driver = {
1682 .name = "lm85",
Javier Martinez Canillas00c0f9d2017-02-24 10:13:03 -03001683 .of_match_table = of_match_ptr(lm85_of_match),
Axel Lin6fd5dd52014-07-23 12:27:39 +08001684 },
1685 .probe = lm85_probe,
Axel Lin6fd5dd52014-07-23 12:27:39 +08001686 .id_table = lm85_id,
1687 .detect = lm85_detect,
1688 .address_list = normal_i2c,
1689};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Axel Linf0967ee2012-01-20 15:38:18 +08001691module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001694MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1695 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001696 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697MODULE_DESCRIPTION("LM85-B, LM85-C driver");