Loading...
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--2009 Jean Delvare <khali@linux-fr.org>
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 */
39static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40
41enum chips {
42 any_chip, lm85b, lm85c,
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_VMASK 0xf0
79#define LM85_VERSTEP_GENERIC 0x60
80#define LM85_VERSTEP_GENERIC2 0x70
81#define LM85_VERSTEP_LM85C 0x60
82#define LM85_VERSTEP_LM85B 0x62
83#define LM85_VERSTEP_LM96000_1 0x68
84#define LM85_VERSTEP_LM96000_2 0x69
85#define LM85_VERSTEP_ADM1027 0x60
86#define LM85_VERSTEP_ADT7463 0x62
87#define LM85_VERSTEP_ADT7463C 0x6A
88#define LM85_VERSTEP_ADT7468_1 0x71
89#define LM85_VERSTEP_ADT7468_2 0x72
90#define LM85_VERSTEP_EMC6D100_A0 0x60
91#define LM85_VERSTEP_EMC6D100_A1 0x61
92#define LM85_VERSTEP_EMC6D102 0x65
93#define LM85_VERSTEP_EMC6D103_A0 0x68
94#define LM85_VERSTEP_EMC6D103_A1 0x69
95#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
96
97#define LM85_REG_CONFIG 0x40
98
99#define LM85_REG_ALARM1 0x41
100#define LM85_REG_ALARM2 0x42
101
102#define LM85_REG_VID 0x43
103
104/* Automated FAN control */
105#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
106#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
107#define LM85_REG_AFAN_SPIKE1 0x62
108#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
109#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
110#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
111#define LM85_REG_AFAN_HYST1 0x6d
112#define LM85_REG_AFAN_HYST2 0x6e
113
114#define ADM1027_REG_EXTEND_ADC1 0x76
115#define ADM1027_REG_EXTEND_ADC2 0x77
116
117#define EMC6D100_REG_ALARM3 0x7d
118/* IN5, IN6 and IN7 */
119#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
120#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
121#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
122#define EMC6D102_REG_EXTEND_ADC1 0x85
123#define EMC6D102_REG_EXTEND_ADC2 0x86
124#define EMC6D102_REG_EXTEND_ADC3 0x87
125#define EMC6D102_REG_EXTEND_ADC4 0x88
126
127
128/* Conversions. Rounding and limit checking is only done on the TO_REG
129 variants. Note that you should be a bit careful with which arguments
130 these macros are called: arguments may be evaluated more than once.
131 */
132
133/* IN are scaled according to built-in resistors */
134static const int lm85_scaling[] = { /* .001 Volts */
135 2500, 2250, 3300, 5000, 12000,
136 3300, 1500, 1800 /*EMC6D100*/
137};
138#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
139
140#define INS_TO_REG(n, val) \
141 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
142
143#define INSEXT_FROM_REG(n, val, ext) \
144 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
145
146#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
147
148/* FAN speed is measured using 90kHz clock */
149static inline u16 FAN_TO_REG(unsigned long val)
150{
151 if (!val)
152 return 0xffff;
153 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
154}
155#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
156 5400000 / (val))
157
158/* Temperature is reported in .001 degC increments */
159#define TEMP_TO_REG(val) \
160 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
161#define TEMPEXT_FROM_REG(val, ext) \
162 SCALE(((val) << 4) + (ext), 16, 1000)
163#define TEMP_FROM_REG(val) ((val) * 1000)
164
165#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
166#define PWM_FROM_REG(val) (val)
167
168
169/* ZONEs have the following parameters:
170 * Limit (low) temp, 1. degC
171 * Hysteresis (below limit), 1. degC (0-15)
172 * Range of speed control, .1 degC (2-80)
173 * Critical (high) temp, 1. degC
174 *
175 * FAN PWMs have the following parameters:
176 * Reference Zone, 1, 2, 3, etc.
177 * Spinup time, .05 sec
178 * PWM value at limit/low temp, 1 count
179 * PWM Frequency, 1. Hz
180 * PWM is Min or OFF below limit, flag
181 * Invert PWM output, flag
182 *
183 * Some chips filter the temp, others the fan.
184 * Filter constant (or disabled) .1 seconds
185 */
186
187/* These are the zone temperature range encodings in .001 degree C */
188static const int lm85_range_map[] = {
189 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
190 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
191};
192
193static int RANGE_TO_REG(int range)
194{
195 int i;
196
197 /* Find the closest match */
198 for (i = 0; i < 15; ++i) {
199 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
200 break;
201 }
202
203 return i;
204}
205#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
206
207/* These are the PWM frequency encodings */
208static const int lm85_freq_map[8] = { /* 1 Hz */
209 10, 15, 23, 30, 38, 47, 61, 94
210};
211static const int adm1027_freq_map[8] = { /* 1 Hz */
212 11, 15, 22, 29, 35, 44, 59, 88
213};
214
215static int FREQ_TO_REG(const int *map, int freq)
216{
217 int i;
218
219 /* Find the closest match */
220 for (i = 0; i < 7; ++i)
221 if (freq <= (map[i] + map[i + 1]) / 2)
222 break;
223 return i;
224}
225
226static int FREQ_FROM_REG(const int *map, u8 reg)
227{
228 return map[reg & 0x07];
229}
230
231/* Since we can't use strings, I'm abusing these numbers
232 * to stand in for the following meanings:
233 * 1 -- PWM responds to Zone 1
234 * 2 -- PWM responds to Zone 2
235 * 3 -- PWM responds to Zone 3
236 * 23 -- PWM responds to the higher temp of Zone 2 or 3
237 * 123 -- PWM responds to highest of Zone 1, 2, or 3
238 * 0 -- PWM is always at 0% (ie, off)
239 * -1 -- PWM is always at 100%
240 * -2 -- PWM responds to manual control
241 */
242
243static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
244#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
245
246static int ZONE_TO_REG(int zone)
247{
248 int i;
249
250 for (i = 0; i <= 7; ++i)
251 if (zone == lm85_zone_map[i])
252 break;
253 if (i > 7) /* Not found. */
254 i = 3; /* Always 100% */
255 return i << 5;
256}
257
258#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
259#define HYST_FROM_REG(val) ((val) * 1000)
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/* LM85 can automatically adjust fan speeds based on temperature
278 * This structure encapsulates an entire Zone config. There are
279 * three zones (one for each temperature input) on the lm85
280 */
281struct lm85_zone {
282 s8 limit; /* Low temp limit */
283 u8 hyst; /* Low limit hysteresis. (0-15) */
284 u8 range; /* Temp range, encoded */
285 s8 critical; /* "All fans ON" temp limit */
286 u8 max_desired; /* Actual "max" temperature specified. Preserved
287 * to prevent "drift" as other autofan control
288 * values change.
289 */
290};
291
292struct lm85_autofan {
293 u8 config; /* Register value */
294 u8 min_pwm; /* Minimum PWM value, encoded */
295 u8 min_off; /* Min PWM or OFF below "limit", flag */
296};
297
298/* For each registered chip, we need to keep some data in memory.
299 The structure is dynamically allocated. */
300struct lm85_data {
301 struct device *hwmon_dev;
302 const int *freq_map;
303 enum chips type;
304
305 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
306
307 struct mutex update_lock;
308 int valid; /* !=0 if following fields are valid */
309 unsigned long last_reading; /* In jiffies */
310 unsigned long last_config; /* In jiffies */
311
312 u8 in[8]; /* Register value */
313 u8 in_max[8]; /* Register value */
314 u8 in_min[8]; /* Register value */
315 s8 temp[3]; /* Register value */
316 s8 temp_min[3]; /* Register value */
317 s8 temp_max[3]; /* Register value */
318 u16 fan[4]; /* Register value */
319 u16 fan_min[4]; /* Register value */
320 u8 pwm[3]; /* Register value */
321 u8 pwm_freq[3]; /* Register encoding */
322 u8 temp_ext[3]; /* Decoded values */
323 u8 in_ext[8]; /* Decoded values */
324 u8 vid; /* Register value */
325 u8 vrm; /* VRM version */
326 u32 alarms; /* Register encoding, combined */
327 u8 cfg5; /* Config Register 5 on ADT7468 */
328 struct lm85_autofan autofan[3];
329 struct lm85_zone zone[3];
330};
331
332static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
333static int lm85_probe(struct i2c_client *client,
334 const struct i2c_device_id *id);
335static int lm85_remove(struct i2c_client *client);
336
337static int lm85_read_value(struct i2c_client *client, u8 reg);
338static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
339static struct lm85_data *lm85_update_device(struct device *dev);
340
341
342static const struct i2c_device_id lm85_id[] = {
343 { "adm1027", adm1027 },
344 { "adt7463", adt7463 },
345 { "adt7468", adt7468 },
346 { "lm85", any_chip },
347 { "lm85b", lm85b },
348 { "lm85c", lm85c },
349 { "emc6d100", emc6d100 },
350 { "emc6d101", emc6d100 },
351 { "emc6d102", emc6d102 },
352 { "emc6d103", emc6d103 },
353 { "emc6d103s", emc6d103s },
354 { }
355};
356MODULE_DEVICE_TABLE(i2c, lm85_id);
357
358static struct i2c_driver lm85_driver = {
359 .class = I2C_CLASS_HWMON,
360 .driver = {
361 .name = "lm85",
362 },
363 .probe = lm85_probe,
364 .remove = lm85_remove,
365 .id_table = lm85_id,
366 .detect = lm85_detect,
367 .address_list = normal_i2c,
368};
369
370
371/* 4 Fans */
372static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
373 char *buf)
374{
375 int nr = to_sensor_dev_attr(attr)->index;
376 struct lm85_data *data = lm85_update_device(dev);
377 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
378}
379
380static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
381 char *buf)
382{
383 int nr = to_sensor_dev_attr(attr)->index;
384 struct lm85_data *data = lm85_update_device(dev);
385 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
386}
387
388static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
389 const char *buf, size_t count)
390{
391 int nr = to_sensor_dev_attr(attr)->index;
392 struct i2c_client *client = to_i2c_client(dev);
393 struct lm85_data *data = i2c_get_clientdata(client);
394 unsigned long val = simple_strtoul(buf, NULL, 10);
395
396 mutex_lock(&data->update_lock);
397 data->fan_min[nr] = FAN_TO_REG(val);
398 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
399 mutex_unlock(&data->update_lock);
400 return count;
401}
402
403#define show_fan_offset(offset) \
404static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
405 show_fan, NULL, offset - 1); \
406static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
407 show_fan_min, set_fan_min, offset - 1)
408
409show_fan_offset(1);
410show_fan_offset(2);
411show_fan_offset(3);
412show_fan_offset(4);
413
414/* vid, vrm, alarms */
415
416static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
417 char *buf)
418{
419 struct lm85_data *data = lm85_update_device(dev);
420 int vid;
421
422 if (data->has_vid5) {
423 /* 6-pin VID (VRM 10) */
424 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
425 } else {
426 /* 5-pin VID (VRM 9) */
427 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
428 }
429
430 return sprintf(buf, "%d\n", vid);
431}
432
433static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
434
435static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
436 char *buf)
437{
438 struct lm85_data *data = dev_get_drvdata(dev);
439 return sprintf(buf, "%ld\n", (long) data->vrm);
440}
441
442static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
443 const char *buf, size_t count)
444{
445 struct lm85_data *data = dev_get_drvdata(dev);
446 data->vrm = simple_strtoul(buf, NULL, 10);
447 return count;
448}
449
450static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
451
452static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
453 *attr, char *buf)
454{
455 struct lm85_data *data = lm85_update_device(dev);
456 return sprintf(buf, "%u\n", data->alarms);
457}
458
459static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
460
461static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
462 char *buf)
463{
464 int nr = to_sensor_dev_attr(attr)->index;
465 struct lm85_data *data = lm85_update_device(dev);
466 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
467}
468
469static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
470static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
471static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
472static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
473static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
474static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
475static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
476static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
477static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
478static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
479static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
480static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
481static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
482static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
483static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
484static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
485static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
486
487/* pwm */
488
489static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
490 char *buf)
491{
492 int nr = to_sensor_dev_attr(attr)->index;
493 struct lm85_data *data = lm85_update_device(dev);
494 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
495}
496
497static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
499{
500 int nr = to_sensor_dev_attr(attr)->index;
501 struct i2c_client *client = to_i2c_client(dev);
502 struct lm85_data *data = i2c_get_clientdata(client);
503 long val = simple_strtol(buf, NULL, 10);
504
505 mutex_lock(&data->update_lock);
506 data->pwm[nr] = PWM_TO_REG(val);
507 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
508 mutex_unlock(&data->update_lock);
509 return count;
510}
511
512static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
513 *attr, char *buf)
514{
515 int nr = to_sensor_dev_attr(attr)->index;
516 struct lm85_data *data = lm85_update_device(dev);
517 int pwm_zone, enable;
518
519 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
520 switch (pwm_zone) {
521 case -1: /* PWM is always at 100% */
522 enable = 0;
523 break;
524 case 0: /* PWM is always at 0% */
525 case -2: /* PWM responds to manual control */
526 enable = 1;
527 break;
528 default: /* PWM in automatic mode */
529 enable = 2;
530 }
531 return sprintf(buf, "%d\n", enable);
532}
533
534static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
535 *attr, const char *buf, size_t count)
536{
537 int nr = to_sensor_dev_attr(attr)->index;
538 struct i2c_client *client = to_i2c_client(dev);
539 struct lm85_data *data = i2c_get_clientdata(client);
540 long val = simple_strtol(buf, NULL, 10);
541 u8 config;
542
543 switch (val) {
544 case 0:
545 config = 3;
546 break;
547 case 1:
548 config = 7;
549 break;
550 case 2:
551 /* Here we have to choose arbitrarily one of the 5 possible
552 configurations; I go for the safest */
553 config = 6;
554 break;
555 default:
556 return -EINVAL;
557 }
558
559 mutex_lock(&data->update_lock);
560 data->autofan[nr].config = lm85_read_value(client,
561 LM85_REG_AFAN_CONFIG(nr));
562 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
563 | (config << 5);
564 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
565 data->autofan[nr].config);
566 mutex_unlock(&data->update_lock);
567 return count;
568}
569
570static ssize_t show_pwm_freq(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 int nr = to_sensor_dev_attr(attr)->index;
574 struct lm85_data *data = lm85_update_device(dev);
575 int freq;
576
577 if (IS_ADT7468_HFPWM(data))
578 freq = 22500;
579 else
580 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
581
582 return sprintf(buf, "%d\n", freq);
583}
584
585static ssize_t set_pwm_freq(struct device *dev,
586 struct device_attribute *attr, const char *buf, size_t count)
587{
588 int nr = to_sensor_dev_attr(attr)->index;
589 struct i2c_client *client = to_i2c_client(dev);
590 struct lm85_data *data = i2c_get_clientdata(client);
591 long val = simple_strtol(buf, NULL, 10);
592
593 mutex_lock(&data->update_lock);
594 /* The ADT7468 has a special high-frequency PWM output mode,
595 * where all PWM outputs are driven by a 22.5 kHz clock.
596 * This might confuse the user, but there's not much we can do. */
597 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
598 data->cfg5 &= ~ADT7468_HFPWM;
599 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
600 } else { /* Low freq. mode */
601 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
602 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
603 (data->zone[nr].range << 4)
604 | data->pwm_freq[nr]);
605 if (data->type == adt7468) {
606 data->cfg5 |= ADT7468_HFPWM;
607 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
608 }
609 }
610 mutex_unlock(&data->update_lock);
611 return count;
612}
613
614#define show_pwm_reg(offset) \
615static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
616 show_pwm, set_pwm, offset - 1); \
617static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
618 show_pwm_enable, set_pwm_enable, offset - 1); \
619static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
620 show_pwm_freq, set_pwm_freq, offset - 1)
621
622show_pwm_reg(1);
623show_pwm_reg(2);
624show_pwm_reg(3);
625
626/* Voltages */
627
628static ssize_t show_in(struct device *dev, struct device_attribute *attr,
629 char *buf)
630{
631 int nr = to_sensor_dev_attr(attr)->index;
632 struct lm85_data *data = lm85_update_device(dev);
633 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
634 data->in_ext[nr]));
635}
636
637static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
638 char *buf)
639{
640 int nr = to_sensor_dev_attr(attr)->index;
641 struct lm85_data *data = lm85_update_device(dev);
642 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
643}
644
645static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
646 const char *buf, size_t count)
647{
648 int nr = to_sensor_dev_attr(attr)->index;
649 struct i2c_client *client = to_i2c_client(dev);
650 struct lm85_data *data = i2c_get_clientdata(client);
651 long val = simple_strtol(buf, NULL, 10);
652
653 mutex_lock(&data->update_lock);
654 data->in_min[nr] = INS_TO_REG(nr, val);
655 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
656 mutex_unlock(&data->update_lock);
657 return count;
658}
659
660static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
661 char *buf)
662{
663 int nr = to_sensor_dev_attr(attr)->index;
664 struct lm85_data *data = lm85_update_device(dev);
665 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
666}
667
668static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
669 const char *buf, size_t count)
670{
671 int nr = to_sensor_dev_attr(attr)->index;
672 struct i2c_client *client = to_i2c_client(dev);
673 struct lm85_data *data = i2c_get_clientdata(client);
674 long val = simple_strtol(buf, NULL, 10);
675
676 mutex_lock(&data->update_lock);
677 data->in_max[nr] = INS_TO_REG(nr, val);
678 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
679 mutex_unlock(&data->update_lock);
680 return count;
681}
682
683#define show_in_reg(offset) \
684static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
685 show_in, NULL, offset); \
686static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
687 show_in_min, set_in_min, offset); \
688static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
689 show_in_max, set_in_max, offset)
690
691show_in_reg(0);
692show_in_reg(1);
693show_in_reg(2);
694show_in_reg(3);
695show_in_reg(4);
696show_in_reg(5);
697show_in_reg(6);
698show_in_reg(7);
699
700/* Temps */
701
702static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
703 char *buf)
704{
705 int nr = to_sensor_dev_attr(attr)->index;
706 struct lm85_data *data = lm85_update_device(dev);
707 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
708 data->temp_ext[nr]));
709}
710
711static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
712 char *buf)
713{
714 int nr = to_sensor_dev_attr(attr)->index;
715 struct lm85_data *data = lm85_update_device(dev);
716 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
717}
718
719static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
720 const char *buf, size_t count)
721{
722 int nr = to_sensor_dev_attr(attr)->index;
723 struct i2c_client *client = to_i2c_client(dev);
724 struct lm85_data *data = i2c_get_clientdata(client);
725 long val = simple_strtol(buf, NULL, 10);
726
727 if (IS_ADT7468_OFF64(data))
728 val += 64;
729
730 mutex_lock(&data->update_lock);
731 data->temp_min[nr] = TEMP_TO_REG(val);
732 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
733 mutex_unlock(&data->update_lock);
734 return count;
735}
736
737static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
738 char *buf)
739{
740 int nr = to_sensor_dev_attr(attr)->index;
741 struct lm85_data *data = lm85_update_device(dev);
742 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
743}
744
745static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
747{
748 int nr = to_sensor_dev_attr(attr)->index;
749 struct i2c_client *client = to_i2c_client(dev);
750 struct lm85_data *data = i2c_get_clientdata(client);
751 long val = simple_strtol(buf, NULL, 10);
752
753 if (IS_ADT7468_OFF64(data))
754 val += 64;
755
756 mutex_lock(&data->update_lock);
757 data->temp_max[nr] = TEMP_TO_REG(val);
758 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
759 mutex_unlock(&data->update_lock);
760 return count;
761}
762
763#define show_temp_reg(offset) \
764static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
765 show_temp, NULL, offset - 1); \
766static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
767 show_temp_min, set_temp_min, offset - 1); \
768static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
769 show_temp_max, set_temp_max, offset - 1);
770
771show_temp_reg(1);
772show_temp_reg(2);
773show_temp_reg(3);
774
775
776/* Automatic PWM control */
777
778static ssize_t show_pwm_auto_channels(struct device *dev,
779 struct device_attribute *attr, char *buf)
780{
781 int nr = to_sensor_dev_attr(attr)->index;
782 struct lm85_data *data = lm85_update_device(dev);
783 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
784}
785
786static ssize_t set_pwm_auto_channels(struct device *dev,
787 struct device_attribute *attr, const char *buf, size_t count)
788{
789 int nr = to_sensor_dev_attr(attr)->index;
790 struct i2c_client *client = to_i2c_client(dev);
791 struct lm85_data *data = i2c_get_clientdata(client);
792 long val = simple_strtol(buf, NULL, 10);
793
794 mutex_lock(&data->update_lock);
795 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
796 | ZONE_TO_REG(val);
797 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
798 data->autofan[nr].config);
799 mutex_unlock(&data->update_lock);
800 return count;
801}
802
803static ssize_t show_pwm_auto_pwm_min(struct device *dev,
804 struct device_attribute *attr, char *buf)
805{
806 int nr = to_sensor_dev_attr(attr)->index;
807 struct lm85_data *data = lm85_update_device(dev);
808 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
809}
810
811static ssize_t set_pwm_auto_pwm_min(struct device *dev,
812 struct device_attribute *attr, const char *buf, size_t count)
813{
814 int nr = to_sensor_dev_attr(attr)->index;
815 struct i2c_client *client = to_i2c_client(dev);
816 struct lm85_data *data = i2c_get_clientdata(client);
817 long val = simple_strtol(buf, NULL, 10);
818
819 mutex_lock(&data->update_lock);
820 data->autofan[nr].min_pwm = PWM_TO_REG(val);
821 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
822 data->autofan[nr].min_pwm);
823 mutex_unlock(&data->update_lock);
824 return count;
825}
826
827static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
828 struct device_attribute *attr, char *buf)
829{
830 int nr = to_sensor_dev_attr(attr)->index;
831 struct lm85_data *data = lm85_update_device(dev);
832 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
833}
834
835static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
836 struct device_attribute *attr, const char *buf, size_t count)
837{
838 int nr = to_sensor_dev_attr(attr)->index;
839 struct i2c_client *client = to_i2c_client(dev);
840 struct lm85_data *data = i2c_get_clientdata(client);
841 long val = simple_strtol(buf, NULL, 10);
842 u8 tmp;
843
844 mutex_lock(&data->update_lock);
845 data->autofan[nr].min_off = val;
846 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
847 tmp &= ~(0x20 << nr);
848 if (data->autofan[nr].min_off)
849 tmp |= 0x20 << nr;
850 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
851 mutex_unlock(&data->update_lock);
852 return count;
853}
854
855#define pwm_auto(offset) \
856static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
857 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
858 set_pwm_auto_channels, offset - 1); \
859static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
860 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
861 set_pwm_auto_pwm_min, offset - 1); \
862static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
863 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
864 set_pwm_auto_pwm_minctl, offset - 1)
865
866pwm_auto(1);
867pwm_auto(2);
868pwm_auto(3);
869
870/* Temperature settings for automatic PWM control */
871
872static ssize_t show_temp_auto_temp_off(struct device *dev,
873 struct device_attribute *attr, char *buf)
874{
875 int nr = to_sensor_dev_attr(attr)->index;
876 struct lm85_data *data = lm85_update_device(dev);
877 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
878 HYST_FROM_REG(data->zone[nr].hyst));
879}
880
881static ssize_t set_temp_auto_temp_off(struct device *dev,
882 struct device_attribute *attr, const char *buf, size_t count)
883{
884 int nr = to_sensor_dev_attr(attr)->index;
885 struct i2c_client *client = to_i2c_client(dev);
886 struct lm85_data *data = i2c_get_clientdata(client);
887 int min;
888 long val = simple_strtol(buf, NULL, 10);
889
890 mutex_lock(&data->update_lock);
891 min = TEMP_FROM_REG(data->zone[nr].limit);
892 data->zone[nr].hyst = HYST_TO_REG(min - val);
893 if (nr == 0 || nr == 1) {
894 lm85_write_value(client, LM85_REG_AFAN_HYST1,
895 (data->zone[0].hyst << 4)
896 | data->zone[1].hyst);
897 } else {
898 lm85_write_value(client, LM85_REG_AFAN_HYST2,
899 (data->zone[2].hyst << 4));
900 }
901 mutex_unlock(&data->update_lock);
902 return count;
903}
904
905static ssize_t show_temp_auto_temp_min(struct device *dev,
906 struct device_attribute *attr, char *buf)
907{
908 int nr = to_sensor_dev_attr(attr)->index;
909 struct lm85_data *data = lm85_update_device(dev);
910 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
911}
912
913static ssize_t set_temp_auto_temp_min(struct device *dev,
914 struct device_attribute *attr, const char *buf, size_t count)
915{
916 int nr = to_sensor_dev_attr(attr)->index;
917 struct i2c_client *client = to_i2c_client(dev);
918 struct lm85_data *data = i2c_get_clientdata(client);
919 long val = simple_strtol(buf, NULL, 10);
920
921 mutex_lock(&data->update_lock);
922 data->zone[nr].limit = TEMP_TO_REG(val);
923 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
924 data->zone[nr].limit);
925
926/* Update temp_auto_max and temp_auto_range */
927 data->zone[nr].range = RANGE_TO_REG(
928 TEMP_FROM_REG(data->zone[nr].max_desired) -
929 TEMP_FROM_REG(data->zone[nr].limit));
930 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
931 ((data->zone[nr].range & 0x0f) << 4)
932 | (data->pwm_freq[nr] & 0x07));
933
934 mutex_unlock(&data->update_lock);
935 return count;
936}
937
938static ssize_t show_temp_auto_temp_max(struct device *dev,
939 struct device_attribute *attr, char *buf)
940{
941 int nr = to_sensor_dev_attr(attr)->index;
942 struct lm85_data *data = lm85_update_device(dev);
943 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
944 RANGE_FROM_REG(data->zone[nr].range));
945}
946
947static ssize_t set_temp_auto_temp_max(struct device *dev,
948 struct device_attribute *attr, const char *buf, size_t count)
949{
950 int nr = to_sensor_dev_attr(attr)->index;
951 struct i2c_client *client = to_i2c_client(dev);
952 struct lm85_data *data = i2c_get_clientdata(client);
953 int min;
954 long val = simple_strtol(buf, NULL, 10);
955
956 mutex_lock(&data->update_lock);
957 min = TEMP_FROM_REG(data->zone[nr].limit);
958 data->zone[nr].max_desired = TEMP_TO_REG(val);
959 data->zone[nr].range = RANGE_TO_REG(
960 val - min);
961 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
962 ((data->zone[nr].range & 0x0f) << 4)
963 | (data->pwm_freq[nr] & 0x07));
964 mutex_unlock(&data->update_lock);
965 return count;
966}
967
968static ssize_t show_temp_auto_temp_crit(struct device *dev,
969 struct device_attribute *attr, char *buf)
970{
971 int nr = to_sensor_dev_attr(attr)->index;
972 struct lm85_data *data = lm85_update_device(dev);
973 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
974}
975
976static ssize_t set_temp_auto_temp_crit(struct device *dev,
977 struct device_attribute *attr, const char *buf, size_t count)
978{
979 int nr = to_sensor_dev_attr(attr)->index;
980 struct i2c_client *client = to_i2c_client(dev);
981 struct lm85_data *data = i2c_get_clientdata(client);
982 long val = simple_strtol(buf, NULL, 10);
983
984 mutex_lock(&data->update_lock);
985 data->zone[nr].critical = TEMP_TO_REG(val);
986 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
987 data->zone[nr].critical);
988 mutex_unlock(&data->update_lock);
989 return count;
990}
991
992#define temp_auto(offset) \
993static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
994 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
995 set_temp_auto_temp_off, offset - 1); \
996static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
997 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
998 set_temp_auto_temp_min, offset - 1); \
999static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1000 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1001 set_temp_auto_temp_max, offset - 1); \
1002static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1003 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1004 set_temp_auto_temp_crit, offset - 1);
1005
1006temp_auto(1);
1007temp_auto(2);
1008temp_auto(3);
1009
1010static struct attribute *lm85_attributes[] = {
1011 &sensor_dev_attr_fan1_input.dev_attr.attr,
1012 &sensor_dev_attr_fan2_input.dev_attr.attr,
1013 &sensor_dev_attr_fan3_input.dev_attr.attr,
1014 &sensor_dev_attr_fan4_input.dev_attr.attr,
1015 &sensor_dev_attr_fan1_min.dev_attr.attr,
1016 &sensor_dev_attr_fan2_min.dev_attr.attr,
1017 &sensor_dev_attr_fan3_min.dev_attr.attr,
1018 &sensor_dev_attr_fan4_min.dev_attr.attr,
1019 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1020 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1021 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1022 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1023
1024 &sensor_dev_attr_pwm1.dev_attr.attr,
1025 &sensor_dev_attr_pwm2.dev_attr.attr,
1026 &sensor_dev_attr_pwm3.dev_attr.attr,
1027 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1028 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1029 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1030 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1031 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1032 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1033
1034 &sensor_dev_attr_in0_input.dev_attr.attr,
1035 &sensor_dev_attr_in1_input.dev_attr.attr,
1036 &sensor_dev_attr_in2_input.dev_attr.attr,
1037 &sensor_dev_attr_in3_input.dev_attr.attr,
1038 &sensor_dev_attr_in0_min.dev_attr.attr,
1039 &sensor_dev_attr_in1_min.dev_attr.attr,
1040 &sensor_dev_attr_in2_min.dev_attr.attr,
1041 &sensor_dev_attr_in3_min.dev_attr.attr,
1042 &sensor_dev_attr_in0_max.dev_attr.attr,
1043 &sensor_dev_attr_in1_max.dev_attr.attr,
1044 &sensor_dev_attr_in2_max.dev_attr.attr,
1045 &sensor_dev_attr_in3_max.dev_attr.attr,
1046 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1047 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1048 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1049 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1050
1051 &sensor_dev_attr_temp1_input.dev_attr.attr,
1052 &sensor_dev_attr_temp2_input.dev_attr.attr,
1053 &sensor_dev_attr_temp3_input.dev_attr.attr,
1054 &sensor_dev_attr_temp1_min.dev_attr.attr,
1055 &sensor_dev_attr_temp2_min.dev_attr.attr,
1056 &sensor_dev_attr_temp3_min.dev_attr.attr,
1057 &sensor_dev_attr_temp1_max.dev_attr.attr,
1058 &sensor_dev_attr_temp2_max.dev_attr.attr,
1059 &sensor_dev_attr_temp3_max.dev_attr.attr,
1060 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1061 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1062 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1063 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1064 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1065
1066 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1067 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1068 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1069 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1070 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1071 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1072
1073 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1074 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1075 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1076 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1077 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1078 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1079 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1080 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1081 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1082
1083 &dev_attr_vrm.attr,
1084 &dev_attr_cpu0_vid.attr,
1085 &dev_attr_alarms.attr,
1086 NULL
1087};
1088
1089static const struct attribute_group lm85_group = {
1090 .attrs = lm85_attributes,
1091};
1092
1093static struct attribute *lm85_attributes_minctl[] = {
1094 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1095 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1096 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1097 NULL
1098};
1099
1100static const struct attribute_group lm85_group_minctl = {
1101 .attrs = lm85_attributes_minctl,
1102};
1103
1104static struct attribute *lm85_attributes_temp_off[] = {
1105 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1106 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1107 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1108 NULL
1109};
1110
1111static const struct attribute_group lm85_group_temp_off = {
1112 .attrs = lm85_attributes_temp_off,
1113};
1114
1115static struct attribute *lm85_attributes_in4[] = {
1116 &sensor_dev_attr_in4_input.dev_attr.attr,
1117 &sensor_dev_attr_in4_min.dev_attr.attr,
1118 &sensor_dev_attr_in4_max.dev_attr.attr,
1119 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1120 NULL
1121};
1122
1123static const struct attribute_group lm85_group_in4 = {
1124 .attrs = lm85_attributes_in4,
1125};
1126
1127static struct attribute *lm85_attributes_in567[] = {
1128 &sensor_dev_attr_in5_input.dev_attr.attr,
1129 &sensor_dev_attr_in6_input.dev_attr.attr,
1130 &sensor_dev_attr_in7_input.dev_attr.attr,
1131 &sensor_dev_attr_in5_min.dev_attr.attr,
1132 &sensor_dev_attr_in6_min.dev_attr.attr,
1133 &sensor_dev_attr_in7_min.dev_attr.attr,
1134 &sensor_dev_attr_in5_max.dev_attr.attr,
1135 &sensor_dev_attr_in6_max.dev_attr.attr,
1136 &sensor_dev_attr_in7_max.dev_attr.attr,
1137 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1138 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1139 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1140 NULL
1141};
1142
1143static const struct attribute_group lm85_group_in567 = {
1144 .attrs = lm85_attributes_in567,
1145};
1146
1147static void lm85_init_client(struct i2c_client *client)
1148{
1149 int value;
1150
1151 /* Start monitoring if needed */
1152 value = lm85_read_value(client, LM85_REG_CONFIG);
1153 if (!(value & 0x01)) {
1154 dev_info(&client->dev, "Starting monitoring\n");
1155 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1156 }
1157
1158 /* Warn about unusual configuration bits */
1159 if (value & 0x02)
1160 dev_warn(&client->dev, "Device configuration is locked\n");
1161 if (!(value & 0x04))
1162 dev_warn(&client->dev, "Device is not ready\n");
1163}
1164
1165static int lm85_is_fake(struct i2c_client *client)
1166{
1167 /*
1168 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1169 * emulate the former except that it has no hardware monitoring function
1170 * so the readings are always 0.
1171 */
1172 int i;
1173 u8 in_temp, fan;
1174
1175 for (i = 0; i < 8; i++) {
1176 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1177 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1178 if (in_temp != 0x00 || fan != 0xff)
1179 return 0;
1180 }
1181
1182 return 1;
1183}
1184
1185/* Return 0 if detection is successful, -ENODEV otherwise */
1186static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1187{
1188 struct i2c_adapter *adapter = client->adapter;
1189 int address = client->addr;
1190 const char *type_name;
1191 int company, verstep;
1192
1193 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1194 /* We need to be able to do byte I/O */
1195 return -ENODEV;
1196 }
1197
1198 /* Determine the chip type */
1199 company = lm85_read_value(client, LM85_REG_COMPANY);
1200 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1201
1202 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1203 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1204 address, company, verstep);
1205
1206 /* All supported chips have the version in common */
1207 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1208 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1209 dev_dbg(&adapter->dev,
1210 "Autodetection failed: unsupported version\n");
1211 return -ENODEV;
1212 }
1213 type_name = "lm85";
1214
1215 /* Now, refine the detection */
1216 if (company == LM85_COMPANY_NATIONAL) {
1217 switch (verstep) {
1218 case LM85_VERSTEP_LM85C:
1219 type_name = "lm85c";
1220 break;
1221 case LM85_VERSTEP_LM85B:
1222 type_name = "lm85b";
1223 break;
1224 case LM85_VERSTEP_LM96000_1:
1225 case LM85_VERSTEP_LM96000_2:
1226 /* Check for Winbond WPCD377I */
1227 if (lm85_is_fake(client)) {
1228 dev_dbg(&adapter->dev,
1229 "Found Winbond WPCD377I, ignoring\n");
1230 return -ENODEV;
1231 }
1232 break;
1233 }
1234 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1235 switch (verstep) {
1236 case LM85_VERSTEP_ADM1027:
1237 type_name = "adm1027";
1238 break;
1239 case LM85_VERSTEP_ADT7463:
1240 case LM85_VERSTEP_ADT7463C:
1241 type_name = "adt7463";
1242 break;
1243 case LM85_VERSTEP_ADT7468_1:
1244 case LM85_VERSTEP_ADT7468_2:
1245 type_name = "adt7468";
1246 break;
1247 }
1248 } else if (company == LM85_COMPANY_SMSC) {
1249 switch (verstep) {
1250 case LM85_VERSTEP_EMC6D100_A0:
1251 case LM85_VERSTEP_EMC6D100_A1:
1252 /* Note: we can't tell a '100 from a '101 */
1253 type_name = "emc6d100";
1254 break;
1255 case LM85_VERSTEP_EMC6D102:
1256 type_name = "emc6d102";
1257 break;
1258 case LM85_VERSTEP_EMC6D103_A0:
1259 case LM85_VERSTEP_EMC6D103_A1:
1260 type_name = "emc6d103";
1261 break;
1262 case LM85_VERSTEP_EMC6D103S:
1263 type_name = "emc6d103s";
1264 break;
1265 }
1266 } else {
1267 dev_dbg(&adapter->dev,
1268 "Autodetection failed: unknown vendor\n");
1269 return -ENODEV;
1270 }
1271
1272 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1273
1274 return 0;
1275}
1276
1277static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1278{
1279 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1280 if (data->type != emc6d103s) {
1281 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1282 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1283 }
1284 if (!data->has_vid5)
1285 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1286 if (data->type == emc6d100)
1287 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1288}
1289
1290static int lm85_probe(struct i2c_client *client,
1291 const struct i2c_device_id *id)
1292{
1293 struct lm85_data *data;
1294 int err;
1295
1296 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1297 if (!data)
1298 return -ENOMEM;
1299
1300 i2c_set_clientdata(client, data);
1301 data->type = id->driver_data;
1302 mutex_init(&data->update_lock);
1303
1304 /* Fill in the chip specific driver values */
1305 switch (data->type) {
1306 case adm1027:
1307 case adt7463:
1308 case adt7468:
1309 case emc6d100:
1310 case emc6d102:
1311 case emc6d103:
1312 case emc6d103s:
1313 data->freq_map = adm1027_freq_map;
1314 break;
1315 default:
1316 data->freq_map = lm85_freq_map;
1317 }
1318
1319 /* Set the VRM version */
1320 data->vrm = vid_which_vrm();
1321
1322 /* Initialize the LM85 chip */
1323 lm85_init_client(client);
1324
1325 /* Register sysfs hooks */
1326 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1327 if (err)
1328 goto err_kfree;
1329
1330 /* minctl and temp_off exist on all chips except emc6d103s */
1331 if (data->type != emc6d103s) {
1332 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1333 if (err)
1334 goto err_remove_files;
1335 err = sysfs_create_group(&client->dev.kobj,
1336 &lm85_group_temp_off);
1337 if (err)
1338 goto err_remove_files;
1339 }
1340
1341 /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1342 as a sixth digital VID input rather than an analog input. */
1343 if (data->type == adt7463 || data->type == adt7468) {
1344 u8 vid = lm85_read_value(client, LM85_REG_VID);
1345 if (vid & 0x80)
1346 data->has_vid5 = true;
1347 }
1348
1349 if (!data->has_vid5)
1350 if ((err = sysfs_create_group(&client->dev.kobj,
1351 &lm85_group_in4)))
1352 goto err_remove_files;
1353
1354 /* The EMC6D100 has 3 additional voltage inputs */
1355 if (data->type == emc6d100)
1356 if ((err = sysfs_create_group(&client->dev.kobj,
1357 &lm85_group_in567)))
1358 goto err_remove_files;
1359
1360 data->hwmon_dev = hwmon_device_register(&client->dev);
1361 if (IS_ERR(data->hwmon_dev)) {
1362 err = PTR_ERR(data->hwmon_dev);
1363 goto err_remove_files;
1364 }
1365
1366 return 0;
1367
1368 /* Error out and cleanup code */
1369 err_remove_files:
1370 lm85_remove_files(client, data);
1371 err_kfree:
1372 kfree(data);
1373 return err;
1374}
1375
1376static int lm85_remove(struct i2c_client *client)
1377{
1378 struct lm85_data *data = i2c_get_clientdata(client);
1379 hwmon_device_unregister(data->hwmon_dev);
1380 lm85_remove_files(client, data);
1381 kfree(data);
1382 return 0;
1383}
1384
1385
1386static int lm85_read_value(struct i2c_client *client, u8 reg)
1387{
1388 int res;
1389
1390 /* What size location is it? */
1391 switch (reg) {
1392 case LM85_REG_FAN(0): /* Read WORD data */
1393 case LM85_REG_FAN(1):
1394 case LM85_REG_FAN(2):
1395 case LM85_REG_FAN(3):
1396 case LM85_REG_FAN_MIN(0):
1397 case LM85_REG_FAN_MIN(1):
1398 case LM85_REG_FAN_MIN(2):
1399 case LM85_REG_FAN_MIN(3):
1400 case LM85_REG_ALARM1: /* Read both bytes at once */
1401 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1402 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1403 break;
1404 default: /* Read BYTE data */
1405 res = i2c_smbus_read_byte_data(client, reg);
1406 break;
1407 }
1408
1409 return res;
1410}
1411
1412static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
1413{
1414 switch (reg) {
1415 case LM85_REG_FAN(0): /* Write WORD data */
1416 case LM85_REG_FAN(1):
1417 case LM85_REG_FAN(2):
1418 case LM85_REG_FAN(3):
1419 case LM85_REG_FAN_MIN(0):
1420 case LM85_REG_FAN_MIN(1):
1421 case LM85_REG_FAN_MIN(2):
1422 case LM85_REG_FAN_MIN(3):
1423 /* NOTE: ALARM is read only, so not included here */
1424 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1425 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
1426 break;
1427 default: /* Write BYTE data */
1428 i2c_smbus_write_byte_data(client, reg, value);
1429 break;
1430 }
1431}
1432
1433static struct lm85_data *lm85_update_device(struct device *dev)
1434{
1435 struct i2c_client *client = to_i2c_client(dev);
1436 struct lm85_data *data = i2c_get_clientdata(client);
1437 int i;
1438
1439 mutex_lock(&data->update_lock);
1440
1441 if (!data->valid ||
1442 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
1443 /* Things that change quickly */
1444 dev_dbg(&client->dev, "Reading sensor values\n");
1445
1446 /* Have to read extended bits first to "freeze" the
1447 * more significant bits that are read later.
1448 * There are 2 additional resolution bits per channel and we
1449 * have room for 4, so we shift them to the left.
1450 */
1451 if (data->type == adm1027 || data->type == adt7463 ||
1452 data->type == adt7468) {
1453 int ext1 = lm85_read_value(client,
1454 ADM1027_REG_EXTEND_ADC1);
1455 int ext2 = lm85_read_value(client,
1456 ADM1027_REG_EXTEND_ADC2);
1457 int val = (ext1 << 8) + ext2;
1458
1459 for (i = 0; i <= 4; i++)
1460 data->in_ext[i] =
1461 ((val >> (i * 2)) & 0x03) << 2;
1462
1463 for (i = 0; i <= 2; i++)
1464 data->temp_ext[i] =
1465 (val >> ((i + 4) * 2)) & 0x0c;
1466 }
1467
1468 data->vid = lm85_read_value(client, LM85_REG_VID);
1469
1470 for (i = 0; i <= 3; ++i) {
1471 data->in[i] =
1472 lm85_read_value(client, LM85_REG_IN(i));
1473 data->fan[i] =
1474 lm85_read_value(client, LM85_REG_FAN(i));
1475 }
1476
1477 if (!data->has_vid5)
1478 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
1479
1480 if (data->type == adt7468)
1481 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1482
1483 for (i = 0; i <= 2; ++i) {
1484 data->temp[i] =
1485 lm85_read_value(client, LM85_REG_TEMP(i));
1486 data->pwm[i] =
1487 lm85_read_value(client, LM85_REG_PWM(i));
1488
1489 if (IS_ADT7468_OFF64(data))
1490 data->temp[i] -= 64;
1491 }
1492
1493 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1494
1495 if (data->type == emc6d100) {
1496 /* Three more voltage sensors */
1497 for (i = 5; i <= 7; ++i) {
1498 data->in[i] = lm85_read_value(client,
1499 EMC6D100_REG_IN(i));
1500 }
1501 /* More alarm bits */
1502 data->alarms |= lm85_read_value(client,
1503 EMC6D100_REG_ALARM3) << 16;
1504 } else if (data->type == emc6d102 || data->type == emc6d103 ||
1505 data->type == emc6d103s) {
1506 /* Have to read LSB bits after the MSB ones because
1507 the reading of the MSB bits has frozen the
1508 LSBs (backward from the ADM1027).
1509 */
1510 int ext1 = lm85_read_value(client,
1511 EMC6D102_REG_EXTEND_ADC1);
1512 int ext2 = lm85_read_value(client,
1513 EMC6D102_REG_EXTEND_ADC2);
1514 int ext3 = lm85_read_value(client,
1515 EMC6D102_REG_EXTEND_ADC3);
1516 int ext4 = lm85_read_value(client,
1517 EMC6D102_REG_EXTEND_ADC4);
1518 data->in_ext[0] = ext3 & 0x0f;
1519 data->in_ext[1] = ext4 & 0x0f;
1520 data->in_ext[2] = ext4 >> 4;
1521 data->in_ext[3] = ext3 >> 4;
1522 data->in_ext[4] = ext2 >> 4;
1523
1524 data->temp_ext[0] = ext1 & 0x0f;
1525 data->temp_ext[1] = ext2 & 0x0f;
1526 data->temp_ext[2] = ext1 >> 4;
1527 }
1528
1529 data->last_reading = jiffies;
1530 } /* last_reading */
1531
1532 if (!data->valid ||
1533 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
1534 /* Things that don't change often */
1535 dev_dbg(&client->dev, "Reading config values\n");
1536
1537 for (i = 0; i <= 3; ++i) {
1538 data->in_min[i] =
1539 lm85_read_value(client, LM85_REG_IN_MIN(i));
1540 data->in_max[i] =
1541 lm85_read_value(client, LM85_REG_IN_MAX(i));
1542 data->fan_min[i] =
1543 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1544 }
1545
1546 if (!data->has_vid5) {
1547 data->in_min[4] = lm85_read_value(client,
1548 LM85_REG_IN_MIN(4));
1549 data->in_max[4] = lm85_read_value(client,
1550 LM85_REG_IN_MAX(4));
1551 }
1552
1553 if (data->type == emc6d100) {
1554 for (i = 5; i <= 7; ++i) {
1555 data->in_min[i] = lm85_read_value(client,
1556 EMC6D100_REG_IN_MIN(i));
1557 data->in_max[i] = lm85_read_value(client,
1558 EMC6D100_REG_IN_MAX(i));
1559 }
1560 }
1561
1562 for (i = 0; i <= 2; ++i) {
1563 int val;
1564
1565 data->temp_min[i] =
1566 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1567 data->temp_max[i] =
1568 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1569
1570 data->autofan[i].config =
1571 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1572 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1573 data->pwm_freq[i] = val & 0x07;
1574 data->zone[i].range = val >> 4;
1575 data->autofan[i].min_pwm =
1576 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1577 data->zone[i].limit =
1578 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1579 data->zone[i].critical =
1580 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1581
1582 if (IS_ADT7468_OFF64(data)) {
1583 data->temp_min[i] -= 64;
1584 data->temp_max[i] -= 64;
1585 data->zone[i].limit -= 64;
1586 data->zone[i].critical -= 64;
1587 }
1588 }
1589
1590 if (data->type != emc6d103s) {
1591 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1592 data->autofan[0].min_off = (i & 0x20) != 0;
1593 data->autofan[1].min_off = (i & 0x40) != 0;
1594 data->autofan[2].min_off = (i & 0x80) != 0;
1595
1596 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1597 data->zone[0].hyst = i >> 4;
1598 data->zone[1].hyst = i & 0x0f;
1599
1600 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1601 data->zone[2].hyst = i >> 4;
1602 }
1603
1604 data->last_config = jiffies;
1605 } /* last_config */
1606
1607 data->valid = 1;
1608
1609 mutex_unlock(&data->update_lock);
1610
1611 return data;
1612}
1613
1614
1615static int __init sm_lm85_init(void)
1616{
1617 return i2c_add_driver(&lm85_driver);
1618}
1619
1620static void __exit sm_lm85_exit(void)
1621{
1622 i2c_del_driver(&lm85_driver);
1623}
1624
1625MODULE_LICENSE("GPL");
1626MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1627 "Margit Schubert-While <margitsw@t-online.de>, "
1628 "Justin Thiessen <jthiessen@penguincomputing.com>");
1629MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1630
1631module_init(sm_lm85_init);
1632module_exit(sm_lm85_exit);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * lm85.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
6 * Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
7 * Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
8 * Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
9 * Copyright (C) 2007--2014 Jean Delvare <jdelvare@suse.de>
10 *
11 * Chip details at <http://www.national.com/ds/LM/LM85.pdf>
12 */
13
14#include <linux/module.h>
15#include <linux/of_device.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/jiffies.h>
19#include <linux/i2c.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-vid.h>
22#include <linux/hwmon-sysfs.h>
23#include <linux/err.h>
24#include <linux/mutex.h>
25#include <linux/util_macros.h>
26
27/* Addresses to scan */
28static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
29
30enum chips {
31 lm85, lm96000,
32 adm1027, adt7463, adt7468,
33 emc6d100, emc6d102, emc6d103, emc6d103s
34};
35
36/* The LM85 registers */
37
38#define LM85_REG_IN(nr) (0x20 + (nr))
39#define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
40#define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
41
42#define LM85_REG_TEMP(nr) (0x25 + (nr))
43#define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
44#define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
45
46/* Fan speeds are LSB, MSB (2 bytes) */
47#define LM85_REG_FAN(nr) (0x28 + (nr) * 2)
48#define LM85_REG_FAN_MIN(nr) (0x54 + (nr) * 2)
49
50#define LM85_REG_PWM(nr) (0x30 + (nr))
51
52#define LM85_REG_COMPANY 0x3e
53#define LM85_REG_VERSTEP 0x3f
54
55#define ADT7468_REG_CFG5 0x7c
56#define ADT7468_OFF64 (1 << 0)
57#define ADT7468_HFPWM (1 << 1)
58#define IS_ADT7468_OFF64(data) \
59 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
60#define IS_ADT7468_HFPWM(data) \
61 ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_HFPWM))
62
63/* These are the recognized values for the above regs */
64#define LM85_COMPANY_NATIONAL 0x01
65#define LM85_COMPANY_ANALOG_DEV 0x41
66#define LM85_COMPANY_SMSC 0x5c
67#define LM85_VERSTEP_LM85C 0x60
68#define LM85_VERSTEP_LM85B 0x62
69#define LM85_VERSTEP_LM96000_1 0x68
70#define LM85_VERSTEP_LM96000_2 0x69
71#define LM85_VERSTEP_ADM1027 0x60
72#define LM85_VERSTEP_ADT7463 0x62
73#define LM85_VERSTEP_ADT7463C 0x6A
74#define LM85_VERSTEP_ADT7468_1 0x71
75#define LM85_VERSTEP_ADT7468_2 0x72
76#define LM85_VERSTEP_EMC6D100_A0 0x60
77#define LM85_VERSTEP_EMC6D100_A1 0x61
78#define LM85_VERSTEP_EMC6D102 0x65
79#define LM85_VERSTEP_EMC6D103_A0 0x68
80#define LM85_VERSTEP_EMC6D103_A1 0x69
81#define LM85_VERSTEP_EMC6D103S 0x6A /* Also known as EMC6D103:A2 */
82
83#define LM85_REG_CONFIG 0x40
84
85#define LM85_REG_ALARM1 0x41
86#define LM85_REG_ALARM2 0x42
87
88#define LM85_REG_VID 0x43
89
90/* Automated FAN control */
91#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
92#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
93#define LM85_REG_AFAN_SPIKE1 0x62
94#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
95#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
96#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
97#define LM85_REG_AFAN_HYST1 0x6d
98#define LM85_REG_AFAN_HYST2 0x6e
99
100#define ADM1027_REG_EXTEND_ADC1 0x76
101#define ADM1027_REG_EXTEND_ADC2 0x77
102
103#define EMC6D100_REG_ALARM3 0x7d
104/* IN5, IN6 and IN7 */
105#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
106#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
107#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
108#define EMC6D102_REG_EXTEND_ADC1 0x85
109#define EMC6D102_REG_EXTEND_ADC2 0x86
110#define EMC6D102_REG_EXTEND_ADC3 0x87
111#define EMC6D102_REG_EXTEND_ADC4 0x88
112
113/*
114 * Conversions. Rounding and limit checking is only done on the TO_REG
115 * variants. Note that you should be a bit careful with which arguments
116 * these macros are called: arguments may be evaluated more than once.
117 */
118
119/* IN are scaled according to built-in resistors */
120static const int lm85_scaling[] = { /* .001 Volts */
121 2500, 2250, 3300, 5000, 12000,
122 3300, 1500, 1800 /*EMC6D100*/
123};
124#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
125
126#define INS_TO_REG(n, val) \
127 SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
128 lm85_scaling[n], 192)
129
130#define INSEXT_FROM_REG(n, val, ext) \
131 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
132
133#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
134
135/* FAN speed is measured using 90kHz clock */
136static inline u16 FAN_TO_REG(unsigned long val)
137{
138 if (!val)
139 return 0xffff;
140 return clamp_val(5400000 / val, 1, 0xfffe);
141}
142#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
143 5400000 / (val))
144
145/* Temperature is reported in .001 degC increments */
146#define TEMP_TO_REG(val) \
147 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
148#define TEMPEXT_FROM_REG(val, ext) \
149 SCALE(((val) << 4) + (ext), 16, 1000)
150#define TEMP_FROM_REG(val) ((val) * 1000)
151
152#define PWM_TO_REG(val) clamp_val(val, 0, 255)
153#define PWM_FROM_REG(val) (val)
154
155/*
156 * ZONEs have the following parameters:
157 * Limit (low) temp, 1. degC
158 * Hysteresis (below limit), 1. degC (0-15)
159 * Range of speed control, .1 degC (2-80)
160 * Critical (high) temp, 1. degC
161 *
162 * FAN PWMs have the following parameters:
163 * Reference Zone, 1, 2, 3, etc.
164 * Spinup time, .05 sec
165 * PWM value at limit/low temp, 1 count
166 * PWM Frequency, 1. Hz
167 * PWM is Min or OFF below limit, flag
168 * Invert PWM output, flag
169 *
170 * Some chips filter the temp, others the fan.
171 * Filter constant (or disabled) .1 seconds
172 */
173
174/* These are the zone temperature range encodings in .001 degree C */
175static const int lm85_range_map[] = {
176 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
177 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
178};
179
180static int RANGE_TO_REG(long range)
181{
182 return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map));
183}
184#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
185
186/* These are the PWM frequency encodings */
187static const int lm85_freq_map[] = { /* 1 Hz */
188 10, 15, 23, 30, 38, 47, 61, 94
189};
190
191static const int lm96000_freq_map[] = { /* 1 Hz */
192 10, 15, 23, 30, 38, 47, 61, 94,
193 22500, 24000, 25700, 25700, 27700, 27700, 30000, 30000
194};
195
196static const int adm1027_freq_map[] = { /* 1 Hz */
197 11, 15, 22, 29, 35, 44, 59, 88
198};
199
200static int FREQ_TO_REG(const int *map,
201 unsigned int map_size, unsigned long freq)
202{
203 return find_closest(freq, map, map_size);
204}
205
206static int FREQ_FROM_REG(const int *map, unsigned int map_size, u8 reg)
207{
208 return map[reg % map_size];
209}
210
211/*
212 * Since we can't use strings, I'm abusing these numbers
213 * to stand in for the following meanings:
214 * 1 -- PWM responds to Zone 1
215 * 2 -- PWM responds to Zone 2
216 * 3 -- PWM responds to Zone 3
217 * 23 -- PWM responds to the higher temp of Zone 2 or 3
218 * 123 -- PWM responds to highest of Zone 1, 2, or 3
219 * 0 -- PWM is always at 0% (ie, off)
220 * -1 -- PWM is always at 100%
221 * -2 -- PWM responds to manual control
222 */
223
224static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
225#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
226
227static int ZONE_TO_REG(int zone)
228{
229 int i;
230
231 for (i = 0; i <= 7; ++i)
232 if (zone == lm85_zone_map[i])
233 break;
234 if (i > 7) /* Not found. */
235 i = 3; /* Always 100% */
236 return i << 5;
237}
238
239#define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
240#define HYST_FROM_REG(val) ((val) * 1000)
241
242/*
243 * Chip sampling rates
244 *
245 * Some sensors are not updated more frequently than once per second
246 * so it doesn't make sense to read them more often than that.
247 * We cache the results and return the saved data if the driver
248 * is called again before a second has elapsed.
249 *
250 * Also, there is significant configuration data for this chip
251 * given the automatic PWM fan control that is possible. There
252 * are about 47 bytes of config data to only 22 bytes of actual
253 * readings. So, we keep the config data up to date in the cache
254 * when it is written and only sample it once every 1 *minute*
255 */
256#define LM85_DATA_INTERVAL (HZ + HZ / 2)
257#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
258
259/*
260 * LM85 can automatically adjust fan speeds based on temperature
261 * This structure encapsulates an entire Zone config. There are
262 * three zones (one for each temperature input) on the lm85
263 */
264struct lm85_zone {
265 s8 limit; /* Low temp limit */
266 u8 hyst; /* Low limit hysteresis. (0-15) */
267 u8 range; /* Temp range, encoded */
268 s8 critical; /* "All fans ON" temp limit */
269 u8 max_desired; /*
270 * Actual "max" temperature specified. Preserved
271 * to prevent "drift" as other autofan control
272 * values change.
273 */
274};
275
276struct lm85_autofan {
277 u8 config; /* Register value */
278 u8 min_pwm; /* Minimum PWM value, encoded */
279 u8 min_off; /* Min PWM or OFF below "limit", flag */
280};
281
282/*
283 * For each registered chip, we need to keep some data in memory.
284 * The structure is dynamically allocated.
285 */
286struct lm85_data {
287 struct i2c_client *client;
288 const struct attribute_group *groups[6];
289 const int *freq_map;
290 unsigned int freq_map_size;
291
292 enum chips type;
293
294 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
295
296 struct mutex update_lock;
297 bool valid; /* true if following fields are valid */
298 unsigned long last_reading; /* In jiffies */
299 unsigned long last_config; /* In jiffies */
300
301 u8 in[8]; /* Register value */
302 u8 in_max[8]; /* Register value */
303 u8 in_min[8]; /* Register value */
304 s8 temp[3]; /* Register value */
305 s8 temp_min[3]; /* Register value */
306 s8 temp_max[3]; /* Register value */
307 u16 fan[4]; /* Register value */
308 u16 fan_min[4]; /* Register value */
309 u8 pwm[3]; /* Register value */
310 u8 pwm_freq[3]; /* Register encoding */
311 u8 temp_ext[3]; /* Decoded values */
312 u8 in_ext[8]; /* Decoded values */
313 u8 vid; /* Register value */
314 u8 vrm; /* VRM version */
315 u32 alarms; /* Register encoding, combined */
316 u8 cfg5; /* Config Register 5 on ADT7468 */
317 struct lm85_autofan autofan[3];
318 struct lm85_zone zone[3];
319};
320
321static int lm85_read_value(struct i2c_client *client, u8 reg)
322{
323 int res;
324
325 /* What size location is it? */
326 switch (reg) {
327 case LM85_REG_FAN(0): /* Read WORD data */
328 case LM85_REG_FAN(1):
329 case LM85_REG_FAN(2):
330 case LM85_REG_FAN(3):
331 case LM85_REG_FAN_MIN(0):
332 case LM85_REG_FAN_MIN(1):
333 case LM85_REG_FAN_MIN(2):
334 case LM85_REG_FAN_MIN(3):
335 case LM85_REG_ALARM1: /* Read both bytes at once */
336 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
337 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
338 break;
339 default: /* Read BYTE data */
340 res = i2c_smbus_read_byte_data(client, reg);
341 break;
342 }
343
344 return res;
345}
346
347static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
348{
349 switch (reg) {
350 case LM85_REG_FAN(0): /* Write WORD data */
351 case LM85_REG_FAN(1):
352 case LM85_REG_FAN(2):
353 case LM85_REG_FAN(3):
354 case LM85_REG_FAN_MIN(0):
355 case LM85_REG_FAN_MIN(1):
356 case LM85_REG_FAN_MIN(2):
357 case LM85_REG_FAN_MIN(3):
358 /* NOTE: ALARM is read only, so not included here */
359 i2c_smbus_write_byte_data(client, reg, value & 0xff);
360 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
361 break;
362 default: /* Write BYTE data */
363 i2c_smbus_write_byte_data(client, reg, value);
364 break;
365 }
366}
367
368static struct lm85_data *lm85_update_device(struct device *dev)
369{
370 struct lm85_data *data = dev_get_drvdata(dev);
371 struct i2c_client *client = data->client;
372 int i;
373
374 mutex_lock(&data->update_lock);
375
376 if (!data->valid ||
377 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
378 /* Things that change quickly */
379 dev_dbg(&client->dev, "Reading sensor values\n");
380
381 /*
382 * Have to read extended bits first to "freeze" the
383 * more significant bits that are read later.
384 * There are 2 additional resolution bits per channel and we
385 * have room for 4, so we shift them to the left.
386 */
387 if (data->type == adm1027 || data->type == adt7463 ||
388 data->type == adt7468) {
389 int ext1 = lm85_read_value(client,
390 ADM1027_REG_EXTEND_ADC1);
391 int ext2 = lm85_read_value(client,
392 ADM1027_REG_EXTEND_ADC2);
393 int val = (ext1 << 8) + ext2;
394
395 for (i = 0; i <= 4; i++)
396 data->in_ext[i] =
397 ((val >> (i * 2)) & 0x03) << 2;
398
399 for (i = 0; i <= 2; i++)
400 data->temp_ext[i] =
401 (val >> ((i + 4) * 2)) & 0x0c;
402 }
403
404 data->vid = lm85_read_value(client, LM85_REG_VID);
405
406 for (i = 0; i <= 3; ++i) {
407 data->in[i] =
408 lm85_read_value(client, LM85_REG_IN(i));
409 data->fan[i] =
410 lm85_read_value(client, LM85_REG_FAN(i));
411 }
412
413 if (!data->has_vid5)
414 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
415
416 if (data->type == adt7468)
417 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
418
419 for (i = 0; i <= 2; ++i) {
420 data->temp[i] =
421 lm85_read_value(client, LM85_REG_TEMP(i));
422 data->pwm[i] =
423 lm85_read_value(client, LM85_REG_PWM(i));
424
425 if (IS_ADT7468_OFF64(data))
426 data->temp[i] -= 64;
427 }
428
429 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
430
431 if (data->type == emc6d100) {
432 /* Three more voltage sensors */
433 for (i = 5; i <= 7; ++i) {
434 data->in[i] = lm85_read_value(client,
435 EMC6D100_REG_IN(i));
436 }
437 /* More alarm bits */
438 data->alarms |= lm85_read_value(client,
439 EMC6D100_REG_ALARM3) << 16;
440 } else if (data->type == emc6d102 || data->type == emc6d103 ||
441 data->type == emc6d103s) {
442 /*
443 * Have to read LSB bits after the MSB ones because
444 * the reading of the MSB bits has frozen the
445 * LSBs (backward from the ADM1027).
446 */
447 int ext1 = lm85_read_value(client,
448 EMC6D102_REG_EXTEND_ADC1);
449 int ext2 = lm85_read_value(client,
450 EMC6D102_REG_EXTEND_ADC2);
451 int ext3 = lm85_read_value(client,
452 EMC6D102_REG_EXTEND_ADC3);
453 int ext4 = lm85_read_value(client,
454 EMC6D102_REG_EXTEND_ADC4);
455 data->in_ext[0] = ext3 & 0x0f;
456 data->in_ext[1] = ext4 & 0x0f;
457 data->in_ext[2] = ext4 >> 4;
458 data->in_ext[3] = ext3 >> 4;
459 data->in_ext[4] = ext2 >> 4;
460
461 data->temp_ext[0] = ext1 & 0x0f;
462 data->temp_ext[1] = ext2 & 0x0f;
463 data->temp_ext[2] = ext1 >> 4;
464 }
465
466 data->last_reading = jiffies;
467 } /* last_reading */
468
469 if (!data->valid ||
470 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
471 /* Things that don't change often */
472 dev_dbg(&client->dev, "Reading config values\n");
473
474 for (i = 0; i <= 3; ++i) {
475 data->in_min[i] =
476 lm85_read_value(client, LM85_REG_IN_MIN(i));
477 data->in_max[i] =
478 lm85_read_value(client, LM85_REG_IN_MAX(i));
479 data->fan_min[i] =
480 lm85_read_value(client, LM85_REG_FAN_MIN(i));
481 }
482
483 if (!data->has_vid5) {
484 data->in_min[4] = lm85_read_value(client,
485 LM85_REG_IN_MIN(4));
486 data->in_max[4] = lm85_read_value(client,
487 LM85_REG_IN_MAX(4));
488 }
489
490 if (data->type == emc6d100) {
491 for (i = 5; i <= 7; ++i) {
492 data->in_min[i] = lm85_read_value(client,
493 EMC6D100_REG_IN_MIN(i));
494 data->in_max[i] = lm85_read_value(client,
495 EMC6D100_REG_IN_MAX(i));
496 }
497 }
498
499 for (i = 0; i <= 2; ++i) {
500 int val;
501
502 data->temp_min[i] =
503 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
504 data->temp_max[i] =
505 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
506
507 data->autofan[i].config =
508 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
509 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
510 data->pwm_freq[i] = val % data->freq_map_size;
511 data->zone[i].range = val >> 4;
512 data->autofan[i].min_pwm =
513 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
514 data->zone[i].limit =
515 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
516 data->zone[i].critical =
517 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
518
519 if (IS_ADT7468_OFF64(data)) {
520 data->temp_min[i] -= 64;
521 data->temp_max[i] -= 64;
522 data->zone[i].limit -= 64;
523 data->zone[i].critical -= 64;
524 }
525 }
526
527 if (data->type != emc6d103s) {
528 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
529 data->autofan[0].min_off = (i & 0x20) != 0;
530 data->autofan[1].min_off = (i & 0x40) != 0;
531 data->autofan[2].min_off = (i & 0x80) != 0;
532
533 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
534 data->zone[0].hyst = i >> 4;
535 data->zone[1].hyst = i & 0x0f;
536
537 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
538 data->zone[2].hyst = i >> 4;
539 }
540
541 data->last_config = jiffies;
542 } /* last_config */
543
544 data->valid = true;
545
546 mutex_unlock(&data->update_lock);
547
548 return data;
549}
550
551/* 4 Fans */
552static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
553 char *buf)
554{
555 int nr = to_sensor_dev_attr(attr)->index;
556 struct lm85_data *data = lm85_update_device(dev);
557 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
558}
559
560static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
561 char *buf)
562{
563 int nr = to_sensor_dev_attr(attr)->index;
564 struct lm85_data *data = lm85_update_device(dev);
565 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
566}
567
568static ssize_t fan_min_store(struct device *dev,
569 struct device_attribute *attr, const char *buf,
570 size_t count)
571{
572 int nr = to_sensor_dev_attr(attr)->index;
573 struct lm85_data *data = dev_get_drvdata(dev);
574 struct i2c_client *client = data->client;
575 unsigned long val;
576 int err;
577
578 err = kstrtoul(buf, 10, &val);
579 if (err)
580 return err;
581
582 mutex_lock(&data->update_lock);
583 data->fan_min[nr] = FAN_TO_REG(val);
584 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
585 mutex_unlock(&data->update_lock);
586 return count;
587}
588
589static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
590static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
591static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
592static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
593static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
594static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
595static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
596static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
597
598/* vid, vrm, alarms */
599
600static ssize_t cpu0_vid_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
603 struct lm85_data *data = lm85_update_device(dev);
604 int vid;
605
606 if (data->has_vid5) {
607 /* 6-pin VID (VRM 10) */
608 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
609 } else {
610 /* 5-pin VID (VRM 9) */
611 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
612 }
613
614 return sprintf(buf, "%d\n", vid);
615}
616
617static DEVICE_ATTR_RO(cpu0_vid);
618
619static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
620 char *buf)
621{
622 struct lm85_data *data = dev_get_drvdata(dev);
623 return sprintf(buf, "%ld\n", (long) data->vrm);
624}
625
626static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
628{
629 struct lm85_data *data = dev_get_drvdata(dev);
630 unsigned long val;
631 int err;
632
633 err = kstrtoul(buf, 10, &val);
634 if (err)
635 return err;
636
637 if (val > 255)
638 return -EINVAL;
639
640 data->vrm = val;
641 return count;
642}
643
644static DEVICE_ATTR_RW(vrm);
645
646static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
647 char *buf)
648{
649 struct lm85_data *data = lm85_update_device(dev);
650 return sprintf(buf, "%u\n", data->alarms);
651}
652
653static DEVICE_ATTR_RO(alarms);
654
655static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
656 char *buf)
657{
658 int nr = to_sensor_dev_attr(attr)->index;
659 struct lm85_data *data = lm85_update_device(dev);
660 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
661}
662
663static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
664static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
665static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
666static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
667static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 8);
668static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 18);
669static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 16);
670static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm, 17);
671static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
672static SENSOR_DEVICE_ATTR_RO(temp1_fault, alarm, 14);
673static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
674static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 6);
675static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 15);
676static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 10);
677static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 11);
678static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, 12);
679static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, 13);
680
681/* pwm */
682
683static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
684 char *buf)
685{
686 int nr = to_sensor_dev_attr(attr)->index;
687 struct lm85_data *data = lm85_update_device(dev);
688 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
689}
690
691static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
692 const char *buf, size_t count)
693{
694 int nr = to_sensor_dev_attr(attr)->index;
695 struct lm85_data *data = dev_get_drvdata(dev);
696 struct i2c_client *client = data->client;
697 unsigned long val;
698 int err;
699
700 err = kstrtoul(buf, 10, &val);
701 if (err)
702 return err;
703
704 mutex_lock(&data->update_lock);
705 data->pwm[nr] = PWM_TO_REG(val);
706 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
707 mutex_unlock(&data->update_lock);
708 return count;
709}
710
711static ssize_t pwm_enable_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
713{
714 int nr = to_sensor_dev_attr(attr)->index;
715 struct lm85_data *data = lm85_update_device(dev);
716 int pwm_zone, enable;
717
718 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
719 switch (pwm_zone) {
720 case -1: /* PWM is always at 100% */
721 enable = 0;
722 break;
723 case 0: /* PWM is always at 0% */
724 case -2: /* PWM responds to manual control */
725 enable = 1;
726 break;
727 default: /* PWM in automatic mode */
728 enable = 2;
729 }
730 return sprintf(buf, "%d\n", enable);
731}
732
733static ssize_t pwm_enable_store(struct device *dev,
734 struct device_attribute *attr,
735 const char *buf, size_t count)
736{
737 int nr = to_sensor_dev_attr(attr)->index;
738 struct lm85_data *data = dev_get_drvdata(dev);
739 struct i2c_client *client = data->client;
740 u8 config;
741 unsigned long val;
742 int err;
743
744 err = kstrtoul(buf, 10, &val);
745 if (err)
746 return err;
747
748 switch (val) {
749 case 0:
750 config = 3;
751 break;
752 case 1:
753 config = 7;
754 break;
755 case 2:
756 /*
757 * Here we have to choose arbitrarily one of the 5 possible
758 * configurations; I go for the safest
759 */
760 config = 6;
761 break;
762 default:
763 return -EINVAL;
764 }
765
766 mutex_lock(&data->update_lock);
767 data->autofan[nr].config = lm85_read_value(client,
768 LM85_REG_AFAN_CONFIG(nr));
769 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
770 | (config << 5);
771 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
772 data->autofan[nr].config);
773 mutex_unlock(&data->update_lock);
774 return count;
775}
776
777static ssize_t pwm_freq_show(struct device *dev,
778 struct device_attribute *attr, char *buf)
779{
780 int nr = to_sensor_dev_attr(attr)->index;
781 struct lm85_data *data = lm85_update_device(dev);
782 int freq;
783
784 if (IS_ADT7468_HFPWM(data))
785 freq = 22500;
786 else
787 freq = FREQ_FROM_REG(data->freq_map, data->freq_map_size,
788 data->pwm_freq[nr]);
789
790 return sprintf(buf, "%d\n", freq);
791}
792
793static ssize_t pwm_freq_store(struct device *dev,
794 struct device_attribute *attr, const char *buf,
795 size_t count)
796{
797 int nr = to_sensor_dev_attr(attr)->index;
798 struct lm85_data *data = dev_get_drvdata(dev);
799 struct i2c_client *client = data->client;
800 unsigned long val;
801 int err;
802
803 err = kstrtoul(buf, 10, &val);
804 if (err)
805 return err;
806
807 mutex_lock(&data->update_lock);
808 /*
809 * The ADT7468 has a special high-frequency PWM output mode,
810 * where all PWM outputs are driven by a 22.5 kHz clock.
811 * This might confuse the user, but there's not much we can do.
812 */
813 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
814 data->cfg5 &= ~ADT7468_HFPWM;
815 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
816 } else { /* Low freq. mode */
817 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map,
818 data->freq_map_size, val);
819 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
820 (data->zone[nr].range << 4)
821 | data->pwm_freq[nr]);
822 if (data->type == adt7468) {
823 data->cfg5 |= ADT7468_HFPWM;
824 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
825 }
826 }
827 mutex_unlock(&data->update_lock);
828 return count;
829}
830
831static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
832static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
833static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
834static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
835static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
836static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
837static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
838static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_enable, 2);
839static SENSOR_DEVICE_ATTR_RW(pwm3_freq, pwm_freq, 2);
840
841/* Voltages */
842
843static ssize_t in_show(struct device *dev, struct device_attribute *attr,
844 char *buf)
845{
846 int nr = to_sensor_dev_attr(attr)->index;
847 struct lm85_data *data = lm85_update_device(dev);
848 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
849 data->in_ext[nr]));
850}
851
852static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
853 char *buf)
854{
855 int nr = to_sensor_dev_attr(attr)->index;
856 struct lm85_data *data = lm85_update_device(dev);
857 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
858}
859
860static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
861 const char *buf, size_t count)
862{
863 int nr = to_sensor_dev_attr(attr)->index;
864 struct lm85_data *data = dev_get_drvdata(dev);
865 struct i2c_client *client = data->client;
866 long val;
867 int err;
868
869 err = kstrtol(buf, 10, &val);
870 if (err)
871 return err;
872
873 mutex_lock(&data->update_lock);
874 data->in_min[nr] = INS_TO_REG(nr, val);
875 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
876 mutex_unlock(&data->update_lock);
877 return count;
878}
879
880static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
881 char *buf)
882{
883 int nr = to_sensor_dev_attr(attr)->index;
884 struct lm85_data *data = lm85_update_device(dev);
885 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
886}
887
888static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
889 const char *buf, size_t count)
890{
891 int nr = to_sensor_dev_attr(attr)->index;
892 struct lm85_data *data = dev_get_drvdata(dev);
893 struct i2c_client *client = data->client;
894 long val;
895 int err;
896
897 err = kstrtol(buf, 10, &val);
898 if (err)
899 return err;
900
901 mutex_lock(&data->update_lock);
902 data->in_max[nr] = INS_TO_REG(nr, val);
903 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
904 mutex_unlock(&data->update_lock);
905 return count;
906}
907
908static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
909static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
910static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
911static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
912static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
913static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
914static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
915static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
916static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
917static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
918static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
919static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
920static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
921static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
922static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
923static SENSOR_DEVICE_ATTR_RO(in5_input, in, 5);
924static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 5);
925static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 5);
926static SENSOR_DEVICE_ATTR_RO(in6_input, in, 6);
927static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 6);
928static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 6);
929static SENSOR_DEVICE_ATTR_RO(in7_input, in, 7);
930static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 7);
931static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 7);
932
933/* Temps */
934
935static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
936 char *buf)
937{
938 int nr = to_sensor_dev_attr(attr)->index;
939 struct lm85_data *data = lm85_update_device(dev);
940 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
941 data->temp_ext[nr]));
942}
943
944static ssize_t temp_min_show(struct device *dev,
945 struct device_attribute *attr, char *buf)
946{
947 int nr = to_sensor_dev_attr(attr)->index;
948 struct lm85_data *data = lm85_update_device(dev);
949 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
950}
951
952static ssize_t temp_min_store(struct device *dev,
953 struct device_attribute *attr, const char *buf,
954 size_t count)
955{
956 int nr = to_sensor_dev_attr(attr)->index;
957 struct lm85_data *data = dev_get_drvdata(dev);
958 struct i2c_client *client = data->client;
959 long val;
960 int err;
961
962 err = kstrtol(buf, 10, &val);
963 if (err)
964 return err;
965
966 if (IS_ADT7468_OFF64(data))
967 val += 64;
968
969 mutex_lock(&data->update_lock);
970 data->temp_min[nr] = TEMP_TO_REG(val);
971 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
972 mutex_unlock(&data->update_lock);
973 return count;
974}
975
976static ssize_t temp_max_show(struct device *dev,
977 struct device_attribute *attr, char *buf)
978{
979 int nr = to_sensor_dev_attr(attr)->index;
980 struct lm85_data *data = lm85_update_device(dev);
981 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
982}
983
984static ssize_t temp_max_store(struct device *dev,
985 struct device_attribute *attr, const char *buf,
986 size_t count)
987{
988 int nr = to_sensor_dev_attr(attr)->index;
989 struct lm85_data *data = dev_get_drvdata(dev);
990 struct i2c_client *client = data->client;
991 long val;
992 int err;
993
994 err = kstrtol(buf, 10, &val);
995 if (err)
996 return err;
997
998 if (IS_ADT7468_OFF64(data))
999 val += 64;
1000
1001 mutex_lock(&data->update_lock);
1002 data->temp_max[nr] = TEMP_TO_REG(val);
1003 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
1004 mutex_unlock(&data->update_lock);
1005 return count;
1006}
1007
1008static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1009static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1010static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1011static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1012static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1013static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1014static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1015static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1016static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
1017
1018/* Automatic PWM control */
1019
1020static ssize_t pwm_auto_channels_show(struct device *dev,
1021 struct device_attribute *attr,
1022 char *buf)
1023{
1024 int nr = to_sensor_dev_attr(attr)->index;
1025 struct lm85_data *data = lm85_update_device(dev);
1026 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
1027}
1028
1029static ssize_t pwm_auto_channels_store(struct device *dev,
1030 struct device_attribute *attr,
1031 const char *buf, size_t count)
1032{
1033 int nr = to_sensor_dev_attr(attr)->index;
1034 struct lm85_data *data = dev_get_drvdata(dev);
1035 struct i2c_client *client = data->client;
1036 long val;
1037 int err;
1038
1039 err = kstrtol(buf, 10, &val);
1040 if (err)
1041 return err;
1042
1043 mutex_lock(&data->update_lock);
1044 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
1045 | ZONE_TO_REG(val);
1046 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
1047 data->autofan[nr].config);
1048 mutex_unlock(&data->update_lock);
1049 return count;
1050}
1051
1052static ssize_t pwm_auto_pwm_min_show(struct device *dev,
1053 struct device_attribute *attr, char *buf)
1054{
1055 int nr = to_sensor_dev_attr(attr)->index;
1056 struct lm85_data *data = lm85_update_device(dev);
1057 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
1058}
1059
1060static ssize_t pwm_auto_pwm_min_store(struct device *dev,
1061 struct device_attribute *attr,
1062 const char *buf, size_t count)
1063{
1064 int nr = to_sensor_dev_attr(attr)->index;
1065 struct lm85_data *data = dev_get_drvdata(dev);
1066 struct i2c_client *client = data->client;
1067 unsigned long val;
1068 int err;
1069
1070 err = kstrtoul(buf, 10, &val);
1071 if (err)
1072 return err;
1073
1074 mutex_lock(&data->update_lock);
1075 data->autofan[nr].min_pwm = PWM_TO_REG(val);
1076 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
1077 data->autofan[nr].min_pwm);
1078 mutex_unlock(&data->update_lock);
1079 return count;
1080}
1081
1082static ssize_t pwm_auto_pwm_minctl_show(struct device *dev,
1083 struct device_attribute *attr,
1084 char *buf)
1085{
1086 int nr = to_sensor_dev_attr(attr)->index;
1087 struct lm85_data *data = lm85_update_device(dev);
1088 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
1089}
1090
1091static ssize_t pwm_auto_pwm_minctl_store(struct device *dev,
1092 struct device_attribute *attr,
1093 const char *buf, size_t count)
1094{
1095 int nr = to_sensor_dev_attr(attr)->index;
1096 struct lm85_data *data = dev_get_drvdata(dev);
1097 struct i2c_client *client = data->client;
1098 u8 tmp;
1099 long val;
1100 int err;
1101
1102 err = kstrtol(buf, 10, &val);
1103 if (err)
1104 return err;
1105
1106 mutex_lock(&data->update_lock);
1107 data->autofan[nr].min_off = val;
1108 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1109 tmp &= ~(0x20 << nr);
1110 if (data->autofan[nr].min_off)
1111 tmp |= 0x20 << nr;
1112 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
1113 mutex_unlock(&data->update_lock);
1114 return count;
1115}
1116
1117static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels, pwm_auto_channels, 0);
1118static SENSOR_DEVICE_ATTR_RW(pwm1_auto_pwm_min, pwm_auto_pwm_min, 0);
1119static SENSOR_DEVICE_ATTR_RW(pwm1_auto_pwm_minctl, pwm_auto_pwm_minctl, 0);
1120static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels, pwm_auto_channels, 1);
1121static SENSOR_DEVICE_ATTR_RW(pwm2_auto_pwm_min, pwm_auto_pwm_min, 1);
1122static SENSOR_DEVICE_ATTR_RW(pwm2_auto_pwm_minctl, pwm_auto_pwm_minctl, 1);
1123static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels, pwm_auto_channels, 2);
1124static SENSOR_DEVICE_ATTR_RW(pwm3_auto_pwm_min, pwm_auto_pwm_min, 2);
1125static SENSOR_DEVICE_ATTR_RW(pwm3_auto_pwm_minctl, pwm_auto_pwm_minctl, 2);
1126
1127/* Temperature settings for automatic PWM control */
1128
1129static ssize_t temp_auto_temp_off_show(struct device *dev,
1130 struct device_attribute *attr,
1131 char *buf)
1132{
1133 int nr = to_sensor_dev_attr(attr)->index;
1134 struct lm85_data *data = lm85_update_device(dev);
1135 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
1136 HYST_FROM_REG(data->zone[nr].hyst));
1137}
1138
1139static ssize_t temp_auto_temp_off_store(struct device *dev,
1140 struct device_attribute *attr,
1141 const char *buf, size_t count)
1142{
1143 int nr = to_sensor_dev_attr(attr)->index;
1144 struct lm85_data *data = dev_get_drvdata(dev);
1145 struct i2c_client *client = data->client;
1146 int min;
1147 long val;
1148 int err;
1149
1150 err = kstrtol(buf, 10, &val);
1151 if (err)
1152 return err;
1153
1154 mutex_lock(&data->update_lock);
1155 min = TEMP_FROM_REG(data->zone[nr].limit);
1156 data->zone[nr].hyst = HYST_TO_REG(min - val);
1157 if (nr == 0 || nr == 1) {
1158 lm85_write_value(client, LM85_REG_AFAN_HYST1,
1159 (data->zone[0].hyst << 4)
1160 | data->zone[1].hyst);
1161 } else {
1162 lm85_write_value(client, LM85_REG_AFAN_HYST2,
1163 (data->zone[2].hyst << 4));
1164 }
1165 mutex_unlock(&data->update_lock);
1166 return count;
1167}
1168
1169static ssize_t temp_auto_temp_min_show(struct device *dev,
1170 struct device_attribute *attr,
1171 char *buf)
1172{
1173 int nr = to_sensor_dev_attr(attr)->index;
1174 struct lm85_data *data = lm85_update_device(dev);
1175 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
1176}
1177
1178static ssize_t temp_auto_temp_min_store(struct device *dev,
1179 struct device_attribute *attr,
1180 const char *buf, size_t count)
1181{
1182 int nr = to_sensor_dev_attr(attr)->index;
1183 struct lm85_data *data = dev_get_drvdata(dev);
1184 struct i2c_client *client = data->client;
1185 long val;
1186 int err;
1187
1188 err = kstrtol(buf, 10, &val);
1189 if (err)
1190 return err;
1191
1192 mutex_lock(&data->update_lock);
1193 data->zone[nr].limit = TEMP_TO_REG(val);
1194 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1195 data->zone[nr].limit);
1196
1197/* Update temp_auto_max and temp_auto_range */
1198 data->zone[nr].range = RANGE_TO_REG(
1199 TEMP_FROM_REG(data->zone[nr].max_desired) -
1200 TEMP_FROM_REG(data->zone[nr].limit));
1201 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1202 ((data->zone[nr].range & 0x0f) << 4)
1203 | data->pwm_freq[nr]);
1204
1205 mutex_unlock(&data->update_lock);
1206 return count;
1207}
1208
1209static ssize_t temp_auto_temp_max_show(struct device *dev,
1210 struct device_attribute *attr,
1211 char *buf)
1212{
1213 int nr = to_sensor_dev_attr(attr)->index;
1214 struct lm85_data *data = lm85_update_device(dev);
1215 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1216 RANGE_FROM_REG(data->zone[nr].range));
1217}
1218
1219static ssize_t temp_auto_temp_max_store(struct device *dev,
1220 struct device_attribute *attr,
1221 const char *buf, size_t count)
1222{
1223 int nr = to_sensor_dev_attr(attr)->index;
1224 struct lm85_data *data = dev_get_drvdata(dev);
1225 struct i2c_client *client = data->client;
1226 int min;
1227 long val;
1228 int err;
1229
1230 err = kstrtol(buf, 10, &val);
1231 if (err)
1232 return err;
1233
1234 mutex_lock(&data->update_lock);
1235 min = TEMP_FROM_REG(data->zone[nr].limit);
1236 data->zone[nr].max_desired = TEMP_TO_REG(val);
1237 data->zone[nr].range = RANGE_TO_REG(
1238 val - min);
1239 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1240 ((data->zone[nr].range & 0x0f) << 4)
1241 | data->pwm_freq[nr]);
1242 mutex_unlock(&data->update_lock);
1243 return count;
1244}
1245
1246static ssize_t temp_auto_temp_crit_show(struct device *dev,
1247 struct device_attribute *attr,
1248 char *buf)
1249{
1250 int nr = to_sensor_dev_attr(attr)->index;
1251 struct lm85_data *data = lm85_update_device(dev);
1252 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1253}
1254
1255static ssize_t temp_auto_temp_crit_store(struct device *dev,
1256 struct device_attribute *attr,
1257 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
1277static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_off, temp_auto_temp_off, 0);
1278static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_min, temp_auto_temp_min, 0);
1279static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_max, temp_auto_temp_max, 0);
1280static SENSOR_DEVICE_ATTR_RW(temp1_auto_temp_crit, temp_auto_temp_crit, 0);
1281static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_off, temp_auto_temp_off, 1);
1282static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_min, temp_auto_temp_min, 1);
1283static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_max, temp_auto_temp_max, 1);
1284static SENSOR_DEVICE_ATTR_RW(temp2_auto_temp_crit, temp_auto_temp_crit, 1);
1285static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_off, temp_auto_temp_off, 2);
1286static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_min, temp_auto_temp_min, 2);
1287static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_max, temp_auto_temp_max, 2);
1288static SENSOR_DEVICE_ATTR_RW(temp3_auto_temp_crit, temp_auto_temp_crit, 2);
1289
1290static struct attribute *lm85_attributes[] = {
1291 &sensor_dev_attr_fan1_input.dev_attr.attr,
1292 &sensor_dev_attr_fan2_input.dev_attr.attr,
1293 &sensor_dev_attr_fan3_input.dev_attr.attr,
1294 &sensor_dev_attr_fan4_input.dev_attr.attr,
1295 &sensor_dev_attr_fan1_min.dev_attr.attr,
1296 &sensor_dev_attr_fan2_min.dev_attr.attr,
1297 &sensor_dev_attr_fan3_min.dev_attr.attr,
1298 &sensor_dev_attr_fan4_min.dev_attr.attr,
1299 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1300 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1301 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1302 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1303
1304 &sensor_dev_attr_pwm1.dev_attr.attr,
1305 &sensor_dev_attr_pwm2.dev_attr.attr,
1306 &sensor_dev_attr_pwm3.dev_attr.attr,
1307 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1308 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1309 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1310 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1311 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1312 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1313
1314 &sensor_dev_attr_in0_input.dev_attr.attr,
1315 &sensor_dev_attr_in1_input.dev_attr.attr,
1316 &sensor_dev_attr_in2_input.dev_attr.attr,
1317 &sensor_dev_attr_in3_input.dev_attr.attr,
1318 &sensor_dev_attr_in0_min.dev_attr.attr,
1319 &sensor_dev_attr_in1_min.dev_attr.attr,
1320 &sensor_dev_attr_in2_min.dev_attr.attr,
1321 &sensor_dev_attr_in3_min.dev_attr.attr,
1322 &sensor_dev_attr_in0_max.dev_attr.attr,
1323 &sensor_dev_attr_in1_max.dev_attr.attr,
1324 &sensor_dev_attr_in2_max.dev_attr.attr,
1325 &sensor_dev_attr_in3_max.dev_attr.attr,
1326 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1327 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1328 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1329 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1330
1331 &sensor_dev_attr_temp1_input.dev_attr.attr,
1332 &sensor_dev_attr_temp2_input.dev_attr.attr,
1333 &sensor_dev_attr_temp3_input.dev_attr.attr,
1334 &sensor_dev_attr_temp1_min.dev_attr.attr,
1335 &sensor_dev_attr_temp2_min.dev_attr.attr,
1336 &sensor_dev_attr_temp3_min.dev_attr.attr,
1337 &sensor_dev_attr_temp1_max.dev_attr.attr,
1338 &sensor_dev_attr_temp2_max.dev_attr.attr,
1339 &sensor_dev_attr_temp3_max.dev_attr.attr,
1340 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1341 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1342 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1343 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1344 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1345
1346 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1347 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1348 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1349 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1350 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1351 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1352
1353 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1354 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1355 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1356 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1357 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1358 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1359 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1360 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1361 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1362
1363 &dev_attr_vrm.attr,
1364 &dev_attr_cpu0_vid.attr,
1365 &dev_attr_alarms.attr,
1366 NULL
1367};
1368
1369static const struct attribute_group lm85_group = {
1370 .attrs = lm85_attributes,
1371};
1372
1373static struct attribute *lm85_attributes_minctl[] = {
1374 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1375 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1376 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1377 NULL
1378};
1379
1380static const struct attribute_group lm85_group_minctl = {
1381 .attrs = lm85_attributes_minctl,
1382};
1383
1384static struct attribute *lm85_attributes_temp_off[] = {
1385 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1386 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1387 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1388 NULL
1389};
1390
1391static const struct attribute_group lm85_group_temp_off = {
1392 .attrs = lm85_attributes_temp_off,
1393};
1394
1395static struct attribute *lm85_attributes_in4[] = {
1396 &sensor_dev_attr_in4_input.dev_attr.attr,
1397 &sensor_dev_attr_in4_min.dev_attr.attr,
1398 &sensor_dev_attr_in4_max.dev_attr.attr,
1399 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1400 NULL
1401};
1402
1403static const struct attribute_group lm85_group_in4 = {
1404 .attrs = lm85_attributes_in4,
1405};
1406
1407static struct attribute *lm85_attributes_in567[] = {
1408 &sensor_dev_attr_in5_input.dev_attr.attr,
1409 &sensor_dev_attr_in6_input.dev_attr.attr,
1410 &sensor_dev_attr_in7_input.dev_attr.attr,
1411 &sensor_dev_attr_in5_min.dev_attr.attr,
1412 &sensor_dev_attr_in6_min.dev_attr.attr,
1413 &sensor_dev_attr_in7_min.dev_attr.attr,
1414 &sensor_dev_attr_in5_max.dev_attr.attr,
1415 &sensor_dev_attr_in6_max.dev_attr.attr,
1416 &sensor_dev_attr_in7_max.dev_attr.attr,
1417 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1418 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1419 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1420 NULL
1421};
1422
1423static const struct attribute_group lm85_group_in567 = {
1424 .attrs = lm85_attributes_in567,
1425};
1426
1427static void lm85_init_client(struct i2c_client *client)
1428{
1429 int value;
1430
1431 /* Start monitoring if needed */
1432 value = lm85_read_value(client, LM85_REG_CONFIG);
1433 if (!(value & 0x01)) {
1434 dev_info(&client->dev, "Starting monitoring\n");
1435 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1436 }
1437
1438 /* Warn about unusual configuration bits */
1439 if (value & 0x02)
1440 dev_warn(&client->dev, "Device configuration is locked\n");
1441 if (!(value & 0x04))
1442 dev_warn(&client->dev, "Device is not ready\n");
1443}
1444
1445static int lm85_is_fake(struct i2c_client *client)
1446{
1447 /*
1448 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1449 * emulate the former except that it has no hardware monitoring function
1450 * so the readings are always 0.
1451 */
1452 int i;
1453 u8 in_temp, fan;
1454
1455 for (i = 0; i < 8; i++) {
1456 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1457 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1458 if (in_temp != 0x00 || fan != 0xff)
1459 return 0;
1460 }
1461
1462 return 1;
1463}
1464
1465/* Return 0 if detection is successful, -ENODEV otherwise */
1466static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1467{
1468 struct i2c_adapter *adapter = client->adapter;
1469 int address = client->addr;
1470 const char *type_name = NULL;
1471 int company, verstep;
1472
1473 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1474 /* We need to be able to do byte I/O */
1475 return -ENODEV;
1476 }
1477
1478 /* Determine the chip type */
1479 company = lm85_read_value(client, LM85_REG_COMPANY);
1480 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1481
1482 dev_dbg(&adapter->dev,
1483 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1484 address, company, verstep);
1485
1486 if (company == LM85_COMPANY_NATIONAL) {
1487 switch (verstep) {
1488 case LM85_VERSTEP_LM85C:
1489 type_name = "lm85c";
1490 break;
1491 case LM85_VERSTEP_LM85B:
1492 type_name = "lm85b";
1493 break;
1494 case LM85_VERSTEP_LM96000_1:
1495 case LM85_VERSTEP_LM96000_2:
1496 /* Check for Winbond WPCD377I */
1497 if (lm85_is_fake(client)) {
1498 dev_dbg(&adapter->dev,
1499 "Found Winbond WPCD377I, ignoring\n");
1500 return -ENODEV;
1501 }
1502 type_name = "lm96000";
1503 break;
1504 }
1505 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1506 switch (verstep) {
1507 case LM85_VERSTEP_ADM1027:
1508 type_name = "adm1027";
1509 break;
1510 case LM85_VERSTEP_ADT7463:
1511 case LM85_VERSTEP_ADT7463C:
1512 type_name = "adt7463";
1513 break;
1514 case LM85_VERSTEP_ADT7468_1:
1515 case LM85_VERSTEP_ADT7468_2:
1516 type_name = "adt7468";
1517 break;
1518 }
1519 } else if (company == LM85_COMPANY_SMSC) {
1520 switch (verstep) {
1521 case LM85_VERSTEP_EMC6D100_A0:
1522 case LM85_VERSTEP_EMC6D100_A1:
1523 /* Note: we can't tell a '100 from a '101 */
1524 type_name = "emc6d100";
1525 break;
1526 case LM85_VERSTEP_EMC6D102:
1527 type_name = "emc6d102";
1528 break;
1529 case LM85_VERSTEP_EMC6D103_A0:
1530 case LM85_VERSTEP_EMC6D103_A1:
1531 type_name = "emc6d103";
1532 break;
1533 case LM85_VERSTEP_EMC6D103S:
1534 type_name = "emc6d103s";
1535 break;
1536 }
1537 }
1538
1539 if (!type_name)
1540 return -ENODEV;
1541
1542 strscpy(info->type, type_name, I2C_NAME_SIZE);
1543
1544 return 0;
1545}
1546
1547static const struct i2c_device_id lm85_id[];
1548
1549static int lm85_probe(struct i2c_client *client)
1550{
1551 struct device *dev = &client->dev;
1552 struct device *hwmon_dev;
1553 struct lm85_data *data;
1554 int idx = 0;
1555
1556 data = devm_kzalloc(dev, sizeof(struct lm85_data), GFP_KERNEL);
1557 if (!data)
1558 return -ENOMEM;
1559
1560 data->client = client;
1561 if (client->dev.of_node)
1562 data->type = (enum chips)of_device_get_match_data(&client->dev);
1563 else
1564 data->type = i2c_match_id(lm85_id, client)->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 data->freq_map_size = ARRAY_SIZE(adm1027_freq_map);
1578 break;
1579 case lm96000:
1580 data->freq_map = lm96000_freq_map;
1581 data->freq_map_size = ARRAY_SIZE(lm96000_freq_map);
1582 break;
1583 default:
1584 data->freq_map = lm85_freq_map;
1585 data->freq_map_size = ARRAY_SIZE(lm85_freq_map);
1586 }
1587
1588 /* Set the VRM version */
1589 data->vrm = vid_which_vrm();
1590
1591 /* Initialize the LM85 chip */
1592 lm85_init_client(client);
1593
1594 /* sysfs hooks */
1595 data->groups[idx++] = &lm85_group;
1596
1597 /* minctl and temp_off exist on all chips except emc6d103s */
1598 if (data->type != emc6d103s) {
1599 data->groups[idx++] = &lm85_group_minctl;
1600 data->groups[idx++] = &lm85_group_temp_off;
1601 }
1602
1603 /*
1604 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1605 * as a sixth digital VID input rather than an analog input.
1606 */
1607 if (data->type == adt7463 || data->type == adt7468) {
1608 u8 vid = lm85_read_value(client, LM85_REG_VID);
1609 if (vid & 0x80)
1610 data->has_vid5 = true;
1611 }
1612
1613 if (!data->has_vid5)
1614 data->groups[idx++] = &lm85_group_in4;
1615
1616 /* The EMC6D100 has 3 additional voltage inputs */
1617 if (data->type == emc6d100)
1618 data->groups[idx++] = &lm85_group_in567;
1619
1620 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1621 data, data->groups);
1622 return PTR_ERR_OR_ZERO(hwmon_dev);
1623}
1624
1625static const struct i2c_device_id lm85_id[] = {
1626 { "adm1027", adm1027 },
1627 { "adt7463", adt7463 },
1628 { "adt7468", adt7468 },
1629 { "lm85", lm85 },
1630 { "lm85b", lm85 },
1631 { "lm85c", lm85 },
1632 { "lm96000", lm96000 },
1633 { "emc6d100", emc6d100 },
1634 { "emc6d101", emc6d100 },
1635 { "emc6d102", emc6d102 },
1636 { "emc6d103", emc6d103 },
1637 { "emc6d103s", emc6d103s },
1638 { }
1639};
1640MODULE_DEVICE_TABLE(i2c, lm85_id);
1641
1642static const struct of_device_id __maybe_unused lm85_of_match[] = {
1643 {
1644 .compatible = "adi,adm1027",
1645 .data = (void *)adm1027
1646 },
1647 {
1648 .compatible = "adi,adt7463",
1649 .data = (void *)adt7463
1650 },
1651 {
1652 .compatible = "adi,adt7468",
1653 .data = (void *)adt7468
1654 },
1655 {
1656 .compatible = "national,lm85",
1657 .data = (void *)lm85
1658 },
1659 {
1660 .compatible = "national,lm85b",
1661 .data = (void *)lm85
1662 },
1663 {
1664 .compatible = "national,lm85c",
1665 .data = (void *)lm85
1666 },
1667 {
1668 .compatible = "ti,lm96000",
1669 .data = (void *)lm96000
1670 },
1671 {
1672 .compatible = "smsc,emc6d100",
1673 .data = (void *)emc6d100
1674 },
1675 {
1676 .compatible = "smsc,emc6d101",
1677 .data = (void *)emc6d100
1678 },
1679 {
1680 .compatible = "smsc,emc6d102",
1681 .data = (void *)emc6d102
1682 },
1683 {
1684 .compatible = "smsc,emc6d103",
1685 .data = (void *)emc6d103
1686 },
1687 {
1688 .compatible = "smsc,emc6d103s",
1689 .data = (void *)emc6d103s
1690 },
1691 { },
1692};
1693MODULE_DEVICE_TABLE(of, lm85_of_match);
1694
1695static struct i2c_driver lm85_driver = {
1696 .class = I2C_CLASS_HWMON,
1697 .driver = {
1698 .name = "lm85",
1699 .of_match_table = of_match_ptr(lm85_of_match),
1700 },
1701 .probe_new = lm85_probe,
1702 .id_table = lm85_id,
1703 .detect = lm85_detect,
1704 .address_list = normal_i2c,
1705};
1706
1707module_i2c_driver(lm85_driver);
1708
1709MODULE_LICENSE("GPL");
1710MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1711 "Margit Schubert-While <margitsw@t-online.de>, "
1712 "Justin Thiessen <jthiessen@penguincomputing.com>");
1713MODULE_DESCRIPTION("LM85-B, LM85-C driver");