Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / adt7475.c
blob0a87c5b5128623de65065452695f29ede095a0a9
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>
8 * Derived from the lm83 driver by Jean Delvare
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.
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/hwmon-vid.h>
23 #include <linux/err.h>
24 #include <linux/jiffies.h>
25 #include <linux/util_macros.h>
27 /* Indexes for the sysfs hooks */
29 #define INPUT 0
30 #define MIN 1
31 #define MAX 2
32 #define CONTROL 3
33 #define OFFSET 3
34 #define AUTOMIN 4
35 #define THERM 5
36 #define HYSTERSIS 6
39 * These are unique identifiers for the sysfs functions - unlike the
40 * numbers above, these are not also indexes into an array
43 #define ALARM 9
44 #define FAULT 10
46 /* 7475 Common Registers */
48 #define REG_DEVREV2 0x12 /* ADT7490 only */
50 #define REG_VTT 0x1E /* ADT7490 only */
51 #define REG_EXTEND3 0x1F /* ADT7490 only */
53 #define REG_VOLTAGE_BASE 0x20
54 #define REG_TEMP_BASE 0x25
55 #define REG_TACH_BASE 0x28
56 #define REG_PWM_BASE 0x30
57 #define REG_PWM_MAX_BASE 0x38
59 #define REG_DEVID 0x3D
60 #define REG_VENDID 0x3E
61 #define REG_DEVID2 0x3F
63 #define REG_CONFIG1 0x40
65 #define REG_STATUS1 0x41
66 #define REG_STATUS2 0x42
68 #define REG_VID 0x43 /* ADT7476 only */
70 #define REG_VOLTAGE_MIN_BASE 0x44
71 #define REG_VOLTAGE_MAX_BASE 0x45
73 #define REG_TEMP_MIN_BASE 0x4E
74 #define REG_TEMP_MAX_BASE 0x4F
76 #define REG_TACH_MIN_BASE 0x54
78 #define REG_PWM_CONFIG_BASE 0x5C
80 #define REG_TEMP_TRANGE_BASE 0x5F
82 #define REG_ENHANCE_ACOUSTICS1 0x62
83 #define REG_ENHANCE_ACOUSTICS2 0x63
85 #define REG_PWM_MIN_BASE 0x64
87 #define REG_TEMP_TMIN_BASE 0x67
88 #define REG_TEMP_THERM_BASE 0x6A
90 #define REG_REMOTE1_HYSTERSIS 0x6D
91 #define REG_REMOTE2_HYSTERSIS 0x6E
93 #define REG_TEMP_OFFSET_BASE 0x70
95 #define REG_CONFIG2 0x73
97 #define REG_EXTEND1 0x76
98 #define REG_EXTEND2 0x77
100 #define REG_CONFIG3 0x78
101 #define REG_CONFIG5 0x7C
102 #define REG_CONFIG4 0x7D
104 #define REG_STATUS4 0x81 /* ADT7490 only */
106 #define REG_VTT_MIN 0x84 /* ADT7490 only */
107 #define REG_VTT_MAX 0x86 /* ADT7490 only */
109 #define VID_VIDSEL 0x80 /* ADT7476 only */
111 #define CONFIG2_ATTN 0x20
113 #define CONFIG3_SMBALERT 0x01
114 #define CONFIG3_THERM 0x02
116 #define CONFIG4_PINFUNC 0x03
117 #define CONFIG4_MAXDUTY 0x08
118 #define CONFIG4_ATTN_IN10 0x30
119 #define CONFIG4_ATTN_IN43 0xC0
121 #define CONFIG5_TWOSCOMP 0x01
122 #define CONFIG5_TEMPOFFSET 0x02
123 #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
125 /* ADT7475 Settings */
127 #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
128 #define ADT7475_TEMP_COUNT 3
129 #define ADT7475_TACH_COUNT 4
130 #define ADT7475_PWM_COUNT 3
132 /* Macro to read the registers */
134 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
136 /* Macros to easily index the registers */
138 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
139 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
141 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
142 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
143 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
144 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
146 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
147 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
148 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
150 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
151 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
152 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
153 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
154 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
155 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
156 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
158 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
160 enum chips { adt7473, adt7475, adt7476, adt7490 };
162 static const struct i2c_device_id adt7475_id[] = {
163 { "adt7473", adt7473 },
164 { "adt7475", adt7475 },
165 { "adt7476", adt7476 },
166 { "adt7490", adt7490 },
169 MODULE_DEVICE_TABLE(i2c, adt7475_id);
171 static const struct of_device_id adt7475_of_match[] = {
173 .compatible = "adi,adt7473",
174 .data = (void *)adt7473
177 .compatible = "adi,adt7475",
178 .data = (void *)adt7475
181 .compatible = "adi,adt7476",
182 .data = (void *)adt7476
185 .compatible = "adi,adt7490",
186 .data = (void *)adt7490
188 { },
190 MODULE_DEVICE_TABLE(of, adt7475_of_match);
192 struct adt7475_data {
193 struct device *hwmon_dev;
194 struct mutex lock;
196 unsigned long measure_updated;
197 bool valid;
199 u8 config4;
200 u8 config5;
201 u8 has_voltage;
202 u8 bypass_attn; /* Bypass voltage attenuator */
203 u8 has_pwm2:1;
204 u8 has_fan4:1;
205 u8 has_vid:1;
206 u32 alarms;
207 u16 voltage[3][6];
208 u16 temp[7][3];
209 u16 tach[2][4];
210 u8 pwm[4][3];
211 u8 range[3];
212 u8 pwmctl[3];
213 u8 pwmchan[3];
214 u8 enh_acoustics[2];
216 u8 vid;
217 u8 vrm;
220 static struct i2c_driver adt7475_driver;
221 static struct adt7475_data *adt7475_update_device(struct device *dev);
222 static void adt7475_read_hystersis(struct i2c_client *client);
223 static void adt7475_read_pwm(struct i2c_client *client, int index);
225 /* Given a temp value, convert it to register value */
227 static inline u16 temp2reg(struct adt7475_data *data, long val)
229 u16 ret;
231 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
232 val = clamp_val(val, -64000, 191000);
233 ret = (val + 64500) / 1000;
234 } else {
235 val = clamp_val(val, -128000, 127000);
236 if (val < -500)
237 ret = (256500 + val) / 1000;
238 else
239 ret = (val + 500) / 1000;
242 return ret << 2;
245 /* Given a register value, convert it to a real temp value */
247 static inline int reg2temp(struct adt7475_data *data, u16 reg)
249 if (data->config5 & CONFIG5_TWOSCOMP) {
250 if (reg >= 512)
251 return (reg - 1024) * 250;
252 else
253 return reg * 250;
254 } else
255 return (reg - 256) * 250;
258 static inline int tach2rpm(u16 tach)
260 if (tach == 0 || tach == 0xFFFF)
261 return 0;
263 return (90000 * 60) / tach;
266 static inline u16 rpm2tach(unsigned long rpm)
268 if (rpm == 0)
269 return 0;
271 return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
274 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
275 static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
276 { 45, 94 }, /* +2.5V */
277 { 175, 525 }, /* Vccp */
278 { 68, 71 }, /* Vcc */
279 { 93, 47 }, /* +5V */
280 { 120, 20 }, /* +12V */
281 { 45, 45 }, /* Vtt */
284 static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
286 const int *r = adt7473_in_scaling[channel];
288 if (bypass_attn & (1 << channel))
289 return DIV_ROUND_CLOSEST(reg * 2250, 1024);
290 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
293 static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
295 const int *r = adt7473_in_scaling[channel];
296 long reg;
298 if (bypass_attn & (1 << channel))
299 reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
300 else
301 reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
302 (r[0] + r[1]) * 2250);
303 return clamp_val(reg, 0, 1023) & (0xff << 2);
306 static int adt7475_read_word(struct i2c_client *client, int reg)
308 int val1, val2;
310 val1 = i2c_smbus_read_byte_data(client, reg);
311 if (val1 < 0)
312 return val1;
313 val2 = i2c_smbus_read_byte_data(client, reg + 1);
314 if (val2 < 0)
315 return val2;
317 return val1 | (val2 << 8);
320 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
322 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
323 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
326 static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
327 char *buf)
329 struct adt7475_data *data = adt7475_update_device(dev);
330 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
331 unsigned short val;
333 if (IS_ERR(data))
334 return PTR_ERR(data);
336 switch (sattr->nr) {
337 case ALARM:
338 return sprintf(buf, "%d\n",
339 (data->alarms >> sattr->index) & 1);
340 default:
341 val = data->voltage[sattr->nr][sattr->index];
342 return sprintf(buf, "%d\n",
343 reg2volt(sattr->index, val, data->bypass_attn));
347 static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count)
351 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
352 struct i2c_client *client = to_i2c_client(dev);
353 struct adt7475_data *data = i2c_get_clientdata(client);
354 unsigned char reg;
355 long val;
357 if (kstrtol(buf, 10, &val))
358 return -EINVAL;
360 mutex_lock(&data->lock);
362 data->voltage[sattr->nr][sattr->index] =
363 volt2reg(sattr->index, val, data->bypass_attn);
365 if (sattr->index < ADT7475_VOLTAGE_COUNT) {
366 if (sattr->nr == MIN)
367 reg = VOLTAGE_MIN_REG(sattr->index);
368 else
369 reg = VOLTAGE_MAX_REG(sattr->index);
370 } else {
371 if (sattr->nr == MIN)
372 reg = REG_VTT_MIN;
373 else
374 reg = REG_VTT_MAX;
377 i2c_smbus_write_byte_data(client, reg,
378 data->voltage[sattr->nr][sattr->index] >> 2);
379 mutex_unlock(&data->lock);
381 return count;
384 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
385 char *buf)
387 struct adt7475_data *data = adt7475_update_device(dev);
388 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
389 int out;
391 if (IS_ERR(data))
392 return PTR_ERR(data);
394 switch (sattr->nr) {
395 case HYSTERSIS:
396 mutex_lock(&data->lock);
397 out = data->temp[sattr->nr][sattr->index];
398 if (sattr->index != 1)
399 out = (out >> 4) & 0xF;
400 else
401 out = (out & 0xF);
403 * Show the value as an absolute number tied to
404 * THERM
406 out = reg2temp(data, data->temp[THERM][sattr->index]) -
407 out * 1000;
408 mutex_unlock(&data->lock);
409 break;
411 case OFFSET:
413 * Offset is always 2's complement, regardless of the
414 * setting in CONFIG5
416 mutex_lock(&data->lock);
417 out = (s8)data->temp[sattr->nr][sattr->index];
418 if (data->config5 & CONFIG5_TEMPOFFSET)
419 out *= 1000;
420 else
421 out *= 500;
422 mutex_unlock(&data->lock);
423 break;
425 case ALARM:
426 out = (data->alarms >> (sattr->index + 4)) & 1;
427 break;
429 case FAULT:
430 /* Note - only for remote1 and remote2 */
431 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
432 break;
434 default:
435 /* All other temp values are in the configured format */
436 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
439 return sprintf(buf, "%d\n", out);
442 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
443 const char *buf, size_t count)
445 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
446 struct i2c_client *client = to_i2c_client(dev);
447 struct adt7475_data *data = i2c_get_clientdata(client);
448 unsigned char reg = 0;
449 u8 out;
450 int temp;
451 long val;
453 if (kstrtol(buf, 10, &val))
454 return -EINVAL;
456 mutex_lock(&data->lock);
458 /* We need the config register in all cases for temp <-> reg conv. */
459 data->config5 = adt7475_read(REG_CONFIG5);
461 switch (sattr->nr) {
462 case OFFSET:
463 if (data->config5 & CONFIG5_TEMPOFFSET) {
464 val = clamp_val(val, -63000, 127000);
465 out = data->temp[OFFSET][sattr->index] = val / 1000;
466 } else {
467 val = clamp_val(val, -63000, 64000);
468 out = data->temp[OFFSET][sattr->index] = val / 500;
470 break;
472 case HYSTERSIS:
474 * The value will be given as an absolute value, turn it
475 * into an offset based on THERM
478 /* Read fresh THERM and HYSTERSIS values from the chip */
479 data->temp[THERM][sattr->index] =
480 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
481 adt7475_read_hystersis(client);
483 temp = reg2temp(data, data->temp[THERM][sattr->index]);
484 val = clamp_val(val, temp - 15000, temp);
485 val = (temp - val) / 1000;
487 if (sattr->index != 1) {
488 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
489 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
490 } else {
491 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
492 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
495 out = data->temp[HYSTERSIS][sattr->index];
496 break;
498 default:
499 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
502 * We maintain an extra 2 digits of precision for simplicity
503 * - shift those back off before writing the value
505 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
508 switch (sattr->nr) {
509 case MIN:
510 reg = TEMP_MIN_REG(sattr->index);
511 break;
512 case MAX:
513 reg = TEMP_MAX_REG(sattr->index);
514 break;
515 case OFFSET:
516 reg = TEMP_OFFSET_REG(sattr->index);
517 break;
518 case AUTOMIN:
519 reg = TEMP_TMIN_REG(sattr->index);
520 break;
521 case THERM:
522 reg = TEMP_THERM_REG(sattr->index);
523 break;
524 case HYSTERSIS:
525 if (sattr->index != 2)
526 reg = REG_REMOTE1_HYSTERSIS;
527 else
528 reg = REG_REMOTE2_HYSTERSIS;
530 break;
533 i2c_smbus_write_byte_data(client, reg, out);
535 mutex_unlock(&data->lock);
536 return count;
539 /* Assuming CONFIG6[SLOW] is 0 */
540 static const int ad7475_st_map[] = {
541 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
544 static ssize_t show_temp_st(struct device *dev, struct device_attribute *attr,
545 char *buf)
547 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
548 struct i2c_client *client = to_i2c_client(dev);
549 struct adt7475_data *data = i2c_get_clientdata(client);
550 long val;
552 switch (sattr->index) {
553 case 0:
554 val = data->enh_acoustics[0] & 0xf;
555 break;
556 case 1:
557 val = (data->enh_acoustics[1] >> 4) & 0xf;
558 break;
559 case 2:
560 default:
561 val = data->enh_acoustics[1] & 0xf;
562 break;
565 if (val & 0x8)
566 return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
567 else
568 return sprintf(buf, "0\n");
571 static ssize_t set_temp_st(struct device *dev, struct device_attribute *attr,
572 const char *buf, size_t count)
574 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
575 struct i2c_client *client = to_i2c_client(dev);
576 struct adt7475_data *data = i2c_get_clientdata(client);
577 unsigned char reg;
578 int shift, idx;
579 ulong val;
581 if (kstrtoul(buf, 10, &val))
582 return -EINVAL;
584 switch (sattr->index) {
585 case 0:
586 reg = REG_ENHANCE_ACOUSTICS1;
587 shift = 0;
588 idx = 0;
589 break;
590 case 1:
591 reg = REG_ENHANCE_ACOUSTICS2;
592 shift = 0;
593 idx = 1;
594 break;
595 case 2:
596 default:
597 reg = REG_ENHANCE_ACOUSTICS2;
598 shift = 4;
599 idx = 1;
600 break;
603 if (val > 0) {
604 val = find_closest_descending(val, ad7475_st_map,
605 ARRAY_SIZE(ad7475_st_map));
606 val |= 0x8;
609 mutex_lock(&data->lock);
611 data->enh_acoustics[idx] &= ~(0xf << shift);
612 data->enh_acoustics[idx] |= (val << shift);
614 i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
616 mutex_unlock(&data->lock);
618 return count;
622 * Table of autorange values - the user will write the value in millidegrees,
623 * and we'll convert it
625 static const int autorange_table[] = {
626 2000, 2500, 3330, 4000, 5000, 6670, 8000,
627 10000, 13330, 16000, 20000, 26670, 32000, 40000,
628 53330, 80000
631 static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
632 char *buf)
634 struct adt7475_data *data = adt7475_update_device(dev);
635 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
636 int out, val;
638 if (IS_ERR(data))
639 return PTR_ERR(data);
641 mutex_lock(&data->lock);
642 out = (data->range[sattr->index] >> 4) & 0x0F;
643 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
644 mutex_unlock(&data->lock);
646 return sprintf(buf, "%d\n", val + autorange_table[out]);
649 static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
650 const char *buf, size_t count)
652 struct i2c_client *client = to_i2c_client(dev);
653 struct adt7475_data *data = i2c_get_clientdata(client);
654 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
655 int temp;
656 long val;
658 if (kstrtol(buf, 10, &val))
659 return -EINVAL;
661 mutex_lock(&data->lock);
663 /* Get a fresh copy of the needed registers */
664 data->config5 = adt7475_read(REG_CONFIG5);
665 data->temp[AUTOMIN][sattr->index] =
666 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
667 data->range[sattr->index] =
668 adt7475_read(TEMP_TRANGE_REG(sattr->index));
671 * The user will write an absolute value, so subtract the start point
672 * to figure the range
674 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
675 val = clamp_val(val, temp + autorange_table[0],
676 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
677 val -= temp;
679 /* Find the nearest table entry to what the user wrote */
680 val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
682 data->range[sattr->index] &= ~0xF0;
683 data->range[sattr->index] |= val << 4;
685 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
686 data->range[sattr->index]);
688 mutex_unlock(&data->lock);
689 return count;
692 static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
693 char *buf)
695 struct adt7475_data *data = adt7475_update_device(dev);
696 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
697 int out;
699 if (IS_ERR(data))
700 return PTR_ERR(data);
702 if (sattr->nr == ALARM)
703 out = (data->alarms >> (sattr->index + 10)) & 1;
704 else
705 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
707 return sprintf(buf, "%d\n", out);
710 static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
714 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
715 struct i2c_client *client = to_i2c_client(dev);
716 struct adt7475_data *data = i2c_get_clientdata(client);
717 unsigned long val;
719 if (kstrtoul(buf, 10, &val))
720 return -EINVAL;
722 mutex_lock(&data->lock);
724 data->tach[MIN][sattr->index] = rpm2tach(val);
726 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
727 data->tach[MIN][sattr->index]);
729 mutex_unlock(&data->lock);
730 return count;
733 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
734 char *buf)
736 struct adt7475_data *data = adt7475_update_device(dev);
737 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
739 if (IS_ERR(data))
740 return PTR_ERR(data);
742 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
745 static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
746 char *buf)
748 struct adt7475_data *data = adt7475_update_device(dev);
749 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
751 if (IS_ERR(data))
752 return PTR_ERR(data);
754 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
757 static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
758 char *buf)
760 struct adt7475_data *data = adt7475_update_device(dev);
761 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
763 if (IS_ERR(data))
764 return PTR_ERR(data);
766 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
769 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
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 unsigned char reg = 0;
777 long val;
779 if (kstrtol(buf, 10, &val))
780 return -EINVAL;
782 mutex_lock(&data->lock);
784 switch (sattr->nr) {
785 case INPUT:
786 /* Get a fresh value for CONTROL */
787 data->pwm[CONTROL][sattr->index] =
788 adt7475_read(PWM_CONFIG_REG(sattr->index));
791 * If we are not in manual mode, then we shouldn't allow
792 * the user to set the pwm speed
794 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
795 mutex_unlock(&data->lock);
796 return count;
799 reg = PWM_REG(sattr->index);
800 break;
802 case MIN:
803 reg = PWM_MIN_REG(sattr->index);
804 break;
806 case MAX:
807 reg = PWM_MAX_REG(sattr->index);
808 break;
811 data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
812 i2c_smbus_write_byte_data(client, reg,
813 data->pwm[sattr->nr][sattr->index]);
814 mutex_unlock(&data->lock);
816 return count;
819 static ssize_t show_stall_disable(struct device *dev,
820 struct device_attribute *attr, char *buf)
822 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
823 struct i2c_client *client = to_i2c_client(dev);
824 struct adt7475_data *data = i2c_get_clientdata(client);
825 u8 mask = BIT(5 + sattr->index);
827 return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
830 static ssize_t set_stall_disable(struct device *dev,
831 struct device_attribute *attr, const char *buf,
832 size_t count)
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 long val;
838 u8 mask = BIT(5 + sattr->index);
840 if (kstrtol(buf, 10, &val))
841 return -EINVAL;
843 mutex_lock(&data->lock);
845 data->enh_acoustics[0] &= ~mask;
846 if (val)
847 data->enh_acoustics[0] |= mask;
849 i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
850 data->enh_acoustics[0]);
852 mutex_unlock(&data->lock);
854 return count;
857 /* Called by set_pwmctrl and set_pwmchan */
859 static int hw_set_pwm(struct i2c_client *client, int index,
860 unsigned int pwmctl, unsigned int pwmchan)
862 struct adt7475_data *data = i2c_get_clientdata(client);
863 long val = 0;
865 switch (pwmctl) {
866 case 0:
867 val = 0x03; /* Run at full speed */
868 break;
869 case 1:
870 val = 0x07; /* Manual mode */
871 break;
872 case 2:
873 switch (pwmchan) {
874 case 1:
875 /* Remote1 controls PWM */
876 val = 0x00;
877 break;
878 case 2:
879 /* local controls PWM */
880 val = 0x01;
881 break;
882 case 4:
883 /* remote2 controls PWM */
884 val = 0x02;
885 break;
886 case 6:
887 /* local/remote2 control PWM */
888 val = 0x05;
889 break;
890 case 7:
891 /* All three control PWM */
892 val = 0x06;
893 break;
894 default:
895 return -EINVAL;
897 break;
898 default:
899 return -EINVAL;
902 data->pwmctl[index] = pwmctl;
903 data->pwmchan[index] = pwmchan;
905 data->pwm[CONTROL][index] &= ~0xE0;
906 data->pwm[CONTROL][index] |= (val & 7) << 5;
908 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
909 data->pwm[CONTROL][index]);
911 return 0;
914 static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
915 const char *buf, size_t count)
917 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
918 struct i2c_client *client = to_i2c_client(dev);
919 struct adt7475_data *data = i2c_get_clientdata(client);
920 int r;
921 long val;
923 if (kstrtol(buf, 10, &val))
924 return -EINVAL;
926 mutex_lock(&data->lock);
927 /* Read Modify Write PWM values */
928 adt7475_read_pwm(client, sattr->index);
929 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
930 if (r)
931 count = r;
932 mutex_unlock(&data->lock);
934 return count;
937 static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
938 const char *buf, size_t count)
940 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
941 struct i2c_client *client = to_i2c_client(dev);
942 struct adt7475_data *data = i2c_get_clientdata(client);
943 int r;
944 long val;
946 if (kstrtol(buf, 10, &val))
947 return -EINVAL;
949 mutex_lock(&data->lock);
950 /* Read Modify Write PWM values */
951 adt7475_read_pwm(client, sattr->index);
952 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
953 if (r)
954 count = r;
955 mutex_unlock(&data->lock);
957 return count;
960 /* List of frequencies for the PWM */
961 static const int pwmfreq_table[] = {
962 11, 14, 22, 29, 35, 44, 58, 88, 22500
965 static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
966 char *buf)
968 struct adt7475_data *data = adt7475_update_device(dev);
969 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
970 int idx;
972 if (IS_ERR(data))
973 return PTR_ERR(data);
974 idx = clamp_val(data->range[sattr->index] & 0xf, 0,
975 ARRAY_SIZE(pwmfreq_table) - 1);
977 return sprintf(buf, "%d\n", pwmfreq_table[idx]);
980 static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
981 const char *buf, size_t count)
983 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
984 struct i2c_client *client = to_i2c_client(dev);
985 struct adt7475_data *data = i2c_get_clientdata(client);
986 int out;
987 long val;
989 if (kstrtol(buf, 10, &val))
990 return -EINVAL;
992 out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
994 mutex_lock(&data->lock);
996 data->range[sattr->index] =
997 adt7475_read(TEMP_TRANGE_REG(sattr->index));
998 data->range[sattr->index] &= ~0xf;
999 data->range[sattr->index] |= out;
1001 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
1002 data->range[sattr->index]);
1004 mutex_unlock(&data->lock);
1005 return count;
1008 static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
1009 struct device_attribute *devattr,
1010 char *buf)
1012 struct adt7475_data *data = adt7475_update_device(dev);
1014 if (IS_ERR(data))
1015 return PTR_ERR(data);
1017 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1020 static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1021 struct device_attribute *devattr,
1022 const char *buf, size_t count)
1024 struct i2c_client *client = to_i2c_client(dev);
1025 struct adt7475_data *data = i2c_get_clientdata(client);
1026 long val;
1028 if (kstrtol(buf, 10, &val))
1029 return -EINVAL;
1030 if (val != 0 && val != 1)
1031 return -EINVAL;
1033 mutex_lock(&data->lock);
1034 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1035 if (val)
1036 data->config4 |= CONFIG4_MAXDUTY;
1037 else
1038 data->config4 &= ~CONFIG4_MAXDUTY;
1039 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1040 mutex_unlock(&data->lock);
1042 return count;
1045 static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
1046 char *buf)
1048 struct adt7475_data *data = dev_get_drvdata(dev);
1049 return sprintf(buf, "%d\n", (int)data->vrm);
1052 static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
1053 const char *buf, size_t count)
1055 struct adt7475_data *data = dev_get_drvdata(dev);
1056 long val;
1058 if (kstrtol(buf, 10, &val))
1059 return -EINVAL;
1060 if (val < 0 || val > 255)
1061 return -EINVAL;
1062 data->vrm = val;
1064 return count;
1067 static ssize_t cpu0_vid_show(struct device *dev,
1068 struct device_attribute *devattr, char *buf)
1070 struct adt7475_data *data = adt7475_update_device(dev);
1072 if (IS_ERR(data))
1073 return PTR_ERR(data);
1075 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1078 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
1079 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
1080 set_voltage, MAX, 0);
1081 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
1082 set_voltage, MIN, 0);
1083 static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
1084 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
1085 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
1086 set_voltage, MAX, 1);
1087 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
1088 set_voltage, MIN, 1);
1089 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
1090 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
1091 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
1092 set_voltage, MAX, 2);
1093 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
1094 set_voltage, MIN, 2);
1095 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
1096 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
1097 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
1098 set_voltage, MAX, 3);
1099 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
1100 set_voltage, MIN, 3);
1101 static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
1102 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
1103 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
1104 set_voltage, MAX, 4);
1105 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
1106 set_voltage, MIN, 4);
1107 static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
1108 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
1109 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
1110 set_voltage, MAX, 5);
1111 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
1112 set_voltage, MIN, 5);
1113 static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
1114 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
1115 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
1116 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
1117 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1118 MAX, 0);
1119 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1120 MIN, 0);
1121 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
1122 set_temp, OFFSET, 0);
1123 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
1124 show_temp, set_temp, AUTOMIN, 0);
1125 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
1126 show_point2, set_point2, 0, 0);
1127 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1128 THERM, 0);
1129 static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1130 set_temp, HYSTERSIS, 0);
1131 static SENSOR_DEVICE_ATTR_2(temp1_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
1132 set_temp_st, 0, 0);
1133 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
1134 static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
1135 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1136 MAX, 1);
1137 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1138 MIN, 1);
1139 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1140 set_temp, OFFSET, 1);
1141 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
1142 show_temp, set_temp, AUTOMIN, 1);
1143 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
1144 show_point2, set_point2, 0, 1);
1145 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1146 THERM, 1);
1147 static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1148 set_temp, HYSTERSIS, 1);
1149 static SENSOR_DEVICE_ATTR_2(temp2_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
1150 set_temp_st, 0, 1);
1151 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
1152 static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
1153 static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
1154 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1155 MAX, 2);
1156 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1157 MIN, 2);
1158 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1159 set_temp, OFFSET, 2);
1160 static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
1161 show_temp, set_temp, AUTOMIN, 2);
1162 static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
1163 show_point2, set_point2, 0, 2);
1164 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
1165 THERM, 2);
1166 static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
1167 set_temp, HYSTERSIS, 2);
1168 static SENSOR_DEVICE_ATTR_2(temp3_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
1169 set_temp_st, 0, 2);
1170 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
1171 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1172 MIN, 0);
1173 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
1174 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
1175 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1176 MIN, 1);
1177 static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
1178 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
1179 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1180 MIN, 2);
1181 static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
1182 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
1183 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
1184 MIN, 3);
1185 static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
1186 static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1188 static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1189 set_pwmfreq, INPUT, 0);
1190 static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1191 set_pwmctrl, INPUT, 0);
1192 static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1193 show_pwmchan, set_pwmchan, INPUT, 0);
1194 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1195 set_pwm, MIN, 0);
1196 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1197 set_pwm, MAX, 0);
1198 static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable, S_IRUGO | S_IWUSR,
1199 show_stall_disable, set_stall_disable, 0, 0);
1200 static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1202 static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1203 set_pwmfreq, INPUT, 1);
1204 static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1205 set_pwmctrl, INPUT, 1);
1206 static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1207 show_pwmchan, set_pwmchan, INPUT, 1);
1208 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1209 set_pwm, MIN, 1);
1210 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1211 set_pwm, MAX, 1);
1212 static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable, S_IRUGO | S_IWUSR,
1213 show_stall_disable, set_stall_disable, 0, 1);
1214 static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
1216 static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
1217 set_pwmfreq, INPUT, 2);
1218 static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
1219 set_pwmctrl, INPUT, 2);
1220 static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1221 show_pwmchan, set_pwmchan, INPUT, 2);
1222 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
1223 set_pwm, MIN, 2);
1224 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
1225 set_pwm, MAX, 2);
1226 static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable, S_IRUGO | S_IWUSR,
1227 show_stall_disable, set_stall_disable, 0, 2);
1229 /* Non-standard name, might need revisiting */
1230 static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1232 static DEVICE_ATTR_RW(vrm);
1233 static DEVICE_ATTR_RO(cpu0_vid);
1235 static struct attribute *adt7475_attrs[] = {
1236 &sensor_dev_attr_in1_input.dev_attr.attr,
1237 &sensor_dev_attr_in1_max.dev_attr.attr,
1238 &sensor_dev_attr_in1_min.dev_attr.attr,
1239 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1240 &sensor_dev_attr_in2_input.dev_attr.attr,
1241 &sensor_dev_attr_in2_max.dev_attr.attr,
1242 &sensor_dev_attr_in2_min.dev_attr.attr,
1243 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1244 &sensor_dev_attr_temp1_input.dev_attr.attr,
1245 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1246 &sensor_dev_attr_temp1_fault.dev_attr.attr,
1247 &sensor_dev_attr_temp1_max.dev_attr.attr,
1248 &sensor_dev_attr_temp1_min.dev_attr.attr,
1249 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1250 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1251 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1252 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1253 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1254 &sensor_dev_attr_temp1_smoothing.dev_attr.attr,
1255 &sensor_dev_attr_temp2_input.dev_attr.attr,
1256 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1257 &sensor_dev_attr_temp2_max.dev_attr.attr,
1258 &sensor_dev_attr_temp2_min.dev_attr.attr,
1259 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1260 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1261 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1262 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1263 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1264 &sensor_dev_attr_temp2_smoothing.dev_attr.attr,
1265 &sensor_dev_attr_temp3_input.dev_attr.attr,
1266 &sensor_dev_attr_temp3_fault.dev_attr.attr,
1267 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1268 &sensor_dev_attr_temp3_max.dev_attr.attr,
1269 &sensor_dev_attr_temp3_min.dev_attr.attr,
1270 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1271 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1272 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1273 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1274 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1275 &sensor_dev_attr_temp3_smoothing.dev_attr.attr,
1276 &sensor_dev_attr_fan1_input.dev_attr.attr,
1277 &sensor_dev_attr_fan1_min.dev_attr.attr,
1278 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1279 &sensor_dev_attr_fan2_input.dev_attr.attr,
1280 &sensor_dev_attr_fan2_min.dev_attr.attr,
1281 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1282 &sensor_dev_attr_fan3_input.dev_attr.attr,
1283 &sensor_dev_attr_fan3_min.dev_attr.attr,
1284 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1285 &sensor_dev_attr_pwm1.dev_attr.attr,
1286 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1287 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1288 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1289 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1290 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1291 &sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
1292 &sensor_dev_attr_pwm3.dev_attr.attr,
1293 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1294 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1295 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1296 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1297 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1298 &sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1299 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
1300 NULL,
1303 static struct attribute *fan4_attrs[] = {
1304 &sensor_dev_attr_fan4_input.dev_attr.attr,
1305 &sensor_dev_attr_fan4_min.dev_attr.attr,
1306 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1307 NULL
1310 static struct attribute *pwm2_attrs[] = {
1311 &sensor_dev_attr_pwm2.dev_attr.attr,
1312 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1313 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1314 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1315 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1316 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1317 &sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1318 NULL
1321 static struct attribute *in0_attrs[] = {
1322 &sensor_dev_attr_in0_input.dev_attr.attr,
1323 &sensor_dev_attr_in0_max.dev_attr.attr,
1324 &sensor_dev_attr_in0_min.dev_attr.attr,
1325 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1326 NULL
1329 static struct attribute *in3_attrs[] = {
1330 &sensor_dev_attr_in3_input.dev_attr.attr,
1331 &sensor_dev_attr_in3_max.dev_attr.attr,
1332 &sensor_dev_attr_in3_min.dev_attr.attr,
1333 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1334 NULL
1337 static struct attribute *in4_attrs[] = {
1338 &sensor_dev_attr_in4_input.dev_attr.attr,
1339 &sensor_dev_attr_in4_max.dev_attr.attr,
1340 &sensor_dev_attr_in4_min.dev_attr.attr,
1341 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1342 NULL
1345 static struct attribute *in5_attrs[] = {
1346 &sensor_dev_attr_in5_input.dev_attr.attr,
1347 &sensor_dev_attr_in5_max.dev_attr.attr,
1348 &sensor_dev_attr_in5_min.dev_attr.attr,
1349 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1350 NULL
1353 static struct attribute *vid_attrs[] = {
1354 &dev_attr_cpu0_vid.attr,
1355 &dev_attr_vrm.attr,
1356 NULL
1359 static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1360 static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1361 static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1362 static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1363 static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1364 static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1365 static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1366 static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
1368 static int adt7475_detect(struct i2c_client *client,
1369 struct i2c_board_info *info)
1371 struct i2c_adapter *adapter = client->adapter;
1372 int vendid, devid, devid2;
1373 const char *name;
1375 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1376 return -ENODEV;
1378 vendid = adt7475_read(REG_VENDID);
1379 devid2 = adt7475_read(REG_DEVID2);
1380 if (vendid != 0x41 || /* Analog Devices */
1381 (devid2 & 0xf8) != 0x68)
1382 return -ENODEV;
1384 devid = adt7475_read(REG_DEVID);
1385 if (devid == 0x73)
1386 name = "adt7473";
1387 else if (devid == 0x75 && client->addr == 0x2e)
1388 name = "adt7475";
1389 else if (devid == 0x76)
1390 name = "adt7476";
1391 else if ((devid2 & 0xfc) == 0x6c)
1392 name = "adt7490";
1393 else {
1394 dev_dbg(&adapter->dev,
1395 "Couldn't detect an ADT7473/75/76/90 part at "
1396 "0x%02x\n", (unsigned int)client->addr);
1397 return -ENODEV;
1400 strlcpy(info->type, name, I2C_NAME_SIZE);
1402 return 0;
1405 static void adt7475_remove_files(struct i2c_client *client,
1406 struct adt7475_data *data)
1408 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1409 if (data->has_fan4)
1410 sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1411 if (data->has_pwm2)
1412 sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1413 if (data->has_voltage & (1 << 0))
1414 sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1415 if (data->has_voltage & (1 << 3))
1416 sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1417 if (data->has_voltage & (1 << 4))
1418 sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1419 if (data->has_voltage & (1 << 5))
1420 sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
1421 if (data->has_vid)
1422 sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
1425 static int adt7475_update_limits(struct i2c_client *client)
1427 struct adt7475_data *data = i2c_get_clientdata(client);
1428 int i;
1429 int ret;
1431 ret = adt7475_read(REG_CONFIG4);
1432 if (ret < 0)
1433 return ret;
1434 data->config4 = ret;
1436 ret = adt7475_read(REG_CONFIG5);
1437 if (ret < 0)
1438 return ret;
1439 data->config5 = ret;
1441 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1442 if (!(data->has_voltage & (1 << i)))
1443 continue;
1444 /* Adjust values so they match the input precision */
1445 ret = adt7475_read(VOLTAGE_MIN_REG(i));
1446 if (ret < 0)
1447 return ret;
1448 data->voltage[MIN][i] = ret << 2;
1450 ret = adt7475_read(VOLTAGE_MAX_REG(i));
1451 if (ret < 0)
1452 return ret;
1453 data->voltage[MAX][i] = ret << 2;
1456 if (data->has_voltage & (1 << 5)) {
1457 ret = adt7475_read(REG_VTT_MIN);
1458 if (ret < 0)
1459 return ret;
1460 data->voltage[MIN][5] = ret << 2;
1462 ret = adt7475_read(REG_VTT_MAX);
1463 if (ret < 0)
1464 return ret;
1465 data->voltage[MAX][5] = ret << 2;
1468 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1469 /* Adjust values so they match the input precision */
1470 ret = adt7475_read(TEMP_MIN_REG(i));
1471 if (ret < 0)
1472 return ret;
1473 data->temp[MIN][i] = ret << 2;
1475 ret = adt7475_read(TEMP_MAX_REG(i));
1476 if (ret < 0)
1477 return ret;
1478 data->temp[MAX][i] = ret << 2;
1480 ret = adt7475_read(TEMP_TMIN_REG(i));
1481 if (ret < 0)
1482 return ret;
1483 data->temp[AUTOMIN][i] = ret << 2;
1485 ret = adt7475_read(TEMP_THERM_REG(i));
1486 if (ret < 0)
1487 return ret;
1488 data->temp[THERM][i] = ret << 2;
1490 ret = adt7475_read(TEMP_OFFSET_REG(i));
1491 if (ret < 0)
1492 return ret;
1493 data->temp[OFFSET][i] = ret;
1495 adt7475_read_hystersis(client);
1497 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1498 if (i == 3 && !data->has_fan4)
1499 continue;
1500 ret = adt7475_read_word(client, TACH_MIN_REG(i));
1501 if (ret < 0)
1502 return ret;
1503 data->tach[MIN][i] = ret;
1506 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1507 if (i == 1 && !data->has_pwm2)
1508 continue;
1509 ret = adt7475_read(PWM_MAX_REG(i));
1510 if (ret < 0)
1511 return ret;
1512 data->pwm[MAX][i] = ret;
1514 ret = adt7475_read(PWM_MIN_REG(i));
1515 if (ret < 0)
1516 return ret;
1517 data->pwm[MIN][i] = ret;
1518 /* Set the channel and control information */
1519 adt7475_read_pwm(client, i);
1522 ret = adt7475_read(TEMP_TRANGE_REG(0));
1523 if (ret < 0)
1524 return ret;
1525 data->range[0] = ret;
1527 ret = adt7475_read(TEMP_TRANGE_REG(1));
1528 if (ret < 0)
1529 return ret;
1530 data->range[1] = ret;
1532 ret = adt7475_read(TEMP_TRANGE_REG(2));
1533 if (ret < 0)
1534 return ret;
1535 data->range[2] = ret;
1537 return 0;
1540 static int adt7475_probe(struct i2c_client *client,
1541 const struct i2c_device_id *id)
1543 enum chips chip;
1544 static const char * const names[] = {
1545 [adt7473] = "ADT7473",
1546 [adt7475] = "ADT7475",
1547 [adt7476] = "ADT7476",
1548 [adt7490] = "ADT7490",
1551 struct adt7475_data *data;
1552 int i, ret = 0, revision;
1553 u8 config2, config3;
1555 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1556 if (data == NULL)
1557 return -ENOMEM;
1559 mutex_init(&data->lock);
1560 i2c_set_clientdata(client, data);
1562 if (client->dev.of_node)
1563 chip = (enum chips)of_device_get_match_data(&client->dev);
1564 else
1565 chip = id->driver_data;
1567 /* Initialize device-specific values */
1568 switch (chip) {
1569 case adt7476:
1570 data->has_voltage = 0x0e; /* in1 to in3 */
1571 revision = adt7475_read(REG_DEVID2) & 0x07;
1572 break;
1573 case adt7490:
1574 data->has_voltage = 0x3e; /* in1 to in5 */
1575 revision = adt7475_read(REG_DEVID2) & 0x03;
1576 if (revision == 0x03)
1577 revision += adt7475_read(REG_DEVREV2);
1578 break;
1579 default:
1580 data->has_voltage = 0x06; /* in1, in2 */
1581 revision = adt7475_read(REG_DEVID2) & 0x07;
1584 config3 = adt7475_read(REG_CONFIG3);
1585 /* Pin PWM2 may alternatively be used for ALERT output */
1586 if (!(config3 & CONFIG3_SMBALERT))
1587 data->has_pwm2 = 1;
1588 /* Meaning of this bit is inverted for the ADT7473-1 */
1589 if (id->driver_data == adt7473 && revision >= 1)
1590 data->has_pwm2 = !data->has_pwm2;
1592 data->config4 = adt7475_read(REG_CONFIG4);
1593 /* Pin TACH4 may alternatively be used for THERM */
1594 if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1595 data->has_fan4 = 1;
1598 * THERM configuration is more complex on the ADT7476 and ADT7490,
1599 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1600 * this function
1602 if (id->driver_data == adt7490) {
1603 if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1604 !(config3 & CONFIG3_THERM))
1605 data->has_fan4 = 1;
1607 if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1608 if (!(config3 & CONFIG3_THERM) ||
1609 (data->config4 & CONFIG4_PINFUNC) == 0x1)
1610 data->has_voltage |= (1 << 0); /* in0 */
1614 * On the ADT7476, the +12V input pin may instead be used as VID5,
1615 * and VID pins may alternatively be used as GPIO
1617 if (id->driver_data == adt7476) {
1618 u8 vid = adt7475_read(REG_VID);
1619 if (!(vid & VID_VIDSEL))
1620 data->has_voltage |= (1 << 4); /* in4 */
1622 data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1625 /* Voltage attenuators can be bypassed, globally or individually */
1626 config2 = adt7475_read(REG_CONFIG2);
1627 if (config2 & CONFIG2_ATTN) {
1628 data->bypass_attn = (0x3 << 3) | 0x3;
1629 } else {
1630 data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1631 ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1633 data->bypass_attn &= data->has_voltage;
1636 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1637 * pwm's which are disabled to manual mode with 0% duty cycle
1639 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1640 adt7475_read_pwm(client, i);
1642 /* Start monitoring */
1643 switch (chip) {
1644 case adt7475:
1645 case adt7476:
1646 i2c_smbus_write_byte_data(client, REG_CONFIG1,
1647 adt7475_read(REG_CONFIG1) | 0x01);
1648 break;
1649 default:
1650 break;
1653 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1654 if (ret)
1655 return ret;
1657 /* Features that can be disabled individually */
1658 if (data->has_fan4) {
1659 ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1660 if (ret)
1661 goto eremove;
1663 if (data->has_pwm2) {
1664 ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1665 if (ret)
1666 goto eremove;
1668 if (data->has_voltage & (1 << 0)) {
1669 ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1670 if (ret)
1671 goto eremove;
1673 if (data->has_voltage & (1 << 3)) {
1674 ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1675 if (ret)
1676 goto eremove;
1678 if (data->has_voltage & (1 << 4)) {
1679 ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1680 if (ret)
1681 goto eremove;
1683 if (data->has_voltage & (1 << 5)) {
1684 ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1685 if (ret)
1686 goto eremove;
1688 if (data->has_vid) {
1689 data->vrm = vid_which_vrm();
1690 ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1691 if (ret)
1692 goto eremove;
1695 data->hwmon_dev = hwmon_device_register(&client->dev);
1696 if (IS_ERR(data->hwmon_dev)) {
1697 ret = PTR_ERR(data->hwmon_dev);
1698 goto eremove;
1701 dev_info(&client->dev, "%s device, revision %d\n",
1702 names[id->driver_data], revision);
1703 if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
1704 dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1705 (data->has_voltage & (1 << 0)) ? " in0" : "",
1706 (data->has_voltage & (1 << 4)) ? " in4" : "",
1707 data->has_fan4 ? " fan4" : "",
1708 data->has_pwm2 ? " pwm2" : "",
1709 data->has_vid ? " vid" : "");
1710 if (data->bypass_attn)
1711 dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1712 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1713 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1714 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1715 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1717 /* Limits and settings, should never change update more than once */
1718 ret = adt7475_update_limits(client);
1719 if (ret)
1720 goto eremove;
1722 return 0;
1724 eremove:
1725 adt7475_remove_files(client, data);
1726 return ret;
1729 static int adt7475_remove(struct i2c_client *client)
1731 struct adt7475_data *data = i2c_get_clientdata(client);
1733 hwmon_device_unregister(data->hwmon_dev);
1734 adt7475_remove_files(client, data);
1736 return 0;
1739 static struct i2c_driver adt7475_driver = {
1740 .class = I2C_CLASS_HWMON,
1741 .driver = {
1742 .name = "adt7475",
1743 .of_match_table = of_match_ptr(adt7475_of_match),
1745 .probe = adt7475_probe,
1746 .remove = adt7475_remove,
1747 .id_table = adt7475_id,
1748 .detect = adt7475_detect,
1749 .address_list = normal_i2c,
1752 static void adt7475_read_hystersis(struct i2c_client *client)
1754 struct adt7475_data *data = i2c_get_clientdata(client);
1756 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1757 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1758 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1761 static void adt7475_read_pwm(struct i2c_client *client, int index)
1763 struct adt7475_data *data = i2c_get_clientdata(client);
1764 unsigned int v;
1766 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1769 * Figure out the internal value for pwmctrl and pwmchan
1770 * based on the current settings
1772 v = (data->pwm[CONTROL][index] >> 5) & 7;
1774 if (v == 3)
1775 data->pwmctl[index] = 0;
1776 else if (v == 7)
1777 data->pwmctl[index] = 1;
1778 else if (v == 4) {
1780 * The fan is disabled - we don't want to
1781 * support that, so change to manual mode and
1782 * set the duty cycle to 0 instead
1784 data->pwm[INPUT][index] = 0;
1785 data->pwm[CONTROL][index] &= ~0xE0;
1786 data->pwm[CONTROL][index] |= (7 << 5);
1788 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1789 data->pwm[INPUT][index]);
1791 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1792 data->pwm[CONTROL][index]);
1794 data->pwmctl[index] = 1;
1795 } else {
1796 data->pwmctl[index] = 2;
1798 switch (v) {
1799 case 0:
1800 data->pwmchan[index] = 1;
1801 break;
1802 case 1:
1803 data->pwmchan[index] = 2;
1804 break;
1805 case 2:
1806 data->pwmchan[index] = 4;
1807 break;
1808 case 5:
1809 data->pwmchan[index] = 6;
1810 break;
1811 case 6:
1812 data->pwmchan[index] = 7;
1813 break;
1818 static int adt7475_update_measure(struct device *dev)
1820 struct i2c_client *client = to_i2c_client(dev);
1821 struct adt7475_data *data = i2c_get_clientdata(client);
1822 u16 ext;
1823 int i;
1824 int ret;
1826 ret = adt7475_read(REG_STATUS2);
1827 if (ret < 0)
1828 return ret;
1829 data->alarms = ret << 8;
1831 ret = adt7475_read(REG_STATUS1);
1832 if (ret < 0)
1833 return ret;
1834 data->alarms |= ret;
1836 ret = adt7475_read(REG_EXTEND2);
1837 if (ret < 0)
1838 return ret;
1840 ext = (ret << 8);
1842 ret = adt7475_read(REG_EXTEND1);
1843 if (ret < 0)
1844 return ret;
1846 ext |= ret;
1848 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1849 if (!(data->has_voltage & (1 << i)))
1850 continue;
1851 ret = adt7475_read(VOLTAGE_REG(i));
1852 if (ret < 0)
1853 return ret;
1854 data->voltage[INPUT][i] =
1855 (ret << 2) |
1856 ((ext >> (i * 2)) & 3);
1859 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1860 ret = adt7475_read(TEMP_REG(i));
1861 if (ret < 0)
1862 return ret;
1863 data->temp[INPUT][i] =
1864 (ret << 2) |
1865 ((ext >> ((i + 5) * 2)) & 3);
1868 if (data->has_voltage & (1 << 5)) {
1869 ret = adt7475_read(REG_STATUS4);
1870 if (ret < 0)
1871 return ret;
1872 data->alarms |= ret << 24;
1874 ret = adt7475_read(REG_EXTEND3);
1875 if (ret < 0)
1876 return ret;
1877 ext = ret;
1879 ret = adt7475_read(REG_VTT);
1880 if (ret < 0)
1881 return ret;
1882 data->voltage[INPUT][5] = ret << 2 |
1883 ((ext >> 4) & 3);
1886 for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1887 if (i == 3 && !data->has_fan4)
1888 continue;
1889 ret = adt7475_read_word(client, TACH_REG(i));
1890 if (ret < 0)
1891 return ret;
1892 data->tach[INPUT][i] = ret;
1895 /* Updated by hw when in auto mode */
1896 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1897 if (i == 1 && !data->has_pwm2)
1898 continue;
1899 ret = adt7475_read(PWM_REG(i));
1900 if (ret < 0)
1901 return ret;
1902 data->pwm[INPUT][i] = ret;
1905 if (data->has_vid) {
1906 ret = adt7475_read(REG_VID);
1907 if (ret < 0)
1908 return ret;
1909 data->vid = ret & 0x3f;
1912 return 0;
1915 static struct adt7475_data *adt7475_update_device(struct device *dev)
1917 struct i2c_client *client = to_i2c_client(dev);
1918 struct adt7475_data *data = i2c_get_clientdata(client);
1919 int ret;
1921 mutex_lock(&data->lock);
1923 /* Measurement values update every 2 seconds */
1924 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1925 !data->valid) {
1926 ret = adt7475_update_measure(dev);
1927 if (ret) {
1928 data->valid = false;
1929 mutex_unlock(&data->lock);
1930 return ERR_PTR(ret);
1932 data->measure_updated = jiffies;
1933 data->valid = true;
1936 mutex_unlock(&data->lock);
1938 return data;
1941 module_i2c_driver(adt7475_driver);
1943 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1944 MODULE_DESCRIPTION("adt7475 driver");
1945 MODULE_LICENSE("GPL");