blob: 0a65d164c8f094847f3042da81a479e5e20bc2c2 [file] [log] [blame]
Thomas Gleixnerff7924b2019-05-29 07:12:46 -07001// SPDX-License-Identifier: GPL-2.0-only
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002/*
Guenter Roeck47efe872012-01-15 10:48:48 -08003 * w83793.c - Linux kernel driver for hardware monitoring
4 * Copyright (C) 2006 Winbond Electronics Corp.
5 * Yuan Mu
6 * Rudolf Marek <r.marek@assembler.cz>
7 * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
8 * Watchdog driver part
9 * (Based partially on fschmd driver,
10 * Copyright 2007-2008 by Hans de Goede)
Guenter Roeck47efe872012-01-15 10:48:48 -080011 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010012
13/*
Guenter Roeck47efe872012-01-15 10:48:48 -080014 * Supports following chips:
15 *
16 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
17 * w83793 10 12 8 6 0x7b 0x5ca3 yes no
18 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010019
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/i2c.h>
24#include <linux/hwmon.h>
25#include <linux/hwmon-vid.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
28#include <linux/mutex.h>
Sven Anders5852f962010-03-05 22:17:22 +010029#include <linux/fs.h>
30#include <linux/watchdog.h>
31#include <linux/miscdevice.h>
32#include <linux/uaccess.h>
33#include <linux/kref.h>
34#include <linux/notifier.h>
35#include <linux/reboot.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020036#include <linux/jiffies.h>
Sven Anders5852f962010-03-05 22:17:22 +010037
38/* Default values */
39#define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010040
41/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050042static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
43 I2C_CLIENT_END };
Rudolf Marek6800c3d2006-12-12 18:18:30 +010044
45/* Insmod parameters */
Jean Delvare3aed1982009-01-07 16:37:32 +010046
47static unsigned short force_subclients[4];
48module_param_array(force_subclients, short, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080049MODULE_PARM_DESC(force_subclients,
50 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
Rudolf Marek6800c3d2006-12-12 18:18:30 +010051
Rusty Russell90ab5ee2012-01-13 09:32:20 +103052static bool reset;
Rudolf Marek6800c3d2006-12-12 18:18:30 +010053module_param(reset, bool, 0);
54MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
55
Sven Anders5852f962010-03-05 22:17:22 +010056static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
57module_param(timeout, int, 0);
58MODULE_PARM_DESC(timeout,
59 "Watchdog timeout in minutes. 2<= timeout <=255 (default="
60 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
61
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010062static bool nowayout = WATCHDOG_NOWAYOUT;
63module_param(nowayout, bool, 0);
Sven Anders5852f962010-03-05 22:17:22 +010064MODULE_PARM_DESC(nowayout,
65 "Watchdog cannot be stopped once started (default="
66 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
67
Rudolf Marek6800c3d2006-12-12 18:18:30 +010068/*
Guenter Roeck47efe872012-01-15 10:48:48 -080069 * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
70 * as ID, Bank Select registers
71 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010072#define W83793_REG_BANKSEL 0x00
73#define W83793_REG_VENDORID 0x0d
74#define W83793_REG_CHIPID 0x0e
75#define W83793_REG_DEVICEID 0x0f
76
77#define W83793_REG_CONFIG 0x40
78#define W83793_REG_MFC 0x58
79#define W83793_REG_FANIN_CTRL 0x5c
80#define W83793_REG_FANIN_SEL 0x5d
81#define W83793_REG_I2C_ADDR 0x0b
82#define W83793_REG_I2C_SUBADDR 0x0c
83#define W83793_REG_VID_INA 0x05
84#define W83793_REG_VID_INB 0x06
85#define W83793_REG_VID_LATCHA 0x07
86#define W83793_REG_VID_LATCHB 0x08
87#define W83793_REG_VID_CTRL 0x59
88
Sven Anders5852f962010-03-05 22:17:22 +010089#define W83793_REG_WDT_LOCK 0x01
90#define W83793_REG_WDT_ENABLE 0x02
91#define W83793_REG_WDT_STATUS 0x03
92#define W83793_REG_WDT_TIMEOUT 0x04
93
Rudolf Marek6800c3d2006-12-12 18:18:30 +010094static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
95
96#define TEMP_READ 0
97#define TEMP_CRIT 1
98#define TEMP_CRIT_HYST 2
99#define TEMP_WARN 3
100#define TEMP_WARN_HYST 4
Guenter Roeck47efe872012-01-15 10:48:48 -0800101/*
102 * only crit and crit_hyst affect real-time alarm status
103 * current crit crit_hyst warn warn_hyst
104 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100105static u16 W83793_REG_TEMP[][5] = {
106 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
107 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
108 {0x1e, 0x80, 0x81, 0x82, 0x83},
109 {0x1f, 0x84, 0x85, 0x86, 0x87},
110 {0x20, 0x88, 0x89, 0x8a, 0x8b},
111 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
112};
113
114#define W83793_REG_TEMP_LOW_BITS 0x22
115
116#define W83793_REG_BEEP(index) (0x53 + (index))
117#define W83793_REG_ALARM(index) (0x4b + (index))
118
119#define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
120#define W83793_REG_IRQ_CTRL 0x50
121#define W83793_REG_OVT_CTRL 0x51
122#define W83793_REG_OVT_BEEP 0x52
123
124#define IN_READ 0
125#define IN_MAX 1
126#define IN_LOW 2
127static const u16 W83793_REG_IN[][3] = {
128 /* Current, High, Low */
129 {0x10, 0x60, 0x61}, /* Vcore A */
130 {0x11, 0x62, 0x63}, /* Vcore B */
131 {0x12, 0x64, 0x65}, /* Vtt */
132 {0x14, 0x6a, 0x6b}, /* VSEN1 */
133 {0x15, 0x6c, 0x6d}, /* VSEN2 */
134 {0x16, 0x6e, 0x6f}, /* +3VSEN */
135 {0x17, 0x70, 0x71}, /* +12VSEN */
136 {0x18, 0x72, 0x73}, /* 5VDD */
137 {0x19, 0x74, 0x75}, /* 5VSB */
138 {0x1a, 0x76, 0x77}, /* VBAT */
139};
140
141/* Low Bits of Vcore A/B Vtt Read/High/Low */
142static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
143static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
Gong Junddca9332007-01-18 22:14:23 +0100144static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100145
146#define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
147#define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
148
149#define W83793_REG_PWM_DEFAULT 0xb2
150#define W83793_REG_PWM_ENABLE 0x207
151#define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
152#define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
153#define W83793_REG_TEMP_CRITICAL 0xc5
154
155#define PWM_DUTY 0
156#define PWM_START 1
157#define PWM_NONSTOP 2
Nicolas Kaiser5aebefb2007-11-07 13:28:59 +0100158#define PWM_STOP_TIME 3
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100159#define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
160 (nr) == 1 ? 0x220 : 0x218) + (index))
161
162/* bit field, fan1 is bit0, fan2 is bit1 ... */
163#define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
164#define W83793_REG_TEMP_TOL(index) (0x208 + (index))
165#define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
166#define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
167#define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
168#define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
169
170static inline unsigned long FAN_FROM_REG(u16 val)
171{
172 if ((val >= 0xfff) || (val == 0))
173 return 0;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100174 return 1350000UL / val;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100175}
176
177static inline u16 FAN_TO_REG(long rpm)
178{
179 if (rpm <= 0)
180 return 0x0fff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800181 return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100182}
183
184static inline unsigned long TIME_FROM_REG(u8 reg)
185{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100186 return reg * 100;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100187}
188
189static inline u8 TIME_TO_REG(unsigned long val)
190{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800191 return clamp_val((val + 50) / 100, 0, 0xff);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100192}
193
194static inline long TEMP_FROM_REG(s8 reg)
195{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100196 return reg * 1000;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100197}
198
199static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
200{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800201 return clamp_val((val + (val < 0 ? -500 : 500)) / 1000, min, max);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100202}
203
204struct w83793_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700205 struct device *hwmon_dev;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100206 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +0300207 bool valid; /* true if following fields are valid */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100208 unsigned long last_updated; /* In jiffies */
209 unsigned long last_nonvolatile; /* In jiffies, last time we update the
Guenter Roeck47efe872012-01-15 10:48:48 -0800210 * nonvolatile registers
211 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100212
213 u8 bank;
214 u8 vrm;
215 u8 vid[2];
216 u8 in[10][3]; /* Register value, read/high/low */
217 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
218
219 u16 has_fan; /* Only fan1- fan5 has own pins */
220 u16 fan[12]; /* Register value combine */
221 u16 fan_min[12]; /* Register value combine */
222
223 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
224 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
225 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
Guenter Roeck47efe872012-01-15 10:48:48 -0800226 * byte 1: Temp R1,R2 mode, each has 1 bit
227 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100228 u8 temp_critical; /* If reached all fan will be at full speed */
229 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
230
231 u8 has_pwm;
Gong Jun46bed4d2007-01-18 22:14:24 +0100232 u8 has_temp;
Gong Junc70a8c32007-01-18 22:14:24 +0100233 u8 has_vid;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100234 u8 pwm_enable; /* Register value, each Temp has 1 bit */
235 u8 pwm_uptime; /* Register value */
236 u8 pwm_downtime; /* Register value */
237 u8 pwm_default; /* All fan default pwm, next poweron valid */
238 u8 pwm[8][3]; /* Register value */
239 u8 pwm_stop_time[8];
240 u8 temp_cruise[6];
241
242 u8 alarms[5]; /* realtime status registers */
243 u8 beeps[5];
244 u8 beep_enable;
245 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
246 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
247 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
Sven Anders5852f962010-03-05 22:17:22 +0100248
249 /* watchdog */
250 struct i2c_client *client;
251 struct mutex watchdog_lock;
252 struct list_head list; /* member of the watchdog_data_list */
253 struct kref kref;
254 struct miscdevice watchdog_miscdev;
255 unsigned long watchdog_is_open;
256 char watchdog_expect_close;
257 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
258 unsigned int watchdog_caused_reboot;
259 int watchdog_timeout; /* watchdog timeout in minutes */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100260};
261
Guenter Roeck47efe872012-01-15 10:48:48 -0800262/*
263 * Somewhat ugly :( global data pointer list with all devices, so that
264 * we can find our device data as when using misc_register. There is no
265 * other method to get to one's device data from the open file-op and
266 * for usage in the reboot notifier callback.
267 */
Sven Anders5852f962010-03-05 22:17:22 +0100268static LIST_HEAD(watchdog_data_list);
269
270/* Note this lock not only protect list access, but also data.kref access */
271static DEFINE_MUTEX(watchdog_data_mutex);
272
Guenter Roeck47efe872012-01-15 10:48:48 -0800273/*
274 * Release our data struct when we're detached from the i2c client *and* all
275 * references to our watchdog device are released
276 */
Sven Anders5852f962010-03-05 22:17:22 +0100277static void w83793_release_resources(struct kref *ref)
278{
279 struct w83793_data *data = container_of(ref, struct w83793_data, kref);
280 kfree(data);
281}
282
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100283static u8 w83793_read_value(struct i2c_client *client, u16 reg);
284static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
Stephen Kitt16b237f2020-08-13 18:18:45 +0200285static int w83793_probe(struct i2c_client *client);
Jean Delvare310ec792009-12-14 21:17:23 +0100286static int w83793_detect(struct i2c_client *client,
Jean Delvarea7f13a62008-07-16 19:30:17 +0200287 struct i2c_board_info *info);
288static int w83793_remove(struct i2c_client *client);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100289static void w83793_init_client(struct i2c_client *client);
290static void w83793_update_nonvolatile(struct device *dev);
291static struct w83793_data *w83793_update_device(struct device *dev);
292
Jean Delvarea7f13a62008-07-16 19:30:17 +0200293static const struct i2c_device_id w83793_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100294 { "w83793", 0 },
Jean Delvarea7f13a62008-07-16 19:30:17 +0200295 { }
296};
297MODULE_DEVICE_TABLE(i2c, w83793_id);
298
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100299static struct i2c_driver w83793_driver = {
Jean Delvarea7f13a62008-07-16 19:30:17 +0200300 .class = I2C_CLASS_HWMON,
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100301 .driver = {
302 .name = "w83793",
303 },
Stephen Kitt16b237f2020-08-13 18:18:45 +0200304 .probe_new = w83793_probe,
Jean Delvarea7f13a62008-07-16 19:30:17 +0200305 .remove = w83793_remove,
306 .id_table = w83793_id,
307 .detect = w83793_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100308 .address_list = normal_i2c,
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100309};
310
311static ssize_t
Julia Lawall329beb72016-12-22 13:05:17 +0100312vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100313{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100314 struct w83793_data *data = dev_get_drvdata(dev);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100315 return sprintf(buf, "%d\n", data->vrm);
316}
317
318static ssize_t
319show_vid(struct device *dev, struct device_attribute *attr, char *buf)
320{
321 struct w83793_data *data = w83793_update_device(dev);
322 struct sensor_device_attribute_2 *sensor_attr =
323 to_sensor_dev_attr_2(attr);
324 int index = sensor_attr->index;
325
326 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
327}
328
329static ssize_t
Julia Lawall329beb72016-12-22 13:05:17 +0100330vrm_store(struct device *dev, struct device_attribute *attr,
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100331 const char *buf, size_t count)
332{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100333 struct w83793_data *data = dev_get_drvdata(dev);
Guenter Roeck47efe872012-01-15 10:48:48 -0800334 unsigned long val;
335 int err;
336
337 err = kstrtoul(buf, 10, &val);
338 if (err)
339 return err;
340
Axel Lin2aeee042014-08-06 08:28:46 +0800341 if (val > 255)
342 return -EINVAL;
343
Guenter Roeck47efe872012-01-15 10:48:48 -0800344 data->vrm = val;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100345 return count;
346}
347
348#define ALARM_STATUS 0
349#define BEEP_ENABLE 1
350static ssize_t
351show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
352{
353 struct w83793_data *data = w83793_update_device(dev);
354 struct sensor_device_attribute_2 *sensor_attr =
355 to_sensor_dev_attr_2(attr);
356 int nr = sensor_attr->nr;
357 int index = sensor_attr->index >> 3;
358 int bit = sensor_attr->index & 0x07;
359 u8 val;
360
Guenter Roeck47efe872012-01-15 10:48:48 -0800361 if (nr == ALARM_STATUS) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100362 val = (data->alarms[index] >> (bit)) & 1;
363 } else { /* BEEP_ENABLE */
364 val = (data->beeps[index] >> (bit)) & 1;
365 }
366
367 return sprintf(buf, "%u\n", val);
368}
369
370static ssize_t
371store_beep(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t count)
373{
374 struct i2c_client *client = to_i2c_client(dev);
375 struct w83793_data *data = i2c_get_clientdata(client);
376 struct sensor_device_attribute_2 *sensor_attr =
377 to_sensor_dev_attr_2(attr);
378 int index = sensor_attr->index >> 3;
379 int shift = sensor_attr->index & 0x07;
380 u8 beep_bit = 1 << shift;
Guenter Roeck47efe872012-01-15 10:48:48 -0800381 unsigned long val;
382 int err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100383
Guenter Roeck47efe872012-01-15 10:48:48 -0800384 err = kstrtoul(buf, 10, &val);
385 if (err)
386 return err;
387
388 if (val > 1)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100389 return -EINVAL;
390
391 mutex_lock(&data->update_lock);
392 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
393 data->beeps[index] &= ~beep_bit;
394 data->beeps[index] |= val << shift;
395 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
396 mutex_unlock(&data->update_lock);
397
398 return count;
399}
400
401static ssize_t
402show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
403{
404 struct w83793_data *data = w83793_update_device(dev);
405 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
406}
407
408static ssize_t
409store_beep_enable(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
411{
412 struct i2c_client *client = to_i2c_client(dev);
413 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800414 unsigned long val;
415 int err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100416
Guenter Roeck47efe872012-01-15 10:48:48 -0800417 err = kstrtoul(buf, 10, &val);
418 if (err)
419 return err;
420
421 if (val > 1)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100422 return -EINVAL;
423
424 mutex_lock(&data->update_lock);
425 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
426 & 0xfd;
427 data->beep_enable |= val << 1;
428 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
429 mutex_unlock(&data->update_lock);
430
431 return count;
432}
433
Jean Delvarea516dc32011-01-12 21:55:10 +0100434/* Write 0 to clear chassis alarm */
435static ssize_t
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100436store_chassis_clear(struct device *dev,
437 struct device_attribute *attr, const char *buf,
438 size_t count)
439{
440 struct i2c_client *client = to_i2c_client(dev);
441 struct w83793_data *data = i2c_get_clientdata(client);
Jean Delvarea516dc32011-01-12 21:55:10 +0100442 unsigned long val;
443 u8 reg;
Guenter Roeck47efe872012-01-15 10:48:48 -0800444 int err;
Jean Delvarea516dc32011-01-12 21:55:10 +0100445
Guenter Roeck47efe872012-01-15 10:48:48 -0800446 err = kstrtoul(buf, 10, &val);
447 if (err)
448 return err;
449 if (val)
Jean Delvarea516dc32011-01-12 21:55:10 +0100450 return -EINVAL;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100451
452 mutex_lock(&data->update_lock);
Jean Delvarea516dc32011-01-12 21:55:10 +0100453 reg = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
454 w83793_write_value(client, W83793_REG_CLR_CHASSIS, reg | 0x80);
Paul Fertser952a11c2021-09-24 22:52:02 +0300455 data->valid = false; /* Force cache refresh */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100456 mutex_unlock(&data->update_lock);
457 return count;
458}
459
460#define FAN_INPUT 0
461#define FAN_MIN 1
462static ssize_t
463show_fan(struct device *dev, struct device_attribute *attr, char *buf)
464{
465 struct sensor_device_attribute_2 *sensor_attr =
466 to_sensor_dev_attr_2(attr);
467 int nr = sensor_attr->nr;
468 int index = sensor_attr->index;
469 struct w83793_data *data = w83793_update_device(dev);
470 u16 val;
471
Guenter Roeck47efe872012-01-15 10:48:48 -0800472 if (nr == FAN_INPUT)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100473 val = data->fan[index] & 0x0fff;
Guenter Roeck47efe872012-01-15 10:48:48 -0800474 else
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100475 val = data->fan_min[index] & 0x0fff;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100476
477 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
478}
479
480static ssize_t
481store_fan_min(struct device *dev, struct device_attribute *attr,
482 const char *buf, size_t count)
483{
484 struct sensor_device_attribute_2 *sensor_attr =
485 to_sensor_dev_attr_2(attr);
486 int index = sensor_attr->index;
487 struct i2c_client *client = to_i2c_client(dev);
488 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800489 unsigned long val;
490 int err;
491
492 err = kstrtoul(buf, 10, &val);
493 if (err)
494 return err;
495 val = FAN_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100496
497 mutex_lock(&data->update_lock);
498 data->fan_min[index] = val;
499 w83793_write_value(client, W83793_REG_FAN_MIN(index),
500 (val >> 8) & 0xff);
501 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
502 mutex_unlock(&data->update_lock);
503
504 return count;
505}
506
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100507static ssize_t
508show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
509{
510 struct sensor_device_attribute_2 *sensor_attr =
511 to_sensor_dev_attr_2(attr);
512 struct w83793_data *data = w83793_update_device(dev);
513 u16 val;
514 int nr = sensor_attr->nr;
515 int index = sensor_attr->index;
516
Guenter Roeck47efe872012-01-15 10:48:48 -0800517 if (nr == PWM_STOP_TIME)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100518 val = TIME_FROM_REG(data->pwm_stop_time[index]);
519 else
520 val = (data->pwm[index][nr] & 0x3f) << 2;
521
522 return sprintf(buf, "%d\n", val);
523}
524
525static ssize_t
526store_pwm(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
528{
529 struct i2c_client *client = to_i2c_client(dev);
530 struct w83793_data *data = i2c_get_clientdata(client);
531 struct sensor_device_attribute_2 *sensor_attr =
532 to_sensor_dev_attr_2(attr);
533 int nr = sensor_attr->nr;
534 int index = sensor_attr->index;
Guenter Roeck47efe872012-01-15 10:48:48 -0800535 unsigned long val;
536 int err;
537
538 err = kstrtoul(buf, 10, &val);
539 if (err)
540 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100541
542 mutex_lock(&data->update_lock);
Guenter Roeck47efe872012-01-15 10:48:48 -0800543 if (nr == PWM_STOP_TIME) {
544 val = TIME_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100545 data->pwm_stop_time[index] = val;
546 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
547 val);
548 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800549 val = clamp_val(val, 0, 0xff) >> 2;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100550 data->pwm[index][nr] =
551 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
552 data->pwm[index][nr] |= val;
553 w83793_write_value(client, W83793_REG_PWM(index, nr),
554 data->pwm[index][nr]);
555 }
556
557 mutex_unlock(&data->update_lock);
558 return count;
559}
560
561static ssize_t
562show_temp(struct device *dev, struct device_attribute *attr, char *buf)
563{
564 struct sensor_device_attribute_2 *sensor_attr =
565 to_sensor_dev_attr_2(attr);
566 int nr = sensor_attr->nr;
567 int index = sensor_attr->index;
568 struct w83793_data *data = w83793_update_device(dev);
569 long temp = TEMP_FROM_REG(data->temp[index][nr]);
570
Guenter Roeck47efe872012-01-15 10:48:48 -0800571 if (nr == TEMP_READ && index < 4) { /* Only TD1-TD4 have low bits */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100572 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
573 temp += temp > 0 ? low : -low;
574 }
575 return sprintf(buf, "%ld\n", temp);
576}
577
578static ssize_t
579store_temp(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
581{
582 struct sensor_device_attribute_2 *sensor_attr =
583 to_sensor_dev_attr_2(attr);
584 int nr = sensor_attr->nr;
585 int index = sensor_attr->index;
586 struct i2c_client *client = to_i2c_client(dev);
587 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800588 long tmp;
589 int err;
590
591 err = kstrtol(buf, 10, &tmp);
592 if (err)
593 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100594
595 mutex_lock(&data->update_lock);
596 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
597 w83793_write_value(client, W83793_REG_TEMP[index][nr],
598 data->temp[index][nr]);
599 mutex_unlock(&data->update_lock);
600 return count;
601}
602
603/*
Guenter Roeck47efe872012-01-15 10:48:48 -0800604 * TD1-TD4
605 * each has 4 mode:(2 bits)
606 * 0: Stop monitor
607 * 1: Use internal temp sensor(default)
608 * 2: Reserved
609 * 3: Use sensor in Intel CPU and get result by PECI
610 *
611 * TR1-TR2
612 * each has 2 mode:(1 bit)
613 * 0: Disable temp sensor monitor
614 * 1: To enable temp sensors monitor
615 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100616
Gong Junddca9332007-01-18 22:14:23 +0100617/* 0 disable, 6 PECI */
618static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100619
620static ssize_t
621show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
622{
623 struct w83793_data *data = w83793_update_device(dev);
624 struct sensor_device_attribute_2 *sensor_attr =
625 to_sensor_dev_attr_2(attr);
626 int index = sensor_attr->index;
627 u8 mask = (index < 4) ? 0x03 : 0x01;
628 u8 shift = (index < 4) ? (2 * index) : (index - 4);
629 u8 tmp;
630 index = (index < 4) ? 0 : 1;
631
632 tmp = (data->temp_mode[index] >> shift) & mask;
633
634 /* for the internal sensor, found out if diode or thermistor */
Guenter Roeck47efe872012-01-15 10:48:48 -0800635 if (tmp == 1)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100636 tmp = index == 0 ? 3 : 4;
Guenter Roeck47efe872012-01-15 10:48:48 -0800637 else
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100638 tmp = TO_TEMP_MODE[tmp];
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100639
640 return sprintf(buf, "%d\n", tmp);
641}
642
643static ssize_t
644store_temp_mode(struct device *dev, struct device_attribute *attr,
645 const char *buf, size_t count)
646{
647 struct i2c_client *client = to_i2c_client(dev);
648 struct w83793_data *data = i2c_get_clientdata(client);
649 struct sensor_device_attribute_2 *sensor_attr =
650 to_sensor_dev_attr_2(attr);
651 int index = sensor_attr->index;
652 u8 mask = (index < 4) ? 0x03 : 0x01;
653 u8 shift = (index < 4) ? (2 * index) : (index - 4);
Guenter Roeck47efe872012-01-15 10:48:48 -0800654 unsigned long val;
655 int err;
656
657 err = kstrtoul(buf, 10, &val);
658 if (err)
659 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100660
661 /* transform the sysfs interface values into table above */
Gong Junddca9332007-01-18 22:14:23 +0100662 if ((val == 6) && (index < 4)) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100663 val -= 3;
664 } else if ((val == 3 && index < 4)
Gong Jun46bed4d2007-01-18 22:14:24 +0100665 || (val == 4 && index >= 4)) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100666 /* transform diode or thermistor into internal enable */
667 val = !!val;
668 } else {
669 return -EINVAL;
670 }
671
672 index = (index < 4) ? 0 : 1;
673 mutex_lock(&data->update_lock);
674 data->temp_mode[index] =
675 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
676 data->temp_mode[index] &= ~(mask << shift);
677 data->temp_mode[index] |= val << shift;
678 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
679 data->temp_mode[index]);
680 mutex_unlock(&data->update_lock);
681
682 return count;
683}
684
685#define SETUP_PWM_DEFAULT 0
686#define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
687#define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
688#define SETUP_TEMP_CRITICAL 3
689static ssize_t
690show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
691{
692 struct sensor_device_attribute_2 *sensor_attr =
693 to_sensor_dev_attr_2(attr);
694 int nr = sensor_attr->nr;
695 struct w83793_data *data = w83793_update_device(dev);
696 u32 val = 0;
697
Guenter Roeck47efe872012-01-15 10:48:48 -0800698 if (nr == SETUP_PWM_DEFAULT)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100699 val = (data->pwm_default & 0x3f) << 2;
Guenter Roeck47efe872012-01-15 10:48:48 -0800700 else if (nr == SETUP_PWM_UPTIME)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100701 val = TIME_FROM_REG(data->pwm_uptime);
Guenter Roeck47efe872012-01-15 10:48:48 -0800702 else if (nr == SETUP_PWM_DOWNTIME)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100703 val = TIME_FROM_REG(data->pwm_downtime);
Guenter Roeck47efe872012-01-15 10:48:48 -0800704 else if (nr == SETUP_TEMP_CRITICAL)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100705 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100706
707 return sprintf(buf, "%d\n", val);
708}
709
710static ssize_t
711store_sf_setup(struct device *dev, struct device_attribute *attr,
712 const char *buf, size_t count)
713{
714 struct sensor_device_attribute_2 *sensor_attr =
715 to_sensor_dev_attr_2(attr);
716 int nr = sensor_attr->nr;
717 struct i2c_client *client = to_i2c_client(dev);
718 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800719 long val;
720 int err;
721
722 err = kstrtol(buf, 10, &val);
723 if (err)
724 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100725
726 mutex_lock(&data->update_lock);
Guenter Roeck47efe872012-01-15 10:48:48 -0800727 if (nr == SETUP_PWM_DEFAULT) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100728 data->pwm_default =
729 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800730 data->pwm_default |= clamp_val(val, 0, 0xff) >> 2;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100731 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
732 data->pwm_default);
Guenter Roeck47efe872012-01-15 10:48:48 -0800733 } else if (nr == SETUP_PWM_UPTIME) {
734 data->pwm_uptime = TIME_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100735 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
736 w83793_write_value(client, W83793_REG_PWM_UPTIME,
737 data->pwm_uptime);
Guenter Roeck47efe872012-01-15 10:48:48 -0800738 } else if (nr == SETUP_PWM_DOWNTIME) {
739 data->pwm_downtime = TIME_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100740 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
741 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
742 data->pwm_downtime);
743 } else { /* SETUP_TEMP_CRITICAL */
744 data->temp_critical =
745 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
Guenter Roeck47efe872012-01-15 10:48:48 -0800746 data->temp_critical |= TEMP_TO_REG(val, 0, 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100747 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
748 data->temp_critical);
749 }
750
751 mutex_unlock(&data->update_lock);
752 return count;
753}
754
755/*
Guenter Roeck47efe872012-01-15 10:48:48 -0800756 * Temp SmartFan control
757 * TEMP_FAN_MAP
758 * Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
759 * It's possible two or more temp channels control the same fan, w83793
760 * always prefers to pick the most critical request and applies it to
761 * the related Fan.
762 * It's possible one fan is not in any mapping of 6 temp channels, this
763 * means the fan is manual mode
764 *
765 * TEMP_PWM_ENABLE
766 * Each temp channel has its own SmartFan mode, and temp channel
767 * control fans that are set by TEMP_FAN_MAP
768 * 0: SmartFanII mode
769 * 1: Thermal Cruise Mode
770 *
771 * TEMP_CRUISE
772 * Target temperature in thermal cruise mode, w83793 will try to turn
773 * fan speed to keep the temperature of target device around this
774 * temperature.
775 *
776 * TEMP_TOLERANCE
777 * If Temp higher or lower than target with this tolerance, w83793
778 * will take actions to speed up or slow down the fan to keep the
779 * temperature within the tolerance range.
780 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100781
782#define TEMP_FAN_MAP 0
783#define TEMP_PWM_ENABLE 1
784#define TEMP_CRUISE 2
785#define TEMP_TOLERANCE 3
786static ssize_t
787show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
788{
789 struct sensor_device_attribute_2 *sensor_attr =
790 to_sensor_dev_attr_2(attr);
791 int nr = sensor_attr->nr;
792 int index = sensor_attr->index;
793 struct w83793_data *data = w83793_update_device(dev);
794 u32 val;
795
Guenter Roeck47efe872012-01-15 10:48:48 -0800796 if (nr == TEMP_FAN_MAP) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100797 val = data->temp_fan_map[index];
Guenter Roeck47efe872012-01-15 10:48:48 -0800798 } else if (nr == TEMP_PWM_ENABLE) {
LABBE Corentin84fb0292013-09-27 14:36:04 +0200799 /* +2 to transform into 2 and 3 to conform with sysfs intf */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100800 val = ((data->pwm_enable >> index) & 0x01) + 2;
Guenter Roeck47efe872012-01-15 10:48:48 -0800801 } else if (nr == TEMP_CRUISE) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100802 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
803 } else { /* TEMP_TOLERANCE */
804 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
805 val = TEMP_FROM_REG(val & 0x0f);
806 }
807 return sprintf(buf, "%d\n", val);
808}
809
810static ssize_t
811store_sf_ctrl(struct device *dev, struct device_attribute *attr,
812 const char *buf, size_t count)
813{
814 struct sensor_device_attribute_2 *sensor_attr =
815 to_sensor_dev_attr_2(attr);
816 int nr = sensor_attr->nr;
817 int index = sensor_attr->index;
818 struct i2c_client *client = to_i2c_client(dev);
819 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800820 long val;
821 int err;
822
823 err = kstrtol(buf, 10, &val);
824 if (err)
825 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100826
827 mutex_lock(&data->update_lock);
Guenter Roeck47efe872012-01-15 10:48:48 -0800828 if (nr == TEMP_FAN_MAP) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800829 val = clamp_val(val, 0, 255);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100830 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
831 data->temp_fan_map[index] = val;
Guenter Roeck47efe872012-01-15 10:48:48 -0800832 } else if (nr == TEMP_PWM_ENABLE) {
833 if (val == 2 || val == 3) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100834 data->pwm_enable =
835 w83793_read_value(client, W83793_REG_PWM_ENABLE);
836 if (val - 2)
837 data->pwm_enable |= 1 << index;
838 else
839 data->pwm_enable &= ~(1 << index);
840 w83793_write_value(client, W83793_REG_PWM_ENABLE,
841 data->pwm_enable);
842 } else {
843 mutex_unlock(&data->update_lock);
844 return -EINVAL;
845 }
Guenter Roeck47efe872012-01-15 10:48:48 -0800846 } else if (nr == TEMP_CRUISE) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100847 data->temp_cruise[index] =
848 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100849 data->temp_cruise[index] &= 0x80;
Guenter Roeck47efe872012-01-15 10:48:48 -0800850 data->temp_cruise[index] |= TEMP_TO_REG(val, 0, 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100851
852 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
853 data->temp_cruise[index]);
854 } else { /* TEMP_TOLERANCE */
855 int i = index >> 1;
856 u8 shift = (index & 0x01) ? 4 : 0;
857 data->tolerance[i] =
858 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
859
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100860 data->tolerance[i] &= ~(0x0f << shift);
Guenter Roeck47efe872012-01-15 10:48:48 -0800861 data->tolerance[i] |= TEMP_TO_REG(val, 0, 0x0f) << shift;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100862 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
863 data->tolerance[i]);
864 }
865
866 mutex_unlock(&data->update_lock);
867 return count;
868}
869
870static ssize_t
871show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
872{
873 struct sensor_device_attribute_2 *sensor_attr =
874 to_sensor_dev_attr_2(attr);
875 int nr = sensor_attr->nr;
876 int index = sensor_attr->index;
877 struct w83793_data *data = w83793_update_device(dev);
878
879 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
880}
881
882static ssize_t
883store_sf2_pwm(struct device *dev, struct device_attribute *attr,
884 const char *buf, size_t count)
885{
886 struct i2c_client *client = to_i2c_client(dev);
887 struct w83793_data *data = i2c_get_clientdata(client);
888 struct sensor_device_attribute_2 *sensor_attr =
889 to_sensor_dev_attr_2(attr);
890 int nr = sensor_attr->nr;
891 int index = sensor_attr->index;
Guenter Roeck47efe872012-01-15 10:48:48 -0800892 unsigned long val;
893 int err;
894
895 err = kstrtoul(buf, 10, &val);
896 if (err)
897 return err;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800898 val = clamp_val(val, 0, 0xff) >> 2;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100899
900 mutex_lock(&data->update_lock);
901 data->sf2_pwm[index][nr] =
902 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
903 data->sf2_pwm[index][nr] |= val;
904 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
905 data->sf2_pwm[index][nr]);
906 mutex_unlock(&data->update_lock);
907 return count;
908}
909
910static ssize_t
911show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
912{
913 struct sensor_device_attribute_2 *sensor_attr =
914 to_sensor_dev_attr_2(attr);
915 int nr = sensor_attr->nr;
916 int index = sensor_attr->index;
917 struct w83793_data *data = w83793_update_device(dev);
918
919 return sprintf(buf, "%ld\n",
920 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
921}
922
923static ssize_t
924store_sf2_temp(struct device *dev, struct device_attribute *attr,
925 const char *buf, size_t count)
926{
927 struct i2c_client *client = to_i2c_client(dev);
928 struct w83793_data *data = i2c_get_clientdata(client);
929 struct sensor_device_attribute_2 *sensor_attr =
930 to_sensor_dev_attr_2(attr);
931 int nr = sensor_attr->nr;
932 int index = sensor_attr->index;
Guenter Roeck47efe872012-01-15 10:48:48 -0800933 long val;
934 int err;
935
936 err = kstrtol(buf, 10, &val);
937 if (err)
938 return err;
939 val = TEMP_TO_REG(val, 0, 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100940
941 mutex_lock(&data->update_lock);
942 data->sf2_temp[index][nr] =
943 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
944 data->sf2_temp[index][nr] |= val;
945 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
946 data->sf2_temp[index][nr]);
947 mutex_unlock(&data->update_lock);
948 return count;
949}
950
951/* only Vcore A/B and Vtt have additional 2 bits precision */
952static ssize_t
953show_in(struct device *dev, struct device_attribute *attr, char *buf)
954{
955 struct sensor_device_attribute_2 *sensor_attr =
956 to_sensor_dev_attr_2(attr);
957 int nr = sensor_attr->nr;
958 int index = sensor_attr->index;
959 struct w83793_data *data = w83793_update_device(dev);
960 u16 val = data->in[index][nr];
961
962 if (index < 3) {
963 val <<= 2;
964 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
965 }
Gong Junddca9332007-01-18 22:14:23 +0100966 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
967 val = val * scale_in[index] + scale_in_add[index];
968 return sprintf(buf, "%d\n", val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100969}
970
971static ssize_t
972store_in(struct device *dev, struct device_attribute *attr,
973 const char *buf, size_t count)
974{
975 struct sensor_device_attribute_2 *sensor_attr =
976 to_sensor_dev_attr_2(attr);
977 int nr = sensor_attr->nr;
978 int index = sensor_attr->index;
979 struct i2c_client *client = to_i2c_client(dev);
980 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800981 unsigned long val;
982 int err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100983
Guenter Roeck47efe872012-01-15 10:48:48 -0800984 err = kstrtoul(buf, 10, &val);
985 if (err)
986 return err;
987 val = (val + scale_in[index] / 2) / scale_in[index];
988
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100989 mutex_lock(&data->update_lock);
990 if (index > 2) {
Gong Junddca9332007-01-18 22:14:23 +0100991 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
Guenter Roeck47efe872012-01-15 10:48:48 -0800992 if (nr == 1 || nr == 2)
Gong Junddca9332007-01-18 22:14:23 +0100993 val -= scale_in_add[index] / scale_in[index];
Guenter Roeck2a844c12013-01-09 08:09:34 -0800994 val = clamp_val(val, 0, 255);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100995 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800996 val = clamp_val(val, 0, 0x3FF);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100997 data->in_low_bits[nr] =
998 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
999 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
1000 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
1001 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
1002 data->in_low_bits[nr]);
1003 val >>= 2;
1004 }
1005 data->in[index][nr] = val;
1006 w83793_write_value(client, W83793_REG_IN[index][nr],
1007 data->in[index][nr]);
1008 mutex_unlock(&data->update_lock);
1009 return count;
1010}
1011
1012#define NOT_USED -1
1013
1014#define SENSOR_ATTR_IN(index) \
1015 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1016 IN_READ, index), \
1017 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1018 store_in, IN_MAX, index), \
1019 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1020 store_in, IN_LOW, index), \
1021 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1022 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
1023 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1024 show_alarm_beep, store_beep, BEEP_ENABLE, \
1025 index + ((index > 2) ? 1 : 0))
1026
1027#define SENSOR_ATTR_FAN(index) \
1028 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1029 NULL, ALARM_STATUS, index + 17), \
1030 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1031 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
1032 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1033 NULL, FAN_INPUT, index - 1), \
1034 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1035 show_fan, store_fan_min, FAN_MIN, index - 1)
1036
1037#define SENSOR_ATTR_PWM(index) \
1038 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1039 store_pwm, PWM_DUTY, index - 1), \
1040 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1041 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1042 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1043 show_pwm, store_pwm, PWM_START, index - 1), \
1044 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1045 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
1046
1047#define SENSOR_ATTR_TEMP(index) \
1048 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1049 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1050 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1051 NULL, TEMP_READ, index - 1), \
1052 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1053 store_temp, TEMP_CRIT, index - 1), \
1054 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1055 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1056 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
1057 store_temp, TEMP_WARN, index - 1), \
1058 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
1059 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1060 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1061 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
1062 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1063 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
1064 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
1065 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
1066 TEMP_FAN_MAP, index - 1), \
1067 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1068 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
1069 index - 1), \
1070 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
1071 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
1072 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
1073 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
1074 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1075 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
1076 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1077 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
1078 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1079 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
1080 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1081 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
1082 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1083 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
1084 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1085 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
1086 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1087 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
1088 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1089 show_sf2_temp, store_sf2_temp, 0, index - 1), \
1090 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1091 show_sf2_temp, store_sf2_temp, 1, index - 1), \
1092 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1093 show_sf2_temp, store_sf2_temp, 2, index - 1), \
1094 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1095 show_sf2_temp, store_sf2_temp, 3, index - 1), \
1096 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1097 show_sf2_temp, store_sf2_temp, 4, index - 1), \
1098 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1099 show_sf2_temp, store_sf2_temp, 5, index - 1), \
1100 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1101 show_sf2_temp, store_sf2_temp, 6, index - 1)
1102
1103static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
1104 SENSOR_ATTR_IN(0),
1105 SENSOR_ATTR_IN(1),
1106 SENSOR_ATTR_IN(2),
1107 SENSOR_ATTR_IN(3),
1108 SENSOR_ATTR_IN(4),
1109 SENSOR_ATTR_IN(5),
1110 SENSOR_ATTR_IN(6),
1111 SENSOR_ATTR_IN(7),
1112 SENSOR_ATTR_IN(8),
1113 SENSOR_ATTR_IN(9),
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001114 SENSOR_ATTR_FAN(1),
1115 SENSOR_ATTR_FAN(2),
1116 SENSOR_ATTR_FAN(3),
1117 SENSOR_ATTR_FAN(4),
1118 SENSOR_ATTR_FAN(5),
1119 SENSOR_ATTR_PWM(1),
1120 SENSOR_ATTR_PWM(2),
1121 SENSOR_ATTR_PWM(3),
1122};
1123
Gong Jun46bed4d2007-01-18 22:14:24 +01001124static struct sensor_device_attribute_2 w83793_temp[] = {
1125 SENSOR_ATTR_TEMP(1),
1126 SENSOR_ATTR_TEMP(2),
1127 SENSOR_ATTR_TEMP(3),
1128 SENSOR_ATTR_TEMP(4),
1129 SENSOR_ATTR_TEMP(5),
1130 SENSOR_ATTR_TEMP(6),
1131};
1132
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001133/* Fan6-Fan12 */
1134static struct sensor_device_attribute_2 w83793_left_fan[] = {
1135 SENSOR_ATTR_FAN(6),
1136 SENSOR_ATTR_FAN(7),
1137 SENSOR_ATTR_FAN(8),
1138 SENSOR_ATTR_FAN(9),
1139 SENSOR_ATTR_FAN(10),
1140 SENSOR_ATTR_FAN(11),
1141 SENSOR_ATTR_FAN(12),
1142};
1143
1144/* Pwm4-Pwm8 */
1145static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1146 SENSOR_ATTR_PWM(4),
1147 SENSOR_ATTR_PWM(5),
1148 SENSOR_ATTR_PWM(6),
1149 SENSOR_ATTR_PWM(7),
1150 SENSOR_ATTR_PWM(8),
1151};
1152
Gong Junc70a8c32007-01-18 22:14:24 +01001153static struct sensor_device_attribute_2 w83793_vid[] = {
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001154 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1155 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
Gong Junc70a8c32007-01-18 22:14:24 +01001156};
Julia Lawall329beb72016-12-22 13:05:17 +01001157static DEVICE_ATTR_RW(vrm);
Gong Junc70a8c32007-01-18 22:14:24 +01001158
1159static struct sensor_device_attribute_2 sda_single_files[] = {
Jean Delvarea516dc32011-01-12 21:55:10 +01001160 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001161 store_chassis_clear, ALARM_STATUS, 30),
1162 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1163 store_beep_enable, NOT_USED, NOT_USED),
1164 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1165 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1166 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1167 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1168 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1169 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1170 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1171 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1172};
1173
1174static void w83793_init_client(struct i2c_client *client)
1175{
Guenter Roeck47efe872012-01-15 10:48:48 -08001176 if (reset)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001177 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001178
1179 /* Start monitoring */
1180 w83793_write_value(client, W83793_REG_CONFIG,
1181 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001182}
1183
Sven Anders5852f962010-03-05 22:17:22 +01001184/*
1185 * Watchdog routines
1186 */
1187
1188static int watchdog_set_timeout(struct w83793_data *data, int timeout)
1189{
Dan Carpenter26336c82013-10-19 12:12:22 +03001190 unsigned int mtimeout;
1191 int ret;
Sven Anders5852f962010-03-05 22:17:22 +01001192
1193 mtimeout = DIV_ROUND_UP(timeout, 60);
1194
1195 if (mtimeout > 255)
1196 return -EINVAL;
1197
1198 mutex_lock(&data->watchdog_lock);
1199 if (!data->client) {
1200 ret = -ENODEV;
1201 goto leave;
1202 }
1203
1204 data->watchdog_timeout = mtimeout;
1205
1206 /* Set Timeout value (in Minutes) */
1207 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1208 data->watchdog_timeout);
1209
1210 ret = mtimeout * 60;
1211
1212leave:
1213 mutex_unlock(&data->watchdog_lock);
1214 return ret;
1215}
1216
1217static int watchdog_get_timeout(struct w83793_data *data)
1218{
1219 int timeout;
1220
1221 mutex_lock(&data->watchdog_lock);
1222 timeout = data->watchdog_timeout * 60;
1223 mutex_unlock(&data->watchdog_lock);
1224
1225 return timeout;
1226}
1227
1228static int watchdog_trigger(struct w83793_data *data)
1229{
1230 int ret = 0;
1231
1232 mutex_lock(&data->watchdog_lock);
1233 if (!data->client) {
1234 ret = -ENODEV;
1235 goto leave;
1236 }
1237
1238 /* Set Timeout value (in Minutes) */
1239 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1240 data->watchdog_timeout);
1241
1242leave:
1243 mutex_unlock(&data->watchdog_lock);
1244 return ret;
1245}
1246
1247static int watchdog_enable(struct w83793_data *data)
1248{
1249 int ret = 0;
1250
1251 mutex_lock(&data->watchdog_lock);
1252 if (!data->client) {
1253 ret = -ENODEV;
1254 goto leave;
1255 }
1256
1257 /* Set initial timeout */
1258 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1259 data->watchdog_timeout);
1260
1261 /* Enable Soft Watchdog */
1262 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
1263
1264leave:
1265 mutex_unlock(&data->watchdog_lock);
1266 return ret;
1267}
1268
1269static int watchdog_disable(struct w83793_data *data)
1270{
1271 int ret = 0;
1272
1273 mutex_lock(&data->watchdog_lock);
1274 if (!data->client) {
1275 ret = -ENODEV;
1276 goto leave;
1277 }
1278
1279 /* Disable Soft Watchdog */
1280 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
1281
1282leave:
1283 mutex_unlock(&data->watchdog_lock);
1284 return ret;
1285}
1286
1287static int watchdog_open(struct inode *inode, struct file *filp)
1288{
1289 struct w83793_data *pos, *data = NULL;
1290 int watchdog_is_open;
1291
Guenter Roeck47efe872012-01-15 10:48:48 -08001292 /*
1293 * We get called from drivers/char/misc.c with misc_mtx hold, and we
1294 * call misc_register() from w83793_probe() with watchdog_data_mutex
1295 * hold, as misc_register() takes the misc_mtx lock, this is a possible
1296 * deadlock, so we use mutex_trylock here.
1297 */
Sven Anders5852f962010-03-05 22:17:22 +01001298 if (!mutex_trylock(&watchdog_data_mutex))
1299 return -ERESTARTSYS;
1300 list_for_each_entry(pos, &watchdog_data_list, list) {
1301 if (pos->watchdog_miscdev.minor == iminor(inode)) {
1302 data = pos;
1303 break;
1304 }
1305 }
1306
1307 /* Check, if device is already open */
1308 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
1309
Guenter Roeck47efe872012-01-15 10:48:48 -08001310 /*
1311 * Increase data reference counter (if not already done).
1312 * Note we can never not have found data, so we don't check for this
1313 */
Sven Anders5852f962010-03-05 22:17:22 +01001314 if (!watchdog_is_open)
1315 kref_get(&data->kref);
1316
1317 mutex_unlock(&watchdog_data_mutex);
1318
1319 /* Check, if device is already open and possibly issue error */
1320 if (watchdog_is_open)
1321 return -EBUSY;
1322
1323 /* Enable Soft Watchdog */
1324 watchdog_enable(data);
1325
1326 /* Store pointer to data into filp's private data */
1327 filp->private_data = data;
1328
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +03001329 return stream_open(inode, filp);
Sven Anders5852f962010-03-05 22:17:22 +01001330}
1331
1332static int watchdog_close(struct inode *inode, struct file *filp)
1333{
1334 struct w83793_data *data = filp->private_data;
1335
1336 if (data->watchdog_expect_close) {
1337 watchdog_disable(data);
1338 data->watchdog_expect_close = 0;
1339 } else {
1340 watchdog_trigger(data);
1341 dev_crit(&data->client->dev,
1342 "unexpected close, not stopping watchdog!\n");
1343 }
1344
1345 clear_bit(0, &data->watchdog_is_open);
1346
1347 /* Decrease data reference counter */
1348 mutex_lock(&watchdog_data_mutex);
1349 kref_put(&data->kref, w83793_release_resources);
1350 mutex_unlock(&watchdog_data_mutex);
1351
1352 return 0;
1353}
1354
1355static ssize_t watchdog_write(struct file *filp, const char __user *buf,
1356 size_t count, loff_t *offset)
1357{
Dan Carpenter3f7cd7e2010-03-29 22:03:03 +02001358 ssize_t ret;
Sven Anders5852f962010-03-05 22:17:22 +01001359 struct w83793_data *data = filp->private_data;
1360
1361 if (count) {
1362 if (!nowayout) {
1363 size_t i;
1364
1365 /* Clear it in case it was set with a previous write */
1366 data->watchdog_expect_close = 0;
1367
1368 for (i = 0; i != count; i++) {
1369 char c;
1370 if (get_user(c, buf + i))
1371 return -EFAULT;
1372 if (c == 'V')
1373 data->watchdog_expect_close = 1;
1374 }
1375 }
1376 ret = watchdog_trigger(data);
1377 if (ret < 0)
1378 return ret;
1379 }
1380 return count;
1381}
1382
Arnd Bergmann55929332010-04-27 00:24:05 +02001383static long watchdog_ioctl(struct file *filp, unsigned int cmd,
1384 unsigned long arg)
Sven Anders5852f962010-03-05 22:17:22 +01001385{
Jean Delvare36c7fe12011-01-12 21:55:11 +01001386 struct watchdog_info ident = {
Sven Anders5852f962010-03-05 22:17:22 +01001387 .options = WDIOF_KEEPALIVEPING |
1388 WDIOF_SETTIMEOUT |
1389 WDIOF_CARDRESET,
1390 .identity = "w83793 watchdog"
1391 };
1392
1393 int val, ret = 0;
1394 struct w83793_data *data = filp->private_data;
1395
1396 switch (cmd) {
1397 case WDIOC_GETSUPPORT:
1398 if (!nowayout)
1399 ident.options |= WDIOF_MAGICCLOSE;
1400 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
1401 ret = -EFAULT;
1402 break;
1403
1404 case WDIOC_GETSTATUS:
1405 val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
1406 ret = put_user(val, (int __user *)arg);
1407 break;
1408
1409 case WDIOC_GETBOOTSTATUS:
1410 ret = put_user(0, (int __user *)arg);
1411 break;
1412
1413 case WDIOC_KEEPALIVE:
1414 ret = watchdog_trigger(data);
1415 break;
1416
1417 case WDIOC_GETTIMEOUT:
1418 val = watchdog_get_timeout(data);
1419 ret = put_user(val, (int __user *)arg);
1420 break;
1421
1422 case WDIOC_SETTIMEOUT:
1423 if (get_user(val, (int __user *)arg)) {
1424 ret = -EFAULT;
1425 break;
1426 }
1427 ret = watchdog_set_timeout(data, val);
1428 if (ret > 0)
1429 ret = put_user(ret, (int __user *)arg);
1430 break;
1431
1432 case WDIOC_SETOPTIONS:
1433 if (get_user(val, (int __user *)arg)) {
1434 ret = -EFAULT;
1435 break;
1436 }
1437
1438 if (val & WDIOS_DISABLECARD)
1439 ret = watchdog_disable(data);
1440 else if (val & WDIOS_ENABLECARD)
1441 ret = watchdog_enable(data);
1442 else
1443 ret = -EINVAL;
1444
1445 break;
1446 default:
1447 ret = -ENOTTY;
1448 }
Sven Anders5852f962010-03-05 22:17:22 +01001449 return ret;
1450}
1451
1452static const struct file_operations watchdog_fops = {
1453 .owner = THIS_MODULE,
1454 .llseek = no_llseek,
1455 .open = watchdog_open,
1456 .release = watchdog_close,
1457 .write = watchdog_write,
Arnd Bergmann55929332010-04-27 00:24:05 +02001458 .unlocked_ioctl = watchdog_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +02001459 .compat_ioctl = compat_ptr_ioctl,
Sven Anders5852f962010-03-05 22:17:22 +01001460};
1461
1462/*
1463 * Notifier for system down
1464 */
1465
1466static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
1467 void *unused)
1468{
1469 struct w83793_data *data = NULL;
1470
1471 if (code == SYS_DOWN || code == SYS_HALT) {
1472
1473 /* Disable each registered watchdog */
1474 mutex_lock(&watchdog_data_mutex);
1475 list_for_each_entry(data, &watchdog_data_list, list) {
1476 if (data->watchdog_miscdev.minor)
1477 watchdog_disable(data);
1478 }
1479 mutex_unlock(&watchdog_data_mutex);
1480 }
1481
1482 return NOTIFY_DONE;
1483}
1484
1485/*
1486 * The WDT needs to learn about soft shutdowns in order to
1487 * turn the timebomb registers off.
1488 */
1489
1490static struct notifier_block watchdog_notifier = {
1491 .notifier_call = watchdog_notify_sys,
1492};
1493
1494/*
1495 * Init / remove routines
1496 */
1497
Jean Delvarea7f13a62008-07-16 19:30:17 +02001498static int w83793_remove(struct i2c_client *client)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001499{
1500 struct w83793_data *data = i2c_get_clientdata(client);
1501 struct device *dev = &client->dev;
Sven Anders5852f962010-03-05 22:17:22 +01001502 int i, tmp;
1503
1504 /* Unregister the watchdog (if registered) */
1505 if (data->watchdog_miscdev.minor) {
1506 misc_deregister(&data->watchdog_miscdev);
1507
1508 if (data->watchdog_is_open) {
1509 dev_warn(&client->dev,
1510 "i2c client detached with watchdog open! "
1511 "Stopping watchdog.\n");
1512 watchdog_disable(data);
1513 }
1514
1515 mutex_lock(&watchdog_data_mutex);
1516 list_del(&data->list);
1517 mutex_unlock(&watchdog_data_mutex);
1518
1519 /* Tell the watchdog code the client is gone */
1520 mutex_lock(&data->watchdog_lock);
1521 data->client = NULL;
1522 mutex_unlock(&data->watchdog_lock);
1523 }
1524
1525 /* Reset Configuration Register to Disable Watch Dog Registers */
1526 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1527 w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
1528
1529 unregister_reboot_notifier(&watchdog_notifier);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001530
Jean Delvarea7f13a62008-07-16 19:30:17 +02001531 hwmon_device_unregister(data->hwmon_dev);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001532
Jean Delvarea7f13a62008-07-16 19:30:17 +02001533 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1534 device_remove_file(dev,
1535 &w83793_sensor_attr_2[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001536
Jean Delvarea7f13a62008-07-16 19:30:17 +02001537 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1538 device_remove_file(dev, &sda_single_files[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001539
Jean Delvarea7f13a62008-07-16 19:30:17 +02001540 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1541 device_remove_file(dev, &w83793_vid[i].dev_attr);
1542 device_remove_file(dev, &dev_attr_vrm);
Gong Junc70a8c32007-01-18 22:14:24 +01001543
Jean Delvarea7f13a62008-07-16 19:30:17 +02001544 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1545 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001546
Jean Delvarea7f13a62008-07-16 19:30:17 +02001547 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1548 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
Gong Jun46bed4d2007-01-18 22:14:24 +01001549
Jean Delvarea7f13a62008-07-16 19:30:17 +02001550 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1551 device_remove_file(dev, &w83793_temp[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001552
Sven Anders5852f962010-03-05 22:17:22 +01001553 /* Decrease data reference counter */
1554 mutex_lock(&watchdog_data_mutex);
1555 kref_put(&data->kref, w83793_release_resources);
1556 mutex_unlock(&watchdog_data_mutex);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001557
1558 return 0;
1559}
1560
1561static int
Jean Delvarea7f13a62008-07-16 19:30:17 +02001562w83793_detect_subclients(struct i2c_client *client)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001563{
Wolfram Sangcf48d172019-09-03 20:12:56 +02001564 int i, id;
Jean Delvarea7f13a62008-07-16 19:30:17 +02001565 int address = client->addr;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001566 u8 tmp;
Jean Delvarea7f13a62008-07-16 19:30:17 +02001567 struct i2c_adapter *adapter = client->adapter;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001568
1569 id = i2c_adapter_id(adapter);
1570 if (force_subclients[0] == id && force_subclients[1] == address) {
1571 for (i = 2; i <= 3; i++) {
1572 if (force_subclients[i] < 0x48
1573 || force_subclients[i] > 0x4f) {
1574 dev_err(&client->dev,
1575 "invalid subclient "
1576 "address %d; must be 0x48-0x4f\n",
1577 force_subclients[i]);
Wolfram Sangcf48d172019-09-03 20:12:56 +02001578 return -EINVAL;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001579 }
1580 }
1581 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1582 (force_subclients[2] & 0x07) |
1583 ((force_subclients[3] & 0x07) << 4));
1584 }
1585
1586 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
Nadezda Lutovinovadd4d7472021-09-21 18:51:53 +03001587
1588 if (!(tmp & 0x88) && (tmp & 0x7) == ((tmp >> 4) & 0x7)) {
1589 dev_err(&client->dev,
1590 "duplicate addresses 0x%x, use force_subclient\n", 0x48 + (tmp & 0x7));
1591 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001592 }
1593
Nadezda Lutovinovadd4d7472021-09-21 18:51:53 +03001594 if (!(tmp & 0x08))
1595 devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + (tmp & 0x7));
1596
1597 if (!(tmp & 0x80))
1598 devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + ((tmp >> 4) & 0x7));
1599
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001600 return 0;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001601}
1602
Jean Delvarea7f13a62008-07-16 19:30:17 +02001603/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001604static int w83793_detect(struct i2c_client *client,
Jean Delvarea7f13a62008-07-16 19:30:17 +02001605 struct i2c_board_info *info)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001606{
Jean Delvare52df6442009-12-09 20:35:57 +01001607 u8 tmp, bank, chip_id;
Jean Delvarea7f13a62008-07-16 19:30:17 +02001608 struct i2c_adapter *adapter = client->adapter;
1609 unsigned short address = client->addr;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001610
Guenter Roeck47efe872012-01-15 10:48:48 -08001611 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea7f13a62008-07-16 19:30:17 +02001612 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001613
Jean Delvarea7f13a62008-07-16 19:30:17 +02001614 bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001615
Jean Delvare52df6442009-12-09 20:35:57 +01001616 tmp = bank & 0x80 ? 0x5c : 0xa3;
1617 /* Check Winbond vendor ID */
1618 if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
1619 pr_debug("w83793: Detection failed at check vendor id\n");
1620 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001621 }
1622
Guenter Roeck47efe872012-01-15 10:48:48 -08001623 /*
1624 * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1625 * should match
1626 */
Jean Delvare52df6442009-12-09 20:35:57 +01001627 if ((bank & 0x07) == 0
1628 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1629 (address << 1)) {
1630 pr_debug("w83793: Detection failed at check i2c addr\n");
1631 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001632 }
1633
Jean Delvare52df6442009-12-09 20:35:57 +01001634 /* Determine the chip type now */
1635 chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
1636 if (chip_id != 0x7b)
1637 return -ENODEV;
1638
Jean Delvarea7f13a62008-07-16 19:30:17 +02001639 strlcpy(info->type, "w83793", I2C_NAME_SIZE);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001640
Jean Delvarea7f13a62008-07-16 19:30:17 +02001641 return 0;
1642}
1643
Stephen Kitt16b237f2020-08-13 18:18:45 +02001644static int w83793_probe(struct i2c_client *client)
Jean Delvarea7f13a62008-07-16 19:30:17 +02001645{
1646 struct device *dev = &client->dev;
Colin Ian King7a76a7f2017-09-22 15:36:20 +01001647 static const int watchdog_minors[] = {
1648 WATCHDOG_MINOR, 212, 213, 214, 215
1649 };
Jean Delvarea7f13a62008-07-16 19:30:17 +02001650 struct w83793_data *data;
1651 int i, tmp, val, err;
1652 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1653 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1654 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1655
1656 data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
1657 if (!data) {
1658 err = -ENOMEM;
1659 goto exit;
1660 }
1661
1662 i2c_set_clientdata(client, data);
1663 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001664 mutex_init(&data->update_lock);
Sven Anders5852f962010-03-05 22:17:22 +01001665 mutex_init(&data->watchdog_lock);
1666 INIT_LIST_HEAD(&data->list);
1667 kref_init(&data->kref);
1668
Guenter Roeck47efe872012-01-15 10:48:48 -08001669 /*
1670 * Store client pointer in our data struct for watchdog usage
1671 * (where the client is found through a data ptr instead of the
1672 * otherway around)
1673 */
Sven Anders5852f962010-03-05 22:17:22 +01001674 data->client = client;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001675
Jean Delvarea7f13a62008-07-16 19:30:17 +02001676 err = w83793_detect_subclients(client);
1677 if (err)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001678 goto free_mem;
1679
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001680 /* Initialize the chip */
1681 w83793_init_client(client);
1682
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001683 /*
Guenter Roeck47efe872012-01-15 10:48:48 -08001684 * Only fan 1-5 has their own input pins,
1685 * Pwm 1-3 has their own pins
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001686 */
1687 data->has_fan = 0x1f;
1688 data->has_pwm = 0x07;
1689 tmp = w83793_read_value(client, W83793_REG_MFC);
1690 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1691
1692 /* check the function of pins 49-56 */
Jean Delvare93c75a42008-02-12 11:25:07 +01001693 if (tmp & 0x80) {
1694 data->has_vid |= 0x2; /* has VIDB */
1695 } else {
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001696 data->has_pwm |= 0x18; /* pwm 4,5 */
1697 if (val & 0x01) { /* fan 6 */
1698 data->has_fan |= 0x20;
1699 data->has_pwm |= 0x20;
1700 }
1701 if (val & 0x02) { /* fan 7 */
1702 data->has_fan |= 0x40;
1703 data->has_pwm |= 0x40;
1704 }
1705 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1706 data->has_fan |= 0x80;
1707 data->has_pwm |= 0x80;
1708 }
1709 }
1710
Jean Delvare93c75a42008-02-12 11:25:07 +01001711 /* check the function of pins 37-40 */
1712 if (!(tmp & 0x29))
1713 data->has_vid |= 0x1; /* has VIDA */
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001714 if (0x08 == (tmp & 0x0c)) {
1715 if (val & 0x08) /* fan 9 */
1716 data->has_fan |= 0x100;
1717 if (val & 0x10) /* fan 10 */
1718 data->has_fan |= 0x200;
1719 }
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001720 if (0x20 == (tmp & 0x30)) {
1721 if (val & 0x20) /* fan 11 */
1722 data->has_fan |= 0x400;
1723 if (val & 0x40) /* fan 12 */
1724 data->has_fan |= 0x800;
1725 }
1726
1727 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1728 data->has_fan |= 0x80;
1729 data->has_pwm |= 0x80;
1730 }
1731
Rudolf Marekc9294312007-01-18 22:14:24 +01001732 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1733 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1734 data->has_fan |= 0x100;
1735 }
1736 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1737 data->has_fan |= 0x200;
1738 }
1739 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1740 data->has_fan |= 0x400;
1741 }
1742 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1743 data->has_fan |= 0x800;
1744 }
1745
Gong Jun46bed4d2007-01-18 22:14:24 +01001746 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
Guenter Roeck47efe872012-01-15 10:48:48 -08001747 tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
Gong Jun46bed4d2007-01-18 22:14:24 +01001748 if (tmp & 0x01)
1749 data->has_temp |= 0x01;
1750 if (tmp & 0x04)
1751 data->has_temp |= 0x02;
1752 if (tmp & 0x10)
1753 data->has_temp |= 0x04;
1754 if (tmp & 0x40)
1755 data->has_temp |= 0x08;
1756
Guenter Roeck47efe872012-01-15 10:48:48 -08001757 tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
Gong Jun46bed4d2007-01-18 22:14:24 +01001758 if (tmp & 0x01)
1759 data->has_temp |= 0x10;
1760 if (tmp & 0x02)
1761 data->has_temp |= 0x20;
1762
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001763 /* Register sysfs hooks */
1764 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1765 err = device_create_file(dev,
1766 &w83793_sensor_attr_2[i].dev_attr);
1767 if (err)
1768 goto exit_remove;
1769 }
1770
Gong Junc70a8c32007-01-18 22:14:24 +01001771 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1772 if (!(data->has_vid & (1 << i)))
1773 continue;
1774 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1775 if (err)
1776 goto exit_remove;
1777 }
Jean Delvare93c75a42008-02-12 11:25:07 +01001778 if (data->has_vid) {
1779 data->vrm = vid_which_vrm();
1780 err = device_create_file(dev, &dev_attr_vrm);
1781 if (err)
1782 goto exit_remove;
1783 }
Gong Junc70a8c32007-01-18 22:14:24 +01001784
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001785 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1786 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1787 if (err)
1788 goto exit_remove;
1789
1790 }
1791
Gong Jun46bed4d2007-01-18 22:14:24 +01001792 for (i = 0; i < 6; i++) {
1793 int j;
1794 if (!(data->has_temp & (1 << i)))
1795 continue;
1796 for (j = 0; j < files_temp; j++) {
1797 err = device_create_file(dev,
1798 &w83793_temp[(i) * files_temp
1799 + j].dev_attr);
1800 if (err)
1801 goto exit_remove;
1802 }
1803 }
1804
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001805 for (i = 5; i < 12; i++) {
1806 int j;
1807 if (!(data->has_fan & (1 << i)))
1808 continue;
1809 for (j = 0; j < files_fan; j++) {
1810 err = device_create_file(dev,
1811 &w83793_left_fan[(i - 5) * files_fan
1812 + j].dev_attr);
1813 if (err)
1814 goto exit_remove;
1815 }
1816 }
1817
1818 for (i = 3; i < 8; i++) {
1819 int j;
1820 if (!(data->has_pwm & (1 << i)))
1821 continue;
1822 for (j = 0; j < files_pwm; j++) {
1823 err = device_create_file(dev,
1824 &w83793_left_pwm[(i - 3) * files_pwm
1825 + j].dev_attr);
1826 if (err)
1827 goto exit_remove;
1828 }
1829 }
1830
Tony Jones1beeffe2007-08-20 13:46:20 -07001831 data->hwmon_dev = hwmon_device_register(dev);
1832 if (IS_ERR(data->hwmon_dev)) {
1833 err = PTR_ERR(data->hwmon_dev);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001834 goto exit_remove;
1835 }
1836
Sven Anders5852f962010-03-05 22:17:22 +01001837 /* Watchdog initialization */
1838
1839 /* Register boot notifier */
1840 err = register_reboot_notifier(&watchdog_notifier);
1841 if (err != 0) {
1842 dev_err(&client->dev,
1843 "cannot register reboot notifier (err=%d)\n", err);
1844 goto exit_devunreg;
1845 }
1846
Guenter Roeck47efe872012-01-15 10:48:48 -08001847 /*
1848 * Enable Watchdog registers.
1849 * Set Configuration Register to Enable Watch Dog Registers
1850 * (Bit 2) = XXXX, X1XX.
1851 */
Sven Anders5852f962010-03-05 22:17:22 +01001852 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1853 w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
1854
1855 /* Set the default watchdog timeout */
1856 data->watchdog_timeout = timeout;
1857
1858 /* Check, if last reboot was caused by watchdog */
1859 data->watchdog_caused_reboot =
1860 w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
1861
1862 /* Disable Soft Watchdog during initialiation */
1863 watchdog_disable(data);
1864
Guenter Roeck47efe872012-01-15 10:48:48 -08001865 /*
1866 * We take the data_mutex lock early so that watchdog_open() cannot
1867 * run when misc_register() has completed, but we've not yet added
1868 * our data to the watchdog_data_list (and set the default timeout)
1869 */
Sven Anders5852f962010-03-05 22:17:22 +01001870 mutex_lock(&watchdog_data_mutex);
1871 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1872 /* Register our watchdog part */
1873 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1874 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1875 data->watchdog_miscdev.name = data->watchdog_name;
1876 data->watchdog_miscdev.fops = &watchdog_fops;
1877 data->watchdog_miscdev.minor = watchdog_minors[i];
1878
1879 err = misc_register(&data->watchdog_miscdev);
1880 if (err == -EBUSY)
1881 continue;
1882 if (err) {
1883 data->watchdog_miscdev.minor = 0;
1884 dev_err(&client->dev,
1885 "Registering watchdog chardev: %d\n", err);
1886 break;
1887 }
1888
1889 list_add(&data->list, &watchdog_data_list);
1890
1891 dev_info(&client->dev,
1892 "Registered watchdog chardev major 10, minor: %d\n",
1893 watchdog_minors[i]);
1894 break;
1895 }
1896 if (i == ARRAY_SIZE(watchdog_minors)) {
1897 data->watchdog_miscdev.minor = 0;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001898 dev_warn(&client->dev,
1899 "Couldn't register watchdog chardev (due to no free minor)\n");
Sven Anders5852f962010-03-05 22:17:22 +01001900 }
1901
1902 mutex_unlock(&watchdog_data_mutex);
1903
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001904 return 0;
1905
Sven Anders5852f962010-03-05 22:17:22 +01001906 /* Unregister hwmon device */
1907
1908exit_devunreg:
1909
1910 hwmon_device_unregister(data->hwmon_dev);
1911
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001912 /* Unregister sysfs hooks */
1913
1914exit_remove:
1915 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1916 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1917
1918 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1919 device_remove_file(dev, &sda_single_files[i].dev_attr);
1920
Gong Junc70a8c32007-01-18 22:14:24 +01001921 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1922 device_remove_file(dev, &w83793_vid[i].dev_attr);
1923
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001924 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1925 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1926
1927 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1928 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1929
Gong Jun46bed4d2007-01-18 22:14:24 +01001930 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1931 device_remove_file(dev, &w83793_temp[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001932free_mem:
1933 kfree(data);
1934exit:
1935 return err;
1936}
1937
1938static void w83793_update_nonvolatile(struct device *dev)
1939{
1940 struct i2c_client *client = to_i2c_client(dev);
1941 struct w83793_data *data = i2c_get_clientdata(client);
1942 int i, j;
1943 /*
Guenter Roeck47efe872012-01-15 10:48:48 -08001944 * They are somewhat "stable" registers, and to update them every time
1945 * takes so much time, it's just not worthy. Update them in a long
1946 * interval to avoid exception.
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001947 */
1948 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1949 || !data->valid))
1950 return;
1951 /* update voltage limits */
1952 for (i = 1; i < 3; i++) {
1953 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1954 data->in[j][i] =
1955 w83793_read_value(client, W83793_REG_IN[j][i]);
1956 }
1957 data->in_low_bits[i] =
1958 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1959 }
1960
1961 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1962 /* Update the Fan measured value and limits */
Guenter Roeck47efe872012-01-15 10:48:48 -08001963 if (!(data->has_fan & (1 << i)))
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001964 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001965 data->fan_min[i] =
1966 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1967 data->fan_min[i] |=
1968 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
1969 }
1970
1971 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
Gong Jun46bed4d2007-01-18 22:14:24 +01001972 if (!(data->has_temp & (1 << i)))
1973 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001974 data->temp_fan_map[i] =
1975 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
1976 for (j = 1; j < 5; j++) {
1977 data->temp[i][j] =
1978 w83793_read_value(client, W83793_REG_TEMP[i][j]);
1979 }
1980 data->temp_cruise[i] =
1981 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
1982 for (j = 0; j < 7; j++) {
1983 data->sf2_pwm[i][j] =
1984 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
1985 data->sf2_temp[i][j] =
1986 w83793_read_value(client,
1987 W83793_REG_SF2_TEMP(i, j));
1988 }
1989 }
1990
1991 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
1992 data->temp_mode[i] =
1993 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
1994
1995 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
1996 data->tolerance[i] =
1997 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
1998 }
1999
2000 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2001 if (!(data->has_pwm & (1 << i)))
2002 continue;
2003 data->pwm[i][PWM_NONSTOP] =
2004 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
2005 data->pwm[i][PWM_START] =
2006 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
2007 data->pwm_stop_time[i] =
2008 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
2009 }
2010
2011 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
2012 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
2013 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
2014 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
2015 data->temp_critical =
2016 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
2017 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
2018
Guenter Roeck47efe872012-01-15 10:48:48 -08002019 for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002020 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002021
2022 data->last_nonvolatile = jiffies;
2023}
2024
2025static struct w83793_data *w83793_update_device(struct device *dev)
2026{
2027 struct i2c_client *client = to_i2c_client(dev);
2028 struct w83793_data *data = i2c_get_clientdata(client);
2029 int i;
2030
2031 mutex_lock(&data->update_lock);
2032
2033 if (!(time_after(jiffies, data->last_updated + HZ * 2)
2034 || !data->valid))
2035 goto END;
2036
2037 /* Update the voltages measured value and limits */
2038 for (i = 0; i < ARRAY_SIZE(data->in); i++)
2039 data->in[i][IN_READ] =
2040 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
2041
2042 data->in_low_bits[IN_READ] =
2043 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
2044
2045 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
Guenter Roeck47efe872012-01-15 10:48:48 -08002046 if (!(data->has_fan & (1 << i)))
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002047 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002048 data->fan[i] =
2049 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
2050 data->fan[i] |=
2051 w83793_read_value(client, W83793_REG_FAN(i) + 1);
2052 }
2053
Gong Jun46bed4d2007-01-18 22:14:24 +01002054 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2055 if (!(data->has_temp & (1 << i)))
2056 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002057 data->temp[i][TEMP_READ] =
2058 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
Gong Jun46bed4d2007-01-18 22:14:24 +01002059 }
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002060
2061 data->temp_low_bits =
2062 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
2063
2064 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2065 if (data->has_pwm & (1 << i))
2066 data->pwm[i][PWM_DUTY] =
2067 w83793_read_value(client,
2068 W83793_REG_PWM(i, PWM_DUTY));
2069 }
2070
2071 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
2072 data->alarms[i] =
2073 w83793_read_value(client, W83793_REG_ALARM(i));
Gong Junc70a8c32007-01-18 22:14:24 +01002074 if (data->has_vid & 0x01)
2075 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
2076 if (data->has_vid & 0x02)
2077 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002078 w83793_update_nonvolatile(dev);
2079 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +03002080 data->valid = true;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002081
2082END:
2083 mutex_unlock(&data->update_lock);
2084 return data;
2085}
2086
Guenter Roeck47efe872012-01-15 10:48:48 -08002087/*
2088 * Ignore the possibility that somebody change bank outside the driver
2089 * Must be called with data->update_lock held, except during initialization
2090 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002091static u8 w83793_read_value(struct i2c_client *client, u16 reg)
2092{
2093 struct w83793_data *data = i2c_get_clientdata(client);
Colin Ian King58a24b522019-10-11 18:02:15 +01002094 u8 res;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002095 u8 new_bank = reg >> 8;
2096
2097 new_bank |= data->bank & 0xfc;
2098 if (data->bank != new_bank) {
2099 if (i2c_smbus_write_byte_data
2100 (client, W83793_REG_BANKSEL, new_bank) >= 0)
2101 data->bank = new_bank;
2102 else {
2103 dev_err(&client->dev,
2104 "set bank to %d failed, fall back "
2105 "to bank %d, read reg 0x%x error\n",
2106 new_bank, data->bank, reg);
2107 res = 0x0; /* read 0x0 from the chip */
2108 goto END;
2109 }
2110 }
2111 res = i2c_smbus_read_byte_data(client, reg & 0xff);
2112END:
2113 return res;
2114}
2115
2116/* Must be called with data->update_lock held, except during initialization */
2117static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
2118{
2119 struct w83793_data *data = i2c_get_clientdata(client);
2120 int res;
2121 u8 new_bank = reg >> 8;
2122
2123 new_bank |= data->bank & 0xfc;
2124 if (data->bank != new_bank) {
Guenter Roeck47efe872012-01-15 10:48:48 -08002125 res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
2126 new_bank);
2127 if (res < 0) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002128 dev_err(&client->dev,
2129 "set bank to %d failed, fall back "
2130 "to bank %d, write reg 0x%x error\n",
2131 new_bank, data->bank, reg);
2132 goto END;
2133 }
Guenter Roeck47efe872012-01-15 10:48:48 -08002134 data->bank = new_bank;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002135 }
2136
2137 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
2138END:
2139 return res;
2140}
2141
Axel Linf0967ee2012-01-20 15:38:18 +08002142module_i2c_driver(w83793_driver);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002143
Sven Anders5852f962010-03-05 22:17:22 +01002144MODULE_AUTHOR("Yuan Mu, Sven Anders");
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002145MODULE_DESCRIPTION("w83793 driver");
2146MODULE_LICENSE("GPL");