1 /*
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>
8 * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de>
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 */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/jiffies.h>
31 #include <linux/i2c.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-vid.h>
34 #include <linux/hwmon-sysfs.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37
38 /* Addresses to scan */
39 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40
41 enum chips {
42 lm85,
43 adm1027, adt7463, adt7468,
44 emc6d100, emc6d102, emc6d103, emc6d103s
45 };
46
47 /* The LM85 registers */
48
49 #define LM85_REG_IN(nr) (0x20 + (nr))
50 #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
51 #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
52
53 #define LM85_REG_TEMP(nr) (0x25 + (nr))
54 #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
55 #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
56
57 /* Fan speeds are LSB, MSB (2 bytes) */
58 #define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
59 #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
60
61 #define LM85_REG_PWM(nr) (0x30 + (nr))
62
63 #define LM85_REG_COMPANY 0x3e
64 #define LM85_REG_VERSTEP 0x3f
65
66 #define ADT7468_REG_CFG5 0x7c
67 #define ADT7468_OFF64 (1 << 0)
68 #define ADT7468_HFPWM (1 << 1)
69 #define IS_ADT7468_OFF64(data) \
70 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
71 #define IS_ADT7468_HFPWM(data) \
72 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
73
74 /* These are the recognized values for the above regs */
75 #define LM85_COMPANY_NATIONAL 0x01
76 #define LM85_COMPANY_ANALOG_DEV 0x41
77 #define LM85_COMPANY_SMSC 0x5c
78 #define LM85_VERSTEP_LM85C 0x60
79 #define LM85_VERSTEP_LM85B 0x62
80 #define LM85_VERSTEP_LM96000_1 0x68
81 #define LM85_VERSTEP_LM96000_2 0x69
82 #define LM85_VERSTEP_ADM1027 0x60
83 #define LM85_VERSTEP_ADT7463 0x62
84 #define LM85_VERSTEP_ADT7463C 0x6A
85 #define LM85_VERSTEP_ADT7468_1 0x71
86 #define LM85_VERSTEP_ADT7468_2 0x72
87 #define LM85_VERSTEP_EMC6D100_A0 0x60
88 #define LM85_VERSTEP_EMC6D100_A1 0x61
89 #define LM85_VERSTEP_EMC6D102 0x65
90 #define LM85_VERSTEP_EMC6D103_A0 0x68
91 #define LM85_VERSTEP_EMC6D103_A1 0x69
92 #define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
93
94 #define LM85_REG_CONFIG 0x40
95
96 #define LM85_REG_ALARM1 0x41
97 #define LM85_REG_ALARM2 0x42
98
99 #define LM85_REG_VID 0x43
100
101 /* Automated FAN control */
102 #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
103 #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
104 #define LM85_REG_AFAN_SPIKE1 0x62
105 #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
106 #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
107 #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
108 #define LM85_REG_AFAN_HYST1 0x6d
109 #define LM85_REG_AFAN_HYST2 0x6e
110
111 #define ADM1027_REG_EXTEND_ADC1 0x76
112 #define ADM1027_REG_EXTEND_ADC2 0x77
113
114 #define EMC6D100_REG_ALARM3 0x7d
115 /* IN5, IN6 and IN7 */
116 #define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
117 #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
118 #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
119 #define EMC6D102_REG_EXTEND_ADC1 0x85
120 #define EMC6D102_REG_EXTEND_ADC2 0x86
121 #define EMC6D102_REG_EXTEND_ADC3 0x87
122 #define EMC6D102_REG_EXTEND_ADC4 0x88
123
124 /*
125 * Conversions. Rounding and limit checking is only done on the TO_REG
126 * variants. Note that you should be a bit careful with which arguments
127 * these macros are called: arguments may be evaluated more than once.
128 */
129
130 /* IN are scaled according to built-in resistors */
131 static const int lm85_scaling[] = { /* .001 Volts */
132 2500, 2250, 3300, 5000, 12000,
133 3300, 1500, 1800 /*EMC6D100*/
134 };
135 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
136
137 #define INS_TO_REG(n, val) \
138 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
139
140 #define INSEXT_FROM_REG(n, val, ext) \
141 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
142
143 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
144
145 /* FAN speed is measured using 90kHz clock */
FAN_TO_REG(unsigned long val)146 static inline u16 FAN_TO_REG(unsigned long val)
147 {
148 if (!val)
149 return 0xffff;
150 return clamp_val(5400000 / val, 1, 0xfffe);
151 }
152 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
153 5400000 / (val))
154
155 /* Temperature is reported in .001 degC increments */
156 #define TEMP_TO_REG(val) \
157 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
158 #define TEMPEXT_FROM_REG(val, ext) \
159 SCALE(((val) << 4) + (ext), 16, 1000)
160 #define TEMP_FROM_REG(val) ((val) * 1000)
161
162 #define PWM_TO_REG(val) clamp_val(val, 0, 255)
163 #define PWM_FROM_REG(val) (val)
164
165
166 /*
167 * ZONEs have the following parameters:
168 * Limit (low) temp, 1. degC
169 * Hysteresis (below limit), 1. degC (0-15)
170 * Range of speed control, .1 degC (2-80)
171 * Critical (high) temp, 1. degC
172 *
173 * FAN PWMs have the following parameters:
174 * Reference Zone, 1, 2, 3, etc.
175 * Spinup time, .05 sec
176 * PWM value at limit/low temp, 1 count
177 * PWM Frequency, 1. Hz
178 * PWM is Min or OFF below limit, flag
179 * Invert PWM output, flag
180 *
181 * Some chips filter the temp, others the fan.
182 * Filter constant (or disabled) .1 seconds
183 */
184
185 /* These are the zone temperature range encodings in .001 degree C */
186 static const int lm85_range_map[] = {
187 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
188 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
189 };
190
RANGE_TO_REG(long range)191 static int RANGE_TO_REG(long range)
192 {
193 int i;
194
195 /* Find the closest match */
196 for (i = 0; i < 15; ++i) {
197 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
198 break;
199 }
200
201 return i;
202 }
203 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
204
205 /* These are the PWM frequency encodings */
206 static const int lm85_freq_map[8] = { /* 1 Hz */
207 10, 15, 23, 30, 38, 47, 61, 94
208 };
209 static const int adm1027_freq_map[8] = { /* 1 Hz */
210 11, 15, 22, 29, 35, 44, 59, 88
211 };
212
FREQ_TO_REG(const int * map,unsigned long freq)213 static int FREQ_TO_REG(const int *map, unsigned long freq)
214 {
215 int i;
216
217 /* Find the closest match */
218 for (i = 0; i < 7; ++i)
219 if (freq <= (map[i] + map[i + 1]) / 2)
220 break;
221 return i;
222 }
223
FREQ_FROM_REG(const int * map,u8 reg)224 static int FREQ_FROM_REG(const int *map, u8 reg)
225 {
226 return map[reg & 0x07];
227 }
228
229 /*
230 * Since we can't use strings, I'm abusing these numbers
231 * to stand in for the following meanings:
232 * 1 -- PWM responds to Zone 1
233 * 2 -- PWM responds to Zone 2
234 * 3 -- PWM responds to Zone 3
235 * 23 -- PWM responds to the higher temp of Zone 2 or 3
236 * 123 -- PWM responds to highest of Zone 1, 2, or 3
237 * 0 -- PWM is always at 0% (ie, off)
238 * -1 -- PWM is always at 100%
239 * -2 -- PWM responds to manual control
240 */
241
242 static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
243 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
244
ZONE_TO_REG(int zone)245 static int ZONE_TO_REG(int zone)
246 {
247 int i;
248
249 for (i = 0; i <= 7; ++i)
250 if (zone == lm85_zone_map[i])
251 break;
252 if (i > 7) /* Not found. */
253 i = 3; /* Always 100% */
254 return i << 5;
255 }
256
257 #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
258 #define HYST_FROM_REG(val) ((val) * 1000)
259
260 /*
261 * Chip sampling rates
262 *
263 * Some sensors are not updated more frequently than once per second
264 * so it doesn't make sense to read them more often than that.
265 * We cache the results and return the saved data if the driver
266 * is called again before a second has elapsed.
267 *
268 * Also, there is significant configuration data for this chip
269 * given the automatic PWM fan control that is possible. There
270 * are about 47 bytes of config data to only 22 bytes of actual
271 * readings. So, we keep the config data up to date in the cache
272 * when it is written and only sample it once every 1 *minute*
273 */
274 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
275 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
276
277 /*
278 * LM85 can automatically adjust fan speeds based on temperature
279 * This structure encapsulates an entire Zone config. There are
280 * three zones (one for each temperature input) on the lm85
281 */
282 struct lm85_zone {
283 s8 limit; /* Low temp limit */
284 u8 hyst; /* Low limit hysteresis. (0-15) */
285 u8 range; /* Temp range, encoded */
286 s8 critical; /* "All fans ON" temp limit */
287 u8 max_desired; /*
288 * Actual "max" temperature specified. Preserved
289 * to prevent "drift" as other autofan control
290 * values change.
291 */
292 };
293
294 struct lm85_autofan {
295 u8 config; /* Register value */
296 u8 min_pwm; /* Minimum PWM value, encoded */
297 u8 min_off; /* Min PWM or OFF below "limit", flag */
298 };
299
300 /*
301 * For each registered chip, we need to keep some data in memory.
302 * The structure is dynamically allocated.
303 */
304 struct lm85_data {
305 struct i2c_client *client;
306 const struct attribute_group *groups[6];
307 const int *freq_map;
308 enum chips type;
309
310 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
311
312 struct mutex update_lock;
313 int valid; /* !=0 if following fields are valid */
314 unsigned long last_reading; /* In jiffies */
315 unsigned long last_config; /* In jiffies */
316
317 u8 in[8]; /* Register value */
318 u8 in_max[8]; /* Register value */
319 u8 in_min[8]; /* Register value */
320 s8 temp[3]; /* Register value */
321 s8 temp_min[3]; /* Register value */
322 s8 temp_max[3]; /* Register value */
323 u16 fan[4]; /* Register value */
324 u16 fan_min[4]; /* Register value */
325 u8 pwm[3]; /* Register value */
326 u8 pwm_freq[3]; /* Register encoding */
327 u8 temp_ext[3]; /* Decoded values */
328 u8 in_ext[8]; /* Decoded values */
329 u8 vid; /* Register value */
330 u8 vrm; /* VRM version */
331 u32 alarms; /* Register encoding, combined */
332 u8 cfg5; /* Config Register 5 on ADT7468 */
333 struct lm85_autofan autofan[3];
334 struct lm85_zone zone[3];
335 };
336
lm85_read_value(struct i2c_client * client,u8 reg)337 static int lm85_read_value(struct i2c_client *client, u8 reg)
338 {
339 int res;
340
341 /* What size location is it? */
342 switch (reg) {
343 case LM85_REG_FAN(0): /* Read WORD data */
344 case LM85_REG_FAN(1):
345 case LM85_REG_FAN(2):
346 case LM85_REG_FAN(3):
347 case LM85_REG_FAN_MIN(0):
348 case LM85_REG_FAN_MIN(1):
349 case LM85_REG_FAN_MIN(2):
350 case LM85_REG_FAN_MIN(3):
351 case LM85_REG_ALARM1: /* Read both bytes at once */
352 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
353 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
354 break;
355 default: /* Read BYTE data */
356 res = i2c_smbus_read_byte_data(client, reg);
357 break;
358 }
359
360 return res;
361 }
362
lm85_write_value(struct i2c_client * client,u8 reg,int value)363 static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
364 {
365 switch (reg) {
366 case LM85_REG_FAN(0): /* Write WORD data */
367 case LM85_REG_FAN(1):
368 case LM85_REG_FAN(2):
369 case LM85_REG_FAN(3):
370 case LM85_REG_FAN_MIN(0):
371 case LM85_REG_FAN_MIN(1):
372 case LM85_REG_FAN_MIN(2):
373 case LM85_REG_FAN_MIN(3):
374 /* NOTE: ALARM is read only, so not included here */
375 i2c_smbus_write_byte_data(client, reg, value & 0xff);
376 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
377 break;
378 default: /* Write BYTE data */
379 i2c_smbus_write_byte_data(client, reg, value);
380 break;
381 }
382 }
383
lm85_update_device(struct device * dev)384 static struct lm85_data *lm85_update_device(struct device *dev)
385 {
386 struct lm85_data *data = dev_get_drvdata(dev);
387 struct i2c_client *client = data->client;
388 int i;
389
390 mutex_lock(&data->update_lock);
391
392 if (!data->valid ||
393 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
394 /* Things that change quickly */
395 dev_dbg(&client->dev, "Reading sensor values\n");
396
397 /*
398 * Have to read extended bits first to "freeze" the
399 * more significant bits that are read later.
400 * There are 2 additional resolution bits per channel and we
401 * have room for 4, so we shift them to the left.
402 */
403 if (data->type == adm1027 || data->type == adt7463 ||
404 data->type == adt7468) {
405 int ext1 = lm85_read_value(client,
406 ADM1027_REG_EXTEND_ADC1);
407 int ext2 = lm85_read_value(client,
408 ADM1027_REG_EXTEND_ADC2);
409 int val = (ext1 << 8) + ext2;
410
411 for (i = 0; i <= 4; i++)
412 data->in_ext[i] =
413 ((val >> (i * 2)) & 0x03) << 2;
414
415 for (i = 0; i <= 2; i++)
416 data->temp_ext[i] =
417 (val >> ((i + 4) * 2)) & 0x0c;
418 }
419
420 data->vid = lm85_read_value(client, LM85_REG_VID);
421
422 for (i = 0; i <= 3; ++i) {
423 data->in[i] =
424 lm85_read_value(client, LM85_REG_IN(i));
425 data->fan[i] =
426 lm85_read_value(client, LM85_REG_FAN(i));
427 }
428
429 if (!data->has_vid5)
430 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
431
432 if (data->type == adt7468)
433 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
434
435 for (i = 0; i <= 2; ++i) {
436 data->temp[i] =
437 lm85_read_value(client, LM85_REG_TEMP(i));
438 data->pwm[i] =
439 lm85_read_value(client, LM85_REG_PWM(i));
440
441 if (IS_ADT7468_OFF64(data))
442 data->temp[i] -= 64;
443 }
444
445 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
446
447 if (data->type == emc6d100) {
448 /* Three more voltage sensors */
449 for (i = 5; i <= 7; ++i) {
450 data->in[i] = lm85_read_value(client,
451 EMC6D100_REG_IN(i));
452 }
453 /* More alarm bits */
454 data->alarms |= lm85_read_value(client,
455 EMC6D100_REG_ALARM3) << 16;
456 } else if (data->type == emc6d102 || data->type == emc6d103 ||
457 data->type == emc6d103s) {
458 /*
459 * Have to read LSB bits after the MSB ones because
460 * the reading of the MSB bits has frozen the
461 * LSBs (backward from the ADM1027).
462 */
463 int ext1 = lm85_read_value(client,
464 EMC6D102_REG_EXTEND_ADC1);
465 int ext2 = lm85_read_value(client,
466 EMC6D102_REG_EXTEND_ADC2);
467 int ext3 = lm85_read_value(client,
468 EMC6D102_REG_EXTEND_ADC3);
469 int ext4 = lm85_read_value(client,
470 EMC6D102_REG_EXTEND_ADC4);
471 data->in_ext[0] = ext3 & 0x0f;
472 data->in_ext[1] = ext4 & 0x0f;
473 data->in_ext[2] = ext4 >> 4;
474 data->in_ext[3] = ext3 >> 4;
475 data->in_ext[4] = ext2 >> 4;
476
477 data->temp_ext[0] = ext1 & 0x0f;
478 data->temp_ext[1] = ext2 & 0x0f;
479 data->temp_ext[2] = ext1 >> 4;
480 }
481
482 data->last_reading = jiffies;
483 } /* last_reading */
484
485 if (!data->valid ||
486 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
487 /* Things that don't change often */
488 dev_dbg(&client->dev, "Reading config values\n");
489
490 for (i = 0; i <= 3; ++i) {
491 data->in_min[i] =
492 lm85_read_value(client, LM85_REG_IN_MIN(i));
493 data->in_max[i] =
494 lm85_read_value(client, LM85_REG_IN_MAX(i));
495 data->fan_min[i] =
496 lm85_read_value(client, LM85_REG_FAN_MIN(i));
497 }
498
499 if (!data->has_vid5) {
500 data->in_min[4] = lm85_read_value(client,
501 LM85_REG_IN_MIN(4));
502 data->in_max[4] = lm85_read_value(client,
503 LM85_REG_IN_MAX(4));
504 }
505
506 if (data->type == emc6d100) {
507 for (i = 5; i <= 7; ++i) {
508 data->in_min[i] = lm85_read_value(client,
509 EMC6D100_REG_IN_MIN(i));
510 data->in_max[i] = lm85_read_value(client,
511 EMC6D100_REG_IN_MAX(i));
512 }
513 }
514
515 for (i = 0; i <= 2; ++i) {
516 int val;
517
518 data->temp_min[i] =
519 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
520 data->temp_max[i] =
521 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
522
523 data->autofan[i].config =
524 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
525 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
526 data->pwm_freq[i] = val & 0x07;
527 data->zone[i].range = val >> 4;
528 data->autofan[i].min_pwm =
529 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
530 data->zone[i].limit =
531 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
532 data->zone[i].critical =
533 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
534
535 if (IS_ADT7468_OFF64(data)) {
536 data->temp_min[i] -= 64;
537 data->temp_max[i] -= 64;
538 data->zone[i].limit -= 64;
539 data->zone[i].critical -= 64;
540 }
541 }
542
543 if (data->type != emc6d103s) {
544 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
545 data->autofan[0].min_off = (i & 0x20) != 0;
546 data->autofan[1].min_off = (i & 0x40) != 0;
547 data->autofan[2].min_off = (i & 0x80) != 0;
548
549 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
550 data->zone[0].hyst = i >> 4;
551 data->zone[1].hyst = i & 0x0f;
552
553 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
554 data->zone[2].hyst = i >> 4;
555 }
556
557 data->last_config = jiffies;
558 } /* last_config */
559
560 data->valid = 1;
561
562 mutex_unlock(&data->update_lock);
563
564 return data;
565 }
566
567 /* 4 Fans */
show_fan(struct device * dev,struct device_attribute * attr,char * buf)568 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
569 char *buf)
570 {
571 int nr = to_sensor_dev_attr(attr)->index;
572 struct lm85_data *data = lm85_update_device(dev);
573 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
574 }
575
show_fan_min(struct device * dev,struct device_attribute * attr,char * buf)576 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
577 char *buf)
578 {
579 int nr = to_sensor_dev_attr(attr)->index;
580 struct lm85_data *data = lm85_update_device(dev);
581 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
582 }
583
set_fan_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)584 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
585 const char *buf, size_t count)
586 {
587 int nr = to_sensor_dev_attr(attr)->index;
588 struct lm85_data *data = dev_get_drvdata(dev);
589 struct i2c_client *client = data->client;
590 unsigned long val;
591 int err;
592
593 err = kstrtoul(buf, 10, &val);
594 if (err)
595 return err;
596
597 mutex_lock(&data->update_lock);
598 data->fan_min[nr] = FAN_TO_REG(val);
599 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
600 mutex_unlock(&data->update_lock);
601 return count;
602 }
603
604 #define show_fan_offset(offset) \
605 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
606 show_fan, NULL, offset - 1); \
607 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
608 show_fan_min, set_fan_min, offset - 1)
609
610 show_fan_offset(1);
611 show_fan_offset(2);
612 show_fan_offset(3);
613 show_fan_offset(4);
614
615 /* vid, vrm, alarms */
616
show_vid_reg(struct device * dev,struct device_attribute * attr,char * buf)617 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
618 char *buf)
619 {
620 struct lm85_data *data = lm85_update_device(dev);
621 int vid;
622
623 if (data->has_vid5) {
624 /* 6-pin VID (VRM 10) */
625 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
626 } else {
627 /* 5-pin VID (VRM 9) */
628 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
629 }
630
631 return sprintf(buf, "%d\n", vid);
632 }
633
634 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
635
show_vrm_reg(struct device * dev,struct device_attribute * attr,char * buf)636 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
637 char *buf)
638 {
639 struct lm85_data *data = dev_get_drvdata(dev);
640 return sprintf(buf, "%ld\n", (long) data->vrm);
641 }
642
store_vrm_reg(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)643 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
644 const char *buf, size_t count)
645 {
646 struct lm85_data *data = dev_get_drvdata(dev);
647 unsigned long val;
648 int err;
649
650 err = kstrtoul(buf, 10, &val);
651 if (err)
652 return err;
653
654 if (val > 255)
655 return -EINVAL;
656
657 data->vrm = val;
658 return count;
659 }
660
661 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
662
show_alarms_reg(struct device * dev,struct device_attribute * attr,char * buf)663 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
664 *attr, char *buf)
665 {
666 struct lm85_data *data = lm85_update_device(dev);
667 return sprintf(buf, "%u\n", data->alarms);
668 }
669
670 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
671
show_alarm(struct device * dev,struct device_attribute * attr,char * buf)672 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
673 char *buf)
674 {
675 int nr = to_sensor_dev_attr(attr)->index;
676 struct lm85_data *data = lm85_update_device(dev);
677 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
678 }
679
680 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
681 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
682 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
683 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
684 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
685 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
686 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
687 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
688 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
689 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
690 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
691 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
692 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
693 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
694 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
695 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
696 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
697
698 /* pwm */
699
show_pwm(struct device * dev,struct device_attribute * attr,char * buf)700 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
701 char *buf)
702 {
703 int nr = to_sensor_dev_attr(attr)->index;
704 struct lm85_data *data = lm85_update_device(dev);
705 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
706 }
707
set_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)708 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
709 const char *buf, size_t count)
710 {
711 int nr = to_sensor_dev_attr(attr)->index;
712 struct lm85_data *data = dev_get_drvdata(dev);
713 struct i2c_client *client = data->client;
714 unsigned long val;
715 int err;
716
717 err = kstrtoul(buf, 10, &val);
718 if (err)
719 return err;
720
721 mutex_lock(&data->update_lock);
722 data->pwm[nr] = PWM_TO_REG(val);
723 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
724 mutex_unlock(&data->update_lock);
725 return count;
726 }
727
show_pwm_enable(struct device * dev,struct device_attribute * attr,char * buf)728 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
729 *attr, char *buf)
730 {
731 int nr = to_sensor_dev_attr(attr)->index;
732 struct lm85_data *data = lm85_update_device(dev);
733 int pwm_zone, enable;
734
735 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
736 switch (pwm_zone) {
737 case -1: /* PWM is always at 100% */
738 enable = 0;
739 break;
740 case 0: /* PWM is always at 0% */
741 case -2: /* PWM responds to manual control */
742 enable = 1;
743 break;
744 default: /* PWM in automatic mode */
745 enable = 2;
746 }
747 return sprintf(buf, "%d\n", enable);
748 }
749
set_pwm_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)750 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
751 *attr, const char *buf, size_t count)
752 {
753 int nr = to_sensor_dev_attr(attr)->index;
754 struct lm85_data *data = dev_get_drvdata(dev);
755 struct i2c_client *client = data->client;
756 u8 config;
757 unsigned long val;
758 int err;
759
760 err = kstrtoul(buf, 10, &val);
761 if (err)
762 return err;
763
764 switch (val) {
765 case 0:
766 config = 3;
767 break;
768 case 1:
769 config = 7;
770 break;
771 case 2:
772 /*
773 * Here we have to choose arbitrarily one of the 5 possible
774 * configurations; I go for the safest
775 */
776 config = 6;
777 break;
778 default:
779 return -EINVAL;
780 }
781
782 mutex_lock(&data->update_lock);
783 data->autofan[nr].config = lm85_read_value(client,
784 LM85_REG_AFAN_CONFIG(nr));
785 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
786 | (config << 5);
787 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
788 data->autofan[nr].config);
789 mutex_unlock(&data->update_lock);
790 return count;
791 }
792
show_pwm_freq(struct device * dev,struct device_attribute * attr,char * buf)793 static ssize_t show_pwm_freq(struct device *dev,
794 struct device_attribute *attr, char *buf)
795 {
796 int nr = to_sensor_dev_attr(attr)->index;
797 struct lm85_data *data = lm85_update_device(dev);
798 int freq;
799
800 if (IS_ADT7468_HFPWM(data))
801 freq = 22500;
802 else
803 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
804
805 return sprintf(buf, "%d\n", freq);
806 }
807
set_pwm_freq(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)808 static ssize_t set_pwm_freq(struct device *dev,
809 struct device_attribute *attr, const char *buf, size_t count)
810 {
811 int nr = to_sensor_dev_attr(attr)->index;
812 struct lm85_data *data = dev_get_drvdata(dev);
813 struct i2c_client *client = data->client;
814 unsigned long val;
815 int err;
816
817 err = kstrtoul(buf, 10, &val);
818 if (err)
819 return err;
820
821 mutex_lock(&data->update_lock);
822 /*
823 * The ADT7468 has a special high-frequency PWM output mode,
824 * where all PWM outputs are driven by a 22.5 kHz clock.
825 * This might confuse the user, but there's not much we can do.
826 */
827 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
828 data->cfg5 &= ~ADT7468_HFPWM;
829 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
830 } else { /* Low freq. mode */
831 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
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 }
840 mutex_unlock(&data->update_lock);
841 return count;
842 }
843
844 #define show_pwm_reg(offset) \
845 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
846 show_pwm, set_pwm, offset - 1); \
847 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
848 show_pwm_enable, set_pwm_enable, offset - 1); \
849 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
850 show_pwm_freq, set_pwm_freq, offset - 1)
851
852 show_pwm_reg(1);
853 show_pwm_reg(2);
854 show_pwm_reg(3);
855
856 /* Voltages */
857
show_in(struct device * dev,struct device_attribute * attr,char * buf)858 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
859 char *buf)
860 {
861 int nr = to_sensor_dev_attr(attr)->index;
862 struct lm85_data *data = lm85_update_device(dev);
863 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
864 data->in_ext[nr]));
865 }
866
show_in_min(struct device * dev,struct device_attribute * attr,char * buf)867 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
868 char *buf)
869 {
870 int nr = to_sensor_dev_attr(attr)->index;
871 struct lm85_data *data = lm85_update_device(dev);
872 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
873 }
874
set_in_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)875 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
876 const char *buf, size_t count)
877 {
878 int nr = to_sensor_dev_attr(attr)->index;
879 struct lm85_data *data = dev_get_drvdata(dev);
880 struct i2c_client *client = data->client;
881 long val;
882 int err;
883
884 err = kstrtol(buf, 10, &val);
885 if (err)
886 return err;
887
888 mutex_lock(&data->update_lock);
889 data->in_min[nr] = INS_TO_REG(nr, val);
890 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
891 mutex_unlock(&data->update_lock);
892 return count;
893 }
894
show_in_max(struct device * dev,struct device_attribute * attr,char * buf)895 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
896 char *buf)
897 {
898 int nr = to_sensor_dev_attr(attr)->index;
899 struct lm85_data *data = lm85_update_device(dev);
900 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
901 }
902
set_in_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)903 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
904 const char *buf, size_t count)
905 {
906 int nr = to_sensor_dev_attr(attr)->index;
907 struct lm85_data *data = dev_get_drvdata(dev);
908 struct i2c_client *client = data->client;
909 long val;
910 int err;
911
912 err = kstrtol(buf, 10, &val);
913 if (err)
914 return err;
915
916 mutex_lock(&data->update_lock);
917 data->in_max[nr] = INS_TO_REG(nr, val);
918 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
919 mutex_unlock(&data->update_lock);
920 return count;
921 }
922
923 #define show_in_reg(offset) \
924 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
925 show_in, NULL, offset); \
926 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
927 show_in_min, set_in_min, offset); \
928 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
929 show_in_max, set_in_max, offset)
930
931 show_in_reg(0);
932 show_in_reg(1);
933 show_in_reg(2);
934 show_in_reg(3);
935 show_in_reg(4);
936 show_in_reg(5);
937 show_in_reg(6);
938 show_in_reg(7);
939
940 /* Temps */
941
show_temp(struct device * dev,struct device_attribute * attr,char * buf)942 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
943 char *buf)
944 {
945 int nr = to_sensor_dev_attr(attr)->index;
946 struct lm85_data *data = lm85_update_device(dev);
947 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
948 data->temp_ext[nr]));
949 }
950
show_temp_min(struct device * dev,struct device_attribute * attr,char * buf)951 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
952 char *buf)
953 {
954 int nr = to_sensor_dev_attr(attr)->index;
955 struct lm85_data *data = lm85_update_device(dev);
956 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
957 }
958
set_temp_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)959 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
960 const char *buf, size_t count)
961 {
962 int nr = to_sensor_dev_attr(attr)->index;
963 struct lm85_data *data = dev_get_drvdata(dev);
964 struct i2c_client *client = data->client;
965 long val;
966 int err;
967
968 err = kstrtol(buf, 10, &val);
969 if (err)
970 return err;
971
972 if (IS_ADT7468_OFF64(data))
973 val += 64;
974
975 mutex_lock(&data->update_lock);
976 data->temp_min[nr] = TEMP_TO_REG(val);
977 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
978 mutex_unlock(&data->update_lock);
979 return count;
980 }
981
show_temp_max(struct device * dev,struct device_attribute * attr,char * buf)982 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
983 char *buf)
984 {
985 int nr = to_sensor_dev_attr(attr)->index;
986 struct lm85_data *data = lm85_update_device(dev);
987 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
988 }
989
set_temp_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)990 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
991 const char *buf, size_t count)
992 {
993 int nr = to_sensor_dev_attr(attr)->index;
994 struct lm85_data *data = dev_get_drvdata(dev);
995 struct i2c_client *client = data->client;
996 long val;
997 int err;
998
999 err = kstrtol(buf, 10, &val);
1000 if (err)
1001 return err;
1002
1003 if (IS_ADT7468_OFF64(data))
1004 val += 64;
1005
1006 mutex_lock(&data->update_lock);
1007 data->temp_max[nr] = TEMP_TO_REG(val);
1008 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
1009 mutex_unlock(&data->update_lock);
1010 return count;
1011 }
1012
1013 #define show_temp_reg(offset) \
1014 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
1015 show_temp, NULL, offset - 1); \
1016 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1017 show_temp_min, set_temp_min, offset - 1); \
1018 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1019 show_temp_max, set_temp_max, offset - 1);
1020
1021 show_temp_reg(1);
1022 show_temp_reg(2);
1023 show_temp_reg(3);
1024
1025
1026 /* Automatic PWM control */
1027
show_pwm_auto_channels(struct device * dev,struct device_attribute * attr,char * buf)1028 static ssize_t show_pwm_auto_channels(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1030 {
1031 int nr = to_sensor_dev_attr(attr)->index;
1032 struct lm85_data *data = lm85_update_device(dev);
1033 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
1034 }
1035
set_pwm_auto_channels(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1036 static ssize_t set_pwm_auto_channels(struct device *dev,
1037 struct device_attribute *attr, const char *buf, size_t count)
1038 {
1039 int nr = to_sensor_dev_attr(attr)->index;
1040 struct lm85_data *data = dev_get_drvdata(dev);
1041 struct i2c_client *client = data->client;
1042 long val;
1043 int err;
1044
1045 err = kstrtol(buf, 10, &val);
1046 if (err)
1047 return err;
1048
1049 mutex_lock(&data->update_lock);
1050 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
1051 | ZONE_TO_REG(val);
1052 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1053 data->autofan[nr].config);
1054 mutex_unlock(&data->update_lock);
1055 return count;
1056 }
1057
show_pwm_auto_pwm_min(struct device * dev,struct device_attribute * attr,char * buf)1058 static ssize_t show_pwm_auto_pwm_min(struct device *dev,
1059 struct device_attribute *attr, char *buf)
1060 {
1061 int nr = to_sensor_dev_attr(attr)->index;
1062 struct lm85_data *data = lm85_update_device(dev);
1063 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
1064 }
1065
set_pwm_auto_pwm_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1066 static ssize_t set_pwm_auto_pwm_min(struct device *dev,
1067 struct device_attribute *attr, const char *buf, size_t count)
1068 {
1069 int nr = to_sensor_dev_attr(attr)->index;
1070 struct lm85_data *data = dev_get_drvdata(dev);
1071 struct i2c_client *client = data->client;
1072 unsigned long val;
1073 int err;
1074
1075 err = kstrtoul(buf, 10, &val);
1076 if (err)
1077 return err;
1078
1079 mutex_lock(&data->update_lock);
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);
1083 mutex_unlock(&data->update_lock);
1084 return count;
1085 }
1086
show_pwm_auto_pwm_minctl(struct device * dev,struct device_attribute * attr,char * buf)1087 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
1088 struct device_attribute *attr, char *buf)
1089 {
1090 int nr = to_sensor_dev_attr(attr)->index;
1091 struct lm85_data *data = lm85_update_device(dev);
1092 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
1093 }
1094
set_pwm_auto_pwm_minctl(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1095 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
1096 struct device_attribute *attr, const char *buf, size_t count)
1097 {
1098 int nr = to_sensor_dev_attr(attr)->index;
1099 struct lm85_data *data = dev_get_drvdata(dev);
1100 struct i2c_client *client = data->client;
1101 u8 tmp;
1102 long val;
1103 int err;
1104
1105 err = kstrtol(buf, 10, &val);
1106 if (err)
1107 return err;
1108
1109 mutex_lock(&data->update_lock);
1110 data->autofan[nr].min_off = val;
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);
1116 mutex_unlock(&data->update_lock);
1117 return count;
1118 }
1119
1120 #define pwm_auto(offset) \
1121 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
1122 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
1123 set_pwm_auto_channels, offset - 1); \
1124 static 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); \
1127 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
1128 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
1129 set_pwm_auto_pwm_minctl, offset - 1)
1130
1131 pwm_auto(1);
1132 pwm_auto(2);
1133 pwm_auto(3);
1134
1135 /* Temperature settings for automatic PWM control */
1136
show_temp_auto_temp_off(struct device * dev,struct device_attribute * attr,char * buf)1137 static ssize_t show_temp_auto_temp_off(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1139 {
1140 int nr = to_sensor_dev_attr(attr)->index;
1141 struct lm85_data *data = lm85_update_device(dev);
1142 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
1143 HYST_FROM_REG(data->zone[nr].hyst));
1144 }
1145
set_temp_auto_temp_off(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1146 static ssize_t set_temp_auto_temp_off(struct device *dev,
1147 struct device_attribute *attr, const char *buf, size_t count)
1148 {
1149 int nr = to_sensor_dev_attr(attr)->index;
1150 struct lm85_data *data = dev_get_drvdata(dev);
1151 struct i2c_client *client = data->client;
1152 int min;
1153 long val;
1154 int err;
1155
1156 err = kstrtol(buf, 10, &val);
1157 if (err)
1158 return err;
1159
1160 mutex_lock(&data->update_lock);
1161 min = TEMP_FROM_REG(data->zone[nr].limit);
1162 data->zone[nr].hyst = HYST_TO_REG(min - val);
1163 if (nr == 0 || nr == 1) {
1164 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1165 (data->zone[0].hyst << 4)
1166 | data->zone[1].hyst);
1167 } else {
1168 lm85_write_value(client, LM85_REG_AFAN_HYST2,
1169 (data->zone[2].hyst << 4));
1170 }
1171 mutex_unlock(&data->update_lock);
1172 return count;
1173 }
1174
show_temp_auto_temp_min(struct device * dev,struct device_attribute * attr,char * buf)1175 static ssize_t show_temp_auto_temp_min(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1177 {
1178 int nr = to_sensor_dev_attr(attr)->index;
1179 struct lm85_data *data = lm85_update_device(dev);
1180 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
1181 }
1182
set_temp_auto_temp_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1183 static ssize_t set_temp_auto_temp_min(struct device *dev,
1184 struct device_attribute *attr, const char *buf, size_t count)
1185 {
1186 int nr = to_sensor_dev_attr(attr)->index;
1187 struct lm85_data *data = dev_get_drvdata(dev);
1188 struct i2c_client *client = data->client;
1189 long val;
1190 int err;
1191
1192 err = kstrtol(buf, 10, &val);
1193 if (err)
1194 return err;
1195
1196 mutex_lock(&data->update_lock);
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)
1207 | (data->pwm_freq[nr] & 0x07));
1208
1209 mutex_unlock(&data->update_lock);
1210 return count;
1211 }
1212
show_temp_auto_temp_max(struct device * dev,struct device_attribute * attr,char * buf)1213 static ssize_t show_temp_auto_temp_max(struct device *dev,
1214 struct device_attribute *attr, char *buf)
1215 {
1216 int nr = to_sensor_dev_attr(attr)->index;
1217 struct lm85_data *data = lm85_update_device(dev);
1218 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1219 RANGE_FROM_REG(data->zone[nr].range));
1220 }
1221
set_temp_auto_temp_max(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1222 static ssize_t set_temp_auto_temp_max(struct device *dev,
1223 struct device_attribute *attr, const char *buf, size_t count)
1224 {
1225 int nr = to_sensor_dev_attr(attr)->index;
1226 struct lm85_data *data = dev_get_drvdata(dev);
1227 struct i2c_client *client = data->client;
1228 int min;
1229 long val;
1230 int err;
1231
1232 err = kstrtol(buf, 10, &val);
1233 if (err)
1234 return err;
1235
1236 mutex_lock(&data->update_lock);
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)
1243 | (data->pwm_freq[nr] & 0x07));
1244 mutex_unlock(&data->update_lock);
1245 return count;
1246 }
1247
show_temp_auto_temp_crit(struct device * dev,struct device_attribute * attr,char * buf)1248 static ssize_t show_temp_auto_temp_crit(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1250 {
1251 int nr = to_sensor_dev_attr(attr)->index;
1252 struct lm85_data *data = lm85_update_device(dev);
1253 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1254 }
1255
set_temp_auto_temp_crit(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1256 static ssize_t set_temp_auto_temp_crit(struct device *dev,
1257 struct device_attribute *attr, const char *buf, size_t count)
1258 {
1259 int nr = to_sensor_dev_attr(attr)->index;
1260 struct lm85_data *data = dev_get_drvdata(dev);
1261 struct i2c_client *client = data->client;
1262 long val;
1263 int err;
1264
1265 err = kstrtol(buf, 10, &val);
1266 if (err)
1267 return err;
1268
1269 mutex_lock(&data->update_lock);
1270 data->zone[nr].critical = TEMP_TO_REG(val);
1271 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1272 data->zone[nr].critical);
1273 mutex_unlock(&data->update_lock);
1274 return count;
1275 }
1276
1277 #define temp_auto(offset) \
1278 static 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); \
1281 static 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); \
1284 static 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); \
1287 static 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
1291 temp_auto(1);
1292 temp_auto(2);
1293 temp_auto(3);
1294
1295 static struct attribute *lm85_attributes[] = {
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,
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,
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,
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,
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,
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,
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,
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,
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,
1357
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
1368 &dev_attr_vrm.attr,
1369 &dev_attr_cpu0_vid.attr,
1370 &dev_attr_alarms.attr,
1371 NULL
1372 };
1373
1374 static const struct attribute_group lm85_group = {
1375 .attrs = lm85_attributes,
1376 };
1377
1378 static 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,
1382 NULL
1383 };
1384
1385 static const struct attribute_group lm85_group_minctl = {
1386 .attrs = lm85_attributes_minctl,
1387 };
1388
1389 static 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,
1393 NULL
1394 };
1395
1396 static const struct attribute_group lm85_group_temp_off = {
1397 .attrs = lm85_attributes_temp_off,
1398 };
1399
1400 static struct attribute *lm85_attributes_in4[] = {
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,
1404 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1405 NULL
1406 };
1407
1408 static const struct attribute_group lm85_group_in4 = {
1409 .attrs = lm85_attributes_in4,
1410 };
1411
1412 static 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,
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,
1425 NULL
1426 };
1427
1428 static const struct attribute_group lm85_group_in567 = {
1429 .attrs = lm85_attributes_in567,
1430 };
1431
lm85_init_client(struct i2c_client * client)1432 static 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
lm85_is_fake(struct i2c_client * client)1450 static 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
1470 /* Return 0 if detection is successful, -ENODEV otherwise */
lm85_detect(struct i2c_client * client,struct i2c_board_info * info)1471 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1472 {
1473 struct i2c_adapter *adapter = client->adapter;
1474 int address = client->addr;
1475 const char *type_name = NULL;
1476 int company, verstep;
1477
1478 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1479 /* We need to be able to do byte I/O */
1480 return -ENODEV;
1481 }
1482
1483 /* Determine the chip type */
1484 company = lm85_read_value(client, LM85_REG_COMPANY);
1485 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1486
1487 dev_dbg(&adapter->dev,
1488 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1489 address, company, verstep);
1490
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;
1506 }
1507 type_name = "lm85";
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;
1523 }
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;
1534 case LM85_VERSTEP_EMC6D103_A0:
1535 case LM85_VERSTEP_EMC6D103_A1:
1536 type_name = "emc6d103";
1537 break;
1538 case LM85_VERSTEP_EMC6D103S:
1539 type_name = "emc6d103s";
1540 break;
1541 }
1542 }
1543
1544 if (!type_name)
1545 return -ENODEV;
1546
1547 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1548
1549 return 0;
1550 }
1551
lm85_probe(struct i2c_client * client,const struct i2c_device_id * id)1552 static int lm85_probe(struct i2c_client *client, const struct i2c_device_id *id)
1553 {
1554 struct device *dev = &client->dev;
1555 struct device *hwmon_dev;
1556 struct lm85_data *data;
1557 int idx = 0;
1558
1559 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
1560 if (!data)
1561 return -ENOMEM;
1562
1563 data->client = client;
1564 data->type = id->driver_data;
1565 mutex_init(&data->update_lock);
1566
1567 /* Fill in the chip specific driver values */
1568 switch (data->type) {
1569 case adm1027:
1570 case adt7463:
1571 case adt7468:
1572 case emc6d100:
1573 case emc6d102:
1574 case emc6d103:
1575 case emc6d103s:
1576 data->freq_map = adm1027_freq_map;
1577 break;
1578 default:
1579 data->freq_map = lm85_freq_map;
1580 }
1581
1582 /* Set the VRM version */
1583 data->vrm = vid_which_vrm();
1584
1585 /* Initialize the LM85 chip */
1586 lm85_init_client(client);
1587
1588 /* sysfs hooks */
1589 data->groups[idx++] = &lm85_group;
1590
1591 /* minctl and temp_off exist on all chips except emc6d103s */
1592 if (data->type != emc6d103s) {
1593 data->groups[idx++] = &lm85_group_minctl;
1594 data->groups[idx++] = &lm85_group_temp_off;
1595 }
1596
1597 /*
1598 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1599 * as a sixth digital VID input rather than an analog input.
1600 */
1601 if (data->type == adt7463 || data->type == adt7468) {
1602 u8 vid = lm85_read_value(client, LM85_REG_VID);
1603 if (vid & 0x80)
1604 data->has_vid5 = true;
1605 }
1606
1607 if (!data->has_vid5)
1608 data->groups[idx++] = &lm85_group_in4;
1609
1610 /* The EMC6D100 has 3 additional voltage inputs */
1611 if (data->type == emc6d100)
1612 data->groups[idx++] = &lm85_group_in567;
1613
1614 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1615 data, data->groups);
1616 return PTR_ERR_OR_ZERO(hwmon_dev);
1617 }
1618
1619 static const struct i2c_device_id lm85_id[] = {
1620 { "adm1027", adm1027 },
1621 { "adt7463", adt7463 },
1622 { "adt7468", adt7468 },
1623 { "lm85", lm85 },
1624 { "lm85b", lm85 },
1625 { "lm85c", lm85 },
1626 { "emc6d100", emc6d100 },
1627 { "emc6d101", emc6d100 },
1628 { "emc6d102", emc6d102 },
1629 { "emc6d103", emc6d103 },
1630 { "emc6d103s", emc6d103s },
1631 { }
1632 };
1633 MODULE_DEVICE_TABLE(i2c, lm85_id);
1634
1635 static struct i2c_driver lm85_driver = {
1636 .class = I2C_CLASS_HWMON,
1637 .driver = {
1638 .name = "lm85",
1639 },
1640 .probe = lm85_probe,
1641 .id_table = lm85_id,
1642 .detect = lm85_detect,
1643 .address_list = normal_i2c,
1644 };
1645
1646 module_i2c_driver(lm85_driver);
1647
1648 MODULE_LICENSE("GPL");
1649 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1650 "Margit Schubert-While <margitsw@t-online.de>, "
1651 "Justin Thiessen <jthiessen@penguincomputing.com>");
1652 MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1653