mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / hwmon / lm85.c
blobb9d6e7d0ba37c3207a0fe414c9199c93fb2f0111
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>
10 * Chip details at <http://www.national.com/ds/LM/LM85.pdf>
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.
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.
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.
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>
38 /* Addresses to scan */
39 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
41 enum chips {
42 any_chip, lm85b, lm85c,
43 adm1027, adt7463, adt7468,
44 emc6d100, emc6d102, emc6d103, emc6d103s
47 /* The LM85 registers */
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)
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)
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)
61 #define LM85_REG_PWM(nr) (0x30 + (nr))
63 #define LM85_REG_COMPANY 0x3e
64 #define LM85_REG_VERSTEP 0x3f
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))
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 */
97 #define LM85_REG_CONFIG 0x40
99 #define LM85_REG_ALARM1 0x41
100 #define LM85_REG_ALARM2 0x42
102 #define LM85_REG_VID 0x43
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
114 #define ADM1027_REG_EXTEND_ADC1 0x76
115 #define ADM1027_REG_EXTEND_ADC2 0x77
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
129 * Conversions. Rounding and limit checking is only done on the TO_REG
130 * variants. Note that you should be a bit careful with which arguments
131 * these macros are called: arguments may be evaluated more than once.
134 /* IN are scaled according to built-in resistors */
135 static const int lm85_scaling[] = { /* .001 Volts */
136 2500, 2250, 3300, 5000, 12000,
137 3300, 1500, 1800 /*EMC6D100*/
139 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
141 #define INS_TO_REG(n, val) \
142 clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
144 #define INSEXT_FROM_REG(n, val, ext) \
145 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
147 #define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
149 /* FAN speed is measured using 90kHz clock */
150 static inline u16 FAN_TO_REG(unsigned long val)
152 if (!val)
153 return 0xffff;
154 return clamp_val(5400000 / val, 1, 0xfffe);
156 #define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
157 5400000 / (val))
159 /* Temperature is reported in .001 degC increments */
160 #define TEMP_TO_REG(val) \
161 DIV_ROUND_CLOSEST(clamp_val((val), -127000, 127000), 1000)
162 #define TEMPEXT_FROM_REG(val, ext) \
163 SCALE(((val) << 4) + (ext), 16, 1000)
164 #define TEMP_FROM_REG(val) ((val) * 1000)
166 #define PWM_TO_REG(val) clamp_val(val, 0, 255)
167 #define PWM_FROM_REG(val) (val)
171 * ZONEs have the following parameters:
172 * Limit (low) temp, 1. degC
173 * Hysteresis (below limit), 1. degC (0-15)
174 * Range of speed control, .1 degC (2-80)
175 * Critical (high) temp, 1. degC
177 * FAN PWMs have the following parameters:
178 * Reference Zone, 1, 2, 3, etc.
179 * Spinup time, .05 sec
180 * PWM value at limit/low temp, 1 count
181 * PWM Frequency, 1. Hz
182 * PWM is Min or OFF below limit, flag
183 * Invert PWM output, flag
185 * Some chips filter the temp, others the fan.
186 * Filter constant (or disabled) .1 seconds
189 /* These are the zone temperature range encodings in .001 degree C */
190 static const int lm85_range_map[] = {
191 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
192 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
195 static int RANGE_TO_REG(long range)
197 int i;
199 /* Find the closest match */
200 for (i = 0; i < 15; ++i) {
201 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
202 break;
205 return i;
207 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
209 /* These are the PWM frequency encodings */
210 static const int lm85_freq_map[8] = { /* 1 Hz */
211 10, 15, 23, 30, 38, 47, 61, 94
213 static const int adm1027_freq_map[8] = { /* 1 Hz */
214 11, 15, 22, 29, 35, 44, 59, 88
217 static int FREQ_TO_REG(const int *map, unsigned long freq)
219 int i;
221 /* Find the closest match */
222 for (i = 0; i < 7; ++i)
223 if (freq <= (map[i] + map[i + 1]) / 2)
224 break;
225 return i;
228 static int FREQ_FROM_REG(const int *map, u8 reg)
230 return map[reg & 0x07];
234 * Since we can't use strings, I'm abusing these numbers
235 * to stand in for the following meanings:
236 * 1 -- PWM responds to Zone 1
237 * 2 -- PWM responds to Zone 2
238 * 3 -- PWM responds to Zone 3
239 * 23 -- PWM responds to the higher temp of Zone 2 or 3
240 * 123 -- PWM responds to highest of Zone 1, 2, or 3
241 * 0 -- PWM is always at 0% (ie, off)
242 * -1 -- PWM is always at 100%
243 * -2 -- PWM responds to manual control
246 static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
247 #define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
249 static int ZONE_TO_REG(int zone)
251 int i;
253 for (i = 0; i <= 7; ++i)
254 if (zone == lm85_zone_map[i])
255 break;
256 if (i > 7) /* Not found. */
257 i = 3; /* Always 100% */
258 return i << 5;
261 #define HYST_TO_REG(val) clamp_val(((val) + 500) / 1000, 0, 15)
262 #define HYST_FROM_REG(val) ((val) * 1000)
265 * Chip sampling rates
267 * Some sensors are not updated more frequently than once per second
268 * so it doesn't make sense to read them more often than that.
269 * We cache the results and return the saved data if the driver
270 * is called again before a second has elapsed.
272 * Also, there is significant configuration data for this chip
273 * given the automatic PWM fan control that is possible. There
274 * are about 47 bytes of config data to only 22 bytes of actual
275 * readings. So, we keep the config data up to date in the cache
276 * when it is written and only sample it once every 1 *minute*
278 #define LM85_DATA_INTERVAL (HZ + HZ / 2)
279 #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
282 * LM85 can automatically adjust fan speeds based on temperature
283 * This structure encapsulates an entire Zone config. There are
284 * three zones (one for each temperature input) on the lm85
286 struct lm85_zone {
287 s8 limit; /* Low temp limit */
288 u8 hyst; /* Low limit hysteresis. (0-15) */
289 u8 range; /* Temp range, encoded */
290 s8 critical; /* "All fans ON" temp limit */
291 u8 max_desired; /*
292 * Actual "max" temperature specified. Preserved
293 * to prevent "drift" as other autofan control
294 * values change.
298 struct lm85_autofan {
299 u8 config; /* Register value */
300 u8 min_pwm; /* Minimum PWM value, encoded */
301 u8 min_off; /* Min PWM or OFF below "limit", flag */
305 * For each registered chip, we need to keep some data in memory.
306 * The structure is dynamically allocated.
308 struct lm85_data {
309 struct device *hwmon_dev;
310 const int *freq_map;
311 enum chips type;
313 bool has_vid5; /* true if VID5 is configured for ADT7463 or ADT7468 */
315 struct mutex update_lock;
316 int valid; /* !=0 if following fields are valid */
317 unsigned long last_reading; /* In jiffies */
318 unsigned long last_config; /* In jiffies */
320 u8 in[8]; /* Register value */
321 u8 in_max[8]; /* Register value */
322 u8 in_min[8]; /* Register value */
323 s8 temp[3]; /* Register value */
324 s8 temp_min[3]; /* Register value */
325 s8 temp_max[3]; /* Register value */
326 u16 fan[4]; /* Register value */
327 u16 fan_min[4]; /* Register value */
328 u8 pwm[3]; /* Register value */
329 u8 pwm_freq[3]; /* Register encoding */
330 u8 temp_ext[3]; /* Decoded values */
331 u8 in_ext[8]; /* Decoded values */
332 u8 vid; /* Register value */
333 u8 vrm; /* VRM version */
334 u32 alarms; /* Register encoding, combined */
335 u8 cfg5; /* Config Register 5 on ADT7468 */
336 struct lm85_autofan autofan[3];
337 struct lm85_zone zone[3];
340 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
341 static int lm85_probe(struct i2c_client *client,
342 const struct i2c_device_id *id);
343 static int lm85_remove(struct i2c_client *client);
345 static int lm85_read_value(struct i2c_client *client, u8 reg);
346 static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
347 static struct lm85_data *lm85_update_device(struct device *dev);
350 static const struct i2c_device_id lm85_id[] = {
351 { "adm1027", adm1027 },
352 { "adt7463", adt7463 },
353 { "adt7468", adt7468 },
354 { "lm85", any_chip },
355 { "lm85b", lm85b },
356 { "lm85c", lm85c },
357 { "emc6d100", emc6d100 },
358 { "emc6d101", emc6d100 },
359 { "emc6d102", emc6d102 },
360 { "emc6d103", emc6d103 },
361 { "emc6d103s", emc6d103s },
364 MODULE_DEVICE_TABLE(i2c, lm85_id);
366 static struct i2c_driver lm85_driver = {
367 .class = I2C_CLASS_HWMON,
368 .driver = {
369 .name = "lm85",
371 .probe = lm85_probe,
372 .remove = lm85_remove,
373 .id_table = lm85_id,
374 .detect = lm85_detect,
375 .address_list = normal_i2c,
379 /* 4 Fans */
380 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
381 char *buf)
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[nr]));
388 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
389 char *buf)
391 int nr = to_sensor_dev_attr(attr)->index;
392 struct lm85_data *data = lm85_update_device(dev);
393 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr]));
396 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
399 int nr = to_sensor_dev_attr(attr)->index;
400 struct i2c_client *client = to_i2c_client(dev);
401 struct lm85_data *data = i2c_get_clientdata(client);
402 unsigned long val;
403 int err;
405 err = kstrtoul(buf, 10, &val);
406 if (err)
407 return err;
409 mutex_lock(&data->update_lock);
410 data->fan_min[nr] = FAN_TO_REG(val);
411 lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
412 mutex_unlock(&data->update_lock);
413 return count;
416 #define show_fan_offset(offset) \
417 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan, NULL, offset - 1); \
419 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
420 show_fan_min, set_fan_min, offset - 1)
422 show_fan_offset(1);
423 show_fan_offset(2);
424 show_fan_offset(3);
425 show_fan_offset(4);
427 /* vid, vrm, alarms */
429 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
430 char *buf)
432 struct lm85_data *data = lm85_update_device(dev);
433 int vid;
435 if (data->has_vid5) {
436 /* 6-pin VID (VRM 10) */
437 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
438 } else {
439 /* 5-pin VID (VRM 9) */
440 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
443 return sprintf(buf, "%d\n", vid);
446 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
448 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
449 char *buf)
451 struct lm85_data *data = dev_get_drvdata(dev);
452 return sprintf(buf, "%ld\n", (long) data->vrm);
455 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
456 const char *buf, size_t count)
458 struct lm85_data *data = dev_get_drvdata(dev);
459 unsigned long val;
460 int err;
462 err = kstrtoul(buf, 10, &val);
463 if (err)
464 return err;
466 if (val > 255)
467 return -EINVAL;
469 data->vrm = val;
470 return count;
473 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
475 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
476 *attr, char *buf)
478 struct lm85_data *data = lm85_update_device(dev);
479 return sprintf(buf, "%u\n", data->alarms);
482 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
484 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
485 char *buf)
487 int nr = to_sensor_dev_attr(attr)->index;
488 struct lm85_data *data = lm85_update_device(dev);
489 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
492 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
493 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
494 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
495 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
496 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
497 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
498 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
499 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
500 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
501 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
502 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
503 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
504 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
505 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
506 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
507 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
508 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
510 /* pwm */
512 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
513 char *buf)
515 int nr = to_sensor_dev_attr(attr)->index;
516 struct lm85_data *data = lm85_update_device(dev);
517 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
520 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
521 const char *buf, size_t count)
523 int nr = to_sensor_dev_attr(attr)->index;
524 struct i2c_client *client = to_i2c_client(dev);
525 struct lm85_data *data = i2c_get_clientdata(client);
526 unsigned long val;
527 int err;
529 err = kstrtoul(buf, 10, &val);
530 if (err)
531 return err;
533 mutex_lock(&data->update_lock);
534 data->pwm[nr] = PWM_TO_REG(val);
535 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
536 mutex_unlock(&data->update_lock);
537 return count;
540 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
541 *attr, char *buf)
543 int nr = to_sensor_dev_attr(attr)->index;
544 struct lm85_data *data = lm85_update_device(dev);
545 int pwm_zone, enable;
547 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
548 switch (pwm_zone) {
549 case -1: /* PWM is always at 100% */
550 enable = 0;
551 break;
552 case 0: /* PWM is always at 0% */
553 case -2: /* PWM responds to manual control */
554 enable = 1;
555 break;
556 default: /* PWM in automatic mode */
557 enable = 2;
559 return sprintf(buf, "%d\n", enable);
562 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
563 *attr, const char *buf, size_t count)
565 int nr = to_sensor_dev_attr(attr)->index;
566 struct i2c_client *client = to_i2c_client(dev);
567 struct lm85_data *data = i2c_get_clientdata(client);
568 u8 config;
569 unsigned long val;
570 int err;
572 err = kstrtoul(buf, 10, &val);
573 if (err)
574 return err;
576 switch (val) {
577 case 0:
578 config = 3;
579 break;
580 case 1:
581 config = 7;
582 break;
583 case 2:
585 * Here we have to choose arbitrarily one of the 5 possible
586 * configurations; I go for the safest
588 config = 6;
589 break;
590 default:
591 return -EINVAL;
594 mutex_lock(&data->update_lock);
595 data->autofan[nr].config = lm85_read_value(client,
596 LM85_REG_AFAN_CONFIG(nr));
597 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
598 | (config << 5);
599 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
600 data->autofan[nr].config);
601 mutex_unlock(&data->update_lock);
602 return count;
605 static ssize_t show_pwm_freq(struct device *dev,
606 struct device_attribute *attr, char *buf)
608 int nr = to_sensor_dev_attr(attr)->index;
609 struct lm85_data *data = lm85_update_device(dev);
610 int freq;
612 if (IS_ADT7468_HFPWM(data))
613 freq = 22500;
614 else
615 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
617 return sprintf(buf, "%d\n", freq);
620 static ssize_t set_pwm_freq(struct device *dev,
621 struct device_attribute *attr, const char *buf, size_t count)
623 int nr = to_sensor_dev_attr(attr)->index;
624 struct i2c_client *client = to_i2c_client(dev);
625 struct lm85_data *data = i2c_get_clientdata(client);
626 unsigned long val;
627 int err;
629 err = kstrtoul(buf, 10, &val);
630 if (err)
631 return err;
633 mutex_lock(&data->update_lock);
635 * The ADT7468 has a special high-frequency PWM output mode,
636 * where all PWM outputs are driven by a 22.5 kHz clock.
637 * This might confuse the user, but there's not much we can do.
639 if (data->type == adt7468 && val >= 11300) { /* High freq. mode */
640 data->cfg5 &= ~ADT7468_HFPWM;
641 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
642 } else { /* Low freq. mode */
643 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
644 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
645 (data->zone[nr].range << 4)
646 | data->pwm_freq[nr]);
647 if (data->type == adt7468) {
648 data->cfg5 |= ADT7468_HFPWM;
649 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
652 mutex_unlock(&data->update_lock);
653 return count;
656 #define show_pwm_reg(offset) \
657 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
658 show_pwm, set_pwm, offset - 1); \
659 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
660 show_pwm_enable, set_pwm_enable, offset - 1); \
661 static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
662 show_pwm_freq, set_pwm_freq, offset - 1)
664 show_pwm_reg(1);
665 show_pwm_reg(2);
666 show_pwm_reg(3);
668 /* Voltages */
670 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
671 char *buf)
673 int nr = to_sensor_dev_attr(attr)->index;
674 struct lm85_data *data = lm85_update_device(dev);
675 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
676 data->in_ext[nr]));
679 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
680 char *buf)
682 int nr = to_sensor_dev_attr(attr)->index;
683 struct lm85_data *data = lm85_update_device(dev);
684 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
687 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
690 int nr = to_sensor_dev_attr(attr)->index;
691 struct i2c_client *client = to_i2c_client(dev);
692 struct lm85_data *data = i2c_get_clientdata(client);
693 long val;
694 int err;
696 err = kstrtol(buf, 10, &val);
697 if (err)
698 return err;
700 mutex_lock(&data->update_lock);
701 data->in_min[nr] = INS_TO_REG(nr, val);
702 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
703 mutex_unlock(&data->update_lock);
704 return count;
707 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
708 char *buf)
710 int nr = to_sensor_dev_attr(attr)->index;
711 struct lm85_data *data = lm85_update_device(dev);
712 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
715 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
716 const char *buf, size_t count)
718 int nr = to_sensor_dev_attr(attr)->index;
719 struct i2c_client *client = to_i2c_client(dev);
720 struct lm85_data *data = i2c_get_clientdata(client);
721 long val;
722 int err;
724 err = kstrtol(buf, 10, &val);
725 if (err)
726 return err;
728 mutex_lock(&data->update_lock);
729 data->in_max[nr] = INS_TO_REG(nr, val);
730 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
731 mutex_unlock(&data->update_lock);
732 return count;
735 #define show_in_reg(offset) \
736 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
737 show_in, NULL, offset); \
738 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
739 show_in_min, set_in_min, offset); \
740 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
741 show_in_max, set_in_max, offset)
743 show_in_reg(0);
744 show_in_reg(1);
745 show_in_reg(2);
746 show_in_reg(3);
747 show_in_reg(4);
748 show_in_reg(5);
749 show_in_reg(6);
750 show_in_reg(7);
752 /* Temps */
754 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
755 char *buf)
757 int nr = to_sensor_dev_attr(attr)->index;
758 struct lm85_data *data = lm85_update_device(dev);
759 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
760 data->temp_ext[nr]));
763 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
764 char *buf)
766 int nr = to_sensor_dev_attr(attr)->index;
767 struct lm85_data *data = lm85_update_device(dev);
768 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
771 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
772 const char *buf, size_t count)
774 int nr = to_sensor_dev_attr(attr)->index;
775 struct i2c_client *client = to_i2c_client(dev);
776 struct lm85_data *data = i2c_get_clientdata(client);
777 long val;
778 int err;
780 err = kstrtol(buf, 10, &val);
781 if (err)
782 return err;
784 if (IS_ADT7468_OFF64(data))
785 val += 64;
787 mutex_lock(&data->update_lock);
788 data->temp_min[nr] = TEMP_TO_REG(val);
789 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
790 mutex_unlock(&data->update_lock);
791 return count;
794 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
795 char *buf)
797 int nr = to_sensor_dev_attr(attr)->index;
798 struct lm85_data *data = lm85_update_device(dev);
799 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
802 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
805 int nr = to_sensor_dev_attr(attr)->index;
806 struct i2c_client *client = to_i2c_client(dev);
807 struct lm85_data *data = i2c_get_clientdata(client);
808 long val;
809 int err;
811 err = kstrtol(buf, 10, &val);
812 if (err)
813 return err;
815 if (IS_ADT7468_OFF64(data))
816 val += 64;
818 mutex_lock(&data->update_lock);
819 data->temp_max[nr] = TEMP_TO_REG(val);
820 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
821 mutex_unlock(&data->update_lock);
822 return count;
825 #define show_temp_reg(offset) \
826 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
827 show_temp, NULL, offset - 1); \
828 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
829 show_temp_min, set_temp_min, offset - 1); \
830 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
831 show_temp_max, set_temp_max, offset - 1);
833 show_temp_reg(1);
834 show_temp_reg(2);
835 show_temp_reg(3);
838 /* Automatic PWM control */
840 static ssize_t show_pwm_auto_channels(struct device *dev,
841 struct device_attribute *attr, char *buf)
843 int nr = to_sensor_dev_attr(attr)->index;
844 struct lm85_data *data = lm85_update_device(dev);
845 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
848 static ssize_t set_pwm_auto_channels(struct device *dev,
849 struct device_attribute *attr, const char *buf, size_t count)
851 int nr = to_sensor_dev_attr(attr)->index;
852 struct i2c_client *client = to_i2c_client(dev);
853 struct lm85_data *data = i2c_get_clientdata(client);
854 long val;
855 int err;
857 err = kstrtol(buf, 10, &val);
858 if (err)
859 return err;
861 mutex_lock(&data->update_lock);
862 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
863 | ZONE_TO_REG(val);
864 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
865 data->autofan[nr].config);
866 mutex_unlock(&data->update_lock);
867 return count;
870 static ssize_t show_pwm_auto_pwm_min(struct device *dev,
871 struct device_attribute *attr, char *buf)
873 int nr = to_sensor_dev_attr(attr)->index;
874 struct lm85_data *data = lm85_update_device(dev);
875 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
878 static ssize_t set_pwm_auto_pwm_min(struct device *dev,
879 struct device_attribute *attr, const char *buf, size_t count)
881 int nr = to_sensor_dev_attr(attr)->index;
882 struct i2c_client *client = to_i2c_client(dev);
883 struct lm85_data *data = i2c_get_clientdata(client);
884 unsigned long val;
885 int err;
887 err = kstrtoul(buf, 10, &val);
888 if (err)
889 return err;
891 mutex_lock(&data->update_lock);
892 data->autofan[nr].min_pwm = PWM_TO_REG(val);
893 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
894 data->autofan[nr].min_pwm);
895 mutex_unlock(&data->update_lock);
896 return count;
899 static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
900 struct device_attribute *attr, char *buf)
902 int nr = to_sensor_dev_attr(attr)->index;
903 struct lm85_data *data = lm85_update_device(dev);
904 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
907 static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
908 struct device_attribute *attr, const char *buf, size_t count)
910 int nr = to_sensor_dev_attr(attr)->index;
911 struct i2c_client *client = to_i2c_client(dev);
912 struct lm85_data *data = i2c_get_clientdata(client);
913 u8 tmp;
914 long val;
915 int err;
917 err = kstrtol(buf, 10, &val);
918 if (err)
919 return err;
921 mutex_lock(&data->update_lock);
922 data->autofan[nr].min_off = val;
923 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
924 tmp &= ~(0x20 << nr);
925 if (data->autofan[nr].min_off)
926 tmp |= 0x20 << nr;
927 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
928 mutex_unlock(&data->update_lock);
929 return count;
932 #define pwm_auto(offset) \
933 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
934 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
935 set_pwm_auto_channels, offset - 1); \
936 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
937 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
938 set_pwm_auto_pwm_min, offset - 1); \
939 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
940 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
941 set_pwm_auto_pwm_minctl, offset - 1)
943 pwm_auto(1);
944 pwm_auto(2);
945 pwm_auto(3);
947 /* Temperature settings for automatic PWM control */
949 static ssize_t show_temp_auto_temp_off(struct device *dev,
950 struct device_attribute *attr, char *buf)
952 int nr = to_sensor_dev_attr(attr)->index;
953 struct lm85_data *data = lm85_update_device(dev);
954 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
955 HYST_FROM_REG(data->zone[nr].hyst));
958 static ssize_t set_temp_auto_temp_off(struct device *dev,
959 struct device_attribute *attr, const char *buf, size_t count)
961 int nr = to_sensor_dev_attr(attr)->index;
962 struct i2c_client *client = to_i2c_client(dev);
963 struct lm85_data *data = i2c_get_clientdata(client);
964 int min;
965 long val;
966 int err;
968 err = kstrtol(buf, 10, &val);
969 if (err)
970 return err;
972 mutex_lock(&data->update_lock);
973 min = TEMP_FROM_REG(data->zone[nr].limit);
974 data->zone[nr].hyst = HYST_TO_REG(min - val);
975 if (nr == 0 || nr == 1) {
976 lm85_write_value(client, LM85_REG_AFAN_HYST1,
977 (data->zone[0].hyst << 4)
978 | data->zone[1].hyst);
979 } else {
980 lm85_write_value(client, LM85_REG_AFAN_HYST2,
981 (data->zone[2].hyst << 4));
983 mutex_unlock(&data->update_lock);
984 return count;
987 static ssize_t show_temp_auto_temp_min(struct device *dev,
988 struct device_attribute *attr, char *buf)
990 int nr = to_sensor_dev_attr(attr)->index;
991 struct lm85_data *data = lm85_update_device(dev);
992 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
995 static ssize_t set_temp_auto_temp_min(struct device *dev,
996 struct device_attribute *attr, const char *buf, size_t count)
998 int nr = to_sensor_dev_attr(attr)->index;
999 struct i2c_client *client = to_i2c_client(dev);
1000 struct lm85_data *data = i2c_get_clientdata(client);
1001 long val;
1002 int err;
1004 err = kstrtol(buf, 10, &val);
1005 if (err)
1006 return err;
1008 mutex_lock(&data->update_lock);
1009 data->zone[nr].limit = TEMP_TO_REG(val);
1010 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1011 data->zone[nr].limit);
1013 /* Update temp_auto_max and temp_auto_range */
1014 data->zone[nr].range = RANGE_TO_REG(
1015 TEMP_FROM_REG(data->zone[nr].max_desired) -
1016 TEMP_FROM_REG(data->zone[nr].limit));
1017 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1018 ((data->zone[nr].range & 0x0f) << 4)
1019 | (data->pwm_freq[nr] & 0x07));
1021 mutex_unlock(&data->update_lock);
1022 return count;
1025 static ssize_t show_temp_auto_temp_max(struct device *dev,
1026 struct device_attribute *attr, char *buf)
1028 int nr = to_sensor_dev_attr(attr)->index;
1029 struct lm85_data *data = lm85_update_device(dev);
1030 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1031 RANGE_FROM_REG(data->zone[nr].range));
1034 static ssize_t set_temp_auto_temp_max(struct device *dev,
1035 struct device_attribute *attr, const char *buf, size_t count)
1037 int nr = to_sensor_dev_attr(attr)->index;
1038 struct i2c_client *client = to_i2c_client(dev);
1039 struct lm85_data *data = i2c_get_clientdata(client);
1040 int min;
1041 long val;
1042 int err;
1044 err = kstrtol(buf, 10, &val);
1045 if (err)
1046 return err;
1048 mutex_lock(&data->update_lock);
1049 min = TEMP_FROM_REG(data->zone[nr].limit);
1050 data->zone[nr].max_desired = TEMP_TO_REG(val);
1051 data->zone[nr].range = RANGE_TO_REG(
1052 val - min);
1053 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1054 ((data->zone[nr].range & 0x0f) << 4)
1055 | (data->pwm_freq[nr] & 0x07));
1056 mutex_unlock(&data->update_lock);
1057 return count;
1060 static ssize_t show_temp_auto_temp_crit(struct device *dev,
1061 struct device_attribute *attr, char *buf)
1063 int nr = to_sensor_dev_attr(attr)->index;
1064 struct lm85_data *data = lm85_update_device(dev);
1065 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1068 static ssize_t set_temp_auto_temp_crit(struct device *dev,
1069 struct device_attribute *attr, const char *buf, size_t count)
1071 int nr = to_sensor_dev_attr(attr)->index;
1072 struct i2c_client *client = to_i2c_client(dev);
1073 struct lm85_data *data = i2c_get_clientdata(client);
1074 long val;
1075 int err;
1077 err = kstrtol(buf, 10, &val);
1078 if (err)
1079 return err;
1081 mutex_lock(&data->update_lock);
1082 data->zone[nr].critical = TEMP_TO_REG(val);
1083 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1084 data->zone[nr].critical);
1085 mutex_unlock(&data->update_lock);
1086 return count;
1089 #define temp_auto(offset) \
1090 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1091 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1092 set_temp_auto_temp_off, offset - 1); \
1093 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1094 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1095 set_temp_auto_temp_min, offset - 1); \
1096 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1097 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1098 set_temp_auto_temp_max, offset - 1); \
1099 static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1100 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1101 set_temp_auto_temp_crit, offset - 1);
1103 temp_auto(1);
1104 temp_auto(2);
1105 temp_auto(3);
1107 static struct attribute *lm85_attributes[] = {
1108 &sensor_dev_attr_fan1_input.dev_attr.attr,
1109 &sensor_dev_attr_fan2_input.dev_attr.attr,
1110 &sensor_dev_attr_fan3_input.dev_attr.attr,
1111 &sensor_dev_attr_fan4_input.dev_attr.attr,
1112 &sensor_dev_attr_fan1_min.dev_attr.attr,
1113 &sensor_dev_attr_fan2_min.dev_attr.attr,
1114 &sensor_dev_attr_fan3_min.dev_attr.attr,
1115 &sensor_dev_attr_fan4_min.dev_attr.attr,
1116 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1117 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1118 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1119 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1121 &sensor_dev_attr_pwm1.dev_attr.attr,
1122 &sensor_dev_attr_pwm2.dev_attr.attr,
1123 &sensor_dev_attr_pwm3.dev_attr.attr,
1124 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1125 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1126 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1127 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1128 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1129 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1131 &sensor_dev_attr_in0_input.dev_attr.attr,
1132 &sensor_dev_attr_in1_input.dev_attr.attr,
1133 &sensor_dev_attr_in2_input.dev_attr.attr,
1134 &sensor_dev_attr_in3_input.dev_attr.attr,
1135 &sensor_dev_attr_in0_min.dev_attr.attr,
1136 &sensor_dev_attr_in1_min.dev_attr.attr,
1137 &sensor_dev_attr_in2_min.dev_attr.attr,
1138 &sensor_dev_attr_in3_min.dev_attr.attr,
1139 &sensor_dev_attr_in0_max.dev_attr.attr,
1140 &sensor_dev_attr_in1_max.dev_attr.attr,
1141 &sensor_dev_attr_in2_max.dev_attr.attr,
1142 &sensor_dev_attr_in3_max.dev_attr.attr,
1143 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1144 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1145 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1146 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1148 &sensor_dev_attr_temp1_input.dev_attr.attr,
1149 &sensor_dev_attr_temp2_input.dev_attr.attr,
1150 &sensor_dev_attr_temp3_input.dev_attr.attr,
1151 &sensor_dev_attr_temp1_min.dev_attr.attr,
1152 &sensor_dev_attr_temp2_min.dev_attr.attr,
1153 &sensor_dev_attr_temp3_min.dev_attr.attr,
1154 &sensor_dev_attr_temp1_max.dev_attr.attr,
1155 &sensor_dev_attr_temp2_max.dev_attr.attr,
1156 &sensor_dev_attr_temp3_max.dev_attr.attr,
1157 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1158 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1159 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1160 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1161 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1163 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1164 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1165 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1166 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1167 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1168 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1170 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1171 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1172 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1173 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1174 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1175 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1176 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1177 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1178 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1180 &dev_attr_vrm.attr,
1181 &dev_attr_cpu0_vid.attr,
1182 &dev_attr_alarms.attr,
1183 NULL
1186 static const struct attribute_group lm85_group = {
1187 .attrs = lm85_attributes,
1190 static struct attribute *lm85_attributes_minctl[] = {
1191 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1192 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1193 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1194 NULL
1197 static const struct attribute_group lm85_group_minctl = {
1198 .attrs = lm85_attributes_minctl,
1201 static struct attribute *lm85_attributes_temp_off[] = {
1202 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1203 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1204 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1205 NULL
1208 static const struct attribute_group lm85_group_temp_off = {
1209 .attrs = lm85_attributes_temp_off,
1212 static struct attribute *lm85_attributes_in4[] = {
1213 &sensor_dev_attr_in4_input.dev_attr.attr,
1214 &sensor_dev_attr_in4_min.dev_attr.attr,
1215 &sensor_dev_attr_in4_max.dev_attr.attr,
1216 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1217 NULL
1220 static const struct attribute_group lm85_group_in4 = {
1221 .attrs = lm85_attributes_in4,
1224 static struct attribute *lm85_attributes_in567[] = {
1225 &sensor_dev_attr_in5_input.dev_attr.attr,
1226 &sensor_dev_attr_in6_input.dev_attr.attr,
1227 &sensor_dev_attr_in7_input.dev_attr.attr,
1228 &sensor_dev_attr_in5_min.dev_attr.attr,
1229 &sensor_dev_attr_in6_min.dev_attr.attr,
1230 &sensor_dev_attr_in7_min.dev_attr.attr,
1231 &sensor_dev_attr_in5_max.dev_attr.attr,
1232 &sensor_dev_attr_in6_max.dev_attr.attr,
1233 &sensor_dev_attr_in7_max.dev_attr.attr,
1234 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1235 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1236 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1237 NULL
1240 static const struct attribute_group lm85_group_in567 = {
1241 .attrs = lm85_attributes_in567,
1244 static void lm85_init_client(struct i2c_client *client)
1246 int value;
1248 /* Start monitoring if needed */
1249 value = lm85_read_value(client, LM85_REG_CONFIG);
1250 if (!(value & 0x01)) {
1251 dev_info(&client->dev, "Starting monitoring\n");
1252 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1255 /* Warn about unusual configuration bits */
1256 if (value & 0x02)
1257 dev_warn(&client->dev, "Device configuration is locked\n");
1258 if (!(value & 0x04))
1259 dev_warn(&client->dev, "Device is not ready\n");
1262 static int lm85_is_fake(struct i2c_client *client)
1265 * Differenciate between real LM96000 and Winbond WPCD377I. The latter
1266 * emulate the former except that it has no hardware monitoring function
1267 * so the readings are always 0.
1269 int i;
1270 u8 in_temp, fan;
1272 for (i = 0; i < 8; i++) {
1273 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1274 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1275 if (in_temp != 0x00 || fan != 0xff)
1276 return 0;
1279 return 1;
1282 /* Return 0 if detection is successful, -ENODEV otherwise */
1283 static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1285 struct i2c_adapter *adapter = client->adapter;
1286 int address = client->addr;
1287 const char *type_name;
1288 int company, verstep;
1290 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1291 /* We need to be able to do byte I/O */
1292 return -ENODEV;
1295 /* Determine the chip type */
1296 company = lm85_read_value(client, LM85_REG_COMPANY);
1297 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1299 dev_dbg(&adapter->dev,
1300 "Detecting device at 0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1301 address, company, verstep);
1303 /* All supported chips have the version in common */
1304 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1305 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1306 dev_dbg(&adapter->dev,
1307 "Autodetection failed: unsupported version\n");
1308 return -ENODEV;
1310 type_name = "lm85";
1312 /* Now, refine the detection */
1313 if (company == LM85_COMPANY_NATIONAL) {
1314 switch (verstep) {
1315 case LM85_VERSTEP_LM85C:
1316 type_name = "lm85c";
1317 break;
1318 case LM85_VERSTEP_LM85B:
1319 type_name = "lm85b";
1320 break;
1321 case LM85_VERSTEP_LM96000_1:
1322 case LM85_VERSTEP_LM96000_2:
1323 /* Check for Winbond WPCD377I */
1324 if (lm85_is_fake(client)) {
1325 dev_dbg(&adapter->dev,
1326 "Found Winbond WPCD377I, ignoring\n");
1327 return -ENODEV;
1329 break;
1331 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1332 switch (verstep) {
1333 case LM85_VERSTEP_ADM1027:
1334 type_name = "adm1027";
1335 break;
1336 case LM85_VERSTEP_ADT7463:
1337 case LM85_VERSTEP_ADT7463C:
1338 type_name = "adt7463";
1339 break;
1340 case LM85_VERSTEP_ADT7468_1:
1341 case LM85_VERSTEP_ADT7468_2:
1342 type_name = "adt7468";
1343 break;
1345 } else if (company == LM85_COMPANY_SMSC) {
1346 switch (verstep) {
1347 case LM85_VERSTEP_EMC6D100_A0:
1348 case LM85_VERSTEP_EMC6D100_A1:
1349 /* Note: we can't tell a '100 from a '101 */
1350 type_name = "emc6d100";
1351 break;
1352 case LM85_VERSTEP_EMC6D102:
1353 type_name = "emc6d102";
1354 break;
1355 case LM85_VERSTEP_EMC6D103_A0:
1356 case LM85_VERSTEP_EMC6D103_A1:
1357 type_name = "emc6d103";
1358 break;
1359 case LM85_VERSTEP_EMC6D103S:
1360 type_name = "emc6d103s";
1361 break;
1363 } else {
1364 dev_dbg(&adapter->dev,
1365 "Autodetection failed: unknown vendor\n");
1366 return -ENODEV;
1369 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1371 return 0;
1374 static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1376 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1377 if (data->type != emc6d103s) {
1378 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1379 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1381 if (!data->has_vid5)
1382 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1383 if (data->type == emc6d100)
1384 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1387 static int lm85_probe(struct i2c_client *client,
1388 const struct i2c_device_id *id)
1390 struct lm85_data *data;
1391 int err;
1393 data = devm_kzalloc(&client->dev, sizeof(struct lm85_data), GFP_KERNEL);
1394 if (!data)
1395 return -ENOMEM;
1397 i2c_set_clientdata(client, data);
1398 data->type = id->driver_data;
1399 mutex_init(&data->update_lock);
1401 /* Fill in the chip specific driver values */
1402 switch (data->type) {
1403 case adm1027:
1404 case adt7463:
1405 case adt7468:
1406 case emc6d100:
1407 case emc6d102:
1408 case emc6d103:
1409 case emc6d103s:
1410 data->freq_map = adm1027_freq_map;
1411 break;
1412 default:
1413 data->freq_map = lm85_freq_map;
1416 /* Set the VRM version */
1417 data->vrm = vid_which_vrm();
1419 /* Initialize the LM85 chip */
1420 lm85_init_client(client);
1422 /* Register sysfs hooks */
1423 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1424 if (err)
1425 return err;
1427 /* minctl and temp_off exist on all chips except emc6d103s */
1428 if (data->type != emc6d103s) {
1429 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1430 if (err)
1431 goto err_remove_files;
1432 err = sysfs_create_group(&client->dev.kobj,
1433 &lm85_group_temp_off);
1434 if (err)
1435 goto err_remove_files;
1439 * The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
1440 * as a sixth digital VID input rather than an analog input.
1442 if (data->type == adt7463 || data->type == adt7468) {
1443 u8 vid = lm85_read_value(client, LM85_REG_VID);
1444 if (vid & 0x80)
1445 data->has_vid5 = true;
1448 if (!data->has_vid5) {
1449 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in4);
1450 if (err)
1451 goto err_remove_files;
1454 /* The EMC6D100 has 3 additional voltage inputs */
1455 if (data->type == emc6d100) {
1456 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in567);
1457 if (err)
1458 goto err_remove_files;
1461 data->hwmon_dev = hwmon_device_register(&client->dev);
1462 if (IS_ERR(data->hwmon_dev)) {
1463 err = PTR_ERR(data->hwmon_dev);
1464 goto err_remove_files;
1467 return 0;
1469 /* Error out and cleanup code */
1470 err_remove_files:
1471 lm85_remove_files(client, data);
1472 return err;
1475 static int lm85_remove(struct i2c_client *client)
1477 struct lm85_data *data = i2c_get_clientdata(client);
1478 hwmon_device_unregister(data->hwmon_dev);
1479 lm85_remove_files(client, data);
1480 return 0;
1484 static int lm85_read_value(struct i2c_client *client, u8 reg)
1486 int res;
1488 /* What size location is it? */
1489 switch (reg) {
1490 case LM85_REG_FAN(0): /* Read WORD data */
1491 case LM85_REG_FAN(1):
1492 case LM85_REG_FAN(2):
1493 case LM85_REG_FAN(3):
1494 case LM85_REG_FAN_MIN(0):
1495 case LM85_REG_FAN_MIN(1):
1496 case LM85_REG_FAN_MIN(2):
1497 case LM85_REG_FAN_MIN(3):
1498 case LM85_REG_ALARM1: /* Read both bytes at once */
1499 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
1500 res |= i2c_smbus_read_byte_data(client, reg + 1) << 8;
1501 break;
1502 default: /* Read BYTE data */
1503 res = i2c_smbus_read_byte_data(client, reg);
1504 break;
1507 return res;
1510 static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
1512 switch (reg) {
1513 case LM85_REG_FAN(0): /* Write WORD data */
1514 case LM85_REG_FAN(1):
1515 case LM85_REG_FAN(2):
1516 case LM85_REG_FAN(3):
1517 case LM85_REG_FAN_MIN(0):
1518 case LM85_REG_FAN_MIN(1):
1519 case LM85_REG_FAN_MIN(2):
1520 case LM85_REG_FAN_MIN(3):
1521 /* NOTE: ALARM is read only, so not included here */
1522 i2c_smbus_write_byte_data(client, reg, value & 0xff);
1523 i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
1524 break;
1525 default: /* Write BYTE data */
1526 i2c_smbus_write_byte_data(client, reg, value);
1527 break;
1531 static struct lm85_data *lm85_update_device(struct device *dev)
1533 struct i2c_client *client = to_i2c_client(dev);
1534 struct lm85_data *data = i2c_get_clientdata(client);
1535 int i;
1537 mutex_lock(&data->update_lock);
1539 if (!data->valid ||
1540 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
1541 /* Things that change quickly */
1542 dev_dbg(&client->dev, "Reading sensor values\n");
1545 * Have to read extended bits first to "freeze" the
1546 * more significant bits that are read later.
1547 * There are 2 additional resolution bits per channel and we
1548 * have room for 4, so we shift them to the left.
1550 if (data->type == adm1027 || data->type == adt7463 ||
1551 data->type == adt7468) {
1552 int ext1 = lm85_read_value(client,
1553 ADM1027_REG_EXTEND_ADC1);
1554 int ext2 = lm85_read_value(client,
1555 ADM1027_REG_EXTEND_ADC2);
1556 int val = (ext1 << 8) + ext2;
1558 for (i = 0; i <= 4; i++)
1559 data->in_ext[i] =
1560 ((val >> (i * 2)) & 0x03) << 2;
1562 for (i = 0; i <= 2; i++)
1563 data->temp_ext[i] =
1564 (val >> ((i + 4) * 2)) & 0x0c;
1567 data->vid = lm85_read_value(client, LM85_REG_VID);
1569 for (i = 0; i <= 3; ++i) {
1570 data->in[i] =
1571 lm85_read_value(client, LM85_REG_IN(i));
1572 data->fan[i] =
1573 lm85_read_value(client, LM85_REG_FAN(i));
1576 if (!data->has_vid5)
1577 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
1579 if (data->type == adt7468)
1580 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1582 for (i = 0; i <= 2; ++i) {
1583 data->temp[i] =
1584 lm85_read_value(client, LM85_REG_TEMP(i));
1585 data->pwm[i] =
1586 lm85_read_value(client, LM85_REG_PWM(i));
1588 if (IS_ADT7468_OFF64(data))
1589 data->temp[i] -= 64;
1592 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1594 if (data->type == emc6d100) {
1595 /* Three more voltage sensors */
1596 for (i = 5; i <= 7; ++i) {
1597 data->in[i] = lm85_read_value(client,
1598 EMC6D100_REG_IN(i));
1600 /* More alarm bits */
1601 data->alarms |= lm85_read_value(client,
1602 EMC6D100_REG_ALARM3) << 16;
1603 } else if (data->type == emc6d102 || data->type == emc6d103 ||
1604 data->type == emc6d103s) {
1606 * Have to read LSB bits after the MSB ones because
1607 * the reading of the MSB bits has frozen the
1608 * LSBs (backward from the ADM1027).
1610 int ext1 = lm85_read_value(client,
1611 EMC6D102_REG_EXTEND_ADC1);
1612 int ext2 = lm85_read_value(client,
1613 EMC6D102_REG_EXTEND_ADC2);
1614 int ext3 = lm85_read_value(client,
1615 EMC6D102_REG_EXTEND_ADC3);
1616 int ext4 = lm85_read_value(client,
1617 EMC6D102_REG_EXTEND_ADC4);
1618 data->in_ext[0] = ext3 & 0x0f;
1619 data->in_ext[1] = ext4 & 0x0f;
1620 data->in_ext[2] = ext4 >> 4;
1621 data->in_ext[3] = ext3 >> 4;
1622 data->in_ext[4] = ext2 >> 4;
1624 data->temp_ext[0] = ext1 & 0x0f;
1625 data->temp_ext[1] = ext2 & 0x0f;
1626 data->temp_ext[2] = ext1 >> 4;
1629 data->last_reading = jiffies;
1630 } /* last_reading */
1632 if (!data->valid ||
1633 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
1634 /* Things that don't change often */
1635 dev_dbg(&client->dev, "Reading config values\n");
1637 for (i = 0; i <= 3; ++i) {
1638 data->in_min[i] =
1639 lm85_read_value(client, LM85_REG_IN_MIN(i));
1640 data->in_max[i] =
1641 lm85_read_value(client, LM85_REG_IN_MAX(i));
1642 data->fan_min[i] =
1643 lm85_read_value(client, LM85_REG_FAN_MIN(i));
1646 if (!data->has_vid5) {
1647 data->in_min[4] = lm85_read_value(client,
1648 LM85_REG_IN_MIN(4));
1649 data->in_max[4] = lm85_read_value(client,
1650 LM85_REG_IN_MAX(4));
1653 if (data->type == emc6d100) {
1654 for (i = 5; i <= 7; ++i) {
1655 data->in_min[i] = lm85_read_value(client,
1656 EMC6D100_REG_IN_MIN(i));
1657 data->in_max[i] = lm85_read_value(client,
1658 EMC6D100_REG_IN_MAX(i));
1662 for (i = 0; i <= 2; ++i) {
1663 int val;
1665 data->temp_min[i] =
1666 lm85_read_value(client, LM85_REG_TEMP_MIN(i));
1667 data->temp_max[i] =
1668 lm85_read_value(client, LM85_REG_TEMP_MAX(i));
1670 data->autofan[i].config =
1671 lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
1672 val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
1673 data->pwm_freq[i] = val & 0x07;
1674 data->zone[i].range = val >> 4;
1675 data->autofan[i].min_pwm =
1676 lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
1677 data->zone[i].limit =
1678 lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
1679 data->zone[i].critical =
1680 lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
1682 if (IS_ADT7468_OFF64(data)) {
1683 data->temp_min[i] -= 64;
1684 data->temp_max[i] -= 64;
1685 data->zone[i].limit -= 64;
1686 data->zone[i].critical -= 64;
1690 if (data->type != emc6d103s) {
1691 i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
1692 data->autofan[0].min_off = (i & 0x20) != 0;
1693 data->autofan[1].min_off = (i & 0x40) != 0;
1694 data->autofan[2].min_off = (i & 0x80) != 0;
1696 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1697 data->zone[0].hyst = i >> 4;
1698 data->zone[1].hyst = i & 0x0f;
1700 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1701 data->zone[2].hyst = i >> 4;
1704 data->last_config = jiffies;
1705 } /* last_config */
1707 data->valid = 1;
1709 mutex_unlock(&data->update_lock);
1711 return data;
1714 module_i2c_driver(lm85_driver);
1716 MODULE_LICENSE("GPL");
1717 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1718 "Margit Schubert-While <margitsw@t-online.de>, "
1719 "Justin Thiessen <jthiessen@penguincomputing.com>");
1720 MODULE_DESCRIPTION("LM85-B, LM85-C driver");