1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
39static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40
41enum chips {
42 any_chip, lm85b, lm85c,
43 adm1027, adt7463, adt7468,
44 emc6d100, emc6d102, emc6d103, emc6d103s
45};
46
47
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
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
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
96
97#define LM85_REG_CONFIG 0x40
98
99#define LM85_REG_ALARM1 0x41
100#define LM85_REG_ALARM2 0x42
101
102#define LM85_REG_VID 0x43
103
104
105#define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
106#define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
107#define LM85_REG_AFAN_SPIKE1 0x62
108#define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
109#define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
110#define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
111#define LM85_REG_AFAN_HYST1 0x6d
112#define LM85_REG_AFAN_HYST2 0x6e
113
114#define ADM1027_REG_EXTEND_ADC1 0x76
115#define ADM1027_REG_EXTEND_ADC2 0x77
116
117#define EMC6D100_REG_ALARM3 0x7d
118
119#define EMC6D100_REG_IN(nr) (0x70 + ((nr) - 5))
120#define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr) - 5) * 2)
121#define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr) - 5) * 2)
122#define EMC6D102_REG_EXTEND_ADC1 0x85
123#define EMC6D102_REG_EXTEND_ADC2 0x86
124#define EMC6D102_REG_EXTEND_ADC3 0x87
125#define EMC6D102_REG_EXTEND_ADC4 0x88
126
127
128
129
130
131
132
133
134
135static const int lm85_scaling[] = {
136 2500, 2250, 3300, 5000, 12000,
137 3300, 1500, 1800
138};
139#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
140
141#define INS_TO_REG(n, val) \
142 SENSORS_LIMIT(SCALE(val, lm85_scaling[n], 192), 0, 255)
143
144#define INSEXT_FROM_REG(n, val, ext) \
145 SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
146
147#define INS_FROM_REG(n, val) SCALE((val), 192, lm85_scaling[n])
148
149
150static inline u16 FAN_TO_REG(unsigned long val)
151{
152 if (!val)
153 return 0xffff;
154 return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
155}
156#define FAN_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
157 5400000 / (val))
158
159
160#define TEMP_TO_REG(val) \
161 SENSORS_LIMIT(SCALE(val, 1000, 1), -127, 127)
162#define TEMPEXT_FROM_REG(val, ext) \
163 SCALE(((val) << 4) + (ext), 16, 1000)
164#define TEMP_FROM_REG(val) ((val) * 1000)
165
166#define PWM_TO_REG(val) SENSORS_LIMIT(val, 0, 255)
167#define PWM_FROM_REG(val) (val)
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190static const int lm85_range_map[] = {
191 2000, 2500, 3300, 4000, 5000, 6600, 8000, 10000,
192 13300, 16000, 20000, 26600, 32000, 40000, 53300, 80000
193};
194
195static int RANGE_TO_REG(int range)
196{
197 int i;
198
199
200 for (i = 0; i < 15; ++i) {
201 if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
202 break;
203 }
204
205 return i;
206}
207#define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f]
208
209
210static const int lm85_freq_map[8] = {
211 10, 15, 23, 30, 38, 47, 61, 94
212};
213static const int adm1027_freq_map[8] = {
214 11, 15, 22, 29, 35, 44, 59, 88
215};
216
217static int FREQ_TO_REG(const int *map, int freq)
218{
219 int i;
220
221
222 for (i = 0; i < 7; ++i)
223 if (freq <= (map[i] + map[i + 1]) / 2)
224 break;
225 return i;
226}
227
228static int FREQ_FROM_REG(const int *map, u8 reg)
229{
230 return map[reg & 0x07];
231}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246static const int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
247#define ZONE_FROM_REG(val) lm85_zone_map[(val) >> 5]
248
249static int ZONE_TO_REG(int zone)
250{
251 int i;
252
253 for (i = 0; i <= 7; ++i)
254 if (zone == lm85_zone_map[i])
255 break;
256 if (i > 7)
257 i = 3;
258 return i << 5;
259}
260
261#define HYST_TO_REG(val) SENSORS_LIMIT(((val) + 500) / 1000, 0, 15)
262#define HYST_FROM_REG(val) ((val) * 1000)
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278#define LM85_DATA_INTERVAL (HZ + HZ / 2)
279#define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
280
281
282
283
284
285
286struct lm85_zone {
287 s8 limit;
288 u8 hyst;
289 u8 range;
290 s8 critical;
291 u8 max_desired;
292
293
294
295
296};
297
298struct lm85_autofan {
299 u8 config;
300 u8 min_pwm;
301 u8 min_off;
302};
303
304
305
306
307
308struct lm85_data {
309 struct device *hwmon_dev;
310 const int *freq_map;
311 enum chips type;
312
313 bool has_vid5;
314
315 struct mutex update_lock;
316 int valid;
317 unsigned long last_reading;
318 unsigned long last_config;
319
320 u8 in[8];
321 u8 in_max[8];
322 u8 in_min[8];
323 s8 temp[3];
324 s8 temp_min[3];
325 s8 temp_max[3];
326 u16 fan[4];
327 u16 fan_min[4];
328 u8 pwm[3];
329 u8 pwm_freq[3];
330 u8 temp_ext[3];
331 u8 in_ext[8];
332 u8 vid;
333 u8 vrm;
334 u32 alarms;
335 u8 cfg5;
336 struct lm85_autofan autofan[3];
337 struct lm85_zone zone[3];
338};
339
340static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info);
341static int lm85_probe(struct i2c_client *client,
342 const struct i2c_device_id *id);
343static int lm85_remove(struct i2c_client *client);
344
345static int lm85_read_value(struct i2c_client *client, u8 reg);
346static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
347static struct lm85_data *lm85_update_device(struct device *dev);
348
349
350static 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 },
362 { }
363};
364MODULE_DEVICE_TABLE(i2c, lm85_id);
365
366static struct i2c_driver lm85_driver = {
367 .class = I2C_CLASS_HWMON,
368 .driver = {
369 .name = "lm85",
370 },
371 .probe = lm85_probe,
372 .remove = lm85_remove,
373 .id_table = lm85_id,
374 .detect = lm85_detect,
375 .address_list = normal_i2c,
376};
377
378
379
380static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
381 char *buf)
382{
383 int nr = to_sensor_dev_attr(attr)->index;
384 struct lm85_data *data = lm85_update_device(dev);
385 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr]));
386}
387
388static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
389 char *buf)
390{
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]));
394}
395
396static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
398{
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;
404
405 err = kstrtoul(buf, 10, &val);
406 if (err)
407 return err;
408
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;
414}
415
416#define show_fan_offset(offset) \
417static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
418 show_fan, NULL, offset - 1); \
419static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
420 show_fan_min, set_fan_min, offset - 1)
421
422show_fan_offset(1);
423show_fan_offset(2);
424show_fan_offset(3);
425show_fan_offset(4);
426
427
428
429static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
430 char *buf)
431{
432 struct lm85_data *data = lm85_update_device(dev);
433 int vid;
434
435 if (data->has_vid5) {
436
437 vid = vid_from_reg(data->vid & 0x3f, data->vrm);
438 } else {
439
440 vid = vid_from_reg(data->vid & 0x1f, data->vrm);
441 }
442
443 return sprintf(buf, "%d\n", vid);
444}
445
446static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
447
448static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
449 char *buf)
450{
451 struct lm85_data *data = dev_get_drvdata(dev);
452 return sprintf(buf, "%ld\n", (long) data->vrm);
453}
454
455static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
456 const char *buf, size_t count)
457{
458 struct lm85_data *data = dev_get_drvdata(dev);
459 unsigned long val;
460 int err;
461
462 err = kstrtoul(buf, 10, &val);
463 if (err)
464 return err;
465
466 data->vrm = val;
467 return count;
468}
469
470static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
471
472static ssize_t show_alarms_reg(struct device *dev, struct device_attribute
473 *attr, char *buf)
474{
475 struct lm85_data *data = lm85_update_device(dev);
476 return sprintf(buf, "%u\n", data->alarms);
477}
478
479static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
480
481static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
482 char *buf)
483{
484 int nr = to_sensor_dev_attr(attr)->index;
485 struct lm85_data *data = lm85_update_device(dev);
486 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
487}
488
489static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
490static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
491static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
492static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
493static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
494static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
495static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
496static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
497static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
498static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
499static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
500static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
501static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
502static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
503static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
504static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
505static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
506
507
508
509static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
510 char *buf)
511{
512 int nr = to_sensor_dev_attr(attr)->index;
513 struct lm85_data *data = lm85_update_device(dev);
514 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
515}
516
517static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
518 const char *buf, size_t count)
519{
520 int nr = to_sensor_dev_attr(attr)->index;
521 struct i2c_client *client = to_i2c_client(dev);
522 struct lm85_data *data = i2c_get_clientdata(client);
523 unsigned long val;
524 int err;
525
526 err = kstrtoul(buf, 10, &val);
527 if (err)
528 return err;
529
530 mutex_lock(&data->update_lock);
531 data->pwm[nr] = PWM_TO_REG(val);
532 lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
533 mutex_unlock(&data->update_lock);
534 return count;
535}
536
537static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
538 *attr, char *buf)
539{
540 int nr = to_sensor_dev_attr(attr)->index;
541 struct lm85_data *data = lm85_update_device(dev);
542 int pwm_zone, enable;
543
544 pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
545 switch (pwm_zone) {
546 case -1:
547 enable = 0;
548 break;
549 case 0:
550 case -2:
551 enable = 1;
552 break;
553 default:
554 enable = 2;
555 }
556 return sprintf(buf, "%d\n", enable);
557}
558
559static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
560 *attr, const char *buf, size_t count)
561{
562 int nr = to_sensor_dev_attr(attr)->index;
563 struct i2c_client *client = to_i2c_client(dev);
564 struct lm85_data *data = i2c_get_clientdata(client);
565 u8 config;
566 unsigned long val;
567 int err;
568
569 err = kstrtoul(buf, 10, &val);
570 if (err)
571 return err;
572
573 switch (val) {
574 case 0:
575 config = 3;
576 break;
577 case 1:
578 config = 7;
579 break;
580 case 2:
581
582
583
584
585 config = 6;
586 break;
587 default:
588 return -EINVAL;
589 }
590
591 mutex_lock(&data->update_lock);
592 data->autofan[nr].config = lm85_read_value(client,
593 LM85_REG_AFAN_CONFIG(nr));
594 data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
595 | (config << 5);
596 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
597 data->autofan[nr].config);
598 mutex_unlock(&data->update_lock);
599 return count;
600}
601
602static ssize_t show_pwm_freq(struct device *dev,
603 struct device_attribute *attr, char *buf)
604{
605 int nr = to_sensor_dev_attr(attr)->index;
606 struct lm85_data *data = lm85_update_device(dev);
607 int freq;
608
609 if (IS_ADT7468_HFPWM(data))
610 freq = 22500;
611 else
612 freq = FREQ_FROM_REG(data->freq_map, data->pwm_freq[nr]);
613
614 return sprintf(buf, "%d\n", freq);
615}
616
617static ssize_t set_pwm_freq(struct device *dev,
618 struct device_attribute *attr, const char *buf, size_t count)
619{
620 int nr = to_sensor_dev_attr(attr)->index;
621 struct i2c_client *client = to_i2c_client(dev);
622 struct lm85_data *data = i2c_get_clientdata(client);
623 unsigned long val;
624 int err;
625
626 err = kstrtoul(buf, 10, &val);
627 if (err)
628 return err;
629
630 mutex_lock(&data->update_lock);
631
632
633
634
635
636 if (data->type == adt7468 && val >= 11300) {
637 data->cfg5 &= ~ADT7468_HFPWM;
638 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
639 } else {
640 data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val);
641 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
642 (data->zone[nr].range << 4)
643 | data->pwm_freq[nr]);
644 if (data->type == adt7468) {
645 data->cfg5 |= ADT7468_HFPWM;
646 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5);
647 }
648 }
649 mutex_unlock(&data->update_lock);
650 return count;
651}
652
653#define show_pwm_reg(offset) \
654static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
655 show_pwm, set_pwm, offset - 1); \
656static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
657 show_pwm_enable, set_pwm_enable, offset - 1); \
658static SENSOR_DEVICE_ATTR(pwm##offset##_freq, S_IRUGO | S_IWUSR, \
659 show_pwm_freq, set_pwm_freq, offset - 1)
660
661show_pwm_reg(1);
662show_pwm_reg(2);
663show_pwm_reg(3);
664
665
666
667static ssize_t show_in(struct device *dev, struct device_attribute *attr,
668 char *buf)
669{
670 int nr = to_sensor_dev_attr(attr)->index;
671 struct lm85_data *data = lm85_update_device(dev);
672 return sprintf(buf, "%d\n", INSEXT_FROM_REG(nr, data->in[nr],
673 data->in_ext[nr]));
674}
675
676static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
677 char *buf)
678{
679 int nr = to_sensor_dev_attr(attr)->index;
680 struct lm85_data *data = lm85_update_device(dev);
681 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
682}
683
684static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
686{
687 int nr = to_sensor_dev_attr(attr)->index;
688 struct i2c_client *client = to_i2c_client(dev);
689 struct lm85_data *data = i2c_get_clientdata(client);
690 long val;
691 int err;
692
693 err = kstrtol(buf, 10, &val);
694 if (err)
695 return err;
696
697 mutex_lock(&data->update_lock);
698 data->in_min[nr] = INS_TO_REG(nr, val);
699 lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
700 mutex_unlock(&data->update_lock);
701 return count;
702}
703
704static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
705 char *buf)
706{
707 int nr = to_sensor_dev_attr(attr)->index;
708 struct lm85_data *data = lm85_update_device(dev);
709 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
710}
711
712static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
713 const char *buf, size_t count)
714{
715 int nr = to_sensor_dev_attr(attr)->index;
716 struct i2c_client *client = to_i2c_client(dev);
717 struct lm85_data *data = i2c_get_clientdata(client);
718 long val;
719 int err;
720
721 err = kstrtol(buf, 10, &val);
722 if (err)
723 return err;
724
725 mutex_lock(&data->update_lock);
726 data->in_max[nr] = INS_TO_REG(nr, val);
727 lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
728 mutex_unlock(&data->update_lock);
729 return count;
730}
731
732#define show_in_reg(offset) \
733static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
734 show_in, NULL, offset); \
735static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
736 show_in_min, set_in_min, offset); \
737static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
738 show_in_max, set_in_max, offset)
739
740show_in_reg(0);
741show_in_reg(1);
742show_in_reg(2);
743show_in_reg(3);
744show_in_reg(4);
745show_in_reg(5);
746show_in_reg(6);
747show_in_reg(7);
748
749
750
751static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
752 char *buf)
753{
754 int nr = to_sensor_dev_attr(attr)->index;
755 struct lm85_data *data = lm85_update_device(dev);
756 return sprintf(buf, "%d\n", TEMPEXT_FROM_REG(data->temp[nr],
757 data->temp_ext[nr]));
758}
759
760static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
761 char *buf)
762{
763 int nr = to_sensor_dev_attr(attr)->index;
764 struct lm85_data *data = lm85_update_device(dev);
765 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
766}
767
768static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
769 const char *buf, size_t count)
770{
771 int nr = to_sensor_dev_attr(attr)->index;
772 struct i2c_client *client = to_i2c_client(dev);
773 struct lm85_data *data = i2c_get_clientdata(client);
774 long val;
775 int err;
776
777 err = kstrtol(buf, 10, &val);
778 if (err)
779 return err;
780
781 if (IS_ADT7468_OFF64(data))
782 val += 64;
783
784 mutex_lock(&data->update_lock);
785 data->temp_min[nr] = TEMP_TO_REG(val);
786 lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
787 mutex_unlock(&data->update_lock);
788 return count;
789}
790
791static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
792 char *buf)
793{
794 int nr = to_sensor_dev_attr(attr)->index;
795 struct lm85_data *data = lm85_update_device(dev);
796 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
797}
798
799static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
800 const char *buf, size_t count)
801{
802 int nr = to_sensor_dev_attr(attr)->index;
803 struct i2c_client *client = to_i2c_client(dev);
804 struct lm85_data *data = i2c_get_clientdata(client);
805 long val;
806 int err;
807
808 err = kstrtol(buf, 10, &val);
809 if (err)
810 return err;
811
812 if (IS_ADT7468_OFF64(data))
813 val += 64;
814
815 mutex_lock(&data->update_lock);
816 data->temp_max[nr] = TEMP_TO_REG(val);
817 lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
818 mutex_unlock(&data->update_lock);
819 return count;
820}
821
822#define show_temp_reg(offset) \
823static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
824 show_temp, NULL, offset - 1); \
825static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
826 show_temp_min, set_temp_min, offset - 1); \
827static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
828 show_temp_max, set_temp_max, offset - 1);
829
830show_temp_reg(1);
831show_temp_reg(2);
832show_temp_reg(3);
833
834
835
836
837static ssize_t show_pwm_auto_channels(struct device *dev,
838 struct device_attribute *attr, char *buf)
839{
840 int nr = to_sensor_dev_attr(attr)->index;
841 struct lm85_data *data = lm85_update_device(dev);
842 return sprintf(buf, "%d\n", ZONE_FROM_REG(data->autofan[nr].config));
843}
844
845static ssize_t set_pwm_auto_channels(struct device *dev,
846 struct device_attribute *attr, const char *buf, size_t count)
847{
848 int nr = to_sensor_dev_attr(attr)->index;
849 struct i2c_client *client = to_i2c_client(dev);
850 struct lm85_data *data = i2c_get_clientdata(client);
851 long val;
852 int err;
853
854 err = kstrtol(buf, 10, &val);
855 if (err)
856 return err;
857
858 mutex_lock(&data->update_lock);
859 data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
860 | ZONE_TO_REG(val);
861 lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
862 data->autofan[nr].config);
863 mutex_unlock(&data->update_lock);
864 return count;
865}
866
867static ssize_t show_pwm_auto_pwm_min(struct device *dev,
868 struct device_attribute *attr, char *buf)
869{
870 int nr = to_sensor_dev_attr(attr)->index;
871 struct lm85_data *data = lm85_update_device(dev);
872 return sprintf(buf, "%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
873}
874
875static ssize_t set_pwm_auto_pwm_min(struct device *dev,
876 struct device_attribute *attr, const char *buf, size_t count)
877{
878 int nr = to_sensor_dev_attr(attr)->index;
879 struct i2c_client *client = to_i2c_client(dev);
880 struct lm85_data *data = i2c_get_clientdata(client);
881 unsigned long val;
882 int err;
883
884 err = kstrtoul(buf, 10, &val);
885 if (err)
886 return err;
887
888 mutex_lock(&data->update_lock);
889 data->autofan[nr].min_pwm = PWM_TO_REG(val);
890 lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
891 data->autofan[nr].min_pwm);
892 mutex_unlock(&data->update_lock);
893 return count;
894}
895
896static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
897 struct device_attribute *attr, char *buf)
898{
899 int nr = to_sensor_dev_attr(attr)->index;
900 struct lm85_data *data = lm85_update_device(dev);
901 return sprintf(buf, "%d\n", data->autofan[nr].min_off);
902}
903
904static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
905 struct device_attribute *attr, const char *buf, size_t count)
906{
907 int nr = to_sensor_dev_attr(attr)->index;
908 struct i2c_client *client = to_i2c_client(dev);
909 struct lm85_data *data = i2c_get_clientdata(client);
910 u8 tmp;
911 long val;
912 int err;
913
914 err = kstrtol(buf, 10, &val);
915 if (err)
916 return err;
917
918 mutex_lock(&data->update_lock);
919 data->autofan[nr].min_off = val;
920 tmp = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
921 tmp &= ~(0x20 << nr);
922 if (data->autofan[nr].min_off)
923 tmp |= 0x20 << nr;
924 lm85_write_value(client, LM85_REG_AFAN_SPIKE1, tmp);
925 mutex_unlock(&data->update_lock);
926 return count;
927}
928
929#define pwm_auto(offset) \
930static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
931 S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
932 set_pwm_auto_channels, offset - 1); \
933static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
934 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
935 set_pwm_auto_pwm_min, offset - 1); \
936static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
937 S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
938 set_pwm_auto_pwm_minctl, offset - 1)
939
940pwm_auto(1);
941pwm_auto(2);
942pwm_auto(3);
943
944
945
946static ssize_t show_temp_auto_temp_off(struct device *dev,
947 struct device_attribute *attr, char *buf)
948{
949 int nr = to_sensor_dev_attr(attr)->index;
950 struct lm85_data *data = lm85_update_device(dev);
951 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
952 HYST_FROM_REG(data->zone[nr].hyst));
953}
954
955static ssize_t set_temp_auto_temp_off(struct device *dev,
956 struct device_attribute *attr, const char *buf, size_t count)
957{
958 int nr = to_sensor_dev_attr(attr)->index;
959 struct i2c_client *client = to_i2c_client(dev);
960 struct lm85_data *data = i2c_get_clientdata(client);
961 int min;
962 long val;
963 int err;
964
965 err = kstrtol(buf, 10, &val);
966 if (err)
967 return err;
968
969 mutex_lock(&data->update_lock);
970 min = TEMP_FROM_REG(data->zone[nr].limit);
971 data->zone[nr].hyst = HYST_TO_REG(min - val);
972 if (nr == 0 || nr == 1) {
973 lm85_write_value(client, LM85_REG_AFAN_HYST1,
974 (data->zone[0].hyst << 4)
975 | data->zone[1].hyst);
976 } else {
977 lm85_write_value(client, LM85_REG_AFAN_HYST2,
978 (data->zone[2].hyst << 4));
979 }
980 mutex_unlock(&data->update_lock);
981 return count;
982}
983
984static ssize_t show_temp_auto_temp_min(struct device *dev,
985 struct device_attribute *attr, char *buf)
986{
987 int nr = to_sensor_dev_attr(attr)->index;
988 struct lm85_data *data = lm85_update_device(dev);
989 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit));
990}
991
992static ssize_t set_temp_auto_temp_min(struct device *dev,
993 struct device_attribute *attr, const char *buf, size_t count)
994{
995 int nr = to_sensor_dev_attr(attr)->index;
996 struct i2c_client *client = to_i2c_client(dev);
997 struct lm85_data *data = i2c_get_clientdata(client);
998 long val;
999 int err;
1000
1001 err = kstrtol(buf, 10, &val);
1002 if (err)
1003 return err;
1004
1005 mutex_lock(&data->update_lock);
1006 data->zone[nr].limit = TEMP_TO_REG(val);
1007 lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
1008 data->zone[nr].limit);
1009
1010
1011 data->zone[nr].range = RANGE_TO_REG(
1012 TEMP_FROM_REG(data->zone[nr].max_desired) -
1013 TEMP_FROM_REG(data->zone[nr].limit));
1014 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1015 ((data->zone[nr].range & 0x0f) << 4)
1016 | (data->pwm_freq[nr] & 0x07));
1017
1018 mutex_unlock(&data->update_lock);
1019 return count;
1020}
1021
1022static ssize_t show_temp_auto_temp_max(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1024{
1025 int nr = to_sensor_dev_attr(attr)->index;
1026 struct lm85_data *data = lm85_update_device(dev);
1027 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
1028 RANGE_FROM_REG(data->zone[nr].range));
1029}
1030
1031static ssize_t set_temp_auto_temp_max(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t count)
1033{
1034 int nr = to_sensor_dev_attr(attr)->index;
1035 struct i2c_client *client = to_i2c_client(dev);
1036 struct lm85_data *data = i2c_get_clientdata(client);
1037 int min;
1038 long val;
1039 int err;
1040
1041 err = kstrtol(buf, 10, &val);
1042 if (err)
1043 return err;
1044
1045 mutex_lock(&data->update_lock);
1046 min = TEMP_FROM_REG(data->zone[nr].limit);
1047 data->zone[nr].max_desired = TEMP_TO_REG(val);
1048 data->zone[nr].range = RANGE_TO_REG(
1049 val - min);
1050 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
1051 ((data->zone[nr].range & 0x0f) << 4)
1052 | (data->pwm_freq[nr] & 0x07));
1053 mutex_unlock(&data->update_lock);
1054 return count;
1055}
1056
1057static ssize_t show_temp_auto_temp_crit(struct device *dev,
1058 struct device_attribute *attr, char *buf)
1059{
1060 int nr = to_sensor_dev_attr(attr)->index;
1061 struct lm85_data *data = lm85_update_device(dev);
1062 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->zone[nr].critical));
1063}
1064
1065static ssize_t set_temp_auto_temp_crit(struct device *dev,
1066 struct device_attribute *attr, const char *buf, size_t count)
1067{
1068 int nr = to_sensor_dev_attr(attr)->index;
1069 struct i2c_client *client = to_i2c_client(dev);
1070 struct lm85_data *data = i2c_get_clientdata(client);
1071 long val;
1072 int err;
1073
1074 err = kstrtol(buf, 10, &val);
1075 if (err)
1076 return err;
1077
1078 mutex_lock(&data->update_lock);
1079 data->zone[nr].critical = TEMP_TO_REG(val);
1080 lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
1081 data->zone[nr].critical);
1082 mutex_unlock(&data->update_lock);
1083 return count;
1084}
1085
1086#define temp_auto(offset) \
1087static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
1088 S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
1089 set_temp_auto_temp_off, offset - 1); \
1090static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
1091 S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
1092 set_temp_auto_temp_min, offset - 1); \
1093static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
1094 S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
1095 set_temp_auto_temp_max, offset - 1); \
1096static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
1097 S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
1098 set_temp_auto_temp_crit, offset - 1);
1099
1100temp_auto(1);
1101temp_auto(2);
1102temp_auto(3);
1103
1104static struct attribute *lm85_attributes[] = {
1105 &sensor_dev_attr_fan1_input.dev_attr.attr,
1106 &sensor_dev_attr_fan2_input.dev_attr.attr,
1107 &sensor_dev_attr_fan3_input.dev_attr.attr,
1108 &sensor_dev_attr_fan4_input.dev_attr.attr,
1109 &sensor_dev_attr_fan1_min.dev_attr.attr,
1110 &sensor_dev_attr_fan2_min.dev_attr.attr,
1111 &sensor_dev_attr_fan3_min.dev_attr.attr,
1112 &sensor_dev_attr_fan4_min.dev_attr.attr,
1113 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1114 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1115 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1116 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1117
1118 &sensor_dev_attr_pwm1.dev_attr.attr,
1119 &sensor_dev_attr_pwm2.dev_attr.attr,
1120 &sensor_dev_attr_pwm3.dev_attr.attr,
1121 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1122 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1123 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1124 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1125 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1126 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1127
1128 &sensor_dev_attr_in0_input.dev_attr.attr,
1129 &sensor_dev_attr_in1_input.dev_attr.attr,
1130 &sensor_dev_attr_in2_input.dev_attr.attr,
1131 &sensor_dev_attr_in3_input.dev_attr.attr,
1132 &sensor_dev_attr_in0_min.dev_attr.attr,
1133 &sensor_dev_attr_in1_min.dev_attr.attr,
1134 &sensor_dev_attr_in2_min.dev_attr.attr,
1135 &sensor_dev_attr_in3_min.dev_attr.attr,
1136 &sensor_dev_attr_in0_max.dev_attr.attr,
1137 &sensor_dev_attr_in1_max.dev_attr.attr,
1138 &sensor_dev_attr_in2_max.dev_attr.attr,
1139 &sensor_dev_attr_in3_max.dev_attr.attr,
1140 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1141 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1142 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1143 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1144
1145 &sensor_dev_attr_temp1_input.dev_attr.attr,
1146 &sensor_dev_attr_temp2_input.dev_attr.attr,
1147 &sensor_dev_attr_temp3_input.dev_attr.attr,
1148 &sensor_dev_attr_temp1_min.dev_attr.attr,
1149 &sensor_dev_attr_temp2_min.dev_attr.attr,
1150 &sensor_dev_attr_temp3_min.dev_attr.attr,
1151 &sensor_dev_attr_temp1_max.dev_attr.attr,
1152 &sensor_dev_attr_temp2_max.dev_attr.attr,
1153 &sensor_dev_attr_temp3_max.dev_attr.attr,
1154 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1155 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1156 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1157 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1158 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1159
1160 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
1161 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
1162 &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
1163 &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
1164 &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
1165 &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
1166
1167 &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
1168 &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
1169 &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
1170 &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
1171 &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
1172 &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
1173 &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
1174 &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
1175 &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
1176
1177 &dev_attr_vrm.attr,
1178 &dev_attr_cpu0_vid.attr,
1179 &dev_attr_alarms.attr,
1180 NULL
1181};
1182
1183static const struct attribute_group lm85_group = {
1184 .attrs = lm85_attributes,
1185};
1186
1187static struct attribute *lm85_attributes_minctl[] = {
1188 &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
1189 &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
1190 &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
1191 NULL
1192};
1193
1194static const struct attribute_group lm85_group_minctl = {
1195 .attrs = lm85_attributes_minctl,
1196};
1197
1198static struct attribute *lm85_attributes_temp_off[] = {
1199 &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
1200 &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
1201 &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
1202 NULL
1203};
1204
1205static const struct attribute_group lm85_group_temp_off = {
1206 .attrs = lm85_attributes_temp_off,
1207};
1208
1209static struct attribute *lm85_attributes_in4[] = {
1210 &sensor_dev_attr_in4_input.dev_attr.attr,
1211 &sensor_dev_attr_in4_min.dev_attr.attr,
1212 &sensor_dev_attr_in4_max.dev_attr.attr,
1213 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1214 NULL
1215};
1216
1217static const struct attribute_group lm85_group_in4 = {
1218 .attrs = lm85_attributes_in4,
1219};
1220
1221static struct attribute *lm85_attributes_in567[] = {
1222 &sensor_dev_attr_in5_input.dev_attr.attr,
1223 &sensor_dev_attr_in6_input.dev_attr.attr,
1224 &sensor_dev_attr_in7_input.dev_attr.attr,
1225 &sensor_dev_attr_in5_min.dev_attr.attr,
1226 &sensor_dev_attr_in6_min.dev_attr.attr,
1227 &sensor_dev_attr_in7_min.dev_attr.attr,
1228 &sensor_dev_attr_in5_max.dev_attr.attr,
1229 &sensor_dev_attr_in6_max.dev_attr.attr,
1230 &sensor_dev_attr_in7_max.dev_attr.attr,
1231 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1232 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1233 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1234 NULL
1235};
1236
1237static const struct attribute_group lm85_group_in567 = {
1238 .attrs = lm85_attributes_in567,
1239};
1240
1241static void lm85_init_client(struct i2c_client *client)
1242{
1243 int value;
1244
1245
1246 value = lm85_read_value(client, LM85_REG_CONFIG);
1247 if (!(value & 0x01)) {
1248 dev_info(&client->dev, "Starting monitoring\n");
1249 lm85_write_value(client, LM85_REG_CONFIG, value | 0x01);
1250 }
1251
1252
1253 if (value & 0x02)
1254 dev_warn(&client->dev, "Device configuration is locked\n");
1255 if (!(value & 0x04))
1256 dev_warn(&client->dev, "Device is not ready\n");
1257}
1258
1259static int lm85_is_fake(struct i2c_client *client)
1260{
1261
1262
1263
1264
1265
1266 int i;
1267 u8 in_temp, fan;
1268
1269 for (i = 0; i < 8; i++) {
1270 in_temp = i2c_smbus_read_byte_data(client, 0x20 + i);
1271 fan = i2c_smbus_read_byte_data(client, 0x28 + i);
1272 if (in_temp != 0x00 || fan != 0xff)
1273 return 0;
1274 }
1275
1276 return 1;
1277}
1278
1279
1280static int lm85_detect(struct i2c_client *client, struct i2c_board_info *info)
1281{
1282 struct i2c_adapter *adapter = client->adapter;
1283 int address = client->addr;
1284 const char *type_name;
1285 int company, verstep;
1286
1287 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1288
1289 return -ENODEV;
1290 }
1291
1292
1293 company = lm85_read_value(client, LM85_REG_COMPANY);
1294 verstep = lm85_read_value(client, LM85_REG_VERSTEP);
1295
1296 dev_dbg(&adapter->dev, "Detecting device at 0x%02x with "
1297 "COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1298 address, company, verstep);
1299
1300
1301 if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
1302 (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
1303 dev_dbg(&adapter->dev,
1304 "Autodetection failed: unsupported version\n");
1305 return -ENODEV;
1306 }
1307 type_name = "lm85";
1308
1309
1310 if (company == LM85_COMPANY_NATIONAL) {
1311 switch (verstep) {
1312 case LM85_VERSTEP_LM85C:
1313 type_name = "lm85c";
1314 break;
1315 case LM85_VERSTEP_LM85B:
1316 type_name = "lm85b";
1317 break;
1318 case LM85_VERSTEP_LM96000_1:
1319 case LM85_VERSTEP_LM96000_2:
1320
1321 if (lm85_is_fake(client)) {
1322 dev_dbg(&adapter->dev,
1323 "Found Winbond WPCD377I, ignoring\n");
1324 return -ENODEV;
1325 }
1326 break;
1327 }
1328 } else if (company == LM85_COMPANY_ANALOG_DEV) {
1329 switch (verstep) {
1330 case LM85_VERSTEP_ADM1027:
1331 type_name = "adm1027";
1332 break;
1333 case LM85_VERSTEP_ADT7463:
1334 case LM85_VERSTEP_ADT7463C:
1335 type_name = "adt7463";
1336 break;
1337 case LM85_VERSTEP_ADT7468_1:
1338 case LM85_VERSTEP_ADT7468_2:
1339 type_name = "adt7468";
1340 break;
1341 }
1342 } else if (company == LM85_COMPANY_SMSC) {
1343 switch (verstep) {
1344 case LM85_VERSTEP_EMC6D100_A0:
1345 case LM85_VERSTEP_EMC6D100_A1:
1346
1347 type_name = "emc6d100";
1348 break;
1349 case LM85_VERSTEP_EMC6D102:
1350 type_name = "emc6d102";
1351 break;
1352 case LM85_VERSTEP_EMC6D103_A0:
1353 case LM85_VERSTEP_EMC6D103_A1:
1354 type_name = "emc6d103";
1355 break;
1356 case LM85_VERSTEP_EMC6D103S:
1357 type_name = "emc6d103s";
1358 break;
1359 }
1360 } else {
1361 dev_dbg(&adapter->dev,
1362 "Autodetection failed: unknown vendor\n");
1363 return -ENODEV;
1364 }
1365
1366 strlcpy(info->type, type_name, I2C_NAME_SIZE);
1367
1368 return 0;
1369}
1370
1371static void lm85_remove_files(struct i2c_client *client, struct lm85_data *data)
1372{
1373 sysfs_remove_group(&client->dev.kobj, &lm85_group);
1374 if (data->type != emc6d103s) {
1375 sysfs_remove_group(&client->dev.kobj, &lm85_group_minctl);
1376 sysfs_remove_group(&client->dev.kobj, &lm85_group_temp_off);
1377 }
1378 if (!data->has_vid5)
1379 sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
1380 if (data->type == emc6d100)
1381 sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
1382}
1383
1384static int lm85_probe(struct i2c_client *client,
1385 const struct i2c_device_id *id)
1386{
1387 struct lm85_data *data;
1388 int err;
1389
1390 data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
1391 if (!data)
1392 return -ENOMEM;
1393
1394 i2c_set_clientdata(client, data);
1395 data->type = id->driver_data;
1396 mutex_init(&data->update_lock);
1397
1398
1399 switch (data->type) {
1400 case adm1027:
1401 case adt7463:
1402 case adt7468:
1403 case emc6d100:
1404 case emc6d102:
1405 case emc6d103:
1406 case emc6d103s:
1407 data->freq_map = adm1027_freq_map;
1408 break;
1409 default:
1410 data->freq_map = lm85_freq_map;
1411 }
1412
1413
1414 data->vrm = vid_which_vrm();
1415
1416
1417 lm85_init_client(client);
1418
1419
1420 err = sysfs_create_group(&client->dev.kobj, &lm85_group);
1421 if (err)
1422 goto err_kfree;
1423
1424
1425 if (data->type != emc6d103s) {
1426 err = sysfs_create_group(&client->dev.kobj, &lm85_group_minctl);
1427 if (err)
1428 goto err_remove_files;
1429 err = sysfs_create_group(&client->dev.kobj,
1430 &lm85_group_temp_off);
1431 if (err)
1432 goto err_remove_files;
1433 }
1434
1435
1436
1437
1438
1439 if (data->type == adt7463 || data->type == adt7468) {
1440 u8 vid = lm85_read_value(client, LM85_REG_VID);
1441 if (vid & 0x80)
1442 data->has_vid5 = true;
1443 }
1444
1445 if (!data->has_vid5) {
1446 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in4);
1447 if (err)
1448 goto err_remove_files;
1449 }
1450
1451
1452 if (data->type == emc6d100) {
1453 err = sysfs_create_group(&client->dev.kobj, &lm85_group_in567);
1454 if (err)
1455 goto err_remove_files;
1456 }
1457
1458 data->hwmon_dev = hwmon_device_register(&client->dev);
1459 if (IS_ERR(data->hwmon_dev)) {
1460 err = PTR_ERR(data->hwmon_dev);
1461 goto err_remove_files;
1462 }
1463
1464 return 0;
1465
1466
1467 err_remove_files:
1468 lm85_remove_files(client, data);
1469 err_kfree:
1470 kfree(data);
1471 return err;
1472}
1473
1474static int lm85_remove(struct i2c_client *client)
1475{
1476 struct lm85_data *data = i2c_get_clientdata(client);
1477 hwmon_device_unregister(data->hwmon_dev);
1478 lm85_remove_files(client, data);
1479 kfree(data);
1480 return 0;
1481}
1482
1483
1484static int lm85_read_value(struct i2c_client *client, u8 reg)
1485{
1486 int res;
1487
1488
1489 switch (reg) {
1490 case LM85_REG_FAN(0):
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:
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:
1503 res = i2c_smbus_read_byte_data(client, reg);
1504 break;
1505 }
1506
1507 return res;
1508}
1509
1510static void lm85_write_value(struct i2c_client *client, u8 reg, int value)
1511{
1512 switch (reg) {
1513 case LM85_REG_FAN(0):
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
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:
1526 i2c_smbus_write_byte_data(client, reg, value);
1527 break;
1528 }
1529}
1530
1531static struct lm85_data *lm85_update_device(struct device *dev)
1532{
1533 struct i2c_client *client = to_i2c_client(dev);
1534 struct lm85_data *data = i2c_get_clientdata(client);
1535 int i;
1536
1537 mutex_lock(&data->update_lock);
1538
1539 if (!data->valid ||
1540 time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL)) {
1541
1542 dev_dbg(&client->dev, "Reading sensor values\n");
1543
1544
1545
1546
1547
1548
1549
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;
1557
1558 for (i = 0; i <= 4; i++)
1559 data->in_ext[i] =
1560 ((val >> (i * 2)) & 0x03) << 2;
1561
1562 for (i = 0; i <= 2; i++)
1563 data->temp_ext[i] =
1564 (val >> ((i + 4) * 2)) & 0x0c;
1565 }
1566
1567 data->vid = lm85_read_value(client, LM85_REG_VID);
1568
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));
1574 }
1575
1576 if (!data->has_vid5)
1577 data->in[4] = lm85_read_value(client, LM85_REG_IN(4));
1578
1579 if (data->type == adt7468)
1580 data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
1581
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));
1587
1588 if (IS_ADT7468_OFF64(data))
1589 data->temp[i] -= 64;
1590 }
1591
1592 data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
1593
1594 if (data->type == emc6d100) {
1595
1596 for (i = 5; i <= 7; ++i) {
1597 data->in[i] = lm85_read_value(client,
1598 EMC6D100_REG_IN(i));
1599 }
1600
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) {
1605
1606
1607
1608
1609
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;
1623
1624 data->temp_ext[0] = ext1 & 0x0f;
1625 data->temp_ext[1] = ext2 & 0x0f;
1626 data->temp_ext[2] = ext1 >> 4;
1627 }
1628
1629 data->last_reading = jiffies;
1630 }
1631
1632 if (!data->valid ||
1633 time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL)) {
1634
1635 dev_dbg(&client->dev, "Reading config values\n");
1636
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));
1644 }
1645
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));
1651 }
1652
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));
1659 }
1660 }
1661
1662 for (i = 0; i <= 2; ++i) {
1663 int val;
1664
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));
1669
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));
1681
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;
1687 }
1688 }
1689
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;
1695
1696 i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
1697 data->zone[0].hyst = i >> 4;
1698 data->zone[1].hyst = i & 0x0f;
1699
1700 i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
1701 data->zone[2].hyst = i >> 4;
1702 }
1703
1704 data->last_config = jiffies;
1705 }
1706
1707 data->valid = 1;
1708
1709 mutex_unlock(&data->update_lock);
1710
1711 return data;
1712}
1713
1714module_i2c_driver(lm85_driver);
1715
1716MODULE_LICENSE("GPL");
1717MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1718 "Margit Schubert-While <margitsw@t-online.de>, "
1719 "Justin Thiessen <jthiessen@penguincomputing.com>");
1720MODULE_DESCRIPTION("LM85-B, LM85-C driver");
1721