Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
4 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
5 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
7 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
8 *
9 * Derived from the lm83 driver by Jean Delvare
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/i2c.h>
16#include <linux/hwmon.h>
17#include <linux/hwmon-sysfs.h>
18#include <linux/hwmon-vid.h>
19#include <linux/err.h>
20#include <linux/jiffies.h>
21#include <linux/of.h>
22#include <linux/util_macros.h>
23
24/* Indexes for the sysfs hooks */
25
26#define INPUT 0
27#define MIN 1
28#define MAX 2
29#define CONTROL 3
30#define OFFSET 3
31#define AUTOMIN 4
32#define THERM 5
33#define HYSTERSIS 6
34
35/*
36 * These are unique identifiers for the sysfs functions - unlike the
37 * numbers above, these are not also indexes into an array
38 */
39
40#define ALARM 9
41#define FAULT 10
42
43/* 7475 Common Registers */
44
45#define REG_DEVREV2 0x12 /* ADT7490 only */
46#define REG_IMON 0x1D /* ADT7490 only */
47
48#define REG_VTT 0x1E /* ADT7490 only */
49#define REG_EXTEND3 0x1F /* ADT7490 only */
50
51#define REG_VOLTAGE_BASE 0x20
52#define REG_TEMP_BASE 0x25
53#define REG_TACH_BASE 0x28
54#define REG_PWM_BASE 0x30
55#define REG_PWM_MAX_BASE 0x38
56
57#define REG_DEVID 0x3D
58#define REG_VENDID 0x3E
59#define REG_DEVID2 0x3F
60
61#define REG_CONFIG1 0x40
62
63#define REG_STATUS1 0x41
64#define REG_STATUS2 0x42
65
66#define REG_VID 0x43 /* ADT7476 only */
67
68#define REG_VOLTAGE_MIN_BASE 0x44
69#define REG_VOLTAGE_MAX_BASE 0x45
70
71#define REG_TEMP_MIN_BASE 0x4E
72#define REG_TEMP_MAX_BASE 0x4F
73
74#define REG_TACH_MIN_BASE 0x54
75
76#define REG_PWM_CONFIG_BASE 0x5C
77
78#define REG_TEMP_TRANGE_BASE 0x5F
79
80#define REG_ENHANCE_ACOUSTICS1 0x62
81#define REG_ENHANCE_ACOUSTICS2 0x63
82
83#define REG_PWM_MIN_BASE 0x64
84
85#define REG_TEMP_TMIN_BASE 0x67
86#define REG_TEMP_THERM_BASE 0x6A
87
88#define REG_REMOTE1_HYSTERSIS 0x6D
89#define REG_REMOTE2_HYSTERSIS 0x6E
90
91#define REG_TEMP_OFFSET_BASE 0x70
92
93#define REG_CONFIG2 0x73
94
95#define REG_EXTEND1 0x76
96#define REG_EXTEND2 0x77
97
98#define REG_CONFIG3 0x78
99#define REG_CONFIG5 0x7C
100#define REG_CONFIG4 0x7D
101
102#define REG_STATUS4 0x81 /* ADT7490 only */
103
104#define REG_VTT_MIN 0x84 /* ADT7490 only */
105#define REG_VTT_MAX 0x86 /* ADT7490 only */
106
107#define REG_IMON_MIN 0x85 /* ADT7490 only */
108#define REG_IMON_MAX 0x87 /* ADT7490 only */
109
110#define VID_VIDSEL 0x80 /* ADT7476 only */
111
112#define CONFIG2_ATTN 0x20
113
114#define CONFIG3_SMBALERT 0x01
115#define CONFIG3_THERM 0x02
116
117#define CONFIG4_PINFUNC 0x03
118#define CONFIG4_THERM 0x01
119#define CONFIG4_SMBALERT 0x02
120#define CONFIG4_MAXDUTY 0x08
121#define CONFIG4_ATTN_IN10 0x30
122#define CONFIG4_ATTN_IN43 0xC0
123
124#define CONFIG5_TWOSCOMP 0x01
125#define CONFIG5_TEMPOFFSET 0x02
126#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
127
128/* ADT7475 Settings */
129
130#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt or Imon */
131#define ADT7475_TEMP_COUNT 3
132#define ADT7475_TACH_COUNT 4
133#define ADT7475_PWM_COUNT 3
134
135/* Macro to read the registers */
136
137#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
138
139/* Macros to easily index the registers */
140
141#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
142#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
143
144#define PWM_REG(idx) (REG_PWM_BASE + (idx))
145#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
146#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
147#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
148
149#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
150#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
151#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
152
153#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
154#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
155#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
156#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
157#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
158#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
159#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
160
161static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
162
163enum chips { adt7473, adt7475, adt7476, adt7490 };
164
165static const struct i2c_device_id adt7475_id[] = {
166 { "adt7473", adt7473 },
167 { "adt7475", adt7475 },
168 { "adt7476", adt7476 },
169 { "adt7490", adt7490 },
170 { }
171};
172MODULE_DEVICE_TABLE(i2c, adt7475_id);
173
174static const struct of_device_id __maybe_unused adt7475_of_match[] = {
175 {
176 .compatible = "adi,adt7473",
177 .data = (void *)adt7473
178 },
179 {
180 .compatible = "adi,adt7475",
181 .data = (void *)adt7475
182 },
183 {
184 .compatible = "adi,adt7476",
185 .data = (void *)adt7476
186 },
187 {
188 .compatible = "adi,adt7490",
189 .data = (void *)adt7490
190 },
191 { },
192};
193MODULE_DEVICE_TABLE(of, adt7475_of_match);
194
195struct adt7475_data {
196 struct i2c_client *client;
197 struct mutex lock;
198
199 unsigned long measure_updated;
200 bool valid;
201
202 u8 config2;
203 u8 config4;
204 u8 config5;
205 u8 has_voltage;
206 u8 bypass_attn; /* Bypass voltage attenuator */
207 u8 has_pwm2:1;
208 u8 has_fan4:1;
209 u8 has_vid:1;
210 u32 alarms;
211 u16 voltage[3][7];
212 u16 temp[7][3];
213 u16 tach[2][4];
214 u8 pwm[4][3];
215 u8 range[3];
216 u8 pwmctl[3];
217 u8 pwmchan[3];
218 u8 enh_acoustics[2];
219
220 u8 vid;
221 u8 vrm;
222 const struct attribute_group *groups[10];
223};
224
225static struct i2c_driver adt7475_driver;
226static struct adt7475_data *adt7475_update_device(struct device *dev);
227static void adt7475_read_hystersis(struct i2c_client *client);
228static void adt7475_read_pwm(struct i2c_client *client, int index);
229
230/* Given a temp value, convert it to register value */
231
232static inline u16 temp2reg(struct adt7475_data *data, long val)
233{
234 u16 ret;
235
236 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
237 val = clamp_val(val, -64000, 191000);
238 ret = (val + 64500) / 1000;
239 } else {
240 val = clamp_val(val, -128000, 127000);
241 if (val < -500)
242 ret = (256500 + val) / 1000;
243 else
244 ret = (val + 500) / 1000;
245 }
246
247 return ret << 2;
248}
249
250/* Given a register value, convert it to a real temp value */
251
252static inline int reg2temp(struct adt7475_data *data, u16 reg)
253{
254 if (data->config5 & CONFIG5_TWOSCOMP) {
255 if (reg >= 512)
256 return (reg - 1024) * 250;
257 else
258 return reg * 250;
259 } else
260 return (reg - 256) * 250;
261}
262
263static inline int tach2rpm(u16 tach)
264{
265 if (tach == 0 || tach == 0xFFFF)
266 return 0;
267
268 return (90000 * 60) / tach;
269}
270
271static inline u16 rpm2tach(unsigned long rpm)
272{
273 if (rpm == 0)
274 return 0;
275
276 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
277}
278
279/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
280static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 2][2] = {
281 { 45, 94 }, /* +2.5V */
282 { 175, 525 }, /* Vccp */
283 { 68, 71 }, /* Vcc */
284 { 93, 47 }, /* +5V */
285 { 120, 20 }, /* +12V */
286 { 45, 45 }, /* Vtt */
287 { 45, 45 }, /* Imon */
288};
289
290static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
291{
292 const int *r = adt7473_in_scaling[channel];
293
294 if (bypass_attn & (1 << channel))
295 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
296 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
297}
298
299static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
300{
301 const int *r = adt7473_in_scaling[channel];
302 long reg;
303
304 if (bypass_attn & (1 << channel))
305 reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
306 else
307 reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
308 (r[0] + r[1]) * 2250);
309 return clamp_val(reg, 0, 1023) & (0xff << 2);
310}
311
312static int adt7475_read_word(struct i2c_client *client, int reg)
313{
314 int val1, val2;
315
316 val1 = i2c_smbus_read_byte_data(client, reg);
317 if (val1 < 0)
318 return val1;
319 val2 = i2c_smbus_read_byte_data(client, reg + 1);
320 if (val2 < 0)
321 return val2;
322
323 return val1 | (val2 << 8);
324}
325
326static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
327{
328 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
329 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
330}
331
332static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
333 char *buf)
334{
335 struct adt7475_data *data = adt7475_update_device(dev);
336 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
337 unsigned short val;
338
339 if (IS_ERR(data))
340 return PTR_ERR(data);
341
342 switch (sattr->nr) {
343 case ALARM:
344 return sprintf(buf, "%d\n",
345 (data->alarms >> sattr->index) & 1);
346 default:
347 val = data->voltage[sattr->nr][sattr->index];
348 return sprintf(buf, "%d\n",
349 reg2volt(sattr->index, val, data->bypass_attn));
350 }
351}
352
353static ssize_t voltage_store(struct device *dev,
354 struct device_attribute *attr, const char *buf,
355 size_t count)
356{
357
358 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
359 struct adt7475_data *data = dev_get_drvdata(dev);
360 struct i2c_client *client = data->client;
361 unsigned char reg;
362 long val;
363
364 if (kstrtol(buf, 10, &val))
365 return -EINVAL;
366
367 mutex_lock(&data->lock);
368
369 data->voltage[sattr->nr][sattr->index] =
370 volt2reg(sattr->index, val, data->bypass_attn);
371
372 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
373 if (sattr->nr == MIN)
374 reg = VOLTAGE_MIN_REG(sattr->index);
375 else
376 reg = VOLTAGE_MAX_REG(sattr->index);
377 } else if (sattr->index == 5) {
378 if (sattr->nr == MIN)
379 reg = REG_VTT_MIN;
380 else
381 reg = REG_VTT_MAX;
382 } else {
383 if (sattr->nr == MIN)
384 reg = REG_IMON_MIN;
385 else
386 reg = REG_IMON_MAX;
387 }
388
389 i2c_smbus_write_byte_data(client, reg,
390 data->voltage[sattr->nr][sattr->index] >> 2);
391 mutex_unlock(&data->lock);
392
393 return count;
394}
395
396static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
397 char *buf)
398{
399 struct adt7475_data *data = adt7475_update_device(dev);
400 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
401 int out;
402
403 if (IS_ERR(data))
404 return PTR_ERR(data);
405
406 switch (sattr->nr) {
407 case HYSTERSIS:
408 mutex_lock(&data->lock);
409 out = data->temp[sattr->nr][sattr->index];
410 if (sattr->index != 1)
411 out = (out >> 4) & 0xF;
412 else
413 out = (out & 0xF);
414 /*
415 * Show the value as an absolute number tied to
416 * THERM
417 */
418 out = reg2temp(data, data->temp[THERM][sattr->index]) -
419 out * 1000;
420 mutex_unlock(&data->lock);
421 break;
422
423 case OFFSET:
424 /*
425 * Offset is always 2's complement, regardless of the
426 * setting in CONFIG5
427 */
428 mutex_lock(&data->lock);
429 out = (s8)data->temp[sattr->nr][sattr->index];
430 if (data->config5 & CONFIG5_TEMPOFFSET)
431 out *= 1000;
432 else
433 out *= 500;
434 mutex_unlock(&data->lock);
435 break;
436
437 case ALARM:
438 out = (data->alarms >> (sattr->index + 4)) & 1;
439 break;
440
441 case FAULT:
442 /* Note - only for remote1 and remote2 */
443 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
444 break;
445
446 default:
447 /* All other temp values are in the configured format */
448 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
449 }
450
451 return sprintf(buf, "%d\n", out);
452}
453
454static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
455 const char *buf, size_t count)
456{
457 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
458 struct adt7475_data *data = dev_get_drvdata(dev);
459 struct i2c_client *client = data->client;
460 unsigned char reg = 0;
461 u8 out;
462 int temp;
463 long val;
464
465 if (kstrtol(buf, 10, &val))
466 return -EINVAL;
467
468 mutex_lock(&data->lock);
469
470 /* We need the config register in all cases for temp <-> reg conv. */
471 data->config5 = adt7475_read(REG_CONFIG5);
472
473 switch (sattr->nr) {
474 case OFFSET:
475 if (data->config5 & CONFIG5_TEMPOFFSET) {
476 val = clamp_val(val, -63000, 127000);
477 out = data->temp[OFFSET][sattr->index] = val / 1000;
478 } else {
479 val = clamp_val(val, -63000, 64000);
480 out = data->temp[OFFSET][sattr->index] = val / 500;
481 }
482 break;
483
484 case HYSTERSIS:
485 /*
486 * The value will be given as an absolute value, turn it
487 * into an offset based on THERM
488 */
489
490 /* Read fresh THERM and HYSTERSIS values from the chip */
491 data->temp[THERM][sattr->index] =
492 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
493 adt7475_read_hystersis(client);
494
495 temp = reg2temp(data, data->temp[THERM][sattr->index]);
496 val = clamp_val(val, temp - 15000, temp);
497 val = (temp - val) / 1000;
498
499 if (sattr->index != 1) {
500 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
501 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
502 } else {
503 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
504 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
505 }
506
507 out = data->temp[HYSTERSIS][sattr->index];
508 break;
509
510 default:
511 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
512
513 /*
514 * We maintain an extra 2 digits of precision for simplicity
515 * - shift those back off before writing the value
516 */
517 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
518 }
519
520 switch (sattr->nr) {
521 case MIN:
522 reg = TEMP_MIN_REG(sattr->index);
523 break;
524 case MAX:
525 reg = TEMP_MAX_REG(sattr->index);
526 break;
527 case OFFSET:
528 reg = TEMP_OFFSET_REG(sattr->index);
529 break;
530 case AUTOMIN:
531 reg = TEMP_TMIN_REG(sattr->index);
532 break;
533 case THERM:
534 reg = TEMP_THERM_REG(sattr->index);
535 break;
536 case HYSTERSIS:
537 if (sattr->index != 2)
538 reg = REG_REMOTE1_HYSTERSIS;
539 else
540 reg = REG_REMOTE2_HYSTERSIS;
541
542 break;
543 }
544
545 i2c_smbus_write_byte_data(client, reg, out);
546
547 mutex_unlock(&data->lock);
548 return count;
549}
550
551/* Assuming CONFIG6[SLOW] is 0 */
552static const int ad7475_st_map[] = {
553 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
554};
555
556static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
557 char *buf)
558{
559 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
560 struct adt7475_data *data = dev_get_drvdata(dev);
561 long val;
562
563 switch (sattr->index) {
564 case 0:
565 val = data->enh_acoustics[0] & 0xf;
566 break;
567 case 1:
568 val = data->enh_acoustics[1] & 0xf;
569 break;
570 case 2:
571 default:
572 val = (data->enh_acoustics[1] >> 4) & 0xf;
573 break;
574 }
575
576 if (val & 0x8)
577 return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
578 else
579 return sprintf(buf, "0\n");
580}
581
582static ssize_t temp_st_store(struct device *dev,
583 struct device_attribute *attr, const char *buf,
584 size_t count)
585{
586 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
587 struct adt7475_data *data = dev_get_drvdata(dev);
588 struct i2c_client *client = data->client;
589 unsigned char reg;
590 int shift, idx;
591 ulong val;
592
593 if (kstrtoul(buf, 10, &val))
594 return -EINVAL;
595
596 switch (sattr->index) {
597 case 0:
598 reg = REG_ENHANCE_ACOUSTICS1;
599 shift = 0;
600 idx = 0;
601 break;
602 case 1:
603 reg = REG_ENHANCE_ACOUSTICS2;
604 shift = 0;
605 idx = 1;
606 break;
607 case 2:
608 default:
609 reg = REG_ENHANCE_ACOUSTICS2;
610 shift = 4;
611 idx = 1;
612 break;
613 }
614
615 if (val > 0) {
616 val = find_closest_descending(val, ad7475_st_map,
617 ARRAY_SIZE(ad7475_st_map));
618 val |= 0x8;
619 }
620
621 mutex_lock(&data->lock);
622
623 data->enh_acoustics[idx] &= ~(0xf << shift);
624 data->enh_acoustics[idx] |= (val << shift);
625
626 i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
627
628 mutex_unlock(&data->lock);
629
630 return count;
631}
632
633/*
634 * Table of autorange values - the user will write the value in millidegrees,
635 * and we'll convert it
636 */
637static const int autorange_table[] = {
638 2000, 2500, 3330, 4000, 5000, 6670, 8000,
639 10000, 13330, 16000, 20000, 26670, 32000, 40000,
640 53330, 80000
641};
642
643static ssize_t point2_show(struct device *dev, struct device_attribute *attr,
644 char *buf)
645{
646 struct adt7475_data *data = adt7475_update_device(dev);
647 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
648 int out, val;
649
650 if (IS_ERR(data))
651 return PTR_ERR(data);
652
653 mutex_lock(&data->lock);
654 out = (data->range[sattr->index] >> 4) & 0x0F;
655 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
656 mutex_unlock(&data->lock);
657
658 return sprintf(buf, "%d\n", val + autorange_table[out]);
659}
660
661static ssize_t point2_store(struct device *dev, struct device_attribute *attr,
662 const char *buf, size_t count)
663{
664 struct adt7475_data *data = dev_get_drvdata(dev);
665 struct i2c_client *client = data->client;
666 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
667 int temp;
668 long val;
669
670 if (kstrtol(buf, 10, &val))
671 return -EINVAL;
672
673 mutex_lock(&data->lock);
674
675 /* Get a fresh copy of the needed registers */
676 data->config5 = adt7475_read(REG_CONFIG5);
677 data->temp[AUTOMIN][sattr->index] =
678 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
679 data->range[sattr->index] =
680 adt7475_read(TEMP_TRANGE_REG(sattr->index));
681
682 /*
683 * The user will write an absolute value, so subtract the start point
684 * to figure the range
685 */
686 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
687 val = clamp_val(val, temp + autorange_table[0],
688 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
689 val -= temp;
690
691 /* Find the nearest table entry to what the user wrote */
692 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
693
694 data->range[sattr->index] &= ~0xF0;
695 data->range[sattr->index] |= val << 4;
696
697 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
698 data->range[sattr->index]);
699
700 mutex_unlock(&data->lock);
701 return count;
702}
703
704static ssize_t tach_show(struct device *dev, struct device_attribute *attr,
705 char *buf)
706{
707 struct adt7475_data *data = adt7475_update_device(dev);
708 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
709 int out;
710
711 if (IS_ERR(data))
712 return PTR_ERR(data);
713
714 if (sattr->nr == ALARM)
715 out = (data->alarms >> (sattr->index + 10)) & 1;
716 else
717 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
718
719 return sprintf(buf, "%d\n", out);
720}
721
722static ssize_t tach_store(struct device *dev, struct device_attribute *attr,
723 const char *buf, size_t count)
724{
725
726 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
727 struct adt7475_data *data = dev_get_drvdata(dev);
728 struct i2c_client *client = data->client;
729 unsigned long val;
730
731 if (kstrtoul(buf, 10, &val))
732 return -EINVAL;
733
734 mutex_lock(&data->lock);
735
736 data->tach[MIN][sattr->index] = rpm2tach(val);
737
738 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
739 data->tach[MIN][sattr->index]);
740
741 mutex_unlock(&data->lock);
742 return count;
743}
744
745static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
746 char *buf)
747{
748 struct adt7475_data *data = adt7475_update_device(dev);
749 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
750
751 if (IS_ERR(data))
752 return PTR_ERR(data);
753
754 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
755}
756
757static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr,
758 char *buf)
759{
760 struct adt7475_data *data = adt7475_update_device(dev);
761 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
762
763 if (IS_ERR(data))
764 return PTR_ERR(data);
765
766 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
767}
768
769static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr,
770 char *buf)
771{
772 struct adt7475_data *data = adt7475_update_device(dev);
773 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
774
775 if (IS_ERR(data))
776 return PTR_ERR(data);
777
778 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
779}
780
781static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
783{
784
785 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
786 struct adt7475_data *data = dev_get_drvdata(dev);
787 struct i2c_client *client = data->client;
788 unsigned char reg = 0;
789 long val;
790
791 if (kstrtol(buf, 10, &val))
792 return -EINVAL;
793
794 mutex_lock(&data->lock);
795
796 switch (sattr->nr) {
797 case INPUT:
798 /* Get a fresh value for CONTROL */
799 data->pwm[CONTROL][sattr->index] =
800 adt7475_read(PWM_CONFIG_REG(sattr->index));
801
802 /*
803 * If we are not in manual mode, then we shouldn't allow
804 * the user to set the pwm speed
805 */
806 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
807 mutex_unlock(&data->lock);
808 return count;
809 }
810
811 reg = PWM_REG(sattr->index);
812 break;
813
814 case MIN:
815 reg = PWM_MIN_REG(sattr->index);
816 break;
817
818 case MAX:
819 reg = PWM_MAX_REG(sattr->index);
820 break;
821 }
822
823 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
824 i2c_smbus_write_byte_data(client, reg,
825 data->pwm[sattr->nr][sattr->index]);
826 mutex_unlock(&data->lock);
827
828 return count;
829}
830
831static ssize_t stall_disable_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
833{
834 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
835 struct adt7475_data *data = dev_get_drvdata(dev);
836
837 u8 mask = BIT(5 + sattr->index);
838
839 return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
840}
841
842static ssize_t stall_disable_store(struct device *dev,
843 struct device_attribute *attr,
844 const char *buf, size_t count)
845{
846 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
847 struct adt7475_data *data = dev_get_drvdata(dev);
848 struct i2c_client *client = data->client;
849 long val;
850 u8 mask = BIT(5 + sattr->index);
851
852 if (kstrtol(buf, 10, &val))
853 return -EINVAL;
854
855 mutex_lock(&data->lock);
856
857 data->enh_acoustics[0] &= ~mask;
858 if (val)
859 data->enh_acoustics[0] |= mask;
860
861 i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
862 data->enh_acoustics[0]);
863
864 mutex_unlock(&data->lock);
865
866 return count;
867}
868
869/* Called by set_pwmctrl and set_pwmchan */
870
871static int hw_set_pwm(struct i2c_client *client, int index,
872 unsigned int pwmctl, unsigned int pwmchan)
873{
874 struct adt7475_data *data = i2c_get_clientdata(client);
875 long val = 0;
876
877 switch (pwmctl) {
878 case 0:
879 val = 0x03; /* Run at full speed */
880 break;
881 case 1:
882 val = 0x07; /* Manual mode */
883 break;
884 case 2:
885 switch (pwmchan) {
886 case 1:
887 /* Remote1 controls PWM */
888 val = 0x00;
889 break;
890 case 2:
891 /* local controls PWM */
892 val = 0x01;
893 break;
894 case 4:
895 /* remote2 controls PWM */
896 val = 0x02;
897 break;
898 case 6:
899 /* local/remote2 control PWM */
900 val = 0x05;
901 break;
902 case 7:
903 /* All three control PWM */
904 val = 0x06;
905 break;
906 default:
907 return -EINVAL;
908 }
909 break;
910 default:
911 return -EINVAL;
912 }
913
914 data->pwmctl[index] = pwmctl;
915 data->pwmchan[index] = pwmchan;
916
917 data->pwm[CONTROL][index] &= ~0xE0;
918 data->pwm[CONTROL][index] |= (val & 7) << 5;
919
920 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
921 data->pwm[CONTROL][index]);
922
923 return 0;
924}
925
926static ssize_t pwmchan_store(struct device *dev,
927 struct device_attribute *attr, const char *buf,
928 size_t count)
929{
930 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
931 struct adt7475_data *data = dev_get_drvdata(dev);
932 struct i2c_client *client = data->client;
933 int r;
934 long val;
935
936 if (kstrtol(buf, 10, &val))
937 return -EINVAL;
938
939 mutex_lock(&data->lock);
940 /* Read Modify Write PWM values */
941 adt7475_read_pwm(client, sattr->index);
942 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
943 if (r)
944 count = r;
945 mutex_unlock(&data->lock);
946
947 return count;
948}
949
950static ssize_t pwmctrl_store(struct device *dev,
951 struct device_attribute *attr, const char *buf,
952 size_t count)
953{
954 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
955 struct adt7475_data *data = dev_get_drvdata(dev);
956 struct i2c_client *client = data->client;
957 int r;
958 long val;
959
960 if (kstrtol(buf, 10, &val))
961 return -EINVAL;
962
963 mutex_lock(&data->lock);
964 /* Read Modify Write PWM values */
965 adt7475_read_pwm(client, sattr->index);
966 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
967 if (r)
968 count = r;
969 mutex_unlock(&data->lock);
970
971 return count;
972}
973
974/* List of frequencies for the PWM */
975static const int pwmfreq_table[] = {
976 11, 14, 22, 29, 35, 44, 58, 88, 22500
977};
978
979static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr,
980 char *buf)
981{
982 struct adt7475_data *data = adt7475_update_device(dev);
983 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
984 int idx;
985
986 if (IS_ERR(data))
987 return PTR_ERR(data);
988 idx = clamp_val(data->range[sattr->index] & 0xf, 0,
989 ARRAY_SIZE(pwmfreq_table) - 1);
990
991 return sprintf(buf, "%d\n", pwmfreq_table[idx]);
992}
993
994static ssize_t pwmfreq_store(struct device *dev,
995 struct device_attribute *attr, const char *buf,
996 size_t count)
997{
998 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
999 struct adt7475_data *data = dev_get_drvdata(dev);
1000 struct i2c_client *client = data->client;
1001 int out;
1002 long val;
1003
1004 if (kstrtol(buf, 10, &val))
1005 return -EINVAL;
1006
1007 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
1008
1009 mutex_lock(&data->lock);
1010
1011 data->range[sattr->index] =
1012 adt7475_read(TEMP_TRANGE_REG(sattr->index));
1013 data->range[sattr->index] &= ~0xf;
1014 data->range[sattr->index] |= out;
1015
1016 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
1017 data->range[sattr->index]);
1018
1019 mutex_unlock(&data->lock);
1020 return count;
1021}
1022
1023static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
1024 struct device_attribute *devattr,
1025 char *buf)
1026{
1027 struct adt7475_data *data = adt7475_update_device(dev);
1028
1029 if (IS_ERR(data))
1030 return PTR_ERR(data);
1031
1032 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1033}
1034
1035static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1036 struct device_attribute *devattr,
1037 const char *buf, size_t count)
1038{
1039 struct adt7475_data *data = dev_get_drvdata(dev);
1040 struct i2c_client *client = data->client;
1041 long val;
1042
1043 if (kstrtol(buf, 10, &val))
1044 return -EINVAL;
1045 if (val != 0 && val != 1)
1046 return -EINVAL;
1047
1048 mutex_lock(&data->lock);
1049 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1050 if (val)
1051 data->config4 |= CONFIG4_MAXDUTY;
1052 else
1053 data->config4 &= ~CONFIG4_MAXDUTY;
1054 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1055 mutex_unlock(&data->lock);
1056
1057 return count;
1058}
1059
1060static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
1061 char *buf)
1062{
1063 struct adt7475_data *data = dev_get_drvdata(dev);
1064 return sprintf(buf, "%d\n", (int)data->vrm);
1065}
1066
1067static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
1068 const char *buf, size_t count)
1069{
1070 struct adt7475_data *data = dev_get_drvdata(dev);
1071 long val;
1072
1073 if (kstrtol(buf, 10, &val))
1074 return -EINVAL;
1075 if (val < 0 || val > 255)
1076 return -EINVAL;
1077 data->vrm = val;
1078
1079 return count;
1080}
1081
1082static ssize_t cpu0_vid_show(struct device *dev,
1083 struct device_attribute *devattr, char *buf)
1084{
1085 struct adt7475_data *data = adt7475_update_device(dev);
1086
1087 if (IS_ERR(data))
1088 return PTR_ERR(data);
1089
1090 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1091}
1092
1093static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0);
1094static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0);
1095static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0);
1096static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0);
1097static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1);
1098static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1);
1099static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1);
1100static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1);
1101static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2);
1102static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2);
1103static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2);
1104static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2);
1105static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3);
1106static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3);
1107static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3);
1108static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3);
1109static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4);
1110static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4);
1111static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4);
1112static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8);
1113static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5);
1114static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5);
1115static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5);
1116static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31);
1117static SENSOR_DEVICE_ATTR_2_RO(in6_input, voltage, INPUT, 6);
1118static SENSOR_DEVICE_ATTR_2_RW(in6_max, voltage, MAX, 6);
1119static SENSOR_DEVICE_ATTR_2_RW(in6_min, voltage, MIN, 6);
1120static SENSOR_DEVICE_ATTR_2_RO(in6_alarm, voltage, ALARM, 30);
1121static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0);
1122static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0);
1123static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0);
1124static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0);
1125static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0);
1126static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0);
1127static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0);
1128static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0);
1129static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0);
1130static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0);
1131static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0);
1132static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1);
1133static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1);
1134static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1);
1135static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1);
1136static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1);
1137static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1);
1138static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1);
1139static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1);
1140static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1);
1141static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1);
1142static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2);
1143static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2);
1144static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2);
1145static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2);
1146static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2);
1147static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2);
1148static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2);
1149static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2);
1150static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2);
1151static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2);
1152static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2);
1153static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0);
1154static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0);
1155static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0);
1156static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1);
1157static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1);
1158static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1);
1159static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2);
1160static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2);
1161static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2);
1162static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3);
1163static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3);
1164static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3);
1165static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0);
1166static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0);
1167static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0);
1168static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0);
1169static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0);
1170static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0);
1171static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0);
1172static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1);
1173static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1);
1174static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1);
1175static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1);
1176static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1);
1177static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1);
1178static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1);
1179static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2);
1180static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2);
1181static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2);
1182static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2);
1183static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2);
1184static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2);
1185static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2);
1186
1187/* Non-standard name, might need revisiting */
1188static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1189
1190static DEVICE_ATTR_RW(vrm);
1191static DEVICE_ATTR_RO(cpu0_vid);
1192
1193static struct attribute *adt7475_attrs[] = {
1194 &sensor_dev_attr_in1_input.dev_attr.attr,
1195 &sensor_dev_attr_in1_max.dev_attr.attr,
1196 &sensor_dev_attr_in1_min.dev_attr.attr,
1197 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1198 &sensor_dev_attr_in2_input.dev_attr.attr,
1199 &sensor_dev_attr_in2_max.dev_attr.attr,
1200 &sensor_dev_attr_in2_min.dev_attr.attr,
1201 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1202 &sensor_dev_attr_temp1_input.dev_attr.attr,
1203 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1204 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1205 &sensor_dev_attr_temp1_max.dev_attr.attr,
1206 &sensor_dev_attr_temp1_min.dev_attr.attr,
1207 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1208 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1209 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1210 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1211 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1212 &sensor_dev_attr_temp1_smoothing.dev_attr.attr,
1213 &sensor_dev_attr_temp2_input.dev_attr.attr,
1214 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1215 &sensor_dev_attr_temp2_max.dev_attr.attr,
1216 &sensor_dev_attr_temp2_min.dev_attr.attr,
1217 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1218 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1219 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1220 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1221 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1222 &sensor_dev_attr_temp2_smoothing.dev_attr.attr,
1223 &sensor_dev_attr_temp3_input.dev_attr.attr,
1224 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1225 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1226 &sensor_dev_attr_temp3_max.dev_attr.attr,
1227 &sensor_dev_attr_temp3_min.dev_attr.attr,
1228 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1229 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1230 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1231 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1232 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1233 &sensor_dev_attr_temp3_smoothing.dev_attr.attr,
1234 &sensor_dev_attr_fan1_input.dev_attr.attr,
1235 &sensor_dev_attr_fan1_min.dev_attr.attr,
1236 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1237 &sensor_dev_attr_fan2_input.dev_attr.attr,
1238 &sensor_dev_attr_fan2_min.dev_attr.attr,
1239 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1240 &sensor_dev_attr_fan3_input.dev_attr.attr,
1241 &sensor_dev_attr_fan3_min.dev_attr.attr,
1242 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1243 &sensor_dev_attr_pwm1.dev_attr.attr,
1244 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1245 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1246 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1247 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1248 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1249 &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
1250 &sensor_dev_attr_pwm3.dev_attr.attr,
1251 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1252 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1253 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1254 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1255 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1256 &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1257 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
1258 NULL,
1259};
1260
1261static struct attribute *fan4_attrs[] = {
1262 &sensor_dev_attr_fan4_input.dev_attr.attr,
1263 &sensor_dev_attr_fan4_min.dev_attr.attr,
1264 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1265 NULL
1266};
1267
1268static struct attribute *pwm2_attrs[] = {
1269 &sensor_dev_attr_pwm2.dev_attr.attr,
1270 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1271 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1272 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1273 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1274 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1275 &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1276 NULL
1277};
1278
1279static struct attribute *in0_attrs[] = {
1280 &sensor_dev_attr_in0_input.dev_attr.attr,
1281 &sensor_dev_attr_in0_max.dev_attr.attr,
1282 &sensor_dev_attr_in0_min.dev_attr.attr,
1283 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1284 NULL
1285};
1286
1287static struct attribute *in3_attrs[] = {
1288 &sensor_dev_attr_in3_input.dev_attr.attr,
1289 &sensor_dev_attr_in3_max.dev_attr.attr,
1290 &sensor_dev_attr_in3_min.dev_attr.attr,
1291 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1292 NULL
1293};
1294
1295static struct attribute *in4_attrs[] = {
1296 &sensor_dev_attr_in4_input.dev_attr.attr,
1297 &sensor_dev_attr_in4_max.dev_attr.attr,
1298 &sensor_dev_attr_in4_min.dev_attr.attr,
1299 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1300 NULL
1301};
1302
1303static struct attribute *in5_attrs[] = {
1304 &sensor_dev_attr_in5_input.dev_attr.attr,
1305 &sensor_dev_attr_in5_max.dev_attr.attr,
1306 &sensor_dev_attr_in5_min.dev_attr.attr,
1307 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1308 NULL
1309};
1310
1311static struct attribute *in6_attrs[] = {
1312 &sensor_dev_attr_in6_input.dev_attr.attr,
1313 &sensor_dev_attr_in6_max.dev_attr.attr,
1314 &sensor_dev_attr_in6_min.dev_attr.attr,
1315 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1316 NULL
1317};
1318
1319static struct attribute *vid_attrs[] = {
1320 &dev_attr_cpu0_vid.attr,
1321 &dev_attr_vrm.attr,
1322 NULL
1323};
1324
1325static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1326static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1327static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1328static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1329static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1330static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1331static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1332static const struct attribute_group in6_attr_group = { .attrs = in6_attrs };
1333static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
1334
1335static int adt7475_detect(struct i2c_client *client,
1336 struct i2c_board_info *info)
1337{
1338 struct i2c_adapter *adapter = client->adapter;
1339 int vendid, devid, devid2;
1340 const char *name;
1341
1342 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1343 return -ENODEV;
1344
1345 vendid = adt7475_read(REG_VENDID);
1346 devid2 = adt7475_read(REG_DEVID2);
1347 if (vendid != 0x41 || /* Analog Devices */
1348 (devid2 & 0xf8) != 0x68)
1349 return -ENODEV;
1350
1351 devid = adt7475_read(REG_DEVID);
1352 if (devid == 0x73)
1353 name = "adt7473";
1354 else if (devid == 0x75 && client->addr == 0x2e)
1355 name = "adt7475";
1356 else if (devid == 0x76)
1357 name = "adt7476";
1358 else if ((devid2 & 0xfc) == 0x6c)
1359 name = "adt7490";
1360 else {
1361 dev_dbg(&adapter->dev,
1362 "Couldn't detect an ADT7473/75/76/90 part at "
1363 "0x%02x\n", (unsigned int)client->addr);
1364 return -ENODEV;
1365 }
1366
1367 strscpy(info->type, name, I2C_NAME_SIZE);
1368
1369 return 0;
1370}
1371
1372static int adt7475_update_limits(struct i2c_client *client)
1373{
1374 struct adt7475_data *data = i2c_get_clientdata(client);
1375 int i;
1376 int ret;
1377
1378 ret = adt7475_read(REG_CONFIG4);
1379 if (ret < 0)
1380 return ret;
1381 data->config4 = ret;
1382
1383 ret = adt7475_read(REG_CONFIG5);
1384 if (ret < 0)
1385 return ret;
1386 data->config5 = ret;
1387
1388 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1389 if (!(data->has_voltage & (1 << i)))
1390 continue;
1391 /* Adjust values so they match the input precision */
1392 ret = adt7475_read(VOLTAGE_MIN_REG(i));
1393 if (ret < 0)
1394 return ret;
1395 data->voltage[MIN][i] = ret << 2;
1396
1397 ret = adt7475_read(VOLTAGE_MAX_REG(i));
1398 if (ret < 0)
1399 return ret;
1400 data->voltage[MAX][i] = ret << 2;
1401 }
1402
1403 if (data->has_voltage & (1 << 5)) {
1404 ret = adt7475_read(REG_VTT_MIN);
1405 if (ret < 0)
1406 return ret;
1407 data->voltage[MIN][5] = ret << 2;
1408
1409 ret = adt7475_read(REG_VTT_MAX);
1410 if (ret < 0)
1411 return ret;
1412 data->voltage[MAX][5] = ret << 2;
1413 }
1414
1415 if (data->has_voltage & (1 << 6)) {
1416 ret = adt7475_read(REG_IMON_MIN);
1417 if (ret < 0)
1418 return ret;
1419 data->voltage[MIN][6] = ret << 2;
1420
1421 ret = adt7475_read(REG_IMON_MAX);
1422 if (ret < 0)
1423 return ret;
1424 data->voltage[MAX][6] = ret << 2;
1425 }
1426
1427 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1428 /* Adjust values so they match the input precision */
1429 ret = adt7475_read(TEMP_MIN_REG(i));
1430 if (ret < 0)
1431 return ret;
1432 data->temp[MIN][i] = ret << 2;
1433
1434 ret = adt7475_read(TEMP_MAX_REG(i));
1435 if (ret < 0)
1436 return ret;
1437 data->temp[MAX][i] = ret << 2;
1438
1439 ret = adt7475_read(TEMP_TMIN_REG(i));
1440 if (ret < 0)
1441 return ret;
1442 data->temp[AUTOMIN][i] = ret << 2;
1443
1444 ret = adt7475_read(TEMP_THERM_REG(i));
1445 if (ret < 0)
1446 return ret;
1447 data->temp[THERM][i] = ret << 2;
1448
1449 ret = adt7475_read(TEMP_OFFSET_REG(i));
1450 if (ret < 0)
1451 return ret;
1452 data->temp[OFFSET][i] = ret;
1453 }
1454 adt7475_read_hystersis(client);
1455
1456 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1457 if (i == 3 && !data->has_fan4)
1458 continue;
1459 ret = adt7475_read_word(client, TACH_MIN_REG(i));
1460 if (ret < 0)
1461 return ret;
1462 data->tach[MIN][i] = ret;
1463 }
1464
1465 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1466 if (i == 1 && !data->has_pwm2)
1467 continue;
1468 ret = adt7475_read(PWM_MAX_REG(i));
1469 if (ret < 0)
1470 return ret;
1471 data->pwm[MAX][i] = ret;
1472
1473 ret = adt7475_read(PWM_MIN_REG(i));
1474 if (ret < 0)
1475 return ret;
1476 data->pwm[MIN][i] = ret;
1477 /* Set the channel and control information */
1478 adt7475_read_pwm(client, i);
1479 }
1480
1481 ret = adt7475_read(TEMP_TRANGE_REG(0));
1482 if (ret < 0)
1483 return ret;
1484 data->range[0] = ret;
1485
1486 ret = adt7475_read(TEMP_TRANGE_REG(1));
1487 if (ret < 0)
1488 return ret;
1489 data->range[1] = ret;
1490
1491 ret = adt7475_read(TEMP_TRANGE_REG(2));
1492 if (ret < 0)
1493 return ret;
1494 data->range[2] = ret;
1495
1496 return 0;
1497}
1498
1499static int load_config3(const struct i2c_client *client, const char *propname)
1500{
1501 const char *function;
1502 u8 config3;
1503 int ret;
1504
1505 ret = device_property_read_string(&client->dev, propname, &function);
1506 if (!ret) {
1507 ret = adt7475_read(REG_CONFIG3);
1508 if (ret < 0)
1509 return ret;
1510
1511 config3 = ret & ~CONFIG3_SMBALERT;
1512 if (!strcmp("pwm2", function))
1513 ;
1514 else if (!strcmp("smbalert#", function))
1515 config3 |= CONFIG3_SMBALERT;
1516 else
1517 return -EINVAL;
1518
1519 return i2c_smbus_write_byte_data(client, REG_CONFIG3, config3);
1520 }
1521
1522 return 0;
1523}
1524
1525static int load_config4(const struct i2c_client *client, const char *propname)
1526{
1527 const char *function;
1528 u8 config4;
1529 int ret;
1530
1531 ret = device_property_read_string(&client->dev, propname, &function);
1532 if (!ret) {
1533 ret = adt7475_read(REG_CONFIG4);
1534 if (ret < 0)
1535 return ret;
1536
1537 config4 = ret & ~CONFIG4_PINFUNC;
1538
1539 if (!strcmp("tach4", function))
1540 ;
1541 else if (!strcmp("therm#", function))
1542 config4 |= CONFIG4_THERM;
1543 else if (!strcmp("smbalert#", function))
1544 config4 |= CONFIG4_SMBALERT;
1545 else if (!strcmp("gpio", function))
1546 config4 |= CONFIG4_PINFUNC;
1547 else
1548 return -EINVAL;
1549
1550 return i2c_smbus_write_byte_data(client, REG_CONFIG4, config4);
1551 }
1552
1553 return 0;
1554}
1555
1556static int load_config(const struct i2c_client *client, enum chips chip)
1557{
1558 int err;
1559 const char *prop1, *prop2;
1560
1561 switch (chip) {
1562 case adt7473:
1563 case adt7475:
1564 prop1 = "adi,pin5-function";
1565 prop2 = "adi,pin9-function";
1566 break;
1567 case adt7476:
1568 case adt7490:
1569 prop1 = "adi,pin10-function";
1570 prop2 = "adi,pin14-function";
1571 break;
1572 }
1573
1574 err = load_config3(client, prop1);
1575 if (err) {
1576 dev_err(&client->dev, "failed to configure %s\n", prop1);
1577 return err;
1578 }
1579
1580 err = load_config4(client, prop2);
1581 if (err) {
1582 dev_err(&client->dev, "failed to configure %s\n", prop2);
1583 return err;
1584 }
1585
1586 return 0;
1587}
1588
1589static int set_property_bit(const struct i2c_client *client, char *property,
1590 u8 *config, u8 bit_index)
1591{
1592 u32 prop_value = 0;
1593 int ret = device_property_read_u32(&client->dev, property,
1594 &prop_value);
1595
1596 if (!ret) {
1597 if (prop_value)
1598 *config |= (1 << bit_index);
1599 else
1600 *config &= ~(1 << bit_index);
1601 }
1602
1603 return ret;
1604}
1605
1606static int load_attenuators(const struct i2c_client *client, enum chips chip,
1607 struct adt7475_data *data)
1608{
1609 switch (chip) {
1610 case adt7476:
1611 case adt7490:
1612 set_property_bit(client, "adi,bypass-attenuator-in0",
1613 &data->config4, 4);
1614 set_property_bit(client, "adi,bypass-attenuator-in1",
1615 &data->config4, 5);
1616 set_property_bit(client, "adi,bypass-attenuator-in3",
1617 &data->config4, 6);
1618 set_property_bit(client, "adi,bypass-attenuator-in4",
1619 &data->config4, 7);
1620
1621 return i2c_smbus_write_byte_data(client, REG_CONFIG4,
1622 data->config4);
1623 case adt7473:
1624 case adt7475:
1625 set_property_bit(client, "adi,bypass-attenuator-in1",
1626 &data->config2, 5);
1627
1628 return i2c_smbus_write_byte_data(client, REG_CONFIG2,
1629 data->config2);
1630 }
1631
1632 return 0;
1633}
1634
1635static int adt7475_set_pwm_polarity(struct i2c_client *client)
1636{
1637 u32 states[ADT7475_PWM_COUNT];
1638 int ret, i;
1639 u8 val;
1640
1641 ret = device_property_read_u32_array(&client->dev,
1642 "adi,pwm-active-state", states,
1643 ARRAY_SIZE(states));
1644 if (ret)
1645 return ret;
1646
1647 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1648 ret = adt7475_read(PWM_CONFIG_REG(i));
1649 if (ret < 0)
1650 return ret;
1651 val = ret;
1652 if (states[i])
1653 val &= ~BIT(4);
1654 else
1655 val |= BIT(4);
1656
1657 ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(i), val);
1658 if (ret)
1659 return ret;
1660 }
1661
1662 return 0;
1663}
1664
1665static int adt7475_probe(struct i2c_client *client)
1666{
1667 enum chips chip;
1668 static const char * const names[] = {
1669 [adt7473] = "ADT7473",
1670 [adt7475] = "ADT7475",
1671 [adt7476] = "ADT7476",
1672 [adt7490] = "ADT7490",
1673 };
1674
1675 struct adt7475_data *data;
1676 struct device *hwmon_dev;
1677 int i, ret = 0, revision, group_num = 0;
1678 u8 config3;
1679 const struct i2c_device_id *id = i2c_match_id(adt7475_id, client);
1680
1681 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1682 if (data == NULL)
1683 return -ENOMEM;
1684
1685 mutex_init(&data->lock);
1686 data->client = client;
1687 i2c_set_clientdata(client, data);
1688
1689 if (client->dev.of_node)
1690 chip = (uintptr_t)of_device_get_match_data(&client->dev);
1691 else
1692 chip = id->driver_data;
1693
1694 /* Initialize device-specific values */
1695 switch (chip) {
1696 case adt7476:
1697 data->has_voltage = 0x0e; /* in1 to in3 */
1698 revision = adt7475_read(REG_DEVID2) & 0x07;
1699 break;
1700 case adt7490:
1701 data->has_voltage = 0x7e; /* in1 to in6 */
1702 revision = adt7475_read(REG_DEVID2) & 0x03;
1703 if (revision == 0x03)
1704 revision += adt7475_read(REG_DEVREV2);
1705 break;
1706 default:
1707 data->has_voltage = 0x06; /* in1, in2 */
1708 revision = adt7475_read(REG_DEVID2) & 0x07;
1709 }
1710
1711 ret = load_config(client, chip);
1712 if (ret)
1713 return ret;
1714
1715 config3 = adt7475_read(REG_CONFIG3);
1716 /* Pin PWM2 may alternatively be used for ALERT output */
1717 if (!(config3 & CONFIG3_SMBALERT))
1718 data->has_pwm2 = 1;
1719 /* Meaning of this bit is inverted for the ADT7473-1 */
1720 if (id->driver_data == adt7473 && revision >= 1)
1721 data->has_pwm2 = !data->has_pwm2;
1722
1723 data->config4 = adt7475_read(REG_CONFIG4);
1724 /* Pin TACH4 may alternatively be used for THERM */
1725 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1726 data->has_fan4 = 1;
1727
1728 /*
1729 * THERM configuration is more complex on the ADT7476 and ADT7490,
1730 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1731 * this function
1732 */
1733 if (id->driver_data == adt7490) {
1734 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1735 !(config3 & CONFIG3_THERM))
1736 data->has_fan4 = 1;
1737 }
1738 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1739 if (!(config3 & CONFIG3_THERM) ||
1740 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1741 data->has_voltage |= (1 << 0); /* in0 */
1742 }
1743
1744 /*
1745 * On the ADT7476, the +12V input pin may instead be used as VID5,
1746 * and VID pins may alternatively be used as GPIO
1747 */
1748 if (id->driver_data == adt7476) {
1749 u8 vid = adt7475_read(REG_VID);
1750 if (!(vid & VID_VIDSEL))
1751 data->has_voltage |= (1 << 4); /* in4 */
1752
1753 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1754 }
1755
1756 /* Voltage attenuators can be bypassed, globally or individually */
1757 data->config2 = adt7475_read(REG_CONFIG2);
1758 ret = load_attenuators(client, chip, data);
1759 if (ret)
1760 dev_warn(&client->dev, "Error configuring attenuator bypass\n");
1761
1762 if (data->config2 & CONFIG2_ATTN) {
1763 data->bypass_attn = (0x3 << 3) | 0x3;
1764 } else {
1765 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1766 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1767 }
1768 data->bypass_attn &= data->has_voltage;
1769
1770 /*
1771 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1772 * pwm's which are disabled to manual mode with 0% duty cycle
1773 */
1774 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1775 adt7475_read_pwm(client, i);
1776
1777 ret = adt7475_set_pwm_polarity(client);
1778 if (ret && ret != -EINVAL)
1779 dev_warn(&client->dev, "Error configuring pwm polarity\n");
1780
1781 /* Start monitoring */
1782 switch (chip) {
1783 case adt7475:
1784 case adt7476:
1785 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1786 adt7475_read(REG_CONFIG1) | 0x01);
1787 break;
1788 default:
1789 break;
1790 }
1791
1792 data->groups[group_num++] = &adt7475_attr_group;
1793
1794 /* Features that can be disabled individually */
1795 if (data->has_fan4) {
1796 data->groups[group_num++] = &fan4_attr_group;
1797 }
1798 if (data->has_pwm2) {
1799 data->groups[group_num++] = &pwm2_attr_group;
1800 }
1801 if (data->has_voltage & (1 << 0)) {
1802 data->groups[group_num++] = &in0_attr_group;
1803 }
1804 if (data->has_voltage & (1 << 3)) {
1805 data->groups[group_num++] = &in3_attr_group;
1806 }
1807 if (data->has_voltage & (1 << 4)) {
1808 data->groups[group_num++] = &in4_attr_group;
1809 }
1810 if (data->has_voltage & (1 << 5)) {
1811 data->groups[group_num++] = &in5_attr_group;
1812 }
1813 if (data->has_voltage & (1 << 6)) {
1814 data->groups[group_num++] = &in6_attr_group;
1815 }
1816 if (data->has_vid) {
1817 data->vrm = vid_which_vrm();
1818 data->groups[group_num] = &vid_attr_group;
1819 }
1820
1821 /* register device with all the acquired attributes */
1822 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
1823 client->name, data,
1824 data->groups);
1825
1826 if (IS_ERR(hwmon_dev)) {
1827 ret = PTR_ERR(hwmon_dev);
1828 return ret;
1829 }
1830
1831 dev_info(&client->dev, "%s device, revision %d\n",
1832 names[id->driver_data], revision);
1833 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
1834 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1835 (data->has_voltage & (1 << 0)) ? " in0" : "",
1836 (data->has_voltage & (1 << 4)) ? " in4" : "",
1837 data->has_fan4 ? " fan4" : "",
1838 data->has_pwm2 ? " pwm2" : "",
1839 data->has_vid ? " vid" : "");
1840 if (data->bypass_attn)
1841 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1842 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1843 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1844 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1845 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1846
1847 /* Limits and settings, should never change update more than once */
1848 ret = adt7475_update_limits(client);
1849 if (ret)
1850 return ret;
1851
1852 return 0;
1853}
1854
1855static struct i2c_driver adt7475_driver = {
1856 .class = I2C_CLASS_HWMON,
1857 .driver = {
1858 .name = "adt7475",
1859 .of_match_table = of_match_ptr(adt7475_of_match),
1860 },
1861 .probe = adt7475_probe,
1862 .id_table = adt7475_id,
1863 .detect = adt7475_detect,
1864 .address_list = normal_i2c,
1865};
1866
1867static void adt7475_read_hystersis(struct i2c_client *client)
1868{
1869 struct adt7475_data *data = i2c_get_clientdata(client);
1870
1871 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1872 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1873 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1874}
1875
1876static void adt7475_read_pwm(struct i2c_client *client, int index)
1877{
1878 struct adt7475_data *data = i2c_get_clientdata(client);
1879 unsigned int v;
1880
1881 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1882
1883 /*
1884 * Figure out the internal value for pwmctrl and pwmchan
1885 * based on the current settings
1886 */
1887 v = (data->pwm[CONTROL][index] >> 5) & 7;
1888
1889 if (v == 3)
1890 data->pwmctl[index] = 0;
1891 else if (v == 7)
1892 data->pwmctl[index] = 1;
1893 else if (v == 4) {
1894 /*
1895 * The fan is disabled - we don't want to
1896 * support that, so change to manual mode and
1897 * set the duty cycle to 0 instead
1898 */
1899 data->pwm[INPUT][index] = 0;
1900 data->pwm[CONTROL][index] &= ~0xE0;
1901 data->pwm[CONTROL][index] |= (7 << 5);
1902
1903 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1904 data->pwm[INPUT][index]);
1905
1906 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1907 data->pwm[CONTROL][index]);
1908
1909 data->pwmctl[index] = 1;
1910 } else {
1911 data->pwmctl[index] = 2;
1912
1913 switch (v) {
1914 case 0:
1915 data->pwmchan[index] = 1;
1916 break;
1917 case 1:
1918 data->pwmchan[index] = 2;
1919 break;
1920 case 2:
1921 data->pwmchan[index] = 4;
1922 break;
1923 case 5:
1924 data->pwmchan[index] = 6;
1925 break;
1926 case 6:
1927 data->pwmchan[index] = 7;
1928 break;
1929 }
1930 }
1931}
1932
1933static int adt7475_update_measure(struct device *dev)
1934{
1935 struct adt7475_data *data = dev_get_drvdata(dev);
1936 struct i2c_client *client = data->client;
1937 u16 ext;
1938 int i;
1939 int ret;
1940
1941 ret = adt7475_read(REG_STATUS2);
1942 if (ret < 0)
1943 return ret;
1944 data->alarms = ret << 8;
1945
1946 ret = adt7475_read(REG_STATUS1);
1947 if (ret < 0)
1948 return ret;
1949 data->alarms |= ret;
1950
1951 ret = adt7475_read(REG_EXTEND2);
1952 if (ret < 0)
1953 return ret;
1954
1955 ext = (ret << 8);
1956
1957 ret = adt7475_read(REG_EXTEND1);
1958 if (ret < 0)
1959 return ret;
1960
1961 ext |= ret;
1962
1963 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1964 if (!(data->has_voltage & (1 << i)))
1965 continue;
1966 ret = adt7475_read(VOLTAGE_REG(i));
1967 if (ret < 0)
1968 return ret;
1969 data->voltage[INPUT][i] =
1970 (ret << 2) |
1971 ((ext >> (i * 2)) & 3);
1972 }
1973
1974 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1975 ret = adt7475_read(TEMP_REG(i));
1976 if (ret < 0)
1977 return ret;
1978 data->temp[INPUT][i] =
1979 (ret << 2) |
1980 ((ext >> ((i + 5) * 2)) & 3);
1981 }
1982
1983 if (data->has_voltage & (1 << 5)) {
1984 ret = adt7475_read(REG_STATUS4);
1985 if (ret < 0)
1986 return ret;
1987 data->alarms |= ret << 24;
1988
1989 ret = adt7475_read(REG_EXTEND3);
1990 if (ret < 0)
1991 return ret;
1992 ext = ret;
1993
1994 ret = adt7475_read(REG_VTT);
1995 if (ret < 0)
1996 return ret;
1997 data->voltage[INPUT][5] = ret << 2 |
1998 ((ext >> 4) & 3);
1999 }
2000
2001 if (data->has_voltage & (1 << 6)) {
2002 ret = adt7475_read(REG_STATUS4);
2003 if (ret < 0)
2004 return ret;
2005 data->alarms |= ret << 24;
2006
2007 ret = adt7475_read(REG_EXTEND3);
2008 if (ret < 0)
2009 return ret;
2010 ext = ret;
2011
2012 ret = adt7475_read(REG_IMON);
2013 if (ret < 0)
2014 return ret;
2015 data->voltage[INPUT][6] = ret << 2 |
2016 ((ext >> 6) & 3);
2017 }
2018
2019 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
2020 if (i == 3 && !data->has_fan4)
2021 continue;
2022 ret = adt7475_read_word(client, TACH_REG(i));
2023 if (ret < 0)
2024 return ret;
2025 data->tach[INPUT][i] = ret;
2026 }
2027
2028 /* Updated by hw when in auto mode */
2029 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
2030 if (i == 1 && !data->has_pwm2)
2031 continue;
2032 ret = adt7475_read(PWM_REG(i));
2033 if (ret < 0)
2034 return ret;
2035 data->pwm[INPUT][i] = ret;
2036 }
2037
2038 if (data->has_vid) {
2039 ret = adt7475_read(REG_VID);
2040 if (ret < 0)
2041 return ret;
2042 data->vid = ret & 0x3f;
2043 }
2044
2045 return 0;
2046}
2047
2048static struct adt7475_data *adt7475_update_device(struct device *dev)
2049{
2050 struct adt7475_data *data = dev_get_drvdata(dev);
2051 int ret;
2052
2053 mutex_lock(&data->lock);
2054
2055 /* Measurement values update every 2 seconds */
2056 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
2057 !data->valid) {
2058 ret = adt7475_update_measure(dev);
2059 if (ret) {
2060 data->valid = false;
2061 mutex_unlock(&data->lock);
2062 return ERR_PTR(ret);
2063 }
2064 data->measure_updated = jiffies;
2065 data->valid = true;
2066 }
2067
2068 mutex_unlock(&data->lock);
2069
2070 return data;
2071}
2072
2073module_i2c_driver(adt7475_driver);
2074
2075MODULE_AUTHOR("Advanced Micro Devices, Inc");
2076MODULE_DESCRIPTION("adt7475 driver");
2077MODULE_LICENSE("GPL");
1/*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
7 *
8 * Derived from the lm83 driver by Jean Delvare
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/i2c.h>
19#include <linux/hwmon.h>
20#include <linux/hwmon-sysfs.h>
21#include <linux/hwmon-vid.h>
22#include <linux/err.h>
23#include <linux/jiffies.h>
24
25/* Indexes for the sysfs hooks */
26
27#define INPUT 0
28#define MIN 1
29#define MAX 2
30#define CONTROL 3
31#define OFFSET 3
32#define AUTOMIN 4
33#define THERM 5
34#define HYSTERSIS 6
35
36/*
37 * These are unique identifiers for the sysfs functions - unlike the
38 * numbers above, these are not also indexes into an array
39 */
40
41#define ALARM 9
42#define FAULT 10
43
44/* 7475 Common Registers */
45
46#define REG_DEVREV2 0x12 /* ADT7490 only */
47
48#define REG_VTT 0x1E /* ADT7490 only */
49#define REG_EXTEND3 0x1F /* ADT7490 only */
50
51#define REG_VOLTAGE_BASE 0x20
52#define REG_TEMP_BASE 0x25
53#define REG_TACH_BASE 0x28
54#define REG_PWM_BASE 0x30
55#define REG_PWM_MAX_BASE 0x38
56
57#define REG_DEVID 0x3D
58#define REG_VENDID 0x3E
59#define REG_DEVID2 0x3F
60
61#define REG_STATUS1 0x41
62#define REG_STATUS2 0x42
63
64#define REG_VID 0x43 /* ADT7476 only */
65
66#define REG_VOLTAGE_MIN_BASE 0x44
67#define REG_VOLTAGE_MAX_BASE 0x45
68
69#define REG_TEMP_MIN_BASE 0x4E
70#define REG_TEMP_MAX_BASE 0x4F
71
72#define REG_TACH_MIN_BASE 0x54
73
74#define REG_PWM_CONFIG_BASE 0x5C
75
76#define REG_TEMP_TRANGE_BASE 0x5F
77
78#define REG_PWM_MIN_BASE 0x64
79
80#define REG_TEMP_TMIN_BASE 0x67
81#define REG_TEMP_THERM_BASE 0x6A
82
83#define REG_REMOTE1_HYSTERSIS 0x6D
84#define REG_REMOTE2_HYSTERSIS 0x6E
85
86#define REG_TEMP_OFFSET_BASE 0x70
87
88#define REG_CONFIG2 0x73
89
90#define REG_EXTEND1 0x76
91#define REG_EXTEND2 0x77
92
93#define REG_CONFIG3 0x78
94#define REG_CONFIG5 0x7C
95#define REG_CONFIG4 0x7D
96
97#define REG_STATUS4 0x81 /* ADT7490 only */
98
99#define REG_VTT_MIN 0x84 /* ADT7490 only */
100#define REG_VTT_MAX 0x86 /* ADT7490 only */
101
102#define VID_VIDSEL 0x80 /* ADT7476 only */
103
104#define CONFIG2_ATTN 0x20
105
106#define CONFIG3_SMBALERT 0x01
107#define CONFIG3_THERM 0x02
108
109#define CONFIG4_PINFUNC 0x03
110#define CONFIG4_MAXDUTY 0x08
111#define CONFIG4_ATTN_IN10 0x30
112#define CONFIG4_ATTN_IN43 0xC0
113
114#define CONFIG5_TWOSCOMP 0x01
115#define CONFIG5_TEMPOFFSET 0x02
116#define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
117
118/* ADT7475 Settings */
119
120#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
121#define ADT7475_TEMP_COUNT 3
122#define ADT7475_TACH_COUNT 4
123#define ADT7475_PWM_COUNT 3
124
125/* Macro to read the registers */
126
127#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
128
129/* Macros to easily index the registers */
130
131#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
132#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
133
134#define PWM_REG(idx) (REG_PWM_BASE + (idx))
135#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
136#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
137#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
138
139#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
140#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
141#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
142
143#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
144#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
145#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
146#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
147#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
148#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
149#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
150
151static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
152
153enum chips { adt7473, adt7475, adt7476, adt7490 };
154
155static const struct i2c_device_id adt7475_id[] = {
156 { "adt7473", adt7473 },
157 { "adt7475", adt7475 },
158 { "adt7476", adt7476 },
159 { "adt7490", adt7490 },
160 { }
161};
162MODULE_DEVICE_TABLE(i2c, adt7475_id);
163
164struct adt7475_data {
165 struct device *hwmon_dev;
166 struct mutex lock;
167
168 unsigned long measure_updated;
169 unsigned long limits_updated;
170 char valid;
171
172 u8 config4;
173 u8 config5;
174 u8 has_voltage;
175 u8 bypass_attn; /* Bypass voltage attenuator */
176 u8 has_pwm2:1;
177 u8 has_fan4:1;
178 u8 has_vid:1;
179 u32 alarms;
180 u16 voltage[3][6];
181 u16 temp[7][3];
182 u16 tach[2][4];
183 u8 pwm[4][3];
184 u8 range[3];
185 u8 pwmctl[3];
186 u8 pwmchan[3];
187
188 u8 vid;
189 u8 vrm;
190};
191
192static struct i2c_driver adt7475_driver;
193static struct adt7475_data *adt7475_update_device(struct device *dev);
194static void adt7475_read_hystersis(struct i2c_client *client);
195static void adt7475_read_pwm(struct i2c_client *client, int index);
196
197/* Given a temp value, convert it to register value */
198
199static inline u16 temp2reg(struct adt7475_data *data, long val)
200{
201 u16 ret;
202
203 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
204 val = clamp_val(val, -64000, 191000);
205 ret = (val + 64500) / 1000;
206 } else {
207 val = clamp_val(val, -128000, 127000);
208 if (val < -500)
209 ret = (256500 + val) / 1000;
210 else
211 ret = (val + 500) / 1000;
212 }
213
214 return ret << 2;
215}
216
217/* Given a register value, convert it to a real temp value */
218
219static inline int reg2temp(struct adt7475_data *data, u16 reg)
220{
221 if (data->config5 & CONFIG5_TWOSCOMP) {
222 if (reg >= 512)
223 return (reg - 1024) * 250;
224 else
225 return reg * 250;
226 } else
227 return (reg - 256) * 250;
228}
229
230static inline int tach2rpm(u16 tach)
231{
232 if (tach == 0 || tach == 0xFFFF)
233 return 0;
234
235 return (90000 * 60) / tach;
236}
237
238static inline u16 rpm2tach(unsigned long rpm)
239{
240 if (rpm == 0)
241 return 0;
242
243 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
244}
245
246/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
247static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
248 { 45, 94 }, /* +2.5V */
249 { 175, 525 }, /* Vccp */
250 { 68, 71 }, /* Vcc */
251 { 93, 47 }, /* +5V */
252 { 120, 20 }, /* +12V */
253 { 45, 45 }, /* Vtt */
254};
255
256static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
257{
258 const int *r = adt7473_in_scaling[channel];
259
260 if (bypass_attn & (1 << channel))
261 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
262 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
263}
264
265static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
266{
267 const int *r = adt7473_in_scaling[channel];
268 long reg;
269
270 if (bypass_attn & (1 << channel))
271 reg = (volt * 1024) / 2250;
272 else
273 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
274 return clamp_val(reg, 0, 1023) & (0xff << 2);
275}
276
277static u16 adt7475_read_word(struct i2c_client *client, int reg)
278{
279 u16 val;
280
281 val = i2c_smbus_read_byte_data(client, reg);
282 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
283
284 return val;
285}
286
287static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
288{
289 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
290 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
291}
292
293/*
294 * Find the nearest value in a table - used for pwm frequency and
295 * auto temp range
296 */
297static int find_nearest(long val, const int *array, int size)
298{
299 int i;
300
301 if (val < array[0])
302 return 0;
303
304 if (val > array[size - 1])
305 return size - 1;
306
307 for (i = 0; i < size - 1; i++) {
308 int a, b;
309
310 if (val > array[i + 1])
311 continue;
312
313 a = val - array[i];
314 b = array[i + 1] - val;
315
316 return (a <= b) ? i : i + 1;
317 }
318
319 return 0;
320}
321
322static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct adt7475_data *data = adt7475_update_device(dev);
326 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
327 unsigned short val;
328
329 switch (sattr->nr) {
330 case ALARM:
331 return sprintf(buf, "%d\n",
332 (data->alarms >> sattr->index) & 1);
333 default:
334 val = data->voltage[sattr->nr][sattr->index];
335 return sprintf(buf, "%d\n",
336 reg2volt(sattr->index, val, data->bypass_attn));
337 }
338}
339
340static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
341 const char *buf, size_t count)
342{
343
344 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
345 struct i2c_client *client = to_i2c_client(dev);
346 struct adt7475_data *data = i2c_get_clientdata(client);
347 unsigned char reg;
348 long val;
349
350 if (kstrtol(buf, 10, &val))
351 return -EINVAL;
352
353 mutex_lock(&data->lock);
354
355 data->voltage[sattr->nr][sattr->index] =
356 volt2reg(sattr->index, val, data->bypass_attn);
357
358 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
359 if (sattr->nr == MIN)
360 reg = VOLTAGE_MIN_REG(sattr->index);
361 else
362 reg = VOLTAGE_MAX_REG(sattr->index);
363 } else {
364 if (sattr->nr == MIN)
365 reg = REG_VTT_MIN;
366 else
367 reg = REG_VTT_MAX;
368 }
369
370 i2c_smbus_write_byte_data(client, reg,
371 data->voltage[sattr->nr][sattr->index] >> 2);
372 mutex_unlock(&data->lock);
373
374 return count;
375}
376
377static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
378 char *buf)
379{
380 struct adt7475_data *data = adt7475_update_device(dev);
381 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
382 int out;
383
384 switch (sattr->nr) {
385 case HYSTERSIS:
386 mutex_lock(&data->lock);
387 out = data->temp[sattr->nr][sattr->index];
388 if (sattr->index != 1)
389 out = (out >> 4) & 0xF;
390 else
391 out = (out & 0xF);
392 /*
393 * Show the value as an absolute number tied to
394 * THERM
395 */
396 out = reg2temp(data, data->temp[THERM][sattr->index]) -
397 out * 1000;
398 mutex_unlock(&data->lock);
399 break;
400
401 case OFFSET:
402 /*
403 * Offset is always 2's complement, regardless of the
404 * setting in CONFIG5
405 */
406 mutex_lock(&data->lock);
407 out = (s8)data->temp[sattr->nr][sattr->index];
408 if (data->config5 & CONFIG5_TEMPOFFSET)
409 out *= 1000;
410 else
411 out *= 500;
412 mutex_unlock(&data->lock);
413 break;
414
415 case ALARM:
416 out = (data->alarms >> (sattr->index + 4)) & 1;
417 break;
418
419 case FAULT:
420 /* Note - only for remote1 and remote2 */
421 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
422 break;
423
424 default:
425 /* All other temp values are in the configured format */
426 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
427 }
428
429 return sprintf(buf, "%d\n", out);
430}
431
432static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
433 const char *buf, size_t count)
434{
435 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
436 struct i2c_client *client = to_i2c_client(dev);
437 struct adt7475_data *data = i2c_get_clientdata(client);
438 unsigned char reg = 0;
439 u8 out;
440 int temp;
441 long val;
442
443 if (kstrtol(buf, 10, &val))
444 return -EINVAL;
445
446 mutex_lock(&data->lock);
447
448 /* We need the config register in all cases for temp <-> reg conv. */
449 data->config5 = adt7475_read(REG_CONFIG5);
450
451 switch (sattr->nr) {
452 case OFFSET:
453 if (data->config5 & CONFIG5_TEMPOFFSET) {
454 val = clamp_val(val, -63000, 127000);
455 out = data->temp[OFFSET][sattr->index] = val / 1000;
456 } else {
457 val = clamp_val(val, -63000, 64000);
458 out = data->temp[OFFSET][sattr->index] = val / 500;
459 }
460 break;
461
462 case HYSTERSIS:
463 /*
464 * The value will be given as an absolute value, turn it
465 * into an offset based on THERM
466 */
467
468 /* Read fresh THERM and HYSTERSIS values from the chip */
469 data->temp[THERM][sattr->index] =
470 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
471 adt7475_read_hystersis(client);
472
473 temp = reg2temp(data, data->temp[THERM][sattr->index]);
474 val = clamp_val(val, temp - 15000, temp);
475 val = (temp - val) / 1000;
476
477 if (sattr->index != 1) {
478 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
479 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
480 } else {
481 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
482 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
483 }
484
485 out = data->temp[HYSTERSIS][sattr->index];
486 break;
487
488 default:
489 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
490
491 /*
492 * We maintain an extra 2 digits of precision for simplicity
493 * - shift those back off before writing the value
494 */
495 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
496 }
497
498 switch (sattr->nr) {
499 case MIN:
500 reg = TEMP_MIN_REG(sattr->index);
501 break;
502 case MAX:
503 reg = TEMP_MAX_REG(sattr->index);
504 break;
505 case OFFSET:
506 reg = TEMP_OFFSET_REG(sattr->index);
507 break;
508 case AUTOMIN:
509 reg = TEMP_TMIN_REG(sattr->index);
510 break;
511 case THERM:
512 reg = TEMP_THERM_REG(sattr->index);
513 break;
514 case HYSTERSIS:
515 if (sattr->index != 2)
516 reg = REG_REMOTE1_HYSTERSIS;
517 else
518 reg = REG_REMOTE2_HYSTERSIS;
519
520 break;
521 }
522
523 i2c_smbus_write_byte_data(client, reg, out);
524
525 mutex_unlock(&data->lock);
526 return count;
527}
528
529/*
530 * Table of autorange values - the user will write the value in millidegrees,
531 * and we'll convert it
532 */
533static const int autorange_table[] = {
534 2000, 2500, 3330, 4000, 5000, 6670, 8000,
535 10000, 13330, 16000, 20000, 26670, 32000, 40000,
536 53330, 80000
537};
538
539static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
540 char *buf)
541{
542 struct adt7475_data *data = adt7475_update_device(dev);
543 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
544 int out, val;
545
546 mutex_lock(&data->lock);
547 out = (data->range[sattr->index] >> 4) & 0x0F;
548 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
549 mutex_unlock(&data->lock);
550
551 return sprintf(buf, "%d\n", val + autorange_table[out]);
552}
553
554static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
555 const char *buf, size_t count)
556{
557 struct i2c_client *client = to_i2c_client(dev);
558 struct adt7475_data *data = i2c_get_clientdata(client);
559 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
560 int temp;
561 long val;
562
563 if (kstrtol(buf, 10, &val))
564 return -EINVAL;
565
566 mutex_lock(&data->lock);
567
568 /* Get a fresh copy of the needed registers */
569 data->config5 = adt7475_read(REG_CONFIG5);
570 data->temp[AUTOMIN][sattr->index] =
571 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
572 data->range[sattr->index] =
573 adt7475_read(TEMP_TRANGE_REG(sattr->index));
574
575 /*
576 * The user will write an absolute value, so subtract the start point
577 * to figure the range
578 */
579 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
580 val = clamp_val(val, temp + autorange_table[0],
581 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
582 val -= temp;
583
584 /* Find the nearest table entry to what the user wrote */
585 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
586
587 data->range[sattr->index] &= ~0xF0;
588 data->range[sattr->index] |= val << 4;
589
590 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
591 data->range[sattr->index]);
592
593 mutex_unlock(&data->lock);
594 return count;
595}
596
597static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
598 char *buf)
599{
600 struct adt7475_data *data = adt7475_update_device(dev);
601 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
602 int out;
603
604 if (sattr->nr == ALARM)
605 out = (data->alarms >> (sattr->index + 10)) & 1;
606 else
607 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
608
609 return sprintf(buf, "%d\n", out);
610}
611
612static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
614{
615
616 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
617 struct i2c_client *client = to_i2c_client(dev);
618 struct adt7475_data *data = i2c_get_clientdata(client);
619 unsigned long val;
620
621 if (kstrtoul(buf, 10, &val))
622 return -EINVAL;
623
624 mutex_lock(&data->lock);
625
626 data->tach[MIN][sattr->index] = rpm2tach(val);
627
628 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
629 data->tach[MIN][sattr->index]);
630
631 mutex_unlock(&data->lock);
632 return count;
633}
634
635static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
636 char *buf)
637{
638 struct adt7475_data *data = adt7475_update_device(dev);
639 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
640
641 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
642}
643
644static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
645 char *buf)
646{
647 struct adt7475_data *data = adt7475_update_device(dev);
648 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
649
650 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
651}
652
653static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
654 char *buf)
655{
656 struct adt7475_data *data = adt7475_update_device(dev);
657 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
658
659 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
660}
661
662static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
663 const char *buf, size_t count)
664{
665
666 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
667 struct i2c_client *client = to_i2c_client(dev);
668 struct adt7475_data *data = i2c_get_clientdata(client);
669 unsigned char reg = 0;
670 long val;
671
672 if (kstrtol(buf, 10, &val))
673 return -EINVAL;
674
675 mutex_lock(&data->lock);
676
677 switch (sattr->nr) {
678 case INPUT:
679 /* Get a fresh value for CONTROL */
680 data->pwm[CONTROL][sattr->index] =
681 adt7475_read(PWM_CONFIG_REG(sattr->index));
682
683 /*
684 * If we are not in manual mode, then we shouldn't allow
685 * the user to set the pwm speed
686 */
687 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
688 mutex_unlock(&data->lock);
689 return count;
690 }
691
692 reg = PWM_REG(sattr->index);
693 break;
694
695 case MIN:
696 reg = PWM_MIN_REG(sattr->index);
697 break;
698
699 case MAX:
700 reg = PWM_MAX_REG(sattr->index);
701 break;
702 }
703
704 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
705 i2c_smbus_write_byte_data(client, reg,
706 data->pwm[sattr->nr][sattr->index]);
707
708 mutex_unlock(&data->lock);
709
710 return count;
711}
712
713/* Called by set_pwmctrl and set_pwmchan */
714
715static int hw_set_pwm(struct i2c_client *client, int index,
716 unsigned int pwmctl, unsigned int pwmchan)
717{
718 struct adt7475_data *data = i2c_get_clientdata(client);
719 long val = 0;
720
721 switch (pwmctl) {
722 case 0:
723 val = 0x03; /* Run at full speed */
724 break;
725 case 1:
726 val = 0x07; /* Manual mode */
727 break;
728 case 2:
729 switch (pwmchan) {
730 case 1:
731 /* Remote1 controls PWM */
732 val = 0x00;
733 break;
734 case 2:
735 /* local controls PWM */
736 val = 0x01;
737 break;
738 case 4:
739 /* remote2 controls PWM */
740 val = 0x02;
741 break;
742 case 6:
743 /* local/remote2 control PWM */
744 val = 0x05;
745 break;
746 case 7:
747 /* All three control PWM */
748 val = 0x06;
749 break;
750 default:
751 return -EINVAL;
752 }
753 break;
754 default:
755 return -EINVAL;
756 }
757
758 data->pwmctl[index] = pwmctl;
759 data->pwmchan[index] = pwmchan;
760
761 data->pwm[CONTROL][index] &= ~0xE0;
762 data->pwm[CONTROL][index] |= (val & 7) << 5;
763
764 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
765 data->pwm[CONTROL][index]);
766
767 return 0;
768}
769
770static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
771 const char *buf, size_t count)
772{
773 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
774 struct i2c_client *client = to_i2c_client(dev);
775 struct adt7475_data *data = i2c_get_clientdata(client);
776 int r;
777 long val;
778
779 if (kstrtol(buf, 10, &val))
780 return -EINVAL;
781
782 mutex_lock(&data->lock);
783 /* Read Modify Write PWM values */
784 adt7475_read_pwm(client, sattr->index);
785 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
786 if (r)
787 count = r;
788 mutex_unlock(&data->lock);
789
790 return count;
791}
792
793static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
795{
796 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
797 struct i2c_client *client = to_i2c_client(dev);
798 struct adt7475_data *data = i2c_get_clientdata(client);
799 int r;
800 long val;
801
802 if (kstrtol(buf, 10, &val))
803 return -EINVAL;
804
805 mutex_lock(&data->lock);
806 /* Read Modify Write PWM values */
807 adt7475_read_pwm(client, sattr->index);
808 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
809 if (r)
810 count = r;
811 mutex_unlock(&data->lock);
812
813 return count;
814}
815
816/* List of frequencies for the PWM */
817static const int pwmfreq_table[] = {
818 11, 14, 22, 29, 35, 44, 58, 88
819};
820
821static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
822 char *buf)
823{
824 struct adt7475_data *data = adt7475_update_device(dev);
825 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
826
827 return sprintf(buf, "%d\n",
828 pwmfreq_table[data->range[sattr->index] & 7]);
829}
830
831static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
832 const char *buf, size_t count)
833{
834 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
835 struct i2c_client *client = to_i2c_client(dev);
836 struct adt7475_data *data = i2c_get_clientdata(client);
837 int out;
838 long val;
839
840 if (kstrtol(buf, 10, &val))
841 return -EINVAL;
842
843 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
844
845 mutex_lock(&data->lock);
846
847 data->range[sattr->index] =
848 adt7475_read(TEMP_TRANGE_REG(sattr->index));
849 data->range[sattr->index] &= ~7;
850 data->range[sattr->index] |= out;
851
852 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
853 data->range[sattr->index]);
854
855 mutex_unlock(&data->lock);
856 return count;
857}
858
859static ssize_t show_pwm_at_crit(struct device *dev,
860 struct device_attribute *devattr, char *buf)
861{
862 struct adt7475_data *data = adt7475_update_device(dev);
863 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
864}
865
866static ssize_t set_pwm_at_crit(struct device *dev,
867 struct device_attribute *devattr,
868 const char *buf, size_t count)
869{
870 struct i2c_client *client = to_i2c_client(dev);
871 struct adt7475_data *data = i2c_get_clientdata(client);
872 long val;
873
874 if (kstrtol(buf, 10, &val))
875 return -EINVAL;
876 if (val != 0 && val != 1)
877 return -EINVAL;
878
879 mutex_lock(&data->lock);
880 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
881 if (val)
882 data->config4 |= CONFIG4_MAXDUTY;
883 else
884 data->config4 &= ~CONFIG4_MAXDUTY;
885 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
886 mutex_unlock(&data->lock);
887
888 return count;
889}
890
891static ssize_t show_vrm(struct device *dev, struct device_attribute *devattr,
892 char *buf)
893{
894 struct adt7475_data *data = dev_get_drvdata(dev);
895 return sprintf(buf, "%d\n", (int)data->vrm);
896}
897
898static ssize_t set_vrm(struct device *dev, struct device_attribute *devattr,
899 const char *buf, size_t count)
900{
901 struct adt7475_data *data = dev_get_drvdata(dev);
902 long val;
903
904 if (kstrtol(buf, 10, &val))
905 return -EINVAL;
906 if (val < 0 || val > 255)
907 return -EINVAL;
908 data->vrm = val;
909
910 return count;
911}
912
913static ssize_t show_vid(struct device *dev, struct device_attribute *devattr,
914 char *buf)
915{
916 struct adt7475_data *data = adt7475_update_device(dev);
917 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
918}
919
920static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
921static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
922 set_voltage, MAX, 0);
923static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
924 set_voltage, MIN, 0);
925static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
926static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
927static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
928 set_voltage, MAX, 1);
929static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
930 set_voltage, MIN, 1);
931static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
932static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
933static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
934 set_voltage, MAX, 2);
935static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
936 set_voltage, MIN, 2);
937static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
938static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
939static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
940 set_voltage, MAX, 3);
941static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
942 set_voltage, MIN, 3);
943static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
944static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
945static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
946 set_voltage, MAX, 4);
947static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
948 set_voltage, MIN, 4);
949static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
950static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
951static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
952 set_voltage, MAX, 5);
953static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
954 set_voltage, MIN, 5);
955static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
956static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
957static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
958static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
959static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
960 MAX, 0);
961static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
962 MIN, 0);
963static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
964 set_temp, OFFSET, 0);
965static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
966 show_temp, set_temp, AUTOMIN, 0);
967static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
968 show_point2, set_point2, 0, 0);
969static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
970 THERM, 0);
971static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
972 set_temp, HYSTERSIS, 0);
973static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
974static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
975static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
976 MAX, 1);
977static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
978 MIN, 1);
979static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
980 set_temp, OFFSET, 1);
981static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
982 show_temp, set_temp, AUTOMIN, 1);
983static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
984 show_point2, set_point2, 0, 1);
985static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
986 THERM, 1);
987static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
988 set_temp, HYSTERSIS, 1);
989static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
990static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
991static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
992static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
993 MAX, 2);
994static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
995 MIN, 2);
996static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
997 set_temp, OFFSET, 2);
998static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
999 show_temp, set_temp, AUTOMIN, 2);
1000static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1001 show_point2, set_point2, 0, 2);
1002static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1003 THERM, 2);
1004static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1005 set_temp, HYSTERSIS, 2);
1006static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1007static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1008 MIN, 0);
1009static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1010static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1011static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1012 MIN, 1);
1013static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1014static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1015static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1016 MIN, 2);
1017static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1018static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1019static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1020 MIN, 3);
1021static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1022static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1023 0);
1024static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1025 set_pwmfreq, INPUT, 0);
1026static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1027 set_pwmctrl, INPUT, 0);
1028static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1029 show_pwmchan, set_pwmchan, INPUT, 0);
1030static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1031 set_pwm, MIN, 0);
1032static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1033 set_pwm, MAX, 0);
1034static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1035 1);
1036static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1037 set_pwmfreq, INPUT, 1);
1038static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1039 set_pwmctrl, INPUT, 1);
1040static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1041 show_pwmchan, set_pwmchan, INPUT, 1);
1042static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1043 set_pwm, MIN, 1);
1044static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1045 set_pwm, MAX, 1);
1046static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1047 2);
1048static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1049 set_pwmfreq, INPUT, 2);
1050static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1051 set_pwmctrl, INPUT, 2);
1052static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1053 show_pwmchan, set_pwmchan, INPUT, 2);
1054static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1055 set_pwm, MIN, 2);
1056static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1057 set_pwm, MAX, 2);
1058
1059/* Non-standard name, might need revisiting */
1060static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
1061 show_pwm_at_crit, set_pwm_at_crit);
1062
1063static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, set_vrm);
1064static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
1065
1066static struct attribute *adt7475_attrs[] = {
1067 &sensor_dev_attr_in1_input.dev_attr.attr,
1068 &sensor_dev_attr_in1_max.dev_attr.attr,
1069 &sensor_dev_attr_in1_min.dev_attr.attr,
1070 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1071 &sensor_dev_attr_in2_input.dev_attr.attr,
1072 &sensor_dev_attr_in2_max.dev_attr.attr,
1073 &sensor_dev_attr_in2_min.dev_attr.attr,
1074 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1075 &sensor_dev_attr_temp1_input.dev_attr.attr,
1076 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1077 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1078 &sensor_dev_attr_temp1_max.dev_attr.attr,
1079 &sensor_dev_attr_temp1_min.dev_attr.attr,
1080 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1081 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1082 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1083 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1084 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1085 &sensor_dev_attr_temp2_input.dev_attr.attr,
1086 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1087 &sensor_dev_attr_temp2_max.dev_attr.attr,
1088 &sensor_dev_attr_temp2_min.dev_attr.attr,
1089 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1090 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1091 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1092 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1093 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1094 &sensor_dev_attr_temp3_input.dev_attr.attr,
1095 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1096 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1097 &sensor_dev_attr_temp3_max.dev_attr.attr,
1098 &sensor_dev_attr_temp3_min.dev_attr.attr,
1099 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1100 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1101 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1102 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1103 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1104 &sensor_dev_attr_fan1_input.dev_attr.attr,
1105 &sensor_dev_attr_fan1_min.dev_attr.attr,
1106 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1107 &sensor_dev_attr_fan2_input.dev_attr.attr,
1108 &sensor_dev_attr_fan2_min.dev_attr.attr,
1109 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1110 &sensor_dev_attr_fan3_input.dev_attr.attr,
1111 &sensor_dev_attr_fan3_min.dev_attr.attr,
1112 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1113 &sensor_dev_attr_pwm1.dev_attr.attr,
1114 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1115 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1116 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1117 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1118 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1119 &sensor_dev_attr_pwm3.dev_attr.attr,
1120 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1121 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1122 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1123 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1124 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1125 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
1126 NULL,
1127};
1128
1129static struct attribute *fan4_attrs[] = {
1130 &sensor_dev_attr_fan4_input.dev_attr.attr,
1131 &sensor_dev_attr_fan4_min.dev_attr.attr,
1132 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1133 NULL
1134};
1135
1136static struct attribute *pwm2_attrs[] = {
1137 &sensor_dev_attr_pwm2.dev_attr.attr,
1138 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1139 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1140 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1141 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1142 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1143 NULL
1144};
1145
1146static struct attribute *in0_attrs[] = {
1147 &sensor_dev_attr_in0_input.dev_attr.attr,
1148 &sensor_dev_attr_in0_max.dev_attr.attr,
1149 &sensor_dev_attr_in0_min.dev_attr.attr,
1150 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1151 NULL
1152};
1153
1154static struct attribute *in3_attrs[] = {
1155 &sensor_dev_attr_in3_input.dev_attr.attr,
1156 &sensor_dev_attr_in3_max.dev_attr.attr,
1157 &sensor_dev_attr_in3_min.dev_attr.attr,
1158 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1159 NULL
1160};
1161
1162static struct attribute *in4_attrs[] = {
1163 &sensor_dev_attr_in4_input.dev_attr.attr,
1164 &sensor_dev_attr_in4_max.dev_attr.attr,
1165 &sensor_dev_attr_in4_min.dev_attr.attr,
1166 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1167 NULL
1168};
1169
1170static struct attribute *in5_attrs[] = {
1171 &sensor_dev_attr_in5_input.dev_attr.attr,
1172 &sensor_dev_attr_in5_max.dev_attr.attr,
1173 &sensor_dev_attr_in5_min.dev_attr.attr,
1174 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1175 NULL
1176};
1177
1178static struct attribute *vid_attrs[] = {
1179 &dev_attr_cpu0_vid.attr,
1180 &dev_attr_vrm.attr,
1181 NULL
1182};
1183
1184static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1185static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1186static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1187static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1188static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1189static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1190static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1191static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
1192
1193static int adt7475_detect(struct i2c_client *client,
1194 struct i2c_board_info *info)
1195{
1196 struct i2c_adapter *adapter = client->adapter;
1197 int vendid, devid, devid2;
1198 const char *name;
1199
1200 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1201 return -ENODEV;
1202
1203 vendid = adt7475_read(REG_VENDID);
1204 devid2 = adt7475_read(REG_DEVID2);
1205 if (vendid != 0x41 || /* Analog Devices */
1206 (devid2 & 0xf8) != 0x68)
1207 return -ENODEV;
1208
1209 devid = adt7475_read(REG_DEVID);
1210 if (devid == 0x73)
1211 name = "adt7473";
1212 else if (devid == 0x75 && client->addr == 0x2e)
1213 name = "adt7475";
1214 else if (devid == 0x76)
1215 name = "adt7476";
1216 else if ((devid2 & 0xfc) == 0x6c)
1217 name = "adt7490";
1218 else {
1219 dev_dbg(&adapter->dev,
1220 "Couldn't detect an ADT7473/75/76/90 part at "
1221 "0x%02x\n", (unsigned int)client->addr);
1222 return -ENODEV;
1223 }
1224
1225 strlcpy(info->type, name, I2C_NAME_SIZE);
1226
1227 return 0;
1228}
1229
1230static void adt7475_remove_files(struct i2c_client *client,
1231 struct adt7475_data *data)
1232{
1233 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1234 if (data->has_fan4)
1235 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1236 if (data->has_pwm2)
1237 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1238 if (data->has_voltage & (1 << 0))
1239 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1240 if (data->has_voltage & (1 << 3))
1241 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1242 if (data->has_voltage & (1 << 4))
1243 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1244 if (data->has_voltage & (1 << 5))
1245 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
1246 if (data->has_vid)
1247 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
1248}
1249
1250static int adt7475_probe(struct i2c_client *client,
1251 const struct i2c_device_id *id)
1252{
1253 static const char * const names[] = {
1254 [adt7473] = "ADT7473",
1255 [adt7475] = "ADT7475",
1256 [adt7476] = "ADT7476",
1257 [adt7490] = "ADT7490",
1258 };
1259
1260 struct adt7475_data *data;
1261 int i, ret = 0, revision;
1262 u8 config2, config3;
1263
1264 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1265 if (data == NULL)
1266 return -ENOMEM;
1267
1268 mutex_init(&data->lock);
1269 i2c_set_clientdata(client, data);
1270
1271 /* Initialize device-specific values */
1272 switch (id->driver_data) {
1273 case adt7476:
1274 data->has_voltage = 0x0e; /* in1 to in3 */
1275 revision = adt7475_read(REG_DEVID2) & 0x07;
1276 break;
1277 case adt7490:
1278 data->has_voltage = 0x3e; /* in1 to in5 */
1279 revision = adt7475_read(REG_DEVID2) & 0x03;
1280 if (revision == 0x03)
1281 revision += adt7475_read(REG_DEVREV2);
1282 break;
1283 default:
1284 data->has_voltage = 0x06; /* in1, in2 */
1285 revision = adt7475_read(REG_DEVID2) & 0x07;
1286 }
1287
1288 config3 = adt7475_read(REG_CONFIG3);
1289 /* Pin PWM2 may alternatively be used for ALERT output */
1290 if (!(config3 & CONFIG3_SMBALERT))
1291 data->has_pwm2 = 1;
1292 /* Meaning of this bit is inverted for the ADT7473-1 */
1293 if (id->driver_data == adt7473 && revision >= 1)
1294 data->has_pwm2 = !data->has_pwm2;
1295
1296 data->config4 = adt7475_read(REG_CONFIG4);
1297 /* Pin TACH4 may alternatively be used for THERM */
1298 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1299 data->has_fan4 = 1;
1300
1301 /*
1302 * THERM configuration is more complex on the ADT7476 and ADT7490,
1303 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1304 * this function
1305 */
1306 if (id->driver_data == adt7490) {
1307 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1308 !(config3 & CONFIG3_THERM))
1309 data->has_fan4 = 1;
1310 }
1311 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1312 if (!(config3 & CONFIG3_THERM) ||
1313 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1314 data->has_voltage |= (1 << 0); /* in0 */
1315 }
1316
1317 /*
1318 * On the ADT7476, the +12V input pin may instead be used as VID5,
1319 * and VID pins may alternatively be used as GPIO
1320 */
1321 if (id->driver_data == adt7476) {
1322 u8 vid = adt7475_read(REG_VID);
1323 if (!(vid & VID_VIDSEL))
1324 data->has_voltage |= (1 << 4); /* in4 */
1325
1326 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1327 }
1328
1329 /* Voltage attenuators can be bypassed, globally or individually */
1330 config2 = adt7475_read(REG_CONFIG2);
1331 if (config2 & CONFIG2_ATTN) {
1332 data->bypass_attn = (0x3 << 3) | 0x3;
1333 } else {
1334 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1335 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1336 }
1337 data->bypass_attn &= data->has_voltage;
1338
1339 /*
1340 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1341 * pwm's which are disabled to manual mode with 0% duty cycle
1342 */
1343 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1344 adt7475_read_pwm(client, i);
1345
1346 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1347 if (ret)
1348 return ret;
1349
1350 /* Features that can be disabled individually */
1351 if (data->has_fan4) {
1352 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1353 if (ret)
1354 goto eremove;
1355 }
1356 if (data->has_pwm2) {
1357 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1358 if (ret)
1359 goto eremove;
1360 }
1361 if (data->has_voltage & (1 << 0)) {
1362 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1363 if (ret)
1364 goto eremove;
1365 }
1366 if (data->has_voltage & (1 << 3)) {
1367 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1368 if (ret)
1369 goto eremove;
1370 }
1371 if (data->has_voltage & (1 << 4)) {
1372 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1373 if (ret)
1374 goto eremove;
1375 }
1376 if (data->has_voltage & (1 << 5)) {
1377 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1378 if (ret)
1379 goto eremove;
1380 }
1381 if (data->has_vid) {
1382 data->vrm = vid_which_vrm();
1383 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1384 if (ret)
1385 goto eremove;
1386 }
1387
1388 data->hwmon_dev = hwmon_device_register(&client->dev);
1389 if (IS_ERR(data->hwmon_dev)) {
1390 ret = PTR_ERR(data->hwmon_dev);
1391 goto eremove;
1392 }
1393
1394 dev_info(&client->dev, "%s device, revision %d\n",
1395 names[id->driver_data], revision);
1396 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
1397 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1398 (data->has_voltage & (1 << 0)) ? " in0" : "",
1399 (data->has_voltage & (1 << 4)) ? " in4" : "",
1400 data->has_fan4 ? " fan4" : "",
1401 data->has_pwm2 ? " pwm2" : "",
1402 data->has_vid ? " vid" : "");
1403 if (data->bypass_attn)
1404 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1405 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1406 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1407 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1408 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1409
1410 return 0;
1411
1412eremove:
1413 adt7475_remove_files(client, data);
1414 return ret;
1415}
1416
1417static int adt7475_remove(struct i2c_client *client)
1418{
1419 struct adt7475_data *data = i2c_get_clientdata(client);
1420
1421 hwmon_device_unregister(data->hwmon_dev);
1422 adt7475_remove_files(client, data);
1423
1424 return 0;
1425}
1426
1427static struct i2c_driver adt7475_driver = {
1428 .class = I2C_CLASS_HWMON,
1429 .driver = {
1430 .name = "adt7475",
1431 },
1432 .probe = adt7475_probe,
1433 .remove = adt7475_remove,
1434 .id_table = adt7475_id,
1435 .detect = adt7475_detect,
1436 .address_list = normal_i2c,
1437};
1438
1439static void adt7475_read_hystersis(struct i2c_client *client)
1440{
1441 struct adt7475_data *data = i2c_get_clientdata(client);
1442
1443 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1444 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1445 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1446}
1447
1448static void adt7475_read_pwm(struct i2c_client *client, int index)
1449{
1450 struct adt7475_data *data = i2c_get_clientdata(client);
1451 unsigned int v;
1452
1453 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1454
1455 /*
1456 * Figure out the internal value for pwmctrl and pwmchan
1457 * based on the current settings
1458 */
1459 v = (data->pwm[CONTROL][index] >> 5) & 7;
1460
1461 if (v == 3)
1462 data->pwmctl[index] = 0;
1463 else if (v == 7)
1464 data->pwmctl[index] = 1;
1465 else if (v == 4) {
1466 /*
1467 * The fan is disabled - we don't want to
1468 * support that, so change to manual mode and
1469 * set the duty cycle to 0 instead
1470 */
1471 data->pwm[INPUT][index] = 0;
1472 data->pwm[CONTROL][index] &= ~0xE0;
1473 data->pwm[CONTROL][index] |= (7 << 5);
1474
1475 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1476 data->pwm[INPUT][index]);
1477
1478 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1479 data->pwm[CONTROL][index]);
1480
1481 data->pwmctl[index] = 1;
1482 } else {
1483 data->pwmctl[index] = 2;
1484
1485 switch (v) {
1486 case 0:
1487 data->pwmchan[index] = 1;
1488 break;
1489 case 1:
1490 data->pwmchan[index] = 2;
1491 break;
1492 case 2:
1493 data->pwmchan[index] = 4;
1494 break;
1495 case 5:
1496 data->pwmchan[index] = 6;
1497 break;
1498 case 6:
1499 data->pwmchan[index] = 7;
1500 break;
1501 }
1502 }
1503}
1504
1505static struct adt7475_data *adt7475_update_device(struct device *dev)
1506{
1507 struct i2c_client *client = to_i2c_client(dev);
1508 struct adt7475_data *data = i2c_get_clientdata(client);
1509 u16 ext;
1510 int i;
1511
1512 mutex_lock(&data->lock);
1513
1514 /* Measurement values update every 2 seconds */
1515 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1516 !data->valid) {
1517 data->alarms = adt7475_read(REG_STATUS2) << 8;
1518 data->alarms |= adt7475_read(REG_STATUS1);
1519
1520 ext = (adt7475_read(REG_EXTEND2) << 8) |
1521 adt7475_read(REG_EXTEND1);
1522 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1523 if (!(data->has_voltage & (1 << i)))
1524 continue;
1525 data->voltage[INPUT][i] =
1526 (adt7475_read(VOLTAGE_REG(i)) << 2) |
1527 ((ext >> (i * 2)) & 3);
1528 }
1529
1530 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1531 data->temp[INPUT][i] =
1532 (adt7475_read(TEMP_REG(i)) << 2) |
1533 ((ext >> ((i + 5) * 2)) & 3);
1534
1535 if (data->has_voltage & (1 << 5)) {
1536 data->alarms |= adt7475_read(REG_STATUS4) << 24;
1537 ext = adt7475_read(REG_EXTEND3);
1538 data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
1539 ((ext >> 4) & 3);
1540 }
1541
1542 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1543 if (i == 3 && !data->has_fan4)
1544 continue;
1545 data->tach[INPUT][i] =
1546 adt7475_read_word(client, TACH_REG(i));
1547 }
1548
1549 /* Updated by hw when in auto mode */
1550 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1551 if (i == 1 && !data->has_pwm2)
1552 continue;
1553 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1554 }
1555
1556 if (data->has_vid)
1557 data->vid = adt7475_read(REG_VID) & 0x3f;
1558
1559 data->measure_updated = jiffies;
1560 }
1561
1562 /* Limits and settings, should never change update every 60 seconds */
1563 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
1564 !data->valid) {
1565 data->config4 = adt7475_read(REG_CONFIG4);
1566 data->config5 = adt7475_read(REG_CONFIG5);
1567
1568 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1569 if (!(data->has_voltage & (1 << i)))
1570 continue;
1571 /* Adjust values so they match the input precision */
1572 data->voltage[MIN][i] =
1573 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1574 data->voltage[MAX][i] =
1575 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1576 }
1577
1578 if (data->has_voltage & (1 << 5)) {
1579 data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
1580 data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
1581 }
1582
1583 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1584 /* Adjust values so they match the input precision */
1585 data->temp[MIN][i] =
1586 adt7475_read(TEMP_MIN_REG(i)) << 2;
1587 data->temp[MAX][i] =
1588 adt7475_read(TEMP_MAX_REG(i)) << 2;
1589 data->temp[AUTOMIN][i] =
1590 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1591 data->temp[THERM][i] =
1592 adt7475_read(TEMP_THERM_REG(i)) << 2;
1593 data->temp[OFFSET][i] =
1594 adt7475_read(TEMP_OFFSET_REG(i));
1595 }
1596 adt7475_read_hystersis(client);
1597
1598 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1599 if (i == 3 && !data->has_fan4)
1600 continue;
1601 data->tach[MIN][i] =
1602 adt7475_read_word(client, TACH_MIN_REG(i));
1603 }
1604
1605 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1606 if (i == 1 && !data->has_pwm2)
1607 continue;
1608 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1609 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1610 /* Set the channel and control information */
1611 adt7475_read_pwm(client, i);
1612 }
1613
1614 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1615 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1616 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1617
1618 data->limits_updated = jiffies;
1619 data->valid = 1;
1620 }
1621
1622 mutex_unlock(&data->lock);
1623
1624 return data;
1625}
1626
1627module_i2c_driver(adt7475_driver);
1628
1629MODULE_AUTHOR("Advanced Micro Devices, Inc");
1630MODULE_DESCRIPTION("adt7475 driver");
1631MODULE_LICENSE("GPL");