blob: 9d63d71214cade1cddad0b8bfbbbbae726c4dda5 [file] [log] [blame]
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001/*
Guenter Roeck47efe872012-01-15 10:48:48 -08002 * w83793.c - Linux kernel driver for hardware monitoring
3 * Copyright (C) 2006 Winbond Electronics Corp.
4 * Yuan Mu
5 * Rudolf Marek <r.marek@assembler.cz>
6 * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
7 * Watchdog driver part
8 * (Based partially on fschmd driver,
9 * Copyright 2007-2008 by Hans de Goede)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation - version 2.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * 02110-1301 USA.
24 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010025
26/*
Guenter Roeck47efe872012-01-15 10:48:48 -080027 * Supports following chips:
28 *
29 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
30 * w83793 10 12 8 6 0x7b 0x5ca3 yes no
31 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010032
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/hwmon.h>
38#include <linux/hwmon-vid.h>
39#include <linux/hwmon-sysfs.h>
40#include <linux/err.h>
41#include <linux/mutex.h>
Sven Anders5852f962010-03-05 22:17:22 +010042#include <linux/fs.h>
43#include <linux/watchdog.h>
44#include <linux/miscdevice.h>
45#include <linux/uaccess.h>
46#include <linux/kref.h>
47#include <linux/notifier.h>
48#include <linux/reboot.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020049#include <linux/jiffies.h>
Sven Anders5852f962010-03-05 22:17:22 +010050
51/* Default values */
52#define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010053
54/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050055static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
56 I2C_CLIENT_END };
Rudolf Marek6800c3d2006-12-12 18:18:30 +010057
58/* Insmod parameters */
Jean Delvare3aed1982009-01-07 16:37:32 +010059
60static unsigned short force_subclients[4];
61module_param_array(force_subclients, short, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080062MODULE_PARM_DESC(force_subclients,
63 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
Rudolf Marek6800c3d2006-12-12 18:18:30 +010064
Rusty Russell90ab5ee2012-01-13 09:32:20 +103065static bool reset;
Rudolf Marek6800c3d2006-12-12 18:18:30 +010066module_param(reset, bool, 0);
67MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
68
Sven Anders5852f962010-03-05 22:17:22 +010069static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
70module_param(timeout, int, 0);
71MODULE_PARM_DESC(timeout,
72 "Watchdog timeout in minutes. 2<= timeout <=255 (default="
73 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
74
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010075static bool nowayout = WATCHDOG_NOWAYOUT;
76module_param(nowayout, bool, 0);
Sven Anders5852f962010-03-05 22:17:22 +010077MODULE_PARM_DESC(nowayout,
78 "Watchdog cannot be stopped once started (default="
79 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
80
Rudolf Marek6800c3d2006-12-12 18:18:30 +010081/*
Guenter Roeck47efe872012-01-15 10:48:48 -080082 * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
83 * as ID, Bank Select registers
84 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +010085#define W83793_REG_BANKSEL 0x00
86#define W83793_REG_VENDORID 0x0d
87#define W83793_REG_CHIPID 0x0e
88#define W83793_REG_DEVICEID 0x0f
89
90#define W83793_REG_CONFIG 0x40
91#define W83793_REG_MFC 0x58
92#define W83793_REG_FANIN_CTRL 0x5c
93#define W83793_REG_FANIN_SEL 0x5d
94#define W83793_REG_I2C_ADDR 0x0b
95#define W83793_REG_I2C_SUBADDR 0x0c
96#define W83793_REG_VID_INA 0x05
97#define W83793_REG_VID_INB 0x06
98#define W83793_REG_VID_LATCHA 0x07
99#define W83793_REG_VID_LATCHB 0x08
100#define W83793_REG_VID_CTRL 0x59
101
Sven Anders5852f962010-03-05 22:17:22 +0100102#define W83793_REG_WDT_LOCK 0x01
103#define W83793_REG_WDT_ENABLE 0x02
104#define W83793_REG_WDT_STATUS 0x03
105#define W83793_REG_WDT_TIMEOUT 0x04
106
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100107static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
108
109#define TEMP_READ 0
110#define TEMP_CRIT 1
111#define TEMP_CRIT_HYST 2
112#define TEMP_WARN 3
113#define TEMP_WARN_HYST 4
Guenter Roeck47efe872012-01-15 10:48:48 -0800114/*
115 * only crit and crit_hyst affect real-time alarm status
116 * current crit crit_hyst warn warn_hyst
117 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100118static u16 W83793_REG_TEMP[][5] = {
119 {0x1c, 0x78, 0x79, 0x7a, 0x7b},
120 {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
121 {0x1e, 0x80, 0x81, 0x82, 0x83},
122 {0x1f, 0x84, 0x85, 0x86, 0x87},
123 {0x20, 0x88, 0x89, 0x8a, 0x8b},
124 {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
125};
126
127#define W83793_REG_TEMP_LOW_BITS 0x22
128
129#define W83793_REG_BEEP(index) (0x53 + (index))
130#define W83793_REG_ALARM(index) (0x4b + (index))
131
132#define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
133#define W83793_REG_IRQ_CTRL 0x50
134#define W83793_REG_OVT_CTRL 0x51
135#define W83793_REG_OVT_BEEP 0x52
136
137#define IN_READ 0
138#define IN_MAX 1
139#define IN_LOW 2
140static const u16 W83793_REG_IN[][3] = {
141 /* Current, High, Low */
142 {0x10, 0x60, 0x61}, /* Vcore A */
143 {0x11, 0x62, 0x63}, /* Vcore B */
144 {0x12, 0x64, 0x65}, /* Vtt */
145 {0x14, 0x6a, 0x6b}, /* VSEN1 */
146 {0x15, 0x6c, 0x6d}, /* VSEN2 */
147 {0x16, 0x6e, 0x6f}, /* +3VSEN */
148 {0x17, 0x70, 0x71}, /* +12VSEN */
149 {0x18, 0x72, 0x73}, /* 5VDD */
150 {0x19, 0x74, 0x75}, /* 5VSB */
151 {0x1a, 0x76, 0x77}, /* VBAT */
152};
153
154/* Low Bits of Vcore A/B Vtt Read/High/Low */
155static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
156static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
Gong Junddca9332007-01-18 22:14:23 +0100157static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100158
159#define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
160#define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
161
162#define W83793_REG_PWM_DEFAULT 0xb2
163#define W83793_REG_PWM_ENABLE 0x207
164#define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
165#define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
166#define W83793_REG_TEMP_CRITICAL 0xc5
167
168#define PWM_DUTY 0
169#define PWM_START 1
170#define PWM_NONSTOP 2
Nicolas Kaiser5aebefb2007-11-07 13:28:59 +0100171#define PWM_STOP_TIME 3
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100172#define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
173 (nr) == 1 ? 0x220 : 0x218) + (index))
174
175/* bit field, fan1 is bit0, fan2 is bit1 ... */
176#define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
177#define W83793_REG_TEMP_TOL(index) (0x208 + (index))
178#define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
179#define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
180#define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
181#define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
182
183static inline unsigned long FAN_FROM_REG(u16 val)
184{
185 if ((val >= 0xfff) || (val == 0))
186 return 0;
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100187 return 1350000UL / val;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100188}
189
190static inline u16 FAN_TO_REG(long rpm)
191{
192 if (rpm <= 0)
193 return 0x0fff;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800194 return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100195}
196
197static inline unsigned long TIME_FROM_REG(u8 reg)
198{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100199 return reg * 100;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100200}
201
202static inline u8 TIME_TO_REG(unsigned long val)
203{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800204 return clamp_val((val + 50) / 100, 0, 0xff);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100205}
206
207static inline long TEMP_FROM_REG(s8 reg)
208{
Frans Meulenbroeks7fe83ad2012-01-05 19:50:18 +0100209 return reg * 1000;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100210}
211
212static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
213{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800214 return clamp_val((val + (val < 0 ? -500 : 500)) / 1000, min, max);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100215}
216
217struct w83793_data {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100218 struct i2c_client *lm75[2];
Tony Jones1beeffe2007-08-20 13:46:20 -0700219 struct device *hwmon_dev;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100220 struct mutex update_lock;
221 char valid; /* !=0 if following fields are valid */
222 unsigned long last_updated; /* In jiffies */
223 unsigned long last_nonvolatile; /* In jiffies, last time we update the
Guenter Roeck47efe872012-01-15 10:48:48 -0800224 * nonvolatile registers
225 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100226
227 u8 bank;
228 u8 vrm;
229 u8 vid[2];
230 u8 in[10][3]; /* Register value, read/high/low */
231 u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
232
233 u16 has_fan; /* Only fan1- fan5 has own pins */
234 u16 fan[12]; /* Register value combine */
235 u16 fan_min[12]; /* Register value combine */
236
237 s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
238 u8 temp_low_bits; /* Additional resolution TD1-TD4 */
239 u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
Guenter Roeck47efe872012-01-15 10:48:48 -0800240 * byte 1: Temp R1,R2 mode, each has 1 bit
241 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100242 u8 temp_critical; /* If reached all fan will be at full speed */
243 u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
244
245 u8 has_pwm;
Gong Jun46bed4d2007-01-18 22:14:24 +0100246 u8 has_temp;
Gong Junc70a8c32007-01-18 22:14:24 +0100247 u8 has_vid;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100248 u8 pwm_enable; /* Register value, each Temp has 1 bit */
249 u8 pwm_uptime; /* Register value */
250 u8 pwm_downtime; /* Register value */
251 u8 pwm_default; /* All fan default pwm, next poweron valid */
252 u8 pwm[8][3]; /* Register value */
253 u8 pwm_stop_time[8];
254 u8 temp_cruise[6];
255
256 u8 alarms[5]; /* realtime status registers */
257 u8 beeps[5];
258 u8 beep_enable;
259 u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
260 u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
261 u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
Sven Anders5852f962010-03-05 22:17:22 +0100262
263 /* watchdog */
264 struct i2c_client *client;
265 struct mutex watchdog_lock;
266 struct list_head list; /* member of the watchdog_data_list */
267 struct kref kref;
268 struct miscdevice watchdog_miscdev;
269 unsigned long watchdog_is_open;
270 char watchdog_expect_close;
271 char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
272 unsigned int watchdog_caused_reboot;
273 int watchdog_timeout; /* watchdog timeout in minutes */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100274};
275
Guenter Roeck47efe872012-01-15 10:48:48 -0800276/*
277 * Somewhat ugly :( global data pointer list with all devices, so that
278 * we can find our device data as when using misc_register. There is no
279 * other method to get to one's device data from the open file-op and
280 * for usage in the reboot notifier callback.
281 */
Sven Anders5852f962010-03-05 22:17:22 +0100282static LIST_HEAD(watchdog_data_list);
283
284/* Note this lock not only protect list access, but also data.kref access */
285static DEFINE_MUTEX(watchdog_data_mutex);
286
Guenter Roeck47efe872012-01-15 10:48:48 -0800287/*
288 * Release our data struct when we're detached from the i2c client *and* all
289 * references to our watchdog device are released
290 */
Sven Anders5852f962010-03-05 22:17:22 +0100291static void w83793_release_resources(struct kref *ref)
292{
293 struct w83793_data *data = container_of(ref, struct w83793_data, kref);
294 kfree(data);
295}
296
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100297static u8 w83793_read_value(struct i2c_client *client, u16 reg);
298static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
Jean Delvarea7f13a62008-07-16 19:30:17 +0200299static int w83793_probe(struct i2c_client *client,
300 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100301static int w83793_detect(struct i2c_client *client,
Jean Delvarea7f13a62008-07-16 19:30:17 +0200302 struct i2c_board_info *info);
303static int w83793_remove(struct i2c_client *client);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100304static void w83793_init_client(struct i2c_client *client);
305static void w83793_update_nonvolatile(struct device *dev);
306static struct w83793_data *w83793_update_device(struct device *dev);
307
Jean Delvarea7f13a62008-07-16 19:30:17 +0200308static const struct i2c_device_id w83793_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100309 { "w83793", 0 },
Jean Delvarea7f13a62008-07-16 19:30:17 +0200310 { }
311};
312MODULE_DEVICE_TABLE(i2c, w83793_id);
313
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100314static struct i2c_driver w83793_driver = {
Jean Delvarea7f13a62008-07-16 19:30:17 +0200315 .class = I2C_CLASS_HWMON,
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100316 .driver = {
317 .name = "w83793",
318 },
Jean Delvarea7f13a62008-07-16 19:30:17 +0200319 .probe = w83793_probe,
320 .remove = w83793_remove,
321 .id_table = w83793_id,
322 .detect = w83793_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100323 .address_list = normal_i2c,
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100324};
325
326static ssize_t
327show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
328{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100329 struct w83793_data *data = dev_get_drvdata(dev);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100330 return sprintf(buf, "%d\n", data->vrm);
331}
332
333static ssize_t
334show_vid(struct device *dev, struct device_attribute *attr, char *buf)
335{
336 struct w83793_data *data = w83793_update_device(dev);
337 struct sensor_device_attribute_2 *sensor_attr =
338 to_sensor_dev_attr_2(attr);
339 int index = sensor_attr->index;
340
341 return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
342}
343
344static ssize_t
345store_vrm(struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count)
347{
Jean Delvare8f74efe2007-12-01 11:25:33 +0100348 struct w83793_data *data = dev_get_drvdata(dev);
Guenter Roeck47efe872012-01-15 10:48:48 -0800349 unsigned long val;
350 int err;
351
352 err = kstrtoul(buf, 10, &val);
353 if (err)
354 return err;
355
356 data->vrm = val;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100357 return count;
358}
359
360#define ALARM_STATUS 0
361#define BEEP_ENABLE 1
362static ssize_t
363show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
364{
365 struct w83793_data *data = w83793_update_device(dev);
366 struct sensor_device_attribute_2 *sensor_attr =
367 to_sensor_dev_attr_2(attr);
368 int nr = sensor_attr->nr;
369 int index = sensor_attr->index >> 3;
370 int bit = sensor_attr->index & 0x07;
371 u8 val;
372
Guenter Roeck47efe872012-01-15 10:48:48 -0800373 if (nr == ALARM_STATUS) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100374 val = (data->alarms[index] >> (bit)) & 1;
375 } else { /* BEEP_ENABLE */
376 val = (data->beeps[index] >> (bit)) & 1;
377 }
378
379 return sprintf(buf, "%u\n", val);
380}
381
382static ssize_t
383store_beep(struct device *dev, struct device_attribute *attr,
384 const char *buf, size_t count)
385{
386 struct i2c_client *client = to_i2c_client(dev);
387 struct w83793_data *data = i2c_get_clientdata(client);
388 struct sensor_device_attribute_2 *sensor_attr =
389 to_sensor_dev_attr_2(attr);
390 int index = sensor_attr->index >> 3;
391 int shift = sensor_attr->index & 0x07;
392 u8 beep_bit = 1 << shift;
Guenter Roeck47efe872012-01-15 10:48:48 -0800393 unsigned long val;
394 int err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100395
Guenter Roeck47efe872012-01-15 10:48:48 -0800396 err = kstrtoul(buf, 10, &val);
397 if (err)
398 return err;
399
400 if (val > 1)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100401 return -EINVAL;
402
403 mutex_lock(&data->update_lock);
404 data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
405 data->beeps[index] &= ~beep_bit;
406 data->beeps[index] |= val << shift;
407 w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
408 mutex_unlock(&data->update_lock);
409
410 return count;
411}
412
413static ssize_t
414show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
415{
416 struct w83793_data *data = w83793_update_device(dev);
417 return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
418}
419
420static ssize_t
421store_beep_enable(struct device *dev, struct device_attribute *attr,
422 const char *buf, size_t count)
423{
424 struct i2c_client *client = to_i2c_client(dev);
425 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800426 unsigned long val;
427 int err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100428
Guenter Roeck47efe872012-01-15 10:48:48 -0800429 err = kstrtoul(buf, 10, &val);
430 if (err)
431 return err;
432
433 if (val > 1)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100434 return -EINVAL;
435
436 mutex_lock(&data->update_lock);
437 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
438 & 0xfd;
439 data->beep_enable |= val << 1;
440 w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
441 mutex_unlock(&data->update_lock);
442
443 return count;
444}
445
Jean Delvarea516dc32011-01-12 21:55:10 +0100446/* Write 0 to clear chassis alarm */
447static ssize_t
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100448store_chassis_clear(struct device *dev,
449 struct device_attribute *attr, const char *buf,
450 size_t count)
451{
452 struct i2c_client *client = to_i2c_client(dev);
453 struct w83793_data *data = i2c_get_clientdata(client);
Jean Delvarea516dc32011-01-12 21:55:10 +0100454 unsigned long val;
455 u8 reg;
Guenter Roeck47efe872012-01-15 10:48:48 -0800456 int err;
Jean Delvarea516dc32011-01-12 21:55:10 +0100457
Guenter Roeck47efe872012-01-15 10:48:48 -0800458 err = kstrtoul(buf, 10, &val);
459 if (err)
460 return err;
461 if (val)
Jean Delvarea516dc32011-01-12 21:55:10 +0100462 return -EINVAL;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100463
464 mutex_lock(&data->update_lock);
Jean Delvarea516dc32011-01-12 21:55:10 +0100465 reg = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
466 w83793_write_value(client, W83793_REG_CLR_CHASSIS, reg | 0x80);
467 data->valid = 0; /* Force cache refresh */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100468 mutex_unlock(&data->update_lock);
469 return count;
470}
471
472#define FAN_INPUT 0
473#define FAN_MIN 1
474static ssize_t
475show_fan(struct device *dev, struct device_attribute *attr, char *buf)
476{
477 struct sensor_device_attribute_2 *sensor_attr =
478 to_sensor_dev_attr_2(attr);
479 int nr = sensor_attr->nr;
480 int index = sensor_attr->index;
481 struct w83793_data *data = w83793_update_device(dev);
482 u16 val;
483
Guenter Roeck47efe872012-01-15 10:48:48 -0800484 if (nr == FAN_INPUT)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100485 val = data->fan[index] & 0x0fff;
Guenter Roeck47efe872012-01-15 10:48:48 -0800486 else
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100487 val = data->fan_min[index] & 0x0fff;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100488
489 return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
490}
491
492static ssize_t
493store_fan_min(struct device *dev, struct device_attribute *attr,
494 const char *buf, size_t count)
495{
496 struct sensor_device_attribute_2 *sensor_attr =
497 to_sensor_dev_attr_2(attr);
498 int index = sensor_attr->index;
499 struct i2c_client *client = to_i2c_client(dev);
500 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800501 unsigned long val;
502 int err;
503
504 err = kstrtoul(buf, 10, &val);
505 if (err)
506 return err;
507 val = FAN_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100508
509 mutex_lock(&data->update_lock);
510 data->fan_min[index] = val;
511 w83793_write_value(client, W83793_REG_FAN_MIN(index),
512 (val >> 8) & 0xff);
513 w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
514 mutex_unlock(&data->update_lock);
515
516 return count;
517}
518
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100519static ssize_t
520show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
521{
522 struct sensor_device_attribute_2 *sensor_attr =
523 to_sensor_dev_attr_2(attr);
524 struct w83793_data *data = w83793_update_device(dev);
525 u16 val;
526 int nr = sensor_attr->nr;
527 int index = sensor_attr->index;
528
Guenter Roeck47efe872012-01-15 10:48:48 -0800529 if (nr == PWM_STOP_TIME)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100530 val = TIME_FROM_REG(data->pwm_stop_time[index]);
531 else
532 val = (data->pwm[index][nr] & 0x3f) << 2;
533
534 return sprintf(buf, "%d\n", val);
535}
536
537static ssize_t
538store_pwm(struct device *dev, struct device_attribute *attr,
539 const char *buf, size_t count)
540{
541 struct i2c_client *client = to_i2c_client(dev);
542 struct w83793_data *data = i2c_get_clientdata(client);
543 struct sensor_device_attribute_2 *sensor_attr =
544 to_sensor_dev_attr_2(attr);
545 int nr = sensor_attr->nr;
546 int index = sensor_attr->index;
Guenter Roeck47efe872012-01-15 10:48:48 -0800547 unsigned long val;
548 int err;
549
550 err = kstrtoul(buf, 10, &val);
551 if (err)
552 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100553
554 mutex_lock(&data->update_lock);
Guenter Roeck47efe872012-01-15 10:48:48 -0800555 if (nr == PWM_STOP_TIME) {
556 val = TIME_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100557 data->pwm_stop_time[index] = val;
558 w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
559 val);
560 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800561 val = clamp_val(val, 0, 0xff) >> 2;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100562 data->pwm[index][nr] =
563 w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
564 data->pwm[index][nr] |= val;
565 w83793_write_value(client, W83793_REG_PWM(index, nr),
566 data->pwm[index][nr]);
567 }
568
569 mutex_unlock(&data->update_lock);
570 return count;
571}
572
573static ssize_t
574show_temp(struct device *dev, struct device_attribute *attr, char *buf)
575{
576 struct sensor_device_attribute_2 *sensor_attr =
577 to_sensor_dev_attr_2(attr);
578 int nr = sensor_attr->nr;
579 int index = sensor_attr->index;
580 struct w83793_data *data = w83793_update_device(dev);
581 long temp = TEMP_FROM_REG(data->temp[index][nr]);
582
Guenter Roeck47efe872012-01-15 10:48:48 -0800583 if (nr == TEMP_READ && index < 4) { /* Only TD1-TD4 have low bits */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100584 int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
585 temp += temp > 0 ? low : -low;
586 }
587 return sprintf(buf, "%ld\n", temp);
588}
589
590static ssize_t
591store_temp(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
593{
594 struct sensor_device_attribute_2 *sensor_attr =
595 to_sensor_dev_attr_2(attr);
596 int nr = sensor_attr->nr;
597 int index = sensor_attr->index;
598 struct i2c_client *client = to_i2c_client(dev);
599 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800600 long tmp;
601 int err;
602
603 err = kstrtol(buf, 10, &tmp);
604 if (err)
605 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100606
607 mutex_lock(&data->update_lock);
608 data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
609 w83793_write_value(client, W83793_REG_TEMP[index][nr],
610 data->temp[index][nr]);
611 mutex_unlock(&data->update_lock);
612 return count;
613}
614
615/*
Guenter Roeck47efe872012-01-15 10:48:48 -0800616 * TD1-TD4
617 * each has 4 mode:(2 bits)
618 * 0: Stop monitor
619 * 1: Use internal temp sensor(default)
620 * 2: Reserved
621 * 3: Use sensor in Intel CPU and get result by PECI
622 *
623 * TR1-TR2
624 * each has 2 mode:(1 bit)
625 * 0: Disable temp sensor monitor
626 * 1: To enable temp sensors monitor
627 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100628
Gong Junddca9332007-01-18 22:14:23 +0100629/* 0 disable, 6 PECI */
630static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100631
632static ssize_t
633show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
634{
635 struct w83793_data *data = w83793_update_device(dev);
636 struct sensor_device_attribute_2 *sensor_attr =
637 to_sensor_dev_attr_2(attr);
638 int index = sensor_attr->index;
639 u8 mask = (index < 4) ? 0x03 : 0x01;
640 u8 shift = (index < 4) ? (2 * index) : (index - 4);
641 u8 tmp;
642 index = (index < 4) ? 0 : 1;
643
644 tmp = (data->temp_mode[index] >> shift) & mask;
645
646 /* for the internal sensor, found out if diode or thermistor */
Guenter Roeck47efe872012-01-15 10:48:48 -0800647 if (tmp == 1)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100648 tmp = index == 0 ? 3 : 4;
Guenter Roeck47efe872012-01-15 10:48:48 -0800649 else
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100650 tmp = TO_TEMP_MODE[tmp];
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100651
652 return sprintf(buf, "%d\n", tmp);
653}
654
655static ssize_t
656store_temp_mode(struct device *dev, struct device_attribute *attr,
657 const char *buf, size_t count)
658{
659 struct i2c_client *client = to_i2c_client(dev);
660 struct w83793_data *data = i2c_get_clientdata(client);
661 struct sensor_device_attribute_2 *sensor_attr =
662 to_sensor_dev_attr_2(attr);
663 int index = sensor_attr->index;
664 u8 mask = (index < 4) ? 0x03 : 0x01;
665 u8 shift = (index < 4) ? (2 * index) : (index - 4);
Guenter Roeck47efe872012-01-15 10:48:48 -0800666 unsigned long val;
667 int err;
668
669 err = kstrtoul(buf, 10, &val);
670 if (err)
671 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100672
673 /* transform the sysfs interface values into table above */
Gong Junddca9332007-01-18 22:14:23 +0100674 if ((val == 6) && (index < 4)) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100675 val -= 3;
676 } else if ((val == 3 && index < 4)
Gong Jun46bed4d2007-01-18 22:14:24 +0100677 || (val == 4 && index >= 4)) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100678 /* transform diode or thermistor into internal enable */
679 val = !!val;
680 } else {
681 return -EINVAL;
682 }
683
684 index = (index < 4) ? 0 : 1;
685 mutex_lock(&data->update_lock);
686 data->temp_mode[index] =
687 w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
688 data->temp_mode[index] &= ~(mask << shift);
689 data->temp_mode[index] |= val << shift;
690 w83793_write_value(client, W83793_REG_TEMP_MODE[index],
691 data->temp_mode[index]);
692 mutex_unlock(&data->update_lock);
693
694 return count;
695}
696
697#define SETUP_PWM_DEFAULT 0
698#define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
699#define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
700#define SETUP_TEMP_CRITICAL 3
701static ssize_t
702show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
703{
704 struct sensor_device_attribute_2 *sensor_attr =
705 to_sensor_dev_attr_2(attr);
706 int nr = sensor_attr->nr;
707 struct w83793_data *data = w83793_update_device(dev);
708 u32 val = 0;
709
Guenter Roeck47efe872012-01-15 10:48:48 -0800710 if (nr == SETUP_PWM_DEFAULT)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100711 val = (data->pwm_default & 0x3f) << 2;
Guenter Roeck47efe872012-01-15 10:48:48 -0800712 else if (nr == SETUP_PWM_UPTIME)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100713 val = TIME_FROM_REG(data->pwm_uptime);
Guenter Roeck47efe872012-01-15 10:48:48 -0800714 else if (nr == SETUP_PWM_DOWNTIME)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100715 val = TIME_FROM_REG(data->pwm_downtime);
Guenter Roeck47efe872012-01-15 10:48:48 -0800716 else if (nr == SETUP_TEMP_CRITICAL)
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100717 val = TEMP_FROM_REG(data->temp_critical & 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100718
719 return sprintf(buf, "%d\n", val);
720}
721
722static ssize_t
723store_sf_setup(struct device *dev, struct device_attribute *attr,
724 const char *buf, size_t count)
725{
726 struct sensor_device_attribute_2 *sensor_attr =
727 to_sensor_dev_attr_2(attr);
728 int nr = sensor_attr->nr;
729 struct i2c_client *client = to_i2c_client(dev);
730 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800731 long val;
732 int err;
733
734 err = kstrtol(buf, 10, &val);
735 if (err)
736 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100737
738 mutex_lock(&data->update_lock);
Guenter Roeck47efe872012-01-15 10:48:48 -0800739 if (nr == SETUP_PWM_DEFAULT) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100740 data->pwm_default =
741 w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800742 data->pwm_default |= clamp_val(val, 0, 0xff) >> 2;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100743 w83793_write_value(client, W83793_REG_PWM_DEFAULT,
744 data->pwm_default);
Guenter Roeck47efe872012-01-15 10:48:48 -0800745 } else if (nr == SETUP_PWM_UPTIME) {
746 data->pwm_uptime = TIME_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100747 data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
748 w83793_write_value(client, W83793_REG_PWM_UPTIME,
749 data->pwm_uptime);
Guenter Roeck47efe872012-01-15 10:48:48 -0800750 } else if (nr == SETUP_PWM_DOWNTIME) {
751 data->pwm_downtime = TIME_TO_REG(val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100752 data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
753 w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
754 data->pwm_downtime);
755 } else { /* SETUP_TEMP_CRITICAL */
756 data->temp_critical =
757 w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
Guenter Roeck47efe872012-01-15 10:48:48 -0800758 data->temp_critical |= TEMP_TO_REG(val, 0, 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100759 w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
760 data->temp_critical);
761 }
762
763 mutex_unlock(&data->update_lock);
764 return count;
765}
766
767/*
Guenter Roeck47efe872012-01-15 10:48:48 -0800768 * Temp SmartFan control
769 * TEMP_FAN_MAP
770 * Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
771 * It's possible two or more temp channels control the same fan, w83793
772 * always prefers to pick the most critical request and applies it to
773 * the related Fan.
774 * It's possible one fan is not in any mapping of 6 temp channels, this
775 * means the fan is manual mode
776 *
777 * TEMP_PWM_ENABLE
778 * Each temp channel has its own SmartFan mode, and temp channel
779 * control fans that are set by TEMP_FAN_MAP
780 * 0: SmartFanII mode
781 * 1: Thermal Cruise Mode
782 *
783 * TEMP_CRUISE
784 * Target temperature in thermal cruise mode, w83793 will try to turn
785 * fan speed to keep the temperature of target device around this
786 * temperature.
787 *
788 * TEMP_TOLERANCE
789 * If Temp higher or lower than target with this tolerance, w83793
790 * will take actions to speed up or slow down the fan to keep the
791 * temperature within the tolerance range.
792 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100793
794#define TEMP_FAN_MAP 0
795#define TEMP_PWM_ENABLE 1
796#define TEMP_CRUISE 2
797#define TEMP_TOLERANCE 3
798static ssize_t
799show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
800{
801 struct sensor_device_attribute_2 *sensor_attr =
802 to_sensor_dev_attr_2(attr);
803 int nr = sensor_attr->nr;
804 int index = sensor_attr->index;
805 struct w83793_data *data = w83793_update_device(dev);
806 u32 val;
807
Guenter Roeck47efe872012-01-15 10:48:48 -0800808 if (nr == TEMP_FAN_MAP) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100809 val = data->temp_fan_map[index];
Guenter Roeck47efe872012-01-15 10:48:48 -0800810 } else if (nr == TEMP_PWM_ENABLE) {
LABBE Corentin84fb0292013-09-27 14:36:04 +0200811 /* +2 to transform into 2 and 3 to conform with sysfs intf */
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100812 val = ((data->pwm_enable >> index) & 0x01) + 2;
Guenter Roeck47efe872012-01-15 10:48:48 -0800813 } else if (nr == TEMP_CRUISE) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100814 val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
815 } else { /* TEMP_TOLERANCE */
816 val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
817 val = TEMP_FROM_REG(val & 0x0f);
818 }
819 return sprintf(buf, "%d\n", val);
820}
821
822static ssize_t
823store_sf_ctrl(struct device *dev, struct device_attribute *attr,
824 const char *buf, size_t count)
825{
826 struct sensor_device_attribute_2 *sensor_attr =
827 to_sensor_dev_attr_2(attr);
828 int nr = sensor_attr->nr;
829 int index = sensor_attr->index;
830 struct i2c_client *client = to_i2c_client(dev);
831 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800832 long val;
833 int err;
834
835 err = kstrtol(buf, 10, &val);
836 if (err)
837 return err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100838
839 mutex_lock(&data->update_lock);
Guenter Roeck47efe872012-01-15 10:48:48 -0800840 if (nr == TEMP_FAN_MAP) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800841 val = clamp_val(val, 0, 255);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100842 w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
843 data->temp_fan_map[index] = val;
Guenter Roeck47efe872012-01-15 10:48:48 -0800844 } else if (nr == TEMP_PWM_ENABLE) {
845 if (val == 2 || val == 3) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100846 data->pwm_enable =
847 w83793_read_value(client, W83793_REG_PWM_ENABLE);
848 if (val - 2)
849 data->pwm_enable |= 1 << index;
850 else
851 data->pwm_enable &= ~(1 << index);
852 w83793_write_value(client, W83793_REG_PWM_ENABLE,
853 data->pwm_enable);
854 } else {
855 mutex_unlock(&data->update_lock);
856 return -EINVAL;
857 }
Guenter Roeck47efe872012-01-15 10:48:48 -0800858 } else if (nr == TEMP_CRUISE) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100859 data->temp_cruise[index] =
860 w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100861 data->temp_cruise[index] &= 0x80;
Guenter Roeck47efe872012-01-15 10:48:48 -0800862 data->temp_cruise[index] |= TEMP_TO_REG(val, 0, 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100863
864 w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
865 data->temp_cruise[index]);
866 } else { /* TEMP_TOLERANCE */
867 int i = index >> 1;
868 u8 shift = (index & 0x01) ? 4 : 0;
869 data->tolerance[i] =
870 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
871
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100872 data->tolerance[i] &= ~(0x0f << shift);
Guenter Roeck47efe872012-01-15 10:48:48 -0800873 data->tolerance[i] |= TEMP_TO_REG(val, 0, 0x0f) << shift;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100874 w83793_write_value(client, W83793_REG_TEMP_TOL(i),
875 data->tolerance[i]);
876 }
877
878 mutex_unlock(&data->update_lock);
879 return count;
880}
881
882static ssize_t
883show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
884{
885 struct sensor_device_attribute_2 *sensor_attr =
886 to_sensor_dev_attr_2(attr);
887 int nr = sensor_attr->nr;
888 int index = sensor_attr->index;
889 struct w83793_data *data = w83793_update_device(dev);
890
891 return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
892}
893
894static ssize_t
895store_sf2_pwm(struct device *dev, struct device_attribute *attr,
896 const char *buf, size_t count)
897{
898 struct i2c_client *client = to_i2c_client(dev);
899 struct w83793_data *data = i2c_get_clientdata(client);
900 struct sensor_device_attribute_2 *sensor_attr =
901 to_sensor_dev_attr_2(attr);
902 int nr = sensor_attr->nr;
903 int index = sensor_attr->index;
Guenter Roeck47efe872012-01-15 10:48:48 -0800904 unsigned long val;
905 int err;
906
907 err = kstrtoul(buf, 10, &val);
908 if (err)
909 return err;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800910 val = clamp_val(val, 0, 0xff) >> 2;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100911
912 mutex_lock(&data->update_lock);
913 data->sf2_pwm[index][nr] =
914 w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
915 data->sf2_pwm[index][nr] |= val;
916 w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
917 data->sf2_pwm[index][nr]);
918 mutex_unlock(&data->update_lock);
919 return count;
920}
921
922static ssize_t
923show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
924{
925 struct sensor_device_attribute_2 *sensor_attr =
926 to_sensor_dev_attr_2(attr);
927 int nr = sensor_attr->nr;
928 int index = sensor_attr->index;
929 struct w83793_data *data = w83793_update_device(dev);
930
931 return sprintf(buf, "%ld\n",
932 TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
933}
934
935static ssize_t
936store_sf2_temp(struct device *dev, struct device_attribute *attr,
937 const char *buf, size_t count)
938{
939 struct i2c_client *client = to_i2c_client(dev);
940 struct w83793_data *data = i2c_get_clientdata(client);
941 struct sensor_device_attribute_2 *sensor_attr =
942 to_sensor_dev_attr_2(attr);
943 int nr = sensor_attr->nr;
944 int index = sensor_attr->index;
Guenter Roeck47efe872012-01-15 10:48:48 -0800945 long val;
946 int err;
947
948 err = kstrtol(buf, 10, &val);
949 if (err)
950 return err;
951 val = TEMP_TO_REG(val, 0, 0x7f);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100952
953 mutex_lock(&data->update_lock);
954 data->sf2_temp[index][nr] =
955 w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
956 data->sf2_temp[index][nr] |= val;
957 w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
958 data->sf2_temp[index][nr]);
959 mutex_unlock(&data->update_lock);
960 return count;
961}
962
963/* only Vcore A/B and Vtt have additional 2 bits precision */
964static ssize_t
965show_in(struct device *dev, struct device_attribute *attr, char *buf)
966{
967 struct sensor_device_attribute_2 *sensor_attr =
968 to_sensor_dev_attr_2(attr);
969 int nr = sensor_attr->nr;
970 int index = sensor_attr->index;
971 struct w83793_data *data = w83793_update_device(dev);
972 u16 val = data->in[index][nr];
973
974 if (index < 3) {
975 val <<= 2;
976 val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
977 }
Gong Junddca9332007-01-18 22:14:23 +0100978 /* voltage inputs 5VDD and 5VSB needs 150mV offset */
979 val = val * scale_in[index] + scale_in_add[index];
980 return sprintf(buf, "%d\n", val);
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100981}
982
983static ssize_t
984store_in(struct device *dev, struct device_attribute *attr,
985 const char *buf, size_t count)
986{
987 struct sensor_device_attribute_2 *sensor_attr =
988 to_sensor_dev_attr_2(attr);
989 int nr = sensor_attr->nr;
990 int index = sensor_attr->index;
991 struct i2c_client *client = to_i2c_client(dev);
992 struct w83793_data *data = i2c_get_clientdata(client);
Guenter Roeck47efe872012-01-15 10:48:48 -0800993 unsigned long val;
994 int err;
Rudolf Marek6800c3d2006-12-12 18:18:30 +0100995
Guenter Roeck47efe872012-01-15 10:48:48 -0800996 err = kstrtoul(buf, 10, &val);
997 if (err)
998 return err;
999 val = (val + scale_in[index] / 2) / scale_in[index];
1000
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001001 mutex_lock(&data->update_lock);
1002 if (index > 2) {
Gong Junddca9332007-01-18 22:14:23 +01001003 /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
Guenter Roeck47efe872012-01-15 10:48:48 -08001004 if (nr == 1 || nr == 2)
Gong Junddca9332007-01-18 22:14:23 +01001005 val -= scale_in_add[index] / scale_in[index];
Guenter Roeck2a844c12013-01-09 08:09:34 -08001006 val = clamp_val(val, 0, 255);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001007 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -08001008 val = clamp_val(val, 0, 0x3FF);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001009 data->in_low_bits[nr] =
1010 w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
1011 data->in_low_bits[nr] &= ~(0x03 << (2 * index));
1012 data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
1013 w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
1014 data->in_low_bits[nr]);
1015 val >>= 2;
1016 }
1017 data->in[index][nr] = val;
1018 w83793_write_value(client, W83793_REG_IN[index][nr],
1019 data->in[index][nr]);
1020 mutex_unlock(&data->update_lock);
1021 return count;
1022}
1023
1024#define NOT_USED -1
1025
1026#define SENSOR_ATTR_IN(index) \
1027 SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
1028 IN_READ, index), \
1029 SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
1030 store_in, IN_MAX, index), \
1031 SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
1032 store_in, IN_LOW, index), \
1033 SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
1034 NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
1035 SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
1036 show_alarm_beep, store_beep, BEEP_ENABLE, \
1037 index + ((index > 2) ? 1 : 0))
1038
1039#define SENSOR_ATTR_FAN(index) \
1040 SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
1041 NULL, ALARM_STATUS, index + 17), \
1042 SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
1043 show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
1044 SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
1045 NULL, FAN_INPUT, index - 1), \
1046 SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
1047 show_fan, store_fan_min, FAN_MIN, index - 1)
1048
1049#define SENSOR_ATTR_PWM(index) \
1050 SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
1051 store_pwm, PWM_DUTY, index - 1), \
1052 SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
1053 show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
1054 SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
1055 show_pwm, store_pwm, PWM_START, index - 1), \
1056 SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
1057 show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
1058
1059#define SENSOR_ATTR_TEMP(index) \
1060 SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
1061 show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
1062 SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
1063 NULL, TEMP_READ, index - 1), \
1064 SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
1065 store_temp, TEMP_CRIT, index - 1), \
1066 SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
1067 show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
1068 SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
1069 store_temp, TEMP_WARN, index - 1), \
1070 SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
1071 show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
1072 SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
1073 show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
1074 SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
1075 show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
1076 SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
1077 S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
1078 TEMP_FAN_MAP, index - 1), \
1079 SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
1080 show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
1081 index - 1), \
1082 SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
1083 show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
1084 SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
1085 store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
1086 SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
1087 show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
1088 SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
1089 show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
1090 SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
1091 show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
1092 SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
1093 show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
1094 SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
1095 show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
1096 SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
1097 show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
1098 SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
1099 show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
1100 SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
1101 show_sf2_temp, store_sf2_temp, 0, index - 1), \
1102 SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
1103 show_sf2_temp, store_sf2_temp, 1, index - 1), \
1104 SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
1105 show_sf2_temp, store_sf2_temp, 2, index - 1), \
1106 SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
1107 show_sf2_temp, store_sf2_temp, 3, index - 1), \
1108 SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
1109 show_sf2_temp, store_sf2_temp, 4, index - 1), \
1110 SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
1111 show_sf2_temp, store_sf2_temp, 5, index - 1), \
1112 SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
1113 show_sf2_temp, store_sf2_temp, 6, index - 1)
1114
1115static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
1116 SENSOR_ATTR_IN(0),
1117 SENSOR_ATTR_IN(1),
1118 SENSOR_ATTR_IN(2),
1119 SENSOR_ATTR_IN(3),
1120 SENSOR_ATTR_IN(4),
1121 SENSOR_ATTR_IN(5),
1122 SENSOR_ATTR_IN(6),
1123 SENSOR_ATTR_IN(7),
1124 SENSOR_ATTR_IN(8),
1125 SENSOR_ATTR_IN(9),
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001126 SENSOR_ATTR_FAN(1),
1127 SENSOR_ATTR_FAN(2),
1128 SENSOR_ATTR_FAN(3),
1129 SENSOR_ATTR_FAN(4),
1130 SENSOR_ATTR_FAN(5),
1131 SENSOR_ATTR_PWM(1),
1132 SENSOR_ATTR_PWM(2),
1133 SENSOR_ATTR_PWM(3),
1134};
1135
Gong Jun46bed4d2007-01-18 22:14:24 +01001136static struct sensor_device_attribute_2 w83793_temp[] = {
1137 SENSOR_ATTR_TEMP(1),
1138 SENSOR_ATTR_TEMP(2),
1139 SENSOR_ATTR_TEMP(3),
1140 SENSOR_ATTR_TEMP(4),
1141 SENSOR_ATTR_TEMP(5),
1142 SENSOR_ATTR_TEMP(6),
1143};
1144
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001145/* Fan6-Fan12 */
1146static struct sensor_device_attribute_2 w83793_left_fan[] = {
1147 SENSOR_ATTR_FAN(6),
1148 SENSOR_ATTR_FAN(7),
1149 SENSOR_ATTR_FAN(8),
1150 SENSOR_ATTR_FAN(9),
1151 SENSOR_ATTR_FAN(10),
1152 SENSOR_ATTR_FAN(11),
1153 SENSOR_ATTR_FAN(12),
1154};
1155
1156/* Pwm4-Pwm8 */
1157static struct sensor_device_attribute_2 w83793_left_pwm[] = {
1158 SENSOR_ATTR_PWM(4),
1159 SENSOR_ATTR_PWM(5),
1160 SENSOR_ATTR_PWM(6),
1161 SENSOR_ATTR_PWM(7),
1162 SENSOR_ATTR_PWM(8),
1163};
1164
Gong Junc70a8c32007-01-18 22:14:24 +01001165static struct sensor_device_attribute_2 w83793_vid[] = {
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001166 SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
1167 SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
Gong Junc70a8c32007-01-18 22:14:24 +01001168};
Jean Delvare93c75a42008-02-12 11:25:07 +01001169static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
Gong Junc70a8c32007-01-18 22:14:24 +01001170
1171static struct sensor_device_attribute_2 sda_single_files[] = {
Jean Delvarea516dc32011-01-12 21:55:10 +01001172 SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001173 store_chassis_clear, ALARM_STATUS, 30),
1174 SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
1175 store_beep_enable, NOT_USED, NOT_USED),
1176 SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
1177 store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
1178 SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
1179 store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
1180 SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
1181 store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
1182 SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
1183 store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
1184};
1185
1186static void w83793_init_client(struct i2c_client *client)
1187{
Guenter Roeck47efe872012-01-15 10:48:48 -08001188 if (reset)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001189 w83793_write_value(client, W83793_REG_CONFIG, 0x80);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001190
1191 /* Start monitoring */
1192 w83793_write_value(client, W83793_REG_CONFIG,
1193 w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001194}
1195
Sven Anders5852f962010-03-05 22:17:22 +01001196/*
1197 * Watchdog routines
1198 */
1199
1200static int watchdog_set_timeout(struct w83793_data *data, int timeout)
1201{
Dan Carpenter26336c82013-10-19 12:12:22 +03001202 unsigned int mtimeout;
1203 int ret;
Sven Anders5852f962010-03-05 22:17:22 +01001204
1205 mtimeout = DIV_ROUND_UP(timeout, 60);
1206
1207 if (mtimeout > 255)
1208 return -EINVAL;
1209
1210 mutex_lock(&data->watchdog_lock);
1211 if (!data->client) {
1212 ret = -ENODEV;
1213 goto leave;
1214 }
1215
1216 data->watchdog_timeout = mtimeout;
1217
1218 /* Set Timeout value (in Minutes) */
1219 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1220 data->watchdog_timeout);
1221
1222 ret = mtimeout * 60;
1223
1224leave:
1225 mutex_unlock(&data->watchdog_lock);
1226 return ret;
1227}
1228
1229static int watchdog_get_timeout(struct w83793_data *data)
1230{
1231 int timeout;
1232
1233 mutex_lock(&data->watchdog_lock);
1234 timeout = data->watchdog_timeout * 60;
1235 mutex_unlock(&data->watchdog_lock);
1236
1237 return timeout;
1238}
1239
1240static int watchdog_trigger(struct w83793_data *data)
1241{
1242 int ret = 0;
1243
1244 mutex_lock(&data->watchdog_lock);
1245 if (!data->client) {
1246 ret = -ENODEV;
1247 goto leave;
1248 }
1249
1250 /* Set Timeout value (in Minutes) */
1251 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1252 data->watchdog_timeout);
1253
1254leave:
1255 mutex_unlock(&data->watchdog_lock);
1256 return ret;
1257}
1258
1259static int watchdog_enable(struct w83793_data *data)
1260{
1261 int ret = 0;
1262
1263 mutex_lock(&data->watchdog_lock);
1264 if (!data->client) {
1265 ret = -ENODEV;
1266 goto leave;
1267 }
1268
1269 /* Set initial timeout */
1270 w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
1271 data->watchdog_timeout);
1272
1273 /* Enable Soft Watchdog */
1274 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
1275
1276leave:
1277 mutex_unlock(&data->watchdog_lock);
1278 return ret;
1279}
1280
1281static int watchdog_disable(struct w83793_data *data)
1282{
1283 int ret = 0;
1284
1285 mutex_lock(&data->watchdog_lock);
1286 if (!data->client) {
1287 ret = -ENODEV;
1288 goto leave;
1289 }
1290
1291 /* Disable Soft Watchdog */
1292 w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
1293
1294leave:
1295 mutex_unlock(&data->watchdog_lock);
1296 return ret;
1297}
1298
1299static int watchdog_open(struct inode *inode, struct file *filp)
1300{
1301 struct w83793_data *pos, *data = NULL;
1302 int watchdog_is_open;
1303
Guenter Roeck47efe872012-01-15 10:48:48 -08001304 /*
1305 * We get called from drivers/char/misc.c with misc_mtx hold, and we
1306 * call misc_register() from w83793_probe() with watchdog_data_mutex
1307 * hold, as misc_register() takes the misc_mtx lock, this is a possible
1308 * deadlock, so we use mutex_trylock here.
1309 */
Sven Anders5852f962010-03-05 22:17:22 +01001310 if (!mutex_trylock(&watchdog_data_mutex))
1311 return -ERESTARTSYS;
1312 list_for_each_entry(pos, &watchdog_data_list, list) {
1313 if (pos->watchdog_miscdev.minor == iminor(inode)) {
1314 data = pos;
1315 break;
1316 }
1317 }
1318
1319 /* Check, if device is already open */
1320 watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
1321
Guenter Roeck47efe872012-01-15 10:48:48 -08001322 /*
1323 * Increase data reference counter (if not already done).
1324 * Note we can never not have found data, so we don't check for this
1325 */
Sven Anders5852f962010-03-05 22:17:22 +01001326 if (!watchdog_is_open)
1327 kref_get(&data->kref);
1328
1329 mutex_unlock(&watchdog_data_mutex);
1330
1331 /* Check, if device is already open and possibly issue error */
1332 if (watchdog_is_open)
1333 return -EBUSY;
1334
1335 /* Enable Soft Watchdog */
1336 watchdog_enable(data);
1337
1338 /* Store pointer to data into filp's private data */
1339 filp->private_data = data;
1340
1341 return nonseekable_open(inode, filp);
1342}
1343
1344static int watchdog_close(struct inode *inode, struct file *filp)
1345{
1346 struct w83793_data *data = filp->private_data;
1347
1348 if (data->watchdog_expect_close) {
1349 watchdog_disable(data);
1350 data->watchdog_expect_close = 0;
1351 } else {
1352 watchdog_trigger(data);
1353 dev_crit(&data->client->dev,
1354 "unexpected close, not stopping watchdog!\n");
1355 }
1356
1357 clear_bit(0, &data->watchdog_is_open);
1358
1359 /* Decrease data reference counter */
1360 mutex_lock(&watchdog_data_mutex);
1361 kref_put(&data->kref, w83793_release_resources);
1362 mutex_unlock(&watchdog_data_mutex);
1363
1364 return 0;
1365}
1366
1367static ssize_t watchdog_write(struct file *filp, const char __user *buf,
1368 size_t count, loff_t *offset)
1369{
Dan Carpenter3f7cd7e2010-03-29 22:03:03 +02001370 ssize_t ret;
Sven Anders5852f962010-03-05 22:17:22 +01001371 struct w83793_data *data = filp->private_data;
1372
1373 if (count) {
1374 if (!nowayout) {
1375 size_t i;
1376
1377 /* Clear it in case it was set with a previous write */
1378 data->watchdog_expect_close = 0;
1379
1380 for (i = 0; i != count; i++) {
1381 char c;
1382 if (get_user(c, buf + i))
1383 return -EFAULT;
1384 if (c == 'V')
1385 data->watchdog_expect_close = 1;
1386 }
1387 }
1388 ret = watchdog_trigger(data);
1389 if (ret < 0)
1390 return ret;
1391 }
1392 return count;
1393}
1394
Arnd Bergmann55929332010-04-27 00:24:05 +02001395static long watchdog_ioctl(struct file *filp, unsigned int cmd,
1396 unsigned long arg)
Sven Anders5852f962010-03-05 22:17:22 +01001397{
Jean Delvare36c7fe12011-01-12 21:55:11 +01001398 struct watchdog_info ident = {
Sven Anders5852f962010-03-05 22:17:22 +01001399 .options = WDIOF_KEEPALIVEPING |
1400 WDIOF_SETTIMEOUT |
1401 WDIOF_CARDRESET,
1402 .identity = "w83793 watchdog"
1403 };
1404
1405 int val, ret = 0;
1406 struct w83793_data *data = filp->private_data;
1407
1408 switch (cmd) {
1409 case WDIOC_GETSUPPORT:
1410 if (!nowayout)
1411 ident.options |= WDIOF_MAGICCLOSE;
1412 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
1413 ret = -EFAULT;
1414 break;
1415
1416 case WDIOC_GETSTATUS:
1417 val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
1418 ret = put_user(val, (int __user *)arg);
1419 break;
1420
1421 case WDIOC_GETBOOTSTATUS:
1422 ret = put_user(0, (int __user *)arg);
1423 break;
1424
1425 case WDIOC_KEEPALIVE:
1426 ret = watchdog_trigger(data);
1427 break;
1428
1429 case WDIOC_GETTIMEOUT:
1430 val = watchdog_get_timeout(data);
1431 ret = put_user(val, (int __user *)arg);
1432 break;
1433
1434 case WDIOC_SETTIMEOUT:
1435 if (get_user(val, (int __user *)arg)) {
1436 ret = -EFAULT;
1437 break;
1438 }
1439 ret = watchdog_set_timeout(data, val);
1440 if (ret > 0)
1441 ret = put_user(ret, (int __user *)arg);
1442 break;
1443
1444 case WDIOC_SETOPTIONS:
1445 if (get_user(val, (int __user *)arg)) {
1446 ret = -EFAULT;
1447 break;
1448 }
1449
1450 if (val & WDIOS_DISABLECARD)
1451 ret = watchdog_disable(data);
1452 else if (val & WDIOS_ENABLECARD)
1453 ret = watchdog_enable(data);
1454 else
1455 ret = -EINVAL;
1456
1457 break;
1458 default:
1459 ret = -ENOTTY;
1460 }
Sven Anders5852f962010-03-05 22:17:22 +01001461 return ret;
1462}
1463
1464static const struct file_operations watchdog_fops = {
1465 .owner = THIS_MODULE,
1466 .llseek = no_llseek,
1467 .open = watchdog_open,
1468 .release = watchdog_close,
1469 .write = watchdog_write,
Arnd Bergmann55929332010-04-27 00:24:05 +02001470 .unlocked_ioctl = watchdog_ioctl,
Sven Anders5852f962010-03-05 22:17:22 +01001471};
1472
1473/*
1474 * Notifier for system down
1475 */
1476
1477static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
1478 void *unused)
1479{
1480 struct w83793_data *data = NULL;
1481
1482 if (code == SYS_DOWN || code == SYS_HALT) {
1483
1484 /* Disable each registered watchdog */
1485 mutex_lock(&watchdog_data_mutex);
1486 list_for_each_entry(data, &watchdog_data_list, list) {
1487 if (data->watchdog_miscdev.minor)
1488 watchdog_disable(data);
1489 }
1490 mutex_unlock(&watchdog_data_mutex);
1491 }
1492
1493 return NOTIFY_DONE;
1494}
1495
1496/*
1497 * The WDT needs to learn about soft shutdowns in order to
1498 * turn the timebomb registers off.
1499 */
1500
1501static struct notifier_block watchdog_notifier = {
1502 .notifier_call = watchdog_notify_sys,
1503};
1504
1505/*
1506 * Init / remove routines
1507 */
1508
Jean Delvarea7f13a62008-07-16 19:30:17 +02001509static int w83793_remove(struct i2c_client *client)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001510{
1511 struct w83793_data *data = i2c_get_clientdata(client);
1512 struct device *dev = &client->dev;
Sven Anders5852f962010-03-05 22:17:22 +01001513 int i, tmp;
1514
1515 /* Unregister the watchdog (if registered) */
1516 if (data->watchdog_miscdev.minor) {
1517 misc_deregister(&data->watchdog_miscdev);
1518
1519 if (data->watchdog_is_open) {
1520 dev_warn(&client->dev,
1521 "i2c client detached with watchdog open! "
1522 "Stopping watchdog.\n");
1523 watchdog_disable(data);
1524 }
1525
1526 mutex_lock(&watchdog_data_mutex);
1527 list_del(&data->list);
1528 mutex_unlock(&watchdog_data_mutex);
1529
1530 /* Tell the watchdog code the client is gone */
1531 mutex_lock(&data->watchdog_lock);
1532 data->client = NULL;
1533 mutex_unlock(&data->watchdog_lock);
1534 }
1535
1536 /* Reset Configuration Register to Disable Watch Dog Registers */
1537 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1538 w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
1539
1540 unregister_reboot_notifier(&watchdog_notifier);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001541
Jean Delvarea7f13a62008-07-16 19:30:17 +02001542 hwmon_device_unregister(data->hwmon_dev);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001543
Jean Delvarea7f13a62008-07-16 19:30:17 +02001544 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1545 device_remove_file(dev,
1546 &w83793_sensor_attr_2[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001547
Jean Delvarea7f13a62008-07-16 19:30:17 +02001548 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1549 device_remove_file(dev, &sda_single_files[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001550
Jean Delvarea7f13a62008-07-16 19:30:17 +02001551 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1552 device_remove_file(dev, &w83793_vid[i].dev_attr);
1553 device_remove_file(dev, &dev_attr_vrm);
Gong Junc70a8c32007-01-18 22:14:24 +01001554
Jean Delvarea7f13a62008-07-16 19:30:17 +02001555 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1556 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001557
Jean Delvarea7f13a62008-07-16 19:30:17 +02001558 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1559 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
Gong Jun46bed4d2007-01-18 22:14:24 +01001560
Jean Delvarea7f13a62008-07-16 19:30:17 +02001561 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1562 device_remove_file(dev, &w83793_temp[i].dev_attr);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001563
Jean Delvarea7f13a62008-07-16 19:30:17 +02001564 if (data->lm75[0] != NULL)
1565 i2c_unregister_device(data->lm75[0]);
1566 if (data->lm75[1] != NULL)
1567 i2c_unregister_device(data->lm75[1]);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001568
Sven Anders5852f962010-03-05 22:17:22 +01001569 /* Decrease data reference counter */
1570 mutex_lock(&watchdog_data_mutex);
1571 kref_put(&data->kref, w83793_release_resources);
1572 mutex_unlock(&watchdog_data_mutex);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001573
1574 return 0;
1575}
1576
1577static int
Jean Delvarea7f13a62008-07-16 19:30:17 +02001578w83793_detect_subclients(struct i2c_client *client)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001579{
1580 int i, id, err;
Jean Delvarea7f13a62008-07-16 19:30:17 +02001581 int address = client->addr;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001582 u8 tmp;
Jean Delvarea7f13a62008-07-16 19:30:17 +02001583 struct i2c_adapter *adapter = client->adapter;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001584 struct w83793_data *data = i2c_get_clientdata(client);
1585
1586 id = i2c_adapter_id(adapter);
1587 if (force_subclients[0] == id && force_subclients[1] == address) {
1588 for (i = 2; i <= 3; i++) {
1589 if (force_subclients[i] < 0x48
1590 || force_subclients[i] > 0x4f) {
1591 dev_err(&client->dev,
1592 "invalid subclient "
1593 "address %d; must be 0x48-0x4f\n",
1594 force_subclients[i]);
1595 err = -EINVAL;
1596 goto ERROR_SC_0;
1597 }
1598 }
1599 w83793_write_value(client, W83793_REG_I2C_SUBADDR,
1600 (force_subclients[2] & 0x07) |
1601 ((force_subclients[3] & 0x07) << 4));
1602 }
1603
1604 tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
Guenter Roeck47efe872012-01-15 10:48:48 -08001605 if (!(tmp & 0x08))
Jean Delvarea7f13a62008-07-16 19:30:17 +02001606 data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001607 if (!(tmp & 0x80)) {
1608 if ((data->lm75[0] != NULL)
1609 && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
1610 dev_err(&client->dev,
1611 "duplicate addresses 0x%x, "
1612 "use force_subclients\n", data->lm75[0]->addr);
1613 err = -ENODEV;
1614 goto ERROR_SC_1;
1615 }
Jean Delvarea7f13a62008-07-16 19:30:17 +02001616 data->lm75[1] = i2c_new_dummy(adapter,
1617 0x48 + ((tmp >> 4) & 0x7));
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001618 }
1619
1620 return 0;
1621
1622 /* Undo inits in case of errors */
1623
1624ERROR_SC_1:
Jean Delvarea7f13a62008-07-16 19:30:17 +02001625 if (data->lm75[0] != NULL)
1626 i2c_unregister_device(data->lm75[0]);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001627ERROR_SC_0:
1628 return err;
1629}
1630
Jean Delvarea7f13a62008-07-16 19:30:17 +02001631/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001632static int w83793_detect(struct i2c_client *client,
Jean Delvarea7f13a62008-07-16 19:30:17 +02001633 struct i2c_board_info *info)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001634{
Jean Delvare52df6442009-12-09 20:35:57 +01001635 u8 tmp, bank, chip_id;
Jean Delvarea7f13a62008-07-16 19:30:17 +02001636 struct i2c_adapter *adapter = client->adapter;
1637 unsigned short address = client->addr;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001638
Guenter Roeck47efe872012-01-15 10:48:48 -08001639 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvarea7f13a62008-07-16 19:30:17 +02001640 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001641
Jean Delvarea7f13a62008-07-16 19:30:17 +02001642 bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001643
Jean Delvare52df6442009-12-09 20:35:57 +01001644 tmp = bank & 0x80 ? 0x5c : 0xa3;
1645 /* Check Winbond vendor ID */
1646 if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
1647 pr_debug("w83793: Detection failed at check vendor id\n");
1648 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001649 }
1650
Guenter Roeck47efe872012-01-15 10:48:48 -08001651 /*
1652 * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
1653 * should match
1654 */
Jean Delvare52df6442009-12-09 20:35:57 +01001655 if ((bank & 0x07) == 0
1656 && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
1657 (address << 1)) {
1658 pr_debug("w83793: Detection failed at check i2c addr\n");
1659 return -ENODEV;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001660 }
1661
Jean Delvare52df6442009-12-09 20:35:57 +01001662 /* Determine the chip type now */
1663 chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
1664 if (chip_id != 0x7b)
1665 return -ENODEV;
1666
Jean Delvarea7f13a62008-07-16 19:30:17 +02001667 strlcpy(info->type, "w83793", I2C_NAME_SIZE);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001668
Jean Delvarea7f13a62008-07-16 19:30:17 +02001669 return 0;
1670}
1671
1672static int w83793_probe(struct i2c_client *client,
1673 const struct i2c_device_id *id)
1674{
1675 struct device *dev = &client->dev;
Sven Anders5852f962010-03-05 22:17:22 +01001676 const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
Jean Delvarea7f13a62008-07-16 19:30:17 +02001677 struct w83793_data *data;
1678 int i, tmp, val, err;
1679 int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
1680 int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
1681 int files_temp = ARRAY_SIZE(w83793_temp) / 6;
1682
1683 data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
1684 if (!data) {
1685 err = -ENOMEM;
1686 goto exit;
1687 }
1688
1689 i2c_set_clientdata(client, data);
1690 data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001691 mutex_init(&data->update_lock);
Sven Anders5852f962010-03-05 22:17:22 +01001692 mutex_init(&data->watchdog_lock);
1693 INIT_LIST_HEAD(&data->list);
1694 kref_init(&data->kref);
1695
Guenter Roeck47efe872012-01-15 10:48:48 -08001696 /*
1697 * Store client pointer in our data struct for watchdog usage
1698 * (where the client is found through a data ptr instead of the
1699 * otherway around)
1700 */
Sven Anders5852f962010-03-05 22:17:22 +01001701 data->client = client;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001702
Jean Delvarea7f13a62008-07-16 19:30:17 +02001703 err = w83793_detect_subclients(client);
1704 if (err)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001705 goto free_mem;
1706
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001707 /* Initialize the chip */
1708 w83793_init_client(client);
1709
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001710 /*
Guenter Roeck47efe872012-01-15 10:48:48 -08001711 * Only fan 1-5 has their own input pins,
1712 * Pwm 1-3 has their own pins
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001713 */
1714 data->has_fan = 0x1f;
1715 data->has_pwm = 0x07;
1716 tmp = w83793_read_value(client, W83793_REG_MFC);
1717 val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
1718
1719 /* check the function of pins 49-56 */
Jean Delvare93c75a42008-02-12 11:25:07 +01001720 if (tmp & 0x80) {
1721 data->has_vid |= 0x2; /* has VIDB */
1722 } else {
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001723 data->has_pwm |= 0x18; /* pwm 4,5 */
1724 if (val & 0x01) { /* fan 6 */
1725 data->has_fan |= 0x20;
1726 data->has_pwm |= 0x20;
1727 }
1728 if (val & 0x02) { /* fan 7 */
1729 data->has_fan |= 0x40;
1730 data->has_pwm |= 0x40;
1731 }
1732 if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
1733 data->has_fan |= 0x80;
1734 data->has_pwm |= 0x80;
1735 }
1736 }
1737
Jean Delvare93c75a42008-02-12 11:25:07 +01001738 /* check the function of pins 37-40 */
1739 if (!(tmp & 0x29))
1740 data->has_vid |= 0x1; /* has VIDA */
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001741 if (0x08 == (tmp & 0x0c)) {
1742 if (val & 0x08) /* fan 9 */
1743 data->has_fan |= 0x100;
1744 if (val & 0x10) /* fan 10 */
1745 data->has_fan |= 0x200;
1746 }
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001747 if (0x20 == (tmp & 0x30)) {
1748 if (val & 0x20) /* fan 11 */
1749 data->has_fan |= 0x400;
1750 if (val & 0x40) /* fan 12 */
1751 data->has_fan |= 0x800;
1752 }
1753
1754 if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
1755 data->has_fan |= 0x80;
1756 data->has_pwm |= 0x80;
1757 }
1758
Rudolf Marekc9294312007-01-18 22:14:24 +01001759 tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
1760 if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
1761 data->has_fan |= 0x100;
1762 }
1763 if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
1764 data->has_fan |= 0x200;
1765 }
1766 if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
1767 data->has_fan |= 0x400;
1768 }
1769 if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
1770 data->has_fan |= 0x800;
1771 }
1772
Gong Jun46bed4d2007-01-18 22:14:24 +01001773 /* check the temp1-6 mode, ignore former AMDSI selected inputs */
Guenter Roeck47efe872012-01-15 10:48:48 -08001774 tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
Gong Jun46bed4d2007-01-18 22:14:24 +01001775 if (tmp & 0x01)
1776 data->has_temp |= 0x01;
1777 if (tmp & 0x04)
1778 data->has_temp |= 0x02;
1779 if (tmp & 0x10)
1780 data->has_temp |= 0x04;
1781 if (tmp & 0x40)
1782 data->has_temp |= 0x08;
1783
Guenter Roeck47efe872012-01-15 10:48:48 -08001784 tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
Gong Jun46bed4d2007-01-18 22:14:24 +01001785 if (tmp & 0x01)
1786 data->has_temp |= 0x10;
1787 if (tmp & 0x02)
1788 data->has_temp |= 0x20;
1789
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001790 /* Register sysfs hooks */
1791 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
1792 err = device_create_file(dev,
1793 &w83793_sensor_attr_2[i].dev_attr);
1794 if (err)
1795 goto exit_remove;
1796 }
1797
Gong Junc70a8c32007-01-18 22:14:24 +01001798 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
1799 if (!(data->has_vid & (1 << i)))
1800 continue;
1801 err = device_create_file(dev, &w83793_vid[i].dev_attr);
1802 if (err)
1803 goto exit_remove;
1804 }
Jean Delvare93c75a42008-02-12 11:25:07 +01001805 if (data->has_vid) {
1806 data->vrm = vid_which_vrm();
1807 err = device_create_file(dev, &dev_attr_vrm);
1808 if (err)
1809 goto exit_remove;
1810 }
Gong Junc70a8c32007-01-18 22:14:24 +01001811
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001812 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
1813 err = device_create_file(dev, &sda_single_files[i].dev_attr);
1814 if (err)
1815 goto exit_remove;
1816
1817 }
1818
Gong Jun46bed4d2007-01-18 22:14:24 +01001819 for (i = 0; i < 6; i++) {
1820 int j;
1821 if (!(data->has_temp & (1 << i)))
1822 continue;
1823 for (j = 0; j < files_temp; j++) {
1824 err = device_create_file(dev,
1825 &w83793_temp[(i) * files_temp
1826 + j].dev_attr);
1827 if (err)
1828 goto exit_remove;
1829 }
1830 }
1831
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001832 for (i = 5; i < 12; i++) {
1833 int j;
1834 if (!(data->has_fan & (1 << i)))
1835 continue;
1836 for (j = 0; j < files_fan; j++) {
1837 err = device_create_file(dev,
1838 &w83793_left_fan[(i - 5) * files_fan
1839 + j].dev_attr);
1840 if (err)
1841 goto exit_remove;
1842 }
1843 }
1844
1845 for (i = 3; i < 8; i++) {
1846 int j;
1847 if (!(data->has_pwm & (1 << i)))
1848 continue;
1849 for (j = 0; j < files_pwm; j++) {
1850 err = device_create_file(dev,
1851 &w83793_left_pwm[(i - 3) * files_pwm
1852 + j].dev_attr);
1853 if (err)
1854 goto exit_remove;
1855 }
1856 }
1857
Tony Jones1beeffe2007-08-20 13:46:20 -07001858 data->hwmon_dev = hwmon_device_register(dev);
1859 if (IS_ERR(data->hwmon_dev)) {
1860 err = PTR_ERR(data->hwmon_dev);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001861 goto exit_remove;
1862 }
1863
Sven Anders5852f962010-03-05 22:17:22 +01001864 /* Watchdog initialization */
1865
1866 /* Register boot notifier */
1867 err = register_reboot_notifier(&watchdog_notifier);
1868 if (err != 0) {
1869 dev_err(&client->dev,
1870 "cannot register reboot notifier (err=%d)\n", err);
1871 goto exit_devunreg;
1872 }
1873
Guenter Roeck47efe872012-01-15 10:48:48 -08001874 /*
1875 * Enable Watchdog registers.
1876 * Set Configuration Register to Enable Watch Dog Registers
1877 * (Bit 2) = XXXX, X1XX.
1878 */
Sven Anders5852f962010-03-05 22:17:22 +01001879 tmp = w83793_read_value(client, W83793_REG_CONFIG);
1880 w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
1881
1882 /* Set the default watchdog timeout */
1883 data->watchdog_timeout = timeout;
1884
1885 /* Check, if last reboot was caused by watchdog */
1886 data->watchdog_caused_reboot =
1887 w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
1888
1889 /* Disable Soft Watchdog during initialiation */
1890 watchdog_disable(data);
1891
Guenter Roeck47efe872012-01-15 10:48:48 -08001892 /*
1893 * We take the data_mutex lock early so that watchdog_open() cannot
1894 * run when misc_register() has completed, but we've not yet added
1895 * our data to the watchdog_data_list (and set the default timeout)
1896 */
Sven Anders5852f962010-03-05 22:17:22 +01001897 mutex_lock(&watchdog_data_mutex);
1898 for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
1899 /* Register our watchdog part */
1900 snprintf(data->watchdog_name, sizeof(data->watchdog_name),
1901 "watchdog%c", (i == 0) ? '\0' : ('0' + i));
1902 data->watchdog_miscdev.name = data->watchdog_name;
1903 data->watchdog_miscdev.fops = &watchdog_fops;
1904 data->watchdog_miscdev.minor = watchdog_minors[i];
1905
1906 err = misc_register(&data->watchdog_miscdev);
1907 if (err == -EBUSY)
1908 continue;
1909 if (err) {
1910 data->watchdog_miscdev.minor = 0;
1911 dev_err(&client->dev,
1912 "Registering watchdog chardev: %d\n", err);
1913 break;
1914 }
1915
1916 list_add(&data->list, &watchdog_data_list);
1917
1918 dev_info(&client->dev,
1919 "Registered watchdog chardev major 10, minor: %d\n",
1920 watchdog_minors[i]);
1921 break;
1922 }
1923 if (i == ARRAY_SIZE(watchdog_minors)) {
1924 data->watchdog_miscdev.minor = 0;
Guenter Roeckb55f3752013-01-10 10:01:24 -08001925 dev_warn(&client->dev,
1926 "Couldn't register watchdog chardev (due to no free minor)\n");
Sven Anders5852f962010-03-05 22:17:22 +01001927 }
1928
1929 mutex_unlock(&watchdog_data_mutex);
1930
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001931 return 0;
1932
Sven Anders5852f962010-03-05 22:17:22 +01001933 /* Unregister hwmon device */
1934
1935exit_devunreg:
1936
1937 hwmon_device_unregister(data->hwmon_dev);
1938
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001939 /* Unregister sysfs hooks */
1940
1941exit_remove:
1942 for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
1943 device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
1944
1945 for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
1946 device_remove_file(dev, &sda_single_files[i].dev_attr);
1947
Gong Junc70a8c32007-01-18 22:14:24 +01001948 for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
1949 device_remove_file(dev, &w83793_vid[i].dev_attr);
1950
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001951 for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
1952 device_remove_file(dev, &w83793_left_fan[i].dev_attr);
1953
1954 for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
1955 device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
1956
Gong Jun46bed4d2007-01-18 22:14:24 +01001957 for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
1958 device_remove_file(dev, &w83793_temp[i].dev_attr);
1959
Jean Delvarea7f13a62008-07-16 19:30:17 +02001960 if (data->lm75[0] != NULL)
1961 i2c_unregister_device(data->lm75[0]);
1962 if (data->lm75[1] != NULL)
1963 i2c_unregister_device(data->lm75[1]);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001964free_mem:
1965 kfree(data);
1966exit:
1967 return err;
1968}
1969
1970static void w83793_update_nonvolatile(struct device *dev)
1971{
1972 struct i2c_client *client = to_i2c_client(dev);
1973 struct w83793_data *data = i2c_get_clientdata(client);
1974 int i, j;
1975 /*
Guenter Roeck47efe872012-01-15 10:48:48 -08001976 * They are somewhat "stable" registers, and to update them every time
1977 * takes so much time, it's just not worthy. Update them in a long
1978 * interval to avoid exception.
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001979 */
1980 if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
1981 || !data->valid))
1982 return;
1983 /* update voltage limits */
1984 for (i = 1; i < 3; i++) {
1985 for (j = 0; j < ARRAY_SIZE(data->in); j++) {
1986 data->in[j][i] =
1987 w83793_read_value(client, W83793_REG_IN[j][i]);
1988 }
1989 data->in_low_bits[i] =
1990 w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
1991 }
1992
1993 for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
1994 /* Update the Fan measured value and limits */
Guenter Roeck47efe872012-01-15 10:48:48 -08001995 if (!(data->has_fan & (1 << i)))
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001996 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01001997 data->fan_min[i] =
1998 w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
1999 data->fan_min[i] |=
2000 w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
2001 }
2002
2003 for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
Gong Jun46bed4d2007-01-18 22:14:24 +01002004 if (!(data->has_temp & (1 << i)))
2005 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002006 data->temp_fan_map[i] =
2007 w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
2008 for (j = 1; j < 5; j++) {
2009 data->temp[i][j] =
2010 w83793_read_value(client, W83793_REG_TEMP[i][j]);
2011 }
2012 data->temp_cruise[i] =
2013 w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
2014 for (j = 0; j < 7; j++) {
2015 data->sf2_pwm[i][j] =
2016 w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
2017 data->sf2_temp[i][j] =
2018 w83793_read_value(client,
2019 W83793_REG_SF2_TEMP(i, j));
2020 }
2021 }
2022
2023 for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
2024 data->temp_mode[i] =
2025 w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
2026
2027 for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
2028 data->tolerance[i] =
2029 w83793_read_value(client, W83793_REG_TEMP_TOL(i));
2030 }
2031
2032 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2033 if (!(data->has_pwm & (1 << i)))
2034 continue;
2035 data->pwm[i][PWM_NONSTOP] =
2036 w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
2037 data->pwm[i][PWM_START] =
2038 w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
2039 data->pwm_stop_time[i] =
2040 w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
2041 }
2042
2043 data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
2044 data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
2045 data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
2046 data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
2047 data->temp_critical =
2048 w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
2049 data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
2050
Guenter Roeck47efe872012-01-15 10:48:48 -08002051 for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002052 data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002053
2054 data->last_nonvolatile = jiffies;
2055}
2056
2057static struct w83793_data *w83793_update_device(struct device *dev)
2058{
2059 struct i2c_client *client = to_i2c_client(dev);
2060 struct w83793_data *data = i2c_get_clientdata(client);
2061 int i;
2062
2063 mutex_lock(&data->update_lock);
2064
2065 if (!(time_after(jiffies, data->last_updated + HZ * 2)
2066 || !data->valid))
2067 goto END;
2068
2069 /* Update the voltages measured value and limits */
2070 for (i = 0; i < ARRAY_SIZE(data->in); i++)
2071 data->in[i][IN_READ] =
2072 w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
2073
2074 data->in_low_bits[IN_READ] =
2075 w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
2076
2077 for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
Guenter Roeck47efe872012-01-15 10:48:48 -08002078 if (!(data->has_fan & (1 << i)))
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002079 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002080 data->fan[i] =
2081 w83793_read_value(client, W83793_REG_FAN(i)) << 8;
2082 data->fan[i] |=
2083 w83793_read_value(client, W83793_REG_FAN(i) + 1);
2084 }
2085
Gong Jun46bed4d2007-01-18 22:14:24 +01002086 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
2087 if (!(data->has_temp & (1 << i)))
2088 continue;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002089 data->temp[i][TEMP_READ] =
2090 w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
Gong Jun46bed4d2007-01-18 22:14:24 +01002091 }
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002092
2093 data->temp_low_bits =
2094 w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
2095
2096 for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
2097 if (data->has_pwm & (1 << i))
2098 data->pwm[i][PWM_DUTY] =
2099 w83793_read_value(client,
2100 W83793_REG_PWM(i, PWM_DUTY));
2101 }
2102
2103 for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
2104 data->alarms[i] =
2105 w83793_read_value(client, W83793_REG_ALARM(i));
Gong Junc70a8c32007-01-18 22:14:24 +01002106 if (data->has_vid & 0x01)
2107 data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
2108 if (data->has_vid & 0x02)
2109 data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002110 w83793_update_nonvolatile(dev);
2111 data->last_updated = jiffies;
2112 data->valid = 1;
2113
2114END:
2115 mutex_unlock(&data->update_lock);
2116 return data;
2117}
2118
Guenter Roeck47efe872012-01-15 10:48:48 -08002119/*
2120 * Ignore the possibility that somebody change bank outside the driver
2121 * Must be called with data->update_lock held, except during initialization
2122 */
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002123static u8 w83793_read_value(struct i2c_client *client, u16 reg)
2124{
2125 struct w83793_data *data = i2c_get_clientdata(client);
2126 u8 res = 0xff;
2127 u8 new_bank = reg >> 8;
2128
2129 new_bank |= data->bank & 0xfc;
2130 if (data->bank != new_bank) {
2131 if (i2c_smbus_write_byte_data
2132 (client, W83793_REG_BANKSEL, new_bank) >= 0)
2133 data->bank = new_bank;
2134 else {
2135 dev_err(&client->dev,
2136 "set bank to %d failed, fall back "
2137 "to bank %d, read reg 0x%x error\n",
2138 new_bank, data->bank, reg);
2139 res = 0x0; /* read 0x0 from the chip */
2140 goto END;
2141 }
2142 }
2143 res = i2c_smbus_read_byte_data(client, reg & 0xff);
2144END:
2145 return res;
2146}
2147
2148/* Must be called with data->update_lock held, except during initialization */
2149static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
2150{
2151 struct w83793_data *data = i2c_get_clientdata(client);
2152 int res;
2153 u8 new_bank = reg >> 8;
2154
2155 new_bank |= data->bank & 0xfc;
2156 if (data->bank != new_bank) {
Guenter Roeck47efe872012-01-15 10:48:48 -08002157 res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
2158 new_bank);
2159 if (res < 0) {
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002160 dev_err(&client->dev,
2161 "set bank to %d failed, fall back "
2162 "to bank %d, write reg 0x%x error\n",
2163 new_bank, data->bank, reg);
2164 goto END;
2165 }
Guenter Roeck47efe872012-01-15 10:48:48 -08002166 data->bank = new_bank;
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002167 }
2168
2169 res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
2170END:
2171 return res;
2172}
2173
Axel Linf0967ee2012-01-20 15:38:18 +08002174module_i2c_driver(w83793_driver);
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002175
Sven Anders5852f962010-03-05 22:17:22 +01002176MODULE_AUTHOR("Yuan Mu, Sven Anders");
Rudolf Marek6800c3d2006-12-12 18:18:30 +01002177MODULE_DESCRIPTION("w83793 driver");
2178MODULE_LICENSE("GPL");