blob: 2b0f182daa8752fd0e7b54f91c4ac53d13a2ccba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeckaff6e002012-01-19 11:02:27 -08002 * w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
5 * Philip Edelbrock <phil@netroedge.com>,
6 * and Mark Studebaker <mdsxyz123@yahoo.com>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01007 * Copyright (c) 2007 - 2008 Jean Delvare <jdelvare@suse.de>
Guenter Roeckaff6e002012-01-19 11:02:27 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
Guenter Roeckaff6e002012-01-19 11:02:27 -080025 * Supports following chips:
26 *
Jean Delvare4101ece2012-11-05 21:54:40 +010027 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeckaff6e002012-01-19 11:02:27 -080028 * as99127f 7 3 0 3 0x31 0x12c3 yes no
29 * as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
30 * w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
31 * w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
32 * w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
33 *
34 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Joe Perches1ca28212011-01-12 21:55:11 +010036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/jiffies.h>
42#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040043#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020044#include <linux/hwmon-vid.h>
Jean Delvare34875332007-05-08 17:22:03 +020045#include <linux/hwmon-sysfs.h>
Jim Cromie311ce2e2006-09-24 21:22:52 +020046#include <linux/sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040047#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010048#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wolfgang Grandegger443850c2008-10-17 17:51:18 +020050#ifdef CONFIG_ISA
51#include <linux/platform_device.h>
52#include <linux/ioport.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020053#include <linux/io.h>
Wolfgang Grandegger443850c2008-10-17 17:51:18 +020054#endif
55
56#include "lm75.h"
Jean Delvare7666c132007-05-08 17:22:02 +020057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050059static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
60 0x2e, 0x2f, I2C_CLIENT_END };
Jean Delvare3aed1982009-01-07 16:37:32 +010061
Jean Delvaree5e9f442009-12-14 21:17:27 +010062enum chips { w83781d, w83782d, w83783s, as99127f };
63
64/* Insmod parameters */
Jean Delvare3aed1982009-01-07 16:37:32 +010065static unsigned short force_subclients[4];
66module_param_array(force_subclients, short, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080067MODULE_PARM_DESC(force_subclients,
68 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Rusty Russell90ab5ee2012-01-13 09:32:20 +103070static bool reset;
Jean Delvarefabddcd2006-02-05 23:26:51 +010071module_param(reset, bool, 0);
72MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075module_param(init, bool, 0);
76MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
77
78/* Constants specified below */
79
80/* Length of ISA address segment */
81#define W83781D_EXTENT 8
82
83/* Where are the ISA address/data registers relative to the base address */
84#define W83781D_ADDR_REG_OFFSET 5
85#define W83781D_DATA_REG_OFFSET 6
86
Jean Delvare34875332007-05-08 17:22:03 +020087/* The device registers */
88/* in nr from 0 to 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
90 (0x554 + (((nr) - 7) * 2)))
91#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
92 (0x555 + (((nr) - 7) * 2)))
93#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
94 (0x550 + (nr) - 7))
95
Jean Delvare34875332007-05-08 17:22:03 +020096/* fan nr from 0 to 2 */
97#define W83781D_REG_FAN_MIN(nr) (0x3b + (nr))
98#define W83781D_REG_FAN(nr) (0x28 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define W83781D_REG_BANK 0x4E
101#define W83781D_REG_TEMP2_CONFIG 0x152
102#define W83781D_REG_TEMP3_CONFIG 0x252
Jean Delvare34875332007-05-08 17:22:03 +0200103/* temp nr from 1 to 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
105 ((nr == 2) ? (0x0150) : \
106 (0x27)))
107#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
108 ((nr == 2) ? (0x153) : \
109 (0x3A)))
110#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
111 ((nr == 2) ? (0x155) : \
112 (0x39)))
113
114#define W83781D_REG_CONFIG 0x40
Jean Delvarec7f5d7e2006-02-05 23:13:48 +0100115
116/* Interrupt status (W83781D, AS99127F) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define W83781D_REG_ALARM1 0x41
118#define W83781D_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jean Delvare05663362007-11-30 23:51:24 +0100120/* Real-time status (W83782D, W83783S) */
Jean Delvarec7f5d7e2006-02-05 23:13:48 +0100121#define W83782D_REG_ALARM1 0x459
122#define W83782D_REG_ALARM2 0x45A
123#define W83782D_REG_ALARM3 0x45B
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define W83781D_REG_BEEP_CONFIG 0x4D
126#define W83781D_REG_BEEP_INTS1 0x56
127#define W83781D_REG_BEEP_INTS2 0x57
128#define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
129
130#define W83781D_REG_VID_FANDIV 0x47
131
132#define W83781D_REG_CHIPID 0x49
133#define W83781D_REG_WCHIPID 0x58
134#define W83781D_REG_CHIPMAN 0x4F
135#define W83781D_REG_PIN 0x4B
136
137/* 782D/783S only */
138#define W83781D_REG_VBAT 0x5D
139
140/* PWM 782D (1-4) and 783S (1-2) only */
Jean Delvare34875332007-05-08 17:22:03 +0200141static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define W83781D_REG_PWMCLK12 0x5C
143#define W83781D_REG_PWMCLK34 0x45C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#define W83781D_REG_I2C_ADDR 0x48
146#define W83781D_REG_I2C_SUBADDR 0x4A
147
Guenter Roeckaff6e002012-01-19 11:02:27 -0800148/*
149 * The following are undocumented in the data sheets however we
150 * received the information in an email from Winbond tech support
151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Sensor selection - not on 781d */
153#define W83781D_REG_SCFG1 0x5D
154static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
155
156#define W83781D_REG_SCFG2 0x59
157static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
158
159#define W83781D_DEFAULT_BETA 3435
160
Jean Delvare474d00a2007-05-08 17:22:03 +0200161/* Conversions */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800162#define IN_TO_REG(val) clamp_val(((val) + 8) / 16, 0, 255)
Jean Delvare474d00a2007-05-08 17:22:03 +0200163#define IN_FROM_REG(val) ((val) * 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165static inline u8
166FAN_TO_REG(long rpm, int div)
167{
168 if (rpm == 0)
169 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800170 rpm = clamp_val(rpm, 1, 1000000);
171 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Jean Delvare474d00a2007-05-08 17:22:03 +0200174static inline long
175FAN_FROM_REG(u8 val, int div)
176{
177 if (val == 0)
178 return -1;
179 if (val == 255)
180 return 0;
181 return 1350000 / (val * div);
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Guenter Roeck2a844c12013-01-09 08:09:34 -0800184#define TEMP_TO_REG(val) clamp_val((val) / 1000, -127, 128)
Jean Delvare474d00a2007-05-08 17:22:03 +0200185#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Guenter Roeckc531eb32012-01-15 09:19:16 -0800187#define BEEP_MASK_FROM_REG(val, type) ((type) == as99127f ? \
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200188 (~(val)) & 0x7fff : (val) & 0xff7fff)
Guenter Roeckc531eb32012-01-15 09:19:16 -0800189#define BEEP_MASK_TO_REG(val, type) ((type) == as99127f ? \
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200190 (~(val)) & 0x7fff : (val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define DIV_FROM_REG(val) (1 << (val))
193
194static inline u8
195DIV_TO_REG(long val, enum chips type)
196{
197 int i;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800198 val = clamp_val(val, 1,
199 ((type == w83781d || type == as99127f) ? 8 : 128)) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000200 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (val == 0)
202 break;
203 val >>= 1;
204 }
Jean Delvare474d00a2007-05-08 17:22:03 +0200205 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208struct w83781d_data {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200209 struct i2c_client *client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700210 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100211 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 enum chips type;
213
Jean Delvare360782d2008-10-17 17:51:19 +0200214 /* For ISA device only */
215 const char *name;
216 int isa_addr;
217
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100218 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 char valid; /* !=0 if following fields are valid */
220 unsigned long last_updated; /* In jiffies */
221
222 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
223 /* array of 2 pointers to subclients */
224
225 u8 in[9]; /* Register value - 8 & 9 for 782D only */
226 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
227 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
228 u8 fan[3]; /* Register value */
229 u8 fan_min[3]; /* Register value */
Jean Delvare474d00a2007-05-08 17:22:03 +0200230 s8 temp; /* Register value */
231 s8 temp_max; /* Register value */
232 s8 temp_max_hyst; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 u16 temp_add[2]; /* Register value */
234 u16 temp_max_add[2]; /* Register value */
235 u16 temp_max_hyst_add[2]; /* Register value */
236 u8 fan_div[3]; /* Register encoding, shifted right */
237 u8 vid; /* Register encoding, combined */
238 u32 alarms; /* Register encoding, combined */
239 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 u8 pwm[4]; /* Register value */
Jean Delvare34875332007-05-08 17:22:03 +0200241 u8 pwm2_enable; /* Boolean */
Guenter Roeckaff6e002012-01-19 11:02:27 -0800242 u16 sens[3]; /*
243 * 782D/783S only.
244 * 1 = pentium diode; 2 = 3904 diode;
245 * 4 = thermistor
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u8 vrm;
248};
249
Wolfgang Grandegger443850c2008-10-17 17:51:18 +0200250static struct w83781d_data *w83781d_data_if_isa(void);
251static int w83781d_alias_detect(struct i2c_client *client, u8 chipid);
252
Jean Delvare31b8dc42007-05-08 17:22:03 +0200253static int w83781d_read_value(struct w83781d_data *data, u16 reg);
254static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static struct w83781d_data *w83781d_update_device(struct device *dev);
Jean Delvare7666c132007-05-08 17:22:02 +0200256static void w83781d_init_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* following are the sysfs callback functions */
259#define show_in_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800260static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200261 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{ \
Jean Delvare34875332007-05-08 17:22:03 +0200263 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct w83781d_data *data = w83781d_update_device(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200265 return sprintf(buf, "%ld\n", \
266 (long)IN_FROM_REG(data->reg[attr->index])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268show_in_reg(in);
269show_in_reg(in_min);
270show_in_reg(in_max);
271
272#define store_in_reg(REG, reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800273static ssize_t store_in_##reg(struct device *dev, struct device_attribute \
Jean Delvare34875332007-05-08 17:22:03 +0200274 *da, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{ \
Jean Delvare34875332007-05-08 17:22:03 +0200276 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Jean Delvare7666c132007-05-08 17:22:02 +0200277 struct w83781d_data *data = dev_get_drvdata(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200278 int nr = attr->index; \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800279 unsigned long val; \
280 int err = kstrtoul(buf, 10, &val); \
281 if (err) \
282 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100283 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 data->in_##reg[nr] = IN_TO_REG(val); \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800285 w83781d_write_value(data, W83781D_REG_IN_##REG(nr), \
286 data->in_##reg[nr]); \
287 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100288 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return count; \
290}
291store_in_reg(MIN, min);
292store_in_reg(MAX, max);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#define sysfs_in_offsets(offset) \
Jean Delvare34875332007-05-08 17:22:03 +0200295static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
296 show_in, NULL, offset); \
297static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
298 show_in_min, store_in_min, offset); \
299static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
300 show_in_max, store_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302sysfs_in_offsets(0);
303sysfs_in_offsets(1);
304sysfs_in_offsets(2);
305sysfs_in_offsets(3);
306sysfs_in_offsets(4);
307sysfs_in_offsets(5);
308sysfs_in_offsets(6);
309sysfs_in_offsets(7);
310sysfs_in_offsets(8);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#define show_fan_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800313static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200314 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{ \
Jean Delvare34875332007-05-08 17:22:03 +0200316 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct w83781d_data *data = w83781d_update_device(dev); \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800318 return sprintf(buf, "%ld\n", \
Jean Delvare34875332007-05-08 17:22:03 +0200319 FAN_FROM_REG(data->reg[attr->index], \
320 DIV_FROM_REG(data->fan_div[attr->index]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322show_fan_reg(fan);
323show_fan_reg(fan_min);
324
325static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200326store_fan_min(struct device *dev, struct device_attribute *da,
327 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Jean Delvare34875332007-05-08 17:22:03 +0200329 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200330 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200331 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800332 unsigned long val;
333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Guenter Roeckc531eb32012-01-15 09:19:16 -0800335 err = kstrtoul(buf, 10, &val);
336 if (err)
337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100339 mutex_lock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200340 data->fan_min[nr] =
341 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare31b8dc42007-05-08 17:22:03 +0200342 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
Jean Delvare34875332007-05-08 17:22:03 +0200343 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100345 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return count;
347}
348
Jean Delvare34875332007-05-08 17:22:03 +0200349static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
350static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
351 show_fan_min, store_fan_min, 0);
352static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
353static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
354 show_fan_min, store_fan_min, 1);
355static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
356static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
357 show_fan_min, store_fan_min, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#define show_temp_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800360static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200361 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{ \
Jean Delvare34875332007-05-08 17:22:03 +0200363 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct w83781d_data *data = w83781d_update_device(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200365 int nr = attr->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800367 return sprintf(buf, "%d\n", \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
369 } else { /* TEMP1 */ \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800370 return sprintf(buf, "%ld\n", (long)TEMP_FROM_REG(data->reg)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } \
372}
373show_temp_reg(temp);
374show_temp_reg(temp_max);
375show_temp_reg(temp_max_hyst);
376
377#define store_temp_reg(REG, reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800378static ssize_t store_temp_##reg(struct device *dev, \
Jean Delvare34875332007-05-08 17:22:03 +0200379 struct device_attribute *da, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{ \
Jean Delvare34875332007-05-08 17:22:03 +0200381 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Jean Delvare7666c132007-05-08 17:22:02 +0200382 struct w83781d_data *data = dev_get_drvdata(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200383 int nr = attr->index; \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200384 long val; \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800385 int err = kstrtol(buf, 10, &val); \
386 if (err) \
387 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100388 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 \
390 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
391 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
Jean Delvare31b8dc42007-05-08 17:22:03 +0200392 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 data->temp_##reg##_add[nr-2]); \
394 } else { /* TEMP1 */ \
395 data->temp_##reg = TEMP_TO_REG(val); \
Jean Delvare31b8dc42007-05-08 17:22:03 +0200396 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 data->temp_##reg); \
398 } \
399 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100400 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return count; \
402}
403store_temp_reg(OVER, max);
404store_temp_reg(HYST, max_hyst);
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#define sysfs_temp_offsets(offset) \
Jean Delvare34875332007-05-08 17:22:03 +0200407static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
408 show_temp, NULL, offset); \
409static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
410 show_temp_max, store_temp_max, offset); \
411static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
412 show_temp_max_hyst, store_temp_max_hyst, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414sysfs_temp_offsets(1);
415sysfs_temp_offsets(2);
416sysfs_temp_offsets(3);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100419cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct w83781d_data *data = w83781d_update_device(dev);
422 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
423}
424
Julia Lawallb80b8142016-12-22 13:05:12 +0100425static DEVICE_ATTR_RO(cpu0_vid);
Jim Cromie311ce2e2006-09-24 21:22:52 +0200426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100428vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Jean Delvare90d66192007-10-08 18:24:35 +0200430 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return sprintf(buf, "%ld\n", (long) data->vrm);
432}
433
434static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100435vrm_store(struct device *dev, struct device_attribute *attr, const char *buf,
436 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jean Delvare7666c132007-05-08 17:22:02 +0200438 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800439 unsigned long val;
440 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Guenter Roeckc531eb32012-01-15 09:19:16 -0800442 err = kstrtoul(buf, 10, &val);
443 if (err)
444 return err;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800445 data->vrm = clamp_val(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 return count;
448}
449
Julia Lawallb80b8142016-12-22 13:05:12 +0100450static DEVICE_ATTR_RW(vrm);
Jim Cromie311ce2e2006-09-24 21:22:52 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100453alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200456 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Julia Lawallb80b8142016-12-22 13:05:12 +0100459static DEVICE_ATTR_RO(alarms);
Jim Cromie311ce2e2006-09-24 21:22:52 +0200460
Jean Delvare7d4a1372007-10-08 18:29:43 +0200461static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
462 char *buf)
463{
464 struct w83781d_data *data = w83781d_update_device(dev);
465 int bitnr = to_sensor_dev_attr(attr)->index;
466 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
467}
468
469/* The W83781D has a single alarm bit for temp2 and temp3 */
470static ssize_t show_temp3_alarm(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
473 struct w83781d_data *data = w83781d_update_device(dev);
474 int bitnr = (data->type == w83781d) ? 5 : 13;
475 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
476}
477
478static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
479static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
480static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
481static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
482static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
483static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
484static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
485static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
486static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
487static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
488static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
489static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
490static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
491static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
492static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0);
493
Julia Lawallb80b8142016-12-22 13:05:12 +0100494static ssize_t beep_mask_show(struct device *dev,
Guenter Roeckc531eb32012-01-15 09:19:16 -0800495 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct w83781d_data *data = w83781d_update_device(dev);
498 return sprintf(buf, "%ld\n",
499 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100503beep_mask_store(struct device *dev, struct device_attribute *attr,
Jean Delvare34875332007-05-08 17:22:03 +0200504 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jean Delvare7666c132007-05-08 17:22:02 +0200506 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800507 unsigned long val;
508 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Guenter Roeckc531eb32012-01-15 09:19:16 -0800510 err = kstrtoul(buf, 10, &val);
511 if (err)
512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100514 mutex_lock(&data->update_lock);
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200515 data->beep_mask &= 0x8000; /* preserve beep enable */
516 data->beep_mask |= BEEP_MASK_TO_REG(val, data->type);
Jean Delvare34875332007-05-08 17:22:03 +0200517 w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
518 data->beep_mask & 0xff);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200519 w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200520 (data->beep_mask >> 8) & 0xff);
Jean Delvare34875332007-05-08 17:22:03 +0200521 if (data->type != w83781d && data->type != as99127f) {
522 w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
523 ((data->beep_mask) >> 16) & 0xff);
524 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100525 mutex_unlock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return count;
528}
529
Julia Lawallb80b8142016-12-22 13:05:12 +0100530static DEVICE_ATTR_RW(beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jean Delvare7d4a1372007-10-08 18:29:43 +0200532static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
533 char *buf)
534{
535 struct w83781d_data *data = w83781d_update_device(dev);
536 int bitnr = to_sensor_dev_attr(attr)->index;
537 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
538}
539
540static ssize_t
541store_beep(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
543{
544 struct w83781d_data *data = dev_get_drvdata(dev);
545 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvare7d4a1372007-10-08 18:29:43 +0200546 u8 reg;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800547 unsigned long bit;
548 int err;
Jean Delvare7d4a1372007-10-08 18:29:43 +0200549
Guenter Roeckc531eb32012-01-15 09:19:16 -0800550 err = kstrtoul(buf, 10, &bit);
551 if (err)
552 return err;
553
Jean Delvare7d4a1372007-10-08 18:29:43 +0200554 if (bit & ~1)
555 return -EINVAL;
556
557 mutex_lock(&data->update_lock);
558 if (bit)
559 data->beep_mask |= (1 << bitnr);
560 else
561 data->beep_mask &= ~(1 << bitnr);
562
563 if (bitnr < 8) {
564 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
565 if (bit)
566 reg |= (1 << bitnr);
567 else
568 reg &= ~(1 << bitnr);
569 w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg);
570 } else if (bitnr < 16) {
571 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
572 if (bit)
573 reg |= (1 << (bitnr - 8));
574 else
575 reg &= ~(1 << (bitnr - 8));
576 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg);
577 } else {
578 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3);
579 if (bit)
580 reg |= (1 << (bitnr - 16));
581 else
582 reg &= ~(1 << (bitnr - 16));
583 w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg);
584 }
585 mutex_unlock(&data->update_lock);
586
587 return count;
588}
589
590/* The W83781D has a single beep bit for temp2 and temp3 */
591static ssize_t show_temp3_beep(struct device *dev,
592 struct device_attribute *attr, char *buf)
593{
594 struct w83781d_data *data = w83781d_update_device(dev);
595 int bitnr = (data->type == w83781d) ? 5 : 13;
596 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
597}
598
599static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
600 show_beep, store_beep, 0);
601static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
602 show_beep, store_beep, 1);
603static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
604 show_beep, store_beep, 2);
605static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
606 show_beep, store_beep, 3);
607static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
608 show_beep, store_beep, 8);
609static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
610 show_beep, store_beep, 9);
611static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
612 show_beep, store_beep, 10);
613static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
614 show_beep, store_beep, 16);
615static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
616 show_beep, store_beep, 17);
617static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
618 show_beep, store_beep, 6);
619static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
620 show_beep, store_beep, 7);
621static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
622 show_beep, store_beep, 11);
623static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
624 show_beep, store_beep, 4);
625static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
626 show_beep, store_beep, 5);
627static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO,
628 show_temp3_beep, store_beep, 13);
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200629static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
630 show_beep, store_beep, 15);
Jean Delvare7d4a1372007-10-08 18:29:43 +0200631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200633show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jean Delvare34875332007-05-08 17:22:03 +0200635 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct w83781d_data *data = w83781d_update_device(dev);
637 return sprintf(buf, "%ld\n",
Jean Delvare34875332007-05-08 17:22:03 +0200638 (long) DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Guenter Roeckaff6e002012-01-19 11:02:27 -0800641/*
642 * Note: we save and restore the fan minimum here, because its value is
643 * determined in part by the fan divisor. This follows the principle of
644 * least surprise; the user doesn't expect the fan minimum to change just
645 * because the divisor changed.
646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200648store_fan_div(struct device *dev, struct device_attribute *da,
649 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Jean Delvare34875332007-05-08 17:22:03 +0200651 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200652 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned long min;
Jean Delvare34875332007-05-08 17:22:03 +0200654 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 u8 reg;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800656 unsigned long val;
657 int err;
658
659 err = kstrtoul(buf, 10, &val);
660 if (err)
661 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100663 mutex_lock(&data->update_lock);
Jean Delvare293c09972007-11-30 23:52:44 +0100664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* Save fan_min */
666 min = FAN_FROM_REG(data->fan_min[nr],
667 DIV_FROM_REG(data->fan_div[nr]));
668
669 data->fan_div[nr] = DIV_TO_REG(val, data->type);
670
Guenter Roeckc531eb32012-01-15 09:19:16 -0800671 reg = (w83781d_read_value(data, nr == 2 ?
672 W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
673 & (nr == 0 ? 0xcf : 0x3f))
674 | ((data->fan_div[nr] & 0x03) << (nr == 0 ? 4 : 6));
675 w83781d_write_value(data, nr == 2 ?
676 W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /* w83781d and as99127f don't have extended divisor bits */
679 if (data->type != w83781d && data->type != as99127f) {
Jean Delvare31b8dc42007-05-08 17:22:03 +0200680 reg = (w83781d_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 & ~(1 << (5 + nr)))
682 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare31b8dc42007-05-08 17:22:03 +0200683 w83781d_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 /* Restore fan_min */
687 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare34875332007-05-08 17:22:03 +0200688 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100690 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return count;
692}
693
Jean Delvare34875332007-05-08 17:22:03 +0200694static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
695 show_fan_div, store_fan_div, 0);
696static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
697 show_fan_div, store_fan_div, 1);
698static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
699 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200702show_pwm(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Jean Delvare34875332007-05-08 17:22:03 +0200704 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200706 return sprintf(buf, "%d\n", (int)data->pwm[attr->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100710pwm2_enable_show(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200713 return sprintf(buf, "%d\n", (int)data->pwm2_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200717store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
718 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jean Delvare34875332007-05-08 17:22:03 +0200720 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200721 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200722 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800723 unsigned long val;
724 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Guenter Roeckc531eb32012-01-15 09:19:16 -0800726 err = kstrtoul(buf, 10, &val);
727 if (err)
728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100730 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800731 data->pwm[nr] = clamp_val(val, 0, 255);
Jean Delvare34875332007-05-08 17:22:03 +0200732 w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100733 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return count;
735}
736
737static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100738pwm2_enable_store(struct device *dev, struct device_attribute *da,
Jean Delvare34875332007-05-08 17:22:03 +0200739 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Jean Delvare7666c132007-05-08 17:22:02 +0200741 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800742 unsigned long val;
743 u32 reg;
744 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Guenter Roeckc531eb32012-01-15 09:19:16 -0800746 err = kstrtoul(buf, 10, &val);
747 if (err)
748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100750 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 switch (val) {
753 case 0:
754 case 1:
Jean Delvare31b8dc42007-05-08 17:22:03 +0200755 reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
756 w83781d_write_value(data, W83781D_REG_PWMCLK12,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 (reg & 0xf7) | (val << 3));
758
Jean Delvare31b8dc42007-05-08 17:22:03 +0200759 reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
760 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 (reg & 0xef) | (!val << 4));
762
Jean Delvare34875332007-05-08 17:22:03 +0200763 data->pwm2_enable = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765
766 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100767 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EINVAL;
769 }
770
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100771 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return count;
773}
774
Jean Delvare34875332007-05-08 17:22:03 +0200775static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
776static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
777static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
778static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
779/* only PWM2 can be enabled/disabled */
Julia Lawallb80b8142016-12-22 13:05:12 +0100780static DEVICE_ATTR_RW(pwm2_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200783show_sensor(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jean Delvare34875332007-05-08 17:22:03 +0200785 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200787 return sprintf(buf, "%d\n", (int)data->sens[attr->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200791store_sensor(struct device *dev, struct device_attribute *da,
792 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Jean Delvare34875332007-05-08 17:22:03 +0200794 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200795 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200796 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800797 unsigned long val;
798 u32 tmp;
799 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Guenter Roeckc531eb32012-01-15 09:19:16 -0800801 err = kstrtoul(buf, 10, &val);
802 if (err)
803 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100805 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 switch (val) {
808 case 1: /* PII/Celeron diode */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200809 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
810 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200811 tmp | BIT_SCFG1[nr]);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200812 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
813 w83781d_write_value(data, W83781D_REG_SCFG2,
Jean Delvare34875332007-05-08 17:22:03 +0200814 tmp | BIT_SCFG2[nr]);
815 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
817 case 2: /* 3904 */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200818 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
819 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200820 tmp | BIT_SCFG1[nr]);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200821 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
822 w83781d_write_value(data, W83781D_REG_SCFG2,
Jean Delvare34875332007-05-08 17:22:03 +0200823 tmp & ~BIT_SCFG2[nr]);
824 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
Jean Delvareb26f9332007-08-16 14:30:01 +0200826 case W83781D_DEFAULT_BETA:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800827 dev_warn(dev,
828 "Sensor type %d is deprecated, please use 4 instead\n",
829 W83781D_DEFAULT_BETA);
Jean Delvareb26f9332007-08-16 14:30:01 +0200830 /* fall through */
831 case 4: /* thermistor */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200832 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
833 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200834 tmp & ~BIT_SCFG1[nr]);
835 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 default:
Jean Delvareb26f9332007-08-16 14:30:01 +0200838 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n",
839 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
841 }
842
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100843 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return count;
845}
846
Jean Delvare34875332007-05-08 17:22:03 +0200847static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
848 show_sensor, store_sensor, 0);
849static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
Mark M. Hoffman393cdad2007-08-09 08:12:46 -0400850 show_sensor, store_sensor, 1);
Jean Delvare34875332007-05-08 17:22:03 +0200851static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
Mark M. Hoffman393cdad2007-08-09 08:12:46 -0400852 show_sensor, store_sensor, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Guenter Roeckaff6e002012-01-19 11:02:27 -0800854/*
855 * Assumes that adapter is of I2C, not ISA variety.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * OTHERWISE DON'T CALL THIS
857 */
858static int
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200859w83781d_detect_subclients(struct i2c_client *new_client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 int i, val1 = 0, id;
862 int err;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200863 int address = new_client->addr;
864 unsigned short sc_addr[2];
865 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct w83781d_data *data = i2c_get_clientdata(new_client);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200867 enum chips kind = data->type;
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700868 int num_sc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 id = i2c_adapter_id(adapter);
871
872 if (force_subclients[0] == id && force_subclients[1] == address) {
873 for (i = 2; i <= 3; i++) {
874 if (force_subclients[i] < 0x48 ||
875 force_subclients[i] > 0x4f) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800876 dev_err(&new_client->dev,
877 "Invalid subclient address %d; must be 0x48-0x4f\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 force_subclients[i]);
879 err = -EINVAL;
880 goto ERROR_SC_1;
881 }
882 }
Jean Delvare31b8dc42007-05-08 17:22:03 +0200883 w83781d_write_value(data, W83781D_REG_I2C_SUBADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 (force_subclients[2] & 0x07) |
885 ((force_subclients[3] & 0x07) << 4));
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200886 sc_addr[0] = force_subclients[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else {
Jean Delvare31b8dc42007-05-08 17:22:03 +0200888 val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200889 sc_addr[0] = 0x48 + (val1 & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 if (kind != w83783s) {
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700893 num_sc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (force_subclients[0] == id &&
895 force_subclients[1] == address) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200896 sc_addr[1] = force_subclients[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 } else {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200898 sc_addr[1] = 0x48 + ((val1 >> 4) & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200900 if (sc_addr[0] == sc_addr[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dev_err(&new_client->dev,
902 "Duplicate addresses 0x%x for subclients.\n",
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200903 sc_addr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 err = -EBUSY;
905 goto ERROR_SC_2;
906 }
907 }
908
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700909 for (i = 0; i < num_sc; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200910 data->lm75[i] = i2c_new_dummy(adapter, sc_addr[i]);
911 if (!data->lm75[i]) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800912 dev_err(&new_client->dev,
913 "Subclient %d registration at address 0x%x failed.\n",
914 i, sc_addr[i]);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200915 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (i == 1)
917 goto ERROR_SC_3;
918 goto ERROR_SC_2;
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
922 return 0;
923
924/* Undo inits in case of errors */
925ERROR_SC_3:
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200926 i2c_unregister_device(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927ERROR_SC_2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928ERROR_SC_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return err;
930}
931
Jean Delvare34875332007-05-08 17:22:03 +0200932#define IN_UNIT_ATTRS(X) \
933 &sensor_dev_attr_in##X##_input.dev_attr.attr, \
934 &sensor_dev_attr_in##X##_min.dev_attr.attr, \
Jean Delvare293c09972007-11-30 23:52:44 +0100935 &sensor_dev_attr_in##X##_max.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200936 &sensor_dev_attr_in##X##_alarm.dev_attr.attr, \
937 &sensor_dev_attr_in##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200938
Jean Delvare34875332007-05-08 17:22:03 +0200939#define FAN_UNIT_ATTRS(X) \
940 &sensor_dev_attr_fan##X##_input.dev_attr.attr, \
941 &sensor_dev_attr_fan##X##_min.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200942 &sensor_dev_attr_fan##X##_div.dev_attr.attr, \
943 &sensor_dev_attr_fan##X##_alarm.dev_attr.attr, \
944 &sensor_dev_attr_fan##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200945
Jean Delvare34875332007-05-08 17:22:03 +0200946#define TEMP_UNIT_ATTRS(X) \
947 &sensor_dev_attr_temp##X##_input.dev_attr.attr, \
948 &sensor_dev_attr_temp##X##_max.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200949 &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr, \
950 &sensor_dev_attr_temp##X##_alarm.dev_attr.attr, \
951 &sensor_dev_attr_temp##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200952
Guenter Roeckc531eb32012-01-15 09:19:16 -0800953static struct attribute *w83781d_attributes[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200954 IN_UNIT_ATTRS(0),
955 IN_UNIT_ATTRS(2),
956 IN_UNIT_ATTRS(3),
957 IN_UNIT_ATTRS(4),
958 IN_UNIT_ATTRS(5),
959 IN_UNIT_ATTRS(6),
960 FAN_UNIT_ATTRS(1),
961 FAN_UNIT_ATTRS(2),
962 FAN_UNIT_ATTRS(3),
963 TEMP_UNIT_ATTRS(1),
964 TEMP_UNIT_ATTRS(2),
965 &dev_attr_cpu0_vid.attr,
966 &dev_attr_vrm.attr,
967 &dev_attr_alarms.attr,
968 &dev_attr_beep_mask.attr,
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200969 &sensor_dev_attr_beep_enable.dev_attr.attr,
Jim Cromie311ce2e2006-09-24 21:22:52 +0200970 NULL
971};
972static const struct attribute_group w83781d_group = {
973 .attrs = w83781d_attributes,
974};
975
Guenter Roeck79501332012-01-16 20:45:16 -0800976static struct attribute *w83781d_attributes_in1[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200977 IN_UNIT_ATTRS(1),
Guenter Roeck79501332012-01-16 20:45:16 -0800978 NULL
979};
980static const struct attribute_group w83781d_group_in1 = {
981 .attrs = w83781d_attributes_in1,
982};
983
984static struct attribute *w83781d_attributes_in78[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200985 IN_UNIT_ATTRS(7),
986 IN_UNIT_ATTRS(8),
Guenter Roeck79501332012-01-16 20:45:16 -0800987 NULL
988};
989static const struct attribute_group w83781d_group_in78 = {
990 .attrs = w83781d_attributes_in78,
991};
992
993static struct attribute *w83781d_attributes_temp3[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200994 TEMP_UNIT_ATTRS(3),
Guenter Roeck79501332012-01-16 20:45:16 -0800995 NULL
996};
997static const struct attribute_group w83781d_group_temp3 = {
998 .attrs = w83781d_attributes_temp3,
999};
1000
1001static struct attribute *w83781d_attributes_pwm12[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001002 &sensor_dev_attr_pwm1.dev_attr.attr,
1003 &sensor_dev_attr_pwm2.dev_attr.attr,
Guenter Roeck79501332012-01-16 20:45:16 -08001004 &dev_attr_pwm2_enable.attr,
1005 NULL
1006};
1007static const struct attribute_group w83781d_group_pwm12 = {
1008 .attrs = w83781d_attributes_pwm12,
1009};
1010
1011static struct attribute *w83781d_attributes_pwm34[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001012 &sensor_dev_attr_pwm3.dev_attr.attr,
1013 &sensor_dev_attr_pwm4.dev_attr.attr,
Guenter Roeck79501332012-01-16 20:45:16 -08001014 NULL
1015};
1016static const struct attribute_group w83781d_group_pwm34 = {
1017 .attrs = w83781d_attributes_pwm34,
1018};
1019
1020static struct attribute *w83781d_attributes_other[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001021 &sensor_dev_attr_temp1_type.dev_attr.attr,
1022 &sensor_dev_attr_temp2_type.dev_attr.attr,
1023 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jim Cromie311ce2e2006-09-24 21:22:52 +02001024 NULL
1025};
Guenter Roeck79501332012-01-16 20:45:16 -08001026static const struct attribute_group w83781d_group_other = {
1027 .attrs = w83781d_attributes_other,
Jim Cromie311ce2e2006-09-24 21:22:52 +02001028};
1029
Jean Delvare7666c132007-05-08 17:22:02 +02001030/* No clean up is done on error, it's up to the caller */
1031static int
1032w83781d_create_files(struct device *dev, int kind, int is_isa)
1033{
1034 int err;
1035
Guenter Roeckc531eb32012-01-15 09:19:16 -08001036 err = sysfs_create_group(&dev->kobj, &w83781d_group);
1037 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001038 return err;
1039
1040 if (kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001041 err = sysfs_create_group(&dev->kobj, &w83781d_group_in1);
1042 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001043 return err;
1044 }
1045 if (kind != as99127f && kind != w83781d && kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001046 err = sysfs_create_group(&dev->kobj, &w83781d_group_in78);
1047 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001048 return err;
1049 }
1050 if (kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001051 err = sysfs_create_group(&dev->kobj, &w83781d_group_temp3);
1052 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001053 return err;
Jean Delvare7d4a1372007-10-08 18:29:43 +02001054
Jean Delvare7768aa72007-10-25 13:11:01 +02001055 if (kind != w83781d) {
Jean Delvare7d4a1372007-10-08 18:29:43 +02001056 err = sysfs_chmod_file(&dev->kobj,
1057 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1058 S_IRUGO | S_IWUSR);
1059 if (err)
1060 return err;
Jean Delvare7768aa72007-10-25 13:11:01 +02001061 }
Jean Delvare7666c132007-05-08 17:22:02 +02001062 }
1063
1064 if (kind != w83781d && kind != as99127f) {
Guenter Roeck79501332012-01-16 20:45:16 -08001065 err = sysfs_create_group(&dev->kobj, &w83781d_group_pwm12);
1066 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001067 return err;
1068 }
1069 if (kind == w83782d && !is_isa) {
Guenter Roeck79501332012-01-16 20:45:16 -08001070 err = sysfs_create_group(&dev->kobj, &w83781d_group_pwm34);
1071 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001072 return err;
1073 }
1074
1075 if (kind != as99127f && kind != w83781d) {
Guenter Roeck79501332012-01-16 20:45:16 -08001076 err = device_create_file(dev,
1077 &sensor_dev_attr_temp1_type.dev_attr);
1078 if (err)
1079 return err;
1080 err = device_create_file(dev,
1081 &sensor_dev_attr_temp2_type.dev_attr);
1082 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001083 return err;
1084 if (kind != w83783s) {
Guenter Roeckc531eb32012-01-15 09:19:16 -08001085 err = device_create_file(dev,
Guenter Roeck79501332012-01-16 20:45:16 -08001086 &sensor_dev_attr_temp3_type.dev_attr);
Guenter Roeckc531eb32012-01-15 09:19:16 -08001087 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001088 return err;
1089 }
1090 }
1091
Jean Delvare7666c132007-05-08 17:22:02 +02001092 return 0;
1093}
1094
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001095/* Return 0 if detection is successful, -ENODEV otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static int
Jean Delvare310ec792009-12-14 21:17:23 +01001097w83781d_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Jean Delvarebab2bf42009-12-09 20:35:54 +01001099 int val1, val2;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001100 struct w83781d_data *isa = w83781d_data_if_isa();
1101 struct i2c_adapter *adapter = client->adapter;
1102 int address = client->addr;
Jean Delvarebab2bf42009-12-09 20:35:54 +01001103 const char *client_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 enum vendor { winbond, asus } vendid;
1105
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001106 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1107 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Guenter Roeckaff6e002012-01-19 11:02:27 -08001109 /*
1110 * We block updates of the ISA device to minimize the risk of
1111 * concurrent access to the same W83781D chip through different
1112 * interfaces.
1113 */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001114 if (isa)
1115 mutex_lock(&isa->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jean Delvarebab2bf42009-12-09 20:35:54 +01001117 if (i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG) & 0x80) {
1118 dev_dbg(&adapter->dev,
1119 "Detection of w83781d chip failed at step 3\n");
1120 goto err_nodev;
1121 }
1122
1123 val1 = i2c_smbus_read_byte_data(client, W83781D_REG_BANK);
1124 val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
1125 /* Check for Winbond or Asus ID if in bank 0 */
1126 if (!(val1 & 0x07) &&
1127 ((!(val1 & 0x80) && val2 != 0xa3 && val2 != 0xc3) ||
Guenter Roeckc531eb32012-01-15 09:19:16 -08001128 ((val1 & 0x80) && val2 != 0x5c && val2 != 0x12))) {
Jean Delvarebab2bf42009-12-09 20:35:54 +01001129 dev_dbg(&adapter->dev,
1130 "Detection of w83781d chip failed at step 4\n");
1131 goto err_nodev;
1132 }
Guenter Roeckaff6e002012-01-19 11:02:27 -08001133 /*
1134 * If Winbond SMBus, check address at 0x48.
1135 * Asus doesn't support, except for as99127f rev.2
1136 */
Jean Delvarebab2bf42009-12-09 20:35:54 +01001137 if ((!(val1 & 0x80) && val2 == 0xa3) ||
Guenter Roeckc531eb32012-01-15 09:19:16 -08001138 ((val1 & 0x80) && val2 == 0x5c)) {
Jean Delvarebab2bf42009-12-09 20:35:54 +01001139 if (i2c_smbus_read_byte_data(client, W83781D_REG_I2C_ADDR)
1140 != address) {
1141 dev_dbg(&adapter->dev,
1142 "Detection of w83781d chip failed at step 5\n");
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001143 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
Jean Delvarebab2bf42009-12-09 20:35:54 +01001147 /* Put it now into bank 0 and Vendor ID High Byte */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001148 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1149 (i2c_smbus_read_byte_data(client, W83781D_REG_BANK)
1150 & 0x78) | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jean Delvarebab2bf42009-12-09 20:35:54 +01001152 /* Get the vendor ID */
1153 val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
1154 if (val2 == 0x5c)
1155 vendid = winbond;
1156 else if (val2 == 0x12)
1157 vendid = asus;
1158 else {
1159 dev_dbg(&adapter->dev,
1160 "w83781d chip vendor is neither Winbond nor Asus\n");
1161 goto err_nodev;
1162 }
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* Determine the chip type. */
Jean Delvarebab2bf42009-12-09 20:35:54 +01001165 val1 = i2c_smbus_read_byte_data(client, W83781D_REG_WCHIPID);
1166 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1167 client_name = "w83781d";
1168 else if (val1 == 0x30 && vendid == winbond)
1169 client_name = "w83782d";
1170 else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
1171 client_name = "w83783s";
1172 else if (val1 == 0x31)
1173 client_name = "as99127f";
1174 else
1175 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jean Delvarebab2bf42009-12-09 20:35:54 +01001177 if (val1 <= 0x30 && w83781d_alias_detect(client, val1)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001178 dev_dbg(&adapter->dev,
1179 "Device at 0x%02x appears to be the same as ISA device\n",
1180 address);
Jean Delvarebab2bf42009-12-09 20:35:54 +01001181 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001184 if (isa)
1185 mutex_unlock(&isa->update_lock);
1186
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001187 strlcpy(info->type, client_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001189 return 0;
1190
1191 err_nodev:
1192 if (isa)
1193 mutex_unlock(&isa->update_lock);
1194 return -ENODEV;
1195}
1196
Guenter Roeck79501332012-01-16 20:45:16 -08001197static void w83781d_remove_files(struct device *dev)
1198{
1199 sysfs_remove_group(&dev->kobj, &w83781d_group);
1200 sysfs_remove_group(&dev->kobj, &w83781d_group_in1);
1201 sysfs_remove_group(&dev->kobj, &w83781d_group_in78);
1202 sysfs_remove_group(&dev->kobj, &w83781d_group_temp3);
1203 sysfs_remove_group(&dev->kobj, &w83781d_group_pwm12);
1204 sysfs_remove_group(&dev->kobj, &w83781d_group_pwm34);
1205 sysfs_remove_group(&dev->kobj, &w83781d_group_other);
1206}
1207
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001208static int
1209w83781d_probe(struct i2c_client *client, const struct i2c_device_id *id)
1210{
1211 struct device *dev = &client->dev;
1212 struct w83781d_data *data;
1213 int err;
1214
Guenter Roeck144d2b92012-06-02 11:48:00 -07001215 data = devm_kzalloc(dev, sizeof(struct w83781d_data), GFP_KERNEL);
1216 if (!data)
1217 return -ENOMEM;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001218
1219 i2c_set_clientdata(client, data);
1220 mutex_init(&data->lock);
1221 mutex_init(&data->update_lock);
1222
1223 data->type = id->driver_data;
1224 data->client = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* attach secondary i2c lm75-like clients */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001227 err = w83781d_detect_subclients(client);
1228 if (err)
Guenter Roeck144d2b92012-06-02 11:48:00 -07001229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* Initialize the chip */
Jean Delvare7666c132007-05-08 17:22:02 +02001232 w83781d_init_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* Register sysfs hooks */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001235 err = w83781d_create_files(dev, data->type, 0);
Jean Delvare7666c132007-05-08 17:22:02 +02001236 if (err)
Guenter Roeck144d2b92012-06-02 11:48:00 -07001237 goto exit_remove_files;
Jim Cromie311ce2e2006-09-24 21:22:52 +02001238
Tony Jones1beeffe2007-08-20 13:46:20 -07001239 data->hwmon_dev = hwmon_device_register(dev);
1240 if (IS_ERR(data->hwmon_dev)) {
1241 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck144d2b92012-06-02 11:48:00 -07001242 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001243 }
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246
Guenter Roeck144d2b92012-06-02 11:48:00 -07001247 exit_remove_files:
Guenter Roeck79501332012-01-16 20:45:16 -08001248 w83781d_remove_files(dev);
Andy Shevchenko0ab21d0ed2017-10-31 16:21:43 +02001249 i2c_unregister_device(data->lm75[0]);
1250 i2c_unregister_device(data->lm75[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return err;
1252}
1253
1254static int
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001255w83781d_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001257 struct w83781d_data *data = i2c_get_clientdata(client);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001258 struct device *dev = &client->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001260 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck79501332012-01-16 20:45:16 -08001261 w83781d_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Andy Shevchenko0ab21d0ed2017-10-31 16:21:43 +02001263 i2c_unregister_device(data->lm75[0]);
1264 i2c_unregister_device(data->lm75[1]);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001270w83781d_read_value_i2c(struct w83781d_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001272 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001273 int res, bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct i2c_client *cl;
1275
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001276 bank = (reg >> 8) & 0x0f;
1277 if (bank > 2)
1278 /* switch banks */
1279 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1280 bank);
1281 if (bank == 0 || bank > 2) {
1282 res = i2c_smbus_read_byte_data(client, reg & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 } else {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001284 /* switch to subclient */
1285 cl = data->lm75[bank - 1];
1286 /* convert from ISA to LM75 I2C addresses */
1287 switch (reg & 0xff) {
1288 case 0x50: /* TEMP */
Jean Delvare90f41022011-11-04 12:00:47 +01001289 res = i2c_smbus_read_word_swapped(cl, 0);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001290 break;
1291 case 0x52: /* CONFIG */
1292 res = i2c_smbus_read_byte_data(cl, 1);
1293 break;
1294 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +01001295 res = i2c_smbus_read_word_swapped(cl, 2);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001296 break;
1297 case 0x55: /* OVER */
1298 default:
Jean Delvare90f41022011-11-04 12:00:47 +01001299 res = i2c_smbus_read_word_swapped(cl, 3);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001303 if (bank > 2)
1304 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return res;
1307}
1308
1309static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001310w83781d_write_value_i2c(struct w83781d_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001312 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001313 int bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct i2c_client *cl;
1315
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001316 bank = (reg >> 8) & 0x0f;
1317 if (bank > 2)
1318 /* switch banks */
1319 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1320 bank);
1321 if (bank == 0 || bank > 2) {
1322 i2c_smbus_write_byte_data(client, reg & 0xff,
1323 value & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 } else {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001325 /* switch to subclient */
1326 cl = data->lm75[bank - 1];
1327 /* convert from ISA to LM75 I2C addresses */
1328 switch (reg & 0xff) {
1329 case 0x52: /* CONFIG */
1330 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1331 break;
1332 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +01001333 i2c_smbus_write_word_swapped(cl, 2, value);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001334 break;
1335 case 0x55: /* OVER */
Jean Delvare90f41022011-11-04 12:00:47 +01001336 i2c_smbus_write_word_swapped(cl, 3, value);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001340 if (bank > 2)
1341 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346static void
Jean Delvare7666c132007-05-08 17:22:02 +02001347w83781d_init_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Jean Delvare7666c132007-05-08 17:22:02 +02001349 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int i, p;
1351 int type = data->type;
1352 u8 tmp;
1353
Guenter Roeckaff6e002012-01-19 11:02:27 -08001354 if (reset && type != as99127f) { /*
1355 * this resets registers we don't have
1356 * documentation for on the as99127f
1357 */
1358 /*
1359 * Resetting the chip has been the default for a long time,
1360 * but it causes the BIOS initializations (fan clock dividers,
1361 * thermal sensor types...) to be lost, so it is now optional.
1362 * It might even go away if nobody reports it as being useful,
1363 * as I see very little reason why this would be needed at
1364 * all.
1365 */
Guenter Roeckb55f3752013-01-10 10:01:24 -08001366 dev_info(dev,
1367 "If reset=1 solved a problem you were having, please report!\n");
Jean Delvarefabddcd2006-02-05 23:26:51 +01001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* save these registers */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001370 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1371 p = w83781d_read_value(data, W83781D_REG_PWMCLK12);
Guenter Roeckaff6e002012-01-19 11:02:27 -08001372 /*
1373 * Reset all except Watchdog values and last conversion values
1374 * This sets fan-divs to 2, among others
1375 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001376 w83781d_write_value(data, W83781D_REG_CONFIG, 0x80);
Guenter Roeckaff6e002012-01-19 11:02:27 -08001377 /*
1378 * Restore the registers and disable power-on abnormal beep.
1379 * This saves FAN 1/2/3 input/output values set by BIOS.
1380 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001381 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
1382 w83781d_write_value(data, W83781D_REG_PWMCLK12, p);
Guenter Roeckc531eb32012-01-15 09:19:16 -08001383 /*
1384 * Disable master beep-enable (reset turns it on).
1385 * Individual beep_mask should be reset to off but for some
1386 * reason disabling this bit helps some people not get beeped
1387 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001388 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Guenter Roeckaff6e002012-01-19 11:02:27 -08001391 /*
1392 * Disable power-on abnormal beep, as advised by the datasheet.
1393 * Already done if reset=1.
1394 */
Jean Delvarefabddcd2006-02-05 23:26:51 +01001395 if (init && !reset && type != as99127f) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001396 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1397 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
Jean Delvarefabddcd2006-02-05 23:26:51 +01001398 }
1399
Jean Delvare303760b2005-07-31 21:52:01 +02001400 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if ((type != w83781d) && (type != as99127f)) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001403 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for (i = 1; i <= 3; i++) {
1405 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001406 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 } else {
1408 if (w83781d_read_value
Jean Delvare31b8dc42007-05-08 17:22:03 +02001409 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1411 data->sens[i - 1] = 1;
1412 else
1413 data->sens[i - 1] = 2;
1414 }
Jean Delvare7c7a5302005-06-16 19:24:14 +02001415 if (type == w83783s && i == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
1417 }
1418 }
1419
1420 if (init && type != as99127f) {
1421 /* Enable temp2 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001422 tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (tmp & 0x01) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001424 dev_warn(dev,
1425 "Enabling temp2, readings might not make sense\n");
Jean Delvare31b8dc42007-05-08 17:22:03 +02001426 w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 tmp & 0xfe);
1428 }
1429
1430 /* Enable temp3 */
Jean Delvare7c7a5302005-06-16 19:24:14 +02001431 if (type != w83783s) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001432 tmp = w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 W83781D_REG_TEMP3_CONFIG);
1434 if (tmp & 0x01) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001435 dev_warn(dev,
1436 "Enabling temp3, readings might not make sense\n");
Jean Delvare31b8dc42007-05-08 17:22:03 +02001437 w83781d_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1439 }
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
1443 /* Start monitoring */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001444 w83781d_write_value(data, W83781D_REG_CONFIG,
1445 (w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 W83781D_REG_CONFIG) & 0xf7)
1447 | 0x01);
Jean Delvare7666c132007-05-08 17:22:02 +02001448
1449 /* A few vars need to be filled upon startup */
Jean Delvare34875332007-05-08 17:22:03 +02001450 for (i = 0; i < 3; i++) {
1451 data->fan_min[i] = w83781d_read_value(data,
Jean Delvare7666c132007-05-08 17:22:02 +02001452 W83781D_REG_FAN_MIN(i));
1453 }
Jean Delvare7666c132007-05-08 17:22:02 +02001454
1455 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458static struct w83781d_data *w83781d_update_device(struct device *dev)
1459{
Jean Delvare7666c132007-05-08 17:22:02 +02001460 struct w83781d_data *data = dev_get_drvdata(dev);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001461 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int i;
1463
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001464 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1467 || !data->valid) {
1468 dev_dbg(dev, "Starting device update\n");
1469
1470 for (i = 0; i <= 8; i++) {
Jean Delvare7c7a5302005-06-16 19:24:14 +02001471 if (data->type == w83783s && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 continue; /* 783S has no in1 */
1473 data->in[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001474 w83781d_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 data->in_min[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001476 w83781d_read_value(data, W83781D_REG_IN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 data->in_max[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001478 w83781d_read_value(data, W83781D_REG_IN_MAX(i));
Jean Delvare05663362007-11-30 23:51:24 +01001479 if ((data->type != w83782d) && (i == 6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481 }
Jean Delvare34875332007-05-08 17:22:03 +02001482 for (i = 0; i < 3; i++) {
1483 data->fan[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001484 w83781d_read_value(data, W83781D_REG_FAN(i));
Jean Delvare34875332007-05-08 17:22:03 +02001485 data->fan_min[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001486 w83781d_read_value(data, W83781D_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 if (data->type != w83781d && data->type != as99127f) {
Jean Delvare34875332007-05-08 17:22:03 +02001489 for (i = 0; i < 4; i++) {
1490 data->pwm[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001491 w83781d_read_value(data,
Jean Delvare34875332007-05-08 17:22:03 +02001492 W83781D_REG_PWM[i]);
Jean Delvare848ddf12009-05-08 20:27:28 +02001493 /* Only W83782D on SMBus has PWM3 and PWM4 */
1494 if ((data->type != w83782d || !client)
Jean Delvare34875332007-05-08 17:22:03 +02001495 && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 }
1498 /* Only PWM2 can be disabled */
Jean Delvare34875332007-05-08 17:22:03 +02001499 data->pwm2_enable = (w83781d_read_value(data,
Guenter Roeckc531eb32012-01-15 09:19:16 -08001500 W83781D_REG_PWMCLK12) & 0x08) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502
Jean Delvare31b8dc42007-05-08 17:22:03 +02001503 data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 data->temp_max =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001505 w83781d_read_value(data, W83781D_REG_TEMP_OVER(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 data->temp_max_hyst =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001507 w83781d_read_value(data, W83781D_REG_TEMP_HYST(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 data->temp_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001509 w83781d_read_value(data, W83781D_REG_TEMP(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 data->temp_max_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001511 w83781d_read_value(data, W83781D_REG_TEMP_OVER(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 data->temp_max_hyst_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001513 w83781d_read_value(data, W83781D_REG_TEMP_HYST(2));
Jean Delvare7c7a5302005-06-16 19:24:14 +02001514 if (data->type != w83783s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 data->temp_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001516 w83781d_read_value(data, W83781D_REG_TEMP(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 data->temp_max_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001518 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 W83781D_REG_TEMP_OVER(3));
1520 data->temp_max_hyst_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001521 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 W83781D_REG_TEMP_HYST(3));
1523 }
Jean Delvare31b8dc42007-05-08 17:22:03 +02001524 i = w83781d_read_value(data, W83781D_REG_VID_FANDIV);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001525 data->vid = i & 0x0f;
Jean Delvare31b8dc42007-05-08 17:22:03 +02001526 data->vid |= (w83781d_read_value(data,
Jean Delvare7c7a5302005-06-16 19:24:14 +02001527 W83781D_REG_CHIPID) & 0x01) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 data->fan_div[0] = (i >> 4) & 0x03;
1529 data->fan_div[1] = (i >> 6) & 0x03;
Jean Delvare31b8dc42007-05-08 17:22:03 +02001530 data->fan_div[2] = (w83781d_read_value(data,
Jean Delvare7c7a5302005-06-16 19:24:14 +02001531 W83781D_REG_PIN) >> 6) & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if ((data->type != w83781d) && (data->type != as99127f)) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001533 i = w83781d_read_value(data, W83781D_REG_VBAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 data->fan_div[0] |= (i >> 3) & 0x04;
1535 data->fan_div[1] |= (i >> 4) & 0x04;
Jean Delvare7c7a5302005-06-16 19:24:14 +02001536 data->fan_div[2] |= (i >> 5) & 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Jean Delvare05663362007-11-30 23:51:24 +01001538 if (data->type == w83782d) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001539 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001540 W83782D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001541 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001542 W83782D_REG_ALARM2) << 8)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001543 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001544 W83782D_REG_ALARM3) << 16);
1545 } else if (data->type == w83783s) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001546 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001547 W83782D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001548 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001549 W83782D_REG_ALARM2) << 8);
1550 } else {
Guenter Roeckaff6e002012-01-19 11:02:27 -08001551 /*
1552 * No real-time status registers, fall back to
1553 * interrupt status registers
1554 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001555 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001556 W83781D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001557 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001558 W83781D_REG_ALARM2) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Jean Delvare31b8dc42007-05-08 17:22:03 +02001560 i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare2fbbbf12008-10-17 17:51:18 +02001561 data->beep_mask = (i << 8) +
Jean Delvare31b8dc42007-05-08 17:22:03 +02001562 w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if ((data->type != w83781d) && (data->type != as99127f)) {
1564 data->beep_mask |=
Jean Delvare31b8dc42007-05-08 17:22:03 +02001565 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 W83781D_REG_BEEP_INTS3) << 16;
1567 }
1568 data->last_updated = jiffies;
1569 data->valid = 1;
1570 }
1571
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001572 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 return data;
1575}
1576
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001577static const struct i2c_device_id w83781d_ids[] = {
1578 { "w83781d", w83781d, },
1579 { "w83782d", w83782d, },
1580 { "w83783s", w83783s, },
1581 { "as99127f", as99127f },
1582 { /* LIST END */ }
1583};
1584MODULE_DEVICE_TABLE(i2c, w83781d_ids);
1585
1586static struct i2c_driver w83781d_driver = {
1587 .class = I2C_CLASS_HWMON,
1588 .driver = {
1589 .name = "w83781d",
1590 },
1591 .probe = w83781d_probe,
1592 .remove = w83781d_remove,
1593 .id_table = w83781d_ids,
1594 .detect = w83781d_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001595 .address_list = normal_i2c,
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001596};
1597
1598/*
1599 * ISA related code
1600 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001601#ifdef CONFIG_ISA
1602
1603/* ISA device, if found */
1604static struct platform_device *pdev;
1605
1606static unsigned short isa_address = 0x290;
1607
Guenter Roeckaff6e002012-01-19 11:02:27 -08001608/*
1609 * I2C devices get this name attribute automatically, but for ISA devices
1610 * we must create it by ourselves.
1611 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001612static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +01001613name_show(struct device *dev, struct device_attribute *devattr, char *buf)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001614{
1615 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare360782d2008-10-17 17:51:19 +02001616 return sprintf(buf, "%s\n", data->name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001617}
Julia Lawallb80b8142016-12-22 13:05:12 +01001618static DEVICE_ATTR_RO(name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001619
1620static struct w83781d_data *w83781d_data_if_isa(void)
1621{
1622 return pdev ? platform_get_drvdata(pdev) : NULL;
1623}
1624
1625/* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
1626static int w83781d_alias_detect(struct i2c_client *client, u8 chipid)
1627{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001628 struct w83781d_data *isa;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001629 int i;
1630
1631 if (!pdev) /* No ISA chip */
1632 return 0;
1633
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001634 isa = platform_get_drvdata(pdev);
1635
1636 if (w83781d_read_value(isa, W83781D_REG_I2C_ADDR) != client->addr)
1637 return 0; /* Address doesn't match */
1638 if (w83781d_read_value(isa, W83781D_REG_WCHIPID) != chipid)
1639 return 0; /* Chip type doesn't match */
1640
Guenter Roeckaff6e002012-01-19 11:02:27 -08001641 /*
1642 * We compare all the limit registers, the config register and the
1643 * interrupt mask registers
1644 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001645 for (i = 0x2b; i <= 0x3d; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001646 if (w83781d_read_value(isa, i) !=
1647 i2c_smbus_read_byte_data(client, i))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001648 return 0;
1649 }
1650 if (w83781d_read_value(isa, W83781D_REG_CONFIG) !=
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001651 i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001652 return 0;
1653 for (i = 0x43; i <= 0x46; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001654 if (w83781d_read_value(isa, i) !=
1655 i2c_smbus_read_byte_data(client, i))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001656 return 0;
1657 }
1658
1659 return 1;
1660}
1661
1662static int
1663w83781d_read_value_isa(struct w83781d_data *data, u16 reg)
1664{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001665 int word_sized, res;
1666
1667 word_sized = (((reg & 0xff00) == 0x100)
1668 || ((reg & 0xff00) == 0x200))
1669 && (((reg & 0x00ff) == 0x50)
1670 || ((reg & 0x00ff) == 0x53)
1671 || ((reg & 0x00ff) == 0x55));
1672 if (reg & 0xff00) {
1673 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001674 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001675 outb_p(reg >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001676 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001677 }
Jean Delvare360782d2008-10-17 17:51:19 +02001678 outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
1679 res = inb_p(data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001680 if (word_sized) {
1681 outb_p((reg & 0xff) + 1,
Jean Delvare360782d2008-10-17 17:51:19 +02001682 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001683 res =
Jean Delvare360782d2008-10-17 17:51:19 +02001684 (res << 8) + inb_p(data->isa_addr +
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001685 W83781D_DATA_REG_OFFSET);
1686 }
1687 if (reg & 0xff00) {
1688 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001689 data->isa_addr + W83781D_ADDR_REG_OFFSET);
1690 outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001691 }
1692 return res;
1693}
1694
1695static void
1696w83781d_write_value_isa(struct w83781d_data *data, u16 reg, u16 value)
1697{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001698 int word_sized;
1699
1700 word_sized = (((reg & 0xff00) == 0x100)
1701 || ((reg & 0xff00) == 0x200))
1702 && (((reg & 0x00ff) == 0x53)
1703 || ((reg & 0x00ff) == 0x55));
1704 if (reg & 0xff00) {
1705 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001706 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001707 outb_p(reg >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001708 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001709 }
Jean Delvare360782d2008-10-17 17:51:19 +02001710 outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001711 if (word_sized) {
1712 outb_p(value >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001713 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001714 outb_p((reg & 0xff) + 1,
Jean Delvare360782d2008-10-17 17:51:19 +02001715 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001716 }
Jean Delvare360782d2008-10-17 17:51:19 +02001717 outb_p(value & 0xff, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001718 if (reg & 0xff00) {
1719 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001720 data->isa_addr + W83781D_ADDR_REG_OFFSET);
1721 outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001722 }
1723}
1724
Guenter Roeckaff6e002012-01-19 11:02:27 -08001725/*
1726 * The SMBus locks itself, usually, but nothing may access the Winbond between
1727 * bank switches. ISA access must always be locked explicitly!
1728 * We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1729 * would slow down the W83781D access and should not be necessary.
1730 * There are some ugly typecasts here, but the good news is - they should
1731 * nowhere else be necessary!
1732 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001733static int
1734w83781d_read_value(struct w83781d_data *data, u16 reg)
1735{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001736 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001737 int res;
1738
1739 mutex_lock(&data->lock);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001740 if (client)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001741 res = w83781d_read_value_i2c(data, reg);
1742 else
1743 res = w83781d_read_value_isa(data, reg);
1744 mutex_unlock(&data->lock);
1745 return res;
1746}
1747
1748static int
1749w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
1750{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001751 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001752
1753 mutex_lock(&data->lock);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001754 if (client)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001755 w83781d_write_value_i2c(data, reg, value);
1756 else
1757 w83781d_write_value_isa(data, reg, value);
1758 mutex_unlock(&data->lock);
1759 return 0;
1760}
1761
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001762static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001763w83781d_isa_probe(struct platform_device *pdev)
1764{
1765 int err, reg;
1766 struct w83781d_data *data;
1767 struct resource *res;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001768
1769 /* Reserve the ISA region */
1770 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck144d2b92012-06-02 11:48:00 -07001771 if (!devm_request_region(&pdev->dev,
1772 res->start + W83781D_ADDR_REG_OFFSET, 2,
1773 "w83781d"))
1774 return -EBUSY;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001775
Guenter Roeck144d2b92012-06-02 11:48:00 -07001776 data = devm_kzalloc(&pdev->dev, sizeof(struct w83781d_data),
1777 GFP_KERNEL);
1778 if (!data)
1779 return -ENOMEM;
1780
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001781 mutex_init(&data->lock);
Jean Delvare360782d2008-10-17 17:51:19 +02001782 data->isa_addr = res->start;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001783 platform_set_drvdata(pdev, data);
1784
1785 reg = w83781d_read_value(data, W83781D_REG_WCHIPID);
1786 switch (reg) {
1787 case 0x30:
1788 data->type = w83782d;
Jean Delvare360782d2008-10-17 17:51:19 +02001789 data->name = "w83782d";
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001790 break;
1791 default:
1792 data->type = w83781d;
Jean Delvare360782d2008-10-17 17:51:19 +02001793 data->name = "w83781d";
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001794 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001795
1796 /* Initialize the W83781D chip */
1797 w83781d_init_device(&pdev->dev);
1798
1799 /* Register sysfs hooks */
1800 err = w83781d_create_files(&pdev->dev, data->type, 1);
1801 if (err)
1802 goto exit_remove_files;
1803
1804 err = device_create_file(&pdev->dev, &dev_attr_name);
1805 if (err)
1806 goto exit_remove_files;
1807
1808 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1809 if (IS_ERR(data->hwmon_dev)) {
1810 err = PTR_ERR(data->hwmon_dev);
1811 goto exit_remove_files;
1812 }
1813
1814 return 0;
1815
1816 exit_remove_files:
Guenter Roeck79501332012-01-16 20:45:16 -08001817 w83781d_remove_files(&pdev->dev);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001818 device_remove_file(&pdev->dev, &dev_attr_name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001819 return err;
1820}
1821
Bill Pemberton281dfd02012-11-19 13:25:51 -05001822static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001823w83781d_isa_remove(struct platform_device *pdev)
1824{
1825 struct w83781d_data *data = platform_get_drvdata(pdev);
1826
1827 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck79501332012-01-16 20:45:16 -08001828 w83781d_remove_files(&pdev->dev);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001829 device_remove_file(&pdev->dev, &dev_attr_name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001830
1831 return 0;
1832}
1833
1834static struct platform_driver w83781d_isa_driver = {
1835 .driver = {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001836 .name = "w83781d",
1837 },
1838 .probe = w83781d_isa_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001839 .remove = w83781d_isa_remove,
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001840};
1841
Jean Delvare7666c132007-05-08 17:22:02 +02001842/* return 1 if a supported chip is found, 0 otherwise */
1843static int __init
1844w83781d_isa_found(unsigned short address)
1845{
1846 int val, save, found = 0;
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001847 int port;
Jean Delvare7666c132007-05-08 17:22:02 +02001848
Guenter Roeckaff6e002012-01-19 11:02:27 -08001849 /*
1850 * Some boards declare base+0 to base+7 as a PNP device, some base+4
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001851 * to base+7 and some base+5 to base+6. So we better request each port
Guenter Roeckaff6e002012-01-19 11:02:27 -08001852 * individually for the probing phase.
1853 */
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001854 for (port = address; port < address + W83781D_EXTENT; port++) {
1855 if (!request_region(port, 1, "w83781d")) {
Joe Perches1ca28212011-01-12 21:55:11 +01001856 pr_debug("Failed to request port 0x%x\n", port);
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001857 goto release;
1858 }
Jean Delvare2961cb22008-03-09 13:34:28 +01001859 }
Jean Delvare7666c132007-05-08 17:22:02 +02001860
1861#define REALLY_SLOW_IO
Guenter Roeckaff6e002012-01-19 11:02:27 -08001862 /*
1863 * We need the timeouts for at least some W83781D-like
1864 * chips. But only if we read 'undefined' registers.
1865 */
Jean Delvare7666c132007-05-08 17:22:02 +02001866 val = inb_p(address + 1);
1867 if (inb_p(address + 2) != val
1868 || inb_p(address + 3) != val
1869 || inb_p(address + 7) != val) {
Joe Perches1ca28212011-01-12 21:55:11 +01001870 pr_debug("Detection failed at step %d\n", 1);
Jean Delvare7666c132007-05-08 17:22:02 +02001871 goto release;
1872 }
1873#undef REALLY_SLOW_IO
1874
Guenter Roeckaff6e002012-01-19 11:02:27 -08001875 /*
1876 * We should be able to change the 7 LSB of the address port. The
1877 * MSB (busy flag) should be clear initially, set after the write.
1878 */
Jean Delvare7666c132007-05-08 17:22:02 +02001879 save = inb_p(address + W83781D_ADDR_REG_OFFSET);
1880 if (save & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001881 pr_debug("Detection failed at step %d\n", 2);
Jean Delvare7666c132007-05-08 17:22:02 +02001882 goto release;
1883 }
1884 val = ~save & 0x7f;
1885 outb_p(val, address + W83781D_ADDR_REG_OFFSET);
1886 if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
1887 outb_p(save, address + W83781D_ADDR_REG_OFFSET);
Joe Perches1ca28212011-01-12 21:55:11 +01001888 pr_debug("Detection failed at step %d\n", 3);
Jean Delvare7666c132007-05-08 17:22:02 +02001889 goto release;
1890 }
1891
1892 /* We found a device, now see if it could be a W83781D */
1893 outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
1894 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1895 if (val & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001896 pr_debug("Detection failed at step %d\n", 4);
Jean Delvare7666c132007-05-08 17:22:02 +02001897 goto release;
1898 }
1899 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1900 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1901 outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
1902 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1903 if ((!(save & 0x80) && (val != 0xa3))
1904 || ((save & 0x80) && (val != 0x5c))) {
Joe Perches1ca28212011-01-12 21:55:11 +01001905 pr_debug("Detection failed at step %d\n", 5);
Jean Delvare7666c132007-05-08 17:22:02 +02001906 goto release;
1907 }
1908 outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
1909 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1910 if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */
Joe Perches1ca28212011-01-12 21:55:11 +01001911 pr_debug("Detection failed at step %d\n", 6);
Jean Delvare7666c132007-05-08 17:22:02 +02001912 goto release;
1913 }
1914
1915 /* The busy flag should be clear again */
1916 if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001917 pr_debug("Detection failed at step %d\n", 7);
Jean Delvare7666c132007-05-08 17:22:02 +02001918 goto release;
1919 }
1920
1921 /* Determine the chip type */
1922 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1923 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1924 outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
1925 outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
1926 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1927 if ((val & 0xfe) == 0x10 /* W83781D */
Jean Delvare05663362007-11-30 23:51:24 +01001928 || val == 0x30) /* W83782D */
Jean Delvare7666c132007-05-08 17:22:02 +02001929 found = 1;
1930
1931 if (found)
Joe Perches1ca28212011-01-12 21:55:11 +01001932 pr_info("Found a %s chip at %#x\n",
Jean Delvare7666c132007-05-08 17:22:02 +02001933 val == 0x30 ? "W83782D" : "W83781D", (int)address);
1934
1935 release:
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001936 for (port--; port >= address; port--)
1937 release_region(port, 1);
Jean Delvare7666c132007-05-08 17:22:02 +02001938 return found;
1939}
1940
1941static int __init
1942w83781d_isa_device_add(unsigned short address)
1943{
1944 struct resource res = {
1945 .start = address,
Jean Delvare15bde2f2007-08-29 10:39:57 +02001946 .end = address + W83781D_EXTENT - 1,
Jean Delvare7666c132007-05-08 17:22:02 +02001947 .name = "w83781d",
1948 .flags = IORESOURCE_IO,
1949 };
1950 int err;
1951
1952 pdev = platform_device_alloc("w83781d", address);
1953 if (!pdev) {
1954 err = -ENOMEM;
Joe Perches1ca28212011-01-12 21:55:11 +01001955 pr_err("Device allocation failed\n");
Jean Delvare7666c132007-05-08 17:22:02 +02001956 goto exit;
1957 }
1958
1959 err = platform_device_add_resources(pdev, &res, 1);
1960 if (err) {
Joe Perches1ca28212011-01-12 21:55:11 +01001961 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare7666c132007-05-08 17:22:02 +02001962 goto exit_device_put;
1963 }
1964
1965 err = platform_device_add(pdev);
1966 if (err) {
Joe Perches1ca28212011-01-12 21:55:11 +01001967 pr_err("Device addition failed (%d)\n", err);
Jean Delvare7666c132007-05-08 17:22:02 +02001968 goto exit_device_put;
1969 }
1970
1971 return 0;
1972
1973 exit_device_put:
1974 platform_device_put(pdev);
1975 exit:
1976 pdev = NULL;
1977 return err;
1978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980static int __init
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001981w83781d_isa_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Jean Delvarefde09502005-07-19 23:51:07 +02001983 int res;
1984
Jean Delvare7666c132007-05-08 17:22:02 +02001985 if (w83781d_isa_found(isa_address)) {
1986 res = platform_driver_register(&w83781d_isa_driver);
1987 if (res)
Jean Delvarec6566202008-10-17 17:51:18 +02001988 goto exit;
Jean Delvare7666c132007-05-08 17:22:02 +02001989
1990 /* Sets global pdev as a side effect */
1991 res = w83781d_isa_device_add(isa_address);
1992 if (res)
1993 goto exit_unreg_isa_driver;
1994 }
Jean Delvarefde09502005-07-19 23:51:07 +02001995
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001996 return 0;
1997
1998exit_unreg_isa_driver:
1999 platform_driver_unregister(&w83781d_isa_driver);
2000exit:
2001 return res;
2002}
2003
Geert Uytterhoevendd56b632008-10-26 17:04:38 +01002004static void
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002005w83781d_isa_unregister(void)
2006{
2007 if (pdev) {
2008 platform_device_unregister(pdev);
2009 platform_driver_unregister(&w83781d_isa_driver);
2010 }
2011}
2012#else /* !CONFIG_ISA */
2013
2014static struct w83781d_data *w83781d_data_if_isa(void)
2015{
2016 return NULL;
2017}
2018
2019static int
2020w83781d_alias_detect(struct i2c_client *client, u8 chipid)
2021{
2022 return 0;
2023}
2024
2025static int
2026w83781d_read_value(struct w83781d_data *data, u16 reg)
2027{
2028 int res;
2029
2030 mutex_lock(&data->lock);
2031 res = w83781d_read_value_i2c(data, reg);
2032 mutex_unlock(&data->lock);
2033
2034 return res;
2035}
2036
2037static int
2038w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
2039{
2040 mutex_lock(&data->lock);
2041 w83781d_write_value_i2c(data, reg, value);
2042 mutex_unlock(&data->lock);
2043
2044 return 0;
2045}
2046
2047static int __init
2048w83781d_isa_register(void)
2049{
2050 return 0;
2051}
2052
Geert Uytterhoevendd56b632008-10-26 17:04:38 +01002053static void
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002054w83781d_isa_unregister(void)
2055{
2056}
2057#endif /* CONFIG_ISA */
2058
2059static int __init
2060sensors_w83781d_init(void)
2061{
2062 int res;
2063
Guenter Roeckaff6e002012-01-19 11:02:27 -08002064 /*
2065 * We register the ISA device first, so that we can skip the
2066 * registration of an I2C interface to the same device.
2067 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002068 res = w83781d_isa_register();
2069 if (res)
2070 goto exit;
2071
Jean Delvarec6566202008-10-17 17:51:18 +02002072 res = i2c_add_driver(&w83781d_driver);
2073 if (res)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002074 goto exit_unreg_isa;
Jean Delvarec6566202008-10-17 17:51:18 +02002075
Jean Delvarefde09502005-07-19 23:51:07 +02002076 return 0;
Jean Delvare7666c132007-05-08 17:22:02 +02002077
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002078 exit_unreg_isa:
2079 w83781d_isa_unregister();
Jean Delvare7666c132007-05-08 17:22:02 +02002080 exit:
2081 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084static void __exit
2085sensors_w83781d_exit(void)
2086{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002087 w83781d_isa_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 i2c_del_driver(&w83781d_driver);
2089}
2090
2091MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2092 "Philip Edelbrock <phil@netroedge.com>, "
2093 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2094MODULE_DESCRIPTION("W83781D driver");
2095MODULE_LICENSE("GPL");
2096
2097module_init(sensors_w83781d_init);
2098module_exit(sensors_w83781d_exit);