]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/lm85.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / lm85.c
CommitLineData
1da177e4 1/*
09770b26
GR
2 * 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>
590e8534 8 * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de>
09770b26
GR
9 *
10 * Chip details at <http://www.national.com/ds/LM/LM85.pdf>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
1da177e4 26
1da177e4 27#include <linux/module.h>
00c0f9d3 28#include <linux/of_device.h>
1da177e4
LT
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/jiffies.h>
32#include <linux/i2c.h>
943b0830 33#include <linux/hwmon.h>
303760b4 34#include <linux/hwmon-vid.h>
b353a487 35#include <linux/hwmon-sysfs.h>
943b0830 36#include <linux/err.h>
9a61bf63 37#include <linux/mutex.h>
0f3721c5 38#include <linux/util_macros.h>
1da177e4
LT
39
40/* Addresses to scan */
25e9c86d 41static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1da177e4 42
e5e9f44c 43enum chips {
590e8534 44 lm85,
e5e9f44c 45 adm1027, adt7463, adt7468,
06923f84 46 emc6d100, emc6d102, emc6d103, emc6d103s
e5e9f44c 47};
1da177e4
LT
48
49/* The LM85 registers */
50
09770b26
GR
51#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)
1da177e4 54
09770b26
GR
55#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)
1da177e4
LT
58
59/* Fan speeds are LSB, MSB (2 bytes) */
09770b26
GR
60#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
61#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
1da177e4 62
09770b26 63#define LM85_REG_PWM(nr) (0x30 + (nr))
1da177e4 64
09770b26
GR
65#define LM85_REG_COMPANY 0x3e
66#define LM85_REG_VERSTEP 0x3f
79b92f2b 67
09770b26
GR
68#define ADT7468_REG_CFG5 0x7c
69#define ADT7468_OFF64 (1 << 0)
70#define ADT7468_HFPWM (1 << 1)
71#define IS_ADT7468_OFF64(data) \
79b92f2b 72 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
09770b26 73#define IS_ADT7468_HFPWM(data) \
f6c61cff 74 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
79b92f2b 75
1da177e4 76/* These are the recognized values for the above regs */
09770b26
GR
77#define LM85_COMPANY_NATIONAL 0x01
78#define LM85_COMPANY_ANALOG_DEV 0x41
79#define LM85_COMPANY_SMSC 0x5c
09770b26
GR
80#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 */
95
96#define LM85_REG_CONFIG 0x40
97
98#define LM85_REG_ALARM1 0x41
99#define LM85_REG_ALARM2 0x42
100
101#define LM85_REG_VID 0x43
1da177e4
LT
102
103/* Automated FAN control */
09770b26
GR
104#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
112
113#define ADM1027_REG_EXTEND_ADC1 0x76
114#define ADM1027_REG_EXTEND_ADC2 0x77
1da177e4
LT
115
116#define EMC6D100_REG_ALARM3 0x7d
117/* IN5, IN6 and IN7 */
09770b26
GR
118#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
125
09770b26
GR
126/*
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.
1da177e4
LT
130 */
131
25985edc 132/* IN are scaled according to built-in resistors */
e89e22b2 133static const int lm85_scaling[] = { /* .001 Volts */
1f44809a
JD
134 2500, 2250, 3300, 5000, 12000,
135 3300, 1500, 1800 /*EMC6D100*/
136};
137#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
1da177e4 138
1f44809a 139#define INS_TO_REG(n, val) \
67b20034
GR
140 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
141 lm85_scaling[n], 192)
1da177e4 142
1f44809a 143#define INSEXT_FROM_REG(n, val, ext) \
5a4d3ef3 144 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
1da177e4 145
1f44809a 146#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
1da177e4
LT
147
148/* FAN speed is measured using 90kHz clock */
63f281a6
JD
149static inline u16 FAN_TO_REG(unsigned long val)
150{
151 if (!val)
152 return 0xffff;
2a844c14 153 return clamp_val(5400000 / val, 1, 0xfffe);
63f281a6 154}
1f44809a
JD
155#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 5400000 / (val))
1da177e4
LT
157
158/* Temperature is reported in .001 degC increments */
159#define TEMP_TO_REG(val) \
3248c3b7 160 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
1f44809a 161#define TEMPEXT_FROM_REG(val, ext) \
5a4d3ef3
JD
162 SCALE(((val) << 4) + (ext), 16, 1000)
163#define TEMP_FROM_REG(val) ((val) * 1000)
1da177e4 164
2a844c14 165#define PWM_TO_REG(val) clamp_val(val, 0, 255)
1da177e4
LT
166#define PWM_FROM_REG(val) (val)
167
168
09770b26
GR
169/*
170 * ZONEs have the following parameters:
1da177e4
LT
171 * 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 */
e89e22b2 189static const int lm85_range_map[] = {
1f44809a
JD
190 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
191 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
192};
193
3248c3b7 194static int RANGE_TO_REG(long range)
1da177e4 195{
0f3721c5 196 return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map));
1da177e4 197}
1f44809a 198#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
1da177e4 199
1da177e4 200/* These are the PWM frequency encodings */
34e7dc6c 201static const int lm85_freq_map[8] = { /* 1 Hz */
8a0795d9
JD
202 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
1f44809a 206};
0f3721c5 207#define FREQ_MAP_LEN 8
1f44809a 208
0f3721c5
BG
209static int FREQ_TO_REG(const int *map,
210 unsigned int map_size, unsigned long freq)
1da177e4 211{
0f3721c5 212 return find_closest(freq, map, map_size);
1da177e4 213}
8a0795d9
JD
214
215static int FREQ_FROM_REG(const int *map, u8 reg)
216{
217 return map[reg & 0x07];
218}
1da177e4 219
09770b26
GR
220/*
221 * Since we can't use strings, I'm abusing these numbers
1da177e4
LT
222 * 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
e89e22b2
JD
233static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
234#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
1da177e4 235
1f44809a 236static int ZONE_TO_REG(int zone)
1da177e4
LT
237{
238 int i;
239
1f44809a
JD
240 for (i = 0; i <= 7; ++i)
241 if (zone == lm85_zone_map[i])
242 break;
243 if (i > 7) /* Not found. */
1da177e4 244 i = 3; /* Always 100% */
e89e22b2 245 return i << 5;
1da177e4
LT
246}
247
2a844c14 248#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
1f44809a 249#define HYST_FROM_REG(val) ((val) * 1000)
1da177e4 250
09770b26
GR
251/*
252 * Chip sampling rates
1da177e4
LT
253 *
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
09770b26
GR
268/*
269 * LM85 can automatically adjust fan speeds based on temperature
1da177e4
LT
270 * 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 */
09770b26
GR
278 u8 max_desired; /*
279 * Actual "max" temperature specified. Preserved
1da177e4
LT
280 * to prevent "drift" as other autofan control
281 * values change.
282 */
283};
284
285struct lm85_autofan {
286 u8 config; /* Register value */
1da177e4
LT
287 u8 min_pwm; /* Minimum PWM value, encoded */
288 u8 min_off; /* Min PWM or OFF below "limit", flag */
289};
290
09770b26
GR
291/*
292 * For each registered chip, we need to keep some data in memory.
293 * The structure is dynamically allocated.
294 */
1da177e4 295struct lm85_data {
746f6884
AL
296 struct i2c_client *client;
297 const struct attribute_group *groups[6];
8a0795d9 298 const int *freq_map;
1da177e4
LT
299 enum chips type;
300
de248805
GR
301 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
302
9a61bf63 303 struct mutex update_lock;
1da177e4
LT
304 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 */
1da177e4
LT
314 u16 fan[4]; /* Register value */
315 u16 fan_min[4]; /* Register value */
316 u8 pwm[3]; /* Register value */
34e7dc6c 317 u8 pwm_freq[3]; /* Register encoding */
1da177e4
LT
318 u8 temp_ext[3]; /* Decoded values */
319 u8 in_ext[8]; /* Decoded values */
1da177e4
LT
320 u8 vid; /* Register value */
321 u8 vrm; /* VRM version */
1da177e4 322 u32 alarms; /* Register encoding, combined */
79b92f2b 323 u8 cfg5; /* Config Register 5 on ADT7468 */
1da177e4
LT
324 struct lm85_autofan autofan[3];
325 struct lm85_zone zone[3];
326};
327
6fd5dd58
AL
328static int lm85_read_value(struct i2c_client *client, u8 reg)
329{
330 int res;
1da177e4 331
6fd5dd58
AL
332 /* 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 }
1da177e4 350
6fd5dd58
AL
351 return res;
352}
1da177e4 353
6fd5dd58
AL
354static 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}
67712d01 374
6fd5dd58
AL
375static struct lm85_data *lm85_update_device(struct device *dev)
376{
746f6884
AL
377 struct lm85_data *data = dev_get_drvdata(dev);
378 struct i2c_client *client = data->client;
6fd5dd58
AL
379 int i;
380
381 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));
1da177e4 431
6fd5dd58
AL
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}
1da177e4
LT
557
558/* 4 Fans */
b353a487
JD
559static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
560 char *buf)
1da177e4 561{
b353a487 562 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 563 struct lm85_data *data = lm85_update_device(dev);
1f44809a 564 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
1da177e4 565}
b353a487
JD
566
567static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
568 char *buf)
1da177e4 569{
b353a487 570 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 571 struct lm85_data *data = lm85_update_device(dev);
1f44809a 572 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
1da177e4 573}
b353a487
JD
574
575static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
1da177e4 577{
b353a487 578 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
579 struct lm85_data *data = dev_get_drvdata(dev);
580 struct i2c_client *client = data->client;
09770b26
GR
581 unsigned long val;
582 int err;
583
584 err = kstrtoul(buf, 10, &val);
585 if (err)
586 return err;
1da177e4 587
9a61bf63 588 mutex_lock(&data->update_lock);
1da177e4
LT
589 data->fan_min[nr] = FAN_TO_REG(val);
590 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 591 mutex_unlock(&data->update_lock);
1da177e4
LT
592 return count;
593}
594
595#define show_fan_offset(offset) \
b353a487
JD
596static 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)
1da177e4
LT
600
601show_fan_offset(1);
602show_fan_offset(2);
603show_fan_offset(3);
604show_fan_offset(4);
605
606/* vid, vrm, alarms */
607
d0ed69d5
JL
608static ssize_t cpu0_vid_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
1da177e4
LT
610{
611 struct lm85_data *data = lm85_update_device(dev);
9c516ef4
JD
612 int vid;
613
de248805 614 if (data->has_vid5) {
9c516ef4
JD
615 /* 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);
1da177e4
LT
623}
624
d0ed69d5 625static DEVICE_ATTR_RO(cpu0_vid);
1da177e4 626
d0ed69d5
JL
627static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
628 char *buf)
1da177e4 629{
90d6619a 630 struct lm85_data *data = dev_get_drvdata(dev);
1da177e4
LT
631 return sprintf(buf, "%ld\n", (long) data->vrm);
632}
633
d0ed69d5
JL
634static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
1da177e4 636{
8f74efe8 637 struct lm85_data *data = dev_get_drvdata(dev);
09770b26
GR
638 unsigned long val;
639 int err;
640
641 err = kstrtoul(buf, 10, &val);
642 if (err)
643 return err;
644
3248c3b7
GR
645 if (val > 255)
646 return -EINVAL;
647
09770b26 648 data->vrm = val;
1da177e4
LT
649 return count;
650}
651
d0ed69d5 652static DEVICE_ATTR_RW(vrm);
1da177e4 653
d0ed69d5
JL
654static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
655 char *buf)
1da177e4
LT
656{
657 struct lm85_data *data = lm85_update_device(dev);
68188ba7 658 return sprintf(buf, "%u\n", data->alarms);
1da177e4
LT
659}
660
d0ed69d5 661static DEVICE_ATTR_RO(alarms);
1da177e4 662
bf76e9d3
JD
663static 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
1da177e4
LT
689/* pwm */
690
b353a487
JD
691static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
692 char *buf)
1da177e4 693{
b353a487 694 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 695 struct lm85_data *data = lm85_update_device(dev);
1f44809a 696 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
1da177e4 697}
b353a487
JD
698
699static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
700 const char *buf, size_t count)
1da177e4 701{
b353a487 702 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
703 struct lm85_data *data = dev_get_drvdata(dev);
704 struct i2c_client *client = data->client;
09770b26
GR
705 unsigned long val;
706 int err;
707
708 err = kstrtoul(buf, 10, &val);
709 if (err)
710 return err;
1da177e4 711
9a61bf63 712 mutex_lock(&data->update_lock);
1da177e4
LT
713 data->pwm[nr] = PWM_TO_REG(val);
714 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
9a61bf63 715 mutex_unlock(&data->update_lock);
1da177e4
LT
716 return count;
717}
b353a487
JD
718
719static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
720 *attr, char *buf)
1da177e4 721{
b353a487 722 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 723 struct lm85_data *data = lm85_update_device(dev);
4b4df95d 724 int pwm_zone, enable;
1da177e4
LT
725
726 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
4b4df95d
JD
727 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);
1da177e4
LT
739}
740
455f791e
JD
741static 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;
746f6884
AL
745 struct lm85_data *data = dev_get_drvdata(dev);
746 struct i2c_client *client = data->client;
455f791e 747 u8 config;
09770b26
GR
748 unsigned long val;
749 int err;
750
751 err = kstrtoul(buf, 10, &val);
752 if (err)
753 return err;
455f791e
JD
754
755 switch (val) {
756 case 0:
757 config = 3;
758 break;
759 case 1:
760 config = 7;
761 break;
762 case 2:
09770b26
GR
763 /*
764 * Here we have to choose arbitrarily one of the 5 possible
765 * configurations; I go for the safest
766 */
455f791e
JD
767 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
34e7dc6c
JD
784static 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);
f6c61cff
JD
789 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);
34e7dc6c
JD
797}
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;
746f6884
AL
803 struct lm85_data *data = dev_get_drvdata(dev);
804 struct i2c_client *client = data->client;
09770b26
GR
805 unsigned long val;
806 int err;
807
808 err = kstrtoul(buf, 10, &val);
809 if (err)
810 return err;
34e7dc6c
JD
811
812 mutex_lock(&data->update_lock);
09770b26
GR
813 /*
814 * The ADT7468 has a special high-frequency PWM output mode,
f6c61cff 815 * where all PWM outputs are driven by a 22.5 kHz clock.
09770b26
GR
816 * This might confuse the user, but there's not much we can do.
817 */
f6c61cff
JD
818 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 */
0f3721c5
BG
822 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
823 FREQ_MAP_LEN, val);
f6c61cff
JD
824 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 }
34e7dc6c
JD
832 mutex_unlock(&data->update_lock);
833 return count;
834}
835
1da177e4 836#define show_pwm_reg(offset) \
b353a487
JD
837static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
838 show_pwm, set_pwm, offset - 1); \
455f791e 839static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
34e7dc6c
JD
840 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)
1da177e4
LT
843
844show_pwm_reg(1);
845show_pwm_reg(2);
846show_pwm_reg(3);
847
848/* Voltages */
849
b353a487
JD
850static ssize_t show_in(struct device *dev, struct device_attribute *attr,
851 char *buf)
1da177e4 852{
b353a487 853 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 854 struct lm85_data *data = lm85_update_device(dev);
1f44809a
JD
855 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
856 data->in_ext[nr]));
1da177e4 857}
b353a487 858
1f44809a 859static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
b353a487 860 char *buf)
1da177e4 861{
b353a487 862 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 863 struct lm85_data *data = lm85_update_device(dev);
1f44809a 864 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
1da177e4 865}
b353a487
JD
866
867static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
868 const char *buf, size_t count)
1da177e4 869{
b353a487 870 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
871 struct lm85_data *data = dev_get_drvdata(dev);
872 struct i2c_client *client = data->client;
09770b26
GR
873 long val;
874 int err;
875
876 err = kstrtol(buf, 10, &val);
877 if (err)
878 return err;
1da177e4 879
9a61bf63 880 mutex_lock(&data->update_lock);
1da177e4
LT
881 data->in_min[nr] = INS_TO_REG(nr, val);
882 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
9a61bf63 883 mutex_unlock(&data->update_lock);
1da177e4
LT
884 return count;
885}
b353a487
JD
886
887static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
888 char *buf)
1da177e4 889{
b353a487 890 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 891 struct lm85_data *data = lm85_update_device(dev);
1f44809a 892 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
1da177e4 893}
b353a487
JD
894
895static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
896 const char *buf, size_t count)
1da177e4 897{
b353a487 898 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
899 struct lm85_data *data = dev_get_drvdata(dev);
900 struct i2c_client *client = data->client;
09770b26
GR
901 long val;
902 int err;
903
904 err = kstrtol(buf, 10, &val);
905 if (err)
906 return err;
1da177e4 907
9a61bf63 908 mutex_lock(&data->update_lock);
1da177e4
LT
909 data->in_max[nr] = INS_TO_REG(nr, val);
910 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
9a61bf63 911 mutex_unlock(&data->update_lock);
1da177e4
LT
912 return count;
913}
b353a487 914
1da177e4 915#define show_in_reg(offset) \
b353a487
JD
916static 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)
1da177e4
LT
922
923show_in_reg(0);
924show_in_reg(1);
925show_in_reg(2);
926show_in_reg(3);
927show_in_reg(4);
6b9aad2d
JD
928show_in_reg(5);
929show_in_reg(6);
930show_in_reg(7);
1da177e4
LT
931
932/* Temps */
933
b353a487
JD
934static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
935 char *buf)
1da177e4 936{
b353a487 937 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 938 struct lm85_data *data = lm85_update_device(dev);
1f44809a
JD
939 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
940 data->temp_ext[nr]));
1da177e4 941}
b353a487
JD
942
943static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
944 char *buf)
1da177e4 945{
b353a487 946 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 947 struct lm85_data *data = lm85_update_device(dev);
1f44809a 948 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
1da177e4 949}
b353a487
JD
950
951static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
952 const char *buf, size_t count)
1da177e4 953{
b353a487 954 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
955 struct lm85_data *data = dev_get_drvdata(dev);
956 struct i2c_client *client = data->client;
09770b26
GR
957 long val;
958 int err;
959
960 err = kstrtol(buf, 10, &val);
961 if (err)
962 return err;
1da177e4 963
79b92f2b
DW
964 if (IS_ADT7468_OFF64(data))
965 val += 64;
966
9a61bf63 967 mutex_lock(&data->update_lock);
1da177e4
LT
968 data->temp_min[nr] = TEMP_TO_REG(val);
969 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
9a61bf63 970 mutex_unlock(&data->update_lock);
1da177e4
LT
971 return count;
972}
b353a487
JD
973
974static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
975 char *buf)
1da177e4 976{
b353a487 977 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 978 struct lm85_data *data = lm85_update_device(dev);
1f44809a 979 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1da177e4 980}
b353a487
JD
981
982static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
983 const char *buf, size_t count)
1da177e4 984{
b353a487 985 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
986 struct lm85_data *data = dev_get_drvdata(dev);
987 struct i2c_client *client = data->client;
09770b26
GR
988 long val;
989 int err;
990
991 err = kstrtol(buf, 10, &val);
992 if (err)
993 return err;
1da177e4 994
79b92f2b
DW
995 if (IS_ADT7468_OFF64(data))
996 val += 64;
997
9a61bf63 998 mutex_lock(&data->update_lock);
1da177e4
LT
999 data->temp_max[nr] = TEMP_TO_REG(val);
1000 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
9a61bf63 1001 mutex_unlock(&data->update_lock);
1da177e4
LT
1002 return count;
1003}
b353a487 1004
1da177e4 1005#define show_temp_reg(offset) \
b353a487
JD
1006static 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);
1da177e4
LT
1012
1013show_temp_reg(1);
1014show_temp_reg(2);
1015show_temp_reg(3);
1016
1017
1018/* Automatic PWM control */
1019
b353a487
JD
1020static ssize_t show_pwm_auto_channels(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1da177e4 1022{
b353a487 1023 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1024 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1025 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
1da177e4 1026}
b353a487
JD
1027
1028static ssize_t set_pwm_auto_channels(struct device *dev,
1029 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1030{
b353a487 1031 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1032 struct lm85_data *data = dev_get_drvdata(dev);
1033 struct i2c_client *client = data->client;
09770b26
GR
1034 long val;
1035 int err;
1036
1037 err = kstrtol(buf, 10, &val);
1038 if (err)
1039 return err;
1da177e4 1040
9a61bf63 1041 mutex_lock(&data->update_lock);
1da177e4 1042 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
1f44809a 1043 | ZONE_TO_REG(val);
1da177e4
LT
1044 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1045 data->autofan[nr].config);
9a61bf63 1046 mutex_unlock(&data->update_lock);
1da177e4
LT
1047 return count;
1048}
b353a487
JD
1049
1050static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1051 struct device_attribute *attr, char *buf)
1da177e4 1052{
b353a487 1053 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1054 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1055 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
1da177e4 1056}
b353a487
JD
1057
1058static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1059 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1060{
b353a487 1061 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1062 struct lm85_data *data = dev_get_drvdata(dev);
1063 struct i2c_client *client = data->client;
09770b26
GR
1064 unsigned long val;
1065 int err;
1066
1067 err = kstrtoul(buf, 10, &val);
1068 if (err)
1069 return err;
1da177e4 1070
9a61bf63 1071 mutex_lock(&data->update_lock);
1da177e4
LT
1072 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);
9a61bf63 1075 mutex_unlock(&data->update_lock);
1da177e4
LT
1076 return count;
1077}
b353a487
JD
1078
1079static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1080 struct device_attribute *attr, char *buf)
1da177e4 1081{
b353a487 1082 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1083 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1084 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
1da177e4 1085}
b353a487
JD
1086
1087static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1088 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1089{
b353a487 1090 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1091 struct lm85_data *data = dev_get_drvdata(dev);
1092 struct i2c_client *client = data->client;
7133e56f 1093 u8 tmp;
09770b26
GR
1094 long val;
1095 int err;
1096
1097 err = kstrtol(buf, 10, &val);
1098 if (err)
1099 return err;
1da177e4 1100
9a61bf63 1101 mutex_lock(&data->update_lock);
1da177e4 1102 data->autofan[nr].min_off = val;
7133e56f
JD
1103 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);
9a61bf63 1108 mutex_unlock(&data->update_lock);
1da177e4
LT
1109 return count;
1110}
b353a487 1111
1da177e4 1112#define pwm_auto(offset) \
b353a487
JD
1113static 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, \
34e7dc6c 1121 set_pwm_auto_pwm_minctl, offset - 1)
b353a487 1122
1da177e4
LT
1123pwm_auto(1);
1124pwm_auto(2);
1125pwm_auto(3);
1126
1127/* Temperature settings for automatic PWM control */
1128
b353a487
JD
1129static ssize_t show_temp_auto_temp_off(struct device *dev,
1130 struct device_attribute *attr, char *buf)
1da177e4 1131{
b353a487 1132 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1133 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1134 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
1da177e4
LT
1135 HYST_FROM_REG(data->zone[nr].hyst));
1136}
b353a487
JD
1137
1138static ssize_t set_temp_auto_temp_off(struct device *dev,
1139 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1140{
b353a487 1141 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1142 struct lm85_data *data = dev_get_drvdata(dev);
1143 struct i2c_client *client = data->client;
1da177e4 1144 int min;
09770b26
GR
1145 long val;
1146 int err;
1147
1148 err = kstrtol(buf, 10, &val);
1149 if (err)
1150 return err;
1da177e4 1151
9a61bf63 1152 mutex_lock(&data->update_lock);
1da177e4 1153 min = TEMP_FROM_REG(data->zone[nr].limit);
1da177e4 1154 data->zone[nr].hyst = HYST_TO_REG(min - val);
1f44809a 1155 if (nr == 0 || nr == 1) {
1da177e4
LT
1156 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1157 (data->zone[0].hyst << 4)
1f44809a 1158 | data->zone[1].hyst);
1da177e4
LT
1159 } else {
1160 lm85_write_value(client, LM85_REG_AFAN_HYST2,
1f44809a 1161 (data->zone[2].hyst << 4));
1da177e4 1162 }
9a61bf63 1163 mutex_unlock(&data->update_lock);
1da177e4
LT
1164 return count;
1165}
b353a487
JD
1166
1167static ssize_t show_temp_auto_temp_min(struct device *dev,
1168 struct device_attribute *attr, char *buf)
1da177e4 1169{
b353a487 1170 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1171 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1172 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
1da177e4 1173}
b353a487
JD
1174
1175static ssize_t set_temp_auto_temp_min(struct device *dev,
1176 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1177{
b353a487 1178 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1179 struct lm85_data *data = dev_get_drvdata(dev);
1180 struct i2c_client *client = data->client;
09770b26
GR
1181 long val;
1182 int err;
1183
1184 err = kstrtol(buf, 10, &val);
1185 if (err)
1186 return err;
1da177e4 1187
9a61bf63 1188 mutex_lock(&data->update_lock);
1da177e4
LT
1189 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)
34e7dc6c 1199 | (data->pwm_freq[nr] & 0x07));
1da177e4 1200
9a61bf63 1201 mutex_unlock(&data->update_lock);
1da177e4
LT
1202 return count;
1203}
b353a487
JD
1204
1205static ssize_t show_temp_auto_temp_max(struct device *dev,
1206 struct device_attribute *attr, char *buf)
1da177e4 1207{
b353a487 1208 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1209 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1210 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1da177e4
LT
1211 RANGE_FROM_REG(data->zone[nr].range));
1212}
b353a487
JD
1213
1214static ssize_t set_temp_auto_temp_max(struct device *dev,
1215 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1216{
b353a487 1217 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1218 struct lm85_data *data = dev_get_drvdata(dev);
1219 struct i2c_client *client = data->client;
1da177e4 1220 int min;
09770b26
GR
1221 long val;
1222 int err;
1223
1224 err = kstrtol(buf, 10, &val);
1225 if (err)
1226 return err;
1da177e4 1227
9a61bf63 1228 mutex_lock(&data->update_lock);
1da177e4
LT
1229 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)
34e7dc6c 1235 | (data->pwm_freq[nr] & 0x07));
9a61bf63 1236 mutex_unlock(&data->update_lock);
1da177e4
LT
1237 return count;
1238}
b353a487
JD
1239
1240static ssize_t show_temp_auto_temp_crit(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1da177e4 1242{
b353a487 1243 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1244 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1245 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1da177e4 1246}
b353a487
JD
1247
1248static ssize_t set_temp_auto_temp_crit(struct device *dev,
1f44809a 1249 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1250{
b353a487 1251 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1252 struct lm85_data *data = dev_get_drvdata(dev);
1253 struct i2c_client *client = data->client;
09770b26
GR
1254 long val;
1255 int err;
1256
1257 err = kstrtol(buf, 10, &val);
1258 if (err)
1259 return err;
1da177e4 1260
9a61bf63 1261 mutex_lock(&data->update_lock);
1da177e4
LT
1262 data->zone[nr].critical = TEMP_TO_REG(val);
1263 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1264 data->zone[nr].critical);
9a61bf63 1265 mutex_unlock(&data->update_lock);
1da177e4
LT
1266 return count;
1267}
b353a487 1268
1da177e4 1269#define temp_auto(offset) \
b353a487
JD
1270static 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
1da177e4
LT
1283temp_auto(1);
1284temp_auto(2);
1285temp_auto(3);
1286
0501a381 1287static struct attribute *lm85_attributes[] = {
b353a487
JD
1288 &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,
bf76e9d3
JD
1296 &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,
b353a487
JD
1300
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,
34e7dc6c
JD
1307 &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,
b353a487
JD
1310
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,
bf76e9d3
JD
1323 &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,
b353a487
JD
1327
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,
bf76e9d3
JD
1337 &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,
b353a487
JD
1342
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,
b353a487 1349
b353a487
JD
1350 &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
0501a381
MH
1360 &dev_attr_vrm.attr,
1361 &dev_attr_cpu0_vid.attr,
1362 &dev_attr_alarms.attr,
0501a381
MH
1363 NULL
1364};
1365
1366static const struct attribute_group lm85_group = {
1367 .attrs = lm85_attributes,
1368};
1369
06923f84
GR
1370static 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,
5f441e22 1374 NULL
06923f84
GR
1375};
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,
5f441e22 1385 NULL
06923f84
GR
1386};
1387
1388static const struct attribute_group lm85_group_temp_off = {
1389 .attrs = lm85_attributes_temp_off,
1390};
1391
6b9aad2d 1392static struct attribute *lm85_attributes_in4[] = {
b353a487
JD
1393 &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,
bf76e9d3 1396 &sensor_dev_attr_in4_alarm.dev_attr.attr,
0501a381
MH
1397 NULL
1398};
1399
6b9aad2d
JD
1400static 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,
bf76e9d3
JD
1414 &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,
6b9aad2d
JD
1417 NULL
1418};
1419
1420static const struct attribute_group lm85_group_in567 = {
1421 .attrs = lm85_attributes_in567,
0501a381
MH
1422};
1423
5f447594
JD
1424static 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
5cfaf338
JD
1442static 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
67712d01 1462/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 1463static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1da177e4 1464{
67712d01
JD
1465 struct i2c_adapter *adapter = client->adapter;
1466 int address = client->addr;
590e8534 1467 const char *type_name = NULL;
d42a2eb5 1468 int company, verstep;
1da177e4 1469
e89e22b2 1470 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1da177e4 1471 /* We need to be able to do byte I/O */
67712d01 1472 return -ENODEV;
1f44809a 1473 }
1da177e4 1474
d42a2eb5
JD
1475 /* Determine the chip type */
1476 company = lm85_read_value(client, LM85_REG_COMPANY);
1477 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1478
b55f3757
GR
1479 dev_dbg(&adapter->dev,
1480 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
d42a2eb5
JD
1481 address, company, verstep);
1482
d42a2eb5
JD
1483 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;
69fc1feb 1498 }
590e8534 1499 type_name = "lm85";
d42a2eb5
JD
1500 break;
1501 }
1502 } 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;
1da177e4 1515 }
d42a2eb5
JD
1516 } 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;
f065a93e
JB
1526 case LM85_VERSTEP_EMC6D103_A0:
1527 case LM85_VERSTEP_EMC6D103_A1:
1528 type_name = "emc6d103";
1529 break;
f065a93e
JB
1530 case LM85_VERSTEP_EMC6D103S:
1531 type_name = "emc6d103s";
1532 break;
d42a2eb5 1533 }
1da177e4
LT
1534 }
1535
590e8534
JD
1536 if (!type_name)
1537 return -ENODEV;
1538
67712d01
JD
1539 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1540
1541 return 0;
1542}
1da177e4 1543
746f6884 1544static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
67712d01 1545{
746f6884
AL
1546 struct device *dev = &client->dev;
1547 struct device *hwmon_dev;
67712d01 1548 struct lm85_data *data;
746f6884 1549 int idx = 0;
67712d01 1550
746f6884 1551 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
67712d01
JD
1552 if (!data)
1553 return -ENOMEM;
1554
746f6884 1555 data->client = client;
00c0f9d3
JMC
1556 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;
9a61bf63 1560 mutex_init(&data->update_lock);
1da177e4 1561
67712d01
JD
1562 /* Fill in the chip specific driver values */
1563 switch (data->type) {
1564 case adm1027:
1565 case adt7463:
fa7a5797 1566 case adt7468:
67712d01
JD
1567 case emc6d100:
1568 case emc6d102:
f065a93e 1569 case emc6d103:
06923f84 1570 case emc6d103s:
67712d01
JD
1571 data->freq_map = adm1027_freq_map;
1572 break;
1573 default:
1574 data->freq_map = lm85_freq_map;
1575 }
1da177e4
LT
1576
1577 /* Set the VRM version */
303760b4 1578 data->vrm = vid_which_vrm();
1da177e4
LT
1579
1580 /* Initialize the LM85 chip */
e89e22b2 1581 lm85_init_client(client);
1da177e4 1582
746f6884
AL
1583 /* sysfs hooks */
1584 data->groups[idx++] = &lm85_group;
1da177e4 1585
06923f84
GR
1586 /* minctl and temp_off exist on all chips except emc6d103s */
1587 if (data->type != emc6d103s) {
746f6884
AL
1588 data->groups[idx++] = &lm85_group_minctl;
1589 data->groups[idx++] = &lm85_group_temp_off;
06923f84
GR
1590 }
1591
09770b26
GR
1592 /*
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 */
de248805
GR
1596 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
746f6884
AL
1602 if (!data->has_vid5)
1603 data->groups[idx++] = &lm85_group_in4;
6b9aad2d
JD
1604
1605 /* The EMC6D100 has 3 additional voltage inputs */
746f6884
AL
1606 if (data->type == emc6d100)
1607 data->groups[idx++] = &lm85_group_in567;
1da177e4 1608
746f6884
AL
1609 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1610 data, data->groups);
1611 return PTR_ERR_OR_ZERO(hwmon_dev);
1da177e4
LT
1612}
1613
6fd5dd58
AL
1614static 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);
1da177e4 1629
00c0f9d3
JMC
1630static 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
6fd5dd58
AL
1679static struct i2c_driver lm85_driver = {
1680 .class = I2C_CLASS_HWMON,
1681 .driver = {
1682 .name = "lm85",
00c0f9d3 1683 .of_match_table = of_match_ptr(lm85_of_match),
6fd5dd58
AL
1684 },
1685 .probe = lm85_probe,
6fd5dd58
AL
1686 .id_table = lm85_id,
1687 .detect = lm85_detect,
1688 .address_list = normal_i2c,
1689};
1da177e4 1690
f0967eea 1691module_i2c_driver(lm85_driver);
1da177e4 1692
1da177e4 1693MODULE_LICENSE("GPL");
1f44809a
JD
1694MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1695 "Margit Schubert-While <margitsw@t-online.de>, "
e89e22b2 1696 "Justin Thiessen <jthiessen@penguincomputing.com>");
1da177e4 1697MODULE_DESCRIPTION("LM85-B, LM85-C driver");