Blame view

drivers/hwmon/w83793.c 59 KB
ff7924b02   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2
  /*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
3
4
5
6
7
8
9
10
   * w83793.c - Linux kernel driver for hardware monitoring
   * Copyright (C) 2006 Winbond Electronics Corp.
   *	      Yuan Mu
   *	      Rudolf Marek <r.marek@assembler.cz>
   * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
   *		Watchdog driver part
   *		(Based partially on fschmd driver,
   *		 Copyright 2007-2008 by Hans de Goede)
47efe8772   Guenter Roeck   hwmon: (w83793) F...
11
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
12
13
  
  /*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
14
15
16
17
18
   * Supports following chips:
   *
   * Chip	#vin	#fanin	#pwm	#temp	wchipid	vendid	i2c	ISA
   * w83793	10	12	8	6	0x7b	0x5ca3	yes	no
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
19
20
21
22
23
24
25
26
27
28
  
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/i2c.h>
  #include <linux/hwmon.h>
  #include <linux/hwmon-vid.h>
  #include <linux/hwmon-sysfs.h>
  #include <linux/err.h>
  #include <linux/mutex.h>
5852f9609   Sven Anders   hwmon: (w83793) A...
29
30
31
32
33
34
35
  #include <linux/fs.h>
  #include <linux/watchdog.h>
  #include <linux/miscdevice.h>
  #include <linux/uaccess.h>
  #include <linux/kref.h>
  #include <linux/notifier.h>
  #include <linux/reboot.h>
dcd8f3923   Jean Delvare   hwmon: Add missin...
36
  #include <linux/jiffies.h>
5852f9609   Sven Anders   hwmon: (w83793) A...
37
38
39
  
  /* Default values */
  #define WATCHDOG_TIMEOUT 2	/* 2 minute default timeout */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
40
41
  
  /* Addresses to scan */
25e9c86d5   Mark M. Hoffman   hwmon: normal_i2c...
42
43
  static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  						I2C_CLIENT_END };
6800c3d02   Rudolf Marek   hwmon: New Winbon...
44
45
  
  /* Insmod parameters */
3aed198c3   Jean Delvare   hwmon: Don't over...
46
47
48
  
  static unsigned short force_subclients[4];
  module_param_array(force_subclients, short, NULL, 0);
b55f37572   Guenter Roeck   hwmon: Fix checkp...
49
50
  MODULE_PARM_DESC(force_subclients,
  		 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
6800c3d02   Rudolf Marek   hwmon: New Winbon...
51

90ab5ee94   Rusty Russell   module_param: mak...
52
  static bool reset;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
53
54
  module_param(reset, bool, 0);
  MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
5852f9609   Sven Anders   hwmon: (w83793) A...
55
56
57
58
59
  static int timeout = WATCHDOG_TIMEOUT;	/* default timeout in minutes */
  module_param(timeout, int, 0);
  MODULE_PARM_DESC(timeout,
  	"Watchdog timeout in minutes. 2<= timeout <=255 (default="
  				__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
86a1e1896   Wim Van Sebroeck   watchdog: nowayou...
60
61
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0);
5852f9609   Sven Anders   hwmon: (w83793) A...
62
63
64
  MODULE_PARM_DESC(nowayout,
  	"Watchdog cannot be stopped once started (default="
  				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
6800c3d02   Rudolf Marek   hwmon: New Winbon...
65
  /*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
66
67
68
   * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
   * as ID, Bank Select registers
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #define W83793_REG_BANKSEL		0x00
  #define W83793_REG_VENDORID		0x0d
  #define W83793_REG_CHIPID		0x0e
  #define W83793_REG_DEVICEID		0x0f
  
  #define W83793_REG_CONFIG		0x40
  #define W83793_REG_MFC			0x58
  #define W83793_REG_FANIN_CTRL		0x5c
  #define W83793_REG_FANIN_SEL		0x5d
  #define W83793_REG_I2C_ADDR		0x0b
  #define W83793_REG_I2C_SUBADDR		0x0c
  #define W83793_REG_VID_INA		0x05
  #define W83793_REG_VID_INB		0x06
  #define W83793_REG_VID_LATCHA		0x07
  #define W83793_REG_VID_LATCHB		0x08
  #define W83793_REG_VID_CTRL		0x59
5852f9609   Sven Anders   hwmon: (w83793) A...
85
86
87
88
  #define W83793_REG_WDT_LOCK		0x01
  #define W83793_REG_WDT_ENABLE		0x02
  #define W83793_REG_WDT_STATUS		0x03
  #define W83793_REG_WDT_TIMEOUT		0x04
6800c3d02   Rudolf Marek   hwmon: New Winbon...
89
90
91
92
93
94
95
  static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
  
  #define TEMP_READ	0
  #define TEMP_CRIT	1
  #define TEMP_CRIT_HYST	2
  #define TEMP_WARN	3
  #define TEMP_WARN_HYST	4
47efe8772   Guenter Roeck   hwmon: (w83793) F...
96
97
98
99
  /*
   * only crit and crit_hyst affect real-time alarm status
   * current crit crit_hyst warn warn_hyst
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  static u16 W83793_REG_TEMP[][5] = {
  	{0x1c, 0x78, 0x79, 0x7a, 0x7b},
  	{0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
  	{0x1e, 0x80, 0x81, 0x82, 0x83},
  	{0x1f, 0x84, 0x85, 0x86, 0x87},
  	{0x20, 0x88, 0x89, 0x8a, 0x8b},
  	{0x21, 0x8c, 0x8d, 0x8e, 0x8f},
  };
  
  #define W83793_REG_TEMP_LOW_BITS	0x22
  
  #define W83793_REG_BEEP(index)		(0x53 + (index))
  #define W83793_REG_ALARM(index)		(0x4b + (index))
  
  #define W83793_REG_CLR_CHASSIS		0x4a	/* SMI MASK4 */
  #define W83793_REG_IRQ_CTRL		0x50
  #define W83793_REG_OVT_CTRL		0x51
  #define W83793_REG_OVT_BEEP		0x52
  
  #define IN_READ				0
  #define IN_MAX				1
  #define IN_LOW				2
  static const u16 W83793_REG_IN[][3] = {
  	/* Current, High, Low */
  	{0x10, 0x60, 0x61},	/* Vcore A	*/
  	{0x11, 0x62, 0x63},	/* Vcore B	*/
  	{0x12, 0x64, 0x65},	/* Vtt		*/
  	{0x14, 0x6a, 0x6b},	/* VSEN1	*/
  	{0x15, 0x6c, 0x6d},	/* VSEN2	*/
  	{0x16, 0x6e, 0x6f},	/* +3VSEN	*/
  	{0x17, 0x70, 0x71},	/* +12VSEN	*/
  	{0x18, 0x72, 0x73},	/* 5VDD		*/
  	{0x19, 0x74, 0x75},	/* 5VSB		*/
  	{0x1a, 0x76, 0x77},	/* VBAT		*/
  };
  
  /* Low Bits of Vcore A/B Vtt Read/High/Low */
  static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
  static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
ddca933bd   Gong Jun   hwmon/w83793: Rem...
139
  static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
6800c3d02   Rudolf Marek   hwmon: New Winbon...
140
141
142
143
144
145
146
147
148
149
150
151
152
  
  #define W83793_REG_FAN(index)		(0x23 + 2 * (index))	/* High byte */
  #define W83793_REG_FAN_MIN(index)	(0x90 + 2 * (index))	/* High byte */
  
  #define W83793_REG_PWM_DEFAULT		0xb2
  #define W83793_REG_PWM_ENABLE		0x207
  #define W83793_REG_PWM_UPTIME		0xc3	/* Unit in 0.1 second */
  #define W83793_REG_PWM_DOWNTIME		0xc4	/* Unit in 0.1 second */
  #define W83793_REG_TEMP_CRITICAL	0xc5
  
  #define PWM_DUTY			0
  #define PWM_START			1
  #define PWM_NONSTOP			2
5aebefb08   Nicolas Kaiser   hwmon: (w83793) r...
153
  #define PWM_STOP_TIME			3
6800c3d02   Rudolf Marek   hwmon: New Winbon...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  #define W83793_REG_PWM(index, nr)	(((nr) == 0 ? 0xb3 : \
  					 (nr) == 1 ? 0x220 : 0x218) + (index))
  
  /* bit field, fan1 is bit0, fan2 is bit1 ... */
  #define W83793_REG_TEMP_FAN_MAP(index)	(0x201 + (index))
  #define W83793_REG_TEMP_TOL(index)	(0x208 + (index))
  #define W83793_REG_TEMP_CRUISE(index)	(0x210 + (index))
  #define W83793_REG_PWM_STOP_TIME(index)	(0x228 + (index))
  #define W83793_REG_SF2_TEMP(index, nr)	(0x230 + ((index) << 4) + (nr))
  #define W83793_REG_SF2_PWM(index, nr)	(0x238 + ((index) << 4) + (nr))
  
  static inline unsigned long FAN_FROM_REG(u16 val)
  {
  	if ((val >= 0xfff) || (val == 0))
  		return	0;
7fe83ad87   Frans Meulenbroeks   hwmon: remove () ...
169
  	return 1350000UL / val;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
170
171
172
173
174
175
  }
  
  static inline u16 FAN_TO_REG(long rpm)
  {
  	if (rpm <= 0)
  		return 0x0fff;
2a844c148   Guenter Roeck   hwmon: Replace SE...
176
  	return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
177
178
179
180
  }
  
  static inline unsigned long TIME_FROM_REG(u8 reg)
  {
7fe83ad87   Frans Meulenbroeks   hwmon: remove () ...
181
  	return reg * 100;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
182
183
184
185
  }
  
  static inline u8 TIME_TO_REG(unsigned long val)
  {
2a844c148   Guenter Roeck   hwmon: Replace SE...
186
  	return clamp_val((val + 50) / 100, 0, 0xff);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
187
188
189
190
  }
  
  static inline long TEMP_FROM_REG(s8 reg)
  {
7fe83ad87   Frans Meulenbroeks   hwmon: remove () ...
191
  	return reg * 1000;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
192
193
194
195
  }
  
  static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
  {
2a844c148   Guenter Roeck   hwmon: Replace SE...
196
  	return clamp_val((val + (val < 0 ? -500 : 500)) / 1000, min, max);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
197
198
199
  }
  
  struct w83793_data {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
200
  	struct i2c_client *lm75[2];
1beeffe43   Tony Jones   hwmon: Convert fr...
201
  	struct device *hwmon_dev;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
202
203
204
205
  	struct mutex update_lock;
  	char valid;			/* !=0 if following fields are valid */
  	unsigned long last_updated;	/* In jiffies */
  	unsigned long last_nonvolatile;	/* In jiffies, last time we update the
47efe8772   Guenter Roeck   hwmon: (w83793) F...
206
207
  					 * nonvolatile registers
  					 */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  
  	u8 bank;
  	u8 vrm;
  	u8 vid[2];
  	u8 in[10][3];		/* Register value, read/high/low */
  	u8 in_low_bits[3];	/* Additional resolution for VCore A/B Vtt */
  
  	u16 has_fan;		/* Only fan1- fan5 has own pins */
  	u16 fan[12];		/* Register value combine */
  	u16 fan_min[12];	/* Register value combine */
  
  	s8 temp[6][5];		/* current, crit, crit_hyst,warn, warn_hyst */
  	u8 temp_low_bits;	/* Additional resolution TD1-TD4 */
  	u8 temp_mode[2];	/* byte 0: Temp D1-D4 mode each has 2 bits
47efe8772   Guenter Roeck   hwmon: (w83793) F...
222
223
  				 * byte 1: Temp R1,R2 mode, each has 1 bit
  				 */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
224
225
226
227
  	u8 temp_critical;	/* If reached all fan will be at full speed */
  	u8 temp_fan_map[6];	/* Temp controls which pwm fan, bit field */
  
  	u8 has_pwm;
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
228
  	u8 has_temp;
c70a8c345   Gong Jun   hwmon/w83793: Hid...
229
  	u8 has_vid;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  	u8 pwm_enable;		/* Register value, each Temp has 1 bit */
  	u8 pwm_uptime;		/* Register value */
  	u8 pwm_downtime;	/* Register value */
  	u8 pwm_default;		/* All fan default pwm, next poweron valid */
  	u8 pwm[8][3];		/* Register value */
  	u8 pwm_stop_time[8];
  	u8 temp_cruise[6];
  
  	u8 alarms[5];		/* realtime status registers */
  	u8 beeps[5];
  	u8 beep_enable;
  	u8 tolerance[3];	/* Temp tolerance(Smart Fan I/II) */
  	u8 sf2_pwm[6][7];	/* Smart FanII: Fan duty cycle */
  	u8 sf2_temp[6][7];	/* Smart FanII: Temp level point */
5852f9609   Sven Anders   hwmon: (w83793) A...
244
245
246
247
248
249
250
251
252
253
254
255
  
  	/* watchdog */
  	struct i2c_client *client;
  	struct mutex watchdog_lock;
  	struct list_head list; /* member of the watchdog_data_list */
  	struct kref kref;
  	struct miscdevice watchdog_miscdev;
  	unsigned long watchdog_is_open;
  	char watchdog_expect_close;
  	char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
  	unsigned int watchdog_caused_reboot;
  	int watchdog_timeout; /* watchdog timeout in minutes */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
256
  };
47efe8772   Guenter Roeck   hwmon: (w83793) F...
257
258
259
260
261
262
  /*
   * Somewhat ugly :( global data pointer list with all devices, so that
   * we can find our device data as when using misc_register. There is no
   * other method to get to one's device data from the open file-op and
   * for usage in the reboot notifier callback.
   */
5852f9609   Sven Anders   hwmon: (w83793) A...
263
264
265
266
  static LIST_HEAD(watchdog_data_list);
  
  /* Note this lock not only protect list access, but also data.kref access */
  static DEFINE_MUTEX(watchdog_data_mutex);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
267
268
269
270
  /*
   * Release our data struct when we're detached from the i2c client *and* all
   * references to our watchdog device are released
   */
5852f9609   Sven Anders   hwmon: (w83793) A...
271
272
273
274
275
  static void w83793_release_resources(struct kref *ref)
  {
  	struct w83793_data *data = container_of(ref, struct w83793_data, kref);
  	kfree(data);
  }
6800c3d02   Rudolf Marek   hwmon: New Winbon...
276
277
  static u8 w83793_read_value(struct i2c_client *client, u16 reg);
  static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
16b237f5e   Stephen Kitt   hwmon: (w83793) u...
278
  static int w83793_probe(struct i2c_client *client);
310ec7921   Jean Delvare   i2c: Drop the kin...
279
  static int w83793_detect(struct i2c_client *client,
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
280
281
  			 struct i2c_board_info *info);
  static int w83793_remove(struct i2c_client *client);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
282
283
284
  static void w83793_init_client(struct i2c_client *client);
  static void w83793_update_nonvolatile(struct device *dev);
  static struct w83793_data *w83793_update_device(struct device *dev);
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
285
  static const struct i2c_device_id w83793_id[] = {
1f86df49d   Jean Delvare   i2c: Drop I2C_CLI...
286
  	{ "w83793", 0 },
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
287
288
289
  	{ }
  };
  MODULE_DEVICE_TABLE(i2c, w83793_id);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
290
  static struct i2c_driver w83793_driver = {
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
291
  	.class		= I2C_CLASS_HWMON,
6800c3d02   Rudolf Marek   hwmon: New Winbon...
292
293
294
  	.driver = {
  		   .name = "w83793",
  	},
16b237f5e   Stephen Kitt   hwmon: (w83793) u...
295
  	.probe_new	= w83793_probe,
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
296
297
298
  	.remove		= w83793_remove,
  	.id_table	= w83793_id,
  	.detect		= w83793_detect,
c3813d6af   Jean Delvare   i2c: Get rid of s...
299
  	.address_list	= normal_i2c,
6800c3d02   Rudolf Marek   hwmon: New Winbon...
300
301
302
  };
  
  static ssize_t
329beb71f   Julia Lawall   hwmon: (w83793) u...
303
  vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
304
  {
8f74efe81   Jean Delvare   hwmon: VRM is not...
305
  	struct w83793_data *data = dev_get_drvdata(dev);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  	return sprintf(buf, "%d
  ", data->vrm);
  }
  
  static ssize_t
  show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct w83793_data *data = w83793_update_device(dev);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int index = sensor_attr->index;
  
  	return sprintf(buf, "%d
  ", vid_from_reg(data->vid[index], data->vrm));
  }
  
  static ssize_t
329beb71f   Julia Lawall   hwmon: (w83793) u...
323
  vrm_store(struct device *dev, struct device_attribute *attr,
6800c3d02   Rudolf Marek   hwmon: New Winbon...
324
325
  	  const char *buf, size_t count)
  {
8f74efe81   Jean Delvare   hwmon: VRM is not...
326
  	struct w83793_data *data = dev_get_drvdata(dev);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
327
328
329
330
331
332
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
2aeee04df   Axel Lin   hwmon: (w83793) F...
333
334
  	if (val > 255)
  		return -EINVAL;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
335
  	data->vrm = val;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  	return count;
  }
  
  #define ALARM_STATUS			0
  #define BEEP_ENABLE			1
  static ssize_t
  show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct w83793_data *data = w83793_update_device(dev);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index >> 3;
  	int bit = sensor_attr->index & 0x07;
  	u8 val;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
351
  	if (nr == ALARM_STATUS) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  		val = (data->alarms[index] >> (bit)) & 1;
  	} else {		/* BEEP_ENABLE */
  		val = (data->beeps[index] >> (bit)) & 1;
  	}
  
  	return sprintf(buf, "%u
  ", val);
  }
  
  static ssize_t
  store_beep(struct device *dev, struct device_attribute *attr,
  	   const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int index = sensor_attr->index >> 3;
  	int shift = sensor_attr->index & 0x07;
  	u8 beep_bit = 1 << shift;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
372
373
  	unsigned long val;
  	int err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
374

47efe8772   Guenter Roeck   hwmon: (w83793) F...
375
376
377
378
379
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
  
  	if (val > 1)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
  	data->beeps[index] &= ~beep_bit;
  	data->beeps[index] |= val << shift;
  	w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  static ssize_t
  show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct w83793_data *data = w83793_update_device(dev);
  	return sprintf(buf, "%u
  ", (data->beep_enable >> 1) & 0x01);
  }
  
  static ssize_t
  store_beep_enable(struct device *dev, struct device_attribute *attr,
  		  const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
406
407
408
409
410
411
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
412

47efe8772   Guenter Roeck   hwmon: (w83793) F...
413
  	if (val > 1)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
414
415
416
417
418
419
420
421
422
423
424
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
  			    & 0xfd;
  	data->beep_enable |= val << 1;
  	w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
a516dc3e9   Jean Delvare   hwmon: (w83793) I...
425
426
427
428
429
430
431
432
433
434
  /* Write 0 to clear chassis alarm */
  static ssize_t
  store_chassis_clear(struct device *dev,
  		    struct device_attribute *attr, const char *buf,
  		    size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	unsigned long val;
  	u8 reg;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
435
  	int err;
a516dc3e9   Jean Delvare   hwmon: (w83793) I...
436

47efe8772   Guenter Roeck   hwmon: (w83793) F...
437
438
439
440
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
  	if (val)
a516dc3e9   Jean Delvare   hwmon: (w83793) I...
441
442
443
444
445
446
447
448
449
  		return -EINVAL;
  
  	mutex_lock(&data->update_lock);
  	reg = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
  	w83793_write_value(client, W83793_REG_CLR_CHASSIS, reg | 0x80);
  	data->valid = 0;		/* Force cache refresh */
  	mutex_unlock(&data->update_lock);
  	return count;
  }
6800c3d02   Rudolf Marek   hwmon: New Winbon...
450
451
452
453
454
455
456
457
458
459
460
  #define FAN_INPUT			0
  #define FAN_MIN				1
  static ssize_t
  show_fan(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct w83793_data *data = w83793_update_device(dev);
  	u16 val;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
461
  	if (nr == FAN_INPUT)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
462
  		val = data->fan[index] & 0x0fff;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
463
  	else
6800c3d02   Rudolf Marek   hwmon: New Winbon...
464
  		val = data->fan_min[index] & 0x0fff;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  
  	return sprintf(buf, "%lu
  ", FAN_FROM_REG(val));
  }
  
  static ssize_t
  store_fan_min(struct device *dev, struct device_attribute *attr,
  	      const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int index = sensor_attr->index;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
479
480
481
482
483
484
485
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
  	val = FAN_TO_REG(val);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
486
487
488
489
490
491
492
493
494
495
  
  	mutex_lock(&data->update_lock);
  	data->fan_min[index] = val;
  	w83793_write_value(client, W83793_REG_FAN_MIN(index),
  			   (val >> 8) & 0xff);
  	w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
6800c3d02   Rudolf Marek   hwmon: New Winbon...
496
497
498
499
500
501
502
503
504
  static ssize_t
  show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	struct w83793_data *data = w83793_update_device(dev);
  	u16 val;
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
505
  	if (nr == PWM_STOP_TIME)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  		val = TIME_FROM_REG(data->pwm_stop_time[index]);
  	else
  		val = (data->pwm[index][nr] & 0x3f) << 2;
  
  	return sprintf(buf, "%d
  ", val);
  }
  
  static ssize_t
  store_pwm(struct device *dev, struct device_attribute *attr,
  	  const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
524
525
526
527
528
529
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
530
531
  
  	mutex_lock(&data->update_lock);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
532
533
  	if (nr == PWM_STOP_TIME) {
  		val = TIME_TO_REG(val);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
534
535
536
537
  		data->pwm_stop_time[index] = val;
  		w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
  				   val);
  	} else {
2a844c148   Guenter Roeck   hwmon: Replace SE...
538
  		val = clamp_val(val, 0, 0xff) >> 2;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  		data->pwm[index][nr] =
  		    w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
  		data->pwm[index][nr] |= val;
  		w83793_write_value(client, W83793_REG_PWM(index, nr),
  							data->pwm[index][nr]);
  	}
  
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  static ssize_t
  show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct w83793_data *data = w83793_update_device(dev);
  	long temp = TEMP_FROM_REG(data->temp[index][nr]);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
559
  	if (nr == TEMP_READ && index < 4) {	/* Only TD1-TD4 have low bits */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  		int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
  		temp += temp > 0 ? low : -low;
  	}
  	return sprintf(buf, "%ld
  ", temp);
  }
  
  static ssize_t
  store_temp(struct device *dev, struct device_attribute *attr,
  	   const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
577
578
579
580
581
582
  	long tmp;
  	int err;
  
  	err = kstrtol(buf, 10, &tmp);
  	if (err)
  		return err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
583
584
585
586
587
588
589
590
591
592
  
  	mutex_lock(&data->update_lock);
  	data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
  	w83793_write_value(client, W83793_REG_TEMP[index][nr],
  			   data->temp[index][nr]);
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  /*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
593
594
595
596
597
598
599
600
601
602
603
604
   * TD1-TD4
   * each has 4 mode:(2 bits)
   * 0:	Stop monitor
   * 1:	Use internal temp sensor(default)
   * 2:	Reserved
   * 3:	Use sensor in Intel CPU and get result by PECI
   *
   * TR1-TR2
   * each has 2 mode:(1 bit)
   * 0:	Disable temp sensor monitor
   * 1:	To enable temp sensors monitor
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
605

ddca933bd   Gong Jun   hwmon/w83793: Rem...
606
607
  /* 0 disable, 6 PECI */
  static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
6800c3d02   Rudolf Marek   hwmon: New Winbon...
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
  
  static ssize_t
  show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct w83793_data *data = w83793_update_device(dev);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int index = sensor_attr->index;
  	u8 mask = (index < 4) ? 0x03 : 0x01;
  	u8 shift = (index < 4) ? (2 * index) : (index - 4);
  	u8 tmp;
  	index = (index < 4) ? 0 : 1;
  
  	tmp = (data->temp_mode[index] >> shift) & mask;
  
  	/* for the internal sensor, found out if diode or thermistor */
47efe8772   Guenter Roeck   hwmon: (w83793) F...
624
  	if (tmp == 1)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
625
  		tmp = index == 0 ? 3 : 4;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
626
  	else
6800c3d02   Rudolf Marek   hwmon: New Winbon...
627
  		tmp = TO_TEMP_MODE[tmp];
6800c3d02   Rudolf Marek   hwmon: New Winbon...
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
  
  	return sprintf(buf, "%d
  ", tmp);
  }
  
  static ssize_t
  store_temp_mode(struct device *dev, struct device_attribute *attr,
  		const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int index = sensor_attr->index;
  	u8 mask = (index < 4) ? 0x03 : 0x01;
  	u8 shift = (index < 4) ? (2 * index) : (index - 4);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
644
645
646
647
648
649
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
650
651
  
  	/* transform the sysfs interface values into table above */
ddca933bd   Gong Jun   hwmon/w83793: Rem...
652
  	if ((val == 6) && (index < 4)) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
653
654
  		val -= 3;
  	} else if ((val == 3 && index < 4)
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
655
  		|| (val == 4 && index >= 4)) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  		/* transform diode or thermistor into internal enable */
  		val = !!val;
  	} else {
  		return -EINVAL;
  	}
  
  	index = (index < 4) ? 0 : 1;
  	mutex_lock(&data->update_lock);
  	data->temp_mode[index] =
  	    w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
  	data->temp_mode[index] &= ~(mask << shift);
  	data->temp_mode[index] |= val << shift;
  	w83793_write_value(client, W83793_REG_TEMP_MODE[index],
  							data->temp_mode[index]);
  	mutex_unlock(&data->update_lock);
  
  	return count;
  }
  
  #define SETUP_PWM_DEFAULT		0
  #define SETUP_PWM_UPTIME		1	/* Unit in 0.1s */
  #define SETUP_PWM_DOWNTIME		2	/* Unit in 0.1s */
  #define SETUP_TEMP_CRITICAL		3
  static ssize_t
  show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	struct w83793_data *data = w83793_update_device(dev);
  	u32 val = 0;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
687
  	if (nr == SETUP_PWM_DEFAULT)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
688
  		val = (data->pwm_default & 0x3f) << 2;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
689
  	else if (nr == SETUP_PWM_UPTIME)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
690
  		val = TIME_FROM_REG(data->pwm_uptime);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
691
  	else if (nr == SETUP_PWM_DOWNTIME)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
692
  		val = TIME_FROM_REG(data->pwm_downtime);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
693
  	else if (nr == SETUP_TEMP_CRITICAL)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
694
  		val = TEMP_FROM_REG(data->temp_critical & 0x7f);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
695
696
697
698
699
700
701
702
703
704
705
706
707
708
  
  	return sprintf(buf, "%d
  ", val);
  }
  
  static ssize_t
  store_sf_setup(struct device *dev, struct device_attribute *attr,
  	       const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
709
710
711
712
713
714
  	long val;
  	int err;
  
  	err = kstrtol(buf, 10, &val);
  	if (err)
  		return err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
715
716
  
  	mutex_lock(&data->update_lock);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
717
  	if (nr == SETUP_PWM_DEFAULT) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
718
719
  		data->pwm_default =
  		    w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
2a844c148   Guenter Roeck   hwmon: Replace SE...
720
  		data->pwm_default |= clamp_val(val, 0, 0xff) >> 2;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
721
722
  		w83793_write_value(client, W83793_REG_PWM_DEFAULT,
  							data->pwm_default);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
723
724
  	} else if (nr == SETUP_PWM_UPTIME) {
  		data->pwm_uptime = TIME_TO_REG(val);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
725
726
727
  		data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
  		w83793_write_value(client, W83793_REG_PWM_UPTIME,
  							data->pwm_uptime);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
728
729
  	} else if (nr == SETUP_PWM_DOWNTIME) {
  		data->pwm_downtime = TIME_TO_REG(val);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
730
731
732
733
734
735
  		data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
  		w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
  							data->pwm_downtime);
  	} else {		/* SETUP_TEMP_CRITICAL */
  		data->temp_critical =
  		    w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
736
  		data->temp_critical |= TEMP_TO_REG(val, 0, 0x7f);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
737
738
739
740
741
742
743
744
745
  		w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
  							data->temp_critical);
  	}
  
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  /*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
   * Temp SmartFan control
   * TEMP_FAN_MAP
   * Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
   * It's possible two or more temp channels control the same fan, w83793
   * always prefers to pick the most critical request and applies it to
   * the related Fan.
   * It's possible one fan is not in any mapping of 6 temp channels, this
   * means the fan is manual mode
   *
   * TEMP_PWM_ENABLE
   * Each temp channel has its own SmartFan mode, and temp channel
   * control fans that are set by TEMP_FAN_MAP
   * 0:	SmartFanII mode
   * 1:	Thermal Cruise Mode
   *
   * TEMP_CRUISE
   * Target temperature in thermal cruise mode, w83793 will try to turn
   * fan speed to keep the temperature of target device around this
   * temperature.
   *
   * TEMP_TOLERANCE
   * If Temp higher or lower than target with this tolerance, w83793
   * will take actions to speed up or slow down the fan to keep the
   * temperature within the tolerance range.
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
  
  #define TEMP_FAN_MAP			0
  #define TEMP_PWM_ENABLE			1
  #define TEMP_CRUISE			2
  #define TEMP_TOLERANCE			3
  static ssize_t
  show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct w83793_data *data = w83793_update_device(dev);
  	u32 val;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
785
  	if (nr == TEMP_FAN_MAP) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
786
  		val = data->temp_fan_map[index];
47efe8772   Guenter Roeck   hwmon: (w83793) F...
787
  	} else if (nr == TEMP_PWM_ENABLE) {
84fb029fa   LABBE Corentin   hwmon: Correct so...
788
  		/* +2 to transform into 2 and 3 to conform with sysfs intf */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
789
  		val = ((data->pwm_enable >> index) & 0x01) + 2;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
790
  	} else if (nr == TEMP_CRUISE) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
  		val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
  	} else {		/* TEMP_TOLERANCE */
  		val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
  		val = TEMP_FROM_REG(val & 0x0f);
  	}
  	return sprintf(buf, "%d
  ", val);
  }
  
  static ssize_t
  store_sf_ctrl(struct device *dev, struct device_attribute *attr,
  	      const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
810
811
812
813
814
815
  	long val;
  	int err;
  
  	err = kstrtol(buf, 10, &val);
  	if (err)
  		return err;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
816
817
  
  	mutex_lock(&data->update_lock);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
818
  	if (nr == TEMP_FAN_MAP) {
2a844c148   Guenter Roeck   hwmon: Replace SE...
819
  		val = clamp_val(val, 0, 255);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
820
821
  		w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
  		data->temp_fan_map[index] = val;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
822
823
  	} else if (nr == TEMP_PWM_ENABLE) {
  		if (val == 2 || val == 3) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
824
825
826
827
828
829
830
831
832
833
834
835
  			data->pwm_enable =
  			    w83793_read_value(client, W83793_REG_PWM_ENABLE);
  			if (val - 2)
  				data->pwm_enable |= 1 << index;
  			else
  				data->pwm_enable &= ~(1 << index);
  			w83793_write_value(client, W83793_REG_PWM_ENABLE,
  							data->pwm_enable);
  		} else {
  			mutex_unlock(&data->update_lock);
  			return -EINVAL;
  		}
47efe8772   Guenter Roeck   hwmon: (w83793) F...
836
  	} else if (nr == TEMP_CRUISE) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
837
838
  		data->temp_cruise[index] =
  		    w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
6800c3d02   Rudolf Marek   hwmon: New Winbon...
839
  		data->temp_cruise[index] &= 0x80;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
840
  		data->temp_cruise[index] |= TEMP_TO_REG(val, 0, 0x7f);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
841
842
843
844
845
846
847
848
  
  		w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
  						data->temp_cruise[index]);
  	} else {		/* TEMP_TOLERANCE */
  		int i = index >> 1;
  		u8 shift = (index & 0x01) ? 4 : 0;
  		data->tolerance[i] =
  		    w83793_read_value(client, W83793_REG_TEMP_TOL(i));
6800c3d02   Rudolf Marek   hwmon: New Winbon...
849
  		data->tolerance[i] &= ~(0x0f << shift);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
850
  		data->tolerance[i] |= TEMP_TO_REG(val, 0, 0x0f) << shift;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
  		w83793_write_value(client, W83793_REG_TEMP_TOL(i),
  							data->tolerance[i]);
  	}
  
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  static ssize_t
  show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct w83793_data *data = w83793_update_device(dev);
  
  	return sprintf(buf, "%d
  ", (data->sf2_pwm[index][nr] & 0x3f) << 2);
  }
  
  static ssize_t
  store_sf2_pwm(struct device *dev, struct device_attribute *attr,
  	      const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
882
883
884
885
886
887
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
2a844c148   Guenter Roeck   hwmon: Replace SE...
888
  	val = clamp_val(val, 0, 0xff) >> 2;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
  
  	mutex_lock(&data->update_lock);
  	data->sf2_pwm[index][nr] =
  	    w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
  	data->sf2_pwm[index][nr] |= val;
  	w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
  						data->sf2_pwm[index][nr]);
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  static ssize_t
  show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct w83793_data *data = w83793_update_device(dev);
  
  	return sprintf(buf, "%ld
  ",
  		       TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
  }
  
  static ssize_t
  store_sf2_temp(struct device *dev, struct device_attribute *attr,
  	       const char *buf, size_t count)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
924
925
926
927
928
929
930
  	long val;
  	int err;
  
  	err = kstrtol(buf, 10, &val);
  	if (err)
  		return err;
  	val = TEMP_TO_REG(val, 0, 0x7f);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
  
  	mutex_lock(&data->update_lock);
  	data->sf2_temp[index][nr] =
  	    w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
  	data->sf2_temp[index][nr] |= val;
  	w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
  					     data->sf2_temp[index][nr]);
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  /* only Vcore A/B and Vtt have additional 2 bits precision */
  static ssize_t
  show_in(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct w83793_data *data = w83793_update_device(dev);
  	u16 val = data->in[index][nr];
  
  	if (index < 3) {
  		val <<= 2;
  		val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
  	}
ddca933bd   Gong Jun   hwmon/w83793: Rem...
957
958
959
960
  	/* voltage inputs 5VDD and 5VSB needs 150mV offset */
  	val = val * scale_in[index] + scale_in_add[index];
  	return sprintf(buf, "%d
  ", val);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
961
962
963
964
965
966
967
968
969
970
971
972
  }
  
  static ssize_t
  store_in(struct device *dev, struct device_attribute *attr,
  	 const char *buf, size_t count)
  {
  	struct sensor_device_attribute_2 *sensor_attr =
  	    to_sensor_dev_attr_2(attr);
  	int nr = sensor_attr->nr;
  	int index = sensor_attr->index;
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
973
974
975
976
977
978
979
  	unsigned long val;
  	int err;
  
  	err = kstrtoul(buf, 10, &val);
  	if (err)
  		return err;
  	val = (val + scale_in[index] / 2) / scale_in[index];
6800c3d02   Rudolf Marek   hwmon: New Winbon...
980

6800c3d02   Rudolf Marek   hwmon: New Winbon...
981
982
  	mutex_lock(&data->update_lock);
  	if (index > 2) {
ddca933bd   Gong Jun   hwmon/w83793: Rem...
983
  		/* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
47efe8772   Guenter Roeck   hwmon: (w83793) F...
984
  		if (nr == 1 || nr == 2)
ddca933bd   Gong Jun   hwmon/w83793: Rem...
985
  			val -= scale_in_add[index] / scale_in[index];
2a844c148   Guenter Roeck   hwmon: Replace SE...
986
  		val = clamp_val(val, 0, 255);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
987
  	} else {
2a844c148   Guenter Roeck   hwmon: Replace SE...
988
  		val = clamp_val(val, 0, 0x3FF);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
  		data->in_low_bits[nr] =
  		    w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
  		data->in_low_bits[nr] &= ~(0x03 << (2 * index));
  		data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
  		w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
  						     data->in_low_bits[nr]);
  		val >>= 2;
  	}
  	data->in[index][nr] = val;
  	w83793_write_value(client, W83793_REG_IN[index][nr],
  							data->in[index][nr]);
  	mutex_unlock(&data->update_lock);
  	return count;
  }
  
  #define NOT_USED			-1
  
  #define SENSOR_ATTR_IN(index)						\
  	SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL,	\
  		IN_READ, index),					\
  	SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in,	\
  		store_in, IN_MAX, index),				\
  	SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in,	\
  		store_in, IN_LOW, index),				\
  	SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep,	\
  		NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)),	\
  	SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO,		\
  		show_alarm_beep, store_beep, BEEP_ENABLE,		\
  		index + ((index > 2) ? 1 : 0))
  
  #define SENSOR_ATTR_FAN(index)						\
  	SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep,	\
  		NULL, ALARM_STATUS, index + 17),			\
  	SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO,		\
  		show_alarm_beep, store_beep, BEEP_ENABLE, index + 17),	\
  	SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan,		\
  		NULL, FAN_INPUT, index - 1),				\
  	SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO,		\
  		show_fan, store_fan_min, FAN_MIN, index - 1)
  
  #define SENSOR_ATTR_PWM(index)						\
  	SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm,		\
  		store_pwm, PWM_DUTY, index - 1),			\
  	SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO,		\
  		show_pwm, store_pwm, PWM_NONSTOP, index - 1),		\
  	SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO,		\
  		show_pwm, store_pwm, PWM_START, index - 1),		\
  	SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO,	\
  		show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
  
  #define SENSOR_ATTR_TEMP(index)						\
  	SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR,		\
  		show_temp_mode, store_temp_mode, NOT_USED, index - 1),	\
  	SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp,		\
  		NULL, TEMP_READ, index - 1),				\
  	SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp,	\
  		store_temp, TEMP_CRIT, index - 1),			\
  	SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR,	\
  		show_temp, store_temp, TEMP_CRIT_HYST, index - 1),	\
  	SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp,	\
  		store_temp, TEMP_WARN, index - 1),			\
  	SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR,	\
  		show_temp, store_temp, TEMP_WARN_HYST, index - 1),	\
  	SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO,			\
  		show_alarm_beep, NULL, ALARM_STATUS, index + 11),	\
  	SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO,		\
  		show_alarm_beep, store_beep, BEEP_ENABLE, index + 11),	\
  	SENSOR_ATTR_2(temp##index##_auto_channels_pwm,			\
  		S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl,		\
  		TEMP_FAN_MAP, index - 1),				\
  	SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO,	\
  		show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE,		\
  		index - 1),						\
  	SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR,		\
  		show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1),	\
  	SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
  		store_sf_ctrl, TEMP_TOLERANCE, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 0, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 1, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 2, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 3, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 4, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 5, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
  		show_sf2_pwm, store_sf2_pwm, 6, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 0, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 1, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 2, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 3, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 4, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 5, index - 1),		\
  	SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
  		show_sf2_temp, store_sf2_temp, 6, index - 1)
  
  static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
  	SENSOR_ATTR_IN(0),
  	SENSOR_ATTR_IN(1),
  	SENSOR_ATTR_IN(2),
  	SENSOR_ATTR_IN(3),
  	SENSOR_ATTR_IN(4),
  	SENSOR_ATTR_IN(5),
  	SENSOR_ATTR_IN(6),
  	SENSOR_ATTR_IN(7),
  	SENSOR_ATTR_IN(8),
  	SENSOR_ATTR_IN(9),
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1106
1107
1108
1109
1110
1111
1112
1113
1114
  	SENSOR_ATTR_FAN(1),
  	SENSOR_ATTR_FAN(2),
  	SENSOR_ATTR_FAN(3),
  	SENSOR_ATTR_FAN(4),
  	SENSOR_ATTR_FAN(5),
  	SENSOR_ATTR_PWM(1),
  	SENSOR_ATTR_PWM(2),
  	SENSOR_ATTR_PWM(3),
  };
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1115
1116
1117
1118
1119
1120
1121
1122
  static struct sensor_device_attribute_2 w83793_temp[] = {
  	SENSOR_ATTR_TEMP(1),
  	SENSOR_ATTR_TEMP(2),
  	SENSOR_ATTR_TEMP(3),
  	SENSOR_ATTR_TEMP(4),
  	SENSOR_ATTR_TEMP(5),
  	SENSOR_ATTR_TEMP(6),
  };
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  /* Fan6-Fan12 */
  static struct sensor_device_attribute_2 w83793_left_fan[] = {
  	SENSOR_ATTR_FAN(6),
  	SENSOR_ATTR_FAN(7),
  	SENSOR_ATTR_FAN(8),
  	SENSOR_ATTR_FAN(9),
  	SENSOR_ATTR_FAN(10),
  	SENSOR_ATTR_FAN(11),
  	SENSOR_ATTR_FAN(12),
  };
  
  /* Pwm4-Pwm8 */
  static struct sensor_device_attribute_2 w83793_left_pwm[] = {
  	SENSOR_ATTR_PWM(4),
  	SENSOR_ATTR_PWM(5),
  	SENSOR_ATTR_PWM(6),
  	SENSOR_ATTR_PWM(7),
  	SENSOR_ATTR_PWM(8),
  };
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1142
  static struct sensor_device_attribute_2 w83793_vid[] = {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1143
1144
  	SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
  	SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1145
  };
329beb71f   Julia Lawall   hwmon: (w83793) u...
1146
  static DEVICE_ATTR_RW(vrm);
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1147
1148
  
  static struct sensor_device_attribute_2 sda_single_files[] = {
a516dc3e9   Jean Delvare   hwmon: (w83793) I...
1149
  	SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
  		      store_chassis_clear, ALARM_STATUS, 30),
  	SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
  		      store_beep_enable, NOT_USED, NOT_USED),
  	SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
  		      store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
  	SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
  		      store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
  	SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
  		      store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
  	SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
  		      store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
  };
  
  static void w83793_init_client(struct i2c_client *client)
  {
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1165
  	if (reset)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1166
  		w83793_write_value(client, W83793_REG_CONFIG, 0x80);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1167
1168
1169
1170
  
  	/* Start monitoring */
  	w83793_write_value(client, W83793_REG_CONFIG,
  			   w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
5852f9609   Sven Anders   hwmon: (w83793) A...
1171
1172
1173
1174
1175
1176
1177
1178
  }
  
  /*
   * Watchdog routines
   */
  
  static int watchdog_set_timeout(struct w83793_data *data, int timeout)
  {
26336c8a3   Dan Carpenter   hwmon: (w83793) C...
1179
1180
  	unsigned int mtimeout;
  	int ret;
5852f9609   Sven Anders   hwmon: (w83793) A...
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
  
  	mtimeout = DIV_ROUND_UP(timeout, 60);
  
  	if (mtimeout > 255)
  		return -EINVAL;
  
  	mutex_lock(&data->watchdog_lock);
  	if (!data->client) {
  		ret = -ENODEV;
  		goto leave;
  	}
  
  	data->watchdog_timeout = mtimeout;
  
  	/* Set Timeout value (in Minutes) */
  	w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  			   data->watchdog_timeout);
  
  	ret = mtimeout * 60;
  
  leave:
  	mutex_unlock(&data->watchdog_lock);
  	return ret;
  }
  
  static int watchdog_get_timeout(struct w83793_data *data)
  {
  	int timeout;
  
  	mutex_lock(&data->watchdog_lock);
  	timeout = data->watchdog_timeout * 60;
  	mutex_unlock(&data->watchdog_lock);
  
  	return timeout;
  }
  
  static int watchdog_trigger(struct w83793_data *data)
  {
  	int ret = 0;
  
  	mutex_lock(&data->watchdog_lock);
  	if (!data->client) {
  		ret = -ENODEV;
  		goto leave;
  	}
  
  	/* Set Timeout value (in Minutes) */
  	w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  			   data->watchdog_timeout);
  
  leave:
  	mutex_unlock(&data->watchdog_lock);
  	return ret;
  }
  
  static int watchdog_enable(struct w83793_data *data)
  {
  	int ret = 0;
  
  	mutex_lock(&data->watchdog_lock);
  	if (!data->client) {
  		ret = -ENODEV;
  		goto leave;
  	}
  
  	/* Set initial timeout */
  	w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  			   data->watchdog_timeout);
  
  	/* Enable Soft Watchdog */
  	w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
  
  leave:
  	mutex_unlock(&data->watchdog_lock);
  	return ret;
  }
  
  static int watchdog_disable(struct w83793_data *data)
  {
  	int ret = 0;
  
  	mutex_lock(&data->watchdog_lock);
  	if (!data->client) {
  		ret = -ENODEV;
  		goto leave;
  	}
  
  	/* Disable Soft Watchdog */
  	w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
  
  leave:
  	mutex_unlock(&data->watchdog_lock);
  	return ret;
  }
  
  static int watchdog_open(struct inode *inode, struct file *filp)
  {
  	struct w83793_data *pos, *data = NULL;
  	int watchdog_is_open;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1280
1281
1282
1283
1284
1285
  	/*
  	 * We get called from drivers/char/misc.c with misc_mtx hold, and we
  	 * call misc_register() from  w83793_probe() with watchdog_data_mutex
  	 * hold, as misc_register() takes the misc_mtx lock, this is a possible
  	 * deadlock, so we use mutex_trylock here.
  	 */
5852f9609   Sven Anders   hwmon: (w83793) A...
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
  	if (!mutex_trylock(&watchdog_data_mutex))
  		return -ERESTARTSYS;
  	list_for_each_entry(pos, &watchdog_data_list, list) {
  		if (pos->watchdog_miscdev.minor == iminor(inode)) {
  			data = pos;
  			break;
  		}
  	}
  
  	/* Check, if device is already open */
  	watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1297
1298
1299
1300
  	/*
  	 * Increase data reference counter (if not already done).
  	 * Note we can never not have found data, so we don't check for this
  	 */
5852f9609   Sven Anders   hwmon: (w83793) A...
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
  	if (!watchdog_is_open)
  		kref_get(&data->kref);
  
  	mutex_unlock(&watchdog_data_mutex);
  
  	/* Check, if device is already open and possibly issue error */
  	if (watchdog_is_open)
  		return -EBUSY;
  
  	/* Enable Soft Watchdog */
  	watchdog_enable(data);
  
  	/* Store pointer to data into filp's private data */
  	filp->private_data = data;
c5bf68fe0   Kirill Smelkov   *: convert stream...
1315
  	return stream_open(inode, filp);
5852f9609   Sven Anders   hwmon: (w83793) A...
1316
1317
1318
1319
1320
  }
  
  static int watchdog_close(struct inode *inode, struct file *filp)
  {
  	struct w83793_data *data = filp->private_data;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1321

5852f9609   Sven Anders   hwmon: (w83793) A...
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
  	if (data->watchdog_expect_close) {
  		watchdog_disable(data);
  		data->watchdog_expect_close = 0;
  	} else {
  		watchdog_trigger(data);
  		dev_crit(&data->client->dev,
  			"unexpected close, not stopping watchdog!
  ");
  	}
  
  	clear_bit(0, &data->watchdog_is_open);
  
  	/* Decrease data reference counter */
  	mutex_lock(&watchdog_data_mutex);
  	kref_put(&data->kref, w83793_release_resources);
  	mutex_unlock(&watchdog_data_mutex);
  
  	return 0;
  }
  
  static ssize_t watchdog_write(struct file *filp, const char __user *buf,
  	size_t count, loff_t *offset)
  {
3f7cd7ea9   Dan Carpenter   hwmon: (w83793) S...
1345
  	ssize_t ret;
5852f9609   Sven Anders   hwmon: (w83793) A...
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
  	struct w83793_data *data = filp->private_data;
  
  	if (count) {
  		if (!nowayout) {
  			size_t i;
  
  			/* Clear it in case it was set with a previous write */
  			data->watchdog_expect_close = 0;
  
  			for (i = 0; i != count; i++) {
  				char c;
  				if (get_user(c, buf + i))
  					return -EFAULT;
  				if (c == 'V')
  					data->watchdog_expect_close = 1;
  			}
  		}
  		ret = watchdog_trigger(data);
  		if (ret < 0)
  			return ret;
  	}
  	return count;
  }
55929332c   Arnd Bergmann   drivers: Push dow...
1369
1370
  static long watchdog_ioctl(struct file *filp, unsigned int cmd,
  			   unsigned long arg)
5852f9609   Sven Anders   hwmon: (w83793) A...
1371
  {
36c7fe133   Jean Delvare   hwmon: (w83793) D...
1372
  	struct watchdog_info ident = {
5852f9609   Sven Anders   hwmon: (w83793) A...
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
  		.options = WDIOF_KEEPALIVEPING |
  			   WDIOF_SETTIMEOUT |
  			   WDIOF_CARDRESET,
  		.identity = "w83793 watchdog"
  	};
  
  	int val, ret = 0;
  	struct w83793_data *data = filp->private_data;
  
  	switch (cmd) {
  	case WDIOC_GETSUPPORT:
  		if (!nowayout)
  			ident.options |= WDIOF_MAGICCLOSE;
  		if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  			ret = -EFAULT;
  		break;
  
  	case WDIOC_GETSTATUS:
  		val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
  		ret = put_user(val, (int __user *)arg);
  		break;
  
  	case WDIOC_GETBOOTSTATUS:
  		ret = put_user(0, (int __user *)arg);
  		break;
  
  	case WDIOC_KEEPALIVE:
  		ret = watchdog_trigger(data);
  		break;
  
  	case WDIOC_GETTIMEOUT:
  		val = watchdog_get_timeout(data);
  		ret = put_user(val, (int __user *)arg);
  		break;
  
  	case WDIOC_SETTIMEOUT:
  		if (get_user(val, (int __user *)arg)) {
  			ret = -EFAULT;
  			break;
  		}
  		ret = watchdog_set_timeout(data, val);
  		if (ret > 0)
  			ret = put_user(ret, (int __user *)arg);
  		break;
  
  	case WDIOC_SETOPTIONS:
  		if (get_user(val, (int __user *)arg)) {
  			ret = -EFAULT;
  			break;
  		}
  
  		if (val & WDIOS_DISABLECARD)
  			ret = watchdog_disable(data);
  		else if (val & WDIOS_ENABLECARD)
  			ret = watchdog_enable(data);
  		else
  			ret = -EINVAL;
  
  		break;
  	default:
  		ret = -ENOTTY;
  	}
5852f9609   Sven Anders   hwmon: (w83793) A...
1435
1436
1437
1438
1439
1440
1441
1442
1443
  	return ret;
  }
  
  static const struct file_operations watchdog_fops = {
  	.owner = THIS_MODULE,
  	.llseek = no_llseek,
  	.open = watchdog_open,
  	.release = watchdog_close,
  	.write = watchdog_write,
55929332c   Arnd Bergmann   drivers: Push dow...
1444
  	.unlocked_ioctl = watchdog_ioctl,
b6dfb2477   Arnd Bergmann   compat_ioctl: mov...
1445
  	.compat_ioctl = compat_ptr_ioctl,
5852f9609   Sven Anders   hwmon: (w83793) A...
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
  };
  
  /*
   *	Notifier for system down
   */
  
  static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
  			       void *unused)
  {
  	struct w83793_data *data = NULL;
  
  	if (code == SYS_DOWN || code == SYS_HALT) {
  
  		/* Disable each registered watchdog */
  		mutex_lock(&watchdog_data_mutex);
  		list_for_each_entry(data, &watchdog_data_list, list) {
  			if (data->watchdog_miscdev.minor)
  				watchdog_disable(data);
  		}
  		mutex_unlock(&watchdog_data_mutex);
  	}
  
  	return NOTIFY_DONE;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1469
  }
5852f9609   Sven Anders   hwmon: (w83793) A...
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
  /*
   *	The WDT needs to learn about soft shutdowns in order to
   *	turn the timebomb registers off.
   */
  
  static struct notifier_block watchdog_notifier = {
  	.notifier_call = watchdog_notify_sys,
  };
  
  /*
   * Init / remove routines
   */
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1482
  static int w83793_remove(struct i2c_client *client)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1483
1484
1485
  {
  	struct w83793_data *data = i2c_get_clientdata(client);
  	struct device *dev = &client->dev;
5852f9609   Sven Anders   hwmon: (w83793) A...
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
  	int i, tmp;
  
  	/* Unregister the watchdog (if registered) */
  	if (data->watchdog_miscdev.minor) {
  		misc_deregister(&data->watchdog_miscdev);
  
  		if (data->watchdog_is_open) {
  			dev_warn(&client->dev,
  				"i2c client detached with watchdog open! "
  				"Stopping watchdog.
  ");
  			watchdog_disable(data);
  		}
  
  		mutex_lock(&watchdog_data_mutex);
  		list_del(&data->list);
  		mutex_unlock(&watchdog_data_mutex);
  
  		/* Tell the watchdog code the client is gone */
  		mutex_lock(&data->watchdog_lock);
  		data->client = NULL;
  		mutex_unlock(&data->watchdog_lock);
  	}
  
  	/* Reset Configuration Register to Disable Watch Dog Registers */
  	tmp = w83793_read_value(client, W83793_REG_CONFIG);
  	w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
  
  	unregister_reboot_notifier(&watchdog_notifier);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1515

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1516
  	hwmon_device_unregister(data->hwmon_dev);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1517

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1518
1519
1520
  	for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  		device_remove_file(dev,
  				   &w83793_sensor_attr_2[i].dev_attr);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1521

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1522
1523
  	for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  		device_remove_file(dev, &sda_single_files[i].dev_attr);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1524

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1525
1526
1527
  	for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  		device_remove_file(dev, &w83793_vid[i].dev_attr);
  	device_remove_file(dev, &dev_attr_vrm);
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1528

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1529
1530
  	for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  		device_remove_file(dev, &w83793_left_fan[i].dev_attr);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1531

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1532
1533
  	for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  		device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1534

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1535
1536
  	for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  		device_remove_file(dev, &w83793_temp[i].dev_attr);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1537

5852f9609   Sven Anders   hwmon: (w83793) A...
1538
1539
1540
1541
  	/* Decrease data reference counter */
  	mutex_lock(&watchdog_data_mutex);
  	kref_put(&data->kref, w83793_release_resources);
  	mutex_unlock(&watchdog_data_mutex);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1542
1543
1544
1545
1546
  
  	return 0;
  }
  
  static int
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1547
  w83793_detect_subclients(struct i2c_client *client)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1548
  {
cf48d1762   Wolfram Sang   hwmon: (w83793d) ...
1549
  	int i, id;
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1550
  	int address = client->addr;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1551
  	u8 tmp;
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1552
  	struct i2c_adapter *adapter = client->adapter;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
  	struct w83793_data *data = i2c_get_clientdata(client);
  
  	id = i2c_adapter_id(adapter);
  	if (force_subclients[0] == id && force_subclients[1] == address) {
  		for (i = 2; i <= 3; i++) {
  			if (force_subclients[i] < 0x48
  			    || force_subclients[i] > 0x4f) {
  				dev_err(&client->dev,
  					"invalid subclient "
  					"address %d; must be 0x48-0x4f
  ",
  					force_subclients[i]);
cf48d1762   Wolfram Sang   hwmon: (w83793d) ...
1565
  				return -EINVAL;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1566
1567
1568
1569
1570
1571
1572
1573
  			}
  		}
  		w83793_write_value(client, W83793_REG_I2C_SUBADDR,
  				   (force_subclients[2] & 0x07) |
  				   ((force_subclients[3] & 0x07) << 4));
  	}
  
  	tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1574
  	if (!(tmp & 0x08))
cf48d1762   Wolfram Sang   hwmon: (w83793d) ...
1575
1576
  		data->lm75[0] = devm_i2c_new_dummy_device(&client->dev, adapter,
  							  0x48 + (tmp & 0x7));
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1577
  	if (!(tmp & 0x80)) {
cf48d1762   Wolfram Sang   hwmon: (w83793d) ...
1578
  		if (!IS_ERR(data->lm75[0])
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1579
1580
1581
1582
1583
  		    && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
  			dev_err(&client->dev,
  				"duplicate addresses 0x%x, "
  				"use force_subclients
  ", data->lm75[0]->addr);
cf48d1762   Wolfram Sang   hwmon: (w83793d) ...
1584
  			return -ENODEV;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1585
  		}
cf48d1762   Wolfram Sang   hwmon: (w83793d) ...
1586
1587
  		data->lm75[1] = devm_i2c_new_dummy_device(&client->dev, adapter,
  							  0x48 + ((tmp >> 4) & 0x7));
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1588
1589
1590
  	}
  
  	return 0;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1591
  }
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1592
  /* Return 0 if detection is successful, -ENODEV otherwise */
310ec7921   Jean Delvare   i2c: Drop the kin...
1593
  static int w83793_detect(struct i2c_client *client,
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1594
  			 struct i2c_board_info *info)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1595
  {
52df6440a   Jean Delvare   hwmon: Clean up d...
1596
  	u8 tmp, bank, chip_id;
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1597
1598
  	struct i2c_adapter *adapter = client->adapter;
  	unsigned short address = client->addr;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1599

47efe8772   Guenter Roeck   hwmon: (w83793) F...
1600
  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1601
  		return -ENODEV;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1602

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1603
  	bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1604

52df6440a   Jean Delvare   hwmon: Clean up d...
1605
1606
1607
1608
1609
1610
  	tmp = bank & 0x80 ? 0x5c : 0xa3;
  	/* Check Winbond vendor ID */
  	if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
  		pr_debug("w83793: Detection failed at check vendor id
  ");
  		return -ENODEV;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1611
  	}
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1612
1613
1614
1615
  	/*
  	 * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
  	 * should match
  	 */
52df6440a   Jean Delvare   hwmon: Clean up d...
1616
1617
1618
1619
1620
1621
  	if ((bank & 0x07) == 0
  	 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
  	    (address << 1)) {
  		pr_debug("w83793: Detection failed at check i2c addr
  ");
  		return -ENODEV;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1622
  	}
52df6440a   Jean Delvare   hwmon: Clean up d...
1623
1624
1625
1626
  	/* Determine the chip type now */
  	chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
  	if (chip_id != 0x7b)
  		return -ENODEV;
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1627
1628
1629
1630
  	strlcpy(info->type, "w83793", I2C_NAME_SIZE);
  
  	return 0;
  }
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1631

16b237f5e   Stephen Kitt   hwmon: (w83793) u...
1632
  static int w83793_probe(struct i2c_client *client)
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1633
1634
  {
  	struct device *dev = &client->dev;
7a76a7f34   Colin Ian King   hwmon: (w83793) m...
1635
1636
1637
  	static const int watchdog_minors[] = {
  		WATCHDOG_MINOR, 212, 213, 214, 215
  	};
a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
  	struct w83793_data *data;
  	int i, tmp, val, err;
  	int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
  	int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
  	int files_temp = ARRAY_SIZE(w83793_temp) / 6;
  
  	data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
  	if (!data) {
  		err = -ENOMEM;
  		goto exit;
  	}
  
  	i2c_set_clientdata(client, data);
  	data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1652
  	mutex_init(&data->update_lock);
5852f9609   Sven Anders   hwmon: (w83793) A...
1653
1654
1655
  	mutex_init(&data->watchdog_lock);
  	INIT_LIST_HEAD(&data->list);
  	kref_init(&data->kref);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1656
1657
1658
1659
1660
  	/*
  	 * Store client pointer in our data struct for watchdog usage
  	 * (where the client is found through a data ptr instead of the
  	 * otherway around)
  	 */
5852f9609   Sven Anders   hwmon: (w83793) A...
1661
  	data->client = client;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1662

a7f13a6ec   Jean Delvare   hwmon: (w83793) C...
1663
1664
  	err = w83793_detect_subclients(client);
  	if (err)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1665
  		goto free_mem;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1666
1667
  	/* Initialize the chip */
  	w83793_init_client(client);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1668
  	/*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1669
1670
  	 * Only fan 1-5 has their own input pins,
  	 * Pwm 1-3 has their own pins
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1671
1672
1673
1674
1675
1676
1677
  	 */
  	data->has_fan = 0x1f;
  	data->has_pwm = 0x07;
  	tmp = w83793_read_value(client, W83793_REG_MFC);
  	val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
  
  	/* check the function of pins 49-56 */
93c75a4ac   Jean Delvare   hwmon: (w83793) V...
1678
1679
1680
  	if (tmp & 0x80) {
  		data->has_vid |= 0x2;	/* has VIDB */
  	} else {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
  		data->has_pwm |= 0x18;	/* pwm 4,5 */
  		if (val & 0x01) {	/* fan 6 */
  			data->has_fan |= 0x20;
  			data->has_pwm |= 0x20;
  		}
  		if (val & 0x02) {	/* fan 7 */
  			data->has_fan |= 0x40;
  			data->has_pwm |= 0x40;
  		}
  		if (!(tmp & 0x40) && (val & 0x04)) {	/* fan 8 */
  			data->has_fan |= 0x80;
  			data->has_pwm |= 0x80;
  		}
  	}
93c75a4ac   Jean Delvare   hwmon: (w83793) V...
1695
1696
1697
  	/* check the function of pins 37-40 */
  	if (!(tmp & 0x29))
  		data->has_vid |= 0x1;	/* has VIDA */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1698
1699
1700
1701
1702
1703
  	if (0x08 == (tmp & 0x0c)) {
  		if (val & 0x08)	/* fan 9 */
  			data->has_fan |= 0x100;
  		if (val & 0x10)	/* fan 10 */
  			data->has_fan |= 0x200;
  	}
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
  	if (0x20 == (tmp & 0x30)) {
  		if (val & 0x20)	/* fan 11 */
  			data->has_fan |= 0x400;
  		if (val & 0x40)	/* fan 12 */
  			data->has_fan |= 0x800;
  	}
  
  	if ((tmp & 0x01) && (val & 0x04)) {	/* fan 8, second location */
  		data->has_fan |= 0x80;
  		data->has_pwm |= 0x80;
  	}
c92943152   Rudolf Marek   hwmon/w83793: Fix...
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
  	tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
  	if ((tmp & 0x01) && (val & 0x08)) {	/* fan 9, second location */
  		data->has_fan |= 0x100;
  	}
  	if ((tmp & 0x02) && (val & 0x10)) {	/* fan 10, second location */
  		data->has_fan |= 0x200;
  	}
  	if ((tmp & 0x04) && (val & 0x20)) {	/* fan 11, second location */
  		data->has_fan |= 0x400;
  	}
  	if ((tmp & 0x08) && (val & 0x40)) {	/* fan 12, second location */
  		data->has_fan |= 0x800;
  	}
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1728
  	/* check the temp1-6 mode, ignore former AMDSI selected inputs */
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1729
  	tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1730
1731
1732
1733
1734
1735
1736
1737
  	if (tmp & 0x01)
  		data->has_temp |= 0x01;
  	if (tmp & 0x04)
  		data->has_temp |= 0x02;
  	if (tmp & 0x10)
  		data->has_temp |= 0x04;
  	if (tmp & 0x40)
  		data->has_temp |= 0x08;
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1738
  	tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1739
1740
1741
1742
  	if (tmp & 0x01)
  		data->has_temp |= 0x10;
  	if (tmp & 0x02)
  		data->has_temp |= 0x20;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1743
1744
1745
1746
1747
1748
1749
  	/* Register sysfs hooks */
  	for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
  		err = device_create_file(dev,
  					 &w83793_sensor_attr_2[i].dev_attr);
  		if (err)
  			goto exit_remove;
  	}
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1750
1751
1752
1753
1754
1755
1756
  	for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
  		if (!(data->has_vid & (1 << i)))
  			continue;
  		err = device_create_file(dev, &w83793_vid[i].dev_attr);
  		if (err)
  			goto exit_remove;
  	}
93c75a4ac   Jean Delvare   hwmon: (w83793) V...
1757
1758
1759
1760
1761
1762
  	if (data->has_vid) {
  		data->vrm = vid_which_vrm();
  		err = device_create_file(dev, &dev_attr_vrm);
  		if (err)
  			goto exit_remove;
  	}
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1763

6800c3d02   Rudolf Marek   hwmon: New Winbon...
1764
1765
1766
1767
1768
1769
  	for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
  		err = device_create_file(dev, &sda_single_files[i].dev_attr);
  		if (err)
  			goto exit_remove;
  
  	}
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
  	for (i = 0; i < 6; i++) {
  		int j;
  		if (!(data->has_temp & (1 << i)))
  			continue;
  		for (j = 0; j < files_temp; j++) {
  			err = device_create_file(dev,
  						&w83793_temp[(i) * files_temp
  								+ j].dev_attr);
  			if (err)
  				goto exit_remove;
  		}
  	}
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
  	for (i = 5; i < 12; i++) {
  		int j;
  		if (!(data->has_fan & (1 << i)))
  			continue;
  		for (j = 0; j < files_fan; j++) {
  			err = device_create_file(dev,
  					   &w83793_left_fan[(i - 5) * files_fan
  								+ j].dev_attr);
  			if (err)
  				goto exit_remove;
  		}
  	}
  
  	for (i = 3; i < 8; i++) {
  		int j;
  		if (!(data->has_pwm & (1 << i)))
  			continue;
  		for (j = 0; j < files_pwm; j++) {
  			err = device_create_file(dev,
  					   &w83793_left_pwm[(i - 3) * files_pwm
  								+ j].dev_attr);
  			if (err)
  				goto exit_remove;
  		}
  	}
1beeffe43   Tony Jones   hwmon: Convert fr...
1807
1808
1809
  	data->hwmon_dev = hwmon_device_register(dev);
  	if (IS_ERR(data->hwmon_dev)) {
  		err = PTR_ERR(data->hwmon_dev);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1810
1811
  		goto exit_remove;
  	}
5852f9609   Sven Anders   hwmon: (w83793) A...
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
  	/* Watchdog initialization */
  
  	/* Register boot notifier */
  	err = register_reboot_notifier(&watchdog_notifier);
  	if (err != 0) {
  		dev_err(&client->dev,
  			"cannot register reboot notifier (err=%d)
  ", err);
  		goto exit_devunreg;
  	}
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1822
1823
1824
1825
1826
  	/*
  	 * Enable Watchdog registers.
  	 * Set Configuration Register to Enable Watch Dog Registers
  	 * (Bit 2) = XXXX, X1XX.
  	 */
5852f9609   Sven Anders   hwmon: (w83793) A...
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
  	tmp = w83793_read_value(client, W83793_REG_CONFIG);
  	w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
  
  	/* Set the default watchdog timeout */
  	data->watchdog_timeout = timeout;
  
  	/* Check, if last reboot was caused by watchdog */
  	data->watchdog_caused_reboot =
  	  w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
  
  	/* Disable Soft Watchdog during initialiation */
  	watchdog_disable(data);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1839
1840
1841
1842
1843
  	/*
  	 * We take the data_mutex lock early so that watchdog_open() cannot
  	 * run when misc_register() has completed, but we've not yet added
  	 * our data to the watchdog_data_list (and set the default timeout)
  	 */
5852f9609   Sven Anders   hwmon: (w83793) A...
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
  	mutex_lock(&watchdog_data_mutex);
  	for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  		/* Register our watchdog part */
  		snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  			"watchdog%c", (i == 0) ? '\0' : ('0' + i));
  		data->watchdog_miscdev.name = data->watchdog_name;
  		data->watchdog_miscdev.fops = &watchdog_fops;
  		data->watchdog_miscdev.minor = watchdog_minors[i];
  
  		err = misc_register(&data->watchdog_miscdev);
  		if (err == -EBUSY)
  			continue;
  		if (err) {
  			data->watchdog_miscdev.minor = 0;
  			dev_err(&client->dev,
  				"Registering watchdog chardev: %d
  ", err);
  			break;
  		}
  
  		list_add(&data->list, &watchdog_data_list);
  
  		dev_info(&client->dev,
  			"Registered watchdog chardev major 10, minor: %d
  ",
  			watchdog_minors[i]);
  		break;
  	}
  	if (i == ARRAY_SIZE(watchdog_minors)) {
  		data->watchdog_miscdev.minor = 0;
b55f37572   Guenter Roeck   hwmon: Fix checkp...
1874
1875
1876
  		dev_warn(&client->dev,
  			 "Couldn't register watchdog chardev (due to no free minor)
  ");
5852f9609   Sven Anders   hwmon: (w83793) A...
1877
1878
1879
  	}
  
  	mutex_unlock(&watchdog_data_mutex);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1880
  	return 0;
5852f9609   Sven Anders   hwmon: (w83793) A...
1881
1882
1883
1884
1885
  	/* Unregister hwmon device */
  
  exit_devunreg:
  
  	hwmon_device_unregister(data->hwmon_dev);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1886
1887
1888
1889
1890
1891
1892
1893
  	/* Unregister sysfs hooks */
  
  exit_remove:
  	for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  		device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
  
  	for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  		device_remove_file(dev, &sda_single_files[i].dev_attr);
c70a8c345   Gong Jun   hwmon/w83793: Hid...
1894
1895
  	for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  		device_remove_file(dev, &w83793_vid[i].dev_attr);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1896
1897
1898
1899
1900
  	for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  		device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  
  	for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  		device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1901
1902
  	for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  		device_remove_file(dev, &w83793_temp[i].dev_attr);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
  free_mem:
  	kfree(data);
  exit:
  	return err;
  }
  
  static void w83793_update_nonvolatile(struct device *dev)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	int i, j;
  	/*
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1915
1916
1917
  	 * They are somewhat "stable" registers, and to update them every time
  	 * takes so much time, it's just not worthy. Update them in a long
  	 * interval to avoid exception.
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
  	 */
  	if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
  	      || !data->valid))
  		return;
  	/* update voltage limits */
  	for (i = 1; i < 3; i++) {
  		for (j = 0; j < ARRAY_SIZE(data->in); j++) {
  			data->in[j][i] =
  			    w83793_read_value(client, W83793_REG_IN[j][i]);
  		}
  		data->in_low_bits[i] =
  		    w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
  	}
  
  	for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
  		/* Update the Fan measured value and limits */
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1934
  		if (!(data->has_fan & (1 << i)))
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1935
  			continue;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1936
1937
1938
1939
1940
1941
1942
  		data->fan_min[i] =
  		    w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
  		data->fan_min[i] |=
  		    w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
  	}
  
  	for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
1943
1944
  		if (!(data->has_temp & (1 << i)))
  			continue;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
  		data->temp_fan_map[i] =
  		    w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
  		for (j = 1; j < 5; j++) {
  			data->temp[i][j] =
  			    w83793_read_value(client, W83793_REG_TEMP[i][j]);
  		}
  		data->temp_cruise[i] =
  		    w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
  		for (j = 0; j < 7; j++) {
  			data->sf2_pwm[i][j] =
  			    w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
  			data->sf2_temp[i][j] =
  			    w83793_read_value(client,
  					      W83793_REG_SF2_TEMP(i, j));
  		}
  	}
  
  	for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
  		data->temp_mode[i] =
  		    w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
  
  	for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
  		data->tolerance[i] =
  		    w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  	}
  
  	for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  		if (!(data->has_pwm & (1 << i)))
  			continue;
  		data->pwm[i][PWM_NONSTOP] =
  		    w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
  		data->pwm[i][PWM_START] =
  		    w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
  		data->pwm_stop_time[i] =
  		    w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
  	}
  
  	data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
  	data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
  	data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
  	data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
  	data->temp_critical =
  	    w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
  	data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
47efe8772   Guenter Roeck   hwmon: (w83793) F...
1989
  	for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1990
  		data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
6800c3d02   Rudolf Marek   hwmon: New Winbon...
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
  
  	data->last_nonvolatile = jiffies;
  }
  
  static struct w83793_data *w83793_update_device(struct device *dev)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	struct w83793_data *data = i2c_get_clientdata(client);
  	int i;
  
  	mutex_lock(&data->update_lock);
  
  	if (!(time_after(jiffies, data->last_updated + HZ * 2)
  	      || !data->valid))
  		goto END;
  
  	/* Update the voltages measured value and limits */
  	for (i = 0; i < ARRAY_SIZE(data->in); i++)
  		data->in[i][IN_READ] =
  		    w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
  
  	data->in_low_bits[IN_READ] =
  	    w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
  
  	for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
47efe8772   Guenter Roeck   hwmon: (w83793) F...
2016
  		if (!(data->has_fan & (1 << i)))
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2017
  			continue;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2018
2019
2020
2021
2022
  		data->fan[i] =
  		    w83793_read_value(client, W83793_REG_FAN(i)) << 8;
  		data->fan[i] |=
  		    w83793_read_value(client, W83793_REG_FAN(i) + 1);
  	}
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
2023
2024
2025
  	for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  		if (!(data->has_temp & (1 << i)))
  			continue;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2026
2027
  		data->temp[i][TEMP_READ] =
  		    w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
46bed4dfe   Gong Jun   hwmon/w83793: Ign...
2028
  	}
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
  
  	data->temp_low_bits =
  	    w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
  
  	for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  		if (data->has_pwm & (1 << i))
  			data->pwm[i][PWM_DUTY] =
  			    w83793_read_value(client,
  					      W83793_REG_PWM(i, PWM_DUTY));
  	}
  
  	for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
  		data->alarms[i] =
  		    w83793_read_value(client, W83793_REG_ALARM(i));
c70a8c345   Gong Jun   hwmon/w83793: Hid...
2043
2044
2045
2046
  	if (data->has_vid & 0x01)
  		data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
  	if (data->has_vid & 0x02)
  		data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2047
2048
2049
2050
2051
2052
2053
2054
  	w83793_update_nonvolatile(dev);
  	data->last_updated = jiffies;
  	data->valid = 1;
  
  END:
  	mutex_unlock(&data->update_lock);
  	return data;
  }
47efe8772   Guenter Roeck   hwmon: (w83793) F...
2055
2056
2057
2058
  /*
   * Ignore the possibility that somebody change bank outside the driver
   * Must be called with data->update_lock held, except during initialization
   */
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2059
2060
2061
  static u8 w83793_read_value(struct i2c_client *client, u16 reg)
  {
  	struct w83793_data *data = i2c_get_clientdata(client);
58a24b524   Colin Ian King   hwmon: (w83793d) ...
2062
  	u8 res;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
  	u8 new_bank = reg >> 8;
  
  	new_bank |= data->bank & 0xfc;
  	if (data->bank != new_bank) {
  		if (i2c_smbus_write_byte_data
  		    (client, W83793_REG_BANKSEL, new_bank) >= 0)
  			data->bank = new_bank;
  		else {
  			dev_err(&client->dev,
  				"set bank to %d failed, fall back "
  				"to bank %d, read reg 0x%x error
  ",
  				new_bank, data->bank, reg);
  			res = 0x0;	/* read 0x0 from the chip */
  			goto END;
  		}
  	}
  	res = i2c_smbus_read_byte_data(client, reg & 0xff);
  END:
  	return res;
  }
  
  /* Must be called with data->update_lock held, except during initialization */
  static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
  {
  	struct w83793_data *data = i2c_get_clientdata(client);
  	int res;
  	u8 new_bank = reg >> 8;
  
  	new_bank |= data->bank & 0xfc;
  	if (data->bank != new_bank) {
47efe8772   Guenter Roeck   hwmon: (w83793) F...
2094
2095
2096
  		res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
  						new_bank);
  		if (res < 0) {
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2097
2098
2099
2100
2101
2102
2103
  			dev_err(&client->dev,
  				"set bank to %d failed, fall back "
  				"to bank %d, write reg 0x%x error
  ",
  				new_bank, data->bank, reg);
  			goto END;
  		}
47efe8772   Guenter Roeck   hwmon: (w83793) F...
2104
  		data->bank = new_bank;
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2105
2106
2107
2108
2109
2110
  	}
  
  	res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
  END:
  	return res;
  }
f0967eea8   Axel Lin   hwmon: convert dr...
2111
  module_i2c_driver(w83793_driver);
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2112

5852f9609   Sven Anders   hwmon: (w83793) A...
2113
  MODULE_AUTHOR("Yuan Mu, Sven Anders");
6800c3d02   Rudolf Marek   hwmon: New Winbon...
2114
2115
  MODULE_DESCRIPTION("w83793 driver");
  MODULE_LICENSE("GPL");