Merge tag 'kbuild-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[linux-2.6-block.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 {
11650cf0 44 lm85, lm96000,
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 */
57bc3019 201static const int lm85_freq_map[] = { /* 1 Hz */
8a0795d9
JD
202 10, 15, 23, 30, 38, 47, 61, 94
203};
57bc3019 204
e9b95485
JG
205static const int lm96000_freq_map[] = { /* 1 Hz */
206 10, 15, 23, 30, 38, 47, 61, 94,
207 22500, 24000, 25700, 25700, 27700, 27700, 30000, 30000
208};
209
57bc3019 210static const int adm1027_freq_map[] = { /* 1 Hz */
8a0795d9 211 11, 15, 22, 29, 35, 44, 59, 88
1f44809a
JD
212};
213
0f3721c5
BG
214static int FREQ_TO_REG(const int *map,
215 unsigned int map_size, unsigned long freq)
1da177e4 216{
0f3721c5 217 return find_closest(freq, map, map_size);
1da177e4 218}
8a0795d9 219
57bc3019 220static int FREQ_FROM_REG(const int *map, unsigned int map_size, u8 reg)
8a0795d9 221{
57bc3019 222 return map[reg % map_size];
8a0795d9 223}
1da177e4 224
09770b26
GR
225/*
226 * Since we can't use strings, I'm abusing these numbers
1da177e4
LT
227 * to stand in for the following meanings:
228 * 1 -- PWM responds to Zone 1
229 * 2 -- PWM responds to Zone 2
230 * 3 -- PWM responds to Zone 3
231 * 23 -- PWM responds to the higher temp of Zone 2 or 3
232 * 123 -- PWM responds to highest of Zone 1, 2, or 3
233 * 0 -- PWM is always at 0% (ie, off)
234 * -1 -- PWM is always at 100%
235 * -2 -- PWM responds to manual control
236 */
237
e89e22b2
JD
238static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
239#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
1da177e4 240
1f44809a 241static int ZONE_TO_REG(int zone)
1da177e4
LT
242{
243 int i;
244
1f44809a
JD
245 for (i = 0; i <= 7; ++i)
246 if (zone == lm85_zone_map[i])
247 break;
248 if (i > 7) /* Not found. */
1da177e4 249 i = 3; /* Always 100% */
e89e22b2 250 return i << 5;
1da177e4
LT
251}
252
2a844c14 253#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
1f44809a 254#define HYST_FROM_REG(val) ((val) * 1000)
1da177e4 255
09770b26
GR
256/*
257 * Chip sampling rates
1da177e4
LT
258 *
259 * Some sensors are not updated more frequently than once per second
260 * so it doesn't make sense to read them more often than that.
261 * We cache the results and return the saved data if the driver
262 * is called again before a second has elapsed.
263 *
264 * Also, there is significant configuration data for this chip
265 * given the automatic PWM fan control that is possible. There
266 * are about 47 bytes of config data to only 22 bytes of actual
267 * readings. So, we keep the config data up to date in the cache
268 * when it is written and only sample it once every 1 *minute*
269 */
270#define LM85_DATA_INTERVAL (HZ + HZ / 2)
271#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
272
09770b26
GR
273/*
274 * LM85 can automatically adjust fan speeds based on temperature
1da177e4
LT
275 * This structure encapsulates an entire Zone config. There are
276 * three zones (one for each temperature input) on the lm85
277 */
278struct lm85_zone {
279 s8 limit; /* Low temp limit */
280 u8 hyst; /* Low limit hysteresis. (0-15) */
281 u8 range; /* Temp range, encoded */
282 s8 critical; /* "All fans ON" temp limit */
09770b26
GR
283 u8 max_desired; /*
284 * Actual "max" temperature specified. Preserved
1da177e4
LT
285 * to prevent "drift" as other autofan control
286 * values change.
287 */
288};
289
290struct lm85_autofan {
291 u8 config; /* Register value */
1da177e4
LT
292 u8 min_pwm; /* Minimum PWM value, encoded */
293 u8 min_off; /* Min PWM or OFF below "limit", flag */
294};
295
09770b26
GR
296/*
297 * For each registered chip, we need to keep some data in memory.
298 * The structure is dynamically allocated.
299 */
1da177e4 300struct lm85_data {
746f6884
AL
301 struct i2c_client *client;
302 const struct attribute_group *groups[6];
8a0795d9 303 const int *freq_map;
57bc3019
JG
304 unsigned int freq_map_size;
305
1da177e4
LT
306 enum chips type;
307
de248805
GR
308 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
309
9a61bf63 310 struct mutex update_lock;
1da177e4
LT
311 int valid; /* !=0 if following fields are valid */
312 unsigned long last_reading; /* In jiffies */
313 unsigned long last_config; /* In jiffies */
314
315 u8 in[8]; /* Register value */
316 u8 in_max[8]; /* Register value */
317 u8 in_min[8]; /* Register value */
318 s8 temp[3]; /* Register value */
319 s8 temp_min[3]; /* Register value */
320 s8 temp_max[3]; /* Register value */
1da177e4
LT
321 u16 fan[4]; /* Register value */
322 u16 fan_min[4]; /* Register value */
323 u8 pwm[3]; /* Register value */
34e7dc6c 324 u8 pwm_freq[3]; /* Register encoding */
1da177e4
LT
325 u8 temp_ext[3]; /* Decoded values */
326 u8 in_ext[8]; /* Decoded values */
1da177e4
LT
327 u8 vid; /* Register value */
328 u8 vrm; /* VRM version */
1da177e4 329 u32 alarms; /* Register encoding, combined */
79b92f2b 330 u8 cfg5; /* Config Register 5 on ADT7468 */
1da177e4
LT
331 struct lm85_autofan autofan[3];
332 struct lm85_zone zone[3];
333};
334
6fd5dd58
AL
335static int lm85_read_value(struct i2c_client *client, u8 reg)
336{
337 int res;
1da177e4 338
6fd5dd58
AL
339 /* What size location is it? */
340 switch (reg) {
341 case LM85_REG_FAN(0): /* Read WORD data */
342 case LM85_REG_FAN(1):
343 case LM85_REG_FAN(2):
344 case LM85_REG_FAN(3):
345 case LM85_REG_FAN_MIN(0):
346 case LM85_REG_FAN_MIN(1):
347 case LM85_REG_FAN_MIN(2):
348 case LM85_REG_FAN_MIN(3):
349 case LM85_REG_ALARM1: /* Read both bytes at once */
350 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
351 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
352 break;
353 default: /* Read BYTE data */
354 res = i2c_smbus_read_byte_data(client, reg);
355 break;
356 }
1da177e4 357
6fd5dd58
AL
358 return res;
359}
1da177e4 360
6fd5dd58
AL
361static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
362{
363 switch (reg) {
364 case LM85_REG_FAN(0): /* Write WORD data */
365 case LM85_REG_FAN(1):
366 case LM85_REG_FAN(2):
367 case LM85_REG_FAN(3):
368 case LM85_REG_FAN_MIN(0):
369 case LM85_REG_FAN_MIN(1):
370 case LM85_REG_FAN_MIN(2):
371 case LM85_REG_FAN_MIN(3):
372 /* NOTE: ALARM is read only, so not included here */
373 i2c_smbus_write_byte_data(client, reg, value & 0xff);
374 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
375 break;
376 default: /* Write BYTE data */
377 i2c_smbus_write_byte_data(client, reg, value);
378 break;
379 }
380}
67712d01 381
6fd5dd58
AL
382static struct lm85_data *lm85_update_device(struct device *dev)
383{
746f6884
AL
384 struct lm85_data *data = dev_get_drvdata(dev);
385 struct i2c_client *client = data->client;
6fd5dd58
AL
386 int i;
387
388 mutex_lock(&data->update_lock);
389
390 if (!data->valid ||
391 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
392 /* Things that change quickly */
393 dev_dbg(&client->dev, "Reading sensor values\n");
394
395 /*
396 * Have to read extended bits first to "freeze" the
397 * more significant bits that are read later.
398 * There are 2 additional resolution bits per channel and we
399 * have room for 4, so we shift them to the left.
400 */
401 if (data->type == adm1027 || data->type == adt7463 ||
402 data->type == adt7468) {
403 int ext1 = lm85_read_value(client,
404 ADM1027_REG_EXTEND_ADC1);
405 int ext2 = lm85_read_value(client,
406 ADM1027_REG_EXTEND_ADC2);
407 int val = (ext1 << 8) + ext2;
408
409 for (i = 0; i <= 4; i++)
410 data->in_ext[i] =
411 ((val >> (i * 2)) & 0x03) << 2;
412
413 for (i = 0; i <= 2; i++)
414 data->temp_ext[i] =
415 (val >> ((i + 4) * 2)) & 0x0c;
416 }
417
418 data->vid = lm85_read_value(client, LM85_REG_VID);
419
420 for (i = 0; i <= 3; ++i) {
421 data->in[i] =
422 lm85_read_value(client, LM85_REG_IN(i));
423 data->fan[i] =
424 lm85_read_value(client, LM85_REG_FAN(i));
425 }
426
427 if (!data->has_vid5)
428 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
429
430 if (data->type == adt7468)
431 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
432
433 for (i = 0; i <= 2; ++i) {
434 data->temp[i] =
435 lm85_read_value(client, LM85_REG_TEMP(i));
436 data->pwm[i] =
437 lm85_read_value(client, LM85_REG_PWM(i));
1da177e4 438
6fd5dd58
AL
439 if (IS_ADT7468_OFF64(data))
440 data->temp[i] -= 64;
441 }
442
443 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
444
445 if (data->type == emc6d100) {
446 /* Three more voltage sensors */
447 for (i = 5; i <= 7; ++i) {
448 data->in[i] = lm85_read_value(client,
449 EMC6D100_REG_IN(i));
450 }
451 /* More alarm bits */
452 data->alarms |= lm85_read_value(client,
453 EMC6D100_REG_ALARM3) << 16;
454 } else if (data->type == emc6d102 || data->type == emc6d103 ||
455 data->type == emc6d103s) {
456 /*
457 * Have to read LSB bits after the MSB ones because
458 * the reading of the MSB bits has frozen the
459 * LSBs (backward from the ADM1027).
460 */
461 int ext1 = lm85_read_value(client,
462 EMC6D102_REG_EXTEND_ADC1);
463 int ext2 = lm85_read_value(client,
464 EMC6D102_REG_EXTEND_ADC2);
465 int ext3 = lm85_read_value(client,
466 EMC6D102_REG_EXTEND_ADC3);
467 int ext4 = lm85_read_value(client,
468 EMC6D102_REG_EXTEND_ADC4);
469 data->in_ext[0] = ext3 & 0x0f;
470 data->in_ext[1] = ext4 & 0x0f;
471 data->in_ext[2] = ext4 >> 4;
472 data->in_ext[3] = ext3 >> 4;
473 data->in_ext[4] = ext2 >> 4;
474
475 data->temp_ext[0] = ext1 & 0x0f;
476 data->temp_ext[1] = ext2 & 0x0f;
477 data->temp_ext[2] = ext1 >> 4;
478 }
479
480 data->last_reading = jiffies;
481 } /* last_reading */
482
483 if (!data->valid ||
484 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
485 /* Things that don't change often */
486 dev_dbg(&client->dev, "Reading config values\n");
487
488 for (i = 0; i <= 3; ++i) {
489 data->in_min[i] =
490 lm85_read_value(client, LM85_REG_IN_MIN(i));
491 data->in_max[i] =
492 lm85_read_value(client, LM85_REG_IN_MAX(i));
493 data->fan_min[i] =
494 lm85_read_value(client, LM85_REG_FAN_MIN(i));
495 }
496
497 if (!data->has_vid5) {
498 data->in_min[4] = lm85_read_value(client,
499 LM85_REG_IN_MIN(4));
500 data->in_max[4] = lm85_read_value(client,
501 LM85_REG_IN_MAX(4));
502 }
503
504 if (data->type == emc6d100) {
505 for (i = 5; i <= 7; ++i) {
506 data->in_min[i] = lm85_read_value(client,
507 EMC6D100_REG_IN_MIN(i));
508 data->in_max[i] = lm85_read_value(client,
509 EMC6D100_REG_IN_MAX(i));
510 }
511 }
512
513 for (i = 0; i <= 2; ++i) {
514 int val;
515
516 data->temp_min[i] =
517 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
518 data->temp_max[i] =
519 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
520
521 data->autofan[i].config =
522 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
523 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
57bc3019 524 data->pwm_freq[i] = val % data->freq_map_size;
6fd5dd58
AL
525 data->zone[i].range = val >> 4;
526 data->autofan[i].min_pwm =
527 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
528 data->zone[i].limit =
529 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
530 data->zone[i].critical =
531 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
532
533 if (IS_ADT7468_OFF64(data)) {
534 data->temp_min[i] -= 64;
535 data->temp_max[i] -= 64;
536 data->zone[i].limit -= 64;
537 data->zone[i].critical -= 64;
538 }
539 }
540
541 if (data->type != emc6d103s) {
542 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
543 data->autofan[0].min_off = (i & 0x20) != 0;
544 data->autofan[1].min_off = (i & 0x40) != 0;
545 data->autofan[2].min_off = (i & 0x80) != 0;
546
547 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
548 data->zone[0].hyst = i >> 4;
549 data->zone[1].hyst = i & 0x0f;
550
551 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
552 data->zone[2].hyst = i >> 4;
553 }
554
555 data->last_config = jiffies;
556 } /* last_config */
557
558 data->valid = 1;
559
560 mutex_unlock(&data->update_lock);
561
562 return data;
563}
1da177e4
LT
564
565/* 4 Fans */
b353a487
JD
566static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
567 char *buf)
1da177e4 568{
b353a487 569 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 570 struct lm85_data *data = lm85_update_device(dev);
1f44809a 571 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
1da177e4 572}
b353a487
JD
573
574static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
575 char *buf)
1da177e4 576{
b353a487 577 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 578 struct lm85_data *data = lm85_update_device(dev);
1f44809a 579 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
1da177e4 580}
b353a487
JD
581
582static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
1da177e4 584{
b353a487 585 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
586 struct lm85_data *data = dev_get_drvdata(dev);
587 struct i2c_client *client = data->client;
09770b26
GR
588 unsigned long val;
589 int err;
590
591 err = kstrtoul(buf, 10, &val);
592 if (err)
593 return err;
1da177e4 594
9a61bf63 595 mutex_lock(&data->update_lock);
1da177e4
LT
596 data->fan_min[nr] = FAN_TO_REG(val);
597 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 598 mutex_unlock(&data->update_lock);
1da177e4
LT
599 return count;
600}
601
602#define show_fan_offset(offset) \
b353a487
JD
603static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
604 show_fan, NULL, offset - 1); \
605static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
606 show_fan_min, set_fan_min, offset - 1)
1da177e4
LT
607
608show_fan_offset(1);
609show_fan_offset(2);
610show_fan_offset(3);
611show_fan_offset(4);
612
613/* vid, vrm, alarms */
614
d0ed69d5
JL
615static ssize_t cpu0_vid_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
1da177e4
LT
617{
618 struct lm85_data *data = lm85_update_device(dev);
9c516ef4
JD
619 int vid;
620
de248805 621 if (data->has_vid5) {
9c516ef4
JD
622 /* 6-pin VID (VRM 10) */
623 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
624 } else {
625 /* 5-pin VID (VRM 9) */
626 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
627 }
628
629 return sprintf(buf, "%d\n", vid);
1da177e4
LT
630}
631
d0ed69d5 632static DEVICE_ATTR_RO(cpu0_vid);
1da177e4 633
d0ed69d5
JL
634static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
635 char *buf)
1da177e4 636{
90d6619a 637 struct lm85_data *data = dev_get_drvdata(dev);
1da177e4
LT
638 return sprintf(buf, "%ld\n", (long) data->vrm);
639}
640
d0ed69d5
JL
641static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
642 const char *buf, size_t count)
1da177e4 643{
8f74efe8 644 struct lm85_data *data = dev_get_drvdata(dev);
09770b26
GR
645 unsigned long val;
646 int err;
647
648 err = kstrtoul(buf, 10, &val);
649 if (err)
650 return err;
651
3248c3b7
GR
652 if (val > 255)
653 return -EINVAL;
654
09770b26 655 data->vrm = val;
1da177e4
LT
656 return count;
657}
658
d0ed69d5 659static DEVICE_ATTR_RW(vrm);
1da177e4 660
d0ed69d5
JL
661static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
662 char *buf)
1da177e4
LT
663{
664 struct lm85_data *data = lm85_update_device(dev);
68188ba7 665 return sprintf(buf, "%u\n", data->alarms);
1da177e4
LT
666}
667
d0ed69d5 668static DEVICE_ATTR_RO(alarms);
1da177e4 669
bf76e9d3
JD
670static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
671 char *buf)
672{
673 int nr = to_sensor_dev_attr(attr)->index;
674 struct lm85_data *data = lm85_update_device(dev);
675 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
676}
677
678static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
679static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
680static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
681static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
682static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
683static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
684static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
685static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
686static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
687static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
688static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
689static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
690static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
691static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
692static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
693static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
694static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
695
1da177e4
LT
696/* pwm */
697
b353a487
JD
698static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
699 char *buf)
1da177e4 700{
b353a487 701 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 702 struct lm85_data *data = lm85_update_device(dev);
1f44809a 703 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
1da177e4 704}
b353a487
JD
705
706static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
707 const char *buf, size_t count)
1da177e4 708{
b353a487 709 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
710 struct lm85_data *data = dev_get_drvdata(dev);
711 struct i2c_client *client = data->client;
09770b26
GR
712 unsigned long val;
713 int err;
714
715 err = kstrtoul(buf, 10, &val);
716 if (err)
717 return err;
1da177e4 718
9a61bf63 719 mutex_lock(&data->update_lock);
1da177e4
LT
720 data->pwm[nr] = PWM_TO_REG(val);
721 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
9a61bf63 722 mutex_unlock(&data->update_lock);
1da177e4
LT
723 return count;
724}
b353a487
JD
725
726static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
727 *attr, char *buf)
1da177e4 728{
b353a487 729 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 730 struct lm85_data *data = lm85_update_device(dev);
4b4df95d 731 int pwm_zone, enable;
1da177e4
LT
732
733 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
4b4df95d
JD
734 switch (pwm_zone) {
735 case -1: /* PWM is always at 100% */
736 enable = 0;
737 break;
738 case 0: /* PWM is always at 0% */
739 case -2: /* PWM responds to manual control */
740 enable = 1;
741 break;
742 default: /* PWM in automatic mode */
743 enable = 2;
744 }
745 return sprintf(buf, "%d\n", enable);
1da177e4
LT
746}
747
455f791e
JD
748static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
749 *attr, const char *buf, size_t count)
750{
751 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
752 struct lm85_data *data = dev_get_drvdata(dev);
753 struct i2c_client *client = data->client;
455f791e 754 u8 config;
09770b26
GR
755 unsigned long val;
756 int err;
757
758 err = kstrtoul(buf, 10, &val);
759 if (err)
760 return err;
455f791e
JD
761
762 switch (val) {
763 case 0:
764 config = 3;
765 break;
766 case 1:
767 config = 7;
768 break;
769 case 2:
09770b26
GR
770 /*
771 * Here we have to choose arbitrarily one of the 5 possible
772 * configurations; I go for the safest
773 */
455f791e
JD
774 config = 6;
775 break;
776 default:
777 return -EINVAL;
778 }
779
780 mutex_lock(&data->update_lock);
781 data->autofan[nr].config = lm85_read_value(client,
782 LM85_REG_AFAN_CONFIG(nr));
783 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
784 | (config << 5);
785 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
786 data->autofan[nr].config);
787 mutex_unlock(&data->update_lock);
788 return count;
789}
790
34e7dc6c
JD
791static ssize_t show_pwm_freq(struct device *dev,
792 struct device_attribute *attr, char *buf)
793{
794 int nr = to_sensor_dev_attr(attr)->index;
795 struct lm85_data *data = lm85_update_device(dev);
f6c61cff
JD
796 int freq;
797
798 if (IS_ADT7468_HFPWM(data))
799 freq = 22500;
800 else
57bc3019
JG
801 freq = FREQ_FROM_REG(data->freq_map, data->freq_map_size,
802 data->pwm_freq[nr]);
f6c61cff
JD
803
804 return sprintf(buf, "%d\n", freq);
34e7dc6c
JD
805}
806
807static ssize_t set_pwm_freq(struct device *dev,
808 struct device_attribute *attr, const char *buf, size_t count)
809{
810 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
811 struct lm85_data *data = dev_get_drvdata(dev);
812 struct i2c_client *client = data->client;
09770b26
GR
813 unsigned long val;
814 int err;
815
816 err = kstrtoul(buf, 10, &val);
817 if (err)
818 return err;
34e7dc6c
JD
819
820 mutex_lock(&data->update_lock);
09770b26
GR
821 /*
822 * The ADT7468 has a special high-frequency PWM output mode,
f6c61cff 823 * where all PWM outputs are driven by a 22.5 kHz clock.
09770b26
GR
824 * This might confuse the user, but there's not much we can do.
825 */
f6c61cff
JD
826 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
827 data->cfg5 &= ~ADT7468_HFPWM;
828 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
829 } else { /* Low freq. mode */
0f3721c5 830 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
57bc3019 831 data->freq_map_size, val);
f6c61cff
JD
832 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
833 (data->zone[nr].range << 4)
834 | data->pwm_freq[nr]);
835 if (data->type == adt7468) {
836 data->cfg5 |= ADT7468_HFPWM;
837 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
838 }
839 }
34e7dc6c
JD
840 mutex_unlock(&data->update_lock);
841 return count;
842}
843
1da177e4 844#define show_pwm_reg(offset) \
b353a487
JD
845static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
846 show_pwm, set_pwm, offset - 1); \
455f791e 847static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
34e7dc6c
JD
848 show_pwm_enable, set_pwm_enable, offset - 1); \
849static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
850 show_pwm_freq, set_pwm_freq, offset - 1)
1da177e4
LT
851
852show_pwm_reg(1);
853show_pwm_reg(2);
854show_pwm_reg(3);
855
856/* Voltages */
857
b353a487
JD
858static ssize_t show_in(struct device *dev, struct device_attribute *attr,
859 char *buf)
1da177e4 860{
b353a487 861 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 862 struct lm85_data *data = lm85_update_device(dev);
1f44809a
JD
863 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
864 data->in_ext[nr]));
1da177e4 865}
b353a487 866
1f44809a 867static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
b353a487 868 char *buf)
1da177e4 869{
b353a487 870 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 871 struct lm85_data *data = lm85_update_device(dev);
1f44809a 872 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
1da177e4 873}
b353a487
JD
874
875static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
876 const char *buf, size_t count)
1da177e4 877{
b353a487 878 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
879 struct lm85_data *data = dev_get_drvdata(dev);
880 struct i2c_client *client = data->client;
09770b26
GR
881 long val;
882 int err;
883
884 err = kstrtol(buf, 10, &val);
885 if (err)
886 return err;
1da177e4 887
9a61bf63 888 mutex_lock(&data->update_lock);
1da177e4
LT
889 data->in_min[nr] = INS_TO_REG(nr, val);
890 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
9a61bf63 891 mutex_unlock(&data->update_lock);
1da177e4
LT
892 return count;
893}
b353a487
JD
894
895static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
896 char *buf)
1da177e4 897{
b353a487 898 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 899 struct lm85_data *data = lm85_update_device(dev);
1f44809a 900 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
1da177e4 901}
b353a487
JD
902
903static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
904 const char *buf, size_t count)
1da177e4 905{
b353a487 906 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
907 struct lm85_data *data = dev_get_drvdata(dev);
908 struct i2c_client *client = data->client;
09770b26
GR
909 long val;
910 int err;
911
912 err = kstrtol(buf, 10, &val);
913 if (err)
914 return err;
1da177e4 915
9a61bf63 916 mutex_lock(&data->update_lock);
1da177e4
LT
917 data->in_max[nr] = INS_TO_REG(nr, val);
918 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
9a61bf63 919 mutex_unlock(&data->update_lock);
1da177e4
LT
920 return count;
921}
b353a487 922
1da177e4 923#define show_in_reg(offset) \
b353a487
JD
924static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
925 show_in, NULL, offset); \
926static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
927 show_in_min, set_in_min, offset); \
928static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
929 show_in_max, set_in_max, offset)
1da177e4
LT
930
931show_in_reg(0);
932show_in_reg(1);
933show_in_reg(2);
934show_in_reg(3);
935show_in_reg(4);
6b9aad2d
JD
936show_in_reg(5);
937show_in_reg(6);
938show_in_reg(7);
1da177e4
LT
939
940/* Temps */
941
b353a487
JD
942static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
943 char *buf)
1da177e4 944{
b353a487 945 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 946 struct lm85_data *data = lm85_update_device(dev);
1f44809a
JD
947 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
948 data->temp_ext[nr]));
1da177e4 949}
b353a487
JD
950
951static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
952 char *buf)
1da177e4 953{
b353a487 954 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 955 struct lm85_data *data = lm85_update_device(dev);
1f44809a 956 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
1da177e4 957}
b353a487
JD
958
959static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
960 const char *buf, size_t count)
1da177e4 961{
b353a487 962 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
963 struct lm85_data *data = dev_get_drvdata(dev);
964 struct i2c_client *client = data->client;
09770b26
GR
965 long val;
966 int err;
967
968 err = kstrtol(buf, 10, &val);
969 if (err)
970 return err;
1da177e4 971
79b92f2b
DW
972 if (IS_ADT7468_OFF64(data))
973 val += 64;
974
9a61bf63 975 mutex_lock(&data->update_lock);
1da177e4
LT
976 data->temp_min[nr] = TEMP_TO_REG(val);
977 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
9a61bf63 978 mutex_unlock(&data->update_lock);
1da177e4
LT
979 return count;
980}
b353a487
JD
981
982static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
983 char *buf)
1da177e4 984{
b353a487 985 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 986 struct lm85_data *data = lm85_update_device(dev);
1f44809a 987 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1da177e4 988}
b353a487
JD
989
990static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
991 const char *buf, size_t count)
1da177e4 992{
b353a487 993 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
994 struct lm85_data *data = dev_get_drvdata(dev);
995 struct i2c_client *client = data->client;
09770b26
GR
996 long val;
997 int err;
998
999 err = kstrtol(buf, 10, &val);
1000 if (err)
1001 return err;
1da177e4 1002
79b92f2b
DW
1003 if (IS_ADT7468_OFF64(data))
1004 val += 64;
1005
9a61bf63 1006 mutex_lock(&data->update_lock);
1da177e4
LT
1007 data->temp_max[nr] = TEMP_TO_REG(val);
1008 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
9a61bf63 1009 mutex_unlock(&data->update_lock);
1da177e4
LT
1010 return count;
1011}
b353a487 1012
1da177e4 1013#define show_temp_reg(offset) \
b353a487
JD
1014static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1015 show_temp, NULL, offset - 1); \
1016static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1017 show_temp_min, set_temp_min, offset - 1); \
1018static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1019 show_temp_max, set_temp_max, offset - 1);
1da177e4
LT
1020
1021show_temp_reg(1);
1022show_temp_reg(2);
1023show_temp_reg(3);
1024
1025
1026/* Automatic PWM control */
1027
b353a487
JD
1028static ssize_t show_pwm_auto_channels(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1da177e4 1030{
b353a487 1031 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1032 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1033 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
1da177e4 1034}
b353a487
JD
1035
1036static ssize_t set_pwm_auto_channels(struct device *dev,
1037 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1038{
b353a487 1039 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1040 struct lm85_data *data = dev_get_drvdata(dev);
1041 struct i2c_client *client = data->client;
09770b26
GR
1042 long val;
1043 int err;
1044
1045 err = kstrtol(buf, 10, &val);
1046 if (err)
1047 return err;
1da177e4 1048
9a61bf63 1049 mutex_lock(&data->update_lock);
1da177e4 1050 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
1f44809a 1051 | ZONE_TO_REG(val);
1da177e4
LT
1052 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1053 data->autofan[nr].config);
9a61bf63 1054 mutex_unlock(&data->update_lock);
1da177e4
LT
1055 return count;
1056}
b353a487
JD
1057
1058static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1059 struct device_attribute *attr, char *buf)
1da177e4 1060{
b353a487 1061 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1062 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1063 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
1da177e4 1064}
b353a487
JD
1065
1066static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1067 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1068{
b353a487 1069 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1070 struct lm85_data *data = dev_get_drvdata(dev);
1071 struct i2c_client *client = data->client;
09770b26
GR
1072 unsigned long val;
1073 int err;
1074
1075 err = kstrtoul(buf, 10, &val);
1076 if (err)
1077 return err;
1da177e4 1078
9a61bf63 1079 mutex_lock(&data->update_lock);
1da177e4
LT
1080 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1081 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1082 data->autofan[nr].min_pwm);
9a61bf63 1083 mutex_unlock(&data->update_lock);
1da177e4
LT
1084 return count;
1085}
b353a487
JD
1086
1087static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1088 struct device_attribute *attr, char *buf)
1da177e4 1089{
b353a487 1090 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1091 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1092 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
1da177e4 1093}
b353a487
JD
1094
1095static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1096 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1097{
b353a487 1098 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1099 struct lm85_data *data = dev_get_drvdata(dev);
1100 struct i2c_client *client = data->client;
7133e56f 1101 u8 tmp;
09770b26
GR
1102 long val;
1103 int err;
1104
1105 err = kstrtol(buf, 10, &val);
1106 if (err)
1107 return err;
1da177e4 1108
9a61bf63 1109 mutex_lock(&data->update_lock);
1da177e4 1110 data->autofan[nr].min_off = val;
7133e56f
JD
1111 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1112 tmp &= ~(0x20 << nr);
1113 if (data->autofan[nr].min_off)
1114 tmp |= 0x20 << nr;
1115 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
9a61bf63 1116 mutex_unlock(&data->update_lock);
1da177e4
LT
1117 return count;
1118}
b353a487 1119
1da177e4 1120#define pwm_auto(offset) \
b353a487
JD
1121static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1122 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1123 set_pwm_auto_channels, offset - 1); \
1124static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
1125 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
1126 set_pwm_auto_pwm_min, offset - 1); \
1127static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1128 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
34e7dc6c 1129 set_pwm_auto_pwm_minctl, offset - 1)
b353a487 1130
1da177e4
LT
1131pwm_auto(1);
1132pwm_auto(2);
1133pwm_auto(3);
1134
1135/* Temperature settings for automatic PWM control */
1136
b353a487
JD
1137static ssize_t show_temp_auto_temp_off(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1da177e4 1139{
b353a487 1140 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1141 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1142 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
1da177e4
LT
1143 HYST_FROM_REG(data->zone[nr].hyst));
1144}
b353a487
JD
1145
1146static ssize_t set_temp_auto_temp_off(struct device *dev,
1147 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1148{
b353a487 1149 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1150 struct lm85_data *data = dev_get_drvdata(dev);
1151 struct i2c_client *client = data->client;
1da177e4 1152 int min;
09770b26
GR
1153 long val;
1154 int err;
1155
1156 err = kstrtol(buf, 10, &val);
1157 if (err)
1158 return err;
1da177e4 1159
9a61bf63 1160 mutex_lock(&data->update_lock);
1da177e4 1161 min = TEMP_FROM_REG(data->zone[nr].limit);
1da177e4 1162 data->zone[nr].hyst = HYST_TO_REG(min - val);
1f44809a 1163 if (nr == 0 || nr == 1) {
1da177e4
LT
1164 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1165 (data->zone[0].hyst << 4)
1f44809a 1166 | data->zone[1].hyst);
1da177e4
LT
1167 } else {
1168 lm85_write_value(client, LM85_REG_AFAN_HYST2,
1f44809a 1169 (data->zone[2].hyst << 4));
1da177e4 1170 }
9a61bf63 1171 mutex_unlock(&data->update_lock);
1da177e4
LT
1172 return count;
1173}
b353a487
JD
1174
1175static ssize_t show_temp_auto_temp_min(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1da177e4 1177{
b353a487 1178 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1179 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1180 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
1da177e4 1181}
b353a487
JD
1182
1183static ssize_t set_temp_auto_temp_min(struct device *dev,
1184 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1185{
b353a487 1186 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1187 struct lm85_data *data = dev_get_drvdata(dev);
1188 struct i2c_client *client = data->client;
09770b26
GR
1189 long val;
1190 int err;
1191
1192 err = kstrtol(buf, 10, &val);
1193 if (err)
1194 return err;
1da177e4 1195
9a61bf63 1196 mutex_lock(&data->update_lock);
1da177e4
LT
1197 data->zone[nr].limit = TEMP_TO_REG(val);
1198 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1199 data->zone[nr].limit);
1200
1201/* Update temp_auto_max and temp_auto_range */
1202 data->zone[nr].range = RANGE_TO_REG(
1203 TEMP_FROM_REG(data->zone[nr].max_desired) -
1204 TEMP_FROM_REG(data->zone[nr].limit));
1205 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1206 ((data->zone[nr].range & 0x0f) << 4)
57bc3019 1207 | data->pwm_freq[nr]);
1da177e4 1208
9a61bf63 1209 mutex_unlock(&data->update_lock);
1da177e4
LT
1210 return count;
1211}
b353a487
JD
1212
1213static ssize_t show_temp_auto_temp_max(struct device *dev,
1214 struct device_attribute *attr, char *buf)
1da177e4 1215{
b353a487 1216 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1217 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1218 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1da177e4
LT
1219 RANGE_FROM_REG(data->zone[nr].range));
1220}
b353a487
JD
1221
1222static ssize_t set_temp_auto_temp_max(struct device *dev,
1223 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1224{
b353a487 1225 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1226 struct lm85_data *data = dev_get_drvdata(dev);
1227 struct i2c_client *client = data->client;
1da177e4 1228 int min;
09770b26
GR
1229 long val;
1230 int err;
1231
1232 err = kstrtol(buf, 10, &val);
1233 if (err)
1234 return err;
1da177e4 1235
9a61bf63 1236 mutex_lock(&data->update_lock);
1da177e4
LT
1237 min = TEMP_FROM_REG(data->zone[nr].limit);
1238 data->zone[nr].max_desired = TEMP_TO_REG(val);
1239 data->zone[nr].range = RANGE_TO_REG(
1240 val - min);
1241 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1242 ((data->zone[nr].range & 0x0f) << 4)
57bc3019 1243 | data->pwm_freq[nr]);
9a61bf63 1244 mutex_unlock(&data->update_lock);
1da177e4
LT
1245 return count;
1246}
b353a487
JD
1247
1248static ssize_t show_temp_auto_temp_crit(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1da177e4 1250{
b353a487 1251 int nr = to_sensor_dev_attr(attr)->index;
1da177e4 1252 struct lm85_data *data = lm85_update_device(dev);
1f44809a 1253 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1da177e4 1254}
b353a487
JD
1255
1256static ssize_t set_temp_auto_temp_crit(struct device *dev,
1f44809a 1257 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 1258{
b353a487 1259 int nr = to_sensor_dev_attr(attr)->index;
746f6884
AL
1260 struct lm85_data *data = dev_get_drvdata(dev);
1261 struct i2c_client *client = data->client;
09770b26
GR
1262 long val;
1263 int err;
1264
1265 err = kstrtol(buf, 10, &val);
1266 if (err)
1267 return err;
1da177e4 1268
9a61bf63 1269 mutex_lock(&data->update_lock);
1da177e4
LT
1270 data->zone[nr].critical = TEMP_TO_REG(val);
1271 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1272 data->zone[nr].critical);
9a61bf63 1273 mutex_unlock(&data->update_lock);
1da177e4
LT
1274 return count;
1275}
b353a487 1276
1da177e4 1277#define temp_auto(offset) \
b353a487
JD
1278static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1279 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1280 set_temp_auto_temp_off, offset - 1); \
1281static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1282 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1283 set_temp_auto_temp_min, offset - 1); \
1284static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1285 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1286 set_temp_auto_temp_max, offset - 1); \
1287static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1288 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1289 set_temp_auto_temp_crit, offset - 1);
1290
1da177e4
LT
1291temp_auto(1);
1292temp_auto(2);
1293temp_auto(3);
1294
0501a381 1295static struct attribute *lm85_attributes[] = {
b353a487
JD
1296 &sensor_dev_attr_fan1_input.dev_attr.attr,
1297 &sensor_dev_attr_fan2_input.dev_attr.attr,
1298 &sensor_dev_attr_fan3_input.dev_attr.attr,
1299 &sensor_dev_attr_fan4_input.dev_attr.attr,
1300 &sensor_dev_attr_fan1_min.dev_attr.attr,
1301 &sensor_dev_attr_fan2_min.dev_attr.attr,
1302 &sensor_dev_attr_fan3_min.dev_attr.attr,
1303 &sensor_dev_attr_fan4_min.dev_attr.attr,
bf76e9d3
JD
1304 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1305 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1306 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1307 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
b353a487
JD
1308
1309 &sensor_dev_attr_pwm1.dev_attr.attr,
1310 &sensor_dev_attr_pwm2.dev_attr.attr,
1311 &sensor_dev_attr_pwm3.dev_attr.attr,
1312 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1313 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1314 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
34e7dc6c
JD
1315 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1316 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1317 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
b353a487
JD
1318
1319 &sensor_dev_attr_in0_input.dev_attr.attr,
1320 &sensor_dev_attr_in1_input.dev_attr.attr,
1321 &sensor_dev_attr_in2_input.dev_attr.attr,
1322 &sensor_dev_attr_in3_input.dev_attr.attr,
1323 &sensor_dev_attr_in0_min.dev_attr.attr,
1324 &sensor_dev_attr_in1_min.dev_attr.attr,
1325 &sensor_dev_attr_in2_min.dev_attr.attr,
1326 &sensor_dev_attr_in3_min.dev_attr.attr,
1327 &sensor_dev_attr_in0_max.dev_attr.attr,
1328 &sensor_dev_attr_in1_max.dev_attr.attr,
1329 &sensor_dev_attr_in2_max.dev_attr.attr,
1330 &sensor_dev_attr_in3_max.dev_attr.attr,
bf76e9d3
JD
1331 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1332 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1333 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1334 &sensor_dev_attr_in3_alarm.dev_attr.attr,
b353a487
JD
1335
1336 &sensor_dev_attr_temp1_input.dev_attr.attr,
1337 &sensor_dev_attr_temp2_input.dev_attr.attr,
1338 &sensor_dev_attr_temp3_input.dev_attr.attr,
1339 &sensor_dev_attr_temp1_min.dev_attr.attr,
1340 &sensor_dev_attr_temp2_min.dev_attr.attr,
1341 &sensor_dev_attr_temp3_min.dev_attr.attr,
1342 &sensor_dev_attr_temp1_max.dev_attr.attr,
1343 &sensor_dev_attr_temp2_max.dev_attr.attr,
1344 &sensor_dev_attr_temp3_max.dev_attr.attr,
bf76e9d3
JD
1345 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1346 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1347 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1348 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1349 &sensor_dev_attr_temp3_fault.dev_attr.attr,
b353a487
JD
1350
1351 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1352 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1353 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1354 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1355 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1356 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
b353a487 1357
b353a487
JD
1358 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1359 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1360 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1361 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1362 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1363 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1364 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1365 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1366 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1367
0501a381
MH
1368 &dev_attr_vrm.attr,
1369 &dev_attr_cpu0_vid.attr,
1370 &dev_attr_alarms.attr,
0501a381
MH
1371 NULL
1372};
1373
1374static const struct attribute_group lm85_group = {
1375 .attrs = lm85_attributes,
1376};
1377
06923f84
GR
1378static struct attribute *lm85_attributes_minctl[] = {
1379 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1380 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1381 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
5f441e22 1382 NULL
06923f84
GR
1383};
1384
1385static const struct attribute_group lm85_group_minctl = {
1386 .attrs = lm85_attributes_minctl,
1387};
1388
1389static struct attribute *lm85_attributes_temp_off[] = {
1390 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1391 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1392 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
5f441e22 1393 NULL
06923f84
GR
1394};
1395
1396static const struct attribute_group lm85_group_temp_off = {
1397 .attrs = lm85_attributes_temp_off,
1398};
1399
6b9aad2d 1400static struct attribute *lm85_attributes_in4[] = {
b353a487
JD
1401 &sensor_dev_attr_in4_input.dev_attr.attr,
1402 &sensor_dev_attr_in4_min.dev_attr.attr,
1403 &sensor_dev_attr_in4_max.dev_attr.attr,
bf76e9d3 1404 &sensor_dev_attr_in4_alarm.dev_attr.attr,
0501a381
MH
1405 NULL
1406};
1407
6b9aad2d
JD
1408static const struct attribute_group lm85_group_in4 = {
1409 .attrs = lm85_attributes_in4,
1410};
1411
1412static struct attribute *lm85_attributes_in567[] = {
1413 &sensor_dev_attr_in5_input.dev_attr.attr,
1414 &sensor_dev_attr_in6_input.dev_attr.attr,
1415 &sensor_dev_attr_in7_input.dev_attr.attr,
1416 &sensor_dev_attr_in5_min.dev_attr.attr,
1417 &sensor_dev_attr_in6_min.dev_attr.attr,
1418 &sensor_dev_attr_in7_min.dev_attr.attr,
1419 &sensor_dev_attr_in5_max.dev_attr.attr,
1420 &sensor_dev_attr_in6_max.dev_attr.attr,
1421 &sensor_dev_attr_in7_max.dev_attr.attr,
bf76e9d3
JD
1422 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1423 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1424 &sensor_dev_attr_in7_alarm.dev_attr.attr,
6b9aad2d
JD
1425 NULL
1426};
1427
1428static const struct attribute_group lm85_group_in567 = {
1429 .attrs = lm85_attributes_in567,
0501a381
MH
1430};
1431
5f447594
JD
1432static void lm85_init_client(struct i2c_client *client)
1433{
1434 int value;
1435
1436 /* Start monitoring if needed */
1437 value = lm85_read_value(client, LM85_REG_CONFIG);
1438 if (!(value & 0x01)) {
1439 dev_info(&client->dev, "Starting monitoring\n");
1440 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1441 }
1442
1443 /* Warn about unusual configuration bits */
1444 if (value & 0x02)
1445 dev_warn(&client->dev, "Device configuration is locked\n");
1446 if (!(value & 0x04))
1447 dev_warn(&client->dev, "Device is not ready\n");
1448}
1449
5cfaf338
JD
1450static int lm85_is_fake(struct i2c_client *client)
1451{
1452 /*
1453 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1454 * emulate the former except that it has no hardware monitoring function
1455 * so the readings are always 0.
1456 */
1457 int i;
1458 u8 in_temp, fan;
1459
1460 for (i = 0; i < 8; i++) {
1461 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1462 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1463 if (in_temp != 0x00 || fan != 0xff)
1464 return 0;
1465 }
1466
1467 return 1;
1468}
1469
67712d01 1470/* Return 0 if detection is successful, -ENODEV otherwise */
310ec792 1471static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1da177e4 1472{
67712d01
JD
1473 struct i2c_adapter *adapter = client->adapter;
1474 int address = client->addr;
590e8534 1475 const char *type_name = NULL;
d42a2eb5 1476 int company, verstep;
1da177e4 1477
e89e22b2 1478 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1da177e4 1479 /* We need to be able to do byte I/O */
67712d01 1480 return -ENODEV;
1f44809a 1481 }
1da177e4 1482
d42a2eb5
JD
1483 /* Determine the chip type */
1484 company = lm85_read_value(client, LM85_REG_COMPANY);
1485 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1486
b55f3757
GR
1487 dev_dbg(&adapter->dev,
1488 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
d42a2eb5
JD
1489 address, company, verstep);
1490
d42a2eb5
JD
1491 if (company == LM85_COMPANY_NATIONAL) {
1492 switch (verstep) {
1493 case LM85_VERSTEP_LM85C:
1494 type_name = "lm85c";
1495 break;
1496 case LM85_VERSTEP_LM85B:
1497 type_name = "lm85b";
1498 break;
1499 case LM85_VERSTEP_LM96000_1:
1500 case LM85_VERSTEP_LM96000_2:
1501 /* Check for Winbond WPCD377I */
1502 if (lm85_is_fake(client)) {
1503 dev_dbg(&adapter->dev,
1504 "Found Winbond WPCD377I, ignoring\n");
1505 return -ENODEV;
69fc1feb 1506 }
11650cf0 1507 type_name = "lm96000";
d42a2eb5
JD
1508 break;
1509 }
1510 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1511 switch (verstep) {
1512 case LM85_VERSTEP_ADM1027:
1513 type_name = "adm1027";
1514 break;
1515 case LM85_VERSTEP_ADT7463:
1516 case LM85_VERSTEP_ADT7463C:
1517 type_name = "adt7463";
1518 break;
1519 case LM85_VERSTEP_ADT7468_1:
1520 case LM85_VERSTEP_ADT7468_2:
1521 type_name = "adt7468";
1522 break;
1da177e4 1523 }
d42a2eb5
JD
1524 } else if (company == LM85_COMPANY_SMSC) {
1525 switch (verstep) {
1526 case LM85_VERSTEP_EMC6D100_A0:
1527 case LM85_VERSTEP_EMC6D100_A1:
1528 /* Note: we can't tell a '100 from a '101 */
1529 type_name = "emc6d100";
1530 break;
1531 case LM85_VERSTEP_EMC6D102:
1532 type_name = "emc6d102";
1533 break;
f065a93e
JB
1534 case LM85_VERSTEP_EMC6D103_A0:
1535 case LM85_VERSTEP_EMC6D103_A1:
1536 type_name = "emc6d103";
1537 break;
f065a93e
JB
1538 case LM85_VERSTEP_EMC6D103S:
1539 type_name = "emc6d103s";
1540 break;
d42a2eb5 1541 }
1da177e4
LT
1542 }
1543
590e8534
JD
1544 if (!type_name)
1545 return -ENODEV;
1546
67712d01
JD
1547 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1548
1549 return 0;
1550}
1da177e4 1551
746f6884 1552static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
67712d01 1553{
746f6884
AL
1554 struct device *dev = &client->dev;
1555 struct device *hwmon_dev;
67712d01 1556 struct lm85_data *data;
746f6884 1557 int idx = 0;
67712d01 1558
746f6884 1559 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
67712d01
JD
1560 if (!data)
1561 return -ENOMEM;
1562
746f6884 1563 data->client = client;
00c0f9d3
JMC
1564 if (client->dev.of_node)
1565 data->type = (enum chips)of_device_get_match_data(&client->dev);
1566 else
1567 data->type = id->driver_data;
9a61bf63 1568 mutex_init(&data->update_lock);
1da177e4 1569
67712d01
JD
1570 /* Fill in the chip specific driver values */
1571 switch (data->type) {
1572 case adm1027:
1573 case adt7463:
fa7a5797 1574 case adt7468:
67712d01
JD
1575 case emc6d100:
1576 case emc6d102:
f065a93e 1577 case emc6d103:
06923f84 1578 case emc6d103s:
67712d01 1579 data->freq_map = adm1027_freq_map;
57bc3019 1580 data->freq_map_size = ARRAY_SIZE(adm1027_freq_map);
67712d01 1581 break;
e9b95485
JG
1582 case lm96000:
1583 data->freq_map = lm96000_freq_map;
1584 data->freq_map_size = ARRAY_SIZE(lm96000_freq_map);
1585 break;
67712d01
JD
1586 default:
1587 data->freq_map = lm85_freq_map;
57bc3019 1588 data->freq_map_size = ARRAY_SIZE(lm85_freq_map);
67712d01 1589 }
1da177e4
LT
1590
1591 /* Set the VRM version */
303760b4 1592 data->vrm = vid_which_vrm();
1da177e4
LT
1593
1594 /* Initialize the LM85 chip */
e89e22b2 1595 lm85_init_client(client);
1da177e4 1596
746f6884
AL
1597 /* sysfs hooks */
1598 data->groups[idx++] = &lm85_group;
1da177e4 1599
06923f84
GR
1600 /* minctl and temp_off exist on all chips except emc6d103s */
1601 if (data->type != emc6d103s) {
746f6884
AL
1602 data->groups[idx++] = &lm85_group_minctl;
1603 data->groups[idx++] = &lm85_group_temp_off;
06923f84
GR
1604 }
1605
09770b26
GR
1606 /*
1607 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1608 * as a sixth digital VID input rather than an analog input.
1609 */
de248805
GR
1610 if (data->type == adt7463 || data->type == adt7468) {
1611 u8 vid = lm85_read_value(client, LM85_REG_VID);
1612 if (vid & 0x80)
1613 data->has_vid5 = true;
1614 }
1615
746f6884
AL
1616 if (!data->has_vid5)
1617 data->groups[idx++] = &lm85_group_in4;
6b9aad2d
JD
1618
1619 /* The EMC6D100 has 3 additional voltage inputs */
746f6884
AL
1620 if (data->type == emc6d100)
1621 data->groups[idx++] = &lm85_group_in567;
1da177e4 1622
746f6884
AL
1623 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1624 data, data->groups);
1625 return PTR_ERR_OR_ZERO(hwmon_dev);
1da177e4
LT
1626}
1627
6fd5dd58
AL
1628static const struct i2c_device_id lm85_id[] = {
1629 { "adm1027", adm1027 },
1630 { "adt7463", adt7463 },
1631 { "adt7468", adt7468 },
1632 { "lm85", lm85 },
1633 { "lm85b", lm85 },
1634 { "lm85c", lm85 },
11650cf0 1635 { "lm96000", lm96000 },
6fd5dd58
AL
1636 { "emc6d100", emc6d100 },
1637 { "emc6d101", emc6d100 },
1638 { "emc6d102", emc6d102 },
1639 { "emc6d103", emc6d103 },
1640 { "emc6d103s", emc6d103s },
1641 { }
1642};
1643MODULE_DEVICE_TABLE(i2c, lm85_id);
1da177e4 1644
00c0f9d3
JMC
1645static const struct of_device_id lm85_of_match[] = {
1646 {
1647 .compatible = "adi,adm1027",
1648 .data = (void *)adm1027
1649 },
1650 {
1651 .compatible = "adi,adt7463",
1652 .data = (void *)adt7463
1653 },
1654 {
1655 .compatible = "adi,adt7468",
1656 .data = (void *)adt7468
1657 },
1658 {
1659 .compatible = "national,lm85",
1660 .data = (void *)lm85
1661 },
1662 {
1663 .compatible = "national,lm85b",
1664 .data = (void *)lm85
1665 },
1666 {
1667 .compatible = "national,lm85c",
1668 .data = (void *)lm85
1669 },
11650cf0
JG
1670 {
1671 .compatible = "ti,lm96000",
1672 .data = (void *)lm96000
1673 },
00c0f9d3
JMC
1674 {
1675 .compatible = "smsc,emc6d100",
1676 .data = (void *)emc6d100
1677 },
1678 {
1679 .compatible = "smsc,emc6d101",
1680 .data = (void *)emc6d100
1681 },
1682 {
1683 .compatible = "smsc,emc6d102",
1684 .data = (void *)emc6d102
1685 },
1686 {
1687 .compatible = "smsc,emc6d103",
1688 .data = (void *)emc6d103
1689 },
1690 {
1691 .compatible = "smsc,emc6d103s",
1692 .data = (void *)emc6d103s
1693 },
1694 { },
1695};
1696MODULE_DEVICE_TABLE(of, lm85_of_match);
1697
6fd5dd58
AL
1698static struct i2c_driver lm85_driver = {
1699 .class = I2C_CLASS_HWMON,
1700 .driver = {
1701 .name = "lm85",
00c0f9d3 1702 .of_match_table = of_match_ptr(lm85_of_match),
6fd5dd58
AL
1703 },
1704 .probe = lm85_probe,
6fd5dd58
AL
1705 .id_table = lm85_id,
1706 .detect = lm85_detect,
1707 .address_list = normal_i2c,
1708};
1da177e4 1709
f0967eea 1710module_i2c_driver(lm85_driver);
1da177e4 1711
1da177e4 1712MODULE_LICENSE("GPL");
1f44809a
JD
1713MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1714 "Margit Schubert-While <margitsw@t-online.de>, "
e89e22b2 1715 "Justin Thiessen <jthiessen@penguincomputing.com>");
1da177e4 1716MODULE_DESCRIPTION("LM85-B, LM85-C driver");