1 // SPDX-License-Identifier: GPL-2.0-only
3 * w83795.c - Linux kernel driver for hardware monitoring
4 * Copyright (C) 2008 Nuvoton Technology Corp.
6 * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
8 * Supports following chips:
10 * Chip #vin #fanin #pwm #temp #dts wchipid vendid i2c ISA
11 * w83795g 21 14 8 6 8 0x79 0x5ca3 yes no
12 * w83795adg 18 14 2 6 8 0x79 0x5ca3 yes no
15 #include <linux/kernel.h>
16 #include <linux/module.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/err.h>
23 #include <linux/mutex.h>
24 #include <linux/jiffies.h>
25 #include <linux/util_macros.h>
27 /* Addresses to scan */
28 static const unsigned short normal_i2c
[] = {
29 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
34 module_param(reset
, bool, 0);
35 MODULE_PARM_DESC(reset
, "Set to 1 to reset chip, not recommended");
38 #define W83795_REG_BANKSEL 0x00
39 #define W83795_REG_VENDORID 0xfd
40 #define W83795_REG_CHIPID 0xfe
41 #define W83795_REG_DEVICEID 0xfb
42 #define W83795_REG_DEVICEID_A 0xff
44 #define W83795_REG_I2C_ADDR 0xfc
45 #define W83795_REG_CONFIG 0x01
46 #define W83795_REG_CONFIG_CONFIG48 0x04
47 #define W83795_REG_CONFIG_START 0x01
49 /* Multi-Function Pin Ctrl Registers */
50 #define W83795_REG_VOLT_CTRL1 0x02
51 #define W83795_REG_VOLT_CTRL2 0x03
52 #define W83795_REG_TEMP_CTRL1 0x04
53 #define W83795_REG_TEMP_CTRL2 0x05
54 #define W83795_REG_FANIN_CTRL1 0x06
55 #define W83795_REG_FANIN_CTRL2 0x07
56 #define W83795_REG_VMIGB_CTRL 0x08
60 #define TEMP_CRIT_HYST 2
62 #define TEMP_WARN_HYST 4
64 * only crit and crit_hyst affect real-time alarm status
65 * current crit crit_hyst warn warn_hyst
67 static const u16 W83795_REG_TEMP
[][5] = {
68 {0x21, 0x96, 0x97, 0x98, 0x99}, /* TD1/TR1 */
69 {0x22, 0x9a, 0x9b, 0x9c, 0x9d}, /* TD2/TR2 */
70 {0x23, 0x9e, 0x9f, 0xa0, 0xa1}, /* TD3/TR3 */
71 {0x24, 0xa2, 0xa3, 0xa4, 0xa5}, /* TD4/TR4 */
72 {0x1f, 0xa6, 0xa7, 0xa8, 0xa9}, /* TR5 */
73 {0x20, 0xaa, 0xab, 0xac, 0xad}, /* TR6 */
79 static const u16 W83795_REG_IN
[][3] = {
81 {0x10, 0x70, 0x71}, /* VSEN1 */
82 {0x11, 0x72, 0x73}, /* VSEN2 */
83 {0x12, 0x74, 0x75}, /* VSEN3 */
84 {0x13, 0x76, 0x77}, /* VSEN4 */
85 {0x14, 0x78, 0x79}, /* VSEN5 */
86 {0x15, 0x7a, 0x7b}, /* VSEN6 */
87 {0x16, 0x7c, 0x7d}, /* VSEN7 */
88 {0x17, 0x7e, 0x7f}, /* VSEN8 */
89 {0x18, 0x80, 0x81}, /* VSEN9 */
90 {0x19, 0x82, 0x83}, /* VSEN10 */
91 {0x1A, 0x84, 0x85}, /* VSEN11 */
92 {0x1B, 0x86, 0x87}, /* VTT */
93 {0x1C, 0x88, 0x89}, /* 3VDD */
94 {0x1D, 0x8a, 0x8b}, /* 3VSB */
95 {0x1E, 0x8c, 0x8d}, /* VBAT */
96 {0x1F, 0xa6, 0xa7}, /* VSEN12 */
97 {0x20, 0xaa, 0xab}, /* VSEN13 */
98 {0x21, 0x96, 0x97}, /* VSEN14 */
99 {0x22, 0x9a, 0x9b}, /* VSEN15 */
100 {0x23, 0x9e, 0x9f}, /* VSEN16 */
101 {0x24, 0xa2, 0xa3}, /* VSEN17 */
103 #define W83795_REG_VRLSB 0x3C
105 static const u8 W83795_REG_IN_HL_LSB
[] = {
109 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
118 #define IN_LSB_REG(index, type) \
119 (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
120 : (W83795_REG_IN_HL_LSB[(index)] + 1))
122 #define IN_LSB_SHIFT 0
124 static const u8 IN_LSB_SHIFT_IDX
[][2] = {
125 /* High/Low LSB shift, LSB No. */
126 {0x00, 0x00}, /* VSEN1 */
127 {0x02, 0x00}, /* VSEN2 */
128 {0x04, 0x00}, /* VSEN3 */
129 {0x06, 0x00}, /* VSEN4 */
130 {0x00, 0x01}, /* VSEN5 */
131 {0x02, 0x01}, /* VSEN6 */
132 {0x04, 0x01}, /* VSEN7 */
133 {0x06, 0x01}, /* VSEN8 */
134 {0x00, 0x02}, /* VSEN9 */
135 {0x02, 0x02}, /* VSEN10 */
136 {0x04, 0x02}, /* VSEN11 */
137 {0x00, 0x03}, /* VTT */
138 {0x02, 0x03}, /* 3VDD */
139 {0x04, 0x03}, /* 3VSB */
140 {0x06, 0x03}, /* VBAT */
141 {0x06, 0x04}, /* VSEN12 */
142 {0x06, 0x05}, /* VSEN13 */
143 {0x06, 0x06}, /* VSEN14 */
144 {0x06, 0x07}, /* VSEN15 */
145 {0x06, 0x08}, /* VSEN16 */
146 {0x06, 0x09}, /* VSEN17 */
150 #define W83795_REG_FAN(index) (0x2E + (index))
151 #define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
152 #define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
153 #define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
154 (((index) & 1) ? 4 : 0)
156 #define W83795_REG_VID_CTRL 0x6A
158 #define W83795_REG_ALARM_CTRL 0x40
159 #define ALARM_CTRL_RTSACS (1 << 7)
160 #define W83795_REG_ALARM(index) (0x41 + (index))
161 #define W83795_REG_CLR_CHASSIS 0x4D
162 #define W83795_REG_BEEP(index) (0x50 + (index))
164 #define W83795_REG_OVT_CFG 0x58
165 #define OVT_CFG_SEL (1 << 7)
168 #define W83795_REG_FCMS1 0x201
169 #define W83795_REG_FCMS2 0x208
170 #define W83795_REG_TFMR(index) (0x202 + (index))
171 #define W83795_REG_FOMC 0x20F
173 #define W83795_REG_TSS(index) (0x209 + (index))
175 #define TSS_MAP_RESERVED 0xff
176 static const u8 tss_map
[4][6] = {
179 {10, 11, 12, 13, 2, 3},
180 { 4, 5, 4, 5, TSS_MAP_RESERVED
, TSS_MAP_RESERVED
},
186 #define PWM_NONSTOP 3
187 #define PWM_STOP_TIME 4
188 #define W83795_REG_PWM(index, nr) (0x210 + (nr) * 8 + (index))
190 #define W83795_REG_FTSH(index) (0x240 + (index) * 2)
191 #define W83795_REG_FTSL(index) (0x241 + (index) * 2)
192 #define W83795_REG_TFTS 0x250
194 #define TEMP_PWM_TTTI 0
195 #define TEMP_PWM_CTFS 1
196 #define TEMP_PWM_HCT 2
197 #define TEMP_PWM_HOT 3
198 #define W83795_REG_TTTI(index) (0x260 + (index))
199 #define W83795_REG_CTFS(index) (0x268 + (index))
200 #define W83795_REG_HT(index) (0x270 + (index))
204 #define W83795_REG_SF4_TEMP(temp_num, index) \
205 (0x280 + 0x10 * (temp_num) + (index))
206 #define W83795_REG_SF4_PWM(temp_num, index) \
207 (0x288 + 0x10 * (temp_num) + (index))
209 #define W83795_REG_DTSC 0x301
210 #define W83795_REG_DTSE 0x302
211 #define W83795_REG_DTS(index) (0x26 + (index))
212 #define W83795_REG_PECI_TBASE(index) (0x320 + (index))
215 #define DTS_CRIT_HYST 1
217 #define DTS_WARN_HYST 3
218 #define W83795_REG_DTS_EXT(index) (0xB2 + (index))
220 #define SETUP_PWM_DEFAULT 0
221 #define SETUP_PWM_UPTIME 1
222 #define SETUP_PWM_DOWNTIME 2
223 #define W83795_REG_SETUP_PWM(index) (0x20C + (index))
225 static inline u16
in_from_reg(u8 index
, u16 val
)
227 /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
228 if (index
>= 12 && index
<= 14)
234 static inline u16
in_to_reg(u8 index
, u16 val
)
236 if (index
>= 12 && index
<= 14)
242 static inline unsigned long fan_from_reg(u16 val
)
244 if ((val
== 0xfff) || (val
== 0))
246 return 1350000UL / val
;
249 static inline u16
fan_to_reg(long rpm
)
253 return clamp_val((1350000 + (rpm
>> 1)) / rpm
, 1, 0xffe);
256 static inline unsigned long time_from_reg(u8 reg
)
261 static inline u8
time_to_reg(unsigned long val
)
263 return clamp_val((val
+ 50) / 100, 0, 0xff);
266 static inline long temp_from_reg(s8 reg
)
271 static inline s8
temp_to_reg(long val
, s8 min
, s8 max
)
273 return clamp_val(val
/ 1000, min
, max
);
276 static const u16 pwm_freq_cksel0
[16] = {
277 1024, 512, 341, 256, 205, 171, 146, 128,
278 85, 64, 32, 16, 8, 4, 2, 1
281 static unsigned int pwm_freq_from_reg(u8 reg
, u16 clkin
)
283 unsigned long base_clock
;
286 base_clock
= clkin
* 1000 / ((clkin
== 48000) ? 384 : 256);
287 return base_clock
/ ((reg
& 0x7f) + 1);
289 return pwm_freq_cksel0
[reg
& 0x0f];
292 static u8
pwm_freq_to_reg(unsigned long val
, u16 clkin
)
294 unsigned long base_clock
;
296 unsigned long best0
, best1
;
298 /* Best fit for cksel = 0 */
299 reg0
= find_closest_descending(val
, pwm_freq_cksel0
,
300 ARRAY_SIZE(pwm_freq_cksel0
));
301 if (val
< 375) /* cksel = 1 can't beat this */
303 best0
= pwm_freq_cksel0
[reg0
];
305 /* Best fit for cksel = 1 */
306 base_clock
= clkin
* 1000 / ((clkin
== 48000) ? 384 : 256);
307 reg1
= clamp_val(DIV_ROUND_CLOSEST(base_clock
, val
), 1, 128);
308 best1
= base_clock
/ reg1
;
309 reg1
= 0x80 | (reg1
- 1);
311 /* Choose the closest one */
312 if (abs(val
- best0
) > abs(val
- best1
))
318 enum chip_types
{w83795g
, w83795adg
};
321 struct device
*hwmon_dev
;
322 struct mutex update_lock
;
323 unsigned long last_updated
; /* In jiffies */
324 enum chip_types chip_type
;
328 u32 has_in
; /* Enable monitor VIN or not */
329 u8 has_dyn_in
; /* Only in2-0 can have this */
330 u16 in
[21][3]; /* Register value, read/high/low */
331 u8 in_lsb
[10][3]; /* LSB Register value, high/low */
332 u8 has_gain
; /* has gain: in17-20 * 8 */
334 u16 has_fan
; /* Enable fan14-1 or not */
335 u16 fan
[14]; /* Register value combine */
336 u16 fan_min
[14]; /* Register value combine */
338 u8 has_temp
; /* Enable monitor temp6-1 or not */
339 s8 temp
[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
340 u8 temp_read_vrlsb
[6];
341 u8 temp_mode
; /* Bit vector, 0 = TR, 1 = TD */
342 u8 temp_src
[3]; /* Register value */
345 * Enable PECI and SB-TSI,
346 * bit 0: =1 enable, =0 disable,
347 * bit 1: =1 AMD SB-TSI, =0 Intel PECI
349 u8 has_dts
; /* Enable monitor DTS temp */
350 s8 dts
[8]; /* Register value */
351 u8 dts_read_vrlsb
[8]; /* Register value */
352 s8 dts_ext
[4]; /* Register value */
355 * 795g supports 8 pwm, 795adg only supports 2,
356 * no config register, only affected by chip
360 * Register value, output, freq, start,
361 * non stop, stop time
363 u16 clkin
; /* CLKIN frequency in kHz */
364 u8 pwm_fcms
[2]; /* Register value */
365 u8 pwm_tfmr
[6]; /* Register value */
366 u8 pwm_fomc
; /* Register value */
368 u16 target_speed
[8]; /*
369 * Register value, target speed for speed
372 u8 tol_speed
; /* tolerance of target speed */
373 u8 pwm_temp
[6][4]; /* TTTI, CTFS, HCT, HOT */
374 u8 sf4_reg
[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
376 u8 setup_pwm
[3]; /* Register value */
378 u8 alarms
[6]; /* Register value */
380 u8 beeps
[6]; /* Register value */
384 char valid_pwm_config
;
389 * We assume that nobdody can change the bank outside the driver.
392 /* Must be called with data->update_lock held, except during initialization */
393 static int w83795_set_bank(struct i2c_client
*client
, u8 bank
)
395 struct w83795_data
*data
= i2c_get_clientdata(client
);
398 /* If the same bank is already set, nothing to do */
399 if ((data
->bank
& 0x07) == bank
)
402 /* Change to new bank, preserve all other bits */
403 bank
|= data
->bank
& ~0x07;
404 err
= i2c_smbus_write_byte_data(client
, W83795_REG_BANKSEL
, bank
);
406 dev_err(&client
->dev
,
407 "Failed to set bank to %d, err %d\n",
416 /* Must be called with data->update_lock held, except during initialization */
417 static u8
w83795_read(struct i2c_client
*client
, u16 reg
)
421 err
= w83795_set_bank(client
, reg
>> 8);
423 return 0x00; /* Arbitrary */
425 err
= i2c_smbus_read_byte_data(client
, reg
& 0xff);
427 dev_err(&client
->dev
,
428 "Failed to read from register 0x%03x, err %d\n",
430 return 0x00; /* Arbitrary */
435 /* Must be called with data->update_lock held, except during initialization */
436 static int w83795_write(struct i2c_client
*client
, u16 reg
, u8 value
)
440 err
= w83795_set_bank(client
, reg
>> 8);
444 err
= i2c_smbus_write_byte_data(client
, reg
& 0xff, value
);
446 dev_err(&client
->dev
,
447 "Failed to write to register 0x%03x, err %d\n",
452 static void w83795_update_limits(struct i2c_client
*client
)
454 struct w83795_data
*data
= i2c_get_clientdata(client
);
458 /* Read the voltage limits */
459 for (i
= 0; i
< ARRAY_SIZE(data
->in
); i
++) {
460 if (!(data
->has_in
& (1 << i
)))
462 data
->in
[i
][IN_MAX
] =
463 w83795_read(client
, W83795_REG_IN
[i
][IN_MAX
]);
464 data
->in
[i
][IN_LOW
] =
465 w83795_read(client
, W83795_REG_IN
[i
][IN_LOW
]);
467 for (i
= 0; i
< ARRAY_SIZE(data
->in_lsb
); i
++) {
468 if ((i
== 2 && data
->chip_type
== w83795adg
) ||
469 (i
>= 4 && !(data
->has_in
& (1 << (i
+ 11)))))
471 data
->in_lsb
[i
][IN_MAX
] =
472 w83795_read(client
, IN_LSB_REG(i
, IN_MAX
));
473 data
->in_lsb
[i
][IN_LOW
] =
474 w83795_read(client
, IN_LSB_REG(i
, IN_LOW
));
477 /* Read the fan limits */
478 lsb
= 0; /* Silent false gcc warning */
479 for (i
= 0; i
< ARRAY_SIZE(data
->fan
); i
++) {
481 * Each register contains LSB for 2 fans, but we want to
482 * read it only once to save time
484 if ((i
& 1) == 0 && (data
->has_fan
& (3 << i
)))
485 lsb
= w83795_read(client
, W83795_REG_FAN_MIN_LSB(i
));
487 if (!(data
->has_fan
& (1 << i
)))
490 w83795_read(client
, W83795_REG_FAN_MIN_HL(i
)) << 4;
492 (lsb
>> W83795_REG_FAN_MIN_LSB_SHIFT(i
)) & 0x0F;
495 /* Read the temperature limits */
496 for (i
= 0; i
< ARRAY_SIZE(data
->temp
); i
++) {
497 if (!(data
->has_temp
& (1 << i
)))
499 for (limit
= TEMP_CRIT
; limit
<= TEMP_WARN_HYST
; limit
++)
500 data
->temp
[i
][limit
] =
501 w83795_read(client
, W83795_REG_TEMP
[i
][limit
]);
504 /* Read the DTS limits */
505 if (data
->enable_dts
) {
506 for (limit
= DTS_CRIT
; limit
<= DTS_WARN_HYST
; limit
++)
507 data
->dts_ext
[limit
] =
508 w83795_read(client
, W83795_REG_DTS_EXT(limit
));
511 /* Read beep settings */
512 if (data
->enable_beep
) {
513 for (i
= 0; i
< ARRAY_SIZE(data
->beeps
); i
++)
515 w83795_read(client
, W83795_REG_BEEP(i
));
518 data
->valid_limits
= 1;
521 static struct w83795_data
*w83795_update_pwm_config(struct device
*dev
)
523 struct i2c_client
*client
= to_i2c_client(dev
);
524 struct w83795_data
*data
= i2c_get_clientdata(client
);
527 mutex_lock(&data
->update_lock
);
529 if (data
->valid_pwm_config
)
532 /* Read temperature source selection */
533 for (i
= 0; i
< ARRAY_SIZE(data
->temp_src
); i
++)
534 data
->temp_src
[i
] = w83795_read(client
, W83795_REG_TSS(i
));
536 /* Read automatic fan speed control settings */
537 data
->pwm_fcms
[0] = w83795_read(client
, W83795_REG_FCMS1
);
538 data
->pwm_fcms
[1] = w83795_read(client
, W83795_REG_FCMS2
);
539 for (i
= 0; i
< ARRAY_SIZE(data
->pwm_tfmr
); i
++)
540 data
->pwm_tfmr
[i
] = w83795_read(client
, W83795_REG_TFMR(i
));
541 data
->pwm_fomc
= w83795_read(client
, W83795_REG_FOMC
);
542 for (i
= 0; i
< data
->has_pwm
; i
++) {
543 for (tmp
= PWM_FREQ
; tmp
<= PWM_STOP_TIME
; tmp
++)
545 w83795_read(client
, W83795_REG_PWM(i
, tmp
));
547 for (i
= 0; i
< ARRAY_SIZE(data
->target_speed
); i
++) {
548 data
->target_speed
[i
] =
549 w83795_read(client
, W83795_REG_FTSH(i
)) << 4;
550 data
->target_speed
[i
] |=
551 w83795_read(client
, W83795_REG_FTSL(i
)) >> 4;
553 data
->tol_speed
= w83795_read(client
, W83795_REG_TFTS
) & 0x3f;
555 for (i
= 0; i
< ARRAY_SIZE(data
->pwm_temp
); i
++) {
556 data
->pwm_temp
[i
][TEMP_PWM_TTTI
] =
557 w83795_read(client
, W83795_REG_TTTI(i
)) & 0x7f;
558 data
->pwm_temp
[i
][TEMP_PWM_CTFS
] =
559 w83795_read(client
, W83795_REG_CTFS(i
));
560 tmp
= w83795_read(client
, W83795_REG_HT(i
));
561 data
->pwm_temp
[i
][TEMP_PWM_HCT
] = tmp
>> 4;
562 data
->pwm_temp
[i
][TEMP_PWM_HOT
] = tmp
& 0x0f;
565 /* Read SmartFanIV trip points */
566 for (i
= 0; i
< ARRAY_SIZE(data
->sf4_reg
); i
++) {
567 for (tmp
= 0; tmp
< 7; tmp
++) {
568 data
->sf4_reg
[i
][SF4_TEMP
][tmp
] =
570 W83795_REG_SF4_TEMP(i
, tmp
));
571 data
->sf4_reg
[i
][SF4_PWM
][tmp
] =
572 w83795_read(client
, W83795_REG_SF4_PWM(i
, tmp
));
577 for (i
= 0; i
< ARRAY_SIZE(data
->setup_pwm
); i
++)
579 w83795_read(client
, W83795_REG_SETUP_PWM(i
));
581 data
->valid_pwm_config
= 1;
584 mutex_unlock(&data
->update_lock
);
588 static struct w83795_data
*w83795_update_device(struct device
*dev
)
590 struct i2c_client
*client
= to_i2c_client(dev
);
591 struct w83795_data
*data
= i2c_get_clientdata(client
);
596 mutex_lock(&data
->update_lock
);
598 if (!data
->valid_limits
)
599 w83795_update_limits(client
);
601 if (!(time_after(jiffies
, data
->last_updated
+ HZ
* 2)
605 /* Update the voltages value */
606 for (i
= 0; i
< ARRAY_SIZE(data
->in
); i
++) {
607 if (!(data
->has_in
& (1 << i
)))
609 tmp
= w83795_read(client
, W83795_REG_IN
[i
][IN_READ
]) << 2;
610 tmp
|= w83795_read(client
, W83795_REG_VRLSB
) >> 6;
611 data
->in
[i
][IN_READ
] = tmp
;
614 /* in0-2 can have dynamic limits (W83795G only) */
615 if (data
->has_dyn_in
) {
616 u8 lsb_max
= w83795_read(client
, IN_LSB_REG(0, IN_MAX
));
617 u8 lsb_low
= w83795_read(client
, IN_LSB_REG(0, IN_LOW
));
619 for (i
= 0; i
< 3; i
++) {
620 if (!(data
->has_dyn_in
& (1 << i
)))
622 data
->in
[i
][IN_MAX
] =
623 w83795_read(client
, W83795_REG_IN
[i
][IN_MAX
]);
624 data
->in
[i
][IN_LOW
] =
625 w83795_read(client
, W83795_REG_IN
[i
][IN_LOW
]);
626 data
->in_lsb
[i
][IN_MAX
] = (lsb_max
>> (2 * i
)) & 0x03;
627 data
->in_lsb
[i
][IN_LOW
] = (lsb_low
>> (2 * i
)) & 0x03;
632 for (i
= 0; i
< ARRAY_SIZE(data
->fan
); i
++) {
633 if (!(data
->has_fan
& (1 << i
)))
635 data
->fan
[i
] = w83795_read(client
, W83795_REG_FAN(i
)) << 4;
636 data
->fan
[i
] |= w83795_read(client
, W83795_REG_VRLSB
) >> 4;
639 /* Update temperature */
640 for (i
= 0; i
< ARRAY_SIZE(data
->temp
); i
++) {
641 data
->temp
[i
][TEMP_READ
] =
642 w83795_read(client
, W83795_REG_TEMP
[i
][TEMP_READ
]);
643 data
->temp_read_vrlsb
[i
] =
644 w83795_read(client
, W83795_REG_VRLSB
);
647 /* Update dts temperature */
648 if (data
->enable_dts
) {
649 for (i
= 0; i
< ARRAY_SIZE(data
->dts
); i
++) {
650 if (!(data
->has_dts
& (1 << i
)))
653 w83795_read(client
, W83795_REG_DTS(i
));
654 data
->dts_read_vrlsb
[i
] =
655 w83795_read(client
, W83795_REG_VRLSB
);
659 /* Update pwm output */
660 for (i
= 0; i
< data
->has_pwm
; i
++) {
661 data
->pwm
[i
][PWM_OUTPUT
] =
662 w83795_read(client
, W83795_REG_PWM(i
, PWM_OUTPUT
));
666 * Update intrusion and alarms
667 * It is important to read intrusion first, because reading from
668 * register SMI STS6 clears the interrupt status temporarily.
670 tmp
= w83795_read(client
, W83795_REG_ALARM_CTRL
);
671 /* Switch to interrupt status for intrusion if needed */
672 if (tmp
& ALARM_CTRL_RTSACS
)
673 w83795_write(client
, W83795_REG_ALARM_CTRL
,
674 tmp
& ~ALARM_CTRL_RTSACS
);
675 intrusion
= w83795_read(client
, W83795_REG_ALARM(5)) & (1 << 6);
676 /* Switch to real-time alarms */
677 w83795_write(client
, W83795_REG_ALARM_CTRL
, tmp
| ALARM_CTRL_RTSACS
);
678 for (i
= 0; i
< ARRAY_SIZE(data
->alarms
); i
++)
679 data
->alarms
[i
] = w83795_read(client
, W83795_REG_ALARM(i
));
680 data
->alarms
[5] |= intrusion
;
681 /* Restore original configuration if needed */
682 if (!(tmp
& ALARM_CTRL_RTSACS
))
683 w83795_write(client
, W83795_REG_ALARM_CTRL
,
684 tmp
& ~ALARM_CTRL_RTSACS
);
686 data
->last_updated
= jiffies
;
690 mutex_unlock(&data
->update_lock
);
698 #define ALARM_STATUS 0
699 #define BEEP_ENABLE 1
701 show_alarm_beep(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
703 struct w83795_data
*data
= w83795_update_device(dev
);
704 struct sensor_device_attribute_2
*sensor_attr
=
705 to_sensor_dev_attr_2(attr
);
706 int nr
= sensor_attr
->nr
;
707 int index
= sensor_attr
->index
>> 3;
708 int bit
= sensor_attr
->index
& 0x07;
711 if (nr
== ALARM_STATUS
)
712 val
= (data
->alarms
[index
] >> bit
) & 1;
713 else /* BEEP_ENABLE */
714 val
= (data
->beeps
[index
] >> bit
) & 1;
716 return sprintf(buf
, "%u\n", val
);
720 store_beep(struct device
*dev
, struct device_attribute
*attr
,
721 const char *buf
, size_t count
)
723 struct i2c_client
*client
= to_i2c_client(dev
);
724 struct w83795_data
*data
= i2c_get_clientdata(client
);
725 struct sensor_device_attribute_2
*sensor_attr
=
726 to_sensor_dev_attr_2(attr
);
727 int index
= sensor_attr
->index
>> 3;
728 int shift
= sensor_attr
->index
& 0x07;
729 u8 beep_bit
= 1 << shift
;
732 if (kstrtoul(buf
, 10, &val
) < 0)
734 if (val
!= 0 && val
!= 1)
737 mutex_lock(&data
->update_lock
);
738 data
->beeps
[index
] = w83795_read(client
, W83795_REG_BEEP(index
));
739 data
->beeps
[index
] &= ~beep_bit
;
740 data
->beeps
[index
] |= val
<< shift
;
741 w83795_write(client
, W83795_REG_BEEP(index
), data
->beeps
[index
]);
742 mutex_unlock(&data
->update_lock
);
747 /* Write 0 to clear chassis alarm */
749 store_chassis_clear(struct device
*dev
,
750 struct device_attribute
*attr
, const char *buf
,
753 struct i2c_client
*client
= to_i2c_client(dev
);
754 struct w83795_data
*data
= i2c_get_clientdata(client
);
757 if (kstrtoul(buf
, 10, &val
) < 0 || val
!= 0)
760 mutex_lock(&data
->update_lock
);
761 val
= w83795_read(client
, W83795_REG_CLR_CHASSIS
);
763 w83795_write(client
, W83795_REG_CLR_CHASSIS
, val
);
765 /* Clear status and force cache refresh */
766 w83795_read(client
, W83795_REG_ALARM(5));
768 mutex_unlock(&data
->update_lock
);
775 show_fan(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
777 struct sensor_device_attribute_2
*sensor_attr
=
778 to_sensor_dev_attr_2(attr
);
779 int nr
= sensor_attr
->nr
;
780 int index
= sensor_attr
->index
;
781 struct w83795_data
*data
= w83795_update_device(dev
);
785 val
= data
->fan
[index
] & 0x0fff;
787 val
= data
->fan_min
[index
] & 0x0fff;
789 return sprintf(buf
, "%lu\n", fan_from_reg(val
));
793 store_fan_min(struct device
*dev
, struct device_attribute
*attr
,
794 const char *buf
, size_t count
)
796 struct sensor_device_attribute_2
*sensor_attr
=
797 to_sensor_dev_attr_2(attr
);
798 int index
= sensor_attr
->index
;
799 struct i2c_client
*client
= to_i2c_client(dev
);
800 struct w83795_data
*data
= i2c_get_clientdata(client
);
803 if (kstrtoul(buf
, 10, &val
))
805 val
= fan_to_reg(val
);
807 mutex_lock(&data
->update_lock
);
808 data
->fan_min
[index
] = val
;
809 w83795_write(client
, W83795_REG_FAN_MIN_HL(index
), (val
>> 4) & 0xff);
813 val
|= w83795_read(client
, W83795_REG_FAN_MIN_LSB(index
))
816 val
|= w83795_read(client
, W83795_REG_FAN_MIN_LSB(index
))
819 w83795_write(client
, W83795_REG_FAN_MIN_LSB(index
), val
& 0xff);
820 mutex_unlock(&data
->update_lock
);
826 show_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
828 struct w83795_data
*data
;
829 struct sensor_device_attribute_2
*sensor_attr
=
830 to_sensor_dev_attr_2(attr
);
831 int nr
= sensor_attr
->nr
;
832 int index
= sensor_attr
->index
;
835 data
= nr
== PWM_OUTPUT
? w83795_update_device(dev
)
836 : w83795_update_pwm_config(dev
);
840 val
= time_from_reg(data
->pwm
[index
][nr
]);
843 val
= pwm_freq_from_reg(data
->pwm
[index
][nr
], data
->clkin
);
846 val
= data
->pwm
[index
][nr
];
850 return sprintf(buf
, "%u\n", val
);
854 store_pwm(struct device
*dev
, struct device_attribute
*attr
,
855 const char *buf
, size_t count
)
857 struct i2c_client
*client
= to_i2c_client(dev
);
858 struct w83795_data
*data
= i2c_get_clientdata(client
);
859 struct sensor_device_attribute_2
*sensor_attr
=
860 to_sensor_dev_attr_2(attr
);
861 int nr
= sensor_attr
->nr
;
862 int index
= sensor_attr
->index
;
865 if (kstrtoul(buf
, 10, &val
) < 0)
868 mutex_lock(&data
->update_lock
);
871 val
= time_to_reg(val
);
874 val
= pwm_freq_to_reg(val
, data
->clkin
);
877 val
= clamp_val(val
, 0, 0xff);
880 w83795_write(client
, W83795_REG_PWM(index
, nr
), val
);
881 data
->pwm
[index
][nr
] = val
;
882 mutex_unlock(&data
->update_lock
);
887 show_pwm_enable(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
889 struct sensor_device_attribute_2
*sensor_attr
=
890 to_sensor_dev_attr_2(attr
);
891 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
892 int index
= sensor_attr
->index
;
895 /* Speed cruise mode */
896 if (data
->pwm_fcms
[0] & (1 << index
)) {
900 /* Thermal cruise or SmartFan IV mode */
901 for (tmp
= 0; tmp
< 6; tmp
++) {
902 if (data
->pwm_tfmr
[tmp
] & (1 << index
)) {
911 return sprintf(buf
, "%u\n", tmp
);
915 store_pwm_enable(struct device
*dev
, struct device_attribute
*attr
,
916 const char *buf
, size_t count
)
918 struct i2c_client
*client
= to_i2c_client(dev
);
919 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
920 struct sensor_device_attribute_2
*sensor_attr
=
921 to_sensor_dev_attr_2(attr
);
922 int index
= sensor_attr
->index
;
926 if (kstrtoul(buf
, 10, &val
) < 0)
928 if (val
< 1 || val
> 2)
931 #ifndef CONFIG_SENSORS_W83795_FANCTRL
933 dev_warn(dev
, "Automatic fan speed control support disabled\n");
934 dev_warn(dev
, "Build with CONFIG_SENSORS_W83795_FANCTRL=y if you want it\n");
939 mutex_lock(&data
->update_lock
);
942 /* Clear speed cruise mode bits */
943 data
->pwm_fcms
[0] &= ~(1 << index
);
944 w83795_write(client
, W83795_REG_FCMS1
, data
->pwm_fcms
[0]);
945 /* Clear thermal cruise mode bits */
946 for (i
= 0; i
< 6; i
++) {
947 data
->pwm_tfmr
[i
] &= ~(1 << index
);
948 w83795_write(client
, W83795_REG_TFMR(i
),
953 data
->pwm_fcms
[0] |= (1 << index
);
954 w83795_write(client
, W83795_REG_FCMS1
, data
->pwm_fcms
[0]);
957 mutex_unlock(&data
->update_lock
);
962 show_pwm_mode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
964 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
965 int index
= to_sensor_dev_attr_2(attr
)->index
;
968 if (data
->pwm_fomc
& (1 << index
))
973 return sprintf(buf
, "%u\n", mode
);
977 * Check whether a given temperature source can ever be useful.
978 * Returns the number of selectable temperature channels which are
981 static int w83795_tss_useful(const struct w83795_data
*data
, int tsrc
)
985 for (i
= 0; i
< 4; i
++) {
986 if (tss_map
[i
][tsrc
] == TSS_MAP_RESERVED
)
988 if (tss_map
[i
][tsrc
] < 6) /* Analog */
989 useful
+= (data
->has_temp
>> tss_map
[i
][tsrc
]) & 1;
991 useful
+= (data
->has_dts
>> (tss_map
[i
][tsrc
] - 6)) & 1;
998 show_temp_src(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1000 struct sensor_device_attribute_2
*sensor_attr
=
1001 to_sensor_dev_attr_2(attr
);
1002 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1003 int index
= sensor_attr
->index
;
1004 u8 tmp
= data
->temp_src
[index
/ 2];
1007 tmp
>>= 4; /* Pick high nibble */
1009 tmp
&= 0x0f; /* Pick low nibble */
1011 /* Look-up the actual temperature channel number */
1012 if (tmp
>= 4 || tss_map
[tmp
][index
] == TSS_MAP_RESERVED
)
1013 return -EINVAL
; /* Shouldn't happen */
1015 return sprintf(buf
, "%u\n", (unsigned int)tss_map
[tmp
][index
] + 1);
1019 store_temp_src(struct device
*dev
, struct device_attribute
*attr
,
1020 const char *buf
, size_t count
)
1022 struct i2c_client
*client
= to_i2c_client(dev
);
1023 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1024 struct sensor_device_attribute_2
*sensor_attr
=
1025 to_sensor_dev_attr_2(attr
);
1026 int index
= sensor_attr
->index
;
1028 unsigned long channel
;
1031 if (kstrtoul(buf
, 10, &channel
) < 0 ||
1032 channel
< 1 || channel
> 14)
1035 /* Check if request can be fulfilled */
1036 for (tmp
= 0; tmp
< 4; tmp
++) {
1037 if (tss_map
[tmp
][index
] == channel
- 1)
1040 if (tmp
== 4) /* No match */
1043 mutex_lock(&data
->update_lock
);
1046 data
->temp_src
[val
] &= 0x0f;
1048 data
->temp_src
[val
] &= 0xf0;
1050 data
->temp_src
[val
] |= tmp
;
1051 w83795_write(client
, W83795_REG_TSS(val
), data
->temp_src
[val
]);
1052 mutex_unlock(&data
->update_lock
);
1057 #define TEMP_PWM_ENABLE 0
1058 #define TEMP_PWM_FAN_MAP 1
1060 show_temp_pwm_enable(struct device
*dev
, struct device_attribute
*attr
,
1063 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1064 struct sensor_device_attribute_2
*sensor_attr
=
1065 to_sensor_dev_attr_2(attr
);
1066 int nr
= sensor_attr
->nr
;
1067 int index
= sensor_attr
->index
;
1071 case TEMP_PWM_ENABLE
:
1072 tmp
= (data
->pwm_fcms
[1] >> index
) & 1;
1078 case TEMP_PWM_FAN_MAP
:
1079 tmp
= data
->pwm_tfmr
[index
];
1083 return sprintf(buf
, "%u\n", tmp
);
1087 store_temp_pwm_enable(struct device
*dev
, struct device_attribute
*attr
,
1088 const char *buf
, size_t count
)
1090 struct i2c_client
*client
= to_i2c_client(dev
);
1091 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1092 struct sensor_device_attribute_2
*sensor_attr
=
1093 to_sensor_dev_attr_2(attr
);
1094 int nr
= sensor_attr
->nr
;
1095 int index
= sensor_attr
->index
;
1098 if (kstrtoul(buf
, 10, &tmp
) < 0)
1102 case TEMP_PWM_ENABLE
:
1103 if (tmp
!= 3 && tmp
!= 4)
1106 mutex_lock(&data
->update_lock
);
1107 data
->pwm_fcms
[1] &= ~(1 << index
);
1108 data
->pwm_fcms
[1] |= tmp
<< index
;
1109 w83795_write(client
, W83795_REG_FCMS2
, data
->pwm_fcms
[1]);
1110 mutex_unlock(&data
->update_lock
);
1112 case TEMP_PWM_FAN_MAP
:
1113 mutex_lock(&data
->update_lock
);
1114 tmp
= clamp_val(tmp
, 0, 0xff);
1115 w83795_write(client
, W83795_REG_TFMR(index
), tmp
);
1116 data
->pwm_tfmr
[index
] = tmp
;
1117 mutex_unlock(&data
->update_lock
);
1123 #define FANIN_TARGET 0
1126 show_fanin(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1128 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1129 struct sensor_device_attribute_2
*sensor_attr
=
1130 to_sensor_dev_attr_2(attr
);
1131 int nr
= sensor_attr
->nr
;
1132 int index
= sensor_attr
->index
;
1137 tmp
= fan_from_reg(data
->target_speed
[index
]);
1140 tmp
= data
->tol_speed
;
1144 return sprintf(buf
, "%u\n", tmp
);
1148 store_fanin(struct device
*dev
, struct device_attribute
*attr
,
1149 const char *buf
, size_t count
)
1151 struct i2c_client
*client
= to_i2c_client(dev
);
1152 struct w83795_data
*data
= i2c_get_clientdata(client
);
1153 struct sensor_device_attribute_2
*sensor_attr
=
1154 to_sensor_dev_attr_2(attr
);
1155 int nr
= sensor_attr
->nr
;
1156 int index
= sensor_attr
->index
;
1159 if (kstrtoul(buf
, 10, &val
) < 0)
1162 mutex_lock(&data
->update_lock
);
1165 val
= fan_to_reg(clamp_val(val
, 0, 0xfff));
1166 w83795_write(client
, W83795_REG_FTSH(index
), val
>> 4);
1167 w83795_write(client
, W83795_REG_FTSL(index
), (val
<< 4) & 0xf0);
1168 data
->target_speed
[index
] = val
;
1171 val
= clamp_val(val
, 0, 0x3f);
1172 w83795_write(client
, W83795_REG_TFTS
, val
);
1173 data
->tol_speed
= val
;
1176 mutex_unlock(&data
->update_lock
);
1183 show_temp_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1185 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1186 struct sensor_device_attribute_2
*sensor_attr
=
1187 to_sensor_dev_attr_2(attr
);
1188 int nr
= sensor_attr
->nr
;
1189 int index
= sensor_attr
->index
;
1190 long tmp
= temp_from_reg(data
->pwm_temp
[index
][nr
]);
1192 return sprintf(buf
, "%ld\n", tmp
);
1196 store_temp_pwm(struct device
*dev
, struct device_attribute
*attr
,
1197 const char *buf
, size_t count
)
1199 struct i2c_client
*client
= to_i2c_client(dev
);
1200 struct w83795_data
*data
= i2c_get_clientdata(client
);
1201 struct sensor_device_attribute_2
*sensor_attr
=
1202 to_sensor_dev_attr_2(attr
);
1203 int nr
= sensor_attr
->nr
;
1204 int index
= sensor_attr
->index
;
1208 if (kstrtoul(buf
, 10, &val
) < 0)
1212 mutex_lock(&data
->update_lock
);
1215 val
= clamp_val(val
, 0, 0x7f);
1216 w83795_write(client
, W83795_REG_TTTI(index
), val
);
1219 val
= clamp_val(val
, 0, 0x7f);
1220 w83795_write(client
, W83795_REG_CTFS(index
), val
);
1223 val
= clamp_val(val
, 0, 0x0f);
1224 tmp
= w83795_read(client
, W83795_REG_HT(index
));
1226 tmp
|= (val
<< 4) & 0xf0;
1227 w83795_write(client
, W83795_REG_HT(index
), tmp
);
1230 val
= clamp_val(val
, 0, 0x0f);
1231 tmp
= w83795_read(client
, W83795_REG_HT(index
));
1234 w83795_write(client
, W83795_REG_HT(index
), tmp
);
1237 data
->pwm_temp
[index
][nr
] = val
;
1238 mutex_unlock(&data
->update_lock
);
1244 show_sf4_pwm(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1246 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1247 struct sensor_device_attribute_2
*sensor_attr
=
1248 to_sensor_dev_attr_2(attr
);
1249 int nr
= sensor_attr
->nr
;
1250 int index
= sensor_attr
->index
;
1252 return sprintf(buf
, "%u\n", data
->sf4_reg
[index
][SF4_PWM
][nr
]);
1256 store_sf4_pwm(struct device
*dev
, struct device_attribute
*attr
,
1257 const char *buf
, size_t count
)
1259 struct i2c_client
*client
= to_i2c_client(dev
);
1260 struct w83795_data
*data
= i2c_get_clientdata(client
);
1261 struct sensor_device_attribute_2
*sensor_attr
=
1262 to_sensor_dev_attr_2(attr
);
1263 int nr
= sensor_attr
->nr
;
1264 int index
= sensor_attr
->index
;
1267 if (kstrtoul(buf
, 10, &val
) < 0)
1270 mutex_lock(&data
->update_lock
);
1271 w83795_write(client
, W83795_REG_SF4_PWM(index
, nr
), val
);
1272 data
->sf4_reg
[index
][SF4_PWM
][nr
] = val
;
1273 mutex_unlock(&data
->update_lock
);
1279 show_sf4_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1281 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1282 struct sensor_device_attribute_2
*sensor_attr
=
1283 to_sensor_dev_attr_2(attr
);
1284 int nr
= sensor_attr
->nr
;
1285 int index
= sensor_attr
->index
;
1287 return sprintf(buf
, "%u\n",
1288 (data
->sf4_reg
[index
][SF4_TEMP
][nr
]) * 1000);
1292 store_sf4_temp(struct device
*dev
, struct device_attribute
*attr
,
1293 const char *buf
, size_t count
)
1295 struct i2c_client
*client
= to_i2c_client(dev
);
1296 struct w83795_data
*data
= i2c_get_clientdata(client
);
1297 struct sensor_device_attribute_2
*sensor_attr
=
1298 to_sensor_dev_attr_2(attr
);
1299 int nr
= sensor_attr
->nr
;
1300 int index
= sensor_attr
->index
;
1303 if (kstrtoul(buf
, 10, &val
) < 0)
1307 mutex_lock(&data
->update_lock
);
1308 w83795_write(client
, W83795_REG_SF4_TEMP(index
, nr
), val
);
1309 data
->sf4_reg
[index
][SF4_TEMP
][nr
] = val
;
1310 mutex_unlock(&data
->update_lock
);
1317 show_temp(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1319 struct sensor_device_attribute_2
*sensor_attr
=
1320 to_sensor_dev_attr_2(attr
);
1321 int nr
= sensor_attr
->nr
;
1322 int index
= sensor_attr
->index
;
1323 struct w83795_data
*data
= w83795_update_device(dev
);
1324 long temp
= temp_from_reg(data
->temp
[index
][nr
]);
1326 if (nr
== TEMP_READ
)
1327 temp
+= (data
->temp_read_vrlsb
[index
] >> 6) * 250;
1328 return sprintf(buf
, "%ld\n", temp
);
1332 store_temp(struct device
*dev
, struct device_attribute
*attr
,
1333 const char *buf
, size_t count
)
1335 struct sensor_device_attribute_2
*sensor_attr
=
1336 to_sensor_dev_attr_2(attr
);
1337 int nr
= sensor_attr
->nr
;
1338 int index
= sensor_attr
->index
;
1339 struct i2c_client
*client
= to_i2c_client(dev
);
1340 struct w83795_data
*data
= i2c_get_clientdata(client
);
1343 if (kstrtol(buf
, 10, &tmp
) < 0)
1346 mutex_lock(&data
->update_lock
);
1347 data
->temp
[index
][nr
] = temp_to_reg(tmp
, -128, 127);
1348 w83795_write(client
, W83795_REG_TEMP
[index
][nr
], data
->temp
[index
][nr
]);
1349 mutex_unlock(&data
->update_lock
);
1355 show_dts_mode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1357 struct w83795_data
*data
= dev_get_drvdata(dev
);
1360 if (data
->enable_dts
& 2)
1365 return sprintf(buf
, "%d\n", tmp
);
1369 show_dts(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1371 struct sensor_device_attribute_2
*sensor_attr
=
1372 to_sensor_dev_attr_2(attr
);
1373 int index
= sensor_attr
->index
;
1374 struct w83795_data
*data
= w83795_update_device(dev
);
1375 long temp
= temp_from_reg(data
->dts
[index
]);
1377 temp
+= (data
->dts_read_vrlsb
[index
] >> 6) * 250;
1378 return sprintf(buf
, "%ld\n", temp
);
1382 show_dts_ext(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1384 struct sensor_device_attribute_2
*sensor_attr
=
1385 to_sensor_dev_attr_2(attr
);
1386 int nr
= sensor_attr
->nr
;
1387 struct w83795_data
*data
= dev_get_drvdata(dev
);
1388 long temp
= temp_from_reg(data
->dts_ext
[nr
]);
1390 return sprintf(buf
, "%ld\n", temp
);
1394 store_dts_ext(struct device
*dev
, struct device_attribute
*attr
,
1395 const char *buf
, size_t count
)
1397 struct sensor_device_attribute_2
*sensor_attr
=
1398 to_sensor_dev_attr_2(attr
);
1399 int nr
= sensor_attr
->nr
;
1400 struct i2c_client
*client
= to_i2c_client(dev
);
1401 struct w83795_data
*data
= i2c_get_clientdata(client
);
1404 if (kstrtol(buf
, 10, &tmp
) < 0)
1407 mutex_lock(&data
->update_lock
);
1408 data
->dts_ext
[nr
] = temp_to_reg(tmp
, -128, 127);
1409 w83795_write(client
, W83795_REG_DTS_EXT(nr
), data
->dts_ext
[nr
]);
1410 mutex_unlock(&data
->update_lock
);
1416 show_temp_mode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1418 struct w83795_data
*data
= dev_get_drvdata(dev
);
1419 struct sensor_device_attribute_2
*sensor_attr
=
1420 to_sensor_dev_attr_2(attr
);
1421 int index
= sensor_attr
->index
;
1424 if (data
->temp_mode
& (1 << index
))
1425 tmp
= 3; /* Thermal diode */
1427 tmp
= 4; /* Thermistor */
1429 return sprintf(buf
, "%d\n", tmp
);
1432 /* Only for temp1-4 (temp5-6 can only be thermistor) */
1434 store_temp_mode(struct device
*dev
, struct device_attribute
*attr
,
1435 const char *buf
, size_t count
)
1437 struct i2c_client
*client
= to_i2c_client(dev
);
1438 struct w83795_data
*data
= i2c_get_clientdata(client
);
1439 struct sensor_device_attribute_2
*sensor_attr
=
1440 to_sensor_dev_attr_2(attr
);
1441 int index
= sensor_attr
->index
;
1446 if (kstrtoul(buf
, 10, &val
) < 0)
1448 if ((val
!= 4) && (val
!= 3))
1451 mutex_lock(&data
->update_lock
);
1455 data
->temp_mode
|= 1 << index
;
1456 } else if (val
== 4) {
1459 data
->temp_mode
&= ~(1 << index
);
1462 reg_shift
= 2 * index
;
1463 tmp
= w83795_read(client
, W83795_REG_TEMP_CTRL2
);
1464 tmp
&= ~(0x03 << reg_shift
);
1465 tmp
|= val
<< reg_shift
;
1466 w83795_write(client
, W83795_REG_TEMP_CTRL2
, tmp
);
1468 mutex_unlock(&data
->update_lock
);
1473 /* show/store VIN */
1475 show_in(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1477 struct sensor_device_attribute_2
*sensor_attr
=
1478 to_sensor_dev_attr_2(attr
);
1479 int nr
= sensor_attr
->nr
;
1480 int index
= sensor_attr
->index
;
1481 struct w83795_data
*data
= w83795_update_device(dev
);
1482 u16 val
= data
->in
[index
][nr
];
1487 /* calculate this value again by sensors as sensors3.conf */
1488 if ((index
>= 17) &&
1489 !((data
->has_gain
>> (index
- 17)) & 1))
1494 lsb_idx
= IN_LSB_SHIFT_IDX
[index
][IN_LSB_IDX
];
1496 val
|= (data
->in_lsb
[lsb_idx
][nr
] >>
1497 IN_LSB_SHIFT_IDX
[index
][IN_LSB_SHIFT
]) & 0x03;
1498 if ((index
>= 17) &&
1499 !((data
->has_gain
>> (index
- 17)) & 1))
1503 val
= in_from_reg(index
, val
);
1505 return sprintf(buf
, "%d\n", val
);
1509 store_in(struct device
*dev
, struct device_attribute
*attr
,
1510 const char *buf
, size_t count
)
1512 struct sensor_device_attribute_2
*sensor_attr
=
1513 to_sensor_dev_attr_2(attr
);
1514 int nr
= sensor_attr
->nr
;
1515 int index
= sensor_attr
->index
;
1516 struct i2c_client
*client
= to_i2c_client(dev
);
1517 struct w83795_data
*data
= i2c_get_clientdata(client
);
1522 if (kstrtoul(buf
, 10, &val
) < 0)
1524 val
= in_to_reg(index
, val
);
1526 if ((index
>= 17) &&
1527 !((data
->has_gain
>> (index
- 17)) & 1))
1529 val
= clamp_val(val
, 0, 0x3FF);
1530 mutex_lock(&data
->update_lock
);
1532 lsb_idx
= IN_LSB_SHIFT_IDX
[index
][IN_LSB_IDX
];
1533 tmp
= w83795_read(client
, IN_LSB_REG(lsb_idx
, nr
));
1534 tmp
&= ~(0x03 << IN_LSB_SHIFT_IDX
[index
][IN_LSB_SHIFT
]);
1535 tmp
|= (val
& 0x03) << IN_LSB_SHIFT_IDX
[index
][IN_LSB_SHIFT
];
1536 w83795_write(client
, IN_LSB_REG(lsb_idx
, nr
), tmp
);
1537 data
->in_lsb
[lsb_idx
][nr
] = tmp
;
1539 tmp
= (val
>> 2) & 0xff;
1540 w83795_write(client
, W83795_REG_IN
[index
][nr
], tmp
);
1541 data
->in
[index
][nr
] = tmp
;
1543 mutex_unlock(&data
->update_lock
);
1548 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1550 show_sf_setup(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1552 struct sensor_device_attribute_2
*sensor_attr
=
1553 to_sensor_dev_attr_2(attr
);
1554 int nr
= sensor_attr
->nr
;
1555 struct w83795_data
*data
= w83795_update_pwm_config(dev
);
1556 u16 val
= data
->setup_pwm
[nr
];
1559 case SETUP_PWM_UPTIME
:
1560 case SETUP_PWM_DOWNTIME
:
1561 val
= time_from_reg(val
);
1565 return sprintf(buf
, "%d\n", val
);
1569 store_sf_setup(struct device
*dev
, struct device_attribute
*attr
,
1570 const char *buf
, size_t count
)
1572 struct sensor_device_attribute_2
*sensor_attr
=
1573 to_sensor_dev_attr_2(attr
);
1574 int nr
= sensor_attr
->nr
;
1575 struct i2c_client
*client
= to_i2c_client(dev
);
1576 struct w83795_data
*data
= i2c_get_clientdata(client
);
1579 if (kstrtoul(buf
, 10, &val
) < 0)
1583 case SETUP_PWM_DEFAULT
:
1584 val
= clamp_val(val
, 0, 0xff);
1586 case SETUP_PWM_UPTIME
:
1587 case SETUP_PWM_DOWNTIME
:
1588 val
= time_to_reg(val
);
1594 mutex_lock(&data
->update_lock
);
1595 data
->setup_pwm
[nr
] = val
;
1596 w83795_write(client
, W83795_REG_SETUP_PWM(nr
), val
);
1597 mutex_unlock(&data
->update_lock
);
1606 * Don't change the attribute order, _max, _min and _beep are accessed by index
1607 * somewhere else in the code
1609 #define SENSOR_ATTR_IN(index) { \
1610 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1612 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1613 store_in, IN_MAX, index), \
1614 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1615 store_in, IN_LOW, index), \
1616 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1617 NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1618 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1619 show_alarm_beep, store_beep, BEEP_ENABLE, \
1620 index + ((index > 14) ? 1 : 0)) }
1623 * Don't change the attribute order, _beep is accessed by index
1624 * somewhere else in the code
1626 #define SENSOR_ATTR_FAN(index) { \
1627 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1628 NULL, FAN_INPUT, index - 1), \
1629 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1630 show_fan, store_fan_min, FAN_MIN, index - 1), \
1631 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1632 NULL, ALARM_STATUS, index + 31), \
1633 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1634 show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
1636 #define SENSOR_ATTR_PWM(index) { \
1637 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1638 store_pwm, PWM_OUTPUT, index - 1), \
1639 SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
1640 show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
1641 SENSOR_ATTR_2(pwm##index##_mode, S_IRUGO, \
1642 show_pwm_mode, NULL, NOT_USED, index - 1), \
1643 SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
1644 show_pwm, store_pwm, PWM_FREQ, index - 1), \
1645 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1646 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1647 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1648 show_pwm, store_pwm, PWM_START, index - 1), \
1649 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1650 show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
1651 SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1652 show_fanin, store_fanin, FANIN_TARGET, index - 1) }
1655 * Don't change the attribute order, _beep is accessed by index
1656 * somewhere else in the code
1658 #define SENSOR_ATTR_DTS(index) { \
1659 SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
1660 show_dts_mode, NULL, NOT_USED, index - 7), \
1661 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
1662 NULL, NOT_USED, index - 7), \
1663 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
1664 store_dts_ext, DTS_CRIT, NOT_USED), \
1665 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1666 show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
1667 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
1668 store_dts_ext, DTS_WARN, NOT_USED), \
1669 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1670 show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
1671 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1672 show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
1673 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1674 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
1677 * Don't change the attribute order, _beep is accessed by index
1678 * somewhere else in the code
1680 #define SENSOR_ATTR_TEMP(index) { \
1681 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 5 ? S_IWUSR : 0), \
1682 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1683 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1684 NULL, TEMP_READ, index - 1), \
1685 SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
1686 store_temp, TEMP_CRIT, index - 1), \
1687 SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
1688 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1689 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1690 store_temp, TEMP_WARN, index - 1), \
1691 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1692 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1693 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1694 show_alarm_beep, NULL, ALARM_STATUS, \
1695 index + (index > 4 ? 11 : 17)), \
1696 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1697 show_alarm_beep, store_beep, BEEP_ENABLE, \
1698 index + (index > 4 ? 11 : 17)), \
1699 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1700 show_temp_pwm_enable, store_temp_pwm_enable, \
1701 TEMP_PWM_ENABLE, index - 1), \
1702 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1703 show_temp_pwm_enable, store_temp_pwm_enable, \
1704 TEMP_PWM_FAN_MAP, index - 1), \
1705 SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
1706 show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
1707 SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
1708 show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
1709 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
1710 show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1711 SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
1712 show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1713 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1714 show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
1715 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1716 show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
1717 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1718 show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
1719 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1720 show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
1721 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1722 show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
1723 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1724 show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
1725 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1726 show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
1727 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1728 show_sf4_temp, store_sf4_temp, 0, index - 1), \
1729 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1730 show_sf4_temp, store_sf4_temp, 1, index - 1), \
1731 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1732 show_sf4_temp, store_sf4_temp, 2, index - 1), \
1733 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1734 show_sf4_temp, store_sf4_temp, 3, index - 1), \
1735 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1736 show_sf4_temp, store_sf4_temp, 4, index - 1), \
1737 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1738 show_sf4_temp, store_sf4_temp, 5, index - 1), \
1739 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1740 show_sf4_temp, store_sf4_temp, 6, index - 1) }
1743 static struct sensor_device_attribute_2 w83795_in
[][5] = {
1767 static const struct sensor_device_attribute_2 w83795_fan
[][4] = {
1777 SENSOR_ATTR_FAN(10),
1778 SENSOR_ATTR_FAN(11),
1779 SENSOR_ATTR_FAN(12),
1780 SENSOR_ATTR_FAN(13),
1781 SENSOR_ATTR_FAN(14),
1784 static const struct sensor_device_attribute_2 w83795_temp
[][28] = {
1785 SENSOR_ATTR_TEMP(1),
1786 SENSOR_ATTR_TEMP(2),
1787 SENSOR_ATTR_TEMP(3),
1788 SENSOR_ATTR_TEMP(4),
1789 SENSOR_ATTR_TEMP(5),
1790 SENSOR_ATTR_TEMP(6),
1793 static const struct sensor_device_attribute_2 w83795_dts
[][8] = {
1797 SENSOR_ATTR_DTS(10),
1798 SENSOR_ATTR_DTS(11),
1799 SENSOR_ATTR_DTS(12),
1800 SENSOR_ATTR_DTS(13),
1801 SENSOR_ATTR_DTS(14),
1804 static const struct sensor_device_attribute_2 w83795_pwm
[][8] = {
1815 static const struct sensor_device_attribute_2 w83795_tss
[6] = {
1816 SENSOR_ATTR_2(temp1_source_sel
, S_IWUSR
| S_IRUGO
,
1817 show_temp_src
, store_temp_src
, NOT_USED
, 0),
1818 SENSOR_ATTR_2(temp2_source_sel
, S_IWUSR
| S_IRUGO
,
1819 show_temp_src
, store_temp_src
, NOT_USED
, 1),
1820 SENSOR_ATTR_2(temp3_source_sel
, S_IWUSR
| S_IRUGO
,
1821 show_temp_src
, store_temp_src
, NOT_USED
, 2),
1822 SENSOR_ATTR_2(temp4_source_sel
, S_IWUSR
| S_IRUGO
,
1823 show_temp_src
, store_temp_src
, NOT_USED
, 3),
1824 SENSOR_ATTR_2(temp5_source_sel
, S_IWUSR
| S_IRUGO
,
1825 show_temp_src
, store_temp_src
, NOT_USED
, 4),
1826 SENSOR_ATTR_2(temp6_source_sel
, S_IWUSR
| S_IRUGO
,
1827 show_temp_src
, store_temp_src
, NOT_USED
, 5),
1830 static const struct sensor_device_attribute_2 sda_single_files
[] = {
1831 SENSOR_ATTR_2(intrusion0_alarm
, S_IWUSR
| S_IRUGO
, show_alarm_beep
,
1832 store_chassis_clear
, ALARM_STATUS
, 46),
1833 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1834 SENSOR_ATTR_2(speed_cruise_tolerance
, S_IWUSR
| S_IRUGO
, show_fanin
,
1835 store_fanin
, FANIN_TOL
, NOT_USED
),
1836 SENSOR_ATTR_2(pwm_default
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1837 store_sf_setup
, SETUP_PWM_DEFAULT
, NOT_USED
),
1838 SENSOR_ATTR_2(pwm_uptime
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1839 store_sf_setup
, SETUP_PWM_UPTIME
, NOT_USED
),
1840 SENSOR_ATTR_2(pwm_downtime
, S_IWUSR
| S_IRUGO
, show_sf_setup
,
1841 store_sf_setup
, SETUP_PWM_DOWNTIME
, NOT_USED
),
1845 static const struct sensor_device_attribute_2 sda_beep_files
[] = {
1846 SENSOR_ATTR_2(intrusion0_beep
, S_IWUSR
| S_IRUGO
, show_alarm_beep
,
1847 store_beep
, BEEP_ENABLE
, 46),
1848 SENSOR_ATTR_2(beep_enable
, S_IWUSR
| S_IRUGO
, show_alarm_beep
,
1849 store_beep
, BEEP_ENABLE
, 47),
1856 static void w83795_init_client(struct i2c_client
*client
)
1858 struct w83795_data
*data
= i2c_get_clientdata(client
);
1859 static const u16 clkin
[4] = { /* in kHz */
1860 14318, 24000, 33333, 48000
1865 w83795_write(client
, W83795_REG_CONFIG
, 0x80);
1867 /* Start monitoring if needed */
1868 config
= w83795_read(client
, W83795_REG_CONFIG
);
1869 if (!(config
& W83795_REG_CONFIG_START
)) {
1870 dev_info(&client
->dev
, "Enabling monitoring operations\n");
1871 w83795_write(client
, W83795_REG_CONFIG
,
1872 config
| W83795_REG_CONFIG_START
);
1875 data
->clkin
= clkin
[(config
>> 3) & 0x3];
1876 dev_dbg(&client
->dev
, "clkin = %u kHz\n", data
->clkin
);
1879 static int w83795_get_device_id(struct i2c_client
*client
)
1883 device_id
= i2c_smbus_read_byte_data(client
, W83795_REG_DEVICEID
);
1886 * Special case for rev. A chips; can't be checked first because later
1887 * revisions emulate this for compatibility
1889 if (device_id
< 0 || (device_id
& 0xf0) != 0x50) {
1892 alt_id
= i2c_smbus_read_byte_data(client
,
1893 W83795_REG_DEVICEID_A
);
1901 /* Return 0 if detection is successful, -ENODEV otherwise */
1902 static int w83795_detect(struct i2c_client
*client
,
1903 struct i2c_board_info
*info
)
1905 int bank
, vendor_id
, device_id
, expected
, i2c_addr
, config
;
1906 struct i2c_adapter
*adapter
= client
->adapter
;
1907 unsigned short address
= client
->addr
;
1908 const char *chip_name
;
1910 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1912 bank
= i2c_smbus_read_byte_data(client
, W83795_REG_BANKSEL
);
1913 if (bank
< 0 || (bank
& 0x7c)) {
1914 dev_dbg(&adapter
->dev
,
1915 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1920 /* Check Nuvoton vendor ID */
1921 vendor_id
= i2c_smbus_read_byte_data(client
, W83795_REG_VENDORID
);
1922 expected
= bank
& 0x80 ? 0x5c : 0xa3;
1923 if (vendor_id
!= expected
) {
1924 dev_dbg(&adapter
->dev
,
1925 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1926 address
, "vendor id");
1930 /* Check device ID */
1931 device_id
= w83795_get_device_id(client
) |
1932 (i2c_smbus_read_byte_data(client
, W83795_REG_CHIPID
) << 8);
1933 if ((device_id
>> 4) != 0x795) {
1934 dev_dbg(&adapter
->dev
,
1935 "w83795: Detection failed at addr 0x%02hx, check %s\n",
1936 address
, "device id\n");
1941 * If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1944 if ((bank
& 0x07) == 0) {
1945 i2c_addr
= i2c_smbus_read_byte_data(client
,
1946 W83795_REG_I2C_ADDR
);
1947 if ((i2c_addr
& 0x7f) != address
) {
1948 dev_dbg(&adapter
->dev
,
1949 "w83795: Detection failed at addr 0x%02hx, "
1950 "check %s\n", address
, "i2c addr");
1956 * Check 795 chip type: 795G or 795ADG
1957 * Usually we don't write to chips during detection, but here we don't
1958 * quite have the choice; hopefully it's OK, we are about to return
1961 if ((bank
& 0x07) != 0)
1962 i2c_smbus_write_byte_data(client
, W83795_REG_BANKSEL
,
1964 config
= i2c_smbus_read_byte_data(client
, W83795_REG_CONFIG
);
1965 if (config
& W83795_REG_CONFIG_CONFIG48
)
1966 chip_name
= "w83795adg";
1968 chip_name
= "w83795g";
1970 strlcpy(info
->type
, chip_name
, I2C_NAME_SIZE
);
1971 dev_info(&adapter
->dev
, "Found %s rev. %c at 0x%02hx\n", chip_name
,
1972 'A' + (device_id
& 0xf), address
);
1977 #ifdef CONFIG_SENSORS_W83795_FANCTRL
1978 #define NUM_PWM_ATTRIBUTES ARRAY_SIZE(w83795_pwm[0])
1979 #define NUM_TEMP_ATTRIBUTES ARRAY_SIZE(w83795_temp[0])
1981 #define NUM_PWM_ATTRIBUTES 4
1982 #define NUM_TEMP_ATTRIBUTES 8
1985 static int w83795_handle_files(struct device
*dev
, int (*fn
)(struct device
*,
1986 const struct device_attribute
*))
1988 struct w83795_data
*data
= dev_get_drvdata(dev
);
1991 for (i
= 0; i
< ARRAY_SIZE(w83795_in
); i
++) {
1992 if (!(data
->has_in
& (1 << i
)))
1994 for (j
= 0; j
< ARRAY_SIZE(w83795_in
[0]); j
++) {
1995 if (j
== 4 && !data
->enable_beep
)
1997 err
= fn(dev
, &w83795_in
[i
][j
].dev_attr
);
2003 for (i
= 0; i
< ARRAY_SIZE(w83795_fan
); i
++) {
2004 if (!(data
->has_fan
& (1 << i
)))
2006 for (j
= 0; j
< ARRAY_SIZE(w83795_fan
[0]); j
++) {
2007 if (j
== 3 && !data
->enable_beep
)
2009 err
= fn(dev
, &w83795_fan
[i
][j
].dev_attr
);
2015 for (i
= 0; i
< ARRAY_SIZE(w83795_tss
); i
++) {
2016 j
= w83795_tss_useful(data
, i
);
2019 err
= fn(dev
, &w83795_tss
[i
].dev_attr
);
2024 for (i
= 0; i
< ARRAY_SIZE(sda_single_files
); i
++) {
2025 err
= fn(dev
, &sda_single_files
[i
].dev_attr
);
2030 if (data
->enable_beep
) {
2031 for (i
= 0; i
< ARRAY_SIZE(sda_beep_files
); i
++) {
2032 err
= fn(dev
, &sda_beep_files
[i
].dev_attr
);
2038 for (i
= 0; i
< data
->has_pwm
; i
++) {
2039 for (j
= 0; j
< NUM_PWM_ATTRIBUTES
; j
++) {
2040 err
= fn(dev
, &w83795_pwm
[i
][j
].dev_attr
);
2046 for (i
= 0; i
< ARRAY_SIZE(w83795_temp
); i
++) {
2047 if (!(data
->has_temp
& (1 << i
)))
2049 for (j
= 0; j
< NUM_TEMP_ATTRIBUTES
; j
++) {
2050 if (j
== 7 && !data
->enable_beep
)
2052 err
= fn(dev
, &w83795_temp
[i
][j
].dev_attr
);
2058 if (data
->enable_dts
) {
2059 for (i
= 0; i
< ARRAY_SIZE(w83795_dts
); i
++) {
2060 if (!(data
->has_dts
& (1 << i
)))
2062 for (j
= 0; j
< ARRAY_SIZE(w83795_dts
[0]); j
++) {
2063 if (j
== 7 && !data
->enable_beep
)
2065 err
= fn(dev
, &w83795_dts
[i
][j
].dev_attr
);
2075 /* We need a wrapper that fits in w83795_handle_files */
2076 static int device_remove_file_wrapper(struct device
*dev
,
2077 const struct device_attribute
*attr
)
2079 device_remove_file(dev
, attr
);
2083 static void w83795_check_dynamic_in_limits(struct i2c_client
*client
)
2085 struct w83795_data
*data
= i2c_get_clientdata(client
);
2087 int i
, err_max
, err_min
;
2089 vid_ctl
= w83795_read(client
, W83795_REG_VID_CTRL
);
2091 /* Return immediately if VRM isn't configured */
2092 if ((vid_ctl
& 0x07) == 0x00 || (vid_ctl
& 0x07) == 0x07)
2095 data
->has_dyn_in
= (vid_ctl
>> 3) & 0x07;
2096 for (i
= 0; i
< 2; i
++) {
2097 if (!(data
->has_dyn_in
& (1 << i
)))
2100 /* Voltage limits in dynamic mode, switch to read-only */
2101 err_max
= sysfs_chmod_file(&client
->dev
.kobj
,
2102 &w83795_in
[i
][2].dev_attr
.attr
,
2104 err_min
= sysfs_chmod_file(&client
->dev
.kobj
,
2105 &w83795_in
[i
][3].dev_attr
.attr
,
2107 if (err_max
|| err_min
)
2108 dev_warn(&client
->dev
,
2109 "Failed to set in%d limits read-only (%d, %d)\n",
2110 i
, err_max
, err_min
);
2112 dev_info(&client
->dev
,
2113 "in%d limits set dynamically from VID\n", i
);
2117 /* Check pins that can be used for either temperature or voltage monitoring */
2118 static void w83795_apply_temp_config(struct w83795_data
*data
, u8 config
,
2119 int temp_chan
, int in_chan
)
2121 /* config is a 2-bit value */
2123 case 0x2: /* Voltage monitoring */
2124 data
->has_in
|= 1 << in_chan
;
2126 case 0x1: /* Thermal diode */
2129 data
->temp_mode
|= 1 << temp_chan
;
2131 case 0x3: /* Thermistor */
2132 data
->has_temp
|= 1 << temp_chan
;
2137 static const struct i2c_device_id w83795_id
[];
2139 static int w83795_probe(struct i2c_client
*client
)
2143 struct device
*dev
= &client
->dev
;
2144 struct w83795_data
*data
;
2147 data
= devm_kzalloc(dev
, sizeof(struct w83795_data
), GFP_KERNEL
);
2151 i2c_set_clientdata(client
, data
);
2152 data
->chip_type
= i2c_match_id(w83795_id
, client
)->driver_data
;
2153 data
->bank
= i2c_smbus_read_byte_data(client
, W83795_REG_BANKSEL
);
2154 mutex_init(&data
->update_lock
);
2156 /* Initialize the chip */
2157 w83795_init_client(client
);
2159 /* Check which voltages and fans are present */
2160 data
->has_in
= w83795_read(client
, W83795_REG_VOLT_CTRL1
)
2161 | (w83795_read(client
, W83795_REG_VOLT_CTRL2
) << 8);
2162 data
->has_fan
= w83795_read(client
, W83795_REG_FANIN_CTRL1
)
2163 | (w83795_read(client
, W83795_REG_FANIN_CTRL2
) << 8);
2165 /* Check which analog temperatures and extra voltages are present */
2166 tmp
= w83795_read(client
, W83795_REG_TEMP_CTRL1
);
2168 data
->enable_dts
= 1;
2169 w83795_apply_temp_config(data
, (tmp
>> 2) & 0x3, 5, 16);
2170 w83795_apply_temp_config(data
, tmp
& 0x3, 4, 15);
2171 tmp
= w83795_read(client
, W83795_REG_TEMP_CTRL2
);
2172 w83795_apply_temp_config(data
, tmp
>> 6, 3, 20);
2173 w83795_apply_temp_config(data
, (tmp
>> 4) & 0x3, 2, 19);
2174 w83795_apply_temp_config(data
, (tmp
>> 2) & 0x3, 1, 18);
2175 w83795_apply_temp_config(data
, tmp
& 0x3, 0, 17);
2177 /* Check DTS enable status */
2178 if (data
->enable_dts
) {
2179 if (1 & w83795_read(client
, W83795_REG_DTSC
))
2180 data
->enable_dts
|= 2;
2181 data
->has_dts
= w83795_read(client
, W83795_REG_DTSE
);
2184 /* Report PECI Tbase values */
2185 if (data
->enable_dts
== 1) {
2186 for (i
= 0; i
< 8; i
++) {
2187 if (!(data
->has_dts
& (1 << i
)))
2189 tmp
= w83795_read(client
, W83795_REG_PECI_TBASE(i
));
2190 dev_info(&client
->dev
,
2191 "PECI agent %d Tbase temperature: %u\n",
2192 i
+ 1, (unsigned int)tmp
& 0x7f);
2196 data
->has_gain
= w83795_read(client
, W83795_REG_VMIGB_CTRL
) & 0x0f;
2198 /* pwm and smart fan */
2199 if (data
->chip_type
== w83795g
)
2204 /* Check if BEEP pin is available */
2205 if (data
->chip_type
== w83795g
) {
2206 /* The W83795G has a dedicated BEEP pin */
2207 data
->enable_beep
= 1;
2210 * The W83795ADG has a shared pin for OVT# and BEEP, so you
2213 tmp
= w83795_read(client
, W83795_REG_OVT_CFG
);
2214 if ((tmp
& OVT_CFG_SEL
) == 0)
2215 data
->enable_beep
= 1;
2218 err
= w83795_handle_files(dev
, device_create_file
);
2222 if (data
->chip_type
== w83795g
)
2223 w83795_check_dynamic_in_limits(client
);
2225 data
->hwmon_dev
= hwmon_device_register(dev
);
2226 if (IS_ERR(data
->hwmon_dev
)) {
2227 err
= PTR_ERR(data
->hwmon_dev
);
2234 w83795_handle_files(dev
, device_remove_file_wrapper
);
2238 static int w83795_remove(struct i2c_client
*client
)
2240 struct w83795_data
*data
= i2c_get_clientdata(client
);
2242 hwmon_device_unregister(data
->hwmon_dev
);
2243 w83795_handle_files(&client
->dev
, device_remove_file_wrapper
);
2249 static const struct i2c_device_id w83795_id
[] = {
2250 { "w83795g", w83795g
},
2251 { "w83795adg", w83795adg
},
2254 MODULE_DEVICE_TABLE(i2c
, w83795_id
);
2256 static struct i2c_driver w83795_driver
= {
2260 .probe_new
= w83795_probe
,
2261 .remove
= w83795_remove
,
2262 .id_table
= w83795_id
,
2264 .class = I2C_CLASS_HWMON
,
2265 .detect
= w83795_detect
,
2266 .address_list
= normal_i2c
,
2269 module_i2c_driver(w83795_driver
);
2271 MODULE_AUTHOR("Wei Song, Jean Delvare <jdelvare@suse.de>");
2272 MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
2273 MODULE_LICENSE("GPL");