blob: b3579721265f7529d8823ac74a98396600db3d7d [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Guenter Roeckaff6e002012-01-19 11:02:27 -08003 * w83781d.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
5 * Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
6 * Philip Edelbrock <phil@netroedge.com>,
7 * and Mark Studebaker <mdsxyz123@yahoo.com>
Jean Delvare7c81c60f2014-01-29 20:40:08 +01008 * Copyright (c) 2007 - 2008 Jean Delvare <jdelvare@suse.de>
Guenter Roeckaff6e002012-01-19 11:02:27 -08009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11/*
Guenter Roeckaff6e002012-01-19 11:02:27 -080012 * Supports following chips:
13 *
Jean Delvare4101ece2012-11-05 21:54:40 +010014 * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
Guenter Roeckaff6e002012-01-19 11:02:27 -080015 * as99127f 7 3 0 3 0x31 0x12c3 yes no
16 * as99127f rev.2 (type_name = as99127f) 0x31 0x5ca3 yes no
17 * w83781d 7 3 0 3 0x10-1 0x5ca3 yes yes
18 * w83782d 9 3 2-4 3 0x30 0x5ca3 yes yes
19 * w83783s 5-6 3 2 1-2 0x40 0x5ca3 yes no
20 *
21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Joe Perches1ca28212011-01-12 21:55:11 +010023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/jiffies.h>
29#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040030#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020031#include <linux/hwmon-vid.h>
Jean Delvare34875332007-05-08 17:22:03 +020032#include <linux/hwmon-sysfs.h>
Jim Cromie311ce2e2006-09-24 21:22:52 +020033#include <linux/sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Wolfgang Grandegger443850c2008-10-17 17:51:18 +020037#ifdef CONFIG_ISA
38#include <linux/platform_device.h>
39#include <linux/ioport.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020040#include <linux/io.h>
Wolfgang Grandegger443850c2008-10-17 17:51:18 +020041#endif
42
43#include "lm75.h"
Jean Delvare7666c132007-05-08 17:22:02 +020044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050046static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
47 0x2e, 0x2f, I2C_CLIENT_END };
Jean Delvare3aed1982009-01-07 16:37:32 +010048
Jean Delvaree5e9f442009-12-14 21:17:27 +010049enum chips { w83781d, w83782d, w83783s, as99127f };
50
51/* Insmod parameters */
Jean Delvare3aed1982009-01-07 16:37:32 +010052static unsigned short force_subclients[4];
53module_param_array(force_subclients, short, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080054MODULE_PARM_DESC(force_subclients,
55 "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Rusty Russell90ab5ee2012-01-13 09:32:20 +103057static bool reset;
Jean Delvarefabddcd2006-02-05 23:26:51 +010058module_param(reset, bool, 0);
59MODULE_PARM_DESC(reset, "Set to one to reset chip on load");
60
Rusty Russell90ab5ee2012-01-13 09:32:20 +103061static bool init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062module_param(init, bool, 0);
63MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
64
65/* Constants specified below */
66
67/* Length of ISA address segment */
68#define W83781D_EXTENT 8
69
70/* Where are the ISA address/data registers relative to the base address */
71#define W83781D_ADDR_REG_OFFSET 5
72#define W83781D_DATA_REG_OFFSET 6
73
Jean Delvare34875332007-05-08 17:22:03 +020074/* The device registers */
75/* in nr from 0 to 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
77 (0x554 + (((nr) - 7) * 2)))
78#define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
79 (0x555 + (((nr) - 7) * 2)))
80#define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
81 (0x550 + (nr) - 7))
82
Jean Delvare34875332007-05-08 17:22:03 +020083/* fan nr from 0 to 2 */
84#define W83781D_REG_FAN_MIN(nr) (0x3b + (nr))
85#define W83781D_REG_FAN(nr) (0x28 + (nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#define W83781D_REG_BANK 0x4E
88#define W83781D_REG_TEMP2_CONFIG 0x152
89#define W83781D_REG_TEMP3_CONFIG 0x252
Jean Delvare34875332007-05-08 17:22:03 +020090/* temp nr from 1 to 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
92 ((nr == 2) ? (0x0150) : \
93 (0x27)))
94#define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
95 ((nr == 2) ? (0x153) : \
96 (0x3A)))
97#define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
98 ((nr == 2) ? (0x155) : \
99 (0x39)))
100
101#define W83781D_REG_CONFIG 0x40
Jean Delvarec7f5d7e2006-02-05 23:13:48 +0100102
103/* Interrupt status (W83781D, AS99127F) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#define W83781D_REG_ALARM1 0x41
105#define W83781D_REG_ALARM2 0x42
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jean Delvare05663362007-11-30 23:51:24 +0100107/* Real-time status (W83782D, W83783S) */
Jean Delvarec7f5d7e2006-02-05 23:13:48 +0100108#define W83782D_REG_ALARM1 0x459
109#define W83782D_REG_ALARM2 0x45A
110#define W83782D_REG_ALARM3 0x45B
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define W83781D_REG_BEEP_CONFIG 0x4D
113#define W83781D_REG_BEEP_INTS1 0x56
114#define W83781D_REG_BEEP_INTS2 0x57
115#define W83781D_REG_BEEP_INTS3 0x453 /* not on W83781D */
116
117#define W83781D_REG_VID_FANDIV 0x47
118
119#define W83781D_REG_CHIPID 0x49
120#define W83781D_REG_WCHIPID 0x58
121#define W83781D_REG_CHIPMAN 0x4F
122#define W83781D_REG_PIN 0x4B
123
124/* 782D/783S only */
125#define W83781D_REG_VBAT 0x5D
126
127/* PWM 782D (1-4) and 783S (1-2) only */
Jean Delvare34875332007-05-08 17:22:03 +0200128static const u8 W83781D_REG_PWM[] = { 0x5B, 0x5A, 0x5E, 0x5F };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define W83781D_REG_PWMCLK12 0x5C
130#define W83781D_REG_PWMCLK34 0x45C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132#define W83781D_REG_I2C_ADDR 0x48
133#define W83781D_REG_I2C_SUBADDR 0x4A
134
Guenter Roeckaff6e002012-01-19 11:02:27 -0800135/*
136 * The following are undocumented in the data sheets however we
137 * received the information in an email from Winbond tech support
138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* Sensor selection - not on 781d */
140#define W83781D_REG_SCFG1 0x5D
141static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
142
143#define W83781D_REG_SCFG2 0x59
144static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
145
146#define W83781D_DEFAULT_BETA 3435
147
Jean Delvare474d00a2007-05-08 17:22:03 +0200148/* Conversions */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800149#define IN_TO_REG(val) clamp_val(((val) + 8) / 16, 0, 255)
Jean Delvare474d00a2007-05-08 17:22:03 +0200150#define IN_FROM_REG(val) ((val) * 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static inline u8
153FAN_TO_REG(long rpm, int div)
154{
155 if (rpm == 0)
156 return 255;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800157 rpm = clamp_val(rpm, 1, 1000000);
158 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Jean Delvare474d00a2007-05-08 17:22:03 +0200161static inline long
162FAN_FROM_REG(u8 val, int div)
163{
164 if (val == 0)
165 return -1;
166 if (val == 255)
167 return 0;
168 return 1350000 / (val * div);
169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Guenter Roeck2a844c12013-01-09 08:09:34 -0800171#define TEMP_TO_REG(val) clamp_val((val) / 1000, -127, 128)
Jean Delvare474d00a2007-05-08 17:22:03 +0200172#define TEMP_FROM_REG(val) ((val) * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Guenter Roeckc531eb32012-01-15 09:19:16 -0800174#define BEEP_MASK_FROM_REG(val, type) ((type) == as99127f ? \
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200175 (~(val)) & 0x7fff : (val) & 0xff7fff)
Guenter Roeckc531eb32012-01-15 09:19:16 -0800176#define BEEP_MASK_TO_REG(val, type) ((type) == as99127f ? \
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200177 (~(val)) & 0x7fff : (val) & 0xff7fff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#define DIV_FROM_REG(val) (1 << (val))
180
181static inline u8
182DIV_TO_REG(long val, enum chips type)
183{
184 int i;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800185 val = clamp_val(val, 1,
186 ((type == w83781d || type == as99127f) ? 8 : 128)) >> 1;
Grant Coadyabc01922005-05-12 13:41:51 +1000187 for (i = 0; i < 7; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (val == 0)
189 break;
190 val >>= 1;
191 }
Jean Delvare474d00a2007-05-08 17:22:03 +0200192 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195struct w83781d_data {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200196 struct i2c_client *client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700197 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100198 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 enum chips type;
200
Jean Delvare360782d2008-10-17 17:51:19 +0200201 /* For ISA device only */
202 const char *name;
203 int isa_addr;
204
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100205 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +0300206 bool valid; /* true if following fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned long last_updated; /* In jiffies */
208
209 struct i2c_client *lm75[2]; /* for secondary I2C addresses */
210 /* array of 2 pointers to subclients */
211
212 u8 in[9]; /* Register value - 8 & 9 for 782D only */
213 u8 in_max[9]; /* Register value - 8 & 9 for 782D only */
214 u8 in_min[9]; /* Register value - 8 & 9 for 782D only */
215 u8 fan[3]; /* Register value */
216 u8 fan_min[3]; /* Register value */
Jean Delvare474d00a2007-05-08 17:22:03 +0200217 s8 temp; /* Register value */
218 s8 temp_max; /* Register value */
219 s8 temp_max_hyst; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 u16 temp_add[2]; /* Register value */
221 u16 temp_max_add[2]; /* Register value */
222 u16 temp_max_hyst_add[2]; /* Register value */
223 u8 fan_div[3]; /* Register encoding, shifted right */
224 u8 vid; /* Register encoding, combined */
225 u32 alarms; /* Register encoding, combined */
226 u32 beep_mask; /* Register encoding, combined */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 u8 pwm[4]; /* Register value */
Jean Delvare34875332007-05-08 17:22:03 +0200228 u8 pwm2_enable; /* Boolean */
Guenter Roeckaff6e002012-01-19 11:02:27 -0800229 u16 sens[3]; /*
230 * 782D/783S only.
231 * 1 = pentium diode; 2 = 3904 diode;
232 * 4 = thermistor
233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 u8 vrm;
235};
236
Wolfgang Grandegger443850c2008-10-17 17:51:18 +0200237static struct w83781d_data *w83781d_data_if_isa(void);
238static int w83781d_alias_detect(struct i2c_client *client, u8 chipid);
239
Jean Delvare31b8dc42007-05-08 17:22:03 +0200240static int w83781d_read_value(struct w83781d_data *data, u16 reg);
241static int w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static struct w83781d_data *w83781d_update_device(struct device *dev);
Jean Delvare7666c132007-05-08 17:22:02 +0200243static void w83781d_init_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/* following are the sysfs callback functions */
246#define show_in_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800247static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200248 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{ \
Jean Delvare34875332007-05-08 17:22:03 +0200250 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct w83781d_data *data = w83781d_update_device(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200252 return sprintf(buf, "%ld\n", \
253 (long)IN_FROM_REG(data->reg[attr->index])); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255show_in_reg(in);
256show_in_reg(in_min);
257show_in_reg(in_max);
258
259#define store_in_reg(REG, reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800260static ssize_t store_in_##reg(struct device *dev, struct device_attribute \
Jean Delvare34875332007-05-08 17:22:03 +0200261 *da, const char *buf, size_t count) \
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); \
Jean Delvare7666c132007-05-08 17:22:02 +0200264 struct w83781d_data *data = dev_get_drvdata(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200265 int nr = attr->index; \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800266 unsigned long val; \
267 int err = kstrtoul(buf, 10, &val); \
268 if (err) \
269 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 data->in_##reg[nr] = IN_TO_REG(val); \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800272 w83781d_write_value(data, W83781D_REG_IN_##REG(nr), \
273 data->in_##reg[nr]); \
274 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100275 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return count; \
277}
278store_in_reg(MIN, min);
279store_in_reg(MAX, max);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#define sysfs_in_offsets(offset) \
Jean Delvare34875332007-05-08 17:22:03 +0200282static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
283 show_in, NULL, offset); \
284static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
285 show_in_min, store_in_min, offset); \
286static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
287 show_in_max, store_in_max, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289sysfs_in_offsets(0);
290sysfs_in_offsets(1);
291sysfs_in_offsets(2);
292sysfs_in_offsets(3);
293sysfs_in_offsets(4);
294sysfs_in_offsets(5);
295sysfs_in_offsets(6);
296sysfs_in_offsets(7);
297sysfs_in_offsets(8);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#define show_fan_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800300static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200301 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{ \
Jean Delvare34875332007-05-08 17:22:03 +0200303 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct w83781d_data *data = w83781d_update_device(dev); \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800305 return sprintf(buf, "%ld\n", \
Jean Delvare34875332007-05-08 17:22:03 +0200306 FAN_FROM_REG(data->reg[attr->index], \
307 DIV_FROM_REG(data->fan_div[attr->index]))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309show_fan_reg(fan);
310show_fan_reg(fan_min);
311
312static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200313store_fan_min(struct device *dev, struct device_attribute *da,
314 const char *buf, size_t count)
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);
Jean Delvare7666c132007-05-08 17:22:02 +0200317 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200318 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800319 unsigned long val;
320 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Guenter Roeckc531eb32012-01-15 09:19:16 -0800322 err = kstrtoul(buf, 10, &val);
323 if (err)
324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100326 mutex_lock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200327 data->fan_min[nr] =
328 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare31b8dc42007-05-08 17:22:03 +0200329 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr),
Jean Delvare34875332007-05-08 17:22:03 +0200330 data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100332 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return count;
334}
335
Jean Delvare34875332007-05-08 17:22:03 +0200336static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
337static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
338 show_fan_min, store_fan_min, 0);
339static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
340static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
341 show_fan_min, store_fan_min, 1);
342static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
343static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
344 show_fan_min, store_fan_min, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#define show_temp_reg(reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800347static ssize_t show_##reg(struct device *dev, struct device_attribute *da, \
Jean Delvare34875332007-05-08 17:22:03 +0200348 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{ \
Jean Delvare34875332007-05-08 17:22:03 +0200350 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct w83781d_data *data = w83781d_update_device(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200352 int nr = attr->index; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800354 return sprintf(buf, "%d\n", \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
356 } else { /* TEMP1 */ \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800357 return sprintf(buf, "%ld\n", (long)TEMP_FROM_REG(data->reg)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } \
359}
360show_temp_reg(temp);
361show_temp_reg(temp_max);
362show_temp_reg(temp_max_hyst);
363
364#define store_temp_reg(REG, reg) \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800365static ssize_t store_temp_##reg(struct device *dev, \
Jean Delvare34875332007-05-08 17:22:03 +0200366 struct device_attribute *da, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{ \
Jean Delvare34875332007-05-08 17:22:03 +0200368 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); \
Jean Delvare7666c132007-05-08 17:22:02 +0200369 struct w83781d_data *data = dev_get_drvdata(dev); \
Jean Delvare34875332007-05-08 17:22:03 +0200370 int nr = attr->index; \
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200371 long val; \
Guenter Roeckc531eb32012-01-15 09:19:16 -0800372 int err = kstrtol(buf, 10, &val); \
373 if (err) \
374 return err; \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100375 mutex_lock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 \
377 if (nr >= 2) { /* TEMP2 and TEMP3 */ \
378 data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
Jean Delvare31b8dc42007-05-08 17:22:03 +0200379 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 data->temp_##reg##_add[nr-2]); \
381 } else { /* TEMP1 */ \
382 data->temp_##reg = TEMP_TO_REG(val); \
Jean Delvare31b8dc42007-05-08 17:22:03 +0200383 w83781d_write_value(data, W83781D_REG_TEMP_##REG(nr), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 data->temp_##reg); \
385 } \
386 \
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100387 mutex_unlock(&data->update_lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return count; \
389}
390store_temp_reg(OVER, max);
391store_temp_reg(HYST, max_hyst);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#define sysfs_temp_offsets(offset) \
Jean Delvare34875332007-05-08 17:22:03 +0200394static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
395 show_temp, NULL, offset); \
396static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
397 show_temp_max, store_temp_max, offset); \
398static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
399 show_temp_max_hyst, store_temp_max_hyst, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401sysfs_temp_offsets(1);
402sysfs_temp_offsets(2);
403sysfs_temp_offsets(3);
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100406cpu0_vid_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct w83781d_data *data = w83781d_update_device(dev);
409 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
410}
411
Julia Lawallb80b8142016-12-22 13:05:12 +0100412static DEVICE_ATTR_RO(cpu0_vid);
Jim Cromie311ce2e2006-09-24 21:22:52 +0200413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100415vrm_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jean Delvare90d66192007-10-08 18:24:35 +0200417 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return sprintf(buf, "%ld\n", (long) data->vrm);
419}
420
421static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100422vrm_store(struct device *dev, struct device_attribute *attr, const char *buf,
423 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Jean Delvare7666c132007-05-08 17:22:02 +0200425 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800426 unsigned long val;
427 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Guenter Roeckc531eb32012-01-15 09:19:16 -0800429 err = kstrtoul(buf, 10, &val);
430 if (err)
431 return err;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800432 data->vrm = clamp_val(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return count;
435}
436
Julia Lawallb80b8142016-12-22 13:05:12 +0100437static DEVICE_ATTR_RW(vrm);
Jim Cromie311ce2e2006-09-24 21:22:52 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100440alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare68188ba2005-05-16 18:52:38 +0200443 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Julia Lawallb80b8142016-12-22 13:05:12 +0100446static DEVICE_ATTR_RO(alarms);
Jim Cromie311ce2e2006-09-24 21:22:52 +0200447
Jean Delvare7d4a1372007-10-08 18:29:43 +0200448static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
449 char *buf)
450{
451 struct w83781d_data *data = w83781d_update_device(dev);
452 int bitnr = to_sensor_dev_attr(attr)->index;
453 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
454}
455
456/* The W83781D has a single alarm bit for temp2 and temp3 */
457static ssize_t show_temp3_alarm(struct device *dev,
458 struct device_attribute *attr, char *buf)
459{
460 struct w83781d_data *data = w83781d_update_device(dev);
461 int bitnr = (data->type == w83781d) ? 5 : 13;
462 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
463}
464
465static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
466static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
467static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
468static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
469static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
470static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
471static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10);
472static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16);
473static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17);
474static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
475static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
476static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
477static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
478static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
479static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_temp3_alarm, NULL, 0);
480
Julia Lawallb80b8142016-12-22 13:05:12 +0100481static ssize_t beep_mask_show(struct device *dev,
Guenter Roeckc531eb32012-01-15 09:19:16 -0800482 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct w83781d_data *data = w83781d_update_device(dev);
485 return sprintf(buf, "%ld\n",
486 (long)BEEP_MASK_FROM_REG(data->beep_mask, data->type));
487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100490beep_mask_store(struct device *dev, struct device_attribute *attr,
Jean Delvare34875332007-05-08 17:22:03 +0200491 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jean Delvare7666c132007-05-08 17:22:02 +0200493 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800494 unsigned long val;
495 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Guenter Roeckc531eb32012-01-15 09:19:16 -0800497 err = kstrtoul(buf, 10, &val);
498 if (err)
499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100501 mutex_lock(&data->update_lock);
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200502 data->beep_mask &= 0x8000; /* preserve beep enable */
503 data->beep_mask |= BEEP_MASK_TO_REG(val, data->type);
Jean Delvare34875332007-05-08 17:22:03 +0200504 w83781d_write_value(data, W83781D_REG_BEEP_INTS1,
505 data->beep_mask & 0xff);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200506 w83781d_write_value(data, W83781D_REG_BEEP_INTS2,
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200507 (data->beep_mask >> 8) & 0xff);
Jean Delvare34875332007-05-08 17:22:03 +0200508 if (data->type != w83781d && data->type != as99127f) {
509 w83781d_write_value(data, W83781D_REG_BEEP_INTS3,
510 ((data->beep_mask) >> 16) & 0xff);
511 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100512 mutex_unlock(&data->update_lock);
Jean Delvare34875332007-05-08 17:22:03 +0200513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return count;
515}
516
Julia Lawallb80b8142016-12-22 13:05:12 +0100517static DEVICE_ATTR_RW(beep_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Jean Delvare7d4a1372007-10-08 18:29:43 +0200519static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
520 char *buf)
521{
522 struct w83781d_data *data = w83781d_update_device(dev);
523 int bitnr = to_sensor_dev_attr(attr)->index;
524 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
525}
526
527static ssize_t
528store_beep(struct device *dev, struct device_attribute *attr,
529 const char *buf, size_t count)
530{
531 struct w83781d_data *data = dev_get_drvdata(dev);
532 int bitnr = to_sensor_dev_attr(attr)->index;
Jean Delvare7d4a1372007-10-08 18:29:43 +0200533 u8 reg;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800534 unsigned long bit;
535 int err;
Jean Delvare7d4a1372007-10-08 18:29:43 +0200536
Guenter Roeckc531eb32012-01-15 09:19:16 -0800537 err = kstrtoul(buf, 10, &bit);
538 if (err)
539 return err;
540
Jean Delvare7d4a1372007-10-08 18:29:43 +0200541 if (bit & ~1)
542 return -EINVAL;
543
544 mutex_lock(&data->update_lock);
545 if (bit)
546 data->beep_mask |= (1 << bitnr);
547 else
548 data->beep_mask &= ~(1 << bitnr);
549
550 if (bitnr < 8) {
551 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
552 if (bit)
553 reg |= (1 << bitnr);
554 else
555 reg &= ~(1 << bitnr);
556 w83781d_write_value(data, W83781D_REG_BEEP_INTS1, reg);
557 } else if (bitnr < 16) {
558 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
559 if (bit)
560 reg |= (1 << (bitnr - 8));
561 else
562 reg &= ~(1 << (bitnr - 8));
563 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, reg);
564 } else {
565 reg = w83781d_read_value(data, W83781D_REG_BEEP_INTS3);
566 if (bit)
567 reg |= (1 << (bitnr - 16));
568 else
569 reg &= ~(1 << (bitnr - 16));
570 w83781d_write_value(data, W83781D_REG_BEEP_INTS3, reg);
571 }
572 mutex_unlock(&data->update_lock);
573
574 return count;
575}
576
577/* The W83781D has a single beep bit for temp2 and temp3 */
578static ssize_t show_temp3_beep(struct device *dev,
579 struct device_attribute *attr, char *buf)
580{
581 struct w83781d_data *data = w83781d_update_device(dev);
582 int bitnr = (data->type == w83781d) ? 5 : 13;
583 return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
584}
585
586static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
587 show_beep, store_beep, 0);
588static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR,
589 show_beep, store_beep, 1);
590static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR,
591 show_beep, store_beep, 2);
592static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR,
593 show_beep, store_beep, 3);
594static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR,
595 show_beep, store_beep, 8);
596static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO | S_IWUSR,
597 show_beep, store_beep, 9);
598static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO | S_IWUSR,
599 show_beep, store_beep, 10);
600static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO | S_IWUSR,
601 show_beep, store_beep, 16);
602static SENSOR_DEVICE_ATTR(in8_beep, S_IRUGO | S_IWUSR,
603 show_beep, store_beep, 17);
604static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR,
605 show_beep, store_beep, 6);
606static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR,
607 show_beep, store_beep, 7);
608static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO | S_IWUSR,
609 show_beep, store_beep, 11);
610static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
611 show_beep, store_beep, 4);
612static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR,
613 show_beep, store_beep, 5);
614static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO,
615 show_temp3_beep, store_beep, 13);
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200616static SENSOR_DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
617 show_beep, store_beep, 15);
Jean Delvare7d4a1372007-10-08 18:29:43 +0200618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200620show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jean Delvare34875332007-05-08 17:22:03 +0200622 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct w83781d_data *data = w83781d_update_device(dev);
624 return sprintf(buf, "%ld\n",
Jean Delvare34875332007-05-08 17:22:03 +0200625 (long) DIV_FROM_REG(data->fan_div[attr->index]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Guenter Roeckaff6e002012-01-19 11:02:27 -0800628/*
629 * Note: we save and restore the fan minimum here, because its value is
630 * determined in part by the fan divisor. This follows the principle of
631 * least surprise; the user doesn't expect the fan minimum to change just
632 * because the divisor changed.
633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200635store_fan_div(struct device *dev, struct device_attribute *da,
636 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jean Delvare34875332007-05-08 17:22:03 +0200638 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200639 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned long min;
Jean Delvare34875332007-05-08 17:22:03 +0200641 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 u8 reg;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800643 unsigned long val;
644 int err;
645
646 err = kstrtoul(buf, 10, &val);
647 if (err)
648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100650 mutex_lock(&data->update_lock);
Jean Delvare293c09972007-11-30 23:52:44 +0100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Save fan_min */
653 min = FAN_FROM_REG(data->fan_min[nr],
654 DIV_FROM_REG(data->fan_div[nr]));
655
656 data->fan_div[nr] = DIV_TO_REG(val, data->type);
657
Guenter Roeckc531eb32012-01-15 09:19:16 -0800658 reg = (w83781d_read_value(data, nr == 2 ?
659 W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
660 & (nr == 0 ? 0xcf : 0x3f))
661 | ((data->fan_div[nr] & 0x03) << (nr == 0 ? 4 : 6));
662 w83781d_write_value(data, nr == 2 ?
663 W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* w83781d and as99127f don't have extended divisor bits */
666 if (data->type != w83781d && data->type != as99127f) {
Jean Delvare31b8dc42007-05-08 17:22:03 +0200667 reg = (w83781d_read_value(data, W83781D_REG_VBAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 & ~(1 << (5 + nr)))
669 | ((data->fan_div[nr] & 0x04) << (3 + nr));
Jean Delvare31b8dc42007-05-08 17:22:03 +0200670 w83781d_write_value(data, W83781D_REG_VBAT, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 /* Restore fan_min */
674 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare34875332007-05-08 17:22:03 +0200675 w83781d_write_value(data, W83781D_REG_FAN_MIN(nr), data->fan_min[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100677 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return count;
679}
680
Jean Delvare34875332007-05-08 17:22:03 +0200681static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
682 show_fan_div, store_fan_div, 0);
683static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
684 show_fan_div, store_fan_div, 1);
685static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR,
686 show_fan_div, store_fan_div, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200689show_pwm(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jean Delvare34875332007-05-08 17:22:03 +0200691 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200693 return sprintf(buf, "%d\n", (int)data->pwm[attr->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100697pwm2_enable_show(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200700 return sprintf(buf, "%d\n", (int)data->pwm2_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200704store_pwm(struct device *dev, struct device_attribute *da, const char *buf,
705 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Jean Delvare34875332007-05-08 17:22:03 +0200707 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200708 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200709 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800710 unsigned long val;
711 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Guenter Roeckc531eb32012-01-15 09:19:16 -0800713 err = kstrtoul(buf, 10, &val);
714 if (err)
715 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100717 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800718 data->pwm[nr] = clamp_val(val, 0, 255);
Jean Delvare34875332007-05-08 17:22:03 +0200719 w83781d_write_value(data, W83781D_REG_PWM[nr], data->pwm[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100720 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return count;
722}
723
724static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +0100725pwm2_enable_store(struct device *dev, struct device_attribute *da,
Jean Delvare34875332007-05-08 17:22:03 +0200726 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Jean Delvare7666c132007-05-08 17:22:02 +0200728 struct w83781d_data *data = dev_get_drvdata(dev);
Guenter Roeckc531eb32012-01-15 09:19:16 -0800729 unsigned long val;
730 u32 reg;
731 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Guenter Roeckc531eb32012-01-15 09:19:16 -0800733 err = kstrtoul(buf, 10, &val);
734 if (err)
735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100737 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 switch (val) {
740 case 0:
741 case 1:
Jean Delvare31b8dc42007-05-08 17:22:03 +0200742 reg = w83781d_read_value(data, W83781D_REG_PWMCLK12);
743 w83781d_write_value(data, W83781D_REG_PWMCLK12,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 (reg & 0xf7) | (val << 3));
745
Jean Delvare31b8dc42007-05-08 17:22:03 +0200746 reg = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
747 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 (reg & 0xef) | (!val << 4));
749
Jean Delvare34875332007-05-08 17:22:03 +0200750 data->pwm2_enable = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752
753 default:
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100754 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EINVAL;
756 }
757
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100758 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return count;
760}
761
Jean Delvare34875332007-05-08 17:22:03 +0200762static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 0);
763static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 1);
764static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 2);
765static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, store_pwm, 3);
766/* only PWM2 can be enabled/disabled */
Julia Lawallb80b8142016-12-22 13:05:12 +0100767static DEVICE_ATTR_RW(pwm2_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200770show_sensor(struct device *dev, struct device_attribute *da, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Jean Delvare34875332007-05-08 17:22:03 +0200772 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct w83781d_data *data = w83781d_update_device(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200774 return sprintf(buf, "%d\n", (int)data->sens[attr->index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777static ssize_t
Jean Delvare34875332007-05-08 17:22:03 +0200778store_sensor(struct device *dev, struct device_attribute *da,
779 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Jean Delvare34875332007-05-08 17:22:03 +0200781 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Jean Delvare7666c132007-05-08 17:22:02 +0200782 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare34875332007-05-08 17:22:03 +0200783 int nr = attr->index;
Guenter Roeckc531eb32012-01-15 09:19:16 -0800784 unsigned long val;
785 u32 tmp;
786 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Guenter Roeckc531eb32012-01-15 09:19:16 -0800788 err = kstrtoul(buf, 10, &val);
789 if (err)
790 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100792 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 switch (val) {
795 case 1: /* PII/Celeron diode */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200796 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
797 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200798 tmp | BIT_SCFG1[nr]);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200799 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
800 w83781d_write_value(data, W83781D_REG_SCFG2,
Jean Delvare34875332007-05-08 17:22:03 +0200801 tmp | BIT_SCFG2[nr]);
802 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804 case 2: /* 3904 */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200805 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
806 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200807 tmp | BIT_SCFG1[nr]);
Jean Delvare31b8dc42007-05-08 17:22:03 +0200808 tmp = w83781d_read_value(data, W83781D_REG_SCFG2);
809 w83781d_write_value(data, W83781D_REG_SCFG2,
Jean Delvare34875332007-05-08 17:22:03 +0200810 tmp & ~BIT_SCFG2[nr]);
811 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
Jean Delvareb26f9332007-08-16 14:30:01 +0200813 case W83781D_DEFAULT_BETA:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800814 dev_warn(dev,
815 "Sensor type %d is deprecated, please use 4 instead\n",
816 W83781D_DEFAULT_BETA);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500817 fallthrough;
Jean Delvareb26f9332007-08-16 14:30:01 +0200818 case 4: /* thermistor */
Jean Delvare31b8dc42007-05-08 17:22:03 +0200819 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
820 w83781d_write_value(data, W83781D_REG_SCFG1,
Jean Delvare34875332007-05-08 17:22:03 +0200821 tmp & ~BIT_SCFG1[nr]);
822 data->sens[nr] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
824 default:
Jean Delvareb26f9332007-08-16 14:30:01 +0200825 dev_err(dev, "Invalid sensor type %ld; must be 1, 2, or 4\n",
826 (long) val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 break;
828 }
829
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100830 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return count;
832}
833
Jean Delvare34875332007-05-08 17:22:03 +0200834static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
835 show_sensor, store_sensor, 0);
836static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
Mark M. Hoffman393cdad2007-08-09 08:12:46 -0400837 show_sensor, store_sensor, 1);
Jean Delvare34875332007-05-08 17:22:03 +0200838static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
Mark M. Hoffman393cdad2007-08-09 08:12:46 -0400839 show_sensor, store_sensor, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Guenter Roeckaff6e002012-01-19 11:02:27 -0800841/*
842 * Assumes that adapter is of I2C, not ISA variety.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * OTHERWISE DON'T CALL THIS
844 */
845static int
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200846w83781d_detect_subclients(struct i2c_client *new_client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int i, val1 = 0, id;
849 int err;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200850 int address = new_client->addr;
851 unsigned short sc_addr[2];
852 struct i2c_adapter *adapter = new_client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct w83781d_data *data = i2c_get_clientdata(new_client);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200854 enum chips kind = data->type;
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700855 int num_sc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 id = i2c_adapter_id(adapter);
858
859 if (force_subclients[0] == id && force_subclients[1] == address) {
860 for (i = 2; i <= 3; i++) {
861 if (force_subclients[i] < 0x48 ||
862 force_subclients[i] > 0x4f) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800863 dev_err(&new_client->dev,
864 "Invalid subclient address %d; must be 0x48-0x4f\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 force_subclients[i]);
866 err = -EINVAL;
867 goto ERROR_SC_1;
868 }
869 }
Jean Delvare31b8dc42007-05-08 17:22:03 +0200870 w83781d_write_value(data, W83781D_REG_I2C_SUBADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 (force_subclients[2] & 0x07) |
872 ((force_subclients[3] & 0x07) << 4));
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200873 sc_addr[0] = force_subclients[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 } else {
Jean Delvare31b8dc42007-05-08 17:22:03 +0200875 val1 = w83781d_read_value(data, W83781D_REG_I2C_SUBADDR);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200876 sc_addr[0] = 0x48 + (val1 & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 if (kind != w83783s) {
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700880 num_sc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (force_subclients[0] == id &&
882 force_subclients[1] == address) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200883 sc_addr[1] = force_subclients[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200885 sc_addr[1] = 0x48 + ((val1 >> 4) & 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200887 if (sc_addr[0] == sc_addr[1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dev_err(&new_client->dev,
889 "Duplicate addresses 0x%x for subclients.\n",
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200890 sc_addr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 err = -EBUSY;
892 goto ERROR_SC_2;
893 }
894 }
895
Guenter Roeckbbc8a562012-06-12 08:27:49 -0700896 for (i = 0; i < num_sc; i++) {
Wolfram Sang22e96ce2019-07-22 19:26:10 +0200897 data->lm75[i] = i2c_new_dummy_device(adapter, sc_addr[i]);
898 if (IS_ERR(data->lm75[i])) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800899 dev_err(&new_client->dev,
900 "Subclient %d registration at address 0x%x failed.\n",
901 i, sc_addr[i]);
Wolfram Sang22e96ce2019-07-22 19:26:10 +0200902 err = PTR_ERR(data->lm75[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (i == 1)
904 goto ERROR_SC_3;
905 goto ERROR_SC_2;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
909 return 0;
910
911/* Undo inits in case of errors */
912ERROR_SC_3:
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +0200913 i2c_unregister_device(data->lm75[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914ERROR_SC_2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915ERROR_SC_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return err;
917}
918
Jean Delvare34875332007-05-08 17:22:03 +0200919#define IN_UNIT_ATTRS(X) \
920 &sensor_dev_attr_in##X##_input.dev_attr.attr, \
921 &sensor_dev_attr_in##X##_min.dev_attr.attr, \
Jean Delvare293c09972007-11-30 23:52:44 +0100922 &sensor_dev_attr_in##X##_max.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200923 &sensor_dev_attr_in##X##_alarm.dev_attr.attr, \
924 &sensor_dev_attr_in##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200925
Jean Delvare34875332007-05-08 17:22:03 +0200926#define FAN_UNIT_ATTRS(X) \
927 &sensor_dev_attr_fan##X##_input.dev_attr.attr, \
928 &sensor_dev_attr_fan##X##_min.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200929 &sensor_dev_attr_fan##X##_div.dev_attr.attr, \
930 &sensor_dev_attr_fan##X##_alarm.dev_attr.attr, \
931 &sensor_dev_attr_fan##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200932
Jean Delvare34875332007-05-08 17:22:03 +0200933#define TEMP_UNIT_ATTRS(X) \
934 &sensor_dev_attr_temp##X##_input.dev_attr.attr, \
935 &sensor_dev_attr_temp##X##_max.dev_attr.attr, \
Jean Delvare7d4a1372007-10-08 18:29:43 +0200936 &sensor_dev_attr_temp##X##_max_hyst.dev_attr.attr, \
937 &sensor_dev_attr_temp##X##_alarm.dev_attr.attr, \
938 &sensor_dev_attr_temp##X##_beep.dev_attr.attr
Jim Cromie311ce2e2006-09-24 21:22:52 +0200939
Guenter Roeckc531eb32012-01-15 09:19:16 -0800940static struct attribute *w83781d_attributes[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200941 IN_UNIT_ATTRS(0),
942 IN_UNIT_ATTRS(2),
943 IN_UNIT_ATTRS(3),
944 IN_UNIT_ATTRS(4),
945 IN_UNIT_ATTRS(5),
946 IN_UNIT_ATTRS(6),
947 FAN_UNIT_ATTRS(1),
948 FAN_UNIT_ATTRS(2),
949 FAN_UNIT_ATTRS(3),
950 TEMP_UNIT_ATTRS(1),
951 TEMP_UNIT_ATTRS(2),
952 &dev_attr_cpu0_vid.attr,
953 &dev_attr_vrm.attr,
954 &dev_attr_alarms.attr,
955 &dev_attr_beep_mask.attr,
Jean Delvare2fbbbf12008-10-17 17:51:18 +0200956 &sensor_dev_attr_beep_enable.dev_attr.attr,
Jim Cromie311ce2e2006-09-24 21:22:52 +0200957 NULL
958};
959static const struct attribute_group w83781d_group = {
960 .attrs = w83781d_attributes,
961};
962
Guenter Roeck79501332012-01-16 20:45:16 -0800963static struct attribute *w83781d_attributes_in1[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200964 IN_UNIT_ATTRS(1),
Guenter Roeck79501332012-01-16 20:45:16 -0800965 NULL
966};
967static const struct attribute_group w83781d_group_in1 = {
968 .attrs = w83781d_attributes_in1,
969};
970
971static struct attribute *w83781d_attributes_in78[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200972 IN_UNIT_ATTRS(7),
973 IN_UNIT_ATTRS(8),
Guenter Roeck79501332012-01-16 20:45:16 -0800974 NULL
975};
976static const struct attribute_group w83781d_group_in78 = {
977 .attrs = w83781d_attributes_in78,
978};
979
980static struct attribute *w83781d_attributes_temp3[] = {
Jim Cromie311ce2e2006-09-24 21:22:52 +0200981 TEMP_UNIT_ATTRS(3),
Guenter Roeck79501332012-01-16 20:45:16 -0800982 NULL
983};
984static const struct attribute_group w83781d_group_temp3 = {
985 .attrs = w83781d_attributes_temp3,
986};
987
988static struct attribute *w83781d_attributes_pwm12[] = {
Jean Delvare34875332007-05-08 17:22:03 +0200989 &sensor_dev_attr_pwm1.dev_attr.attr,
990 &sensor_dev_attr_pwm2.dev_attr.attr,
Guenter Roeck79501332012-01-16 20:45:16 -0800991 &dev_attr_pwm2_enable.attr,
992 NULL
993};
994static const struct attribute_group w83781d_group_pwm12 = {
995 .attrs = w83781d_attributes_pwm12,
996};
997
998static struct attribute *w83781d_attributes_pwm34[] = {
Jean Delvare34875332007-05-08 17:22:03 +0200999 &sensor_dev_attr_pwm3.dev_attr.attr,
1000 &sensor_dev_attr_pwm4.dev_attr.attr,
Guenter Roeck79501332012-01-16 20:45:16 -08001001 NULL
1002};
1003static const struct attribute_group w83781d_group_pwm34 = {
1004 .attrs = w83781d_attributes_pwm34,
1005};
1006
1007static struct attribute *w83781d_attributes_other[] = {
Jean Delvare34875332007-05-08 17:22:03 +02001008 &sensor_dev_attr_temp1_type.dev_attr.attr,
1009 &sensor_dev_attr_temp2_type.dev_attr.attr,
1010 &sensor_dev_attr_temp3_type.dev_attr.attr,
Jim Cromie311ce2e2006-09-24 21:22:52 +02001011 NULL
1012};
Guenter Roeck79501332012-01-16 20:45:16 -08001013static const struct attribute_group w83781d_group_other = {
1014 .attrs = w83781d_attributes_other,
Jim Cromie311ce2e2006-09-24 21:22:52 +02001015};
1016
Jean Delvare7666c132007-05-08 17:22:02 +02001017/* No clean up is done on error, it's up to the caller */
1018static int
1019w83781d_create_files(struct device *dev, int kind, int is_isa)
1020{
1021 int err;
1022
Guenter Roeckc531eb32012-01-15 09:19:16 -08001023 err = sysfs_create_group(&dev->kobj, &w83781d_group);
1024 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001025 return err;
1026
1027 if (kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001028 err = sysfs_create_group(&dev->kobj, &w83781d_group_in1);
1029 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001030 return err;
1031 }
1032 if (kind != as99127f && kind != w83781d && kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001033 err = sysfs_create_group(&dev->kobj, &w83781d_group_in78);
1034 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001035 return err;
1036 }
1037 if (kind != w83783s) {
Guenter Roeck79501332012-01-16 20:45:16 -08001038 err = sysfs_create_group(&dev->kobj, &w83781d_group_temp3);
1039 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001040 return err;
Jean Delvare7d4a1372007-10-08 18:29:43 +02001041
Jean Delvare7768aa72007-10-25 13:11:01 +02001042 if (kind != w83781d) {
Jean Delvare7d4a1372007-10-08 18:29:43 +02001043 err = sysfs_chmod_file(&dev->kobj,
1044 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1045 S_IRUGO | S_IWUSR);
1046 if (err)
1047 return err;
Jean Delvare7768aa72007-10-25 13:11:01 +02001048 }
Jean Delvare7666c132007-05-08 17:22:02 +02001049 }
1050
1051 if (kind != w83781d && kind != as99127f) {
Guenter Roeck79501332012-01-16 20:45:16 -08001052 err = sysfs_create_group(&dev->kobj, &w83781d_group_pwm12);
1053 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001054 return err;
1055 }
1056 if (kind == w83782d && !is_isa) {
Guenter Roeck79501332012-01-16 20:45:16 -08001057 err = sysfs_create_group(&dev->kobj, &w83781d_group_pwm34);
1058 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001059 return err;
1060 }
1061
1062 if (kind != as99127f && kind != w83781d) {
Guenter Roeck79501332012-01-16 20:45:16 -08001063 err = device_create_file(dev,
1064 &sensor_dev_attr_temp1_type.dev_attr);
1065 if (err)
1066 return err;
1067 err = device_create_file(dev,
1068 &sensor_dev_attr_temp2_type.dev_attr);
1069 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001070 return err;
1071 if (kind != w83783s) {
Guenter Roeckc531eb32012-01-15 09:19:16 -08001072 err = device_create_file(dev,
Guenter Roeck79501332012-01-16 20:45:16 -08001073 &sensor_dev_attr_temp3_type.dev_attr);
Guenter Roeckc531eb32012-01-15 09:19:16 -08001074 if (err)
Jean Delvare7666c132007-05-08 17:22:02 +02001075 return err;
1076 }
1077 }
1078
Jean Delvare7666c132007-05-08 17:22:02 +02001079 return 0;
1080}
1081
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001082/* Return 0 if detection is successful, -ENODEV otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083static int
Jean Delvare310ec792009-12-14 21:17:23 +01001084w83781d_detect(struct i2c_client *client, struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Jean Delvarebab2bf42009-12-09 20:35:54 +01001086 int val1, val2;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001087 struct w83781d_data *isa = w83781d_data_if_isa();
1088 struct i2c_adapter *adapter = client->adapter;
1089 int address = client->addr;
Jean Delvarebab2bf42009-12-09 20:35:54 +01001090 const char *client_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 enum vendor { winbond, asus } vendid;
1092
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001093 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1094 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Guenter Roeckaff6e002012-01-19 11:02:27 -08001096 /*
1097 * We block updates of the ISA device to minimize the risk of
1098 * concurrent access to the same W83781D chip through different
1099 * interfaces.
1100 */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001101 if (isa)
1102 mutex_lock(&isa->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Jean Delvarebab2bf42009-12-09 20:35:54 +01001104 if (i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG) & 0x80) {
1105 dev_dbg(&adapter->dev,
1106 "Detection of w83781d chip failed at step 3\n");
1107 goto err_nodev;
1108 }
1109
1110 val1 = i2c_smbus_read_byte_data(client, W83781D_REG_BANK);
1111 val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
1112 /* Check for Winbond or Asus ID if in bank 0 */
1113 if (!(val1 & 0x07) &&
1114 ((!(val1 & 0x80) && val2 != 0xa3 && val2 != 0xc3) ||
Guenter Roeckc531eb32012-01-15 09:19:16 -08001115 ((val1 & 0x80) && val2 != 0x5c && val2 != 0x12))) {
Jean Delvarebab2bf42009-12-09 20:35:54 +01001116 dev_dbg(&adapter->dev,
1117 "Detection of w83781d chip failed at step 4\n");
1118 goto err_nodev;
1119 }
Guenter Roeckaff6e002012-01-19 11:02:27 -08001120 /*
1121 * If Winbond SMBus, check address at 0x48.
1122 * Asus doesn't support, except for as99127f rev.2
1123 */
Jean Delvarebab2bf42009-12-09 20:35:54 +01001124 if ((!(val1 & 0x80) && val2 == 0xa3) ||
Guenter Roeckc531eb32012-01-15 09:19:16 -08001125 ((val1 & 0x80) && val2 == 0x5c)) {
Jean Delvarebab2bf42009-12-09 20:35:54 +01001126 if (i2c_smbus_read_byte_data(client, W83781D_REG_I2C_ADDR)
1127 != address) {
1128 dev_dbg(&adapter->dev,
1129 "Detection of w83781d chip failed at step 5\n");
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001130 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Jean Delvarebab2bf42009-12-09 20:35:54 +01001134 /* Put it now into bank 0 and Vendor ID High Byte */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001135 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1136 (i2c_smbus_read_byte_data(client, W83781D_REG_BANK)
1137 & 0x78) | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jean Delvarebab2bf42009-12-09 20:35:54 +01001139 /* Get the vendor ID */
1140 val2 = i2c_smbus_read_byte_data(client, W83781D_REG_CHIPMAN);
1141 if (val2 == 0x5c)
1142 vendid = winbond;
1143 else if (val2 == 0x12)
1144 vendid = asus;
1145 else {
1146 dev_dbg(&adapter->dev,
1147 "w83781d chip vendor is neither Winbond nor Asus\n");
1148 goto err_nodev;
1149 }
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* Determine the chip type. */
Jean Delvarebab2bf42009-12-09 20:35:54 +01001152 val1 = i2c_smbus_read_byte_data(client, W83781D_REG_WCHIPID);
1153 if ((val1 == 0x10 || val1 == 0x11) && vendid == winbond)
1154 client_name = "w83781d";
1155 else if (val1 == 0x30 && vendid == winbond)
1156 client_name = "w83782d";
1157 else if (val1 == 0x40 && vendid == winbond && address == 0x2d)
1158 client_name = "w83783s";
1159 else if (val1 == 0x31)
1160 client_name = "as99127f";
1161 else
1162 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jean Delvarebab2bf42009-12-09 20:35:54 +01001164 if (val1 <= 0x30 && w83781d_alias_detect(client, val1)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001165 dev_dbg(&adapter->dev,
1166 "Device at 0x%02x appears to be the same as ISA device\n",
1167 address);
Jean Delvarebab2bf42009-12-09 20:35:54 +01001168 goto err_nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001171 if (isa)
1172 mutex_unlock(&isa->update_lock);
1173
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001174 strlcpy(info->type, client_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001176 return 0;
1177
1178 err_nodev:
1179 if (isa)
1180 mutex_unlock(&isa->update_lock);
1181 return -ENODEV;
1182}
1183
Guenter Roeck79501332012-01-16 20:45:16 -08001184static void w83781d_remove_files(struct device *dev)
1185{
1186 sysfs_remove_group(&dev->kobj, &w83781d_group);
1187 sysfs_remove_group(&dev->kobj, &w83781d_group_in1);
1188 sysfs_remove_group(&dev->kobj, &w83781d_group_in78);
1189 sysfs_remove_group(&dev->kobj, &w83781d_group_temp3);
1190 sysfs_remove_group(&dev->kobj, &w83781d_group_pwm12);
1191 sysfs_remove_group(&dev->kobj, &w83781d_group_pwm34);
1192 sysfs_remove_group(&dev->kobj, &w83781d_group_other);
1193}
1194
Stephen Kitt67487032020-08-13 18:02:22 +02001195static const struct i2c_device_id w83781d_ids[];
1196
1197static int w83781d_probe(struct i2c_client *client)
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001198{
1199 struct device *dev = &client->dev;
1200 struct w83781d_data *data;
1201 int err;
1202
Guenter Roeck144d2b92012-06-02 11:48:00 -07001203 data = devm_kzalloc(dev, sizeof(struct w83781d_data), GFP_KERNEL);
1204 if (!data)
1205 return -ENOMEM;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001206
1207 i2c_set_clientdata(client, data);
1208 mutex_init(&data->lock);
1209 mutex_init(&data->update_lock);
1210
Stephen Kitt67487032020-08-13 18:02:22 +02001211 data->type = i2c_match_id(w83781d_ids, client)->driver_data;
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001212 data->client = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /* attach secondary i2c lm75-like clients */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001215 err = w83781d_detect_subclients(client);
1216 if (err)
Guenter Roeck144d2b92012-06-02 11:48:00 -07001217 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* Initialize the chip */
Jean Delvare7666c132007-05-08 17:22:02 +02001220 w83781d_init_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* Register sysfs hooks */
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001223 err = w83781d_create_files(dev, data->type, 0);
Jean Delvare7666c132007-05-08 17:22:02 +02001224 if (err)
Guenter Roeck144d2b92012-06-02 11:48:00 -07001225 goto exit_remove_files;
Jim Cromie311ce2e2006-09-24 21:22:52 +02001226
Tony Jones1beeffe2007-08-20 13:46:20 -07001227 data->hwmon_dev = hwmon_device_register(dev);
1228 if (IS_ERR(data->hwmon_dev)) {
1229 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck144d2b92012-06-02 11:48:00 -07001230 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001231 }
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234
Guenter Roeck144d2b92012-06-02 11:48:00 -07001235 exit_remove_files:
Guenter Roeck79501332012-01-16 20:45:16 -08001236 w83781d_remove_files(dev);
Andy Shevchenko0ab21d0ed2017-10-31 16:21:43 +02001237 i2c_unregister_device(data->lm75[0]);
1238 i2c_unregister_device(data->lm75[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return err;
1240}
1241
1242static int
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001243w83781d_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001245 struct w83781d_data *data = i2c_get_clientdata(client);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001246 struct device *dev = &client->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001248 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck79501332012-01-16 20:45:16 -08001249 w83781d_remove_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Andy Shevchenko0ab21d0ed2017-10-31 16:21:43 +02001251 i2c_unregister_device(data->lm75[0]);
1252 i2c_unregister_device(data->lm75[1]);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001258w83781d_read_value_i2c(struct w83781d_data *data, u16 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001260 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001261 int res, bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct i2c_client *cl;
1263
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001264 bank = (reg >> 8) & 0x0f;
1265 if (bank > 2)
1266 /* switch banks */
1267 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1268 bank);
1269 if (bank == 0 || bank > 2) {
1270 res = i2c_smbus_read_byte_data(client, reg & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001272 /* switch to subclient */
1273 cl = data->lm75[bank - 1];
1274 /* convert from ISA to LM75 I2C addresses */
1275 switch (reg & 0xff) {
1276 case 0x50: /* TEMP */
Jean Delvare90f41022011-11-04 12:00:47 +01001277 res = i2c_smbus_read_word_swapped(cl, 0);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001278 break;
1279 case 0x52: /* CONFIG */
1280 res = i2c_smbus_read_byte_data(cl, 1);
1281 break;
1282 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +01001283 res = i2c_smbus_read_word_swapped(cl, 2);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001284 break;
1285 case 0x55: /* OVER */
1286 default:
Jean Delvare90f41022011-11-04 12:00:47 +01001287 res = i2c_smbus_read_word_swapped(cl, 3);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001291 if (bank > 2)
1292 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return res;
1295}
1296
1297static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001298w83781d_write_value_i2c(struct w83781d_data *data, u16 reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001300 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001301 int bank;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct i2c_client *cl;
1303
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001304 bank = (reg >> 8) & 0x0f;
1305 if (bank > 2)
1306 /* switch banks */
1307 i2c_smbus_write_byte_data(client, W83781D_REG_BANK,
1308 bank);
1309 if (bank == 0 || bank > 2) {
1310 i2c_smbus_write_byte_data(client, reg & 0xff,
1311 value & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 } else {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001313 /* switch to subclient */
1314 cl = data->lm75[bank - 1];
1315 /* convert from ISA to LM75 I2C addresses */
1316 switch (reg & 0xff) {
1317 case 0x52: /* CONFIG */
1318 i2c_smbus_write_byte_data(cl, 1, value & 0xff);
1319 break;
1320 case 0x53: /* HYST */
Jean Delvare90f41022011-11-04 12:00:47 +01001321 i2c_smbus_write_word_swapped(cl, 2, value);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001322 break;
1323 case 0x55: /* OVER */
Jean Delvare90f41022011-11-04 12:00:47 +01001324 i2c_smbus_write_word_swapped(cl, 3, value);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001328 if (bank > 2)
1329 i2c_smbus_write_byte_data(client, W83781D_REG_BANK, 0);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 0;
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static void
Jean Delvare7666c132007-05-08 17:22:02 +02001335w83781d_init_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Jean Delvare7666c132007-05-08 17:22:02 +02001337 struct w83781d_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int i, p;
1339 int type = data->type;
1340 u8 tmp;
1341
Guenter Roeckaff6e002012-01-19 11:02:27 -08001342 if (reset && type != as99127f) { /*
1343 * this resets registers we don't have
1344 * documentation for on the as99127f
1345 */
1346 /*
1347 * Resetting the chip has been the default for a long time,
1348 * but it causes the BIOS initializations (fan clock dividers,
1349 * thermal sensor types...) to be lost, so it is now optional.
1350 * It might even go away if nobody reports it as being useful,
1351 * as I see very little reason why this would be needed at
1352 * all.
1353 */
Guenter Roeckb55f3752013-01-10 10:01:24 -08001354 dev_info(dev,
1355 "If reset=1 solved a problem you were having, please report!\n");
Jean Delvarefabddcd2006-02-05 23:26:51 +01001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* save these registers */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001358 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1359 p = w83781d_read_value(data, W83781D_REG_PWMCLK12);
Guenter Roeckaff6e002012-01-19 11:02:27 -08001360 /*
1361 * Reset all except Watchdog values and last conversion values
1362 * This sets fan-divs to 2, among others
1363 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001364 w83781d_write_value(data, W83781D_REG_CONFIG, 0x80);
Guenter Roeckaff6e002012-01-19 11:02:27 -08001365 /*
1366 * Restore the registers and disable power-on abnormal beep.
1367 * This saves FAN 1/2/3 input/output values set by BIOS.
1368 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001369 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
1370 w83781d_write_value(data, W83781D_REG_PWMCLK12, p);
Guenter Roeckc531eb32012-01-15 09:19:16 -08001371 /*
1372 * Disable master beep-enable (reset turns it on).
1373 * Individual beep_mask should be reset to off but for some
1374 * reason disabling this bit helps some people not get beeped
1375 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001376 w83781d_write_value(data, W83781D_REG_BEEP_INTS2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Guenter Roeckaff6e002012-01-19 11:02:27 -08001379 /*
1380 * Disable power-on abnormal beep, as advised by the datasheet.
1381 * Already done if reset=1.
1382 */
Jean Delvarefabddcd2006-02-05 23:26:51 +01001383 if (init && !reset && type != as99127f) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001384 i = w83781d_read_value(data, W83781D_REG_BEEP_CONFIG);
1385 w83781d_write_value(data, W83781D_REG_BEEP_CONFIG, i | 0x80);
Jean Delvarefabddcd2006-02-05 23:26:51 +01001386 }
1387
Jean Delvare303760b2005-07-31 21:52:01 +02001388 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if ((type != w83781d) && (type != as99127f)) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001391 tmp = w83781d_read_value(data, W83781D_REG_SCFG1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 for (i = 1; i <= 3; i++) {
1393 if (!(tmp & BIT_SCFG1[i - 1])) {
Jean Delvareb26f9332007-08-16 14:30:01 +02001394 data->sens[i - 1] = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 } else {
1396 if (w83781d_read_value
Jean Delvare31b8dc42007-05-08 17:22:03 +02001397 (data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
1399 data->sens[i - 1] = 1;
1400 else
1401 data->sens[i - 1] = 2;
1402 }
Jean Delvare7c7a5302005-06-16 19:24:14 +02001403 if (type == w83783s && i == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 }
1406 }
1407
1408 if (init && type != as99127f) {
1409 /* Enable temp2 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001410 tmp = w83781d_read_value(data, W83781D_REG_TEMP2_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (tmp & 0x01) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001412 dev_warn(dev,
1413 "Enabling temp2, readings might not make sense\n");
Jean Delvare31b8dc42007-05-08 17:22:03 +02001414 w83781d_write_value(data, W83781D_REG_TEMP2_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 tmp & 0xfe);
1416 }
1417
1418 /* Enable temp3 */
Jean Delvare7c7a5302005-06-16 19:24:14 +02001419 if (type != w83783s) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001420 tmp = w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 W83781D_REG_TEMP3_CONFIG);
1422 if (tmp & 0x01) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001423 dev_warn(dev,
1424 "Enabling temp3, readings might not make sense\n");
Jean Delvare31b8dc42007-05-08 17:22:03 +02001425 w83781d_write_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
1427 }
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 /* Start monitoring */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001432 w83781d_write_value(data, W83781D_REG_CONFIG,
1433 (w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 W83781D_REG_CONFIG) & 0xf7)
1435 | 0x01);
Jean Delvare7666c132007-05-08 17:22:02 +02001436
1437 /* A few vars need to be filled upon startup */
Jean Delvare34875332007-05-08 17:22:03 +02001438 for (i = 0; i < 3; i++) {
1439 data->fan_min[i] = w83781d_read_value(data,
Jean Delvare7666c132007-05-08 17:22:02 +02001440 W83781D_REG_FAN_MIN(i));
1441 }
Jean Delvare7666c132007-05-08 17:22:02 +02001442
1443 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446static struct w83781d_data *w83781d_update_device(struct device *dev)
1447{
Jean Delvare7666c132007-05-08 17:22:02 +02001448 struct w83781d_data *data = dev_get_drvdata(dev);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001449 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int i;
1451
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001452 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1455 || !data->valid) {
1456 dev_dbg(dev, "Starting device update\n");
1457
1458 for (i = 0; i <= 8; i++) {
Jean Delvare7c7a5302005-06-16 19:24:14 +02001459 if (data->type == w83783s && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 continue; /* 783S has no in1 */
1461 data->in[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001462 w83781d_read_value(data, W83781D_REG_IN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 data->in_min[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001464 w83781d_read_value(data, W83781D_REG_IN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 data->in_max[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001466 w83781d_read_value(data, W83781D_REG_IN_MAX(i));
Jean Delvare05663362007-11-30 23:51:24 +01001467 if ((data->type != w83782d) && (i == 6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 break;
1469 }
Jean Delvare34875332007-05-08 17:22:03 +02001470 for (i = 0; i < 3; i++) {
1471 data->fan[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001472 w83781d_read_value(data, W83781D_REG_FAN(i));
Jean Delvare34875332007-05-08 17:22:03 +02001473 data->fan_min[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001474 w83781d_read_value(data, W83781D_REG_FAN_MIN(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476 if (data->type != w83781d && data->type != as99127f) {
Jean Delvare34875332007-05-08 17:22:03 +02001477 for (i = 0; i < 4; i++) {
1478 data->pwm[i] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001479 w83781d_read_value(data,
Jean Delvare34875332007-05-08 17:22:03 +02001480 W83781D_REG_PWM[i]);
Jean Delvare848ddf12009-05-08 20:27:28 +02001481 /* Only W83782D on SMBus has PWM3 and PWM4 */
1482 if ((data->type != w83782d || !client)
Jean Delvare34875332007-05-08 17:22:03 +02001483 && i == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
1485 }
1486 /* Only PWM2 can be disabled */
Jean Delvare34875332007-05-08 17:22:03 +02001487 data->pwm2_enable = (w83781d_read_value(data,
Guenter Roeckc531eb32012-01-15 09:19:16 -08001488 W83781D_REG_PWMCLK12) & 0x08) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Jean Delvare31b8dc42007-05-08 17:22:03 +02001491 data->temp = w83781d_read_value(data, W83781D_REG_TEMP(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 data->temp_max =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001493 w83781d_read_value(data, W83781D_REG_TEMP_OVER(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 data->temp_max_hyst =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001495 w83781d_read_value(data, W83781D_REG_TEMP_HYST(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 data->temp_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001497 w83781d_read_value(data, W83781D_REG_TEMP(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 data->temp_max_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001499 w83781d_read_value(data, W83781D_REG_TEMP_OVER(2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 data->temp_max_hyst_add[0] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001501 w83781d_read_value(data, W83781D_REG_TEMP_HYST(2));
Jean Delvare7c7a5302005-06-16 19:24:14 +02001502 if (data->type != w83783s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 data->temp_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001504 w83781d_read_value(data, W83781D_REG_TEMP(3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 data->temp_max_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001506 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 W83781D_REG_TEMP_OVER(3));
1508 data->temp_max_hyst_add[1] =
Jean Delvare31b8dc42007-05-08 17:22:03 +02001509 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 W83781D_REG_TEMP_HYST(3));
1511 }
Jean Delvare31b8dc42007-05-08 17:22:03 +02001512 i = w83781d_read_value(data, W83781D_REG_VID_FANDIV);
Jean Delvare7c7a5302005-06-16 19:24:14 +02001513 data->vid = i & 0x0f;
Jean Delvare31b8dc42007-05-08 17:22:03 +02001514 data->vid |= (w83781d_read_value(data,
Jean Delvare7c7a5302005-06-16 19:24:14 +02001515 W83781D_REG_CHIPID) & 0x01) << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 data->fan_div[0] = (i >> 4) & 0x03;
1517 data->fan_div[1] = (i >> 6) & 0x03;
Jean Delvare31b8dc42007-05-08 17:22:03 +02001518 data->fan_div[2] = (w83781d_read_value(data,
Jean Delvare7c7a5302005-06-16 19:24:14 +02001519 W83781D_REG_PIN) >> 6) & 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if ((data->type != w83781d) && (data->type != as99127f)) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001521 i = w83781d_read_value(data, W83781D_REG_VBAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 data->fan_div[0] |= (i >> 3) & 0x04;
1523 data->fan_div[1] |= (i >> 4) & 0x04;
Jean Delvare7c7a5302005-06-16 19:24:14 +02001524 data->fan_div[2] |= (i >> 5) & 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Jean Delvare05663362007-11-30 23:51:24 +01001526 if (data->type == w83782d) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001527 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001528 W83782D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001529 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001530 W83782D_REG_ALARM2) << 8)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001531 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001532 W83782D_REG_ALARM3) << 16);
1533 } else if (data->type == w83783s) {
Jean Delvare31b8dc42007-05-08 17:22:03 +02001534 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001535 W83782D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001536 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001537 W83782D_REG_ALARM2) << 8);
1538 } else {
Guenter Roeckaff6e002012-01-19 11:02:27 -08001539 /*
1540 * No real-time status registers, fall back to
1541 * interrupt status registers
1542 */
Jean Delvare31b8dc42007-05-08 17:22:03 +02001543 data->alarms = w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001544 W83781D_REG_ALARM1)
Jean Delvare31b8dc42007-05-08 17:22:03 +02001545 | (w83781d_read_value(data,
Jean Delvarec7f5d7e2006-02-05 23:13:48 +01001546 W83781D_REG_ALARM2) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Jean Delvare31b8dc42007-05-08 17:22:03 +02001548 i = w83781d_read_value(data, W83781D_REG_BEEP_INTS2);
Jean Delvare2fbbbf12008-10-17 17:51:18 +02001549 data->beep_mask = (i << 8) +
Jean Delvare31b8dc42007-05-08 17:22:03 +02001550 w83781d_read_value(data, W83781D_REG_BEEP_INTS1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if ((data->type != w83781d) && (data->type != as99127f)) {
1552 data->beep_mask |=
Jean Delvare31b8dc42007-05-08 17:22:03 +02001553 w83781d_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 W83781D_REG_BEEP_INTS3) << 16;
1555 }
1556 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +03001557 data->valid = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001560 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 return data;
1563}
1564
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001565static const struct i2c_device_id w83781d_ids[] = {
1566 { "w83781d", w83781d, },
1567 { "w83782d", w83782d, },
1568 { "w83783s", w83783s, },
1569 { "as99127f", as99127f },
1570 { /* LIST END */ }
1571};
1572MODULE_DEVICE_TABLE(i2c, w83781d_ids);
1573
Linus Walleij2284ed92021-07-30 01:05:43 +02001574static const struct of_device_id w83781d_of_match[] = {
1575 { .compatible = "winbond,w83781d" },
1576 { .compatible = "winbond,w83781g" },
1577 { .compatible = "winbond,w83782d" },
1578 { .compatible = "winbond,w83783s" },
1579 { .compatible = "asus,as99127f" },
1580 { },
1581};
1582MODULE_DEVICE_TABLE(of, w83781d_of_match);
1583
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001584static struct i2c_driver w83781d_driver = {
1585 .class = I2C_CLASS_HWMON,
1586 .driver = {
1587 .name = "w83781d",
Linus Walleij2284ed92021-07-30 01:05:43 +02001588 .of_match_table = w83781d_of_match,
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001589 },
Stephen Kitt67487032020-08-13 18:02:22 +02001590 .probe_new = w83781d_probe,
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001591 .remove = w83781d_remove,
1592 .id_table = w83781d_ids,
1593 .detect = w83781d_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01001594 .address_list = normal_i2c,
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001595};
1596
1597/*
1598 * ISA related code
1599 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001600#ifdef CONFIG_ISA
1601
1602/* ISA device, if found */
1603static struct platform_device *pdev;
1604
1605static unsigned short isa_address = 0x290;
1606
Guenter Roeckaff6e002012-01-19 11:02:27 -08001607/*
1608 * I2C devices get this name attribute automatically, but for ISA devices
1609 * we must create it by ourselves.
1610 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001611static ssize_t
Julia Lawallb80b8142016-12-22 13:05:12 +01001612name_show(struct device *dev, struct device_attribute *devattr, char *buf)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001613{
1614 struct w83781d_data *data = dev_get_drvdata(dev);
Jean Delvare360782d2008-10-17 17:51:19 +02001615 return sprintf(buf, "%s\n", data->name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001616}
Julia Lawallb80b8142016-12-22 13:05:12 +01001617static DEVICE_ATTR_RO(name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001618
1619static struct w83781d_data *w83781d_data_if_isa(void)
1620{
1621 return pdev ? platform_get_drvdata(pdev) : NULL;
1622}
1623
1624/* Returns 1 if the I2C chip appears to be an alias of the ISA chip */
1625static int w83781d_alias_detect(struct i2c_client *client, u8 chipid)
1626{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001627 struct w83781d_data *isa;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001628 int i;
1629
1630 if (!pdev) /* No ISA chip */
1631 return 0;
1632
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001633 isa = platform_get_drvdata(pdev);
1634
1635 if (w83781d_read_value(isa, W83781D_REG_I2C_ADDR) != client->addr)
1636 return 0; /* Address doesn't match */
1637 if (w83781d_read_value(isa, W83781D_REG_WCHIPID) != chipid)
1638 return 0; /* Chip type doesn't match */
1639
Guenter Roeckaff6e002012-01-19 11:02:27 -08001640 /*
1641 * We compare all the limit registers, the config register and the
1642 * interrupt mask registers
1643 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001644 for (i = 0x2b; i <= 0x3d; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001645 if (w83781d_read_value(isa, i) !=
1646 i2c_smbus_read_byte_data(client, i))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001647 return 0;
1648 }
1649 if (w83781d_read_value(isa, W83781D_REG_CONFIG) !=
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001650 i2c_smbus_read_byte_data(client, W83781D_REG_CONFIG))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001651 return 0;
1652 for (i = 0x43; i <= 0x46; i++) {
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001653 if (w83781d_read_value(isa, i) !=
1654 i2c_smbus_read_byte_data(client, i))
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001655 return 0;
1656 }
1657
1658 return 1;
1659}
1660
1661static int
1662w83781d_read_value_isa(struct w83781d_data *data, u16 reg)
1663{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001664 int word_sized, res;
1665
1666 word_sized = (((reg & 0xff00) == 0x100)
1667 || ((reg & 0xff00) == 0x200))
1668 && (((reg & 0x00ff) == 0x50)
1669 || ((reg & 0x00ff) == 0x53)
1670 || ((reg & 0x00ff) == 0x55));
1671 if (reg & 0xff00) {
1672 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001673 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001674 outb_p(reg >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001675 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001676 }
Jean Delvare360782d2008-10-17 17:51:19 +02001677 outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
1678 res = inb_p(data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001679 if (word_sized) {
1680 outb_p((reg & 0xff) + 1,
Jean Delvare360782d2008-10-17 17:51:19 +02001681 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001682 res =
Jean Delvare360782d2008-10-17 17:51:19 +02001683 (res << 8) + inb_p(data->isa_addr +
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001684 W83781D_DATA_REG_OFFSET);
1685 }
1686 if (reg & 0xff00) {
1687 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001688 data->isa_addr + W83781D_ADDR_REG_OFFSET);
1689 outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001690 }
1691 return res;
1692}
1693
1694static void
1695w83781d_write_value_isa(struct w83781d_data *data, u16 reg, u16 value)
1696{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001697 int word_sized;
1698
1699 word_sized = (((reg & 0xff00) == 0x100)
1700 || ((reg & 0xff00) == 0x200))
1701 && (((reg & 0x00ff) == 0x53)
1702 || ((reg & 0x00ff) == 0x55));
1703 if (reg & 0xff00) {
1704 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001705 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001706 outb_p(reg >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001707 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001708 }
Jean Delvare360782d2008-10-17 17:51:19 +02001709 outb_p(reg & 0xff, data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001710 if (word_sized) {
1711 outb_p(value >> 8,
Jean Delvare360782d2008-10-17 17:51:19 +02001712 data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001713 outb_p((reg & 0xff) + 1,
Jean Delvare360782d2008-10-17 17:51:19 +02001714 data->isa_addr + W83781D_ADDR_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001715 }
Jean Delvare360782d2008-10-17 17:51:19 +02001716 outb_p(value & 0xff, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001717 if (reg & 0xff00) {
1718 outb_p(W83781D_REG_BANK,
Jean Delvare360782d2008-10-17 17:51:19 +02001719 data->isa_addr + W83781D_ADDR_REG_OFFSET);
1720 outb_p(0, data->isa_addr + W83781D_DATA_REG_OFFSET);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001721 }
1722}
1723
Guenter Roeckaff6e002012-01-19 11:02:27 -08001724/*
1725 * The SMBus locks itself, usually, but nothing may access the Winbond between
1726 * bank switches. ISA access must always be locked explicitly!
1727 * We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
1728 * would slow down the W83781D access and should not be necessary.
1729 * There are some ugly typecasts here, but the good news is - they should
1730 * nowhere else be necessary!
1731 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001732static int
1733w83781d_read_value(struct w83781d_data *data, u16 reg)
1734{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001735 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001736 int res;
1737
1738 mutex_lock(&data->lock);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001739 if (client)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001740 res = w83781d_read_value_i2c(data, reg);
1741 else
1742 res = w83781d_read_value_isa(data, reg);
1743 mutex_unlock(&data->lock);
1744 return res;
1745}
1746
1747static int
1748w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
1749{
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001750 struct i2c_client *client = data->client;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001751
1752 mutex_lock(&data->lock);
Wolfgang Grandegger0217eae2008-10-17 17:51:19 +02001753 if (client)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001754 w83781d_write_value_i2c(data, reg, value);
1755 else
1756 w83781d_write_value_isa(data, reg, value);
1757 mutex_unlock(&data->lock);
1758 return 0;
1759}
1760
Bill Pemberton6c931ae2012-11-19 13:22:35 -05001761static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001762w83781d_isa_probe(struct platform_device *pdev)
1763{
1764 int err, reg;
1765 struct w83781d_data *data;
1766 struct resource *res;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001767
1768 /* Reserve the ISA region */
1769 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck144d2b92012-06-02 11:48:00 -07001770 if (!devm_request_region(&pdev->dev,
1771 res->start + W83781D_ADDR_REG_OFFSET, 2,
1772 "w83781d"))
1773 return -EBUSY;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001774
Guenter Roeck144d2b92012-06-02 11:48:00 -07001775 data = devm_kzalloc(&pdev->dev, sizeof(struct w83781d_data),
1776 GFP_KERNEL);
1777 if (!data)
1778 return -ENOMEM;
1779
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001780 mutex_init(&data->lock);
Jean Delvare360782d2008-10-17 17:51:19 +02001781 data->isa_addr = res->start;
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001782 platform_set_drvdata(pdev, data);
1783
1784 reg = w83781d_read_value(data, W83781D_REG_WCHIPID);
1785 switch (reg) {
1786 case 0x30:
1787 data->type = w83782d;
Jean Delvare360782d2008-10-17 17:51:19 +02001788 data->name = "w83782d";
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001789 break;
1790 default:
1791 data->type = w83781d;
Jean Delvare360782d2008-10-17 17:51:19 +02001792 data->name = "w83781d";
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001793 }
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001794
1795 /* Initialize the W83781D chip */
1796 w83781d_init_device(&pdev->dev);
1797
1798 /* Register sysfs hooks */
1799 err = w83781d_create_files(&pdev->dev, data->type, 1);
1800 if (err)
1801 goto exit_remove_files;
1802
1803 err = device_create_file(&pdev->dev, &dev_attr_name);
1804 if (err)
1805 goto exit_remove_files;
1806
1807 data->hwmon_dev = hwmon_device_register(&pdev->dev);
1808 if (IS_ERR(data->hwmon_dev)) {
1809 err = PTR_ERR(data->hwmon_dev);
1810 goto exit_remove_files;
1811 }
1812
1813 return 0;
1814
1815 exit_remove_files:
Guenter Roeck79501332012-01-16 20:45:16 -08001816 w83781d_remove_files(&pdev->dev);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001817 device_remove_file(&pdev->dev, &dev_attr_name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001818 return err;
1819}
1820
Bill Pemberton281dfd02012-11-19 13:25:51 -05001821static int
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001822w83781d_isa_remove(struct platform_device *pdev)
1823{
1824 struct w83781d_data *data = platform_get_drvdata(pdev);
1825
1826 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck79501332012-01-16 20:45:16 -08001827 w83781d_remove_files(&pdev->dev);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001828 device_remove_file(&pdev->dev, &dev_attr_name);
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001829
1830 return 0;
1831}
1832
1833static struct platform_driver w83781d_isa_driver = {
1834 .driver = {
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001835 .name = "w83781d",
1836 },
1837 .probe = w83781d_isa_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -05001838 .remove = w83781d_isa_remove,
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001839};
1840
Jean Delvare7666c132007-05-08 17:22:02 +02001841/* return 1 if a supported chip is found, 0 otherwise */
1842static int __init
1843w83781d_isa_found(unsigned short address)
1844{
1845 int val, save, found = 0;
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001846 int port;
Jean Delvare7666c132007-05-08 17:22:02 +02001847
Guenter Roeckaff6e002012-01-19 11:02:27 -08001848 /*
1849 * Some boards declare base+0 to base+7 as a PNP device, some base+4
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001850 * to base+7 and some base+5 to base+6. So we better request each port
Guenter Roeckaff6e002012-01-19 11:02:27 -08001851 * individually for the probing phase.
1852 */
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001853 for (port = address; port < address + W83781D_EXTENT; port++) {
1854 if (!request_region(port, 1, "w83781d")) {
Joe Perches1ca28212011-01-12 21:55:11 +01001855 pr_debug("Failed to request port 0x%x\n", port);
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001856 goto release;
1857 }
Jean Delvare2961cb22008-03-09 13:34:28 +01001858 }
Jean Delvare7666c132007-05-08 17:22:02 +02001859
1860#define REALLY_SLOW_IO
Guenter Roeckaff6e002012-01-19 11:02:27 -08001861 /*
1862 * We need the timeouts for at least some W83781D-like
1863 * chips. But only if we read 'undefined' registers.
1864 */
Jean Delvare7666c132007-05-08 17:22:02 +02001865 val = inb_p(address + 1);
1866 if (inb_p(address + 2) != val
1867 || inb_p(address + 3) != val
1868 || inb_p(address + 7) != val) {
Joe Perches1ca28212011-01-12 21:55:11 +01001869 pr_debug("Detection failed at step %d\n", 1);
Jean Delvare7666c132007-05-08 17:22:02 +02001870 goto release;
1871 }
1872#undef REALLY_SLOW_IO
1873
Guenter Roeckaff6e002012-01-19 11:02:27 -08001874 /*
1875 * We should be able to change the 7 LSB of the address port. The
1876 * MSB (busy flag) should be clear initially, set after the write.
1877 */
Jean Delvare7666c132007-05-08 17:22:02 +02001878 save = inb_p(address + W83781D_ADDR_REG_OFFSET);
1879 if (save & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001880 pr_debug("Detection failed at step %d\n", 2);
Jean Delvare7666c132007-05-08 17:22:02 +02001881 goto release;
1882 }
1883 val = ~save & 0x7f;
1884 outb_p(val, address + W83781D_ADDR_REG_OFFSET);
1885 if (inb_p(address + W83781D_ADDR_REG_OFFSET) != (val | 0x80)) {
1886 outb_p(save, address + W83781D_ADDR_REG_OFFSET);
Joe Perches1ca28212011-01-12 21:55:11 +01001887 pr_debug("Detection failed at step %d\n", 3);
Jean Delvare7666c132007-05-08 17:22:02 +02001888 goto release;
1889 }
1890
1891 /* We found a device, now see if it could be a W83781D */
1892 outb_p(W83781D_REG_CONFIG, address + W83781D_ADDR_REG_OFFSET);
1893 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1894 if (val & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001895 pr_debug("Detection failed at step %d\n", 4);
Jean Delvare7666c132007-05-08 17:22:02 +02001896 goto release;
1897 }
1898 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1899 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1900 outb_p(W83781D_REG_CHIPMAN, address + W83781D_ADDR_REG_OFFSET);
1901 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1902 if ((!(save & 0x80) && (val != 0xa3))
1903 || ((save & 0x80) && (val != 0x5c))) {
Joe Perches1ca28212011-01-12 21:55:11 +01001904 pr_debug("Detection failed at step %d\n", 5);
Jean Delvare7666c132007-05-08 17:22:02 +02001905 goto release;
1906 }
1907 outb_p(W83781D_REG_I2C_ADDR, address + W83781D_ADDR_REG_OFFSET);
1908 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1909 if (val < 0x03 || val > 0x77) { /* Not a valid I2C address */
Joe Perches1ca28212011-01-12 21:55:11 +01001910 pr_debug("Detection failed at step %d\n", 6);
Jean Delvare7666c132007-05-08 17:22:02 +02001911 goto release;
1912 }
1913
1914 /* The busy flag should be clear again */
1915 if (inb_p(address + W83781D_ADDR_REG_OFFSET) & 0x80) {
Joe Perches1ca28212011-01-12 21:55:11 +01001916 pr_debug("Detection failed at step %d\n", 7);
Jean Delvare7666c132007-05-08 17:22:02 +02001917 goto release;
1918 }
1919
1920 /* Determine the chip type */
1921 outb_p(W83781D_REG_BANK, address + W83781D_ADDR_REG_OFFSET);
1922 save = inb_p(address + W83781D_DATA_REG_OFFSET);
1923 outb_p(save & 0xf8, address + W83781D_DATA_REG_OFFSET);
1924 outb_p(W83781D_REG_WCHIPID, address + W83781D_ADDR_REG_OFFSET);
1925 val = inb_p(address + W83781D_DATA_REG_OFFSET);
1926 if ((val & 0xfe) == 0x10 /* W83781D */
Jean Delvare05663362007-11-30 23:51:24 +01001927 || val == 0x30) /* W83782D */
Jean Delvare7666c132007-05-08 17:22:02 +02001928 found = 1;
1929
1930 if (found)
Joe Perches1ca28212011-01-12 21:55:11 +01001931 pr_info("Found a %s chip at %#x\n",
Jean Delvare7666c132007-05-08 17:22:02 +02001932 val == 0x30 ? "W83782D" : "W83781D", (int)address);
1933
1934 release:
Jean Delvareb0bcdd32010-02-05 19:58:36 +01001935 for (port--; port >= address; port--)
1936 release_region(port, 1);
Jean Delvare7666c132007-05-08 17:22:02 +02001937 return found;
1938}
1939
1940static int __init
1941w83781d_isa_device_add(unsigned short address)
1942{
1943 struct resource res = {
1944 .start = address,
Jean Delvare15bde2f2007-08-29 10:39:57 +02001945 .end = address + W83781D_EXTENT - 1,
Jean Delvare7666c132007-05-08 17:22:02 +02001946 .name = "w83781d",
1947 .flags = IORESOURCE_IO,
1948 };
1949 int err;
1950
1951 pdev = platform_device_alloc("w83781d", address);
1952 if (!pdev) {
1953 err = -ENOMEM;
Joe Perches1ca28212011-01-12 21:55:11 +01001954 pr_err("Device allocation failed\n");
Jean Delvare7666c132007-05-08 17:22:02 +02001955 goto exit;
1956 }
1957
1958 err = platform_device_add_resources(pdev, &res, 1);
1959 if (err) {
Joe Perches1ca28212011-01-12 21:55:11 +01001960 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare7666c132007-05-08 17:22:02 +02001961 goto exit_device_put;
1962 }
1963
1964 err = platform_device_add(pdev);
1965 if (err) {
Joe Perches1ca28212011-01-12 21:55:11 +01001966 pr_err("Device addition failed (%d)\n", err);
Jean Delvare7666c132007-05-08 17:22:02 +02001967 goto exit_device_put;
1968 }
1969
1970 return 0;
1971
1972 exit_device_put:
1973 platform_device_put(pdev);
1974 exit:
1975 pdev = NULL;
1976 return err;
1977}
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979static int __init
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001980w83781d_isa_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Jean Delvarefde09502005-07-19 23:51:07 +02001982 int res;
1983
Jean Delvare7666c132007-05-08 17:22:02 +02001984 if (w83781d_isa_found(isa_address)) {
1985 res = platform_driver_register(&w83781d_isa_driver);
1986 if (res)
Jean Delvarec6566202008-10-17 17:51:18 +02001987 goto exit;
Jean Delvare7666c132007-05-08 17:22:02 +02001988
1989 /* Sets global pdev as a side effect */
1990 res = w83781d_isa_device_add(isa_address);
1991 if (res)
1992 goto exit_unreg_isa_driver;
1993 }
Jean Delvarefde09502005-07-19 23:51:07 +02001994
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02001995 return 0;
1996
1997exit_unreg_isa_driver:
1998 platform_driver_unregister(&w83781d_isa_driver);
1999exit:
2000 return res;
2001}
2002
Geert Uytterhoevendd56b632008-10-26 17:04:38 +01002003static void
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002004w83781d_isa_unregister(void)
2005{
2006 if (pdev) {
2007 platform_device_unregister(pdev);
2008 platform_driver_unregister(&w83781d_isa_driver);
2009 }
2010}
2011#else /* !CONFIG_ISA */
2012
2013static struct w83781d_data *w83781d_data_if_isa(void)
2014{
2015 return NULL;
2016}
2017
2018static int
2019w83781d_alias_detect(struct i2c_client *client, u8 chipid)
2020{
2021 return 0;
2022}
2023
2024static int
2025w83781d_read_value(struct w83781d_data *data, u16 reg)
2026{
2027 int res;
2028
2029 mutex_lock(&data->lock);
2030 res = w83781d_read_value_i2c(data, reg);
2031 mutex_unlock(&data->lock);
2032
2033 return res;
2034}
2035
2036static int
2037w83781d_write_value(struct w83781d_data *data, u16 reg, u16 value)
2038{
2039 mutex_lock(&data->lock);
2040 w83781d_write_value_i2c(data, reg, value);
2041 mutex_unlock(&data->lock);
2042
2043 return 0;
2044}
2045
2046static int __init
2047w83781d_isa_register(void)
2048{
2049 return 0;
2050}
2051
Geert Uytterhoevendd56b632008-10-26 17:04:38 +01002052static void
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002053w83781d_isa_unregister(void)
2054{
2055}
2056#endif /* CONFIG_ISA */
2057
2058static int __init
2059sensors_w83781d_init(void)
2060{
2061 int res;
2062
Guenter Roeckaff6e002012-01-19 11:02:27 -08002063 /*
2064 * We register the ISA device first, so that we can skip the
2065 * registration of an I2C interface to the same device.
2066 */
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002067 res = w83781d_isa_register();
2068 if (res)
2069 goto exit;
2070
Jean Delvarec6566202008-10-17 17:51:18 +02002071 res = i2c_add_driver(&w83781d_driver);
2072 if (res)
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002073 goto exit_unreg_isa;
Jean Delvarec6566202008-10-17 17:51:18 +02002074
Jean Delvarefde09502005-07-19 23:51:07 +02002075 return 0;
Jean Delvare7666c132007-05-08 17:22:02 +02002076
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002077 exit_unreg_isa:
2078 w83781d_isa_unregister();
Jean Delvare7666c132007-05-08 17:22:02 +02002079 exit:
2080 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
2083static void __exit
2084sensors_w83781d_exit(void)
2085{
Wolfgang Grandegger443850c2008-10-17 17:51:18 +02002086 w83781d_isa_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 i2c_del_driver(&w83781d_driver);
2088}
2089
2090MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
2091 "Philip Edelbrock <phil@netroedge.com>, "
2092 "and Mark Studebaker <mdsxyz123@yahoo.com>");
2093MODULE_DESCRIPTION("W83781D driver");
2094MODULE_LICENSE("GPL");
2095
2096module_init(sensors_w83781d_init);
2097module_exit(sensors_w83781d_exit);