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