blob: 29c8136ce9c50c5c90156cdb91915e642eabea35 [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>
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -070037#include <linux/util_macros.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050040static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jean Delvaree5e9f442009-12-14 21:17:27 +010042enum chips {
Jean Delvare590e8532014-06-11 18:35:56 +020043 lm85,
Jean Delvaree5e9f442009-12-14 21:17:27 +010044 adm1027, adt7463, adt7468,
Guenter Roeck06923f82011-02-19 08:27:47 -080045 emc6d100, emc6d102, emc6d103, emc6d103s
Jean Delvaree5e9f442009-12-14 21:17:27 +010046};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* The LM85 registers */
49
Guenter Roeck09770b22012-01-14 20:47:36 -080050#define LM85_REG_IN(nr) (0x20 + (nr))
51#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
52#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Guenter Roeck09770b22012-01-14 20:47:36 -080054#define LM85_REG_TEMP(nr) (0x25 + (nr))
55#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
56#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/* Fan speeds are LSB, MSB (2 bytes) */
Guenter Roeck09770b22012-01-14 20:47:36 -080059#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
60#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Guenter Roeck09770b22012-01-14 20:47:36 -080062#define LM85_REG_PWM(nr) (0x30 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Guenter Roeck09770b22012-01-14 20:47:36 -080064#define LM85_REG_COMPANY 0x3e
65#define LM85_REG_VERSTEP 0x3f
Darrick J. Wong79b92f22008-11-12 13:26:59 -080066
Guenter Roeck09770b22012-01-14 20:47:36 -080067#define ADT7468_REG_CFG5 0x7c
68#define ADT7468_OFF64 (1 << 0)
69#define ADT7468_HFPWM (1 << 1)
70#define IS_ADT7468_OFF64(data) \
Darrick J. Wong79b92f22008-11-12 13:26:59 -080071 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
Guenter Roeck09770b22012-01-14 20:47:36 -080072#define IS_ADT7468_HFPWM(data) \
Jean Delvaref6c61cf2010-10-28 20:31:50 +020073 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
Darrick J. Wong79b92f22008-11-12 13:26:59 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* These are the recognized values for the above regs */
Guenter Roeck09770b22012-01-14 20:47:36 -080076#define LM85_COMPANY_NATIONAL 0x01
77#define LM85_COMPANY_ANALOG_DEV 0x41
78#define LM85_COMPANY_SMSC 0x5c
Guenter Roeck09770b22012-01-14 20:47:36 -080079#define LM85_VERSTEP_LM85C 0x60
80#define LM85_VERSTEP_LM85B 0x62
81#define LM85_VERSTEP_LM96000_1 0x68
82#define LM85_VERSTEP_LM96000_2 0x69
83#define LM85_VERSTEP_ADM1027 0x60
84#define LM85_VERSTEP_ADT7463 0x62
85#define LM85_VERSTEP_ADT7463C 0x6A
86#define LM85_VERSTEP_ADT7468_1 0x71
87#define LM85_VERSTEP_ADT7468_2 0x72
88#define LM85_VERSTEP_EMC6D100_A0 0x60
89#define LM85_VERSTEP_EMC6D100_A1 0x61
90#define LM85_VERSTEP_EMC6D102 0x65
91#define LM85_VERSTEP_EMC6D103_A0 0x68
92#define LM85_VERSTEP_EMC6D103_A1 0x69
93#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Guenter Roeck09770b22012-01-14 20:47:36 -080095#define LM85_REG_CONFIG 0x40
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Guenter Roeck09770b22012-01-14 20:47:36 -080097#define LM85_REG_ALARM1 0x41
98#define LM85_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Guenter Roeck09770b22012-01-14 20:47:36 -0800100#define LM85_REG_VID 0x43
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* Automated FAN control */
Guenter Roeck09770b22012-01-14 20:47:36 -0800103#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
104#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
105#define LM85_REG_AFAN_SPIKE1 0x62
106#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
107#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
108#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
109#define LM85_REG_AFAN_HYST1 0x6d
110#define LM85_REG_AFAN_HYST2 0x6e
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Guenter Roeck09770b22012-01-14 20:47:36 -0800112#define ADM1027_REG_EXTEND_ADC1 0x76
113#define ADM1027_REG_EXTEND_ADC2 0x77
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115#define EMC6D100_REG_ALARM3 0x7d
116/* IN5, IN6 and IN7 */
Guenter Roeck09770b22012-01-14 20:47:36 -0800117#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
118#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
119#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
120#define EMC6D102_REG_EXTEND_ADC1 0x85
121#define EMC6D102_REG_EXTEND_ADC2 0x86
122#define EMC6D102_REG_EXTEND_ADC3 0x87
123#define EMC6D102_REG_EXTEND_ADC4 0x88
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Guenter Roeck09770b22012-01-14 20:47:36 -0800125/*
126 * Conversions. Rounding and limit checking is only done on the TO_REG
127 * variants. Note that you should be a bit careful with which arguments
128 * these macros are called: arguments may be evaluated more than once.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
130
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300131/* IN are scaled according to built-in resistors */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400132static const int lm85_scaling[] = { /* .001 Volts */
Jean Delvare1f448092008-04-29 14:03:37 +0200133 2500, 2250, 3300, 5000, 12000,
134 3300, 1500, 1800 /*EMC6D100*/
135};
136#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jean Delvare1f448092008-04-29 14:03:37 +0200138#define INS_TO_REG(n, val) \
Guenter Roeck67b20032016-12-04 18:16:48 -0800139 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
140 lm85_scaling[n], 192)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jean Delvare1f448092008-04-29 14:03:37 +0200142#define INSEXT_FROM_REG(n, val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200143 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jean Delvare1f448092008-04-29 14:03:37 +0200145#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* FAN speed is measured using 90kHz clock */
Jean Delvare63f281a2007-07-05 20:39:40 +0200148static inline u16 FAN_TO_REG(unsigned long val)
149{
150 if (!val)
151 return 0xffff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800152 return clamp_val(5400000 / val, 1, 0xfffe);
Jean Delvare63f281a2007-07-05 20:39:40 +0200153}
Jean Delvare1f448092008-04-29 14:03:37 +0200154#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
155 5400000 / (val))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/* Temperature is reported in .001 degC increments */
158#define TEMP_TO_REG(val) \
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700159 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
Jean Delvare1f448092008-04-29 14:03:37 +0200160#define TEMPEXT_FROM_REG(val, ext) \
Jean Delvare5a4d3ef2007-07-05 20:38:32 +0200161 SCALE(((val) << 4) + (ext), 16, 1000)
162#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Guenter Roeck2a844c12013-01-09 08:09:34 -0800164#define PWM_TO_REG(val) clamp_val(val, 0, 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define PWM_FROM_REG(val) (val)
166
167
Guenter Roeck09770b22012-01-14 20:47:36 -0800168/*
169 * ZONEs have the following parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Limit (low) temp, 1. degC
171 * Hysteresis (below limit), 1. degC (0-15)
172 * Range of speed control, .1 degC (2-80)
173 * Critical (high) temp, 1. degC
174 *
175 * FAN PWMs have the following parameters:
176 * Reference Zone, 1, 2, 3, etc.
177 * Spinup time, .05 sec
178 * PWM value at limit/low temp, 1 count
179 * PWM Frequency, 1. Hz
180 * PWM is Min or OFF below limit, flag
181 * Invert PWM output, flag
182 *
183 * Some chips filter the temp, others the fan.
184 * Filter constant (or disabled) .1 seconds
185 */
186
187/* These are the zone temperature range encodings in .001 degree C */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400188static const int lm85_range_map[] = {
Jean Delvare1f448092008-04-29 14:03:37 +0200189 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
190 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
191};
192
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700193static int RANGE_TO_REG(long range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700195 return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
Jean Delvare1f448092008-04-29 14:03:37 +0200197#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* These are the PWM frequency encodings */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200200static const int lm85_freq_map[8] = { /* 1 Hz */
Jean Delvare8a0795d2008-10-17 17:51:14 +0200201 10, 15, 23, 30, 38, 47, 61, 94
202};
203static const int adm1027_freq_map[8] = { /* 1 Hz */
204 11, 15, 22, 29, 35, 44, 59, 88
Jean Delvare1f448092008-04-29 14:03:37 +0200205};
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700206#define FREQ_MAP_LEN 8
Jean Delvare1f448092008-04-29 14:03:37 +0200207
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700208static int FREQ_TO_REG(const int *map,
209 unsigned int map_size, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700211 return find_closest(freq, map, map_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
Jean Delvare8a0795d2008-10-17 17:51:14 +0200213
214static int FREQ_FROM_REG(const int *map, u8 reg)
215{
216 return map[reg & 0x07];
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Guenter Roeck09770b22012-01-14 20:47:36 -0800219/*
220 * Since we can't use strings, I'm abusing these numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * to stand in for the following meanings:
222 * 1 -- PWM responds to Zone 1
223 * 2 -- PWM responds to Zone 2
224 * 3 -- PWM responds to Zone 3
225 * 23 -- PWM responds to the higher temp of Zone 2 or 3
226 * 123 -- PWM responds to highest of Zone 1, 2, or 3
227 * 0 -- PWM is always at 0% (ie, off)
228 * -1 -- PWM is always at 100%
229 * -2 -- PWM responds to manual control
230 */
231
Jean Delvaree89e22b2008-06-25 08:47:35 -0400232static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
233#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jean Delvare1f448092008-04-29 14:03:37 +0200235static int ZONE_TO_REG(int zone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
238
Jean Delvare1f448092008-04-29 14:03:37 +0200239 for (i = 0; i <= 7; ++i)
240 if (zone == lm85_zone_map[i])
241 break;
242 if (i > 7) /* Not found. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 i = 3; /* Always 100% */
Jean Delvaree89e22b2008-06-25 08:47:35 -0400244 return i << 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Guenter Roeck2a844c12013-01-09 08:09:34 -0800247#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
Jean Delvare1f448092008-04-29 14:03:37 +0200248#define HYST_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Guenter Roeck09770b22012-01-14 20:47:36 -0800250/*
251 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
253 * Some sensors are not updated more frequently than once per second
254 * so it doesn't make sense to read them more often than that.
255 * We cache the results and return the saved data if the driver
256 * is called again before a second has elapsed.
257 *
258 * Also, there is significant configuration data for this chip
259 * given the automatic PWM fan control that is possible. There
260 * are about 47 bytes of config data to only 22 bytes of actual
261 * readings. So, we keep the config data up to date in the cache
262 * when it is written and only sample it once every 1 *minute*
263 */
264#define LM85_DATA_INTERVAL (HZ + HZ / 2)
265#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
266
Guenter Roeck09770b22012-01-14 20:47:36 -0800267/*
268 * LM85 can automatically adjust fan speeds based on temperature
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * This structure encapsulates an entire Zone config. There are
270 * three zones (one for each temperature input) on the lm85
271 */
272struct lm85_zone {
273 s8 limit; /* Low temp limit */
274 u8 hyst; /* Low limit hysteresis. (0-15) */
275 u8 range; /* Temp range, encoded */
276 s8 critical; /* "All fans ON" temp limit */
Guenter Roeck09770b22012-01-14 20:47:36 -0800277 u8 max_desired; /*
278 * Actual "max" temperature specified. Preserved
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * to prevent "drift" as other autofan control
280 * values change.
281 */
282};
283
284struct lm85_autofan {
285 u8 config; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 u8 min_pwm; /* Minimum PWM value, encoded */
287 u8 min_off; /* Min PWM or OFF below "limit", flag */
288};
289
Guenter Roeck09770b22012-01-14 20:47:36 -0800290/*
291 * For each registered chip, we need to keep some data in memory.
292 * The structure is dynamically allocated.
293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294struct lm85_data {
Axel Lin746f6882014-07-23 12:29:11 +0800295 struct i2c_client *client;
296 const struct attribute_group *groups[6];
Jean Delvare8a0795d2008-10-17 17:51:14 +0200297 const int *freq_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 enum chips type;
299
Guenter Roeckde248802011-02-25 08:19:42 -0800300 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
301
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int valid; /* !=0 if following fields are valid */
304 unsigned long last_reading; /* In jiffies */
305 unsigned long last_config; /* In jiffies */
306
307 u8 in[8]; /* Register value */
308 u8 in_max[8]; /* Register value */
309 u8 in_min[8]; /* Register value */
310 s8 temp[3]; /* Register value */
311 s8 temp_min[3]; /* Register value */
312 s8 temp_max[3]; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 u16 fan[4]; /* Register value */
314 u16 fan_min[4]; /* Register value */
315 u8 pwm[3]; /* Register value */
Jean Delvare34e7dc62008-10-17 17:51:13 +0200316 u8 pwm_freq[3]; /* Register encoding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 u8 temp_ext[3]; /* Decoded values */
318 u8 in_ext[8]; /* Decoded values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 u8 vid; /* Register value */
320 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 u32 alarms; /* Register encoding, combined */
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800322 u8 cfg5; /* Config Register 5 on ADT7468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct lm85_autofan autofan[3];
324 struct lm85_zone zone[3];
325};
326
Axel Lin6fd5dd52014-07-23 12:27:39 +0800327static int lm85_read_value(struct i2c_client *client, u8 reg)
328{
329 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Axel Lin6fd5dd52014-07-23 12:27:39 +0800331 /* What size location is it? */
332 switch (reg) {
333 case LM85_REG_FAN(0): /* Read WORD data */
334 case LM85_REG_FAN(1):
335 case LM85_REG_FAN(2):
336 case LM85_REG_FAN(3):
337 case LM85_REG_FAN_MIN(0):
338 case LM85_REG_FAN_MIN(1):
339 case LM85_REG_FAN_MIN(2):
340 case LM85_REG_FAN_MIN(3):
341 case LM85_REG_ALARM1: /* Read both bytes at once */
342 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
343 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
344 break;
345 default: /* Read BYTE data */
346 res = i2c_smbus_read_byte_data(client, reg);
347 break;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Axel Lin6fd5dd52014-07-23 12:27:39 +0800350 return res;
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Axel Lin6fd5dd52014-07-23 12:27:39 +0800353static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
354{
355 switch (reg) {
356 case LM85_REG_FAN(0): /* Write WORD data */
357 case LM85_REG_FAN(1):
358 case LM85_REG_FAN(2):
359 case LM85_REG_FAN(3):
360 case LM85_REG_FAN_MIN(0):
361 case LM85_REG_FAN_MIN(1):
362 case LM85_REG_FAN_MIN(2):
363 case LM85_REG_FAN_MIN(3):
364 /* NOTE: ALARM is read only, so not included here */
365 i2c_smbus_write_byte_data(client, reg, value & 0xff);
366 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
367 break;
368 default: /* Write BYTE data */
369 i2c_smbus_write_byte_data(client, reg, value);
370 break;
371 }
372}
Jean Delvare67712d02008-10-17 17:51:14 +0200373
Axel Lin6fd5dd52014-07-23 12:27:39 +0800374static struct lm85_data *lm85_update_device(struct device *dev)
375{
Axel Lin746f6882014-07-23 12:29:11 +0800376 struct lm85_data *data = dev_get_drvdata(dev);
377 struct i2c_client *client = data->client;
Axel Lin6fd5dd52014-07-23 12:27:39 +0800378 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Axel Lin6fd5dd52014-07-23 12:27:39 +0800380 mutex_lock(&data->update_lock);
381
382 if (!data->valid ||
383 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
384 /* Things that change quickly */
385 dev_dbg(&client->dev, "Reading sensor values\n");
386
387 /*
388 * Have to read extended bits first to "freeze" the
389 * more significant bits that are read later.
390 * There are 2 additional resolution bits per channel and we
391 * have room for 4, so we shift them to the left.
392 */
393 if (data->type == adm1027 || data->type == adt7463 ||
394 data->type == adt7468) {
395 int ext1 = lm85_read_value(client,
396 ADM1027_REG_EXTEND_ADC1);
397 int ext2 = lm85_read_value(client,
398 ADM1027_REG_EXTEND_ADC2);
399 int val = (ext1 << 8) + ext2;
400
401 for (i = 0; i <= 4; i++)
402 data->in_ext[i] =
403 ((val >> (i * 2)) & 0x03) << 2;
404
405 for (i = 0; i <= 2; i++)
406 data->temp_ext[i] =
407 (val >> ((i + 4) * 2)) & 0x0c;
408 }
409
410 data->vid = lm85_read_value(client, LM85_REG_VID);
411
412 for (i = 0; i <= 3; ++i) {
413 data->in[i] =
414 lm85_read_value(client, LM85_REG_IN(i));
415 data->fan[i] =
416 lm85_read_value(client, LM85_REG_FAN(i));
417 }
418
419 if (!data->has_vid5)
420 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
421
422 if (data->type == adt7468)
423 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
424
425 for (i = 0; i <= 2; ++i) {
426 data->temp[i] =
427 lm85_read_value(client, LM85_REG_TEMP(i));
428 data->pwm[i] =
429 lm85_read_value(client, LM85_REG_PWM(i));
430
431 if (IS_ADT7468_OFF64(data))
432 data->temp[i] -= 64;
433 }
434
435 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
436
437 if (data->type == emc6d100) {
438 /* Three more voltage sensors */
439 for (i = 5; i <= 7; ++i) {
440 data->in[i] = lm85_read_value(client,
441 EMC6D100_REG_IN(i));
442 }
443 /* More alarm bits */
444 data->alarms |= lm85_read_value(client,
445 EMC6D100_REG_ALARM3) << 16;
446 } else if (data->type == emc6d102 || data->type == emc6d103 ||
447 data->type == emc6d103s) {
448 /*
449 * Have to read LSB bits after the MSB ones because
450 * the reading of the MSB bits has frozen the
451 * LSBs (backward from the ADM1027).
452 */
453 int ext1 = lm85_read_value(client,
454 EMC6D102_REG_EXTEND_ADC1);
455 int ext2 = lm85_read_value(client,
456 EMC6D102_REG_EXTEND_ADC2);
457 int ext3 = lm85_read_value(client,
458 EMC6D102_REG_EXTEND_ADC3);
459 int ext4 = lm85_read_value(client,
460 EMC6D102_REG_EXTEND_ADC4);
461 data->in_ext[0] = ext3 & 0x0f;
462 data->in_ext[1] = ext4 & 0x0f;
463 data->in_ext[2] = ext4 >> 4;
464 data->in_ext[3] = ext3 >> 4;
465 data->in_ext[4] = ext2 >> 4;
466
467 data->temp_ext[0] = ext1 & 0x0f;
468 data->temp_ext[1] = ext2 & 0x0f;
469 data->temp_ext[2] = ext1 >> 4;
470 }
471
472 data->last_reading = jiffies;
473 } /* last_reading */
474
475 if (!data->valid ||
476 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
477 /* Things that don't change often */
478 dev_dbg(&client->dev, "Reading config values\n");
479
480 for (i = 0; i <= 3; ++i) {
481 data->in_min[i] =
482 lm85_read_value(client, LM85_REG_IN_MIN(i));
483 data->in_max[i] =
484 lm85_read_value(client, LM85_REG_IN_MAX(i));
485 data->fan_min[i] =
486 lm85_read_value(client, LM85_REG_FAN_MIN(i));
487 }
488
489 if (!data->has_vid5) {
490 data->in_min[4] = lm85_read_value(client,
491 LM85_REG_IN_MIN(4));
492 data->in_max[4] = lm85_read_value(client,
493 LM85_REG_IN_MAX(4));
494 }
495
496 if (data->type == emc6d100) {
497 for (i = 5; i <= 7; ++i) {
498 data->in_min[i] = lm85_read_value(client,
499 EMC6D100_REG_IN_MIN(i));
500 data->in_max[i] = lm85_read_value(client,
501 EMC6D100_REG_IN_MAX(i));
502 }
503 }
504
505 for (i = 0; i <= 2; ++i) {
506 int val;
507
508 data->temp_min[i] =
509 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
510 data->temp_max[i] =
511 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
512
513 data->autofan[i].config =
514 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
515 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
516 data->pwm_freq[i] = val & 0x07;
517 data->zone[i].range = val >> 4;
518 data->autofan[i].min_pwm =
519 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
520 data->zone[i].limit =
521 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
522 data->zone[i].critical =
523 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
524
525 if (IS_ADT7468_OFF64(data)) {
526 data->temp_min[i] -= 64;
527 data->temp_max[i] -= 64;
528 data->zone[i].limit -= 64;
529 data->zone[i].critical -= 64;
530 }
531 }
532
533 if (data->type != emc6d103s) {
534 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
535 data->autofan[0].min_off = (i & 0x20) != 0;
536 data->autofan[1].min_off = (i & 0x40) != 0;
537 data->autofan[2].min_off = (i & 0x80) != 0;
538
539 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
540 data->zone[0].hyst = i >> 4;
541 data->zone[1].hyst = i & 0x0f;
542
543 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
544 data->zone[2].hyst = i >> 4;
545 }
546
547 data->last_config = jiffies;
548 } /* last_config */
549
550 data->valid = 1;
551
552 mutex_unlock(&data->update_lock);
553
554 return data;
555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/* 4 Fans */
Jean Delvareb353a482007-07-05 20:36:12 +0200558static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
559 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jean Delvareb353a482007-07-05 20:36:12 +0200561 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200563 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
Jean Delvareb353a482007-07-05 20:36:12 +0200565
566static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
567 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Jean Delvareb353a482007-07-05 20:36:12 +0200569 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200571 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
Jean Delvareb353a482007-07-05 20:36:12 +0200573
574static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
575 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Jean Delvareb353a482007-07-05 20:36:12 +0200577 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800578 struct lm85_data *data = dev_get_drvdata(dev);
579 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800580 unsigned long val;
581 int err;
582
583 err = kstrtoul(buf, 10, &val);
584 if (err)
585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100587 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 data->fan_min[nr] = FAN_TO_REG(val);
589 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100590 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return count;
592}
593
594#define show_fan_offset(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200595static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
596 show_fan, NULL, offset - 1); \
597static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
598 show_fan_min, set_fan_min, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600show_fan_offset(1);
601show_fan_offset(2);
602show_fan_offset(3);
603show_fan_offset(4);
604
605/* vid, vrm, alarms */
606
Jean Delvare1f448092008-04-29 14:03:37 +0200607static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
608 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare9c516ef2005-11-26 20:07:54 +0100611 int vid;
612
Guenter Roeckde248802011-02-25 08:19:42 -0800613 if (data->has_vid5) {
Jean Delvare9c516ef2005-11-26 20:07:54 +0100614 /* 6-pin VID (VRM 10) */
615 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
616 } else {
617 /* 5-pin VID (VRM 9) */
618 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
619 }
620
621 return sprintf(buf, "%d\n", vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
625
Jean Delvare1f448092008-04-29 14:03:37 +0200626static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
627 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Jean Delvare90d66192007-10-08 18:24:35 +0200629 struct lm85_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return sprintf(buf, "%ld\n", (long) data->vrm);
631}
632
Jean Delvare1f448092008-04-29 14:03:37 +0200633static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
634 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100636 struct lm85_data *data = dev_get_drvdata(dev);
Guenter Roeck09770b22012-01-14 20:47:36 -0800637 unsigned long val;
638 int err;
639
640 err = kstrtoul(buf, 10, &val);
641 if (err)
642 return err;
643
Guenter Roeck3248c3b2014-07-29 22:23:12 -0700644 if (val > 255)
645 return -EINVAL;
646
Guenter Roeck09770b22012-01-14 20:47:36 -0800647 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return count;
649}
650
651static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
652
Jean Delvare1f448092008-04-29 14:03:37 +0200653static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
654 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200657 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
661
Jean Delvarebf76e9d2007-07-05 20:37:58 +0200662static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
663 char *buf)
664{
665 int nr = to_sensor_dev_attr(attr)->index;
666 struct lm85_data *data = lm85_update_device(dev);
667 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
668}
669
670static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
671static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
672static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
673static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
674static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
675static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
676static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
677static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
678static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
679static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
680static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
681static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
682static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
683static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
684static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
685static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
686static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/* pwm */
689
Jean Delvareb353a482007-07-05 20:36:12 +0200690static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
691 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jean Delvareb353a482007-07-05 20:36:12 +0200693 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200695 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Jean Delvareb353a482007-07-05 20:36:12 +0200697
698static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
699 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Jean Delvareb353a482007-07-05 20:36:12 +0200701 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800702 struct lm85_data *data = dev_get_drvdata(dev);
703 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800704 unsigned long val;
705 int err;
706
707 err = kstrtoul(buf, 10, &val);
708 if (err)
709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100711 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 data->pwm[nr] = PWM_TO_REG(val);
713 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100714 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return count;
716}
Jean Delvareb353a482007-07-05 20:36:12 +0200717
718static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
719 *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Jean Delvareb353a482007-07-05 20:36:12 +0200721 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare4b4df952007-12-03 23:23:21 +0100723 int pwm_zone, enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
Jean Delvare4b4df952007-12-03 23:23:21 +0100726 switch (pwm_zone) {
727 case -1: /* PWM is always at 100% */
728 enable = 0;
729 break;
730 case 0: /* PWM is always at 0% */
731 case -2: /* PWM responds to manual control */
732 enable = 1;
733 break;
734 default: /* PWM in automatic mode */
735 enable = 2;
736 }
737 return sprintf(buf, "%d\n", enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Jean Delvare455f7912007-12-03 23:28:42 +0100740static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
741 *attr, const char *buf, size_t count)
742{
743 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800744 struct lm85_data *data = dev_get_drvdata(dev);
745 struct i2c_client *client = data->client;
Jean Delvare455f7912007-12-03 23:28:42 +0100746 u8 config;
Guenter Roeck09770b22012-01-14 20:47:36 -0800747 unsigned long val;
748 int err;
749
750 err = kstrtoul(buf, 10, &val);
751 if (err)
752 return err;
Jean Delvare455f7912007-12-03 23:28:42 +0100753
754 switch (val) {
755 case 0:
756 config = 3;
757 break;
758 case 1:
759 config = 7;
760 break;
761 case 2:
Guenter Roeck09770b22012-01-14 20:47:36 -0800762 /*
763 * Here we have to choose arbitrarily one of the 5 possible
764 * configurations; I go for the safest
765 */
Jean Delvare455f7912007-12-03 23:28:42 +0100766 config = 6;
767 break;
768 default:
769 return -EINVAL;
770 }
771
772 mutex_lock(&data->update_lock);
773 data->autofan[nr].config = lm85_read_value(client,
774 LM85_REG_AFAN_CONFIG(nr));
775 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
776 | (config << 5);
777 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
778 data->autofan[nr].config);
779 mutex_unlock(&data->update_lock);
780 return count;
781}
782
Jean Delvare34e7dc62008-10-17 17:51:13 +0200783static ssize_t show_pwm_freq(struct device *dev,
784 struct device_attribute *attr, char *buf)
785{
786 int nr = to_sensor_dev_attr(attr)->index;
787 struct lm85_data *data = lm85_update_device(dev);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200788 int freq;
789
790 if (IS_ADT7468_HFPWM(data))
791 freq = 22500;
792 else
793 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
794
795 return sprintf(buf, "%d\n", freq);
Jean Delvare34e7dc62008-10-17 17:51:13 +0200796}
797
798static ssize_t set_pwm_freq(struct device *dev,
799 struct device_attribute *attr, const char *buf, size_t count)
800{
801 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800802 struct lm85_data *data = dev_get_drvdata(dev);
803 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800804 unsigned long val;
805 int err;
806
807 err = kstrtoul(buf, 10, &val);
808 if (err)
809 return err;
Jean Delvare34e7dc62008-10-17 17:51:13 +0200810
811 mutex_lock(&data->update_lock);
Guenter Roeck09770b22012-01-14 20:47:36 -0800812 /*
813 * The ADT7468 has a special high-frequency PWM output mode,
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200814 * where all PWM outputs are driven by a 22.5 kHz clock.
Guenter Roeck09770b22012-01-14 20:47:36 -0800815 * This might confuse the user, but there's not much we can do.
816 */
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200817 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
818 data->cfg5 &= ~ADT7468_HFPWM;
819 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
820 } else { /* Low freq. mode */
Bartosz Golaszewski0f3721c2015-04-16 12:43:36 -0700821 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
822 FREQ_MAP_LEN, val);
Jean Delvaref6c61cf2010-10-28 20:31:50 +0200823 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
824 (data->zone[nr].range << 4)
825 | data->pwm_freq[nr]);
826 if (data->type == adt7468) {
827 data->cfg5 |= ADT7468_HFPWM;
828 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
829 }
830 }
Jean Delvare34e7dc62008-10-17 17:51:13 +0200831 mutex_unlock(&data->update_lock);
832 return count;
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835#define show_pwm_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200836static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
837 show_pwm, set_pwm, offset - 1); \
Jean Delvare455f7912007-12-03 23:28:42 +0100838static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
Jean Delvare34e7dc62008-10-17 17:51:13 +0200839 show_pwm_enable, set_pwm_enable, offset - 1); \
840static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
841 show_pwm_freq, set_pwm_freq, offset - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843show_pwm_reg(1);
844show_pwm_reg(2);
845show_pwm_reg(3);
846
847/* Voltages */
848
Jean Delvareb353a482007-07-05 20:36:12 +0200849static ssize_t show_in(struct device *dev, struct device_attribute *attr,
850 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Jean Delvareb353a482007-07-05 20:36:12 +0200852 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200854 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
855 data->in_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Jean Delvareb353a482007-07-05 20:36:12 +0200857
Jean Delvare1f448092008-04-29 14:03:37 +0200858static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
Jean Delvareb353a482007-07-05 20:36:12 +0200859 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Jean Delvareb353a482007-07-05 20:36:12 +0200861 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200863 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Jean Delvareb353a482007-07-05 20:36:12 +0200865
866static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
867 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Jean Delvareb353a482007-07-05 20:36:12 +0200869 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800870 struct lm85_data *data = dev_get_drvdata(dev);
871 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800872 long val;
873 int err;
874
875 err = kstrtol(buf, 10, &val);
876 if (err)
877 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100879 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 data->in_min[nr] = INS_TO_REG(nr, val);
881 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100882 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return count;
884}
Jean Delvareb353a482007-07-05 20:36:12 +0200885
886static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
887 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Jean Delvareb353a482007-07-05 20:36:12 +0200889 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200891 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
Jean Delvareb353a482007-07-05 20:36:12 +0200893
894static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Jean Delvareb353a482007-07-05 20:36:12 +0200897 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800898 struct lm85_data *data = dev_get_drvdata(dev);
899 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800900 long val;
901 int err;
902
903 err = kstrtol(buf, 10, &val);
904 if (err)
905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100907 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 data->in_max[nr] = INS_TO_REG(nr, val);
909 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100910 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return count;
912}
Jean Delvareb353a482007-07-05 20:36:12 +0200913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#define show_in_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +0200915static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
916 show_in, NULL, offset); \
917static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
918 show_in_min, set_in_min, offset); \
919static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
920 show_in_max, set_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922show_in_reg(0);
923show_in_reg(1);
924show_in_reg(2);
925show_in_reg(3);
926show_in_reg(4);
Jean Delvare6b9aad22007-07-05 20:37:21 +0200927show_in_reg(5);
928show_in_reg(6);
929show_in_reg(7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931/* Temps */
932
Jean Delvareb353a482007-07-05 20:36:12 +0200933static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
934 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jean Delvareb353a482007-07-05 20:36:12 +0200936 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200938 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
939 data->temp_ext[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
Jean Delvareb353a482007-07-05 20:36:12 +0200941
942static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
943 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jean Delvareb353a482007-07-05 20:36:12 +0200945 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200947 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
Jean Delvareb353a482007-07-05 20:36:12 +0200949
950static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
951 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jean Delvareb353a482007-07-05 20:36:12 +0200953 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800954 struct lm85_data *data = dev_get_drvdata(dev);
955 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800956 long val;
957 int err;
958
959 err = kstrtol(buf, 10, &val);
960 if (err)
961 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800963 if (IS_ADT7468_OFF64(data))
964 val += 64;
965
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100966 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 data->temp_min[nr] = TEMP_TO_REG(val);
968 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100969 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return count;
971}
Jean Delvareb353a482007-07-05 20:36:12 +0200972
973static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
974 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Jean Delvareb353a482007-07-05 20:36:12 +0200976 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +0200978 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
Jean Delvareb353a482007-07-05 20:36:12 +0200980
981static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
982 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jean Delvareb353a482007-07-05 20:36:12 +0200984 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +0800985 struct lm85_data *data = dev_get_drvdata(dev);
986 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -0800987 long val;
988 int err;
989
990 err = kstrtol(buf, 10, &val);
991 if (err)
992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Darrick J. Wong79b92f22008-11-12 13:26:59 -0800994 if (IS_ADT7468_OFF64(data))
995 val += 64;
996
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100997 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 data->temp_max[nr] = TEMP_TO_REG(val);
999 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001000 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return count;
1002}
Jean Delvareb353a482007-07-05 20:36:12 +02001003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#define show_temp_reg(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001005static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1006 show_temp, NULL, offset - 1); \
1007static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1008 show_temp_min, set_temp_min, offset - 1); \
1009static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1010 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012show_temp_reg(1);
1013show_temp_reg(2);
1014show_temp_reg(3);
1015
1016
1017/* Automatic PWM control */
1018
Jean Delvareb353a482007-07-05 20:36:12 +02001019static ssize_t show_pwm_auto_channels(struct device *dev,
1020 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Jean Delvareb353a482007-07-05 20:36:12 +02001022 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001024 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
Jean Delvareb353a482007-07-05 20:36:12 +02001026
1027static ssize_t set_pwm_auto_channels(struct device *dev,
1028 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Jean Delvareb353a482007-07-05 20:36:12 +02001030 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001031 struct lm85_data *data = dev_get_drvdata(dev);
1032 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001033 long val;
1034 int err;
1035
1036 err = kstrtol(buf, 10, &val);
1037 if (err)
1038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001040 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
Jean Delvare1f448092008-04-29 14:03:37 +02001042 | ZONE_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1044 data->autofan[nr].config);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001045 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return count;
1047}
Jean Delvareb353a482007-07-05 20:36:12 +02001048
1049static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1050 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Jean Delvareb353a482007-07-05 20:36:12 +02001052 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001054 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
Jean Delvareb353a482007-07-05 20:36:12 +02001056
1057static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1058 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Jean Delvareb353a482007-07-05 20:36:12 +02001060 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001061 struct lm85_data *data = dev_get_drvdata(dev);
1062 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001063 unsigned long val;
1064 int err;
1065
1066 err = kstrtoul(buf, 10, &val);
1067 if (err)
1068 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001070 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1072 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1073 data->autofan[nr].min_pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001074 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return count;
1076}
Jean Delvareb353a482007-07-05 20:36:12 +02001077
1078static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1079 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Jean Delvareb353a482007-07-05 20:36:12 +02001081 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001083 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
Jean Delvareb353a482007-07-05 20:36:12 +02001085
1086static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1087 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Jean Delvareb353a482007-07-05 20:36:12 +02001089 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001090 struct lm85_data *data = dev_get_drvdata(dev);
1091 struct i2c_client *client = data->client;
Jean Delvare7133e562008-04-12 19:56:35 +02001092 u8 tmp;
Guenter Roeck09770b22012-01-14 20:47:36 -08001093 long val;
1094 int err;
1095
1096 err = kstrtol(buf, 10, &val);
1097 if (err)
1098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001100 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 data->autofan[nr].min_off = val;
Jean Delvare7133e562008-04-12 19:56:35 +02001102 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1103 tmp &= ~(0x20 << nr);
1104 if (data->autofan[nr].min_off)
1105 tmp |= 0x20 << nr;
1106 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001107 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return count;
1109}
Jean Delvareb353a482007-07-05 20:36:12 +02001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111#define pwm_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001112static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1113 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1114 set_pwm_auto_channels, offset - 1); \
1115static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
1116 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
1117 set_pwm_auto_pwm_min, offset - 1); \
1118static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1119 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
Jean Delvare34e7dc62008-10-17 17:51:13 +02001120 set_pwm_auto_pwm_minctl, offset - 1)
Jean Delvareb353a482007-07-05 20:36:12 +02001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122pwm_auto(1);
1123pwm_auto(2);
1124pwm_auto(3);
1125
1126/* Temperature settings for automatic PWM control */
1127
Jean Delvareb353a482007-07-05 20:36:12 +02001128static ssize_t show_temp_auto_temp_off(struct device *dev,
1129 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Jean Delvareb353a482007-07-05 20:36:12 +02001131 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001133 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 HYST_FROM_REG(data->zone[nr].hyst));
1135}
Jean Delvareb353a482007-07-05 20:36:12 +02001136
1137static ssize_t set_temp_auto_temp_off(struct device *dev,
1138 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Jean Delvareb353a482007-07-05 20:36:12 +02001140 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001141 struct lm85_data *data = dev_get_drvdata(dev);
1142 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001144 long val;
1145 int err;
1146
1147 err = kstrtol(buf, 10, &val);
1148 if (err)
1149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001151 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 min = TEMP_FROM_REG(data->zone[nr].limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 data->zone[nr].hyst = HYST_TO_REG(min - val);
Jean Delvare1f448092008-04-29 14:03:37 +02001154 if (nr == 0 || nr == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1156 (data->zone[0].hyst << 4)
Jean Delvare1f448092008-04-29 14:03:37 +02001157 | data->zone[1].hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 } else {
1159 lm85_write_value(client, LM85_REG_AFAN_HYST2,
Jean Delvare1f448092008-04-29 14:03:37 +02001160 (data->zone[2].hyst << 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001162 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return count;
1164}
Jean Delvareb353a482007-07-05 20:36:12 +02001165
1166static ssize_t show_temp_auto_temp_min(struct device *dev,
1167 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Jean Delvareb353a482007-07-05 20:36:12 +02001169 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001171 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
Jean Delvareb353a482007-07-05 20:36:12 +02001173
1174static ssize_t set_temp_auto_temp_min(struct device *dev,
1175 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Jean Delvareb353a482007-07-05 20:36:12 +02001177 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001178 struct lm85_data *data = dev_get_drvdata(dev);
1179 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001180 long val;
1181 int err;
1182
1183 err = kstrtol(buf, 10, &val);
1184 if (err)
1185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001187 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 data->zone[nr].limit = TEMP_TO_REG(val);
1189 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1190 data->zone[nr].limit);
1191
1192/* Update temp_auto_max and temp_auto_range */
1193 data->zone[nr].range = RANGE_TO_REG(
1194 TEMP_FROM_REG(data->zone[nr].max_desired) -
1195 TEMP_FROM_REG(data->zone[nr].limit));
1196 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1197 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001198 | (data->pwm_freq[nr] & 0x07));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001200 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return count;
1202}
Jean Delvareb353a482007-07-05 20:36:12 +02001203
1204static ssize_t show_temp_auto_temp_max(struct device *dev,
1205 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Jean Delvareb353a482007-07-05 20:36:12 +02001207 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001209 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 RANGE_FROM_REG(data->zone[nr].range));
1211}
Jean Delvareb353a482007-07-05 20:36:12 +02001212
1213static ssize_t set_temp_auto_temp_max(struct device *dev,
1214 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Jean Delvareb353a482007-07-05 20:36:12 +02001216 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001217 struct lm85_data *data = dev_get_drvdata(dev);
1218 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int min;
Guenter Roeck09770b22012-01-14 20:47:36 -08001220 long val;
1221 int err;
1222
1223 err = kstrtol(buf, 10, &val);
1224 if (err)
1225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001227 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 min = TEMP_FROM_REG(data->zone[nr].limit);
1229 data->zone[nr].max_desired = TEMP_TO_REG(val);
1230 data->zone[nr].range = RANGE_TO_REG(
1231 val - min);
1232 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1233 ((data->zone[nr].range & 0x0f) << 4)
Jean Delvare34e7dc62008-10-17 17:51:13 +02001234 | (data->pwm_freq[nr] & 0x07));
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001235 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return count;
1237}
Jean Delvareb353a482007-07-05 20:36:12 +02001238
1239static ssize_t show_temp_auto_temp_crit(struct device *dev,
1240 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Jean Delvareb353a482007-07-05 20:36:12 +02001242 int nr = to_sensor_dev_attr(attr)->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct lm85_data *data = lm85_update_device(dev);
Jean Delvare1f448092008-04-29 14:03:37 +02001244 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
Jean Delvareb353a482007-07-05 20:36:12 +02001246
1247static ssize_t set_temp_auto_temp_crit(struct device *dev,
Jean Delvare1f448092008-04-29 14:03:37 +02001248 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Jean Delvareb353a482007-07-05 20:36:12 +02001250 int nr = to_sensor_dev_attr(attr)->index;
Axel Lin746f6882014-07-23 12:29:11 +08001251 struct lm85_data *data = dev_get_drvdata(dev);
1252 struct i2c_client *client = data->client;
Guenter Roeck09770b22012-01-14 20:47:36 -08001253 long val;
1254 int err;
1255
1256 err = kstrtol(buf, 10, &val);
1257 if (err)
1258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001260 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 data->zone[nr].critical = TEMP_TO_REG(val);
1262 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1263 data->zone[nr].critical);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001264 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return count;
1266}
Jean Delvareb353a482007-07-05 20:36:12 +02001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#define temp_auto(offset) \
Jean Delvareb353a482007-07-05 20:36:12 +02001269static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1270 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1271 set_temp_auto_temp_off, offset - 1); \
1272static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1273 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1274 set_temp_auto_temp_min, offset - 1); \
1275static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1276 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1277 set_temp_auto_temp_max, offset - 1); \
1278static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1279 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1280 set_temp_auto_temp_crit, offset - 1);
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282temp_auto(1);
1283temp_auto(2);
1284temp_auto(3);
1285
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001286static struct attribute *lm85_attributes[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001287 &sensor_dev_attr_fan1_input.dev_attr.attr,
1288 &sensor_dev_attr_fan2_input.dev_attr.attr,
1289 &sensor_dev_attr_fan3_input.dev_attr.attr,
1290 &sensor_dev_attr_fan4_input.dev_attr.attr,
1291 &sensor_dev_attr_fan1_min.dev_attr.attr,
1292 &sensor_dev_attr_fan2_min.dev_attr.attr,
1293 &sensor_dev_attr_fan3_min.dev_attr.attr,
1294 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001295 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1296 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1297 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1298 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001299
1300 &sensor_dev_attr_pwm1.dev_attr.attr,
1301 &sensor_dev_attr_pwm2.dev_attr.attr,
1302 &sensor_dev_attr_pwm3.dev_attr.attr,
1303 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1304 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1305 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare34e7dc62008-10-17 17:51:13 +02001306 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1307 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1308 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001309
1310 &sensor_dev_attr_in0_input.dev_attr.attr,
1311 &sensor_dev_attr_in1_input.dev_attr.attr,
1312 &sensor_dev_attr_in2_input.dev_attr.attr,
1313 &sensor_dev_attr_in3_input.dev_attr.attr,
1314 &sensor_dev_attr_in0_min.dev_attr.attr,
1315 &sensor_dev_attr_in1_min.dev_attr.attr,
1316 &sensor_dev_attr_in2_min.dev_attr.attr,
1317 &sensor_dev_attr_in3_min.dev_attr.attr,
1318 &sensor_dev_attr_in0_max.dev_attr.attr,
1319 &sensor_dev_attr_in1_max.dev_attr.attr,
1320 &sensor_dev_attr_in2_max.dev_attr.attr,
1321 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001322 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1323 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1324 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1325 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001326
1327 &sensor_dev_attr_temp1_input.dev_attr.attr,
1328 &sensor_dev_attr_temp2_input.dev_attr.attr,
1329 &sensor_dev_attr_temp3_input.dev_attr.attr,
1330 &sensor_dev_attr_temp1_min.dev_attr.attr,
1331 &sensor_dev_attr_temp2_min.dev_attr.attr,
1332 &sensor_dev_attr_temp3_min.dev_attr.attr,
1333 &sensor_dev_attr_temp1_max.dev_attr.attr,
1334 &sensor_dev_attr_temp2_max.dev_attr.attr,
1335 &sensor_dev_attr_temp3_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001336 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1337 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1338 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1339 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1340 &sensor_dev_attr_temp3_fault.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001341
1342 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1343 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1344 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1345 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1346 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1347 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
Jean Delvareb353a482007-07-05 20:36:12 +02001348
Jean Delvareb353a482007-07-05 20:36:12 +02001349 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1350 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1351 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1352 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1353 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1354 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1355 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1356 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1357 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1358
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001359 &dev_attr_vrm.attr,
1360 &dev_attr_cpu0_vid.attr,
1361 &dev_attr_alarms.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001362 NULL
1363};
1364
1365static const struct attribute_group lm85_group = {
1366 .attrs = lm85_attributes,
1367};
1368
Guenter Roeck06923f82011-02-19 08:27:47 -08001369static struct attribute *lm85_attributes_minctl[] = {
1370 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1371 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1372 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001373 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001374};
1375
1376static const struct attribute_group lm85_group_minctl = {
1377 .attrs = lm85_attributes_minctl,
1378};
1379
1380static struct attribute *lm85_attributes_temp_off[] = {
1381 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1382 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1383 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
Jean Delvare5f441e22011-04-29 16:33:36 +02001384 NULL
Guenter Roeck06923f82011-02-19 08:27:47 -08001385};
1386
1387static const struct attribute_group lm85_group_temp_off = {
1388 .attrs = lm85_attributes_temp_off,
1389};
1390
Jean Delvare6b9aad22007-07-05 20:37:21 +02001391static struct attribute *lm85_attributes_in4[] = {
Jean Delvareb353a482007-07-05 20:36:12 +02001392 &sensor_dev_attr_in4_input.dev_attr.attr,
1393 &sensor_dev_attr_in4_min.dev_attr.attr,
1394 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001395 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001396 NULL
1397};
1398
Jean Delvare6b9aad22007-07-05 20:37:21 +02001399static const struct attribute_group lm85_group_in4 = {
1400 .attrs = lm85_attributes_in4,
1401};
1402
1403static struct attribute *lm85_attributes_in567[] = {
1404 &sensor_dev_attr_in5_input.dev_attr.attr,
1405 &sensor_dev_attr_in6_input.dev_attr.attr,
1406 &sensor_dev_attr_in7_input.dev_attr.attr,
1407 &sensor_dev_attr_in5_min.dev_attr.attr,
1408 &sensor_dev_attr_in6_min.dev_attr.attr,
1409 &sensor_dev_attr_in7_min.dev_attr.attr,
1410 &sensor_dev_attr_in5_max.dev_attr.attr,
1411 &sensor_dev_attr_in6_max.dev_attr.attr,
1412 &sensor_dev_attr_in7_max.dev_attr.attr,
Jean Delvarebf76e9d2007-07-05 20:37:58 +02001413 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1414 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1415 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Jean Delvare6b9aad22007-07-05 20:37:21 +02001416 NULL
1417};
1418
1419static const struct attribute_group lm85_group_in567 = {
1420 .attrs = lm85_attributes_in567,
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001421};
1422
Jean Delvare5f44759472008-06-25 09:10:30 -04001423static void lm85_init_client(struct i2c_client *client)
1424{
1425 int value;
1426
1427 /* Start monitoring if needed */
1428 value = lm85_read_value(client, LM85_REG_CONFIG);
1429 if (!(value & 0x01)) {
1430 dev_info(&client->dev, "Starting monitoring\n");
1431 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1432 }
1433
1434 /* Warn about unusual configuration bits */
1435 if (value & 0x02)
1436 dev_warn(&client->dev, "Device configuration is locked\n");
1437 if (!(value & 0x04))
1438 dev_warn(&client->dev, "Device is not ready\n");
1439}
1440
Jean Delvare5cfaf332009-09-15 17:18:14 +02001441static int lm85_is_fake(struct i2c_client *client)
1442{
1443 /*
1444 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1445 * emulate the former except that it has no hardware monitoring function
1446 * so the readings are always 0.
1447 */
1448 int i;
1449 u8 in_temp, fan;
1450
1451 for (i = 0; i < 8; i++) {
1452 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1453 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1454 if (in_temp != 0x00 || fan != 0xff)
1455 return 0;
1456 }
1457
1458 return 1;
1459}
1460
Jean Delvare67712d02008-10-17 17:51:14 +02001461/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001462static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Jean Delvare67712d02008-10-17 17:51:14 +02001464 struct i2c_adapter *adapter = client->adapter;
1465 int address = client->addr;
Jean Delvare590e8532014-06-11 18:35:56 +02001466 const char *type_name = NULL;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001467 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Jean Delvaree89e22b2008-06-25 08:47:35 -04001469 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* We need to be able to do byte I/O */
Jean Delvare67712d02008-10-17 17:51:14 +02001471 return -ENODEV;
Jean Delvare1f448092008-04-29 14:03:37 +02001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Jean Delvared42a2eb2009-12-09 20:35:53 +01001474 /* Determine the chip type */
1475 company = lm85_read_value(client, LM85_REG_COMPANY);
1476 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Guenter Roeckb55f3752013-01-10 10:01:24 -08001478 dev_dbg(&adapter->dev,
1479 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvared42a2eb2009-12-09 20:35:53 +01001480 address, company, verstep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jean Delvared42a2eb2009-12-09 20:35:53 +01001482 if (company == LM85_COMPANY_NATIONAL) {
1483 switch (verstep) {
1484 case LM85_VERSTEP_LM85C:
1485 type_name = "lm85c";
1486 break;
1487 case LM85_VERSTEP_LM85B:
1488 type_name = "lm85b";
1489 break;
1490 case LM85_VERSTEP_LM96000_1:
1491 case LM85_VERSTEP_LM96000_2:
1492 /* Check for Winbond WPCD377I */
1493 if (lm85_is_fake(client)) {
1494 dev_dbg(&adapter->dev,
1495 "Found Winbond WPCD377I, ignoring\n");
1496 return -ENODEV;
1497 }
Jean Delvare590e8532014-06-11 18:35:56 +02001498 type_name = "lm85";
Jean Delvared42a2eb2009-12-09 20:35:53 +01001499 break;
Jean Delvare69fc1fe2008-10-17 17:51:13 +02001500 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001501 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1502 switch (verstep) {
1503 case LM85_VERSTEP_ADM1027:
1504 type_name = "adm1027";
1505 break;
1506 case LM85_VERSTEP_ADT7463:
1507 case LM85_VERSTEP_ADT7463C:
1508 type_name = "adt7463";
1509 break;
1510 case LM85_VERSTEP_ADT7468_1:
1511 case LM85_VERSTEP_ADT7468_2:
1512 type_name = "adt7468";
1513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Jean Delvared42a2eb2009-12-09 20:35:53 +01001515 } else if (company == LM85_COMPANY_SMSC) {
1516 switch (verstep) {
1517 case LM85_VERSTEP_EMC6D100_A0:
1518 case LM85_VERSTEP_EMC6D100_A1:
1519 /* Note: we can't tell a '100 from a '101 */
1520 type_name = "emc6d100";
1521 break;
1522 case LM85_VERSTEP_EMC6D102:
1523 type_name = "emc6d102";
1524 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001525 case LM85_VERSTEP_EMC6D103_A0:
1526 case LM85_VERSTEP_EMC6D103_A1:
1527 type_name = "emc6d103";
1528 break;
Jan Beulichf065a932011-02-18 03:18:26 -05001529 case LM85_VERSTEP_EMC6D103S:
1530 type_name = "emc6d103s";
1531 break;
Jean Delvared42a2eb2009-12-09 20:35:53 +01001532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
Jean Delvare590e8532014-06-11 18:35:56 +02001535 if (!type_name)
1536 return -ENODEV;
1537
Jean Delvare67712d02008-10-17 17:51:14 +02001538 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Jean Delvare67712d02008-10-17 17:51:14 +02001540 return 0;
1541}
1542
Axel Lin746f6882014-07-23 12:29:11 +08001543static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
Guenter Roeckbc6db2b2011-02-25 08:26:47 -08001544{
Axel Lin746f6882014-07-23 12:29:11 +08001545 struct device *dev = &client->dev;
1546 struct device *hwmon_dev;
Jean Delvare67712d02008-10-17 17:51:14 +02001547 struct lm85_data *data;
Axel Lin746f6882014-07-23 12:29:11 +08001548 int idx = 0;
Jean Delvare67712d02008-10-17 17:51:14 +02001549
Axel Lin746f6882014-07-23 12:29:11 +08001550 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
Jean Delvare67712d02008-10-17 17:51:14 +02001551 if (!data)
1552 return -ENOMEM;
1553
Axel Lin746f6882014-07-23 12:29:11 +08001554 data->client = client;
Jean Delvare67712d02008-10-17 17:51:14 +02001555 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001556 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jean Delvare67712d02008-10-17 17:51:14 +02001558 /* Fill in the chip specific driver values */
1559 switch (data->type) {
1560 case adm1027:
1561 case adt7463:
Jean Delvarefa7a5792010-10-28 20:31:50 +02001562 case adt7468:
Jean Delvare67712d02008-10-17 17:51:14 +02001563 case emc6d100:
1564 case emc6d102:
Jan Beulichf065a932011-02-18 03:18:26 -05001565 case emc6d103:
Guenter Roeck06923f82011-02-19 08:27:47 -08001566 case emc6d103s:
Jean Delvare67712d02008-10-17 17:51:14 +02001567 data->freq_map = adm1027_freq_map;
1568 break;
1569 default:
1570 data->freq_map = lm85_freq_map;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001574 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* Initialize the LM85 chip */
Jean Delvaree89e22b2008-06-25 08:47:35 -04001577 lm85_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Axel Lin746f6882014-07-23 12:29:11 +08001579 /* sysfs hooks */
1580 data->groups[idx++] = &lm85_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Guenter Roeck06923f82011-02-19 08:27:47 -08001582 /* minctl and temp_off exist on all chips except emc6d103s */
1583 if (data->type != emc6d103s) {
Axel Lin746f6882014-07-23 12:29:11 +08001584 data->groups[idx++] = &lm85_group_minctl;
1585 data->groups[idx++] = &lm85_group_temp_off;
Guenter Roeck06923f82011-02-19 08:27:47 -08001586 }
1587
Guenter Roeck09770b22012-01-14 20:47:36 -08001588 /*
1589 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1590 * as a sixth digital VID input rather than an analog input.
1591 */
Guenter Roeckde248802011-02-25 08:19:42 -08001592 if (data->type == adt7463 || data->type == adt7468) {
1593 u8 vid = lm85_read_value(client, LM85_REG_VID);
1594 if (vid & 0x80)
1595 data->has_vid5 = true;
1596 }
1597
Axel Lin746f6882014-07-23 12:29:11 +08001598 if (!data->has_vid5)
1599 data->groups[idx++] = &lm85_group_in4;
Jean Delvare6b9aad22007-07-05 20:37:21 +02001600
1601 /* The EMC6D100 has 3 additional voltage inputs */
Axel Lin746f6882014-07-23 12:29:11 +08001602 if (data->type == emc6d100)
1603 data->groups[idx++] = &lm85_group_in567;
Mark M. Hoffman0501a382006-09-24 21:14:35 +02001604
Axel Lin746f6882014-07-23 12:29:11 +08001605 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1606 data, data->groups);
1607 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Axel Lin6fd5dd52014-07-23 12:27:39 +08001610static const struct i2c_device_id lm85_id[] = {
1611 { "adm1027", adm1027 },
1612 { "adt7463", adt7463 },
1613 { "adt7468", adt7468 },
1614 { "lm85", lm85 },
1615 { "lm85b", lm85 },
1616 { "lm85c", lm85 },
1617 { "emc6d100", emc6d100 },
1618 { "emc6d101", emc6d100 },
1619 { "emc6d102", emc6d102 },
1620 { "emc6d103", emc6d103 },
1621 { "emc6d103s", emc6d103s },
1622 { }
1623};
1624MODULE_DEVICE_TABLE(i2c, lm85_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Axel Lin6fd5dd52014-07-23 12:27:39 +08001626static struct i2c_driver lm85_driver = {
1627 .class = I2C_CLASS_HWMON,
1628 .driver = {
1629 .name = "lm85",
1630 },
1631 .probe = lm85_probe,
Axel Lin6fd5dd52014-07-23 12:27:39 +08001632 .id_table = lm85_id,
1633 .detect = lm85_detect,
1634 .address_list = normal_i2c,
1635};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Axel Linf0967ee2012-01-20 15:38:18 +08001637module_i2c_driver(lm85_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639MODULE_LICENSE("GPL");
Jean Delvare1f448092008-04-29 14:03:37 +02001640MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1641 "Margit Schubert-While <margitsw@t-online.de>, "
Jean Delvaree89e22b2008-06-25 08:47:35 -04001642 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643MODULE_DESCRIPTION("LM85-B, LM85-C driver");