xref: /openbmc/linux/drivers/hwmon/w83795.c (revision 1975d167)
1ff7924b0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2792d376bSWei Song /*
3792d376bSWei Song  *  w83795.c - Linux kernel driver for hardware monitoring
4792d376bSWei Song  *  Copyright (C) 2008 Nuvoton Technology Corp.
5792d376bSWei Song  *                Wei Song
67c81c60fSJean Delvare  *  Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
7792d376bSWei Song  *
8792d376bSWei Song  *  Supports following chips:
9792d376bSWei Song  *
10792d376bSWei Song  *  Chip       #vin   #fanin #pwm #temp #dts wchipid  vendid  i2c  ISA
11792d376bSWei Song  *  w83795g     21     14     8     6     8    0x79   0x5ca3  yes   no
12792d376bSWei Song  *  w83795adg   18     14     2     6     8    0x79   0x5ca3  yes   no
13792d376bSWei Song  */
14792d376bSWei Song 
15792d376bSWei Song #include <linux/kernel.h>
16792d376bSWei Song #include <linux/module.h>
17792d376bSWei Song #include <linux/init.h>
18792d376bSWei Song #include <linux/slab.h>
19792d376bSWei Song #include <linux/i2c.h>
20792d376bSWei Song #include <linux/hwmon.h>
21792d376bSWei Song #include <linux/hwmon-sysfs.h>
22792d376bSWei Song #include <linux/err.h>
23792d376bSWei Song #include <linux/mutex.h>
24dcd8f392SJean Delvare #include <linux/jiffies.h>
25cdb1dc3fSBartosz Golaszewski #include <linux/util_macros.h>
26792d376bSWei Song 
27792d376bSWei Song /* Addresses to scan */
2886ef4d2fSJean Delvare static const unsigned short normal_i2c[] = {
2986ef4d2fSJean Delvare 	0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
3086ef4d2fSJean Delvare };
31792d376bSWei Song 
32792d376bSWei Song 
3390ab5ee9SRusty Russell static bool reset;
34792d376bSWei Song module_param(reset, bool, 0);
35792d376bSWei Song MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
36792d376bSWei Song 
37792d376bSWei Song 
38792d376bSWei Song #define W83795_REG_BANKSEL		0x00
39792d376bSWei Song #define W83795_REG_VENDORID		0xfd
40792d376bSWei Song #define W83795_REG_CHIPID		0xfe
41792d376bSWei Song #define W83795_REG_DEVICEID		0xfb
422be381deSJean Delvare #define W83795_REG_DEVICEID_A		0xff
43792d376bSWei Song 
44792d376bSWei Song #define W83795_REG_I2C_ADDR		0xfc
45792d376bSWei Song #define W83795_REG_CONFIG		0x01
46792d376bSWei Song #define W83795_REG_CONFIG_CONFIG48	0x04
4780646b95SJean Delvare #define W83795_REG_CONFIG_START	0x01
48792d376bSWei Song 
49792d376bSWei Song /* Multi-Function Pin Ctrl Registers */
50792d376bSWei Song #define W83795_REG_VOLT_CTRL1		0x02
51792d376bSWei Song #define W83795_REG_VOLT_CTRL2		0x03
52792d376bSWei Song #define W83795_REG_TEMP_CTRL1		0x04
53792d376bSWei Song #define W83795_REG_TEMP_CTRL2		0x05
54792d376bSWei Song #define W83795_REG_FANIN_CTRL1		0x06
55792d376bSWei Song #define W83795_REG_FANIN_CTRL2		0x07
56792d376bSWei Song #define W83795_REG_VMIGB_CTRL		0x08
57792d376bSWei Song 
58792d376bSWei Song #define TEMP_READ			0
59792d376bSWei Song #define TEMP_CRIT			1
60792d376bSWei Song #define TEMP_CRIT_HYST			2
61792d376bSWei Song #define TEMP_WARN			3
62792d376bSWei Song #define TEMP_WARN_HYST			4
63c10b3ee8SGuenter Roeck /*
64c10b3ee8SGuenter Roeck  * only crit and crit_hyst affect real-time alarm status
65c10b3ee8SGuenter Roeck  * current crit crit_hyst warn warn_hyst
66c10b3ee8SGuenter Roeck  */
6786ef4d2fSJean Delvare static const u16 W83795_REG_TEMP[][5] = {
68792d376bSWei Song 	{0x21, 0x96, 0x97, 0x98, 0x99},	/* TD1/TR1 */
69792d376bSWei Song 	{0x22, 0x9a, 0x9b, 0x9c, 0x9d},	/* TD2/TR2 */
70792d376bSWei Song 	{0x23, 0x9e, 0x9f, 0xa0, 0xa1},	/* TD3/TR3 */
71792d376bSWei Song 	{0x24, 0xa2, 0xa3, 0xa4, 0xa5},	/* TD4/TR4 */
72792d376bSWei Song 	{0x1f, 0xa6, 0xa7, 0xa8, 0xa9},	/* TR5 */
73792d376bSWei Song 	{0x20, 0xaa, 0xab, 0xac, 0xad},	/* TR6 */
74792d376bSWei Song };
75792d376bSWei Song 
76792d376bSWei Song #define IN_READ				0
77792d376bSWei Song #define IN_MAX				1
78792d376bSWei Song #define IN_LOW				2
79792d376bSWei Song static const u16 W83795_REG_IN[][3] = {
80792d376bSWei Song 	/* Current, HL, LL */
81792d376bSWei Song 	{0x10, 0x70, 0x71},	/* VSEN1 */
82792d376bSWei Song 	{0x11, 0x72, 0x73},	/* VSEN2 */
83792d376bSWei Song 	{0x12, 0x74, 0x75},	/* VSEN3 */
84792d376bSWei Song 	{0x13, 0x76, 0x77},	/* VSEN4 */
85792d376bSWei Song 	{0x14, 0x78, 0x79},	/* VSEN5 */
86792d376bSWei Song 	{0x15, 0x7a, 0x7b},	/* VSEN6 */
87792d376bSWei Song 	{0x16, 0x7c, 0x7d},	/* VSEN7 */
88792d376bSWei Song 	{0x17, 0x7e, 0x7f},	/* VSEN8 */
89792d376bSWei Song 	{0x18, 0x80, 0x81},	/* VSEN9 */
90792d376bSWei Song 	{0x19, 0x82, 0x83},	/* VSEN10 */
91792d376bSWei Song 	{0x1A, 0x84, 0x85},	/* VSEN11 */
92792d376bSWei Song 	{0x1B, 0x86, 0x87},	/* VTT */
93792d376bSWei Song 	{0x1C, 0x88, 0x89},	/* 3VDD */
94792d376bSWei Song 	{0x1D, 0x8a, 0x8b},	/* 3VSB */
95792d376bSWei Song 	{0x1E, 0x8c, 0x8d},	/* VBAT */
96792d376bSWei Song 	{0x1F, 0xa6, 0xa7},	/* VSEN12 */
97792d376bSWei Song 	{0x20, 0xaa, 0xab},	/* VSEN13 */
98792d376bSWei Song 	{0x21, 0x96, 0x97},	/* VSEN14 */
99792d376bSWei Song 	{0x22, 0x9a, 0x9b},	/* VSEN15 */
100792d376bSWei Song 	{0x23, 0x9e, 0x9f},	/* VSEN16 */
101792d376bSWei Song 	{0x24, 0xa2, 0xa3},	/* VSEN17 */
102792d376bSWei Song };
103792d376bSWei Song #define W83795_REG_VRLSB		0x3C
104792d376bSWei Song 
105792d376bSWei Song static const u8 W83795_REG_IN_HL_LSB[] = {
106792d376bSWei Song 	0x8e,	/* VSEN1-4 */
107792d376bSWei Song 	0x90,	/* VSEN5-8 */
108792d376bSWei Song 	0x92,	/* VSEN9-11 */
109792d376bSWei Song 	0x94,	/* VTT, 3VDD, 3VSB, 3VBAT */
110792d376bSWei Song 	0xa8,	/* VSEN12 */
111792d376bSWei Song 	0xac,	/* VSEN13 */
112792d376bSWei Song 	0x98,	/* VSEN14 */
113792d376bSWei Song 	0x9c,	/* VSEN15 */
114792d376bSWei Song 	0xa0,	/* VSEN16 */
115792d376bSWei Song 	0xa4,	/* VSEN17 */
116792d376bSWei Song };
117792d376bSWei Song 
118792d376bSWei Song #define IN_LSB_REG(index, type) \
119792d376bSWei Song 	(((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
120792d376bSWei Song 	: (W83795_REG_IN_HL_LSB[(index)] + 1))
121792d376bSWei Song 
122792d376bSWei Song #define IN_LSB_SHIFT			0
123792d376bSWei Song #define IN_LSB_IDX			1
124792d376bSWei Song static const u8 IN_LSB_SHIFT_IDX[][2] = {
125792d376bSWei Song 	/* High/Low LSB shift, LSB No. */
126792d376bSWei Song 	{0x00, 0x00},	/* VSEN1 */
127792d376bSWei Song 	{0x02, 0x00},	/* VSEN2 */
128792d376bSWei Song 	{0x04, 0x00},	/* VSEN3 */
129792d376bSWei Song 	{0x06, 0x00},	/* VSEN4 */
130792d376bSWei Song 	{0x00, 0x01},	/* VSEN5 */
131792d376bSWei Song 	{0x02, 0x01},	/* VSEN6 */
132792d376bSWei Song 	{0x04, 0x01},	/* VSEN7 */
133792d376bSWei Song 	{0x06, 0x01},	/* VSEN8 */
134792d376bSWei Song 	{0x00, 0x02},	/* VSEN9 */
135792d376bSWei Song 	{0x02, 0x02},	/* VSEN10 */
136792d376bSWei Song 	{0x04, 0x02},	/* VSEN11 */
137792d376bSWei Song 	{0x00, 0x03},	/* VTT */
138792d376bSWei Song 	{0x02, 0x03},	/* 3VDD */
139792d376bSWei Song 	{0x04, 0x03},	/* 3VSB	*/
140792d376bSWei Song 	{0x06, 0x03},	/* VBAT	*/
141792d376bSWei Song 	{0x06, 0x04},	/* VSEN12 */
142792d376bSWei Song 	{0x06, 0x05},	/* VSEN13 */
143792d376bSWei Song 	{0x06, 0x06},	/* VSEN14 */
144792d376bSWei Song 	{0x06, 0x07},	/* VSEN15 */
145792d376bSWei Song 	{0x06, 0x08},	/* VSEN16 */
146792d376bSWei Song 	{0x06, 0x09},	/* VSEN17 */
147792d376bSWei Song };
148792d376bSWei Song 
149792d376bSWei Song 
150792d376bSWei Song #define W83795_REG_FAN(index)		(0x2E + (index))
151792d376bSWei Song #define W83795_REG_FAN_MIN_HL(index)	(0xB6 + (index))
152792d376bSWei Song #define W83795_REG_FAN_MIN_LSB(index)	(0xC4 + (index) / 2)
153792d376bSWei Song #define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
1547eb8d508SJean Delvare 	(((index) & 1) ? 4 : 0)
155792d376bSWei Song 
156792d376bSWei Song #define W83795_REG_VID_CTRL		0x6A
157792d376bSWei Song 
158cf6b9ea6SJean Delvare #define W83795_REG_ALARM_CTRL		0x40
159cf6b9ea6SJean Delvare #define ALARM_CTRL_RTSACS		(1 << 7)
160792d376bSWei Song #define W83795_REG_ALARM(index)		(0x41 + (index))
161792d376bSWei Song #define W83795_REG_CLR_CHASSIS		0x4D
162cf6b9ea6SJean Delvare #define W83795_REG_BEEP(index)		(0x50 + (index))
163792d376bSWei Song 
16452d159eeSJean Delvare #define W83795_REG_OVT_CFG		0x58
16552d159eeSJean Delvare #define OVT_CFG_SEL			(1 << 7)
16652d159eeSJean Delvare 
167792d376bSWei Song 
168792d376bSWei Song #define W83795_REG_FCMS1		0x201
169792d376bSWei Song #define W83795_REG_FCMS2		0x208
170792d376bSWei Song #define W83795_REG_TFMR(index)		(0x202 + (index))
171792d376bSWei Song #define W83795_REG_FOMC			0x20F
172792d376bSWei Song 
173792d376bSWei Song #define W83795_REG_TSS(index)		(0x209 + (index))
174792d376bSWei Song 
175edff2f8dSJean Delvare #define TSS_MAP_RESERVED		0xff
176edff2f8dSJean Delvare static const u8 tss_map[4][6] = {
177edff2f8dSJean Delvare 	{ 0,  1,  2,  3,  4,  5},
178edff2f8dSJean Delvare 	{ 6,  7,  8,  9,  0,  1},
179edff2f8dSJean Delvare 	{10, 11, 12, 13,  2,  3},
180edff2f8dSJean Delvare 	{ 4,  5,  4,  5, TSS_MAP_RESERVED, TSS_MAP_RESERVED},
181edff2f8dSJean Delvare };
182edff2f8dSJean Delvare 
183792d376bSWei Song #define PWM_OUTPUT			0
184fd7f82b8SJean Delvare #define PWM_FREQ			1
185fd7f82b8SJean Delvare #define PWM_START			2
186fd7f82b8SJean Delvare #define PWM_NONSTOP			3
187fd7f82b8SJean Delvare #define PWM_STOP_TIME			4
188fd7f82b8SJean Delvare #define W83795_REG_PWM(index, nr)	(0x210 + (nr) * 8 + (index))
189792d376bSWei Song 
190792d376bSWei Song #define W83795_REG_FTSH(index)		(0x240 + (index) * 2)
191792d376bSWei Song #define W83795_REG_FTSL(index)		(0x241 + (index) * 2)
192792d376bSWei Song #define W83795_REG_TFTS			0x250
193792d376bSWei Song 
194792d376bSWei Song #define TEMP_PWM_TTTI			0
195792d376bSWei Song #define TEMP_PWM_CTFS			1
196792d376bSWei Song #define TEMP_PWM_HCT			2
197792d376bSWei Song #define TEMP_PWM_HOT			3
198792d376bSWei Song #define W83795_REG_TTTI(index)		(0x260 + (index))
199792d376bSWei Song #define W83795_REG_CTFS(index)		(0x268 + (index))
200792d376bSWei Song #define W83795_REG_HT(index)		(0x270 + (index))
201792d376bSWei Song 
202792d376bSWei Song #define SF4_TEMP			0
203792d376bSWei Song #define SF4_PWM				1
204792d376bSWei Song #define W83795_REG_SF4_TEMP(temp_num, index) \
205792d376bSWei Song 	(0x280 + 0x10 * (temp_num) + (index))
206792d376bSWei Song #define W83795_REG_SF4_PWM(temp_num, index) \
207792d376bSWei Song 	(0x288 + 0x10 * (temp_num) + (index))
208792d376bSWei Song 
209792d376bSWei Song #define W83795_REG_DTSC			0x301
210792d376bSWei Song #define W83795_REG_DTSE			0x302
211792d376bSWei Song #define W83795_REG_DTS(index)		(0x26 + (index))
21254891a3cSJean Delvare #define W83795_REG_PECI_TBASE(index)	(0x320 + (index))
213792d376bSWei Song 
214792d376bSWei Song #define DTS_CRIT			0
215792d376bSWei Song #define DTS_CRIT_HYST			1
216792d376bSWei Song #define DTS_WARN			2
217792d376bSWei Song #define DTS_WARN_HYST			3
218792d376bSWei Song #define W83795_REG_DTS_EXT(index)	(0xB2 + (index))
219792d376bSWei Song 
220792d376bSWei Song #define SETUP_PWM_DEFAULT		0
221792d376bSWei Song #define SETUP_PWM_UPTIME		1
222792d376bSWei Song #define SETUP_PWM_DOWNTIME		2
223792d376bSWei Song #define W83795_REG_SETUP_PWM(index)    (0x20C + (index))
224792d376bSWei Song 
in_from_reg(u8 index,u16 val)225792d376bSWei Song static inline u16 in_from_reg(u8 index, u16 val)
226792d376bSWei Song {
22749c7347aSJean Delvare 	/* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
22849c7347aSJean Delvare 	if (index >= 12 && index <= 14)
229792d376bSWei Song 		return val * 6;
230792d376bSWei Song 	else
231792d376bSWei Song 		return val * 2;
232792d376bSWei Song }
233792d376bSWei Song 
in_to_reg(u8 index,u16 val)234792d376bSWei Song static inline u16 in_to_reg(u8 index, u16 val)
235792d376bSWei Song {
23649c7347aSJean Delvare 	if (index >= 12 && index <= 14)
237792d376bSWei Song 		return val / 6;
238792d376bSWei Song 	else
239792d376bSWei Song 		return val / 2;
240792d376bSWei Song }
241792d376bSWei Song 
fan_from_reg(u16 val)242792d376bSWei Song static inline unsigned long fan_from_reg(u16 val)
243792d376bSWei Song {
2446c82b2f3SJean Delvare 	if ((val == 0xfff) || (val == 0))
245792d376bSWei Song 		return 0;
246792d376bSWei Song 	return 1350000UL / val;
247792d376bSWei Song }
248792d376bSWei Song 
fan_to_reg(long rpm)249792d376bSWei Song static inline u16 fan_to_reg(long rpm)
250792d376bSWei Song {
251792d376bSWei Song 	if (rpm <= 0)
252792d376bSWei Song 		return 0x0fff;
2532a844c14SGuenter Roeck 	return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
254792d376bSWei Song }
255792d376bSWei Song 
time_from_reg(u8 reg)256792d376bSWei Song static inline unsigned long time_from_reg(u8 reg)
257792d376bSWei Song {
258792d376bSWei Song 	return reg * 100;
259792d376bSWei Song }
260792d376bSWei Song 
time_to_reg(unsigned long val)261792d376bSWei Song static inline u8 time_to_reg(unsigned long val)
262792d376bSWei Song {
2632a844c14SGuenter Roeck 	return clamp_val((val + 50) / 100, 0, 0xff);
264792d376bSWei Song }
265792d376bSWei Song 
temp_from_reg(s8 reg)266792d376bSWei Song static inline long temp_from_reg(s8 reg)
267792d376bSWei Song {
268792d376bSWei Song 	return reg * 1000;
269792d376bSWei Song }
270792d376bSWei Song 
temp_to_reg(long val,s8 min,s8 max)271792d376bSWei Song static inline s8 temp_to_reg(long val, s8 min, s8 max)
272792d376bSWei Song {
2732a844c14SGuenter Roeck 	return clamp_val(val / 1000, min, max);
274792d376bSWei Song }
275792d376bSWei Song 
27601879a85SJean Delvare static const u16 pwm_freq_cksel0[16] = {
27701879a85SJean Delvare 	1024, 512, 341, 256, 205, 171, 146, 128,
27801879a85SJean Delvare 	85, 64, 32, 16, 8, 4, 2, 1
27901879a85SJean Delvare };
28001879a85SJean Delvare 
pwm_freq_from_reg(u8 reg,u16 clkin)28101879a85SJean Delvare static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
28201879a85SJean Delvare {
28301879a85SJean Delvare 	unsigned long base_clock;
28401879a85SJean Delvare 
28501879a85SJean Delvare 	if (reg & 0x80) {
28601879a85SJean Delvare 		base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
28701879a85SJean Delvare 		return base_clock / ((reg & 0x7f) + 1);
28801879a85SJean Delvare 	} else
28901879a85SJean Delvare 		return pwm_freq_cksel0[reg & 0x0f];
29001879a85SJean Delvare }
29101879a85SJean Delvare 
pwm_freq_to_reg(unsigned long val,u16 clkin)29201879a85SJean Delvare static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
29301879a85SJean Delvare {
29401879a85SJean Delvare 	unsigned long base_clock;
29501879a85SJean Delvare 	u8 reg0, reg1;
29601879a85SJean Delvare 	unsigned long best0, best1;
29701879a85SJean Delvare 
29801879a85SJean Delvare 	/* Best fit for cksel = 0 */
299cdb1dc3fSBartosz Golaszewski 	reg0 = find_closest_descending(val, pwm_freq_cksel0,
300cdb1dc3fSBartosz Golaszewski 				       ARRAY_SIZE(pwm_freq_cksel0));
30101879a85SJean Delvare 	if (val < 375)	/* cksel = 1 can't beat this */
30201879a85SJean Delvare 		return reg0;
30301879a85SJean Delvare 	best0 = pwm_freq_cksel0[reg0];
30401879a85SJean Delvare 
30501879a85SJean Delvare 	/* Best fit for cksel = 1 */
30601879a85SJean Delvare 	base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
3072a844c14SGuenter Roeck 	reg1 = clamp_val(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
30801879a85SJean Delvare 	best1 = base_clock / reg1;
30901879a85SJean Delvare 	reg1 = 0x80 | (reg1 - 1);
31001879a85SJean Delvare 
31101879a85SJean Delvare 	/* Choose the closest one */
31201879a85SJean Delvare 	if (abs(val - best0) > abs(val - best1))
31301879a85SJean Delvare 		return reg1;
31401879a85SJean Delvare 	else
31501879a85SJean Delvare 		return reg0;
31601879a85SJean Delvare }
317792d376bSWei Song 
318792d376bSWei Song enum chip_types {w83795g, w83795adg};
319792d376bSWei Song 
320792d376bSWei Song struct w83795_data {
321792d376bSWei Song 	struct device *hwmon_dev;
322792d376bSWei Song 	struct mutex update_lock;
323792d376bSWei Song 	unsigned long last_updated;	/* In jiffies */
324792d376bSWei Song 	enum chip_types chip_type;
325792d376bSWei Song 
326792d376bSWei Song 	u8 bank;
327792d376bSWei Song 
328792d376bSWei Song 	u32 has_in;		/* Enable monitor VIN or not */
3290e256018SJean Delvare 	u8 has_dyn_in;		/* Only in2-0 can have this */
330792d376bSWei Song 	u16 in[21][3];		/* Register value, read/high/low */
331792d376bSWei Song 	u8 in_lsb[10][3];	/* LSB Register value, high/low */
332792d376bSWei Song 	u8 has_gain;		/* has gain: in17-20 * 8 */
333792d376bSWei Song 
334792d376bSWei Song 	u16 has_fan;		/* Enable fan14-1 or not */
335792d376bSWei Song 	u16 fan[14];		/* Register value combine */
336792d376bSWei Song 	u16 fan_min[14];	/* Register value combine */
337792d376bSWei Song 
338792d376bSWei Song 	u8 has_temp;		/* Enable monitor temp6-1 or not */
339dd127f5cSJean Delvare 	s8 temp[6][5];		/* current, crit, crit_hyst, warn, warn_hyst */
340792d376bSWei Song 	u8 temp_read_vrlsb[6];
34139deb699SJean Delvare 	u8 temp_mode;		/* Bit vector, 0 = TR, 1 = TD */
342792d376bSWei Song 	u8 temp_src[3];		/* Register value */
343792d376bSWei Song 
344c10b3ee8SGuenter Roeck 	u8 enable_dts;		/*
345c10b3ee8SGuenter Roeck 				 * Enable PECI and SB-TSI,
346792d376bSWei Song 				 * bit 0: =1 enable, =0 disable,
347c10b3ee8SGuenter Roeck 				 * bit 1: =1 AMD SB-TSI, =0 Intel PECI
348c10b3ee8SGuenter Roeck 				 */
349792d376bSWei Song 	u8 has_dts;		/* Enable monitor DTS temp */
350dd127f5cSJean Delvare 	s8 dts[8];		/* Register value */
351792d376bSWei Song 	u8 dts_read_vrlsb[8];	/* Register value */
352dd127f5cSJean Delvare 	s8 dts_ext[4];		/* Register value */
353792d376bSWei Song 
354c10b3ee8SGuenter Roeck 	u8 has_pwm;		/*
355c10b3ee8SGuenter Roeck 				 * 795g supports 8 pwm, 795adg only supports 2,
356792d376bSWei Song 				 * no config register, only affected by chip
357c10b3ee8SGuenter Roeck 				 * type
358c10b3ee8SGuenter Roeck 				 */
359c10b3ee8SGuenter Roeck 	u8 pwm[8][5];		/*
360c10b3ee8SGuenter Roeck 				 * Register value, output, freq, start,
361c10b3ee8SGuenter Roeck 				 *  non stop, stop time
362c10b3ee8SGuenter Roeck 				 */
36301879a85SJean Delvare 	u16 clkin;		/* CLKIN frequency in kHz */
364792d376bSWei Song 	u8 pwm_fcms[2];		/* Register value */
365792d376bSWei Song 	u8 pwm_tfmr[6];		/* Register value */
366792d376bSWei Song 	u8 pwm_fomc;		/* Register value */
367792d376bSWei Song 
368c10b3ee8SGuenter Roeck 	u16 target_speed[8];	/*
369c10b3ee8SGuenter Roeck 				 * Register value, target speed for speed
370c10b3ee8SGuenter Roeck 				 * cruise
371c10b3ee8SGuenter Roeck 				 */
372792d376bSWei Song 	u8 tol_speed;		/* tolerance of target speed */
373792d376bSWei Song 	u8 pwm_temp[6][4];	/* TTTI, CTFS, HCT, HOT */
374792d376bSWei Song 	u8 sf4_reg[6][2][7];	/* 6 temp, temp/dcpwm, 7 registers */
375792d376bSWei Song 
376792d376bSWei Song 	u8 setup_pwm[3];	/* Register value */
377792d376bSWei Song 
378792d376bSWei Song 	u8 alarms[6];		/* Register value */
37952d159eeSJean Delvare 	u8 enable_beep;
380792d376bSWei Song 	u8 beeps[6];		/* Register value */
381792d376bSWei Song 
382952a11caSPaul Fertser 	bool valid;
3832ae61de9SJean Delvare 	char valid_limits;
3841bb3450cSJean Delvare 	char valid_pwm_config;
385792d376bSWei Song };
386792d376bSWei Song 
387792d376bSWei Song /*
388792d376bSWei Song  * Hardware access
389b2469f42SJean Delvare  * We assume that nobdody can change the bank outside the driver.
390792d376bSWei Song  */
391792d376bSWei Song 
392b2469f42SJean Delvare /* Must be called with data->update_lock held, except during initialization */
w83795_set_bank(struct i2c_client * client,u8 bank)393b2469f42SJean Delvare static int w83795_set_bank(struct i2c_client *client, u8 bank)
394792d376bSWei Song {
395792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
396b2469f42SJean Delvare 	int err;
397792d376bSWei Song 
398b2469f42SJean Delvare 	/* If the same bank is already set, nothing to do */
399b2469f42SJean Delvare 	if ((data->bank & 0x07) == bank)
400b2469f42SJean Delvare 		return 0;
401b2469f42SJean Delvare 
402b2469f42SJean Delvare 	/* Change to new bank, preserve all other bits */
403b2469f42SJean Delvare 	bank |= data->bank & ~0x07;
404b2469f42SJean Delvare 	err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
405b2469f42SJean Delvare 	if (err < 0) {
406792d376bSWei Song 		dev_err(&client->dev,
407b2469f42SJean Delvare 			"Failed to set bank to %d, err %d\n",
408b2469f42SJean Delvare 			(int)bank, err);
409b2469f42SJean Delvare 		return err;
410792d376bSWei Song 	}
411b2469f42SJean Delvare 	data->bank = bank;
412b2469f42SJean Delvare 
413b2469f42SJean Delvare 	return 0;
414792d376bSWei Song }
415b2469f42SJean Delvare 
416b2469f42SJean Delvare /* Must be called with data->update_lock held, except during initialization */
w83795_read(struct i2c_client * client,u16 reg)417b2469f42SJean Delvare static u8 w83795_read(struct i2c_client *client, u16 reg)
418b2469f42SJean Delvare {
419b2469f42SJean Delvare 	int err;
420b2469f42SJean Delvare 
421b2469f42SJean Delvare 	err = w83795_set_bank(client, reg >> 8);
422b2469f42SJean Delvare 	if (err < 0)
423b2469f42SJean Delvare 		return 0x00;	/* Arbitrary */
424b2469f42SJean Delvare 
425b2469f42SJean Delvare 	err = i2c_smbus_read_byte_data(client, reg & 0xff);
426b2469f42SJean Delvare 	if (err < 0) {
427b2469f42SJean Delvare 		dev_err(&client->dev,
428b2469f42SJean Delvare 			"Failed to read from register 0x%03x, err %d\n",
429b2469f42SJean Delvare 			(int)reg, err);
430b2469f42SJean Delvare 		return 0x00;	/* Arbitrary */
431b2469f42SJean Delvare 	}
432b2469f42SJean Delvare 	return err;
433792d376bSWei Song }
434792d376bSWei Song 
435792d376bSWei Song /* Must be called with data->update_lock held, except during initialization */
w83795_write(struct i2c_client * client,u16 reg,u8 value)436792d376bSWei Song static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
437792d376bSWei Song {
438b2469f42SJean Delvare 	int err;
439792d376bSWei Song 
440b2469f42SJean Delvare 	err = w83795_set_bank(client, reg >> 8);
441b2469f42SJean Delvare 	if (err < 0)
442b2469f42SJean Delvare 		return err;
443b2469f42SJean Delvare 
444b2469f42SJean Delvare 	err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
445b2469f42SJean Delvare 	if (err < 0)
446792d376bSWei Song 		dev_err(&client->dev,
447b2469f42SJean Delvare 			"Failed to write to register 0x%03x, err %d\n",
448b2469f42SJean Delvare 			(int)reg, err);
449b2469f42SJean Delvare 	return err;
450792d376bSWei Song }
451792d376bSWei Song 
w83795_update_limits(struct i2c_client * client)4520d7237bfSJean Delvare static void w83795_update_limits(struct i2c_client *client)
4530d7237bfSJean Delvare {
4540d7237bfSJean Delvare 	struct w83795_data *data = i2c_get_clientdata(client);
4550d7237bfSJean Delvare 	int i, limit;
456014bcd28SJean Delvare 	u8 lsb;
4570d7237bfSJean Delvare 
4580d7237bfSJean Delvare 	/* Read the voltage limits */
4590d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->in); i++) {
4600d7237bfSJean Delvare 		if (!(data->has_in & (1 << i)))
4610d7237bfSJean Delvare 			continue;
4620d7237bfSJean Delvare 		data->in[i][IN_MAX] =
4630d7237bfSJean Delvare 			w83795_read(client, W83795_REG_IN[i][IN_MAX]);
4640d7237bfSJean Delvare 		data->in[i][IN_LOW] =
4650d7237bfSJean Delvare 			w83795_read(client, W83795_REG_IN[i][IN_LOW]);
4660d7237bfSJean Delvare 	}
4670d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) {
4680d7237bfSJean Delvare 		if ((i == 2 && data->chip_type == w83795adg) ||
4690d7237bfSJean Delvare 		    (i >= 4 && !(data->has_in & (1 << (i + 11)))))
4700d7237bfSJean Delvare 			continue;
4710d7237bfSJean Delvare 		data->in_lsb[i][IN_MAX] =
4720d7237bfSJean Delvare 			w83795_read(client, IN_LSB_REG(i, IN_MAX));
4730d7237bfSJean Delvare 		data->in_lsb[i][IN_LOW] =
4740d7237bfSJean Delvare 			w83795_read(client, IN_LSB_REG(i, IN_LOW));
4750d7237bfSJean Delvare 	}
4760d7237bfSJean Delvare 
4770d7237bfSJean Delvare 	/* Read the fan limits */
478014bcd28SJean Delvare 	lsb = 0; /* Silent false gcc warning */
4790d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
480c10b3ee8SGuenter Roeck 		/*
481c10b3ee8SGuenter Roeck 		 * Each register contains LSB for 2 fans, but we want to
482c10b3ee8SGuenter Roeck 		 * read it only once to save time
483c10b3ee8SGuenter Roeck 		 */
4840d7237bfSJean Delvare 		if ((i & 1) == 0 && (data->has_fan & (3 << i)))
4850d7237bfSJean Delvare 			lsb = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
4860d7237bfSJean Delvare 
4870d7237bfSJean Delvare 		if (!(data->has_fan & (1 << i)))
4880d7237bfSJean Delvare 			continue;
4890d7237bfSJean Delvare 		data->fan_min[i] =
4900d7237bfSJean Delvare 			w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
4910d7237bfSJean Delvare 		data->fan_min[i] |=
4920d7237bfSJean Delvare 			(lsb >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
4930d7237bfSJean Delvare 	}
4940d7237bfSJean Delvare 
4950d7237bfSJean Delvare 	/* Read the temperature limits */
4960d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
4970d7237bfSJean Delvare 		if (!(data->has_temp & (1 << i)))
4980d7237bfSJean Delvare 			continue;
4990d7237bfSJean Delvare 		for (limit = TEMP_CRIT; limit <= TEMP_WARN_HYST; limit++)
5000d7237bfSJean Delvare 			data->temp[i][limit] =
5010d7237bfSJean Delvare 				w83795_read(client, W83795_REG_TEMP[i][limit]);
5020d7237bfSJean Delvare 	}
5030d7237bfSJean Delvare 
5040d7237bfSJean Delvare 	/* Read the DTS limits */
505eb02755aSJean Delvare 	if (data->enable_dts) {
5060d7237bfSJean Delvare 		for (limit = DTS_CRIT; limit <= DTS_WARN_HYST; limit++)
5070d7237bfSJean Delvare 			data->dts_ext[limit] =
5080d7237bfSJean Delvare 				w83795_read(client, W83795_REG_DTS_EXT(limit));
5090d7237bfSJean Delvare 	}
5100d7237bfSJean Delvare 
5110d7237bfSJean Delvare 	/* Read beep settings */
51252d159eeSJean Delvare 	if (data->enable_beep) {
5130d7237bfSJean Delvare 		for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
51452d159eeSJean Delvare 			data->beeps[i] =
51552d159eeSJean Delvare 				w83795_read(client, W83795_REG_BEEP(i));
51652d159eeSJean Delvare 	}
5172ae61de9SJean Delvare 
5182ae61de9SJean Delvare 	data->valid_limits = 1;
5190d7237bfSJean Delvare }
5200d7237bfSJean Delvare 
w83795_update_pwm_config(struct device * dev)5211bb3450cSJean Delvare static struct w83795_data *w83795_update_pwm_config(struct device *dev)
5220d7237bfSJean Delvare {
5231bb3450cSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
5240d7237bfSJean Delvare 	struct w83795_data *data = i2c_get_clientdata(client);
5250d7237bfSJean Delvare 	int i, tmp;
5260d7237bfSJean Delvare 
5271bb3450cSJean Delvare 	mutex_lock(&data->update_lock);
5281bb3450cSJean Delvare 
5291bb3450cSJean Delvare 	if (data->valid_pwm_config)
5301bb3450cSJean Delvare 		goto END;
5311bb3450cSJean Delvare 
5320d7237bfSJean Delvare 	/* Read temperature source selection */
5330d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->temp_src); i++)
5340d7237bfSJean Delvare 		data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
5350d7237bfSJean Delvare 
5360d7237bfSJean Delvare 	/* Read automatic fan speed control settings */
5370d7237bfSJean Delvare 	data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
5380d7237bfSJean Delvare 	data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
5390d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++)
5400d7237bfSJean Delvare 		data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
5410d7237bfSJean Delvare 	data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
5420d7237bfSJean Delvare 	for (i = 0; i < data->has_pwm; i++) {
5430d7237bfSJean Delvare 		for (tmp = PWM_FREQ; tmp <= PWM_STOP_TIME; tmp++)
5440d7237bfSJean Delvare 			data->pwm[i][tmp] =
5450d7237bfSJean Delvare 				w83795_read(client, W83795_REG_PWM(i, tmp));
5460d7237bfSJean Delvare 	}
5470d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->target_speed); i++) {
5480d7237bfSJean Delvare 		data->target_speed[i] =
5490d7237bfSJean Delvare 			w83795_read(client, W83795_REG_FTSH(i)) << 4;
5500d7237bfSJean Delvare 		data->target_speed[i] |=
5510d7237bfSJean Delvare 			w83795_read(client, W83795_REG_FTSL(i)) >> 4;
5520d7237bfSJean Delvare 	}
5530d7237bfSJean Delvare 	data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
5540d7237bfSJean Delvare 
5550d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) {
5560d7237bfSJean Delvare 		data->pwm_temp[i][TEMP_PWM_TTTI] =
5570d7237bfSJean Delvare 			w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
5580d7237bfSJean Delvare 		data->pwm_temp[i][TEMP_PWM_CTFS] =
5590d7237bfSJean Delvare 			w83795_read(client, W83795_REG_CTFS(i));
5600d7237bfSJean Delvare 		tmp = w83795_read(client, W83795_REG_HT(i));
561eb02755aSJean Delvare 		data->pwm_temp[i][TEMP_PWM_HCT] = tmp >> 4;
5620d7237bfSJean Delvare 		data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
5630d7237bfSJean Delvare 	}
5640d7237bfSJean Delvare 
5650d7237bfSJean Delvare 	/* Read SmartFanIV trip points */
5660d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) {
5670d7237bfSJean Delvare 		for (tmp = 0; tmp < 7; tmp++) {
5680d7237bfSJean Delvare 			data->sf4_reg[i][SF4_TEMP][tmp] =
5690d7237bfSJean Delvare 				w83795_read(client,
5700d7237bfSJean Delvare 					    W83795_REG_SF4_TEMP(i, tmp));
5710d7237bfSJean Delvare 			data->sf4_reg[i][SF4_PWM][tmp] =
5720d7237bfSJean Delvare 				w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
5730d7237bfSJean Delvare 		}
5740d7237bfSJean Delvare 	}
5750d7237bfSJean Delvare 
5760d7237bfSJean Delvare 	/* Read setup PWM */
5770d7237bfSJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->setup_pwm); i++)
5780d7237bfSJean Delvare 		data->setup_pwm[i] =
5790d7237bfSJean Delvare 			w83795_read(client, W83795_REG_SETUP_PWM(i));
5801bb3450cSJean Delvare 
5811bb3450cSJean Delvare 	data->valid_pwm_config = 1;
5821bb3450cSJean Delvare 
5831bb3450cSJean Delvare END:
5841bb3450cSJean Delvare 	mutex_unlock(&data->update_lock);
5851bb3450cSJean Delvare 	return data;
5860d7237bfSJean Delvare }
5870d7237bfSJean Delvare 
w83795_update_device(struct device * dev)588792d376bSWei Song static struct w83795_data *w83795_update_device(struct device *dev)
589792d376bSWei Song {
590792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
591792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
592792d376bSWei Song 	u16 tmp;
593cf6b9ea6SJean Delvare 	u8 intrusion;
594792d376bSWei Song 	int i;
595792d376bSWei Song 
596792d376bSWei Song 	mutex_lock(&data->update_lock);
597792d376bSWei Song 
5982ae61de9SJean Delvare 	if (!data->valid_limits)
5992ae61de9SJean Delvare 		w83795_update_limits(client);
6002ae61de9SJean Delvare 
601792d376bSWei Song 	if (!(time_after(jiffies, data->last_updated + HZ * 2)
602792d376bSWei Song 	      || !data->valid))
603792d376bSWei Song 		goto END;
604792d376bSWei Song 
605792d376bSWei Song 	/* Update the voltages value */
606792d376bSWei Song 	for (i = 0; i < ARRAY_SIZE(data->in); i++) {
607792d376bSWei Song 		if (!(data->has_in & (1 << i)))
608792d376bSWei Song 			continue;
609792d376bSWei Song 		tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
610a654b9d4SJean Delvare 		tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
611792d376bSWei Song 		data->in[i][IN_READ] = tmp;
612792d376bSWei Song 	}
613792d376bSWei Song 
6140e256018SJean Delvare 	/* in0-2 can have dynamic limits (W83795G only) */
6150e256018SJean Delvare 	if (data->has_dyn_in) {
6160e256018SJean Delvare 		u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
6170e256018SJean Delvare 		u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
6180e256018SJean Delvare 
6190e256018SJean Delvare 		for (i = 0; i < 3; i++) {
6200e256018SJean Delvare 			if (!(data->has_dyn_in & (1 << i)))
6210e256018SJean Delvare 				continue;
6220e256018SJean Delvare 			data->in[i][IN_MAX] =
6230e256018SJean Delvare 				w83795_read(client, W83795_REG_IN[i][IN_MAX]);
6240e256018SJean Delvare 			data->in[i][IN_LOW] =
6250e256018SJean Delvare 				w83795_read(client, W83795_REG_IN[i][IN_LOW]);
6260e256018SJean Delvare 			data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
6270e256018SJean Delvare 			data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
6280e256018SJean Delvare 		}
6290e256018SJean Delvare 	}
6300e256018SJean Delvare 
631792d376bSWei Song 	/* Update fan */
632792d376bSWei Song 	for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
633792d376bSWei Song 		if (!(data->has_fan & (1 << i)))
634792d376bSWei Song 			continue;
635792d376bSWei Song 		data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
636eb02755aSJean Delvare 		data->fan[i] |= w83795_read(client, W83795_REG_VRLSB) >> 4;
637792d376bSWei Song 	}
638792d376bSWei Song 
639792d376bSWei Song 	/* Update temperature */
640792d376bSWei Song 	for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
641792d376bSWei Song 		data->temp[i][TEMP_READ] =
642792d376bSWei Song 			w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
643792d376bSWei Song 		data->temp_read_vrlsb[i] =
644792d376bSWei Song 			w83795_read(client, W83795_REG_VRLSB);
645792d376bSWei Song 	}
646792d376bSWei Song 
647792d376bSWei Song 	/* Update dts temperature */
648eb02755aSJean Delvare 	if (data->enable_dts) {
649792d376bSWei Song 		for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
650792d376bSWei Song 			if (!(data->has_dts & (1 << i)))
651792d376bSWei Song 				continue;
652792d376bSWei Song 			data->dts[i] =
653792d376bSWei Song 				w83795_read(client, W83795_REG_DTS(i));
654792d376bSWei Song 			data->dts_read_vrlsb[i] =
655792d376bSWei Song 				w83795_read(client, W83795_REG_VRLSB);
656792d376bSWei Song 		}
657792d376bSWei Song 	}
658792d376bSWei Song 
659792d376bSWei Song 	/* Update pwm output */
660792d376bSWei Song 	for (i = 0; i < data->has_pwm; i++) {
661792d376bSWei Song 		data->pwm[i][PWM_OUTPUT] =
662792d376bSWei Song 		    w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
663792d376bSWei Song 	}
664792d376bSWei Song 
665c10b3ee8SGuenter Roeck 	/*
666c10b3ee8SGuenter Roeck 	 * Update intrusion and alarms
667cf6b9ea6SJean Delvare 	 * It is important to read intrusion first, because reading from
668c10b3ee8SGuenter Roeck 	 * register SMI STS6 clears the interrupt status temporarily.
669c10b3ee8SGuenter Roeck 	 */
670cf6b9ea6SJean Delvare 	tmp = w83795_read(client, W83795_REG_ALARM_CTRL);
671cf6b9ea6SJean Delvare 	/* Switch to interrupt status for intrusion if needed */
672cf6b9ea6SJean Delvare 	if (tmp & ALARM_CTRL_RTSACS)
673cf6b9ea6SJean Delvare 		w83795_write(client, W83795_REG_ALARM_CTRL,
674cf6b9ea6SJean Delvare 			     tmp & ~ALARM_CTRL_RTSACS);
675cf6b9ea6SJean Delvare 	intrusion = w83795_read(client, W83795_REG_ALARM(5)) & (1 << 6);
676cf6b9ea6SJean Delvare 	/* Switch to real-time alarms */
677cf6b9ea6SJean Delvare 	w83795_write(client, W83795_REG_ALARM_CTRL, tmp | ALARM_CTRL_RTSACS);
678cd316df5SJean Delvare 	for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
679792d376bSWei Song 		data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
680cf6b9ea6SJean Delvare 	data->alarms[5] |= intrusion;
681cf6b9ea6SJean Delvare 	/* Restore original configuration if needed */
682cf6b9ea6SJean Delvare 	if (!(tmp & ALARM_CTRL_RTSACS))
683cf6b9ea6SJean Delvare 		w83795_write(client, W83795_REG_ALARM_CTRL,
684cf6b9ea6SJean Delvare 			     tmp & ~ALARM_CTRL_RTSACS);
685792d376bSWei Song 
686792d376bSWei Song 	data->last_updated = jiffies;
687952a11caSPaul Fertser 	data->valid = true;
688792d376bSWei Song 
689792d376bSWei Song END:
690792d376bSWei Song 	mutex_unlock(&data->update_lock);
691792d376bSWei Song 	return data;
692792d376bSWei Song }
693792d376bSWei Song 
694792d376bSWei Song /*
695792d376bSWei Song  * Sysfs attributes
696792d376bSWei Song  */
697792d376bSWei Song 
698792d376bSWei Song #define ALARM_STATUS      0
699792d376bSWei Song #define BEEP_ENABLE       1
700792d376bSWei Song static ssize_t
show_alarm_beep(struct device * dev,struct device_attribute * attr,char * buf)701792d376bSWei Song show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
702792d376bSWei Song {
703792d376bSWei Song 	struct w83795_data *data = w83795_update_device(dev);
704792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
705792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
706792d376bSWei Song 	int nr = sensor_attr->nr;
707792d376bSWei Song 	int index = sensor_attr->index >> 3;
708792d376bSWei Song 	int bit = sensor_attr->index & 0x07;
709792d376bSWei Song 	u8 val;
710792d376bSWei Song 
711eb02755aSJean Delvare 	if (nr == ALARM_STATUS)
712eb02755aSJean Delvare 		val = (data->alarms[index] >> bit) & 1;
713eb02755aSJean Delvare 	else		/* BEEP_ENABLE */
714eb02755aSJean Delvare 		val = (data->beeps[index] >> bit) & 1;
715792d376bSWei Song 
716792d376bSWei Song 	return sprintf(buf, "%u\n", val);
717792d376bSWei Song }
718792d376bSWei Song 
719792d376bSWei Song static ssize_t
store_beep(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)720792d376bSWei Song store_beep(struct device *dev, struct device_attribute *attr,
721792d376bSWei Song 	   const char *buf, size_t count)
722792d376bSWei Song {
723792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
724792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
725792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
726792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
727792d376bSWei Song 	int index = sensor_attr->index >> 3;
728792d376bSWei Song 	int shift = sensor_attr->index & 0x07;
729792d376bSWei Song 	u8 beep_bit = 1 << shift;
730792d376bSWei Song 	unsigned long val;
731792d376bSWei Song 
732179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
733792d376bSWei Song 		return -EINVAL;
734792d376bSWei Song 	if (val != 0 && val != 1)
735792d376bSWei Song 		return -EINVAL;
736792d376bSWei Song 
737792d376bSWei Song 	mutex_lock(&data->update_lock);
738792d376bSWei Song 	data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
739792d376bSWei Song 	data->beeps[index] &= ~beep_bit;
740792d376bSWei Song 	data->beeps[index] |= val << shift;
741792d376bSWei Song 	w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
742792d376bSWei Song 	mutex_unlock(&data->update_lock);
743792d376bSWei Song 
744792d376bSWei Song 	return count;
745792d376bSWei Song }
746792d376bSWei Song 
74724377101SJean Delvare /* Write 0 to clear chassis alarm */
748792d376bSWei Song static ssize_t
store_chassis_clear(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)749792d376bSWei Song store_chassis_clear(struct device *dev,
750792d376bSWei Song 		    struct device_attribute *attr, const char *buf,
751792d376bSWei Song 		    size_t count)
752792d376bSWei Song {
753792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
754792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
75524377101SJean Delvare 	unsigned long val;
75624377101SJean Delvare 
757179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0 || val != 0)
75824377101SJean Delvare 		return -EINVAL;
759792d376bSWei Song 
760792d376bSWei Song 	mutex_lock(&data->update_lock);
761792d376bSWei Song 	val = w83795_read(client, W83795_REG_CLR_CHASSIS);
762792d376bSWei Song 	val |= 0x80;
763792d376bSWei Song 	w83795_write(client, W83795_REG_CLR_CHASSIS, val);
764793c51d5SJean Delvare 
765793c51d5SJean Delvare 	/* Clear status and force cache refresh */
766793c51d5SJean Delvare 	w83795_read(client, W83795_REG_ALARM(5));
767952a11caSPaul Fertser 	data->valid = false;
768792d376bSWei Song 	mutex_unlock(&data->update_lock);
769792d376bSWei Song 	return count;
770792d376bSWei Song }
771792d376bSWei Song 
772792d376bSWei Song #define FAN_INPUT     0
773792d376bSWei Song #define FAN_MIN       1
774792d376bSWei Song static ssize_t
show_fan(struct device * dev,struct device_attribute * attr,char * buf)775792d376bSWei Song show_fan(struct device *dev, struct device_attribute *attr, char *buf)
776792d376bSWei Song {
777792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
778792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
779792d376bSWei Song 	int nr = sensor_attr->nr;
780792d376bSWei Song 	int index = sensor_attr->index;
781792d376bSWei Song 	struct w83795_data *data = w83795_update_device(dev);
782792d376bSWei Song 	u16 val;
783792d376bSWei Song 
784eb02755aSJean Delvare 	if (nr == FAN_INPUT)
785792d376bSWei Song 		val = data->fan[index] & 0x0fff;
786792d376bSWei Song 	else
787792d376bSWei Song 		val = data->fan_min[index] & 0x0fff;
788792d376bSWei Song 
789792d376bSWei Song 	return sprintf(buf, "%lu\n", fan_from_reg(val));
790792d376bSWei Song }
791792d376bSWei Song 
792792d376bSWei Song static ssize_t
store_fan_min(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)793792d376bSWei Song store_fan_min(struct device *dev, struct device_attribute *attr,
794792d376bSWei Song 	      const char *buf, size_t count)
795792d376bSWei Song {
796792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
797792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
798792d376bSWei Song 	int index = sensor_attr->index;
799792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
800792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
801792d376bSWei Song 	unsigned long val;
802792d376bSWei Song 
803179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
804792d376bSWei Song 		return -EINVAL;
805792d376bSWei Song 	val = fan_to_reg(val);
806792d376bSWei Song 
807792d376bSWei Song 	mutex_lock(&data->update_lock);
808792d376bSWei Song 	data->fan_min[index] = val;
809792d376bSWei Song 	w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
810792d376bSWei Song 	val &= 0x0f;
8117eb8d508SJean Delvare 	if (index & 1) {
812792d376bSWei Song 		val <<= 4;
813792d376bSWei Song 		val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
814792d376bSWei Song 		       & 0x0f;
815792d376bSWei Song 	} else {
816792d376bSWei Song 		val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
817792d376bSWei Song 		       & 0xf0;
818792d376bSWei Song 	}
819792d376bSWei Song 	w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
820792d376bSWei Song 	mutex_unlock(&data->update_lock);
821792d376bSWei Song 
822792d376bSWei Song 	return count;
823792d376bSWei Song }
824792d376bSWei Song 
825792d376bSWei Song static ssize_t
show_pwm(struct device * dev,struct device_attribute * attr,char * buf)826792d376bSWei Song show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
827792d376bSWei Song {
8281bb3450cSJean Delvare 	struct w83795_data *data;
829792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
830792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
831792d376bSWei Song 	int nr = sensor_attr->nr;
832792d376bSWei Song 	int index = sensor_attr->index;
83301879a85SJean Delvare 	unsigned int val;
834792d376bSWei Song 
8351bb3450cSJean Delvare 	data = nr == PWM_OUTPUT ? w83795_update_device(dev)
8361bb3450cSJean Delvare 				: w83795_update_pwm_config(dev);
8371bb3450cSJean Delvare 
838792d376bSWei Song 	switch (nr) {
839792d376bSWei Song 	case PWM_STOP_TIME:
840792d376bSWei Song 		val = time_from_reg(data->pwm[index][nr]);
841792d376bSWei Song 		break;
84201879a85SJean Delvare 	case PWM_FREQ:
84301879a85SJean Delvare 		val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
844792d376bSWei Song 		break;
845792d376bSWei Song 	default:
846792d376bSWei Song 		val = data->pwm[index][nr];
847792d376bSWei Song 		break;
848792d376bSWei Song 	}
849792d376bSWei Song 
850792d376bSWei Song 	return sprintf(buf, "%u\n", val);
851792d376bSWei Song }
852792d376bSWei Song 
853792d376bSWei Song static ssize_t
store_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)854792d376bSWei Song store_pwm(struct device *dev, struct device_attribute *attr,
855792d376bSWei Song 	  const char *buf, size_t count)
856792d376bSWei Song {
857792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
858792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
859792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
860792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
861792d376bSWei Song 	int nr = sensor_attr->nr;
862792d376bSWei Song 	int index = sensor_attr->index;
863792d376bSWei Song 	unsigned long val;
864792d376bSWei Song 
865179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
866792d376bSWei Song 		return -EINVAL;
867792d376bSWei Song 
868792d376bSWei Song 	mutex_lock(&data->update_lock);
869792d376bSWei Song 	switch (nr) {
870792d376bSWei Song 	case PWM_STOP_TIME:
871792d376bSWei Song 		val = time_to_reg(val);
872792d376bSWei Song 		break;
87301879a85SJean Delvare 	case PWM_FREQ:
87401879a85SJean Delvare 		val = pwm_freq_to_reg(val, data->clkin);
875792d376bSWei Song 		break;
876792d376bSWei Song 	default:
8772a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0xff);
878792d376bSWei Song 		break;
879792d376bSWei Song 	}
880792d376bSWei Song 	w83795_write(client, W83795_REG_PWM(index, nr), val);
88101879a85SJean Delvare 	data->pwm[index][nr] = val;
882792d376bSWei Song 	mutex_unlock(&data->update_lock);
883792d376bSWei Song 	return count;
884792d376bSWei Song }
885792d376bSWei Song 
886792d376bSWei Song static ssize_t
show_pwm_enable(struct device * dev,struct device_attribute * attr,char * buf)887792d376bSWei Song show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
888792d376bSWei Song {
889792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
890792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
8911bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
892792d376bSWei Song 	int index = sensor_attr->index;
893792d376bSWei Song 	u8 tmp;
894792d376bSWei Song 
895ae51cd9bSJean Delvare 	/* Speed cruise mode */
896ae51cd9bSJean Delvare 	if (data->pwm_fcms[0] & (1 << index)) {
897792d376bSWei Song 		tmp = 2;
898792d376bSWei Song 		goto out;
899792d376bSWei Song 	}
900ae51cd9bSJean Delvare 	/* Thermal cruise or SmartFan IV mode */
901792d376bSWei Song 	for (tmp = 0; tmp < 6; tmp++) {
902792d376bSWei Song 		if (data->pwm_tfmr[tmp] & (1 << index)) {
903792d376bSWei Song 			tmp = 3;
904792d376bSWei Song 			goto out;
905792d376bSWei Song 		}
906792d376bSWei Song 	}
907ae51cd9bSJean Delvare 	/* Manual mode */
908792d376bSWei Song 	tmp = 1;
909792d376bSWei Song 
910792d376bSWei Song out:
911792d376bSWei Song 	return sprintf(buf, "%u\n", tmp);
912792d376bSWei Song }
913792d376bSWei Song 
914792d376bSWei Song static ssize_t
store_pwm_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)915792d376bSWei Song store_pwm_enable(struct device *dev, struct device_attribute *attr,
916792d376bSWei Song 	  const char *buf, size_t count)
917792d376bSWei Song {
918792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
9191bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
920792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
921792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
922792d376bSWei Song 	int index = sensor_attr->index;
923792d376bSWei Song 	unsigned long val;
924792d376bSWei Song 	int i;
925792d376bSWei Song 
926179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
927792d376bSWei Song 		return -EINVAL;
928ae51cd9bSJean Delvare 	if (val < 1 || val > 2)
929792d376bSWei Song 		return -EINVAL;
930792d376bSWei Song 
93172fea694SJean Delvare #ifndef CONFIG_SENSORS_W83795_FANCTRL
93272fea694SJean Delvare 	if (val > 1) {
93372fea694SJean Delvare 		dev_warn(dev, "Automatic fan speed control support disabled\n");
93472fea694SJean Delvare 		dev_warn(dev, "Build with CONFIG_SENSORS_W83795_FANCTRL=y if you want it\n");
93572fea694SJean Delvare 		return -EOPNOTSUPP;
93672fea694SJean Delvare 	}
93772fea694SJean Delvare #endif
93872fea694SJean Delvare 
939792d376bSWei Song 	mutex_lock(&data->update_lock);
940792d376bSWei Song 	switch (val) {
941792d376bSWei Song 	case 1:
942ae51cd9bSJean Delvare 		/* Clear speed cruise mode bits */
943792d376bSWei Song 		data->pwm_fcms[0] &= ~(1 << index);
944792d376bSWei Song 		w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
945ae51cd9bSJean Delvare 		/* Clear thermal cruise mode bits */
946792d376bSWei Song 		for (i = 0; i < 6; i++) {
947792d376bSWei Song 			data->pwm_tfmr[i] &= ~(1 << index);
948792d376bSWei Song 			w83795_write(client, W83795_REG_TFMR(i),
949792d376bSWei Song 				data->pwm_tfmr[i]);
950792d376bSWei Song 		}
951792d376bSWei Song 		break;
952792d376bSWei Song 	case 2:
953792d376bSWei Song 		data->pwm_fcms[0] |= (1 << index);
954792d376bSWei Song 		w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
955792d376bSWei Song 		break;
956792d376bSWei Song 	}
957792d376bSWei Song 	mutex_unlock(&data->update_lock);
958792d376bSWei Song 	return count;
959792d376bSWei Song }
960792d376bSWei Song 
961792d376bSWei Song static ssize_t
show_pwm_mode(struct device * dev,struct device_attribute * attr,char * buf)962d5ab845aSJean Delvare show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
963d5ab845aSJean Delvare {
964d5ab845aSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
965d5ab845aSJean Delvare 	int index = to_sensor_dev_attr_2(attr)->index;
966d5ab845aSJean Delvare 	unsigned int mode;
967d5ab845aSJean Delvare 
968d5ab845aSJean Delvare 	if (data->pwm_fomc & (1 << index))
969d5ab845aSJean Delvare 		mode = 0;	/* DC */
970d5ab845aSJean Delvare 	else
971d5ab845aSJean Delvare 		mode = 1;	/* PWM */
972d5ab845aSJean Delvare 
973d5ab845aSJean Delvare 	return sprintf(buf, "%u\n", mode);
974d5ab845aSJean Delvare }
975d5ab845aSJean Delvare 
976edff2f8dSJean Delvare /*
977edff2f8dSJean Delvare  * Check whether a given temperature source can ever be useful.
978edff2f8dSJean Delvare  * Returns the number of selectable temperature channels which are
979edff2f8dSJean Delvare  * enabled.
980edff2f8dSJean Delvare  */
w83795_tss_useful(const struct w83795_data * data,int tsrc)981edff2f8dSJean Delvare static int w83795_tss_useful(const struct w83795_data *data, int tsrc)
982edff2f8dSJean Delvare {
983edff2f8dSJean Delvare 	int useful = 0, i;
984edff2f8dSJean Delvare 
985edff2f8dSJean Delvare 	for (i = 0; i < 4; i++) {
986edff2f8dSJean Delvare 		if (tss_map[i][tsrc] == TSS_MAP_RESERVED)
987edff2f8dSJean Delvare 			continue;
988edff2f8dSJean Delvare 		if (tss_map[i][tsrc] < 6)	/* Analog */
989edff2f8dSJean Delvare 			useful += (data->has_temp >> tss_map[i][tsrc]) & 1;
990edff2f8dSJean Delvare 		else				/* Digital */
991edff2f8dSJean Delvare 			useful += (data->has_dts >> (tss_map[i][tsrc] - 6)) & 1;
992edff2f8dSJean Delvare 	}
993edff2f8dSJean Delvare 
994edff2f8dSJean Delvare 	return useful;
995edff2f8dSJean Delvare }
996edff2f8dSJean Delvare 
997d5ab845aSJean Delvare static ssize_t
show_temp_src(struct device * dev,struct device_attribute * attr,char * buf)998792d376bSWei Song show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
999792d376bSWei Song {
1000792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1001792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
10021bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1003792d376bSWei Song 	int index = sensor_attr->index;
10042a2d27daSJean Delvare 	u8 tmp = data->temp_src[index / 2];
1005792d376bSWei Song 
10067eb8d508SJean Delvare 	if (index & 1)
10072a2d27daSJean Delvare 		tmp >>= 4;	/* Pick high nibble */
1008792d376bSWei Song 	else
10092a2d27daSJean Delvare 		tmp &= 0x0f;	/* Pick low nibble */
1010792d376bSWei Song 
10112a2d27daSJean Delvare 	/* Look-up the actual temperature channel number */
10122a2d27daSJean Delvare 	if (tmp >= 4 || tss_map[tmp][index] == TSS_MAP_RESERVED)
10132a2d27daSJean Delvare 		return -EINVAL;		/* Shouldn't happen */
10142a2d27daSJean Delvare 
10152a2d27daSJean Delvare 	return sprintf(buf, "%u\n", (unsigned int)tss_map[tmp][index] + 1);
1016792d376bSWei Song }
1017792d376bSWei Song 
1018792d376bSWei Song static ssize_t
store_temp_src(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1019792d376bSWei Song store_temp_src(struct device *dev, struct device_attribute *attr,
1020792d376bSWei Song 	  const char *buf, size_t count)
1021792d376bSWei Song {
1022792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
10231bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1024792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1025792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1026792d376bSWei Song 	int index = sensor_attr->index;
10272a2d27daSJean Delvare 	int tmp;
10282a2d27daSJean Delvare 	unsigned long channel;
1029792d376bSWei Song 	u8 val = index / 2;
1030792d376bSWei Song 
1031179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &channel) < 0 ||
10322a2d27daSJean Delvare 	    channel < 1 || channel > 14)
1033792d376bSWei Song 		return -EINVAL;
10342a2d27daSJean Delvare 
10352a2d27daSJean Delvare 	/* Check if request can be fulfilled */
10362a2d27daSJean Delvare 	for (tmp = 0; tmp < 4; tmp++) {
10372a2d27daSJean Delvare 		if (tss_map[tmp][index] == channel - 1)
10382a2d27daSJean Delvare 			break;
10392a2d27daSJean Delvare 	}
10402a2d27daSJean Delvare 	if (tmp == 4)	/* No match */
10412a2d27daSJean Delvare 		return -EINVAL;
1042792d376bSWei Song 
1043792d376bSWei Song 	mutex_lock(&data->update_lock);
10447eb8d508SJean Delvare 	if (index & 1) {
1045792d376bSWei Song 		tmp <<= 4;
1046792d376bSWei Song 		data->temp_src[val] &= 0x0f;
1047792d376bSWei Song 	} else {
1048792d376bSWei Song 		data->temp_src[val] &= 0xf0;
1049792d376bSWei Song 	}
1050792d376bSWei Song 	data->temp_src[val] |= tmp;
1051792d376bSWei Song 	w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
1052792d376bSWei Song 	mutex_unlock(&data->update_lock);
1053792d376bSWei Song 
1054792d376bSWei Song 	return count;
1055792d376bSWei Song }
1056792d376bSWei Song 
1057792d376bSWei Song #define TEMP_PWM_ENABLE   0
1058792d376bSWei Song #define TEMP_PWM_FAN_MAP  1
1059792d376bSWei Song static ssize_t
show_temp_pwm_enable(struct device * dev,struct device_attribute * attr,char * buf)1060792d376bSWei Song show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
1061792d376bSWei Song 		     char *buf)
1062792d376bSWei Song {
10631bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1064792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1065792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1066792d376bSWei Song 	int nr = sensor_attr->nr;
1067792d376bSWei Song 	int index = sensor_attr->index;
1068792d376bSWei Song 	u8 tmp = 0xff;
1069792d376bSWei Song 
1070792d376bSWei Song 	switch (nr) {
1071792d376bSWei Song 	case TEMP_PWM_ENABLE:
1072792d376bSWei Song 		tmp = (data->pwm_fcms[1] >> index) & 1;
1073792d376bSWei Song 		if (tmp)
1074792d376bSWei Song 			tmp = 4;
1075792d376bSWei Song 		else
1076792d376bSWei Song 			tmp = 3;
1077792d376bSWei Song 		break;
1078792d376bSWei Song 	case TEMP_PWM_FAN_MAP:
1079792d376bSWei Song 		tmp = data->pwm_tfmr[index];
1080792d376bSWei Song 		break;
1081792d376bSWei Song 	}
1082792d376bSWei Song 
1083792d376bSWei Song 	return sprintf(buf, "%u\n", tmp);
1084792d376bSWei Song }
1085792d376bSWei Song 
1086792d376bSWei Song static ssize_t
store_temp_pwm_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1087792d376bSWei Song store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
1088792d376bSWei Song 	  const char *buf, size_t count)
1089792d376bSWei Song {
1090792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
10911bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1092792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1093792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1094792d376bSWei Song 	int nr = sensor_attr->nr;
1095792d376bSWei Song 	int index = sensor_attr->index;
1096792d376bSWei Song 	unsigned long tmp;
1097792d376bSWei Song 
1098179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &tmp) < 0)
1099792d376bSWei Song 		return -EINVAL;
1100792d376bSWei Song 
1101792d376bSWei Song 	switch (nr) {
1102792d376bSWei Song 	case TEMP_PWM_ENABLE:
1103eb02755aSJean Delvare 		if (tmp != 3 && tmp != 4)
1104792d376bSWei Song 			return -EINVAL;
1105792d376bSWei Song 		tmp -= 3;
1106792d376bSWei Song 		mutex_lock(&data->update_lock);
1107792d376bSWei Song 		data->pwm_fcms[1] &= ~(1 << index);
1108792d376bSWei Song 		data->pwm_fcms[1] |= tmp << index;
1109792d376bSWei Song 		w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
1110792d376bSWei Song 		mutex_unlock(&data->update_lock);
1111792d376bSWei Song 		break;
1112792d376bSWei Song 	case TEMP_PWM_FAN_MAP:
1113792d376bSWei Song 		mutex_lock(&data->update_lock);
11142a844c14SGuenter Roeck 		tmp = clamp_val(tmp, 0, 0xff);
1115792d376bSWei Song 		w83795_write(client, W83795_REG_TFMR(index), tmp);
1116792d376bSWei Song 		data->pwm_tfmr[index] = tmp;
1117792d376bSWei Song 		mutex_unlock(&data->update_lock);
1118792d376bSWei Song 		break;
1119792d376bSWei Song 	}
1120792d376bSWei Song 	return count;
1121792d376bSWei Song }
1122792d376bSWei Song 
1123792d376bSWei Song #define FANIN_TARGET   0
1124792d376bSWei Song #define FANIN_TOL      1
1125792d376bSWei Song static ssize_t
show_fanin(struct device * dev,struct device_attribute * attr,char * buf)1126792d376bSWei Song show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
1127792d376bSWei Song {
11281bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1129792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1130792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1131792d376bSWei Song 	int nr = sensor_attr->nr;
1132792d376bSWei Song 	int index = sensor_attr->index;
1133792d376bSWei Song 	u16 tmp = 0;
1134792d376bSWei Song 
1135792d376bSWei Song 	switch (nr) {
1136792d376bSWei Song 	case FANIN_TARGET:
1137792d376bSWei Song 		tmp = fan_from_reg(data->target_speed[index]);
1138792d376bSWei Song 		break;
1139792d376bSWei Song 	case FANIN_TOL:
1140792d376bSWei Song 		tmp = data->tol_speed;
1141792d376bSWei Song 		break;
1142792d376bSWei Song 	}
1143792d376bSWei Song 
1144792d376bSWei Song 	return sprintf(buf, "%u\n", tmp);
1145792d376bSWei Song }
1146792d376bSWei Song 
1147792d376bSWei Song static ssize_t
store_fanin(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1148792d376bSWei Song store_fanin(struct device *dev, struct device_attribute *attr,
1149792d376bSWei Song 	  const char *buf, size_t count)
1150792d376bSWei Song {
1151792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1152792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1153792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1154792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1155792d376bSWei Song 	int nr = sensor_attr->nr;
1156792d376bSWei Song 	int index = sensor_attr->index;
1157792d376bSWei Song 	unsigned long val;
1158792d376bSWei Song 
1159179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1160792d376bSWei Song 		return -EINVAL;
1161792d376bSWei Song 
1162792d376bSWei Song 	mutex_lock(&data->update_lock);
1163792d376bSWei Song 	switch (nr) {
1164792d376bSWei Song 	case FANIN_TARGET:
11652a844c14SGuenter Roeck 		val = fan_to_reg(clamp_val(val, 0, 0xfff));
1166eb02755aSJean Delvare 		w83795_write(client, W83795_REG_FTSH(index), val >> 4);
1167792d376bSWei Song 		w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
1168792d376bSWei Song 		data->target_speed[index] = val;
1169792d376bSWei Song 		break;
1170792d376bSWei Song 	case FANIN_TOL:
11712a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0x3f);
1172792d376bSWei Song 		w83795_write(client, W83795_REG_TFTS, val);
1173792d376bSWei Song 		data->tol_speed = val;
1174792d376bSWei Song 		break;
1175792d376bSWei Song 	}
1176792d376bSWei Song 	mutex_unlock(&data->update_lock);
1177792d376bSWei Song 
1178792d376bSWei Song 	return count;
1179792d376bSWei Song }
1180792d376bSWei Song 
1181792d376bSWei Song 
1182792d376bSWei Song static ssize_t
show_temp_pwm(struct device * dev,struct device_attribute * attr,char * buf)1183792d376bSWei Song show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1184792d376bSWei Song {
11851bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1186792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1187792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1188792d376bSWei Song 	int nr = sensor_attr->nr;
1189792d376bSWei Song 	int index = sensor_attr->index;
1190792d376bSWei Song 	long tmp = temp_from_reg(data->pwm_temp[index][nr]);
1191792d376bSWei Song 
1192792d376bSWei Song 	return sprintf(buf, "%ld\n", tmp);
1193792d376bSWei Song }
1194792d376bSWei Song 
1195792d376bSWei Song static ssize_t
store_temp_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1196792d376bSWei Song store_temp_pwm(struct device *dev, struct device_attribute *attr,
1197792d376bSWei Song 	  const char *buf, size_t count)
1198792d376bSWei Song {
1199792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1200792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1201792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1202792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1203792d376bSWei Song 	int nr = sensor_attr->nr;
1204792d376bSWei Song 	int index = sensor_attr->index;
1205792d376bSWei Song 	unsigned long val;
1206792d376bSWei Song 	u8 tmp;
1207792d376bSWei Song 
1208179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1209792d376bSWei Song 		return -EINVAL;
1210792d376bSWei Song 	val /= 1000;
1211792d376bSWei Song 
1212792d376bSWei Song 	mutex_lock(&data->update_lock);
1213792d376bSWei Song 	switch (nr) {
1214792d376bSWei Song 	case TEMP_PWM_TTTI:
12152a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0x7f);
1216792d376bSWei Song 		w83795_write(client, W83795_REG_TTTI(index), val);
1217792d376bSWei Song 		break;
1218792d376bSWei Song 	case TEMP_PWM_CTFS:
12192a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0x7f);
1220792d376bSWei Song 		w83795_write(client, W83795_REG_CTFS(index), val);
1221792d376bSWei Song 		break;
1222792d376bSWei Song 	case TEMP_PWM_HCT:
12232a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0x0f);
1224792d376bSWei Song 		tmp = w83795_read(client, W83795_REG_HT(index));
1225792d376bSWei Song 		tmp &= 0x0f;
1226792d376bSWei Song 		tmp |= (val << 4) & 0xf0;
1227792d376bSWei Song 		w83795_write(client, W83795_REG_HT(index), tmp);
1228792d376bSWei Song 		break;
1229792d376bSWei Song 	case TEMP_PWM_HOT:
12302a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0x0f);
1231792d376bSWei Song 		tmp = w83795_read(client, W83795_REG_HT(index));
1232792d376bSWei Song 		tmp &= 0xf0;
1233792d376bSWei Song 		tmp |= val & 0x0f;
1234792d376bSWei Song 		w83795_write(client, W83795_REG_HT(index), tmp);
1235792d376bSWei Song 		break;
1236792d376bSWei Song 	}
1237792d376bSWei Song 	data->pwm_temp[index][nr] = val;
1238792d376bSWei Song 	mutex_unlock(&data->update_lock);
1239792d376bSWei Song 
1240792d376bSWei Song 	return count;
1241792d376bSWei Song }
1242792d376bSWei Song 
1243792d376bSWei Song static ssize_t
show_sf4_pwm(struct device * dev,struct device_attribute * attr,char * buf)1244792d376bSWei Song show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
1245792d376bSWei Song {
12461bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1247792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1248792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1249792d376bSWei Song 	int nr = sensor_attr->nr;
1250792d376bSWei Song 	int index = sensor_attr->index;
1251792d376bSWei Song 
1252792d376bSWei Song 	return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
1253792d376bSWei Song }
1254792d376bSWei Song 
1255792d376bSWei Song static ssize_t
store_sf4_pwm(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1256792d376bSWei Song store_sf4_pwm(struct device *dev, struct device_attribute *attr,
1257792d376bSWei Song 	  const char *buf, size_t count)
1258792d376bSWei Song {
1259792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1260792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1261792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1262792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1263792d376bSWei Song 	int nr = sensor_attr->nr;
1264792d376bSWei Song 	int index = sensor_attr->index;
1265792d376bSWei Song 	unsigned long val;
1266792d376bSWei Song 
1267179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1268792d376bSWei Song 		return -EINVAL;
1269792d376bSWei Song 
1270792d376bSWei Song 	mutex_lock(&data->update_lock);
1271792d376bSWei Song 	w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
1272792d376bSWei Song 	data->sf4_reg[index][SF4_PWM][nr] = val;
1273792d376bSWei Song 	mutex_unlock(&data->update_lock);
1274792d376bSWei Song 
1275792d376bSWei Song 	return count;
1276792d376bSWei Song }
1277792d376bSWei Song 
1278792d376bSWei Song static ssize_t
show_sf4_temp(struct device * dev,struct device_attribute * attr,char * buf)1279792d376bSWei Song show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
1280792d376bSWei Song {
12811bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1282792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1283792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1284792d376bSWei Song 	int nr = sensor_attr->nr;
1285792d376bSWei Song 	int index = sensor_attr->index;
1286792d376bSWei Song 
1287792d376bSWei Song 	return sprintf(buf, "%u\n",
1288792d376bSWei Song 		(data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
1289792d376bSWei Song }
1290792d376bSWei Song 
1291792d376bSWei Song static ssize_t
store_sf4_temp(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1292792d376bSWei Song store_sf4_temp(struct device *dev, struct device_attribute *attr,
1293792d376bSWei Song 	  const char *buf, size_t count)
1294792d376bSWei Song {
1295792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1296792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1297792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1298792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1299792d376bSWei Song 	int nr = sensor_attr->nr;
1300792d376bSWei Song 	int index = sensor_attr->index;
1301792d376bSWei Song 	unsigned long val;
1302792d376bSWei Song 
1303179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1304792d376bSWei Song 		return -EINVAL;
1305792d376bSWei Song 	val /= 1000;
1306792d376bSWei Song 
1307792d376bSWei Song 	mutex_lock(&data->update_lock);
1308792d376bSWei Song 	w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
1309792d376bSWei Song 	data->sf4_reg[index][SF4_TEMP][nr] = val;
1310792d376bSWei Song 	mutex_unlock(&data->update_lock);
1311792d376bSWei Song 
1312792d376bSWei Song 	return count;
1313792d376bSWei Song }
1314792d376bSWei Song 
1315792d376bSWei Song 
1316792d376bSWei Song static ssize_t
show_temp(struct device * dev,struct device_attribute * attr,char * buf)1317792d376bSWei Song show_temp(struct device *dev, struct device_attribute *attr, char *buf)
1318792d376bSWei Song {
1319792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1320792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1321792d376bSWei Song 	int nr = sensor_attr->nr;
1322792d376bSWei Song 	int index = sensor_attr->index;
1323792d376bSWei Song 	struct w83795_data *data = w83795_update_device(dev);
1324dd127f5cSJean Delvare 	long temp = temp_from_reg(data->temp[index][nr]);
1325792d376bSWei Song 
1326eb02755aSJean Delvare 	if (nr == TEMP_READ)
1327a654b9d4SJean Delvare 		temp += (data->temp_read_vrlsb[index] >> 6) * 250;
1328792d376bSWei Song 	return sprintf(buf, "%ld\n", temp);
1329792d376bSWei Song }
1330792d376bSWei Song 
1331792d376bSWei Song static ssize_t
store_temp(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1332792d376bSWei Song store_temp(struct device *dev, struct device_attribute *attr,
1333792d376bSWei Song 	   const char *buf, size_t count)
1334792d376bSWei Song {
1335792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1336792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1337792d376bSWei Song 	int nr = sensor_attr->nr;
1338792d376bSWei Song 	int index = sensor_attr->index;
1339792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1340792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1341792d376bSWei Song 	long tmp;
1342792d376bSWei Song 
1343179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &tmp) < 0)
1344792d376bSWei Song 		return -EINVAL;
1345792d376bSWei Song 
1346792d376bSWei Song 	mutex_lock(&data->update_lock);
1347792d376bSWei Song 	data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
1348792d376bSWei Song 	w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
1349792d376bSWei Song 	mutex_unlock(&data->update_lock);
1350792d376bSWei Song 	return count;
1351792d376bSWei Song }
1352792d376bSWei Song 
1353792d376bSWei Song 
1354792d376bSWei Song static ssize_t
show_dts_mode(struct device * dev,struct device_attribute * attr,char * buf)1355792d376bSWei Song show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
1356792d376bSWei Song {
135721fc9775SJean Delvare 	struct w83795_data *data = dev_get_drvdata(dev);
135839deb699SJean Delvare 	int tmp;
1359792d376bSWei Song 
1360792d376bSWei Song 	if (data->enable_dts & 2)
1361792d376bSWei Song 		tmp = 5;
1362792d376bSWei Song 	else
1363792d376bSWei Song 		tmp = 6;
1364792d376bSWei Song 
1365792d376bSWei Song 	return sprintf(buf, "%d\n", tmp);
1366792d376bSWei Song }
1367792d376bSWei Song 
1368792d376bSWei Song static ssize_t
show_dts(struct device * dev,struct device_attribute * attr,char * buf)1369792d376bSWei Song show_dts(struct device *dev, struct device_attribute *attr, char *buf)
1370792d376bSWei Song {
1371792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1372792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1373792d376bSWei Song 	int index = sensor_attr->index;
1374792d376bSWei Song 	struct w83795_data *data = w83795_update_device(dev);
1375dd127f5cSJean Delvare 	long temp = temp_from_reg(data->dts[index]);
1376792d376bSWei Song 
1377a654b9d4SJean Delvare 	temp += (data->dts_read_vrlsb[index] >> 6) * 250;
1378792d376bSWei Song 	return sprintf(buf, "%ld\n", temp);
1379792d376bSWei Song }
1380792d376bSWei Song 
1381792d376bSWei Song static ssize_t
show_dts_ext(struct device * dev,struct device_attribute * attr,char * buf)1382792d376bSWei Song show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
1383792d376bSWei Song {
1384792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1385792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1386792d376bSWei Song 	int nr = sensor_attr->nr;
138721fc9775SJean Delvare 	struct w83795_data *data = dev_get_drvdata(dev);
1388dd127f5cSJean Delvare 	long temp = temp_from_reg(data->dts_ext[nr]);
1389792d376bSWei Song 
1390792d376bSWei Song 	return sprintf(buf, "%ld\n", temp);
1391792d376bSWei Song }
1392792d376bSWei Song 
1393792d376bSWei Song static ssize_t
store_dts_ext(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1394792d376bSWei Song store_dts_ext(struct device *dev, struct device_attribute *attr,
1395792d376bSWei Song 	   const char *buf, size_t count)
1396792d376bSWei Song {
1397792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1398792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1399792d376bSWei Song 	int nr = sensor_attr->nr;
1400792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1401792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1402792d376bSWei Song 	long tmp;
1403792d376bSWei Song 
1404179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &tmp) < 0)
1405792d376bSWei Song 		return -EINVAL;
1406792d376bSWei Song 
1407792d376bSWei Song 	mutex_lock(&data->update_lock);
1408792d376bSWei Song 	data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
1409792d376bSWei Song 	w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
1410792d376bSWei Song 	mutex_unlock(&data->update_lock);
1411792d376bSWei Song 	return count;
1412792d376bSWei Song }
1413792d376bSWei Song 
1414792d376bSWei Song 
1415792d376bSWei Song static ssize_t
show_temp_mode(struct device * dev,struct device_attribute * attr,char * buf)1416792d376bSWei Song show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
1417792d376bSWei Song {
141821fc9775SJean Delvare 	struct w83795_data *data = dev_get_drvdata(dev);
1419792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1420792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1421792d376bSWei Song 	int index = sensor_attr->index;
142239deb699SJean Delvare 	int tmp;
1423792d376bSWei Song 
142439deb699SJean Delvare 	if (data->temp_mode & (1 << index))
142539deb699SJean Delvare 		tmp = 3;	/* Thermal diode */
1426792d376bSWei Song 	else
142739deb699SJean Delvare 		tmp = 4;	/* Thermistor */
1428792d376bSWei Song 
1429792d376bSWei Song 	return sprintf(buf, "%d\n", tmp);
1430792d376bSWei Song }
1431792d376bSWei Song 
143239deb699SJean Delvare /* Only for temp1-4 (temp5-6 can only be thermistor) */
1433792d376bSWei Song static ssize_t
store_temp_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1434792d376bSWei Song store_temp_mode(struct device *dev, struct device_attribute *attr,
1435792d376bSWei Song 		const char *buf, size_t count)
1436792d376bSWei Song {
1437792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1438792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1439792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1440792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1441792d376bSWei Song 	int index = sensor_attr->index;
144239deb699SJean Delvare 	int reg_shift;
1443792d376bSWei Song 	unsigned long val;
1444792d376bSWei Song 	u8 tmp;
1445792d376bSWei Song 
1446179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1447792d376bSWei Song 		return -EINVAL;
1448792d376bSWei Song 	if ((val != 4) && (val != 3))
1449792d376bSWei Song 		return -EINVAL;
1450792d376bSWei Song 
1451792d376bSWei Song 	mutex_lock(&data->update_lock);
1452792d376bSWei Song 	if (val == 3) {
145339deb699SJean Delvare 		/* Thermal diode */
145439deb699SJean Delvare 		val = 0x01;
1455792d376bSWei Song 		data->temp_mode |= 1 << index;
1456792d376bSWei Song 	} else if (val == 4) {
145739deb699SJean Delvare 		/* Thermistor */
145839deb699SJean Delvare 		val = 0x03;
145939deb699SJean Delvare 		data->temp_mode &= ~(1 << index);
1460792d376bSWei Song 	}
1461792d376bSWei Song 
146239deb699SJean Delvare 	reg_shift = 2 * index;
1463792d376bSWei Song 	tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
146439deb699SJean Delvare 	tmp &= ~(0x03 << reg_shift);
146539deb699SJean Delvare 	tmp |= val << reg_shift;
1466792d376bSWei Song 	w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
1467792d376bSWei Song 
1468792d376bSWei Song 	mutex_unlock(&data->update_lock);
1469792d376bSWei Song 	return count;
1470792d376bSWei Song }
1471792d376bSWei Song 
1472792d376bSWei Song 
1473792d376bSWei Song /* show/store VIN */
1474792d376bSWei Song static ssize_t
show_in(struct device * dev,struct device_attribute * attr,char * buf)1475792d376bSWei Song show_in(struct device *dev, struct device_attribute *attr, char *buf)
1476792d376bSWei Song {
1477792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1478792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1479792d376bSWei Song 	int nr = sensor_attr->nr;
1480792d376bSWei Song 	int index = sensor_attr->index;
1481792d376bSWei Song 	struct w83795_data *data = w83795_update_device(dev);
1482792d376bSWei Song 	u16 val = data->in[index][nr];
1483792d376bSWei Song 	u8 lsb_idx;
1484792d376bSWei Song 
1485792d376bSWei Song 	switch (nr) {
1486792d376bSWei Song 	case IN_READ:
1487792d376bSWei Song 		/* calculate this value again by sensors as sensors3.conf */
1488792d376bSWei Song 		if ((index >= 17) &&
14896f9dfd85SJean Delvare 		    !((data->has_gain >> (index - 17)) & 1))
1490792d376bSWei Song 			val *= 8;
1491792d376bSWei Song 		break;
1492792d376bSWei Song 	case IN_MAX:
1493792d376bSWei Song 	case IN_LOW:
1494792d376bSWei Song 		lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1495792d376bSWei Song 		val <<= 2;
1496792d376bSWei Song 		val |= (data->in_lsb[lsb_idx][nr] >>
14975d2cd958SJean Delvare 			IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]) & 0x03;
1498792d376bSWei Song 		if ((index >= 17) &&
14996f9dfd85SJean Delvare 		    !((data->has_gain >> (index - 17)) & 1))
1500792d376bSWei Song 			val *= 8;
1501792d376bSWei Song 		break;
1502792d376bSWei Song 	}
1503792d376bSWei Song 	val = in_from_reg(index, val);
1504792d376bSWei Song 
1505792d376bSWei Song 	return sprintf(buf, "%d\n", val);
1506792d376bSWei Song }
1507792d376bSWei Song 
1508792d376bSWei Song static ssize_t
store_in(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1509792d376bSWei Song store_in(struct device *dev, struct device_attribute *attr,
1510792d376bSWei Song 	 const char *buf, size_t count)
1511792d376bSWei Song {
1512792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1513792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1514792d376bSWei Song 	int nr = sensor_attr->nr;
1515792d376bSWei Song 	int index = sensor_attr->index;
1516792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1517792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1518792d376bSWei Song 	unsigned long val;
1519792d376bSWei Song 	u8 tmp;
1520792d376bSWei Song 	u8 lsb_idx;
1521792d376bSWei Song 
1522179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1523792d376bSWei Song 		return -EINVAL;
1524792d376bSWei Song 	val = in_to_reg(index, val);
1525792d376bSWei Song 
1526792d376bSWei Song 	if ((index >= 17) &&
15276f9dfd85SJean Delvare 	    !((data->has_gain >> (index - 17)) & 1))
1528792d376bSWei Song 		val /= 8;
15292a844c14SGuenter Roeck 	val = clamp_val(val, 0, 0x3FF);
1530792d376bSWei Song 	mutex_lock(&data->update_lock);
1531792d376bSWei Song 
1532792d376bSWei Song 	lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
1533792d376bSWei Song 	tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
1534792d376bSWei Song 	tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
1535792d376bSWei Song 	tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
1536792d376bSWei Song 	w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
1537792d376bSWei Song 	data->in_lsb[lsb_idx][nr] = tmp;
1538792d376bSWei Song 
1539792d376bSWei Song 	tmp = (val >> 2) & 0xff;
1540792d376bSWei Song 	w83795_write(client, W83795_REG_IN[index][nr], tmp);
1541792d376bSWei Song 	data->in[index][nr] = tmp;
1542792d376bSWei Song 
1543792d376bSWei Song 	mutex_unlock(&data->update_lock);
1544792d376bSWei Song 	return count;
1545792d376bSWei Song }
1546792d376bSWei Song 
1547792d376bSWei Song 
154800030af2SJean Delvare #ifdef CONFIG_SENSORS_W83795_FANCTRL
1549792d376bSWei Song static ssize_t
show_sf_setup(struct device * dev,struct device_attribute * attr,char * buf)1550792d376bSWei Song show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
1551792d376bSWei Song {
1552792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1553792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1554792d376bSWei Song 	int nr = sensor_attr->nr;
15551bb3450cSJean Delvare 	struct w83795_data *data = w83795_update_pwm_config(dev);
1556792d376bSWei Song 	u16 val = data->setup_pwm[nr];
1557792d376bSWei Song 
1558792d376bSWei Song 	switch (nr) {
1559792d376bSWei Song 	case SETUP_PWM_UPTIME:
1560792d376bSWei Song 	case SETUP_PWM_DOWNTIME:
1561792d376bSWei Song 		val = time_from_reg(val);
1562792d376bSWei Song 		break;
1563792d376bSWei Song 	}
1564792d376bSWei Song 
1565792d376bSWei Song 	return sprintf(buf, "%d\n", val);
1566792d376bSWei Song }
1567792d376bSWei Song 
1568792d376bSWei Song static ssize_t
store_sf_setup(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1569792d376bSWei Song store_sf_setup(struct device *dev, struct device_attribute *attr,
1570792d376bSWei Song 	 const char *buf, size_t count)
1571792d376bSWei Song {
1572792d376bSWei Song 	struct sensor_device_attribute_2 *sensor_attr =
1573792d376bSWei Song 	    to_sensor_dev_attr_2(attr);
1574792d376bSWei Song 	int nr = sensor_attr->nr;
1575792d376bSWei Song 	struct i2c_client *client = to_i2c_client(dev);
1576792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
1577792d376bSWei Song 	unsigned long val;
1578792d376bSWei Song 
1579179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
1580792d376bSWei Song 		return -EINVAL;
1581792d376bSWei Song 
1582792d376bSWei Song 	switch (nr) {
1583792d376bSWei Song 	case SETUP_PWM_DEFAULT:
15842a844c14SGuenter Roeck 		val = clamp_val(val, 0, 0xff);
1585792d376bSWei Song 		break;
1586792d376bSWei Song 	case SETUP_PWM_UPTIME:
1587792d376bSWei Song 	case SETUP_PWM_DOWNTIME:
1588792d376bSWei Song 		val = time_to_reg(val);
1589792d376bSWei Song 		if (val == 0)
1590792d376bSWei Song 			return -EINVAL;
1591792d376bSWei Song 		break;
1592792d376bSWei Song 	}
1593792d376bSWei Song 
1594792d376bSWei Song 	mutex_lock(&data->update_lock);
1595792d376bSWei Song 	data->setup_pwm[nr] = val;
1596792d376bSWei Song 	w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
1597792d376bSWei Song 	mutex_unlock(&data->update_lock);
1598792d376bSWei Song 	return count;
1599792d376bSWei Song }
160000030af2SJean Delvare #endif
1601792d376bSWei Song 
1602792d376bSWei Song 
1603792d376bSWei Song #define NOT_USED			-1
1604792d376bSWei Song 
1605c10b3ee8SGuenter Roeck /*
1606c10b3ee8SGuenter Roeck  * Don't change the attribute order, _max, _min and _beep are accessed by index
1607c10b3ee8SGuenter Roeck  * somewhere else in the code
1608c10b3ee8SGuenter Roeck  */
160987df0dadSJean Delvare #define SENSOR_ATTR_IN(index) {						\
1610792d376bSWei Song 	SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL,	\
1611792d376bSWei Song 		IN_READ, index), \
1612792d376bSWei Song 	SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in,	\
1613792d376bSWei Song 		store_in, IN_MAX, index),				\
1614792d376bSWei Song 	SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in,	\
1615792d376bSWei Song 		store_in, IN_LOW, index),				\
1616792d376bSWei Song 	SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep,	\
1617792d376bSWei Song 		NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
1618792d376bSWei Song 	SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO,		\
1619792d376bSWei Song 		show_alarm_beep, store_beep, BEEP_ENABLE,		\
162087df0dadSJean Delvare 		index + ((index > 14) ? 1 : 0)) }
1621792d376bSWei Song 
1622c10b3ee8SGuenter Roeck /*
1623c10b3ee8SGuenter Roeck  * Don't change the attribute order, _beep is accessed by index
1624c10b3ee8SGuenter Roeck  * somewhere else in the code
1625c10b3ee8SGuenter Roeck  */
162687df0dadSJean Delvare #define SENSOR_ATTR_FAN(index) {					\
1627792d376bSWei Song 	SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan,		\
1628792d376bSWei Song 		NULL, FAN_INPUT, index - 1), \
1629792d376bSWei Song 	SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO,		\
1630792d376bSWei Song 		show_fan, store_fan_min, FAN_MIN, index - 1),	\
1631792d376bSWei Song 	SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep,	\
1632792d376bSWei Song 		NULL, ALARM_STATUS, index + 31),			\
1633792d376bSWei Song 	SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO,		\
163487df0dadSJean Delvare 		show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
1635792d376bSWei Song 
1636b5f6a90aSJean Delvare #define SENSOR_ATTR_PWM(index) {					\
1637792d376bSWei Song 	SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm,		\
1638792d376bSWei Song 		store_pwm, PWM_OUTPUT, index - 1),			\
163972fea694SJean Delvare 	SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO,		\
164072fea694SJean Delvare 		show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
164172fea694SJean Delvare 	SENSOR_ATTR_2(pwm##index##_mode, S_IRUGO,			\
164272fea694SJean Delvare 		show_pwm_mode, NULL, NOT_USED, index - 1),		\
164372fea694SJean Delvare 	SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO,		\
164472fea694SJean Delvare 		show_pwm, store_pwm, PWM_FREQ, index - 1),		\
1645792d376bSWei Song 	SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO,		\
1646792d376bSWei Song 		show_pwm, store_pwm, PWM_NONSTOP, index - 1),		\
1647792d376bSWei Song 	SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO,		\
1648792d376bSWei Song 		show_pwm, store_pwm, PWM_START, index - 1),		\
1649792d376bSWei Song 	SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO,	\
1650792d376bSWei Song 		show_pwm, store_pwm, PWM_STOP_TIME, index - 1),	 \
1651b2cc528eSJean Delvare 	SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
1652b2cc528eSJean Delvare 		show_fanin, store_fanin, FANIN_TARGET, index - 1) }
1653792d376bSWei Song 
1654c10b3ee8SGuenter Roeck /*
1655c10b3ee8SGuenter Roeck  * Don't change the attribute order, _beep is accessed by index
1656c10b3ee8SGuenter Roeck  * somewhere else in the code
1657c10b3ee8SGuenter Roeck  */
165887df0dadSJean Delvare #define SENSOR_ATTR_DTS(index) {					\
1659792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_type, S_IRUGO ,		\
1660792d376bSWei Song 		show_dts_mode, NULL, NOT_USED, index - 7),	\
1661792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts,		\
1662792d376bSWei Song 		NULL, NOT_USED, index - 7),				\
1663a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
1664792d376bSWei Song 		store_dts_ext, DTS_CRIT, NOT_USED),			\
1665a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR,	\
1666792d376bSWei Song 		show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED),	\
1667a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
1668792d376bSWei Song 		store_dts_ext, DTS_WARN, NOT_USED),			\
1669a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR,	\
1670792d376bSWei Song 		show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED),	\
1671792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO,			\
1672792d376bSWei Song 		show_alarm_beep, NULL, ALARM_STATUS, index + 17),	\
1673792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO,		\
167487df0dadSJean Delvare 		show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
1675792d376bSWei Song 
1676c10b3ee8SGuenter Roeck /*
1677c10b3ee8SGuenter Roeck  * Don't change the attribute order, _beep is accessed by index
1678c10b3ee8SGuenter Roeck  * somewhere else in the code
1679c10b3ee8SGuenter Roeck  */
168087df0dadSJean Delvare #define SENSOR_ATTR_TEMP(index) {					\
168109aaf681SHuacai Chen 	SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 5 ? S_IWUSR : 0), \
1682792d376bSWei Song 		show_temp_mode, store_temp_mode, NOT_USED, index - 1),	\
1683792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp,		\
1684792d376bSWei Song 		NULL, TEMP_READ, index - 1),				\
1685a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp,	\
1686792d376bSWei Song 		store_temp, TEMP_CRIT, index - 1),			\
1687a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR,	\
1688792d376bSWei Song 		show_temp, store_temp, TEMP_CRIT_HYST, index - 1),	\
1689a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp,	\
1690792d376bSWei Song 		store_temp, TEMP_WARN, index - 1),			\
1691a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR,	\
1692792d376bSWei Song 		show_temp, store_temp, TEMP_WARN_HYST, index - 1),	\
1693792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO,			\
1694792d376bSWei Song 		show_alarm_beep, NULL, ALARM_STATUS,			\
1695792d376bSWei Song 		index + (index > 4 ? 11 : 17)),				\
1696792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO,		\
1697792d376bSWei Song 		show_alarm_beep, store_beep, BEEP_ENABLE,		\
1698792d376bSWei Song 		index + (index > 4 ? 11 : 17)),				\
1699792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO,	\
1700792d376bSWei Song 		show_temp_pwm_enable, store_temp_pwm_enable,		\
1701792d376bSWei Song 		TEMP_PWM_ENABLE, index - 1),				\
1702792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
1703792d376bSWei Song 		show_temp_pwm_enable, store_temp_pwm_enable,		\
1704792d376bSWei Song 		TEMP_PWM_FAN_MAP, index - 1),				\
1705792d376bSWei Song 	SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO,		\
1706792d376bSWei Song 		show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
1707a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO,		\
1708792d376bSWei Song 		show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
1709a0ce402fSJean Delvare 	SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO,	\
1710792d376bSWei Song 		show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
1711792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO,	\
1712792d376bSWei Song 		show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
1713792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1714792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 0, index - 1),		\
1715792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1716792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 1, index - 1),		\
1717792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1718792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 2, index - 1),		\
1719792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1720792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 3, index - 1),		\
1721792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1722792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 4, index - 1),		\
1723792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1724792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 5, index - 1),		\
1725792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1726792d376bSWei Song 		show_sf4_pwm, store_sf4_pwm, 6, index - 1),		\
1727792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1728792d376bSWei Song 		show_sf4_temp, store_sf4_temp, 0, index - 1),		\
1729792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1730792d376bSWei Song 		show_sf4_temp, store_sf4_temp, 1, index - 1),		\
1731792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1732792d376bSWei Song 		show_sf4_temp, store_sf4_temp, 2, index - 1),		\
1733792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1734792d376bSWei Song 		show_sf4_temp, store_sf4_temp, 3, index - 1),		\
1735792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1736792d376bSWei Song 		show_sf4_temp, store_sf4_temp, 4, index - 1),		\
1737792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1738792d376bSWei Song 		show_sf4_temp, store_sf4_temp, 5, index - 1),		\
1739792d376bSWei Song 	SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
174087df0dadSJean Delvare 		show_sf4_temp, store_sf4_temp, 6, index - 1) }
1741792d376bSWei Song 
1742792d376bSWei Song 
174387df0dadSJean Delvare static struct sensor_device_attribute_2 w83795_in[][5] = {
1744792d376bSWei Song 	SENSOR_ATTR_IN(0),
1745792d376bSWei Song 	SENSOR_ATTR_IN(1),
1746792d376bSWei Song 	SENSOR_ATTR_IN(2),
1747792d376bSWei Song 	SENSOR_ATTR_IN(3),
1748792d376bSWei Song 	SENSOR_ATTR_IN(4),
1749792d376bSWei Song 	SENSOR_ATTR_IN(5),
1750792d376bSWei Song 	SENSOR_ATTR_IN(6),
1751792d376bSWei Song 	SENSOR_ATTR_IN(7),
1752792d376bSWei Song 	SENSOR_ATTR_IN(8),
1753792d376bSWei Song 	SENSOR_ATTR_IN(9),
1754792d376bSWei Song 	SENSOR_ATTR_IN(10),
1755792d376bSWei Song 	SENSOR_ATTR_IN(11),
1756792d376bSWei Song 	SENSOR_ATTR_IN(12),
1757792d376bSWei Song 	SENSOR_ATTR_IN(13),
1758792d376bSWei Song 	SENSOR_ATTR_IN(14),
1759792d376bSWei Song 	SENSOR_ATTR_IN(15),
1760792d376bSWei Song 	SENSOR_ATTR_IN(16),
1761792d376bSWei Song 	SENSOR_ATTR_IN(17),
1762792d376bSWei Song 	SENSOR_ATTR_IN(18),
1763792d376bSWei Song 	SENSOR_ATTR_IN(19),
1764792d376bSWei Song 	SENSOR_ATTR_IN(20),
1765792d376bSWei Song };
1766792d376bSWei Song 
176786ef4d2fSJean Delvare static const struct sensor_device_attribute_2 w83795_fan[][4] = {
1768792d376bSWei Song 	SENSOR_ATTR_FAN(1),
1769792d376bSWei Song 	SENSOR_ATTR_FAN(2),
1770792d376bSWei Song 	SENSOR_ATTR_FAN(3),
1771792d376bSWei Song 	SENSOR_ATTR_FAN(4),
1772792d376bSWei Song 	SENSOR_ATTR_FAN(5),
1773792d376bSWei Song 	SENSOR_ATTR_FAN(6),
1774792d376bSWei Song 	SENSOR_ATTR_FAN(7),
1775792d376bSWei Song 	SENSOR_ATTR_FAN(8),
1776792d376bSWei Song 	SENSOR_ATTR_FAN(9),
1777792d376bSWei Song 	SENSOR_ATTR_FAN(10),
1778792d376bSWei Song 	SENSOR_ATTR_FAN(11),
1779792d376bSWei Song 	SENSOR_ATTR_FAN(12),
1780792d376bSWei Song 	SENSOR_ATTR_FAN(13),
1781792d376bSWei Song 	SENSOR_ATTR_FAN(14),
1782792d376bSWei Song };
1783792d376bSWei Song 
1784edff2f8dSJean Delvare static const struct sensor_device_attribute_2 w83795_temp[][28] = {
1785792d376bSWei Song 	SENSOR_ATTR_TEMP(1),
1786792d376bSWei Song 	SENSOR_ATTR_TEMP(2),
1787792d376bSWei Song 	SENSOR_ATTR_TEMP(3),
1788792d376bSWei Song 	SENSOR_ATTR_TEMP(4),
1789792d376bSWei Song 	SENSOR_ATTR_TEMP(5),
1790792d376bSWei Song 	SENSOR_ATTR_TEMP(6),
1791792d376bSWei Song };
1792792d376bSWei Song 
179386ef4d2fSJean Delvare static const struct sensor_device_attribute_2 w83795_dts[][8] = {
1794792d376bSWei Song 	SENSOR_ATTR_DTS(7),
1795792d376bSWei Song 	SENSOR_ATTR_DTS(8),
1796792d376bSWei Song 	SENSOR_ATTR_DTS(9),
1797792d376bSWei Song 	SENSOR_ATTR_DTS(10),
1798792d376bSWei Song 	SENSOR_ATTR_DTS(11),
1799792d376bSWei Song 	SENSOR_ATTR_DTS(12),
1800792d376bSWei Song 	SENSOR_ATTR_DTS(13),
1801792d376bSWei Song 	SENSOR_ATTR_DTS(14),
1802792d376bSWei Song };
1803792d376bSWei Song 
1804d5ab845aSJean Delvare static const struct sensor_device_attribute_2 w83795_pwm[][8] = {
1805b5f6a90aSJean Delvare 	SENSOR_ATTR_PWM(1),
1806b5f6a90aSJean Delvare 	SENSOR_ATTR_PWM(2),
1807792d376bSWei Song 	SENSOR_ATTR_PWM(3),
1808792d376bSWei Song 	SENSOR_ATTR_PWM(4),
1809792d376bSWei Song 	SENSOR_ATTR_PWM(5),
1810792d376bSWei Song 	SENSOR_ATTR_PWM(6),
1811792d376bSWei Song 	SENSOR_ATTR_PWM(7),
1812792d376bSWei Song 	SENSOR_ATTR_PWM(8),
1813792d376bSWei Song };
1814792d376bSWei Song 
1815edff2f8dSJean Delvare static const struct sensor_device_attribute_2 w83795_tss[6] = {
1816edff2f8dSJean Delvare 	SENSOR_ATTR_2(temp1_source_sel, S_IWUSR | S_IRUGO,
1817edff2f8dSJean Delvare 		      show_temp_src, store_temp_src, NOT_USED, 0),
1818edff2f8dSJean Delvare 	SENSOR_ATTR_2(temp2_source_sel, S_IWUSR | S_IRUGO,
1819edff2f8dSJean Delvare 		      show_temp_src, store_temp_src, NOT_USED, 1),
1820edff2f8dSJean Delvare 	SENSOR_ATTR_2(temp3_source_sel, S_IWUSR | S_IRUGO,
1821edff2f8dSJean Delvare 		      show_temp_src, store_temp_src, NOT_USED, 2),
1822edff2f8dSJean Delvare 	SENSOR_ATTR_2(temp4_source_sel, S_IWUSR | S_IRUGO,
1823edff2f8dSJean Delvare 		      show_temp_src, store_temp_src, NOT_USED, 3),
1824edff2f8dSJean Delvare 	SENSOR_ATTR_2(temp5_source_sel, S_IWUSR | S_IRUGO,
1825edff2f8dSJean Delvare 		      show_temp_src, store_temp_src, NOT_USED, 4),
1826edff2f8dSJean Delvare 	SENSOR_ATTR_2(temp6_source_sel, S_IWUSR | S_IRUGO,
1827edff2f8dSJean Delvare 		      show_temp_src, store_temp_src, NOT_USED, 5),
1828edff2f8dSJean Delvare };
1829edff2f8dSJean Delvare 
183086ef4d2fSJean Delvare static const struct sensor_device_attribute_2 sda_single_files[] = {
183124377101SJean Delvare 	SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
1832792d376bSWei Song 		      store_chassis_clear, ALARM_STATUS, 46),
183300030af2SJean Delvare #ifdef CONFIG_SENSORS_W83795_FANCTRL
1834792d376bSWei Song 	SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
1835792d376bSWei Song 		store_fanin, FANIN_TOL, NOT_USED),
1836792d376bSWei Song 	SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1837792d376bSWei Song 		      store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1838792d376bSWei Song 	SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1839792d376bSWei Song 		      store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1840792d376bSWei Song 	SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1841792d376bSWei Song 		      store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
184200030af2SJean Delvare #endif
1843792d376bSWei Song };
1844792d376bSWei Song 
184552d159eeSJean Delvare static const struct sensor_device_attribute_2 sda_beep_files[] = {
184652d159eeSJean Delvare 	SENSOR_ATTR_2(intrusion0_beep, S_IWUSR | S_IRUGO, show_alarm_beep,
184752d159eeSJean Delvare 		      store_beep, BEEP_ENABLE, 46),
184852d159eeSJean Delvare 	SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
184952d159eeSJean Delvare 		      store_beep, BEEP_ENABLE, 47),
185052d159eeSJean Delvare };
185152d159eeSJean Delvare 
1852792d376bSWei Song /*
1853792d376bSWei Song  * Driver interface
1854792d376bSWei Song  */
1855792d376bSWei Song 
w83795_init_client(struct i2c_client * client)1856792d376bSWei Song static void w83795_init_client(struct i2c_client *client)
1857792d376bSWei Song {
185801879a85SJean Delvare 	struct w83795_data *data = i2c_get_clientdata(client);
185901879a85SJean Delvare 	static const u16 clkin[4] = {	/* in kHz */
186001879a85SJean Delvare 		14318, 24000, 33333, 48000
186101879a85SJean Delvare 	};
186280646b95SJean Delvare 	u8 config;
186380646b95SJean Delvare 
1864792d376bSWei Song 	if (reset)
1865792d376bSWei Song 		w83795_write(client, W83795_REG_CONFIG, 0x80);
1866792d376bSWei Song 
186780646b95SJean Delvare 	/* Start monitoring if needed */
186880646b95SJean Delvare 	config = w83795_read(client, W83795_REG_CONFIG);
186980646b95SJean Delvare 	if (!(config & W83795_REG_CONFIG_START)) {
187080646b95SJean Delvare 		dev_info(&client->dev, "Enabling monitoring operations\n");
1871792d376bSWei Song 		w83795_write(client, W83795_REG_CONFIG,
187280646b95SJean Delvare 			     config | W83795_REG_CONFIG_START);
187380646b95SJean Delvare 	}
187401879a85SJean Delvare 
187501879a85SJean Delvare 	data->clkin = clkin[(config >> 3) & 0x3];
187601879a85SJean Delvare 	dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
1877792d376bSWei Song }
1878792d376bSWei Song 
w83795_get_device_id(struct i2c_client * client)18792be381deSJean Delvare static int w83795_get_device_id(struct i2c_client *client)
18802be381deSJean Delvare {
18812be381deSJean Delvare 	int device_id;
18822be381deSJean Delvare 
18832be381deSJean Delvare 	device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
18842be381deSJean Delvare 
1885c10b3ee8SGuenter Roeck 	/*
1886c10b3ee8SGuenter Roeck 	 * Special case for rev. A chips; can't be checked first because later
1887c10b3ee8SGuenter Roeck 	 * revisions emulate this for compatibility
1888c10b3ee8SGuenter Roeck 	 */
18892be381deSJean Delvare 	if (device_id < 0 || (device_id & 0xf0) != 0x50) {
18902be381deSJean Delvare 		int alt_id;
18912be381deSJean Delvare 
18922be381deSJean Delvare 		alt_id = i2c_smbus_read_byte_data(client,
18932be381deSJean Delvare 						  W83795_REG_DEVICEID_A);
18942be381deSJean Delvare 		if (alt_id == 0x50)
18952be381deSJean Delvare 			device_id = alt_id;
18962be381deSJean Delvare 	}
18972be381deSJean Delvare 
18982be381deSJean Delvare 	return device_id;
18992be381deSJean Delvare }
19002be381deSJean Delvare 
1901792d376bSWei Song /* Return 0 if detection is successful, -ENODEV otherwise */
w83795_detect(struct i2c_client * client,struct i2c_board_info * info)1902792d376bSWei Song static int w83795_detect(struct i2c_client *client,
1903792d376bSWei Song 			 struct i2c_board_info *info)
1904792d376bSWei Song {
19052be381deSJean Delvare 	int bank, vendor_id, device_id, expected, i2c_addr, config;
1906792d376bSWei Song 	struct i2c_adapter *adapter = client->adapter;
1907792d376bSWei Song 	unsigned short address = client->addr;
1908093d1a47SJean Delvare 	const char *chip_name;
1909792d376bSWei Song 
1910792d376bSWei Song 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1911792d376bSWei Song 		return -ENODEV;
1912792d376bSWei Song 	bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
19132be381deSJean Delvare 	if (bank < 0 || (bank & 0x7c)) {
19142be381deSJean Delvare 		dev_dbg(&adapter->dev,
19152be381deSJean Delvare 			"w83795: Detection failed at addr 0x%02hx, check %s\n",
19162be381deSJean Delvare 			address, "bank");
19172be381deSJean Delvare 		return -ENODEV;
19182be381deSJean Delvare 	}
1919792d376bSWei Song 
1920792d376bSWei Song 	/* Check Nuvoton vendor ID */
19212be381deSJean Delvare 	vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
19222be381deSJean Delvare 	expected = bank & 0x80 ? 0x5c : 0xa3;
19232be381deSJean Delvare 	if (vendor_id != expected) {
19242be381deSJean Delvare 		dev_dbg(&adapter->dev,
19252be381deSJean Delvare 			"w83795: Detection failed at addr 0x%02hx, check %s\n",
19262be381deSJean Delvare 			address, "vendor id");
19272be381deSJean Delvare 		return -ENODEV;
19282be381deSJean Delvare 	}
19292be381deSJean Delvare 
19302be381deSJean Delvare 	/* Check device ID */
19312be381deSJean Delvare 	device_id = w83795_get_device_id(client) |
19322be381deSJean Delvare 		    (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
19332be381deSJean Delvare 	if ((device_id >> 4) != 0x795) {
19342be381deSJean Delvare 		dev_dbg(&adapter->dev,
19352be381deSJean Delvare 			"w83795: Detection failed at addr 0x%02hx, check %s\n",
19362be381deSJean Delvare 			address, "device id\n");
1937792d376bSWei Song 		return -ENODEV;
1938792d376bSWei Song 	}
1939792d376bSWei Song 
1940c10b3ee8SGuenter Roeck 	/*
1941c10b3ee8SGuenter Roeck 	 * If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
1942c10b3ee8SGuenter Roeck 	 * should match
1943c10b3ee8SGuenter Roeck 	 */
19442be381deSJean Delvare 	if ((bank & 0x07) == 0) {
19452be381deSJean Delvare 		i2c_addr = i2c_smbus_read_byte_data(client,
19462be381deSJean Delvare 						    W83795_REG_I2C_ADDR);
19472be381deSJean Delvare 		if ((i2c_addr & 0x7f) != address) {
19482be381deSJean Delvare 			dev_dbg(&adapter->dev,
19492be381deSJean Delvare 				"w83795: Detection failed at addr 0x%02hx, "
19502be381deSJean Delvare 				"check %s\n", address, "i2c addr");
1951792d376bSWei Song 			return -ENODEV;
1952792d376bSWei Song 		}
1953792d376bSWei Song 	}
1954792d376bSWei Song 
1955c10b3ee8SGuenter Roeck 	/*
1956c10b3ee8SGuenter Roeck 	 * Check 795 chip type: 795G or 795ADG
1957c10b3ee8SGuenter Roeck 	 * Usually we don't write to chips during detection, but here we don't
1958c10b3ee8SGuenter Roeck 	 * quite have the choice; hopefully it's OK, we are about to return
1959c10b3ee8SGuenter Roeck 	 * success anyway
1960c10b3ee8SGuenter Roeck 	 */
1961093d1a47SJean Delvare 	if ((bank & 0x07) != 0)
1962093d1a47SJean Delvare 		i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
1963093d1a47SJean Delvare 					  bank & ~0x07);
19642be381deSJean Delvare 	config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
19652be381deSJean Delvare 	if (config & W83795_REG_CONFIG_CONFIG48)
1966093d1a47SJean Delvare 		chip_name = "w83795adg";
19672be381deSJean Delvare 	else
1968093d1a47SJean Delvare 		chip_name = "w83795g";
1969792d376bSWei Song 
1970f2f394dbSWolfram Sang 	strscpy(info->type, chip_name, I2C_NAME_SIZE);
19712be381deSJean Delvare 	dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
19722be381deSJean Delvare 		 'A' + (device_id & 0xf), address);
1973792d376bSWei Song 
1974792d376bSWei Song 	return 0;
1975792d376bSWei Song }
1976792d376bSWei Song 
197772fea694SJean Delvare #ifdef CONFIG_SENSORS_W83795_FANCTRL
197872fea694SJean Delvare #define NUM_PWM_ATTRIBUTES	ARRAY_SIZE(w83795_pwm[0])
197972fea694SJean Delvare #define NUM_TEMP_ATTRIBUTES	ARRAY_SIZE(w83795_temp[0])
198072fea694SJean Delvare #else
198172fea694SJean Delvare #define NUM_PWM_ATTRIBUTES	4
198272fea694SJean Delvare #define NUM_TEMP_ATTRIBUTES	8
198372fea694SJean Delvare #endif
198472fea694SJean Delvare 
w83795_handle_files(struct device * dev,int (* fn)(struct device *,const struct device_attribute *))19856f3dcde9SJean Delvare static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
19866f3dcde9SJean Delvare 			       const struct device_attribute *))
1987892514a6SJean Delvare {
1988892514a6SJean Delvare 	struct w83795_data *data = dev_get_drvdata(dev);
198987df0dadSJean Delvare 	int err, i, j;
1990892514a6SJean Delvare 
1991892514a6SJean Delvare 	for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
199287df0dadSJean Delvare 		if (!(data->has_in & (1 << i)))
1993892514a6SJean Delvare 			continue;
199487df0dadSJean Delvare 		for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
199552d159eeSJean Delvare 			if (j == 4 && !data->enable_beep)
199652d159eeSJean Delvare 				continue;
199787df0dadSJean Delvare 			err = fn(dev, &w83795_in[i][j].dev_attr);
1998892514a6SJean Delvare 			if (err)
1999892514a6SJean Delvare 				return err;
2000892514a6SJean Delvare 		}
200187df0dadSJean Delvare 	}
2002892514a6SJean Delvare 
2003892514a6SJean Delvare 	for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
200487df0dadSJean Delvare 		if (!(data->has_fan & (1 << i)))
2005892514a6SJean Delvare 			continue;
200687df0dadSJean Delvare 		for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
200752d159eeSJean Delvare 			if (j == 3 && !data->enable_beep)
200852d159eeSJean Delvare 				continue;
200987df0dadSJean Delvare 			err = fn(dev, &w83795_fan[i][j].dev_attr);
2010892514a6SJean Delvare 			if (err)
2011892514a6SJean Delvare 				return err;
2012892514a6SJean Delvare 		}
201387df0dadSJean Delvare 	}
2014892514a6SJean Delvare 
2015edff2f8dSJean Delvare 	for (i = 0; i < ARRAY_SIZE(w83795_tss); i++) {
2016edff2f8dSJean Delvare 		j = w83795_tss_useful(data, i);
2017edff2f8dSJean Delvare 		if (!j)
2018edff2f8dSJean Delvare 			continue;
2019edff2f8dSJean Delvare 		err = fn(dev, &w83795_tss[i].dev_attr);
2020edff2f8dSJean Delvare 		if (err)
2021edff2f8dSJean Delvare 			return err;
2022edff2f8dSJean Delvare 	}
2023edff2f8dSJean Delvare 
2024892514a6SJean Delvare 	for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
20256f3dcde9SJean Delvare 		err = fn(dev, &sda_single_files[i].dev_attr);
2026892514a6SJean Delvare 		if (err)
2027892514a6SJean Delvare 			return err;
2028892514a6SJean Delvare 	}
2029892514a6SJean Delvare 
203052d159eeSJean Delvare 	if (data->enable_beep) {
203152d159eeSJean Delvare 		for (i = 0; i < ARRAY_SIZE(sda_beep_files); i++) {
203252d159eeSJean Delvare 			err = fn(dev, &sda_beep_files[i].dev_attr);
203352d159eeSJean Delvare 			if (err)
203452d159eeSJean Delvare 				return err;
203552d159eeSJean Delvare 		}
203652d159eeSJean Delvare 	}
203752d159eeSJean Delvare 
2038b5f6a90aSJean Delvare 	for (i = 0; i < data->has_pwm; i++) {
203972fea694SJean Delvare 		for (j = 0; j < NUM_PWM_ATTRIBUTES; j++) {
2040b5f6a90aSJean Delvare 			err = fn(dev, &w83795_pwm[i][j].dev_attr);
2041892514a6SJean Delvare 			if (err)
2042892514a6SJean Delvare 				return err;
2043892514a6SJean Delvare 		}
2044892514a6SJean Delvare 	}
2045892514a6SJean Delvare 
2046892514a6SJean Delvare 	for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
204787df0dadSJean Delvare 		if (!(data->has_temp & (1 << i)))
2048892514a6SJean Delvare 			continue;
204972fea694SJean Delvare 		for (j = 0; j < NUM_TEMP_ATTRIBUTES; j++) {
205052d159eeSJean Delvare 			if (j == 7 && !data->enable_beep)
205152d159eeSJean Delvare 				continue;
205287df0dadSJean Delvare 			err = fn(dev, &w83795_temp[i][j].dev_attr);
2053892514a6SJean Delvare 			if (err)
2054892514a6SJean Delvare 				return err;
2055892514a6SJean Delvare 		}
205687df0dadSJean Delvare 	}
2057892514a6SJean Delvare 
2058eb02755aSJean Delvare 	if (data->enable_dts) {
2059892514a6SJean Delvare 		for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
206087df0dadSJean Delvare 			if (!(data->has_dts & (1 << i)))
2061892514a6SJean Delvare 				continue;
206287df0dadSJean Delvare 			for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
206352d159eeSJean Delvare 				if (j == 7 && !data->enable_beep)
206452d159eeSJean Delvare 					continue;
206587df0dadSJean Delvare 				err = fn(dev, &w83795_dts[i][j].dev_attr);
2066892514a6SJean Delvare 				if (err)
2067892514a6SJean Delvare 					return err;
2068892514a6SJean Delvare 			}
2069892514a6SJean Delvare 		}
207087df0dadSJean Delvare 	}
2071892514a6SJean Delvare 
2072892514a6SJean Delvare 	return 0;
2073892514a6SJean Delvare }
2074892514a6SJean Delvare 
20756f3dcde9SJean Delvare /* We need a wrapper that fits in w83795_handle_files */
device_remove_file_wrapper(struct device * dev,const struct device_attribute * attr)20766f3dcde9SJean Delvare static int device_remove_file_wrapper(struct device *dev,
20776f3dcde9SJean Delvare 				      const struct device_attribute *attr)
20782fa09878SJean Delvare {
20796f3dcde9SJean Delvare 	device_remove_file(dev, attr);
20806f3dcde9SJean Delvare 	return 0;
20812fa09878SJean Delvare }
20822fa09878SJean Delvare 
w83795_check_dynamic_in_limits(struct i2c_client * client)20830e256018SJean Delvare static void w83795_check_dynamic_in_limits(struct i2c_client *client)
20840e256018SJean Delvare {
20850e256018SJean Delvare 	struct w83795_data *data = i2c_get_clientdata(client);
20860e256018SJean Delvare 	u8 vid_ctl;
20870e256018SJean Delvare 	int i, err_max, err_min;
20880e256018SJean Delvare 
20890e256018SJean Delvare 	vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
20900e256018SJean Delvare 
20910e256018SJean Delvare 	/* Return immediately if VRM isn't configured */
20920e256018SJean Delvare 	if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
20930e256018SJean Delvare 		return;
20940e256018SJean Delvare 
20950e256018SJean Delvare 	data->has_dyn_in = (vid_ctl >> 3) & 0x07;
20960e256018SJean Delvare 	for (i = 0; i < 2; i++) {
20970e256018SJean Delvare 		if (!(data->has_dyn_in & (1 << i)))
20980e256018SJean Delvare 			continue;
20990e256018SJean Delvare 
21000e256018SJean Delvare 		/* Voltage limits in dynamic mode, switch to read-only */
21010e256018SJean Delvare 		err_max = sysfs_chmod_file(&client->dev.kobj,
21020e256018SJean Delvare 					   &w83795_in[i][2].dev_attr.attr,
21030e256018SJean Delvare 					   S_IRUGO);
21040e256018SJean Delvare 		err_min = sysfs_chmod_file(&client->dev.kobj,
21050e256018SJean Delvare 					   &w83795_in[i][3].dev_attr.attr,
21060e256018SJean Delvare 					   S_IRUGO);
21070e256018SJean Delvare 		if (err_max || err_min)
2108b55f3757SGuenter Roeck 			dev_warn(&client->dev,
2109b55f3757SGuenter Roeck 				 "Failed to set in%d limits read-only (%d, %d)\n",
2110b55f3757SGuenter Roeck 				 i, err_max, err_min);
21110e256018SJean Delvare 		else
2112b55f3757SGuenter Roeck 			dev_info(&client->dev,
2113b55f3757SGuenter Roeck 				 "in%d limits set dynamically from VID\n", i);
21140e256018SJean Delvare 	}
21150e256018SJean Delvare }
21160e256018SJean Delvare 
211771caf46fSJean Delvare /* Check pins that can be used for either temperature or voltage monitoring */
w83795_apply_temp_config(struct w83795_data * data,u8 config,int temp_chan,int in_chan)211871caf46fSJean Delvare static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
211971caf46fSJean Delvare 				     int temp_chan, int in_chan)
212071caf46fSJean Delvare {
212171caf46fSJean Delvare 	/* config is a 2-bit value */
212271caf46fSJean Delvare 	switch (config) {
212371caf46fSJean Delvare 	case 0x2: /* Voltage monitoring */
212471caf46fSJean Delvare 		data->has_in |= 1 << in_chan;
212571caf46fSJean Delvare 		break;
212671caf46fSJean Delvare 	case 0x1: /* Thermal diode */
212771caf46fSJean Delvare 		if (temp_chan >= 4)
212871caf46fSJean Delvare 			break;
212971caf46fSJean Delvare 		data->temp_mode |= 1 << temp_chan;
2130df561f66SGustavo A. R. Silva 		fallthrough;
213171caf46fSJean Delvare 	case 0x3: /* Thermistor */
213271caf46fSJean Delvare 		data->has_temp |= 1 << temp_chan;
213371caf46fSJean Delvare 		break;
213471caf46fSJean Delvare 	}
213571caf46fSJean Delvare }
213671caf46fSJean Delvare 
2137673afe46SStephen Kitt static const struct i2c_device_id w83795_id[];
2138673afe46SStephen Kitt 
w83795_probe(struct i2c_client * client)2139673afe46SStephen Kitt static int w83795_probe(struct i2c_client *client)
2140792d376bSWei Song {
2141792d376bSWei Song 	int i;
2142792d376bSWei Song 	u8 tmp;
2143792d376bSWei Song 	struct device *dev = &client->dev;
2144792d376bSWei Song 	struct w83795_data *data;
214571caf46fSJean Delvare 	int err;
2146792d376bSWei Song 
21474cb14a3aSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct w83795_data), GFP_KERNEL);
21484cb14a3aSGuenter Roeck 	if (!data)
21494cb14a3aSGuenter Roeck 		return -ENOMEM;
2150792d376bSWei Song 
2151792d376bSWei Song 	i2c_set_clientdata(client, data);
2152673afe46SStephen Kitt 	data->chip_type = i2c_match_id(w83795_id, client)->driver_data;
2153792d376bSWei Song 	data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
2154792d376bSWei Song 	mutex_init(&data->update_lock);
2155792d376bSWei Song 
2156792d376bSWei Song 	/* Initialize the chip */
2157792d376bSWei Song 	w83795_init_client(client);
2158792d376bSWei Song 
215971caf46fSJean Delvare 	/* Check which voltages and fans are present */
216071caf46fSJean Delvare 	data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
216171caf46fSJean Delvare 		     | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
216271caf46fSJean Delvare 	data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
216371caf46fSJean Delvare 		      | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
2164792d376bSWei Song 
216571caf46fSJean Delvare 	/* Check which analog temperatures and extra voltages are present */
2166792d376bSWei Song 	tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
2167792d376bSWei Song 	if (tmp & 0x20)
2168792d376bSWei Song 		data->enable_dts = 1;
216971caf46fSJean Delvare 	w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
217071caf46fSJean Delvare 	w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
2171792d376bSWei Song 	tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
217271caf46fSJean Delvare 	w83795_apply_temp_config(data, tmp >> 6, 3, 20);
217371caf46fSJean Delvare 	w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
217471caf46fSJean Delvare 	w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
217571caf46fSJean Delvare 	w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
2176792d376bSWei Song 
2177792d376bSWei Song 	/* Check DTS enable status */
217871caf46fSJean Delvare 	if (data->enable_dts) {
2179792d376bSWei Song 		if (1 & w83795_read(client, W83795_REG_DTSC))
2180792d376bSWei Song 			data->enable_dts |= 2;
2181792d376bSWei Song 		data->has_dts = w83795_read(client, W83795_REG_DTSE);
2182792d376bSWei Song 	}
2183792d376bSWei Song 
218454891a3cSJean Delvare 	/* Report PECI Tbase values */
218554891a3cSJean Delvare 	if (data->enable_dts == 1) {
218654891a3cSJean Delvare 		for (i = 0; i < 8; i++) {
218754891a3cSJean Delvare 			if (!(data->has_dts & (1 << i)))
218854891a3cSJean Delvare 				continue;
218954891a3cSJean Delvare 			tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
219054891a3cSJean Delvare 			dev_info(&client->dev,
219154891a3cSJean Delvare 				 "PECI agent %d Tbase temperature: %u\n",
219254891a3cSJean Delvare 				 i + 1, (unsigned int)tmp & 0x7f);
219354891a3cSJean Delvare 		}
219454891a3cSJean Delvare 	}
219554891a3cSJean Delvare 
2196792d376bSWei Song 	data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
2197792d376bSWei Song 
2198792d376bSWei Song 	/* pwm and smart fan */
2199792d376bSWei Song 	if (data->chip_type == w83795g)
2200792d376bSWei Song 		data->has_pwm = 8;
2201792d376bSWei Song 	else
2202792d376bSWei Song 		data->has_pwm = 2;
2203792d376bSWei Song 
220452d159eeSJean Delvare 	/* Check if BEEP pin is available */
220552d159eeSJean Delvare 	if (data->chip_type == w83795g) {
220652d159eeSJean Delvare 		/* The W83795G has a dedicated BEEP pin */
220752d159eeSJean Delvare 		data->enable_beep = 1;
220852d159eeSJean Delvare 	} else {
2209c10b3ee8SGuenter Roeck 		/*
2210c10b3ee8SGuenter Roeck 		 * The W83795ADG has a shared pin for OVT# and BEEP, so you
2211c10b3ee8SGuenter Roeck 		 * can't have both
2212c10b3ee8SGuenter Roeck 		 */
221352d159eeSJean Delvare 		tmp = w83795_read(client, W83795_REG_OVT_CFG);
221452d159eeSJean Delvare 		if ((tmp & OVT_CFG_SEL) == 0)
221552d159eeSJean Delvare 			data->enable_beep = 1;
221652d159eeSJean Delvare 	}
221752d159eeSJean Delvare 
22186f3dcde9SJean Delvare 	err = w83795_handle_files(dev, device_create_file);
2219792d376bSWei Song 	if (err)
2220792d376bSWei Song 		goto exit_remove;
2221792d376bSWei Song 
22220e256018SJean Delvare 	if (data->chip_type == w83795g)
22230e256018SJean Delvare 		w83795_check_dynamic_in_limits(client);
22240e256018SJean Delvare 
2225792d376bSWei Song 	data->hwmon_dev = hwmon_device_register(dev);
2226792d376bSWei Song 	if (IS_ERR(data->hwmon_dev)) {
2227792d376bSWei Song 		err = PTR_ERR(data->hwmon_dev);
2228792d376bSWei Song 		goto exit_remove;
2229792d376bSWei Song 	}
2230792d376bSWei Song 
2231792d376bSWei Song 	return 0;
2232792d376bSWei Song 
2233792d376bSWei Song exit_remove:
22346f3dcde9SJean Delvare 	w83795_handle_files(dev, device_remove_file_wrapper);
2235792d376bSWei Song 	return err;
2236792d376bSWei Song }
2237792d376bSWei Song 
w83795_remove(struct i2c_client * client)2238ed5c2f5fSUwe Kleine-König static void w83795_remove(struct i2c_client *client)
2239792d376bSWei Song {
2240792d376bSWei Song 	struct w83795_data *data = i2c_get_clientdata(client);
2241792d376bSWei Song 
2242792d376bSWei Song 	hwmon_device_unregister(data->hwmon_dev);
22436f3dcde9SJean Delvare 	w83795_handle_files(&client->dev, device_remove_file_wrapper);
2244792d376bSWei Song }
2245792d376bSWei Song 
2246792d376bSWei Song 
2247792d376bSWei Song static const struct i2c_device_id w83795_id[] = {
2248093d1a47SJean Delvare 	{ "w83795g", w83795g },
2249093d1a47SJean Delvare 	{ "w83795adg", w83795adg },
2250792d376bSWei Song 	{ }
2251792d376bSWei Song };
2252792d376bSWei Song MODULE_DEVICE_TABLE(i2c, w83795_id);
2253792d376bSWei Song 
2254792d376bSWei Song static struct i2c_driver w83795_driver = {
2255792d376bSWei Song 	.driver = {
2256792d376bSWei Song 		   .name = "w83795",
2257792d376bSWei Song 	},
2258*1975d167SUwe Kleine-König 	.probe		= w83795_probe,
2259792d376bSWei Song 	.remove		= w83795_remove,
2260792d376bSWei Song 	.id_table	= w83795_id,
2261792d376bSWei Song 
2262792d376bSWei Song 	.class		= I2C_CLASS_HWMON,
2263792d376bSWei Song 	.detect		= w83795_detect,
2264792d376bSWei Song 	.address_list	= normal_i2c,
2265792d376bSWei Song };
2266792d376bSWei Song 
2267f0967eeaSAxel Lin module_i2c_driver(w83795_driver);
2268792d376bSWei Song 
22697c81c60fSJean Delvare MODULE_AUTHOR("Wei Song, Jean Delvare <jdelvare@suse.de>");
2270315bacfdSJean Delvare MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
2271792d376bSWei Song MODULE_LICENSE("GPL");
2272